Open History to see the last commit
1import {
2 EnvironmentId,
3 ProjectId,
4 ThreadId,
5 type ScopedThreadRef,
6 type ThreadPullRequestLink,
7} from "@t3tools/contracts";
8import { AsyncResult, Atom } from "effect/reactivity";
9import { expect, it, vi } from "vite-plus/test";
10import { page } from "vite-plus/test/browser";
11
12const SCRATCH_ROOT = "/home/user/.t3/scratch";
13
14const state = vi.hoisted(() => ({
15 workspaceRoot: "",
16 folder: "",
17 pullRequests: [] as unknown[],
18 configLoaded: true,
19}));
20
21// The read of t3.json is a cache: a re-read is the only way to see what is on disk now.
22const t3File = vi.hoisted(() => ({
23 disk: "",
24 cached: "",
25 listeners: new Set<() => void>(),
26}));
27
28vi.mock(import("~/components/files/projectFilesQueryState"), async (importOriginal) => {
29 const actual = await importOriginal();
30 const { useSyncExternalStore } = await import("react");
31 return {
32 ...actual,
33 useProjectFileQuery: ((_environmentId: unknown, _cwd: string, relativePath: string | null) => {
34 const contents = useSyncExternalStore(
35 (listener) => {
36 t3File.listeners.add(listener);
37 return () => t3File.listeners.delete(listener);
38 },
39 () => t3File.cached,
40 );
41 const refresh = () => {
42 t3File.cached = t3File.disk;
43 for (const listener of t3File.listeners) listener();
44 };
45 const data =
46 relativePath === "t3.json" && contents !== ""
47 ? { relativePath, contents, byteLength: contents.length, truncated: false }
48 : null;
49 return {
50 data,
51 error: null,
52 isPending: false,
53 readError: null,
54 isNotFile: false,
55 isFile: data !== null,
56 refresh,
57 };
58 }) as never,
59 };
60});
61
62vi.mock(import("~/state/entities"), async (importOriginal) => ({
63 ...(await importOriginal()),
64 useProject: ((ref: unknown) =>
65 ref === null
66 ? null
67 : {
68 id: "project",
69 environmentId: "environment",
70 title: state.workspaceRoot === SCRATCH_ROOT ? "No project" : "plain",
71 workspaceRoot: state.workspaceRoot,
72 scripts: [],
73 }) as never,
74 useProjects: (() => []) as never,
75 useQuietThreadShell: ((ref: unknown) =>
76 ref === null
77 ? null
78 : {
79 id: "thread",
80 environmentId: "environment",
81 projectId: "project",
82 worktreePath: state.folder,
83 branch: null,
84 pullRequests: state.pullRequests,
85 }) as never,
86 useServerConfigs: (() =>
87 new Map(
88 state.configLoaded
89 ? [
90 [
91 EnvironmentId.make("environment"),
92 {
93 scratchWorkspaceRoot: SCRATCH_ROOT,
94 environment: { capabilities: { pullRequests: true } },
95 },
96 ],
97 ]
98 : [],
99 )) as never,
100}));
101// Neither folder is a repository, which is what the server reports for both.
102vi.mock(import("~/state/vcs"), async (importOriginal) => {
103 const actual = await importOriginal();
104 const notARepo = Atom.make(
105 AsyncResult.success({
106 isRepo: false,
107 hasPrimaryRemote: false,
108 isDefaultRef: false,
109 refName: null,
110 hasWorkingTreeChanges: false,
111 workingTree: { files: [], insertions: 0, deletions: 0 },
112 hasUpstream: false,
113 aheadCount: 0,
114 behindCount: 0,
115 aheadOfDefaultCount: 0,
116 pr: null,
117 }),
118 );
119 return {
120 ...actual,
121 vcsEnvironment: { ...actual.vcsEnvironment, status: (() => notARepo) as never },
122 };
123});
124
125import { render } from "../../../test/render";
126import { useRightPanelStore } from "../../rightPanelStore";
127import { PopoverCreateHandle } from "../ui/popover";
128import { ChatCanvasContext } from "./ChatCanvasContext";
129import { resolveChatCanvasLayout } from "./chatCanvasLayout";
130import { ThreadDetailsPanel } from "./ThreadDetailsPanel";
131
132const threadRef: ScopedThreadRef = {
133 environmentId: EnvironmentId.make("environment"),
134 threadId: ThreadId.make("thread"),
135};
136const container = { width: 1200, height: 800 };
137const placement = { x: 908, y: 0, right: 12, width: 280, height: 400 } as const;
138const linkedPullRequest: ThreadPullRequestLink = {
139 host: "github.com",
140 repository: "pingdotgg/t3code",
141 number: 27513,
142 url: "https://github.com/pingdotgg/t3code/pull/27513",
143 source: "manual",
144 linkedAt: "2026-10-30T00:00:00.000Z",
145 snapshot: null,
146 stack: null,
147};
148
149function setup(input: {
150 workspaceRoot: string;
151 folder: string;
152 pullRequests?: ThreadPullRequestLink[];
153 configLoaded?: boolean;
154}) {
155 state.configLoaded = input.configLoaded ?? true;
156 state.workspaceRoot = input.workspaceRoot;
157 state.folder = input.folder;
158 state.pullRequests = input.pullRequests ?? [];
159 useRightPanelStore.setState({ threadPanelVisibilityByThreadKey: {} });
160 const anchor = document.createElement("div");
161 document.body.append(anchor);
162 return {
163 anchor: { current: anchor },
164 handle: PopoverCreateHandle(),
165 [Symbol.dispose]: () => anchor.remove(),
166 };
167}
168
169const machines = (["This Mac", "Server"] as const).map((label, index) => ({
170 environmentId: EnvironmentId.make(index === 0 ? "environment" : "other"),
171 projectId: ProjectId.make("project"),
172 label,
173 isPrimary: index === 0,
174 machine: "server" as const,
175}));
176
177function Panel({
178 fixture,
179 draftEnvMode,
180 onMachines = false,
181 withScripts = false,
182}: {
183 fixture: ReturnType<typeof setup>;
184 /** Renders an unsent draft in this mode instead of a started thread. */
185 draftEnvMode?: "local" | "worktree";
186 onMachines?: boolean;
187 withScripts?: boolean;
188}) {
189 const noop = () => {};
190 const scriptResult = async () => ({ ok: true }) as never;
191 return (
192 <ChatCanvasContext
193 value={{
194 container,
195 lane: { padding: 48, minChatWidth: 640 },
196 layout: resolveChatCanvasLayout({ container, preview: null }),
197 previewKey: null,
198 registerTimeline: noop,
199 measureDetailsCard: noop,
200 detailsCardTopInset: 0,
201 preferredDetailsCard: placement,
202 detailsCardPlacement: placement,
203 }}
204 >
205 <ThreadDetailsPanel
206 anchor={fixture.anchor}
207 handle={fixture.handle}
208 environmentId={threadRef.environmentId}
209 threadId={threadRef.threadId}
210 projectId={ProjectId.make("project")}
211 composerDraftTarget={threadRef}
212 worktreePath={draftEnvMode === undefined ? state.folder : null}
213 isServerThread={draftEnvMode === undefined}
214 activeProjectName={state.workspaceRoot === SCRATCH_ROOT ? "No project" : "plain"}
215 activeProjectScripts={withScripts ? [] : undefined}
216 preferredScriptId={null}
217 keybindings={[]}
218 availableEditors={[]}
219 showOpenInPicker={false}
220 openInShortcutEnabled={false}
221 gitCwd={state.folder}
222 workspaceMutationId={null}
223 isGitRepo={false}
224 offersWorkspaceModes={false}
225 envLocked={draftEnvMode === undefined}
226 availableEnvironments={onMachines ? machines : []}
227 {...(onMachines ? { onEnvironmentChange: noop } : {})}
228 onEnvModeChange={noop}
229 envMode={draftEnvMode ?? "local"}
230 startFromOrigin={false}
231 onStartFromOriginChange={noop}
232 onComposerFocusRequest={noop}
233 onRunProjectScript={noop}
234 onAddProjectScript={scriptResult}
235 onUpdateProjectScript={scriptResult}
236 onDeleteProjectScript={scriptResult}
237 usage={{} as never}
238 />
239 </ChatCanvasContext>
240 );
241}
242
243it("offers no git controls in a No project thread, but shows its linked pull request", async () => {
244 using fixture = setup({
245 workspaceRoot: SCRATCH_ROOT,
246 folder: `${SCRATCH_ROOT}/thread`,
247 pullRequests: [linkedPullRequest],
248 });
249 await using _screen = await render(<Panel fixture={fixture} />);
250 await expect.element(page.getByText("#27513")).toBeVisible();
251 await expect
252 .element(page.getByRole("button", { name: "Initialize Git" }))
253 .not.toBeInTheDocument();
254});
255
256it("shows Current checkout where no worktree can come from, even when New worktree is the default", async () => {
257 using fixture = setup({ workspaceRoot: SCRATCH_ROOT, folder: SCRATCH_ROOT });
258 await using _screen = await render(<Panel fixture={fixture} draftEnvMode="worktree" />);
259 await expect.element(page.getByLabelText("Run context: Current checkout")).toBeVisible();
260 await expect.element(page.getByRole("button", { name: /Run context/ })).not.toBeInTheDocument();
261});
262
263it("offers only machines where no worktree can come from and several machines exist", async () => {
264 using fixture = setup({ workspaceRoot: SCRATCH_ROOT, folder: SCRATCH_ROOT });
265 await using _screen = await render(
266 <Panel fixture={fixture} draftEnvMode="worktree" onMachines />,
267 );
268 await page.getByRole("button", { name: "Run context: This Mac, Current checkout" }).click();
269 await expect.element(page.getByText("Run on")).toBeVisible();
270 await expect.element(page.getByText("New worktree")).not.toBeInTheDocument();
271});
272
273it("shows a linked pull request in a folder that is not a git repository", async () => {
274 using fixture = setup({
275 workspaceRoot: "/work/plain",
276 folder: "/work/plain",
277 pullRequests: [linkedPullRequest],
278 });
279 await using _screen = await render(<Panel fixture={fixture} />);
280 await expect.element(page.getByRole("region", { name: "Workspace" })).toBeVisible();
281 await expect.element(page.getByText("#27513")).toBeVisible();
282 await expect.element(page.getByRole("button", { name: "Initialize Git" })).toBeVisible();
283});
284
285it("offers no Initialize Git before the server config says where No project lives", async () => {
286 using fixture = setup({
287 workspaceRoot: SCRATCH_ROOT,
288 folder: `${SCRATCH_ROOT}/thread`,
289 configLoaded: false,
290 });
291 await using _screen = await render(<Panel fixture={fixture} />);
292 await expect.element(page.getByRole("region", { name: "Workspace" })).toBeVisible();
293 await expect
294 .element(page.getByRole("button", { name: "Initialize Git" }))
295 .not.toBeInTheDocument();
296});
297
298it("lists the t3.json actions on disk when the actions menu opens again after the file changed", async () => {
299 using fixture = setup({ workspaceRoot: "/work/plain", folder: "/work/plain" });
300 const t3Json = (name: string) =>
301 JSON.stringify({ scripts: [{ name, command: `npm run dev -- --port ${name.slice(-4)}` }] });
302 t3File.disk = t3Json("Dev 4100");
303 t3File.cached = t3File.disk;
304 await using _screen = await render(<Panel fixture={fixture} withScripts />);
305
306 const chooser = page.getByRole("button", { name: "Choose project action" });
307 await chooser.click();
308 await expect.element(page.getByRole("menuitem", { name: /Dev 4100/ })).toBeVisible();
309 await chooser.click();
310 await expect.element(page.getByRole("menu")).not.toBeInTheDocument();
311
312 // A setup hook rewrites this checkout's t3.json outside T3 Code.
313 t3File.disk = t3Json("Dev 4200");
314
315 await chooser.click();
316 await expect.element(page.getByRole("menuitem", { name: /Dev 4200/ })).toBeVisible();
317});