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

projectWorktreeRoot.test.ts

114 lines4.1 KB

1import * as NodePath from "@effect/platform-node/NodePath";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Option from "effect/Option";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8​
9import type { OrchestrationProject } from "@t3tools/contracts";
10import { ProjectId } from "@t3tools/contracts";
11​
12import { PersistenceSqlError } from "../persistence/Errors.ts";
13import type { ProjectionSnapshotQueryShape } from "../orchestration/Services/ProjectionSnapshotQuery.ts";
14import { makeProjectWorktreeRootResolver } from "./projectWorktreeRoot.ts";
15​
16const WORKSPACE_ROOT = "/tmp/project-worktree-root";
17​
18const makeProject = (worktreeRoot: string | null): OrchestrationProject => ({
19 id: ProjectId.make("project-worktree-root"),
20 title: "Project",
21 workspaceRoot: WORKSPACE_ROOT,
22 repositoryIdentity: null,
23 defaultModelSelection: null,
24 defaultThreadEnvMode: null,
25 autoPull: false,
26 worktreeRoot,
27 faviconPath: null,
28 projectIcon: null,
29 scripts: [],
30 createdAt: "2026-09-01T00:00:00.000Z",
31 updatedAt: "2026-09-01T00:00:00.000Z",
32 deletedAt: null,
33});
34​
35const resolveOn = (
36 pathLayer: Layer.Layer<Path.Path>,
37 getActiveProjectByWorkspaceRoot: ProjectionSnapshotQueryShape["getActiveProjectByWorkspaceRoot"],
38) =>
39 Effect.gen(function* () {
40 const path = yield* Path.Path;
41 const resolve = makeProjectWorktreeRootResolver({
42 projectionSnapshotQuery: {
43 getActiveProjectByWorkspaceRoot,
44 } as ProjectionSnapshotQueryShape,
45 path,
46 });
47 return yield* resolve(WORKSPACE_ROOT);
48 }).pipe(Effect.provide(pathLayer));
49​
50const resolveWith = (
51 getActiveProjectByWorkspaceRoot: ProjectionSnapshotQueryShape["getActiveProjectByWorkspaceRoot"],
52) => resolveOn(NodeServices.layer, getActiveProjectByWorkspaceRoot);
53​
54describe("resolveProjectWorktreeRoot", () => {
55 it.effect("returns undefined when the project sets no override", () =>
56 Effect.gen(function* () {
57 const resolved = yield* resolveWith(() => Effect.succeed(Option.some(makeProject(null))));
58 assert.strictEqual(resolved, undefined);
59 }),
60 );
61​
62 it.effect("returns undefined when no project owns the workspace root", () =>
63 Effect.gen(function* () {
64 const resolved = yield* resolveWith(() => Effect.succeed(Option.none()));
65 assert.strictEqual(resolved, undefined);
66 }),
67 );
68​
69 it.effect("expands a leading ~ so the path never depends on the server cwd", () =>
70 Effect.gen(function* () {
71 const resolved = yield* resolveOn(NodePath.layerPosix, () =>
72 Effect.succeed(Option.some(makeProject("~/code/myrepo.worktrees"))),
73 );
74 // Asserted by shape rather than against the home directory, so the test
75 // does not have to read the environment it is describing.
76 assert.isTrue(resolved?.startsWith("/"));
77 assert.isTrue(resolved?.endsWith("/code/myrepo.worktrees"));
78 }),
79 );
80​
81 it.effect("falls back to the default location for a root this platform cannot resolve", () =>
82 Effect.gen(function* () {
83 // A Windows root reaching a POSIX server: resolving it would silently put
84 // worktrees under the server's cwd, so the default location wins.
85 const resolved = yield* resolveOn(NodePath.layerPosix, () =>
86 Effect.succeed(Option.some(makeProject("C:\\worktrees"))),
87 );
88 assert.strictEqual(resolved, undefined);
89 }),
90 );
91​
92 it.effect("keeps a Windows root on a Windows server", () =>
93 Effect.gen(function* () {
94 const resolved = yield* resolveOn(NodePath.layerWin32, () =>
95 Effect.succeed(Option.some(makeProject("C:\\worktrees"))),
96 );
97 assert.strictEqual(resolved, "C:\\worktrees");
98 }),
99 );
100​
101 it.effect("falls back to the default location when the projection read fails", () =>
102 Effect.gen(function* () {
103 const resolved = yield* resolveWith(() =>
104 Effect.fail(
105 new PersistenceSqlError({
106 operation: "test.getActiveProjectByWorkspaceRoot",
107 detail: "projection unavailable",
108 }),
109 ),
110 );
111 assert.strictEqual(resolved, undefined);
112 }),
113 );
114});