Open History to see the last commit
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 };
61}
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 };
84}
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/shared", name: "shared", state: "git" },
476 ] as const;
477 const choices = worktreeRepositoryChoices({ folders, projectRoot: "/work/wrapper" });
478
479 it("offers tracked Git folders with their place inside the project folder", () => {
480 expect(choices).toEqual([
481 { repositoryPath: "/work/wrapper/web", name: "web", relativePath: "web" },
482 { repositoryPath: "/work/wrapper/services/api", name: "api", relativePath: "services/api" },
483 { repositoryPath: "/work/shared", name: "shared" },
484 ]);
485 });
486
487 it("does not mistake a POSIX backslash for a project-folder separator", () => {
488 const sibling = String.raw`/work/task\outside`;
489 const child = "/work/task/outside";
490 const literalChild = String.raw`/work/task/\\repo`;
491 expect(
492 worktreeRepositoryChoices({
493 projectRoot: "/work/task",
494 folders: [
495 { path: sibling, name: "api", state: "git" },
496 { path: child, name: "api", state: "git" },
497 { path: literalChild, name: "repo", state: "git" },
498 ],
499 }),
500 ).toEqual([
501 { repositoryPath: sibling, name: "api" },
502 { repositoryPath: child, name: "api", relativePath: "outside" },
503 { repositoryPath: literalChild, name: "repo", relativePath: String.raw`\\repo` },
504 ]);
505 });
506
507 it("takes every repository by default and sends nothing until the user chooses", () => {
508 expect(resolveWorktreeRepositorySelection({ choices, chosen: undefined })).toEqual({
509 selected: ["/work/wrapper/web", "/work/wrapper/services/api", "/work/shared"],
510 label: "All repositories",
511 needsChoice: false,
512 strategyRepositories: undefined,
513 });
514 });
515
516 it("sends the chosen repositories the project still tracks, and an empty choice as none", () => {
517 expect(
518 resolveWorktreeRepositorySelection({
519 choices,
520 chosen: ["/work/shared", "/work/untracked", "/work/wrapper/web"],
521 }),
522 ).toMatchObject({
523 label: "2 repositories",
524 strategyRepositories: [{ path: "/work/wrapper/web" }, { path: "/work/shared" }],
525 });
526 expect(resolveWorktreeRepositorySelection({ choices, chosen: [] })).toMatchObject({
527 label: "No repositories",
528 needsChoice: false,
529 strategyRepositories: [],
530 });
531 });
532
533 it("takes none by default above the server's limit and asks for a choice", () => {
534 const many = Array.from({ length: 9 }, (_, index) => ({
535 repositoryPath: `/work/wrapper/repo-${index}`,
536 name: `repo-${index}`,
537 }));
538 expect(resolveWorktreeRepositorySelection({ choices: many, chosen: undefined })).toEqual({
539 selected: [],
540 label: "Choose repositories",
541 needsChoice: true,
542 strategyRepositories: undefined,
543 });
544 expect(
545 resolveWorktreeRepositorySelection({ choices: many.slice(0, 8), chosen: undefined }).selected,
546 ).toHaveLength(8);
547 });
548
549 it("sends a stored choice as it is while the tracked folders are still loading", () => {
550 expect(
551 resolveWorktreeRepositorySelection({ choices: null, chosen: ["/work/shared"] }),
552 ).toMatchObject({ needsChoice: false, strategyRepositories: [{ path: "/work/shared" }] });
553 expect(
554 resolveWorktreeRepositorySelection({ choices: null, chosen: undefined }).strategyRepositories,
555 ).toBeUndefined();
556 });
557});
558
559describe("deriveTurnRepositoryChanges", () => {
560 it("groups each run's repository checkpoints and marks a repository without a baseline", () => {
561 const byRun = deriveTurnRepositoryChanges({ checkpoints, checkpointScopes: scopes });
562 expect([...byRun.keys()]).toEqual(["run-1", "run-2"]);
563 expect(byRun.get(RunId.make("run-1"))).toEqual([
564 {
565 scopeId: "scope-api",
566 name: "api",
567 repositoryPath: "/work/wrapper/services/api",
568 cwd: "/work/wrapper/services/api",
569 relativePath: "services/api",
570 files: [file("src/index.ts", 4, 2)],
571 baselineMissing: false,
572 },
573 {
574 scopeId: "scope-web",
575 name: "web",
576 repositoryPath: "/work/wrapper/web",
577 cwd: "/work/wrapper/web",
578 relativePath: "web",
579 files: [],
580 baselineMissing: true,
581 },
582 ]);
583 });
584
585 it("leaves out a repository checkpoint with no files and a recorded baseline", () => {
586 const byRun = deriveTurnRepositoryChanges({
587 checkpoints: [
588 checkpoint("api-0", "scope-api", 0, { runId: null }),
589 checkpoint("api-1", "scope-api", 1),
590 ],
591 checkpointScopes: scopes,
592 });
593 expect(byRun.size).toBe(0);
594 });
595
596 it("keeps an unchanged turn's repositories when a later turn adds checkpoints", () => {
597 const before = deriveTurnRepositoryChanges({
598 checkpoints: checkpoints.slice(0, 5),
599 checkpointScopes: scopes,
600 });
601 const after = deriveTurnRepositoryChanges({ checkpoints, checkpointScopes: scopes });
602 expect(after.get(RunId.make("run-1"))).toBe(before.get(RunId.make("run-1")));
603 });
604
605 it("gives nothing for a thread without repository scopes", () => {
606 expect(
607 deriveTurnRepositoryChanges({
608 checkpoints: [checkpoint("root-1", "scope-root", 1, { files: [file("a.ts")] })],
609 checkpointScopes: [scopes[0]!],
610 }).size,
611 ).toBe(0);
612 });
613});
614
615describe("turn changes", () => {
616 const summaries = deriveThreadCheckpointSummaries({
617 checkpoints,
618 checkpointScopes: scopes,
619 messages: [],
620 });
621
622 it("attaches repositories to the thread's own turn summary and no summary of their own", () => {
623 expect(summaries.map((summary) => [summary.runId, summary.status])).toEqual([
624 ["run-1", "missing"],
625 ["run-2", "missing"],
626 ]);
627 expect(summaries[0]!.repositories?.map((repository) => repository.name)).toEqual([
628 "api",
629 "web",
630 ]);
631 });
632
633 it("shows a turn whose own checkpoint is missing when repositories changed", () => {
634 expect(summaries.map(turnHasChanges)).toEqual([true, true]);
635 expect(turnHasChanges({ files: [] })).toBe(false);
636 expect(turnHasChanges({ files: [file("a.ts")] })).toBe(true);
637 });
638
639 it("totals the thread folder and every repository, honoring truncated file lists", () => {
640 expect(turnChanges(summaries[0]!).totals).toEqual({ count: 1, additions: 4, deletions: 2 });
641 expect(
642 turnChanges({ files: [file("root.ts", 2, 1)], repositories: summaries[1]!.repositories })
643 .totals,
644 ).toEqual({ count: 301, additions: 902, deletions: 31 });
645 expect(turnChanges({ files: [file("root.ts")] })).toEqual({
646 rootFiles: [file("root.ts")],
647 repositories: [],
648 totals: { count: 1, additions: 1, deletions: 0 },
649 });
650 });
651});
652
653describe("turnDiffSections", () => {
654 const sections = turnDiffSections(
655 {
656 diff: "diff --git a/README.md b/README.md\n",
657 repositories: [
658 { name: "api", repositoryPath: "/work/api", relativePath: "api", diff: "diff api\n" },
659 { name: "empty", repositoryPath: "/work/empty", relativePath: "empty", diff: "" },
660 {
661 name: "api-client",
662 repositoryPath: "/work/api/client",
663 relativePath: "api/client",
664 diff: "diff client\n",
665 },
666 { name: "shared", repositoryPath: "/shared", relativePath: "../shared", diff: "diff s\n" },
667 ],
668 },
669 "wrapper",
670 );
671
672 it("puts the thread folder first and drops empty patches", () => {
673 expect(sections.map(({ key, name, relativePath }) => [key, name, relativePath])).toEqual([
674 [".", "wrapper", ""],
675 ["/work/api", "api", "api"],
676 ["/work/api/client", "api-client", "api/client"],
677 ["/shared", "shared", "../shared"],
678 ]);
679 expect(turnDiffSections({ diff: "" }, "wrapper")).toEqual([]);
680 expect(turnDiffSections({ diff: "", repositories: [] }, "wrapper")).toEqual([]);
681 });
682
683 it("maps a thread-relative path back to the innermost repository", () => {
684 expect(repositoryFilePath("api/client", "src/a.ts")).toBe("api/client/src/a.ts");
685 expect(repositoryFilePath("", "src/a.ts")).toBe("src/a.ts");
686 const match = repositorySectionForPath(sections, "api/client/src/a.ts");
687 expect([match?.section.name, match?.path]).toEqual(["api-client", "src/a.ts"]);
688 expect(repositorySectionForPath(sections, "../shared/x.ts")?.path).toBe("x.ts");
689 expect(repositorySectionForPath(sections, "README.md")).toBeNull();
690 expect(repositorySectionForPath(sections, "apiary/x.ts")).toBeNull();
691 });
692});