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 { beforeEach, describe, expect, it, vi } from "vite-plus/test";
10
11const state = vi.hoisted(() => ({
12 scopes: new Map<string, Set<string>>(),
13 requests: [] as { action: string; environmentId: string | null }[],
14}));
15
16vi.mock("react", () => ({ useCallback: (callback: unknown) => callback }));
17vi.mock("@effect/atom-react", () => ({
18 useAtomValue: () => ({ operation: null, error: null, isRunning: false }),
19}));
20vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} }));
21vi.mock("./session", () => ({
22 useEnvironmentScope: (environmentId: string, scope: string) =>
23 state.scopes.get(environmentId)?.has(scope) === true,
24 readEnvironmentScope: (environmentId: string, scope: string) =>
25 state.scopes.get(environmentId)?.has(scope) === true,
26}));
27vi.mock("./query", () => ({ useEnvironmentQuery: () => ({ refresh: () => {} }) }));
28vi.mock("./use-atom-command", () => ({
29 useAtomCommand:
30 (command: { action: string; environmentId?: string }) =>
31 async (input: { environmentId?: string }) => {
32 state.requests.push({
33 action: command.action,
34 environmentId: input.environmentId ?? command.environmentId ?? null,
35 });
36 return AsyncResult.success({});
37 },
38}));
39vi.mock("./vcs", () => ({
40 vcsEnvironment: { init: { action: "init" }, pull: { action: "pull" }, status: () => "status" },
41 vcsActionManager: {
42 stateAtom: () => "state",
43 resetError: () => {},
44 track: (_registry: unknown, _target: unknown, _input: unknown, execute: () => unknown) =>
45 execute(),
46 runStackedAction: (scope: { environmentId: string }) => ({ action: "stack", ...scope }),
47 },
48}));
49vi.mock("./sourceControl", () => ({
50 sourceControlEnvironment: { publishRepository: { action: "publish" } },
51}));
52vi.mock("./git", () => ({
53 gitEnvironment: { preparePullRequestThread: { action: "prepare" } },
54}));
55
56import {
57 useGitStackedAction,
58 usePreparePullRequestThreadAction,
59 useSourceControlPublishRepositoryAction,
60 useVcsInitAction,
61 useVcsPullAction,
62} from "./sourceControlActions";
63
64const primary = EnvironmentId.make("primary");
65const secondary = EnvironmentId.make("secondary");
66const scope = { environmentId: secondary, cwd: "/repo" };
67const threadId = ThreadId.make("thread");
68const cases = [
69 {
70 name: "init",
71 create: () => {
72 const action = useVcsInitAction(scope);
73 return { isAllowed: action.isAllowed, run: () => action.run() };
74 },
75 },
76 {
77 name: "pull",
78 create: () => {
79 const action = useVcsPullAction(scope);
80 return { isAllowed: action.isAllowed, run: () => action.run() };
81 },
82 },
83 {
84 name: "stack",
85 create: () => {
86 const action = useGitStackedAction(scope);
87 return {
88 isAllowed: action.isAllowed,
89 run: () => action.run({ actionId: "action", action: "commit" }),
90 };
91 },
92 },
93 {
94 name: "publish",
95 create: () => {
96 const action = useSourceControlPublishRepositoryAction(scope);
97 return {
98 isAllowed: action.isAllowed,
99 run: () =>
100 action.run({
101 provider: "github",
102 repository: "owner/repo",
103 visibility: "private",
104 remoteName: "origin",
105 protocol: "ssh",
106 }),
107 };
108 },
109 },
110 {
111 name: "prepare",
112 create: () => {
113 const action = usePreparePullRequestThreadAction(scope);
114 return {
115 isAllowed: action.isAllowed,
116 run: () => action.run({ reference: "1", mode: "local" }),
117 };
118 },
119 },
120] as const;
121
122beforeEach(() => {
123 state.scopes = new Map([
124 [primary, new Set([AuthSourceControlWriteScope, AuthOrchestrationOperateScope])],
125 [secondary, new Set<string>()],
126 ]);
127 state.requests = [];
128});
129
130describe("source control callback grants", () => {
131 it.each(cases)("$name uses the selected environment's permission", async ({ create }) => {
132 const action = create();
133 expect(action.isAllowed).toBe(false);
134 expect((await action.run())._tag).toBe("Failure");
135 expect(state.requests).toEqual([]);
136 });
137
138 it.each(cases)("$name rejects a retained callback after revocation", async ({ create }) => {
139 state.scopes.get(secondary)!.add(AuthSourceControlWriteScope);
140 const action = create();
141 expect(action.isAllowed).toBe(true);
142 state.scopes.get(secondary)!.clear();
143 expect((await action.run())._tag).toBe("Failure");
144 expect(state.requests).toEqual([]);
145 expect(create().isAllowed).toBe(false);
146 });
147
148 it.each(cases)("$name accepts a retained callback after a grant", async ({ name, create }) => {
149 const action = create();
150 expect(action.isAllowed).toBe(false);
151 state.scopes.get(primary)!.clear();
152 state.scopes.get(secondary)!.add(AuthSourceControlWriteScope);
153 expect((await action.run())._tag).toBe("Success");
154 expect(state.requests).toEqual([{ action: name, environmentId: secondary }]);
155 expect(create().isAllowed).toBe(true);
156 });
157});
158
159describe("pull request worktree attachment permission", () => {
160 it.each([
161 ["local", threadId, false, true],
162 ["worktree", undefined, false, true],
163 ["worktree", threadId, false, false],
164 ["worktree", threadId, true, true],
165 ] as const)(
166 "mode %s / thread %s / task grant %s",
167 async (mode, attachedThreadId, operate, allowed) => {
168 state.scopes.get(secondary)!.add(AuthSourceControlWriteScope);
169 if (operate) state.scopes.get(secondary)!.add(AuthOrchestrationOperateScope);
170 const result = await usePreparePullRequestThreadAction(scope).run({
171 reference: "1",
172 mode,
173 ...(attachedThreadId !== undefined ? { threadId: attachedThreadId } : {}),
174 });
175 expect(result._tag).toBe(allowed ? "Success" : "Failure");
176 expect(state.requests).toHaveLength(allowed ? 1 : 0);
177 if (result._tag === "Failure") {
178 expect(Cause.squash(result.cause)).toMatchObject({
179 requiredScope: AuthOrchestrationOperateScope,
180 });
181 }
182 },
183 );
184
185 it.each([false, true])(
186 "reads the fresh task grant before attaching a worktree: %s",
187 async (allowed) => {
188 state.scopes.get(secondary)!.add(AuthSourceControlWriteScope);
189 if (!allowed) state.scopes.get(secondary)!.add(AuthOrchestrationOperateScope);
190 const action = usePreparePullRequestThreadAction(scope);
191 if (allowed) state.scopes.get(secondary)!.add(AuthOrchestrationOperateScope);
192 else state.scopes.get(secondary)!.delete(AuthOrchestrationOperateScope);
193 const result = await action.run({ reference: "1", mode: "worktree", threadId });
194 expect(result._tag).toBe(allowed ? "Success" : "Failure");
195 expect(state.requests).toHaveLength(allowed ? 1 : 0);
196 },
197 );
198
199 it("still requires source-control permission when the task grant is present", async () => {
200 state.scopes.get(secondary)!.add(AuthOrchestrationOperateScope);
201 const result = await usePreparePullRequestThreadAction(scope).run({
202 reference: "1",
203 mode: "worktree",
204 threadId,
205 });
206 expect(result._tag).toBe("Failure");
207 expect(state.requests).toEqual([]);
208 });
209});