Open History to see the last commit
1import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3
4const state = vi.hoisted(() => ({
5 shell: null as { projectId: string; worktreePath: string | null } | null,
6 detail: null as { thread: { projectId: string } } | null,
7 detailWorktreePath: null as string | null,
8 projects: new Map<string, { workspaceRoot: string }>(),
9}));
10
11vi.mock("./entities", () => ({
12 useQuietThreadShell: (ref: unknown) => (ref === null ? null : state.shell),
13 useProject: (ref: { projectId: string } | null) =>
14 ref === null ? null : (state.projects.get(ref.projectId) ?? null),
15}));
16vi.mock("./threads", async () => {
17 const { some, none } = await import("effect/Option");
18 return {
19 useEnvironmentThread: (environmentId: unknown, threadId: unknown) => ({
20 data:
21 environmentId !== null && threadId !== null && state.detail ? some(state.detail) : none(),
22 }),
23 };
24});
25vi.mock("./use-thread-detail", () => ({
26 useThreadWorktreePath: (ref: unknown) => (ref === null ? null : state.detailWorktreePath),
27 useSelectedThreadWorktreePath: () => null,
28}));
29vi.mock("./use-thread-selection", () => ({ useThreadSelection: () => ({}) }));
30
31const { useThreadCwd } = await import("./use-selected-thread-worktree");
32
33const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
34
35function setup(input: Partial<Omit<typeof state, "projects">>) {
36 Object.assign(state, { shell: null, detail: null, detailWorktreePath: null }, input);
37 state.projects = new Map([[ProjectId.make("project"), { workspaceRoot: "/repo" }]]);
38}
39
40describe("useThreadCwd", () => {
41 it("falls back to the project root from the loaded detail when the shell is missing", () => {
42 setup({ detail: { thread: { projectId: "project" } } });
43 expect(useThreadCwd(ref)).toBe("/repo");
44 });
45
46 it("prefers the thread worktree over the project root", () => {
47 setup({ shell: { projectId: "project", worktreePath: "/repo/.worktrees/a" } });
48 expect(useThreadCwd(ref)).toBe("/repo/.worktrees/a");
49 });
50
51 it("is null without a thread", () => {
52 setup({ detail: { thread: { projectId: "project" } } });
53 expect(useThreadCwd(null)).toBeNull();
54 });
55});