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

PullRequestThreadDialog.test.ts

191 lines6.5 KB

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 { isValidElement, type ReactNode } from "react";
10import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
11​
12const state = vi.hoisted(() => ({
13 canOperate: false,
14 canWriteSourceControl: true,
15 hookValues: [] as unknown[],
16 hookIndex: 0,
17 tracks: 0,
18 resets: 0,
19 manager: { operation: null as string | null, error: null, isRunning: false },
20 interrupted: false,
21 pendingResponse: undefined as Promise<void> | undefined,
22 openChanges: [] as boolean[],
23 checkouts: [] as string[],
24 setupScripts: 0,
25 prepared: [] as { branch: string; worktreePath: string | null }[],
26}));
27​
28vi.mock("react", async (importOriginal) => ({
29 ...(await importOriginal<typeof import("react")>()),
30 useCallback: (callback: unknown) => callback,
31 useMemo: (factory: () => unknown) => factory(),
32 useState: (initial: unknown) => {
33 const index = state.hookIndex++;
34 if (index === state.hookValues.length) state.hookValues.push(initial);
35 return [state.hookValues[index], (value: unknown) => (state.hookValues[index] = value)];
36 },
37 useRef: (current: unknown) => ({ current }),
38 useEffect: () => {},
39}));
40vi.mock("gt-react", async (importOriginal) => {
41 const gt = await importOriginal<typeof import("gt-react")>();
42 // GT's testing entry renders the English source with no provider and no React.
43 const sourceGT = await (await import("gt-react/testing")).getGT();
44 return {
45 ...gt,
46 useGT: () => sourceGT,
47 useMessages: () => gt.mFallback,
48 T: ({ children }: { children: ReactNode }) => children,
49 Var: ({ children }: { children: ReactNode }) => children,
50 };
51});
52vi.mock("@tanstack/react-pacer", () => ({
53 useDebouncedValue: (value: unknown) => [value, { state: { isPending: false } }],
54}));
55vi.mock("~/state/session", () => ({
56 useEnvironmentScope: (_environmentId: unknown, scope: string) =>
57 scope === AuthOrchestrationOperateScope
58 ? state.canOperate
59 : scope === AuthSourceControlWriteScope && state.canWriteSourceControl,
60 readEnvironmentScope: (_environmentId: unknown, scope: string) =>
61 scope === AuthOrchestrationOperateScope
62 ? state.canOperate
63 : scope === AuthSourceControlWriteScope && state.canWriteSourceControl,
64}));
65vi.mock("~/state/sourceControlActions", async () => {
66 const { usePreparePullRequestThreadAction } = await vi.importActual<
67 typeof import("~/state/sourceControlActions")
68 >("~/state/sourceControlActions");
69 return {
70 readCachedPullRequestResolution: () => null,
71 usePullRequestResolutionState: () => ({
72 data: { pullRequest: { number: 123, title: "Pull request", state: "open" } },
73 }),
74 usePreparePullRequestThreadAction,
75 };
76});
77vi.mock("@effect/atom-react", () => ({ useAtomValue: () => state.manager }));
78vi.mock("~/rpc/atomRegistry", () => ({ appAtomRegistry: {} }));
79vi.mock("~/state/use-atom-command", () => ({
80 useAtomCommand: (command: unknown) => command,
81}));
82vi.mock("~/state/sourceControl", () => ({ sourceControlEnvironment: {} }));
83vi.mock("~/state/git", () => ({
84 gitEnvironment: {
85 preparePullRequestThread: async ({
86 input: { mode, threadId },
87 }: {
88 input: { mode: string; threadId?: string };
89 }) => {
90 state.checkouts.push(mode);
91 if (mode === "worktree" && threadId) state.setupScripts += 1;
92 await state.pendingResponse;
93 if (state.interrupted) return AsyncResult.failure(Cause.interrupt());
94 return AsyncResult.success({
95 branch: "feature/pr",
96 worktreePath: mode === "worktree" ? "/worktree" : null,
97 isOnPullRequestHead: true,
98 });
99 },
100 },
101}));
102vi.mock("~/lib/utils", () => ({ cn: () => "" }));
103vi.mock("~/state/query", () => ({ useEnvironmentQuery: () => ({ data: null }) }));
104vi.mock("~/state/vcs", () => ({
105 vcsEnvironment: { status: () => null },
106 vcsActionManager: {
107 stateAtom: () => "vcs-state",
108 track: (_registry: unknown, _target: unknown, _input: unknown, execute: () => unknown) => {
109 state.tracks += 1;
110 return execute();
111 },
112 resetError: () => state.resets++,
113 },
114}));
115vi.mock("./ui/button", () => ({ Button: "Button" }));
116vi.mock("./ui/input", () => ({ Input: "Input" }));
117vi.mock("./ui/spinner", () => ({ Spinner: "Spinner" }));
118vi.mock("./ui/toast", () => ({ toastManager: { add: vi.fn() } }));
119vi.mock("./ui/dialog", () => ({
120 Dialog: "Dialog",
121 DialogDescription: "DialogDescription",
122 DialogFooter: "DialogFooter",
123 DialogHeader: "DialogHeader",
124 DialogPanel: "DialogPanel",
125 DialogPopup: "DialogPopup",
126 DialogTitle: "DialogTitle",
127}));
128​
129import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
130​
131function findAction(node: ReactNode, label: string): (() => unknown) | null {
132 if (Array.isArray(node)) {
133 for (const child of node) {
134 const action = findAction(child, label);
135 if (action) return action;
136 }
137 return null;
138 }
139 if (!isValidElement<{ children?: ReactNode; onClick?: () => unknown }>(node)) return null;
140 if (node.props.children === label) return node.props.onClick ?? null;
141 return findAction(node.props.children, label);
143​
144function renderDialog() {
145 state.hookIndex = 0;
146 return PullRequestThreadDialog({
147 open: true,
148 environmentId: EnvironmentId.make("environment"),
149 threadId: ThreadId.make("thread"),
150 cwd: "/repo",
151 initialReference: "123",
152 onOpenChange: (open) => state.openChanges.push(open),
153 onPrepared: (input) => {
154 state.prepared.push(input);
155 },
156 });
158​
159function prepareAction(label: "Local" | "Worktree") {
160 const action = findAction(renderDialog(), label);
161 if (!action) throw new Error(`${label} action missing`);
162 return action;
164​
165describe("pull request worktree permissions", () => {
166 beforeEach(() => {
167 state.canOperate = false;
168 state.canWriteSourceControl = true;
169 state.hookValues = [];
170 state.hookIndex = 0;
171 state.tracks = 0;
172 state.resets = 0;
173 state.manager = { operation: null, error: null, isRunning: false };
174 state.interrupted = false;
175 state.pendingResponse = undefined;
176 state.openChanges = [];
177 state.checkouts = [];
178 state.setupScripts = 0;
179 state.prepared = [];
180 });
181​
182 it("rechecks task permission before invoking a retained worktree action", async () => {
183 state.canOperate = true;
184 const prepare = prepareAction("Worktree");
185 state.canOperate = false;
186 await prepare();
187​
188 expect(state.checkouts).toEqual([]);
189 expect(state.setupScripts).toBe(0);
190 });
191});