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

threadRepositories.test.ts

693 lines24.5 KB

1import {
2 CheckpointId,
3 CheckpointRef,
4 CheckpointScopeId,
5 NodeId,
6 RunId,
7 ThreadId,
8 type OrchestrationV2Checkpoint,
9 type OrchestrationV2CheckpointScope,
10} from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12import { describe, expect, it } from "vite-plus/test";
13​
14import { deriveThreadCheckpointSummaries } from "./threadCheckpoints.ts";
15import {
16 addableTaskFolderRepositories,
17 deriveTurnRepositoryChanges,
18 pinnableThreadRepositories,
19 pinThreadRepository,
20 repositoryCheckpointScopeIds,
21 repositoryFilePath,
22 repositorySectionForPath,
23 resolveWorktreeRepositorySelection,
24 rewindFileRestore,
25 THREAD_REPOSITORY_SECTIONS_VISIBLE,
26 isRelativePathInsideFolder,
27 threadChangedRepositories,
28 threadGitRepositories,
29 threadRepositoryScopes,
30 threadRepositorySections,
31 unpinThreadRepository,
32 turnChanges,
33 turnDiffSections,
34 turnHasChanges,
35 worktreeRepositoryChoices,
36} from "./threadRepositories.ts";
37​
38const now = DateTime.makeUnsafe("2026-11-10T00:00:00.000Z");
39const rootScopeId = CheckpointScopeId.make("scope-root");
40​
41function scope(
42 id: string,
43 cwd: string,
44 repository?: { readonly label?: string; readonly path?: string },
45): OrchestrationV2CheckpointScope {
46 return {
47 id: CheckpointScopeId.make(id),
48 threadId: ThreadId.make("thread"),
49 runId: null,
50 nodeId: NodeId.make("node"),
51 parentScopeId: repository === undefined ? null : rootScopeId,
52 providerThreadId: null,
53 kind: repository === undefined ? "root_run" : "manual",
54 ordinalWithinParent: 0,
55 advancesAppRunCount: repository === undefined,
56 cwd,
57 ...(repository?.label === undefined ? {} : { label: repository.label }),
58 ...(repository === undefined ? {} : { repositoryPath: repository.path ?? cwd }),
59 createdAt: now,
60 };
62​
63function checkpoint(
64 id: string,
65 scopeId: string,
66 ordinal: number,
67 overrides: Partial<OrchestrationV2Checkpoint> = {},
68): OrchestrationV2Checkpoint {
69 return {
70 id: CheckpointId.make(id),
71 threadId: ThreadId.make("thread"),
72 scopeId: CheckpointScopeId.make(scopeId),
73 runId: RunId.make(`run-${ordinal}`),
74 nodeId: NodeId.make("node"),
75 parentCheckpointId: null,
76 ordinalWithinScope: ordinal,
77 appRunOrdinal: null,
78 ref: CheckpointRef.make(`refs/t3/${id}`),
79 status: "ready",
80 files: [],
81 capturedAt: now,
82 ...overrides,
83 };
85​
86const file = (path: string, additions = 1, deletions = 0) => ({
87 path,
88 kind: "modified",
89 additions,
90 deletions,
91});
92​
93const scopes = [
94 scope("scope-root", "/work/wrapper"),
95 scope("scope-web", "/work/wrapper/web", { label: "web" }),
96 scope("scope-api", "/work/wrapper/services/api", { label: "api" }),
97 scope("scope-shared", "/work/shared", { label: "api" }),
98];
99​
100// A plain wrapper folder: the thread's own checkpoint is missing on every turn.
101const checkpoints = [
102 checkpoint("root-1", "scope-root", 1, { status: "missing", appRunOrdinal: 1 }),
103 checkpoint("api-0", "scope-api", 0, { runId: null }),
104 checkpoint("api-1", "scope-api", 1, { files: [file("src/index.ts", 4, 2)] }),
105 checkpoint("web-0", "scope-web", 0, { runId: null, status: "missing" }),
106 checkpoint("web-1", "scope-web", 1),
107 checkpoint("root-2", "scope-root", 2, { status: "missing", appRunOrdinal: 2 }),
108 checkpoint("shared-1", "scope-shared", 1, { runId: null }),
109 checkpoint("shared-2", "scope-shared", 2, {
110 files: [file("a.ts")],
111 filesTotal: { count: 300, additions: 900, deletions: 30 },
112 }),
113];
114​
115describe("threadRepositoryScopes", () => {
116 it("lists repository scopes by name, then path, relative to the thread's folder", () => {
117 expect(
118 threadRepositoryScopes({ checkpointScopes: scopes }).map((entry) => [
119 entry.name,
120 entry.relativePath,
121 entry.cwd,
122 ]),
123 ).toEqual([
124 ["api", "../shared", "/work/shared"],
125 ["api", "services/api", "/work/wrapper/services/api"],
126 ["web", "web", "/work/wrapper/web"],
127 ]);
128 expect([...repositoryCheckpointScopeIds(scopes)]).toEqual([
129 "scope-web",
130 "scope-api",
131 "scope-shared",
132 ]);
133 });
134​
135 it("names an unlabelled repository after its folder on Windows and POSIX", () => {
136 const [repository] = threadRepositoryScopes({
137 checkpointScopes: [
138 scope("scope-root", "C:\\Work\\Wrapper"),
139 scope("scope-api", "c:\\work\\wrapper\\api", {}),
140 ],
141 });
142 expect(repository).toMatchObject({ name: "api", relativePath: "api" });
143 expect(
144 threadRepositoryScopes({
145 checkpointScopes: [scope("scope-root", "C:\\work"), scope("scope-api", "D:\\api", {})],
146 })[0],
147 ).not.toHaveProperty("relativePath");
148 expect(
149 threadRepositoryScopes({
150 checkpointScopes: [
151 scope("scope-root", "/work/task"),
152 scope("scope-api", String.raw`/work/task\outside`, {}),
153 ],
154 })[0],
155 ).toMatchObject({ name: String.raw`task\outside` });
156 });
157});
158​
159describe("threadRepositorySections", () => {
160 const changed = threadChangedRepositories({ checkpoints, checkpointScopes: scopes });
161​
162 it("does not mistake a POSIX backslash for a task-folder separator", () => {
163 const sibling = String.raw`/work/task\outside`;
164 const child = "/work/task/outside";
165 const literalChild = String.raw`/work/task/\\repo`;
166 const sections = threadRepositorySections({
167 changed: [
168 {
169 scopeId: CheckpointScopeId.make("scope-sibling"),
170 name: "api",
171 cwd: sibling,
172 repositoryPath: sibling,
173 },
174 {
175 scopeId: CheckpointScopeId.make("scope-child"),
176 name: "api",
177 cwd: child,
178 repositoryPath: child,
179 },
180 {
181 scopeId: CheckpointScopeId.make("scope-literal-child"),
182 name: "repo",
183 cwd: literalChild,
184 repositoryPath: literalChild,
185 },
186 ],
187 pinned: [],
188 threadCwd: "/work/task",
189 showAll: false,
190 taskFolder: { root: "/work/task", repositories: [] },
191 });
192 expect(sections.visible).toEqual([
193 { key: child, name: "api", cwd: child, origin: "changed" },
194 { key: literalChild, name: "repo", cwd: literalChild, origin: "changed" },
195 ]);
196 });
197​
198 it("orders changed repositories by their latest turn, newest first", () => {
199 expect(changed.map((entry) => entry.cwd)).toEqual([
200 "/work/shared",
201 "/work/wrapper/services/api",
202 "/work/wrapper/web",
203 ]);
204 // A loaded snapshot lists checkpoints by scope, not in the order they were captured.
205 expect(
206 threadChangedRepositories({
207 checkpoints: [
208 checkpoint("api-3", "scope-api", 3),
209 checkpoint("shared-1", "scope-shared", 1),
210 checkpoint("web-2", "scope-web", 2),
211 checkpoint("web-1", "scope-web", 1),
212 ],
213 checkpointScopes: scopes,
214 }).map((entry) => entry.cwd),
215 ).toEqual(["/work/wrapper/services/api", "/work/wrapper/web", "/work/shared"]);
216 // A baseline alone is no change: without a turn the repository sorts last, by name.
217 expect(
218 threadChangedRepositories({
219 checkpoints: [
220 checkpoint("web-0", "scope-web", 0, { runId: null }),
221 checkpoint("api-1", "scope-api", 1),
222 ],
223 checkpointScopes: scopes,
224 }).map((entry) => entry.cwd),
225 ).toEqual(["/work/wrapper/services/api", "/work/shared", "/work/wrapper/web"]);
226 });
227​
228 it("puts pins first, drops the thread's own folder, and folds the rest away", () => {
229 const pinned = [
230 { repositoryPath: "/work/wrapper/docs", name: "docs" },
231 { repositoryPath: "/work/wrapper/web", name: "web" },
232 { repositoryPath: "/work/wrapper", name: "wrapper" },
233 ];
234 const sections = threadRepositorySections({
235 changed,
236 pinned,
237 threadCwd: "/work/wrapper",
238 showAll: false,
239 });
240 expect(sections.visible.map((section) => [section.key, section.origin])).toEqual([
241 ["/work/wrapper/docs", "pinned"],
242 ["/work/wrapper/web", "pinned"],
243 ["/work/shared", "changed"],
244 ["/work/wrapper/services/api", "changed"],
245 ]);
246 expect(sections.hidden).toEqual([]);
247 // A thread that reaches no tracked repository lists what it changed, and no pin.
248 expect(
249 threadRepositorySections({
250 changed,
251 pinned,
252 threadCwd: "/work/wrapper",
253 showAll: false,
254 reachesTracked: false,
255 }).visible.map((section) => section.origin),
256 ).toEqual(["changed", "changed", "changed"]);
257​
258 const many = Array.from({ length: THREAD_REPOSITORY_SECTIONS_VISIBLE + 2 }, (_, index) => ({
259 repositoryPath: `/work/extra-${index}`,
260 name: `extra-${index}`,
261 }));
262 const folded = threadRepositorySections({
263 changed: [],
264 pinned: many,
265 threadCwd: null,
266 showAll: false,
267 });
268 expect(folded.visible).toHaveLength(THREAD_REPOSITORY_SECTIONS_VISIBLE);
269 expect(folded.hidden.map((section) => section.name)).toEqual(["extra-4", "extra-5"]);
270 expect(
271 threadRepositorySections({ changed: [], pinned: many, threadCwd: null, showAll: true })
272 .visible,
273 ).toHaveLength(many.length);
274 });
275​
276 it("pins once, unpins, and offers only Git folders that are not shown", () => {
277 const docs = { repositoryPath: "/work/wrapper/docs", name: "docs" };
278 const pinned = pinThreadRepository([], docs);
279 expect(pinThreadRepository(pinned, { ...docs, name: "renamed" })).toBe(pinned);
280 expect(unpinThreadRepository(pinned, "/work/elsewhere")).toBe(pinned);
281 expect(unpinThreadRepository(pinned, docs.repositoryPath)).toEqual([]);
282​
283 const sections = threadRepositorySections({
284 changed,
285 pinned,
286 threadCwd: "/work/wrapper",
287 showAll: false,
288 });
289 expect(
290 pinnableThreadRepositories({
291 sections,
292 threadCwd: "/work/wrapper",
293 folders: [
294 { path: "/work/wrapper", name: "wrapper", state: "git" },
295 { path: "/work/wrapper/docs", name: "docs", state: "git" },
296 { path: "/work/wrapper/web", name: "web", state: "git" },
297 { path: "/work/wrapper/notes", name: "notes", state: "plain" },
298 { path: "/work/wrapper/gone", name: "gone", state: "missing" },
299 { path: "/work/wrapper/cli", name: "cli", state: "git" },
300 ],
301 }),
302 ).toEqual([{ repositoryPath: "/work/wrapper/cli", name: "cli" }]);
303 });
304​
305 it("offers Git changes of every repository a thread reaches, changed ones first", () => {
306 const changed = [{ repositoryPath: "/work/web", name: "web", cwd: "/work/web" }];
307 const folders = [
308 { path: "/work", name: "work", state: "git" as const },
309 { path: "/work/api", name: "api", state: "git" as const },
310 { path: "/work/web", name: "web", state: "git" as const },
311 { path: "/work/notes", name: "notes", state: "plain" as const },
312 ];
313 expect(
314 threadGitRepositories({ changed, taskFolder: null, folders, threadCwd: "/work" }),
315 ).toEqual([...changed, { repositoryPath: "/work/api", name: "api", cwd: "/work/api" }]);
316 // A worktree thread reads its own checkouts, never the shared ones.
317 expect(
318 threadGitRepositories({
319 changed: [{ repositoryPath: "/work/api", name: "api", cwd: "/work/api" }],
320 taskFolder: {
321 repositories: [{ repositoryPath: "/work/api", worktreePath: "/wt/api", name: "api" }],
322 },
323 folders,
324 threadCwd: "/wt",
325 }),
326 ).toEqual([{ repositoryPath: "/work/api", name: "api", cwd: "/wt/api" }]);
327 });
328​
329 it("lists a task folder's worktrees, changed first, and never a shared checkout", () => {
330 const taskFolder = {
331 root: "/wt/task",
332 projectRoot: "/work/wrapper",
333 repositories: [
334 { repositoryPath: "/work/wrapper/docs", worktreePath: "/wt/task/docs", name: "docs" },
335 { repositoryPath: "/work/wrapper/web", worktreePath: "/wt/task/web", name: "web" },
336 {
337 repositoryPath: "/work/wrapper/services/api",
338 worktreePath: "/wt/task/services/api",
339 name: "api",
340 },
341 ],
342 };
343 const submodule = {
344 scopeId: CheckpointScopeId.make("scope:lib"),
345 name: "lib",
346 repositoryPath: "/work/wrapper/web/vendor/lib",
347 cwd: "/wt/task/web/vendor/lib",
348 };
349 const sections = threadRepositorySections({
350 // `/work/shared` is changed but has no worktree in the task folder.
351 changed: [...changed, submodule],
352 pinned: [{ repositoryPath: "/work/wrapper/cli", name: "cli" }],
353 threadCwd: "/wt/task",
354 showAll: false,
355 taskFolder,
356 });
357 expect(sections.visible.map((section) => [section.key, section.cwd, section.origin])).toEqual([
358 ["/work/wrapper/services/api", "/wt/task/services/api", "worktree"],
359 ["/work/wrapper/web", "/wt/task/web", "worktree"],
360 ["/work/wrapper/docs", "/wt/task/docs", "worktree"],
361 // A submodule the thread changed inside one of its worktrees.
362 ["/work/wrapper/web/vendor/lib", "/wt/task/web/vendor/lib", "changed"],
363 ]);
364 expect(
365 threadRepositorySections({
366 changed,
367 pinned: [],
368 threadCwd: "/wt/task",
369 showAll: false,
370 taskFolder: { root: "/wt/task", repositories: [] },
371 }).visible,
372 ).toEqual([]);
373​
374 expect(
375 addableTaskFolderRepositories({
376 taskFolder,
377 folders: [
378 { path: "/work/wrapper", name: "wrapper", state: "git" },
379 { path: "/work/wrapper/docs", name: "docs", state: "git" },
380 { path: "/work/wrapper/notes", name: "notes", state: "plain" },
381 { path: "/work/shared", name: "shared", state: "git" },
382 ],
383 }),
384 ).toEqual([{ repositoryPath: "/work/shared", name: "shared" }]);
385 });
386});
387​
388describe("rewindFileRestore", () => {
389 const projection = { checkpointScopes: scopes, checkpoints };
390 const missingRoot = (appRunOrdinal: number | null) =>
391 ({ status: "missing", appRunOrdinal }) as const;
392 const linkedProjectFolder = { projectIsWorktree: false };
393​
394 it.each([
395 {
396 why: "a ready snapshot restores every file",
397 checkpoint: { status: "ready", appRunOrdinal: 1 } as const,
398 taskFolder: null,
399 restores: "files",
400 },
401 {
402 why: "a linked project folder restores a repository's own checkpoint",
403 checkpoint: missingRoot(1),
404 taskFolder: linkedProjectFolder,
405 restores: "repositories",
406 },
407 {
408 why: "the thread start restores a repository's baseline",
409 checkpoint: missingRoot(null),
410 taskFolder: linkedProjectFolder,
411 restores: "repositories",
412 },
413 {
414 why: "no repository changed after the target",
415 checkpoint: missingRoot(3),
416 taskFolder: linkedProjectFolder,
417 restores: null,
418 },
419 {
420 why: "a worktree project folder needs its own snapshot",
421 checkpoint: missingRoot(1),
422 taskFolder: { projectIsWorktree: true },
423 restores: null,
424 },
425 {
426 why: "a thread outside a task folder has only its own snapshot",
427 checkpoint: missingRoot(1),
428 taskFolder: null,
429 restores: null,
430 },
431 {
432 why: "a stale snapshot restores nothing",
433 checkpoint: { status: "stale", appRunOrdinal: 1 } as const,
434 taskFolder: linkedProjectFolder,
435 restores: null,
436 },
437 { why: "no target", checkpoint: undefined, taskFolder: linkedProjectFolder, restores: null },
438 ])("$why", ({ checkpoint: target, taskFolder, restores }) => {
439 expect(rewindFileRestore({ projection, checkpoint: target, taskFolder })).toBe(restores);
440 });
441​
442 it("ignores the thread's own scope when looking for repository snapshots", () => {
443 expect(
444 rewindFileRestore({
445 projection: {
446 checkpointScopes: [scope("scope-root", "/work/wrapper")],
447 checkpoints: [checkpoint("root-1", "scope-root", 1, { appRunOrdinal: 1 })],
448 },
449 checkpoint: missingRoot(1),
450 taskFolder: linkedProjectFolder,
451 }),
452 ).toBe(null);
453 });
454});
455​
456describe("isRelativePathInsideFolder", () => {
457 it.each([
458 ["api", undefined, true],
459 ["..api", undefined, true],
460 ["../api", undefined, false],
461 ["..", undefined, false],
462 ["D:/api", undefined, false],
463 [String.raw`\\repo`, "/work/task", true],
464 [String.raw`\\repo`, String.raw`C:\work\task`, false],
465 ])("%j relative to %j is inside: %j", (relativePath, folder, inside) => {
466 expect(isRelativePathInsideFolder(relativePath, folder)).toBe(inside);
467 });
468});
469​
470describe("worktree repository selection", () => {
471 const folders = [
472 { path: "/work/wrapper/web", name: "web", state: "git" },
473 { path: "/work/wrapper/services/api", name: "api", state: "git" },
474 { path: "/work/wrapper/notes", name: "notes", state: "plain" },
475 { path: "/work/wrapper/scratch", name: "scratch", state: "git", hasCommits: false },
476 { path: "/work/shared", name: "shared", state: "git" },
477 ] as const;
478 const choices = worktreeRepositoryChoices({ folders, projectRoot: "/work/wrapper" });
479​
480 it("offers tracked Git folders with their place inside the project folder", () => {
481 expect(choices).toEqual([
482 { repositoryPath: "/work/wrapper/web", name: "web", relativePath: "web" },
483 { repositoryPath: "/work/wrapper/services/api", name: "api", relativePath: "services/api" },
484 { repositoryPath: "/work/shared", name: "shared" },
485 ]);
486 });
487​
488 it("does not mistake a POSIX backslash for a project-folder separator", () => {
489 const sibling = String.raw`/work/task\outside`;
490 const child = "/work/task/outside";
491 const literalChild = String.raw`/work/task/\\repo`;
492 expect(
493 worktreeRepositoryChoices({
494 projectRoot: "/work/task",
495 folders: [
496 { path: sibling, name: "api", state: "git" },
497 { path: child, name: "api", state: "git" },
498 { path: literalChild, name: "repo", state: "git" },
499 ],
500 }),
501 ).toEqual([
502 { repositoryPath: sibling, name: "api" },
503 { repositoryPath: child, name: "api", relativePath: "outside" },
504 { repositoryPath: literalChild, name: "repo", relativePath: String.raw`\\repo` },
505 ]);
506 });
507​
508 it("takes every repository by default and sends nothing until the user chooses", () => {
509 expect(resolveWorktreeRepositorySelection({ choices, chosen: undefined })).toEqual({
510 selected: ["/work/wrapper/web", "/work/wrapper/services/api", "/work/shared"],
511 label: "All repositories",
512 needsChoice: false,
513 strategyRepositories: undefined,
514 });
515 });
516​
517 it("sends the chosen repositories the project still tracks, and an empty choice as none", () => {
518 expect(
519 resolveWorktreeRepositorySelection({
520 choices,
521 chosen: ["/work/shared", "/work/untracked", "/work/wrapper/web"],
522 }),
523 ).toMatchObject({
524 label: "2 repositories",
525 strategyRepositories: [{ path: "/work/wrapper/web" }, { path: "/work/shared" }],
526 });
527 expect(resolveWorktreeRepositorySelection({ choices, chosen: [] })).toMatchObject({
528 label: "No repositories",
529 needsChoice: false,
530 strategyRepositories: [],
531 });
532 });
533​
534 it("takes none by default above the server's limit and asks for a choice", () => {
535 const many = Array.from({ length: 9 }, (_, index) => ({
536 repositoryPath: `/work/wrapper/repo-${index}`,
537 name: `repo-${index}`,
538 }));
539 expect(resolveWorktreeRepositorySelection({ choices: many, chosen: undefined })).toEqual({
540 selected: [],
541 label: "Choose repositories",
542 needsChoice: true,
543 strategyRepositories: undefined,
544 });
545 expect(
546 resolveWorktreeRepositorySelection({ choices: many.slice(0, 8), chosen: undefined }).selected,
547 ).toHaveLength(8);
548 });
549​
550 it("sends a stored choice as it is while the tracked folders are still loading", () => {
551 expect(
552 resolveWorktreeRepositorySelection({ choices: null, chosen: ["/work/shared"] }),
553 ).toMatchObject({ needsChoice: false, strategyRepositories: [{ path: "/work/shared" }] });
554 expect(
555 resolveWorktreeRepositorySelection({ choices: null, chosen: undefined }).strategyRepositories,
556 ).toBeUndefined();
557 });
558});
559​
560describe("deriveTurnRepositoryChanges", () => {
561 it("groups each run's repository checkpoints and marks a repository without a baseline", () => {
562 const byRun = deriveTurnRepositoryChanges({ checkpoints, checkpointScopes: scopes });
563 expect([...byRun.keys()]).toEqual(["run-1", "run-2"]);
564 expect(byRun.get(RunId.make("run-1"))).toEqual([
565 {
566 scopeId: "scope-api",
567 name: "api",
568 repositoryPath: "/work/wrapper/services/api",
569 cwd: "/work/wrapper/services/api",
570 relativePath: "services/api",
571 files: [file("src/index.ts", 4, 2)],
572 baselineMissing: false,
573 },
574 {
575 scopeId: "scope-web",
576 name: "web",
577 repositoryPath: "/work/wrapper/web",
578 cwd: "/work/wrapper/web",
579 relativePath: "web",
580 files: [],
581 baselineMissing: true,
582 },
583 ]);
584 });
585​
586 it("leaves out a repository checkpoint with no files and a recorded baseline", () => {
587 const byRun = deriveTurnRepositoryChanges({
588 checkpoints: [
589 checkpoint("api-0", "scope-api", 0, { runId: null }),
590 checkpoint("api-1", "scope-api", 1),
591 ],
592 checkpointScopes: scopes,
593 });
594 expect(byRun.size).toBe(0);
595 });
596​
597 it("keeps an unchanged turn's repositories when a later turn adds checkpoints", () => {
598 const before = deriveTurnRepositoryChanges({
599 checkpoints: checkpoints.slice(0, 5),
600 checkpointScopes: scopes,
601 });
602 const after = deriveTurnRepositoryChanges({ checkpoints, checkpointScopes: scopes });
603 expect(after.get(RunId.make("run-1"))).toBe(before.get(RunId.make("run-1")));
604 });
605​
606 it("gives nothing for a thread without repository scopes", () => {
607 expect(
608 deriveTurnRepositoryChanges({
609 checkpoints: [checkpoint("root-1", "scope-root", 1, { files: [file("a.ts")] })],
610 checkpointScopes: [scopes[0]!],
611 }).size,
612 ).toBe(0);
613 });
614});
615​
616describe("turn changes", () => {
617 const summaries = deriveThreadCheckpointSummaries({
618 checkpoints,
619 checkpointScopes: scopes,
620 messages: [],
621 });
622​
623 it("attaches repositories to the thread's own turn summary and no summary of their own", () => {
624 expect(summaries.map((summary) => [summary.runId, summary.status])).toEqual([
625 ["run-1", "missing"],
626 ["run-2", "missing"],
627 ]);
628 expect(summaries[0]!.repositories?.map((repository) => repository.name)).toEqual([
629 "api",
630 "web",
631 ]);
632 });
633​
634 it("shows a turn whose own checkpoint is missing when repositories changed", () => {
635 expect(summaries.map(turnHasChanges)).toEqual([true, true]);
636 expect(turnHasChanges({ files: [] })).toBe(false);
637 expect(turnHasChanges({ files: [file("a.ts")] })).toBe(true);
638 });
639​
640 it("totals the thread folder and every repository, honoring truncated file lists", () => {
641 expect(turnChanges(summaries[0]!).totals).toEqual({ count: 1, additions: 4, deletions: 2 });
642 expect(
643 turnChanges({ files: [file("root.ts", 2, 1)], repositories: summaries[1]!.repositories })
644 .totals,
645 ).toEqual({ count: 301, additions: 902, deletions: 31 });
646 expect(turnChanges({ files: [file("root.ts")] })).toEqual({
647 rootFiles: [file("root.ts")],
648 repositories: [],
649 totals: { count: 1, additions: 1, deletions: 0 },
650 });
651 });
652});
653​
654describe("turnDiffSections", () => {
655 const sections = turnDiffSections(
656 {
657 diff: "diff --git a/README.md b/README.md\n",
658 repositories: [
659 { name: "api", repositoryPath: "/work/api", relativePath: "api", diff: "diff api\n" },
660 { name: "empty", repositoryPath: "/work/empty", relativePath: "empty", diff: "" },
661 {
662 name: "api-client",
663 repositoryPath: "/work/api/client",
664 relativePath: "api/client",
665 diff: "diff client\n",
666 },
667 { name: "shared", repositoryPath: "/shared", relativePath: "../shared", diff: "diff s\n" },
668 ],
669 },
670 "wrapper",
671 );
672​
673 it("puts the thread folder first and drops empty patches", () => {
674 expect(sections.map(({ key, name, relativePath }) => [key, name, relativePath])).toEqual([
675 [".", "wrapper", ""],
676 ["/work/api", "api", "api"],
677 ["/work/api/client", "api-client", "api/client"],
678 ["/shared", "shared", "../shared"],
679 ]);
680 expect(turnDiffSections({ diff: "" }, "wrapper")).toEqual([]);
681 expect(turnDiffSections({ diff: "", repositories: [] }, "wrapper")).toEqual([]);
682 });
683​
684 it("maps a thread-relative path back to the innermost repository", () => {
685 expect(repositoryFilePath("api/client", "src/a.ts")).toBe("api/client/src/a.ts");
686 expect(repositoryFilePath("", "src/a.ts")).toBe("src/a.ts");
687 const match = repositorySectionForPath(sections, "api/client/src/a.ts");
688 expect([match?.section.name, match?.path]).toEqual(["api-client", "src/a.ts"]);
689 expect(repositorySectionForPath(sections, "../shared/x.ts")?.path).toBe("x.ts");
690 expect(repositorySectionForPath(sections, "README.md")).toBeNull();
691 expect(repositorySectionForPath(sections, "apiary/x.ts")).toBeNull();
692 });
693});