Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

reviewTurnDiffs.test.ts

129 lines4.5 KB

1import { describe, expect, it } from "@effect/vitest";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import { AtomRegistry } from "effect/reactivity";
5​
6import { buildReviewParsedDiff, reviewTurnDiffsBySectionId } from "../features/review/reviewModel";
7import {
8 lastReviewTurnDiffsAtom,
9 recordReviewTurnDiffAnswer,
10 reviewTurnDiffText,
11} from "./reviewTurnDiffs";
12​
13const environmentId = EnvironmentId.make("env-1");
14const threadId = ThreadId.make("thread-1");
15​
16const answer = (
17 registry: AtomRegistry.AtomRegistry,
18 range: { fromTurnCount: number; toTurnCount: number; diff: string },
19 ignoreWhitespace = false,
20) =>
21 recordReviewTurnDiffAnswer(
22 { environmentId, input: { ignoreWhitespace } },
23 { threadId, ...range },
24 registry,
25 );
26​
27const modified = (path: string, line: string) =>
28 [
29 `diff --git a/${path} b/${path}`,
30 "index 1111111..2222222 100644",
31 `--- a/${path}`,
32 `+++ b/${path}`,
33 "@@ -1 +1,2 @@",
34 " const before = 1;",
35 `+${line}`,
36 "",
37 ].join("\n");
38​
39describe("reviewTurnDiffText", () => {
40 it("leaves a thread without repositories its one patch", () => {
41 const diff = modified("src/index.ts", "const root = 1;");
42 expect(reviewTurnDiffText({ diff })).toBe(diff);
43 expect(reviewTurnDiffText({ diff, repositories: [] })).toBe(diff);
44 });
45​
46 it("names each repository's files from the thread's folder", () => {
47 const text = reviewTurnDiffText({
48 diff: "",
49 repositories: [
50 {
51 name: "api",
52 repositoryPath: "/work/api",
53 relativePath: "api",
54 diff: [
55 modified("src/index.ts", "--- a/not-a-header"),
56 "diff --git a/added.ts b/added.ts",
57 "new file mode 100644",
58 "index 0000000..2222222",
59 "--- /dev/null",
60 "+++ b/added.ts",
61 "@@ -0,0 +1 @@",
62 "+export {};",
63 "diff --git a/old name.ts b/new name.ts",
64 "similarity index 100%",
65 "rename from old name.ts",
66 "rename to new name.ts",
67 "",
68 ].join("\n"),
69 },
70 {
71 name: "web",
72 repositoryPath: "/elsewhere/web",
73 relativePath: "../elsewhere/web",
74 diff: `${modified("src/index.ts", "const web = 1;")}\n[truncated]`,
75 },
76 ],
77 });
78​
79 expect(text).toContain("+--- a/not-a-header\n");
80 expect(text).toContain("rename from api/old name.ts\nrename to api/new name.ts\n");
81 expect(text.endsWith("\n\n[truncated]")).toBe(true);
82 const complete = buildReviewParsedDiff(text.replace("[truncated]", ""), "unit-repositories");
83 expect(complete.kind === "files" && complete.files.map((file) => file.path)).toEqual([
84 "api/src/index.ts",
85 "api/added.ts",
86 "api/new name.ts",
87 "../elsewhere/web/src/index.ts",
88 ]);
89 expect(
90 complete.kind === "files" && complete.files[1]?.completePatchSource?.().newContents,
91 ).toBe("export {};\n");
92 expect(buildReviewParsedDiff(text, "unit-repositories-truncated")).toMatchObject({
93 kind: "files",
94 fileCount: 4,
95 notice: expect.stringContaining("server size cap"),
96 });
97 });
98});
99​
100describe("recordReviewTurnDiffAnswer", () => {
101 it.effect("keeps loaded turn diffs for review after their queries are gone", () =>
102 Effect.gen(function* () {
103 const registry = AtomRegistry.make();
104 yield* answer(registry, { fromTurnCount: 0, toTurnCount: 1, diff: "turn one" });
105 yield* answer(registry, { fromTurnCount: 1, toTurnCount: 2, diff: "turn two" });
106​
107 // A disconnected sheet mounts no turn query; every loaded turn still has its diff.
108 const cached = registry.get(lastReviewTurnDiffsAtom(environmentId, threadId));
109 expect(
110 reviewTurnDiffsBySectionId(
111 [{ checkpointTurnCount: 1 }, { checkpointTurnCount: 2 }, { checkpointTurnCount: 3 }],
112 cached,
113 ),
114 ).toEqual({ "turn:1": "turn one", "turn:2": "turn two", "turn:3": undefined });
115 registry.dispose();
116 }),
117 );
118​
119 it.effect("ignores ranges and whitespace-ignoring reads, which no review section shows", () =>
120 Effect.gen(function* () {
121 const registry = AtomRegistry.make();
122 yield* answer(registry, { fromTurnCount: 0, toTurnCount: 3, diff: "whole thread" });
123 yield* answer(registry, { fromTurnCount: 1, toTurnCount: 2, diff: "no whitespace" }, true);
124​
125 expect(registry.get(lastReviewTurnDiffsAtom(environmentId, threadId))).toEqual({});
126 registry.dispose();
127 }),
128 );
129});