Open History to see the last commit
1import { ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5
6import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
7import * as ProjectService from "../project/ProjectService.ts";
8import * as GitWorkflowService from "./GitWorkflowService.ts";
9import { assertCheckoutIdle, busyThreadsInCheckout } from "./sharedCheckoutGuard.ts";
10
11const projectId = ProjectId.make("project-a");
12const otherProjectId = ProjectId.make("project-b");
13const nestedProjectId = ProjectId.make("project-nested");
14const childRepositoryProjectId = ProjectId.make("project-child-repository");
15
16const thread = (
17 title: string,
18 overrides: {
19 readonly projectId?: ProjectId;
20 readonly worktreePath?: string | null;
21 readonly activityRunStatus?: "preparing" | "starting" | "running" | "waiting" | null;
22 readonly archived?: boolean;
23 } = {},
24) => ({
25 archived: overrides.archived ?? false,
26 id: ThreadId.make(title),
27 title,
28 projectId: overrides.projectId ?? projectId,
29 worktreePath: overrides.worktreePath ?? null,
30 activityRunStatus:
31 overrides.activityRunStatus === undefined ? "running" : overrides.activityRunStatus,
32});
33
34describe("busyThreadsInCheckout", () => {
35 it("finds running local threads in the project root checkout", () => {
36 const busy = busyThreadsInCheckout({
37 checkoutPaths: ["/repo"],
38 projectIds: new Set([projectId]),
39 threads: [
40 thread("local running"),
41 thread("local idle", { activityRunStatus: null }),
42 thread("worktree running", { worktreePath: "/worktrees/a" }),
43 thread("other project", { projectId: otherProjectId }),
44 ],
45 });
46
47 expect(busy.map(({ title }) => title)).toEqual(["local running"]);
48 });
49
50 it("finds running threads that share a worktree", () => {
51 const busy = busyThreadsInCheckout({
52 checkoutPaths: ["/worktrees/a"],
53 projectIds: new Set(),
54 threads: [
55 thread("same worktree", { worktreePath: "/worktrees/a", activityRunStatus: "waiting" }),
56 thread("other worktree", { worktreePath: "/worktrees/b" }),
57 thread("local running"),
58 ],
59 });
60
61 expect(busy.map(({ title }) => title)).toEqual(["same worktree"]);
62 });
63});
64
65describe("assertCheckoutIdle", () => {
66 const guardLayer = (input: {
67 readonly repositoryRoot: string;
68 readonly threads: ReadonlyArray<ReturnType<typeof thread>>;
69 readonly failWorktreeLookup?: boolean;
70 }) =>
71 Layer.mergeAll(
72 Layer.mock(GitWorkflowService.GitWorkflowService)({
73 repositoryRoot: (cwd) =>
74 Effect.succeed(cwd.startsWith("/repo/child") ? "/repo/child" : input.repositoryRoot),
75 }),
76 Layer.mock(ProjectService.ProjectService)({
77 snapshot: Effect.succeed({
78 projects: [
79 { id: projectId, workspaceRoot: "/repo" },
80 { id: nestedProjectId, workspaceRoot: "/repo/packages/app" },
81 { id: childRepositoryProjectId, workspaceRoot: "/repo/child" },
82 { id: otherProjectId, workspaceRoot: "/other" },
83 ],
84 } as never),
85 }),
86 Layer.mock(ThreadManagementService.ThreadManagementService)({
87 getShellSnapshot: (options) =>
88 options?.worktreePath !== undefined && input.failWorktreeLookup
89 ? Effect.fail("worktree lookup failed" as never)
90 : Effect.succeed(
91 (() => {
92 const matching = input.threads.filter(
93 (candidate) =>
94 (options?.projectId === undefined ||
95 candidate.projectId === options.projectId) &&
96 (options?.worktreePath === undefined ||
97 candidate.worktreePath === options.worktreePath),
98 );
99 return {
100 threads: matching.filter((candidate) => !candidate.archived),
101 archivedThreads: matching.filter((candidate) => candidate.archived),
102 } as never;
103 })(),
104 ),
105 }),
106 );
107
108 it.effect("refuses a switch from a subdirectory of a busy checkout", () =>
109 Effect.gen(function* () {
110 const error = yield* assertCheckoutIdle("test", "/repo/src").pipe(
111 Effect.flip,
112 Effect.provide(guardLayer({ repositoryRoot: "/repo", threads: [thread("local running")] })),
113 );
114 expect(error.detail).toContain("'local running'");
115 }),
116 );
117
118 it.effect("keeps a busy thread when another lookup fails", () =>
119 Effect.gen(function* () {
120 const error = yield* assertCheckoutIdle("test", "/repo").pipe(
121 Effect.flip,
122 Effect.provide(
123 guardLayer({
124 repositoryRoot: "/repo",
125 threads: [thread("local running")],
126 failWorktreeLookup: true,
127 }),
128 ),
129 );
130 expect(error.detail).toContain("'local running'");
131 }),
132 );
133
134 it.effect("refuses a switch while a nested project or archived thread is working", () =>
135 Effect.gen(function* () {
136 const error = yield* assertCheckoutIdle("test", "/repo").pipe(
137 Effect.flip,
138 Effect.provide(
139 guardLayer({
140 repositoryRoot: "/repo",
141 threads: [
142 thread("nested running", { projectId: nestedProjectId }),
143 thread("archived running", { archived: true }),
144 thread("other running", { projectId: otherProjectId }),
145 thread("child repository running", { projectId: childRepositoryProjectId }),
146 ],
147 }),
148 ),
149 );
150 // Exactly these two: a third busy thread would read "and 1 more".
151 expect(error.detail).toMatch(
152 /^'(nested|archived) running' and '(nested|archived) running' are still working/,
153 );
154 }),
155 );
156
157 it.effect("allows a switch when no thread is working", () =>
158 assertCheckoutIdle("test", "/repo").pipe(
159 Effect.provide(
160 guardLayer({
161 repositoryRoot: "/repo",
162 threads: [thread("local idle", { activityRunStatus: null })],
163 }),
164 ),
165 ),
166 );
167});