Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import { ProjectId, type ServerSettings as ServerSettingsSchema } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Path from "effect/Path";
7import { ChildProcessSpawner } from "effect/process";
8
9import * as ProjectStore from "../orchestration-v2/ProjectStore.ts";
10import * as ServerSettings from "../serverSettings.ts";
11import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
12import { GitHubAccount } from "./GitHubApi.ts";
13import { makeGitHubProjectAccount } from "./gitHubProjectAccount.ts";
14
15const WORK = ProjectId.make("work");
16const PERSONAL = ProjectId.make("personal");
17const ROOTS: Record<string, ProjectId> = { "/code/work": WORK, "/code/personal": PERSONAL };
18
19function harness(overrides: ServerSettingsSchema["projectSettingsOverrides"]) {
20 const gitCalls: string[] = [];
21 const row = (projectId: ProjectId, workspaceRoot: string): ProjectStore.ProjectRow => ({
22 projectId,
23 title: projectId,
24 workspaceRoot,
25 defaultModelSelection: null,
26 defaultThreadEnvMode: null,
27 autoPull: false,
28 faviconPath: null,
29 projectIcon: null,
30 scripts: [],
31 createdAt: "2026-10-01T00:00:00.000Z",
32 updatedAt: "2026-10-01T00:00:00.000Z",
33 deletedAt: null,
34 });
35 const layer = Layer.mergeAll(
36 ServerSettings.ServerSettingsService.layerTest({ projectSettingsOverrides: overrides }),
37 Layer.mock(ProjectStore.ProjectStoreV2)({
38 findActiveByWorkspaceRoot: (root) => {
39 const projectId = ROOTS[root];
40 return Effect.succeed(
41 projectId === undefined ? Option.none() : Option.some(row(projectId, root)),
42 );
43 },
44 }),
45 Layer.mock(GitVcsDriver.GitVcsDriver)({
46 // A worktree's common directory lives in its main checkout.
47 execute: (input) =>
48 Effect.sync(() => {
49 gitCalls.push(input.cwd);
50 return {
51 exitCode: ChildProcessSpawner.ExitCode(0),
52 stdout: "/code/work/.git\n",
53 stderr: "",
54 stdoutTruncated: false,
55 stderrTruncated: false,
56 };
57 }),
58 }),
59 Path.layer,
60 );
61 return { layer, gitCalls };
62}
63
64it.effect("runs a project's checkouts and worktrees as its own account", () => {
65 const { layer, gitCalls } = harness({ [WORK]: { githubAccount: "bek-work" } });
66 return Effect.gen(function* () {
67 const { accountFor, actAs } = yield* makeGitHubProjectAccount;
68 expect(yield* accountFor({ cwd: "/code/work" })).toBe("bek-work");
69 expect(yield* accountFor({ cwd: "/code/personal" })).toBeNull();
70 expect(yield* accountFor({ cwd: "/worktrees/work/feature" })).toBe("bek-work");
71 expect(yield* accountFor({ cwd: "/worktrees/work/feature" })).toBe("bek-work");
72 expect(gitCalls).toEqual(["/worktrees/work/feature"]);
73 expect(yield* actAs({ cwd: "/worktrees/work/feature" }, GitHubAccount)).toBe("bek-work");
74 expect(yield* actAs({ cwd: "/code/personal" }, GitHubAccount)).toBeNull();
75 }).pipe(Effect.provide(layer));
76});
77
78it.effect("asks nothing of Git or the project store without project overrides", () => {
79 const { layer, gitCalls } = harness({});
80 return Effect.gen(function* () {
81 const { accountFor } = yield* makeGitHubProjectAccount;
82 expect(yield* accountFor({ cwd: "/worktrees/work/feature" })).toBeNull();
83 expect(gitCalls).toEqual([]);
84 }).pipe(Effect.provide(layer));
85});