Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 AuthSourceControlWriteScope,
4 EnvironmentId,
5 ThreadId,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import { AsyncResult } from "effect/reactivity";
9import { memo } from "react";
10import { describe, expect, it, vi } from "vite-plus/test";
11import { page, userEvent } from "vite-plus/test/browser";
12
13import "~/index.css";
14import { act, render } from "../../test/render";
15
16type Branched = { readonly branch: string };
17type GitActionInput = { featureBranch?: boolean; threadId?: string; action: string };
18type Toast = { actionProps?: { onClick: () => void } };
19
20const state = vi.hoisted(() => ({
21 scopes: new Set<string>(),
22 primaryScopes: new Set<string>(),
23 shell: null as Branched | null,
24 detail: null as Branched | null,
25 draft: null as (Branched & { worktreePath: null; envMode: "local" }) | null,
26 branch: "main",
27 isDefaultRef: false,
28 actions: [] as { scope: { cwd: string | null }; input: GitActionInput }[],
29 metadataRequests: [] as { environmentId: string; branch: string }[],
30 toasts: [] as Toast[],
31 afterGitAction: undefined as (() => void) | undefined,
32 workingTreeRoot: undefined as string | undefined,
33 files: [{ path: "file.ts", insertions: 1, deletions: 0 }],
34 editorOpens: [] as { environmentId: string; input: { cwd: string; editor: string } }[],
35}));
36
37vi.mock(import("~/state/session"), async (original) => {
38 const hasScope = (environmentId: unknown, scope: string) =>
39 (environmentId === "environment" ? state.scopes : state.primaryScopes).has(scope);
40 return {
41 ...(await original()),
42 useEnvironmentScope: hasScope as never,
43 readEnvironmentScope: hasScope as never,
44 };
45});
46vi.mock(import("~/state/entities"), async (original) => ({
47 ...(await original()),
48 useQuietThreadShell: ((ref: { threadId: string } | null) =>
49 ref === null || state.shell === null ? null : { ...state.shell, id: ref.threadId }) as never,
50 useThreadProjection: ((ref: unknown) =>
51 ref === null || state.detail === null
52 ? null
53 : { projection: { thread: state.detail } }) as never,
54}));
55vi.mock(import("~/state/query"), async (original) => ({
56 ...(await original()),
57 useEnvironmentQuery: (() => ({
58 data: {
59 sourceControlProviders: [],
60 isRepo: true,
61 refName: state.branch,
62 isDefaultRef: state.isDefaultRef,
63 hasPrimaryRemote: true,
64 hasWorkingTreeChanges: true,
65 workingTree: {
66 ...(state.workingTreeRoot === undefined ? {} : { rootPath: state.workingTreeRoot }),
67 files: state.files,
68 insertions: 1,
69 deletions: 0,
70 },
71 hasUpstream: true,
72 aheadCount: 0,
73 behindCount: 0,
74 pr: null,
75 },
76 error: null,
77 })) as never,
78}));
79vi.mock(import("~/state/use-atom-command"), async (original) => {
80 const { shellEnvironment } = await import("~/state/shell");
81 return {
82 ...(await original()),
83 // Only the thread's branch metadata update and editor opens are observed here.
84 useAtomCommand: ((command: unknown, options: unknown) =>
85 options === "thread branch metadata update"
86 ? async ({
87 environmentId,
88 input,
89 }: {
90 environmentId: string;
91 input: { branch: string };
92 }) => {
93 state.metadataRequests.push({ environmentId, branch: input.branch });
94 if (!state.scopes.has(AuthOrchestrationOperateScope))
95 return AsyncResult.failure(Cause.fail(new Error("Task denied")));
96 if (state.shell) state.shell = { branch: input.branch };
97 if (state.detail) state.detail = { branch: input.branch };
98 return AsyncResult.success(undefined);
99 }
100 : command === shellEnvironment.openInEditor
101 ? async (request: (typeof state.editorOpens)[number]) => {
102 state.editorOpens.push(request);
103 return AsyncResult.success(undefined);
104 }
105 : async () => AsyncResult.success(undefined)) as never,
106 };
107});
108// The preferred-editor flow needs one editor the environment reports as installed.
109vi.mock(import("~/state/server"), async (original) => {
110 const actual = await original();
111 const { Atom } = await import("effect/reactivity");
112 const config = Atom.make(() => ({ availableEditors: ["cursor"] }));
113 return {
114 ...actual,
115 serverEnvironment: { ...actual.serverEnvironment, configValueAtom: (() => config) as never },
116 };
117});
118vi.mock(import("~/lib/sourceControlActions"), async (original) => ({
119 ...(await original()),
120 useSourceControlActionRunning: () => false,
121 useGitStackedAction: ((scope: { cwd: string | null }) => ({
122 run: async (input: GitActionInput) => {
123 state.actions.push({ scope, input });
124 if (input.featureBranch) state.branch = "feature";
125 state.afterGitAction?.();
126 return {
127 _tag: "Success",
128 value: {
129 branch: input.featureBranch
130 ? { status: "created", name: "feature" }
131 : { status: "skipped_not_requested" },
132 toast: {
133 title: "Committed",
134 description: "Committed",
135 cta: {
136 kind: "run_action",
137 label: "Push & create PR",
138 action: { kind: "commit_push_pr" },
139 },
140 },
141 },
142 };
143 },
144 })) as never,
145}));
146vi.mock(import("@tanstack/react-router"), async (original) => ({
147 ...(await original()),
148 useNavigate: (() => async () => {}) as never,
149}));
150vi.mock(import("~/components/ui/toast"), async (original) => {
151 const actual = await original();
152 return {
153 ...actual,
154 toastManager: {
155 ...actual.toastManager,
156 add: ((toast: Toast) => {
157 state.toasts.push(toast);
158 return "toast";
159 }) as never,
160 },
161 };
162});
163
164import { useComposerDraftStore } from "~/composerDraftStore";
165import GitActionsControl from "./GitActionsControl";
166
167const MemoGitActionsControl = memo(GitActionsControl);
168
169function control(thread = "thread", cwd = "/repo") {
170 return (
171 <MemoGitActionsControl
172 gitCwd={cwd}
173 activeThreadRef={{
174 environmentId: EnvironmentId.make("environment"),
175 threadId: ThreadId.make(thread),
176 }}
177 />
178 );
179}
180
181const button = (name: string | RegExp) =>
182 page.getByRole("button", { name, exact: typeof name === "string" });
183
184// The mocked modules read `state`, so every test starts by resetting all of it.
185function setup() {
186 state.scopes = new Set([AuthSourceControlWriteScope]);
187 state.primaryScopes = new Set([AuthSourceControlWriteScope, AuthOrchestrationOperateScope]);
188 state.shell = { branch: "main" };
189 state.detail = null;
190 state.draft = null;
191 state.branch = "main";
192 state.isDefaultRef = false;
193 state.actions = [];
194 state.metadataRequests = [];
195 state.toasts = [];
196 state.afterGitAction = undefined;
197 state.workingTreeRoot = undefined;
198 state.files = [{ path: "file.ts", insertions: 1, deletions: 0 }];
199 state.editorOpens = [];
200 useComposerDraftStore.setState({
201 getDraftSession: () => state.draft as never,
202 getDraftThreadByRef: () => state.draft as never,
203 setDraftThreadContext: (_target, input) => {
204 if (state.draft && input.branch) state.draft = { ...state.draft, branch: input.branch };
205 },
206 });
207}
208
209describe("Git action targets across thread navigation", () => {
210 const quickAction = () => button(/^Commit, push & /);
211
212 it("runs a retained PR toast in the thread that produced it", async () => {
213 setup();
214 await using screen = await render(control("first", "/first"));
215 await userEvent.click(quickAction());
216 await vi.waitFor(() => expect(state.toasts.some((toast) => toast.actionProps)).toBe(true));
217 const toast = state.toasts.find((toast) => toast.actionProps)!;
218
219 await screen.rerender(control("second", "/second"));
220 await act(async () => toast.actionProps!.onClick());
221
222 expect(state.actions.at(-1)).toMatchObject({
223 scope: { cwd: "/first" },
224 input: { threadId: "first", action: "commit_push_pr" },
225 });
226 });
227});