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

worktreeLayoutApproval.test.ts

216 lines7.4 KB

1import {
2 AuthFilesystemReadScope,
3 AuthTerminalOperateScope,
4 EnvironmentId,
5 ProjectId,
6 type AuthEnvironmentScope,
7 type ManagedPolicySummary,
8 type WorktreeLayoutCommand,
9} from "@t3tools/contracts";
10import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
11​
12const environmentId = EnvironmentId.make("environment:layout");
13const projectId = ProjectId.make("project:layout");
14​
15const state = vi.hoisted(() => ({
16 scopes: new Set<string>(),
17 commands: [] as WorktreeLayoutCommand[] | null,
18 policy: undefined as ManagedPolicySummary | undefined,
19 approveFails: false,
20 approved: [] as unknown[],
21}));
22​
23vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => ({
24 ...(await original()),
25 runAtomCommand: (async (_registry: unknown, command: string, value: { input: unknown }) => {
26 if (command === "read") {
27 return state.commands === null
28 ? { _tag: "Failure", cause: new Error("unavailable") }
29 : { _tag: "Success", value: { commands: state.commands } };
30 }
31 state.approved.push(value.input);
32 return state.approveFails
33 ? { _tag: "Failure", cause: new Error("refused") }
34 : { _tag: "Success", value: { commands: [] } };
35 }) as never,
36 squashAtomCommandFailure: ((result: { cause: unknown }) => result.cause) as never,
37}));
38vi.mock(
39 import("./state/filesystem"),
40 () =>
41 ({
42 filesystemEnvironment: { readLayoutCommands: "read", approveLayoutCommands: "approve" },
43 }) as never,
44);
45vi.mock(
46 import("./state/session"),
47 () =>
48 ({
49 readEnvironmentScope: (_: EnvironmentId, scope: AuthEnvironmentScope) =>
50 state.scopes.has(scope),
51 }) as never,
52);
53vi.mock(
54 import("./state/presentation"),
55 () =>
56 ({
57 environmentPresentations: { serverConfigsAtom: "server-configs" },
58 }) as never,
59);
60vi.mock(
61 import("./rpc/atomRegistry"),
62 () =>
63 ({
64 appAtomRegistry: { get: () => new Map([[environmentId, { managedPolicy: state.policy }]]) },
65 }) as never,
66);
67​
68import {
69 ensureWorktreeLayoutApproved,
70 readWorktreeLayoutApprovalState,
71 respondToWorktreeLayoutApproval,
72 subscribeWorktreeLayoutApprovalHost,
73} from "./worktreeLayoutApproval";
74​
75const create: WorktreeLayoutCommand = {
76 kind: "create",
77 command: "./scripts/new-workspace.sh",
78 approved: false,
79};
80const remove: WorktreeLayoutCommand = {
81 kind: "remove",
82 command: "./scripts/remove-workspace.sh",
83 approved: false,
84};
85const ensure = () =>
86 ensureWorktreeLayoutApproved({ environmentId, projectId, projectTitle: "Shop" });
87const untilAsked = () =>
88 vi.waitFor(() => expect(readWorktreeLayoutApprovalState().open).toBe(true));
89/** Answers the request on screen, as the dialog's buttons do. */
90const respond = (approved: boolean) =>
91 respondToWorktreeLayoutApproval(readWorktreeLayoutApprovalState().request!.id, approved);
92​
93describe("worktree layout approval before a worktree thread starts", () => {
94 beforeEach(() => {
95 state.scopes = new Set([AuthFilesystemReadScope, AuthTerminalOperateScope]);
96 state.commands = [create, remove];
97 state.policy = undefined;
98 state.approveFails = false;
99 state.approved = [];
100 });
101​
102 it("shows both unapproved commands and records the texts shown once the user approves", async () => {
103 const unmount = subscribeWorktreeLayoutApprovalHost(() => {});
104 const outcome = ensure();
105 await untilAsked();
106 expect(readWorktreeLayoutApprovalState().request).toMatchObject({
107 projectTitle: "Shop",
108 commands: [create, remove],
109 });
110 expect(state.approved).toEqual([]);
111​
112 respond(true);
113 await expect(outcome).resolves.toEqual({ status: "proceed" });
114 expect(state.approved).toEqual([
115 {
116 projectId,
117 approvals: [
118 { kind: "create", command: create.command },
119 { kind: "remove", command: remove.command },
120 ],
121 },
122 ]);
123 unmount();
124 });
125​
126 it("asks only about the command whose text is not approved", async () => {
127 state.commands = [{ ...create, approved: true }, remove];
128 const unmount = subscribeWorktreeLayoutApprovalHost(() => {});
129 const outcome = ensure();
130 await untilAsked();
131 expect(readWorktreeLayoutApprovalState().request?.commands).toEqual([remove]);
132 respond(true);
133 await outcome;
134 expect(state.approved).toEqual([
135 { projectId, approvals: [{ kind: "remove", command: remove.command }] },
136 ]);
137 unmount();
138 });
139​
140 it("records nothing and stops the send when the user cancels", async () => {
141 const unmount = subscribeWorktreeLayoutApprovalHost(() => {});
142 const outcome = ensure();
143 await untilAsked();
144 respond(false);
145 await expect(outcome).resolves.toEqual({ status: "cancelled" });
146 expect(state.approved).toEqual([]);
147 unmount();
148 });
149​
150 it("drops an answer to a request that another one replaced", async () => {
151 const unmount = subscribeWorktreeLayoutApprovalHost(() => {});
152 const first = ensure();
153 await untilAsked();
154 const shown = readWorktreeLayoutApprovalState().request!;
155 // The file changes and a second send asks while the first is still on screen.
156 state.commands = [{ ...create, command: "./scripts/other.sh" }];
157 const second = ensure();
158 await expect(first).resolves.toEqual({ status: "cancelled" });
159 await vi.waitFor(() => expect(readWorktreeLayoutApprovalState().request).not.toBe(shown));
160​
161 respondToWorktreeLayoutApproval(shown.id, true);
162 expect(readWorktreeLayoutApprovalState().open).toBe(true);
163 expect(state.approved).toEqual([]);
164​
165 respond(false);
166 await expect(second).resolves.toEqual({ status: "cancelled" });
167 expect(state.approved).toEqual([]);
168 unmount();
169 });
170​
171 it("records nothing when nothing is mounted to show the commands", async () => {
172 await expect(ensure()).resolves.toEqual({ status: "cancelled" });
173 expect(state.approved).toEqual([]);
174 });
175​
176 it("does not ask when every command is approved, or the project declares none", async () => {
177 state.commands = [{ ...create, approved: true }];
178 await expect(ensure()).resolves.toEqual({ status: "proceed" });
179 state.commands = [];
180 await expect(ensure()).resolves.toEqual({ status: "proceed" });
181 expect(readWorktreeLayoutApprovalState().open).toBe(false);
182 });
183​
184 it("stops with a reason on a connection that cannot approve", async () => {
185 state.scopes = new Set([AuthFilesystemReadScope]);
186 const outcome = await ensure();
187 expect(outcome.status).toBe("blocked");
188 expect(state.approved).toEqual([]);
189 });
190​
191 it("leaves the answer to the server when the commands cannot be read or policy turns them off", async () => {
192 state.commands = null;
193 await expect(ensure()).resolves.toEqual({ status: "proceed" });
194 state.commands = [create];
195 state.policy = {
196 organization: "Example Corp",
197 lockedSettings: [],
198 limits: { worktreeLayoutCommands: false },
199 };
200 await expect(ensure()).resolves.toEqual({ status: "proceed" });
201 state.scopes = new Set([AuthTerminalOperateScope]);
202 state.policy = undefined;
203 await expect(ensure()).resolves.toEqual({ status: "proceed" });
204 expect(state.approved).toEqual([]);
205 });
206​
207 it("stops with the server's reason when recording the approval fails", async () => {
208 state.approveFails = true;
209 const unmount = subscribeWorktreeLayoutApprovalHost(() => {});
210 const outcome = ensure();
211 await untilAsked();
212 respond(true);
213 await expect(outcome).resolves.toEqual({ status: "blocked", message: "refused" });
214 unmount();
215 });
216});