Open History to see the last commit
1import {
2 AuthFilesystemReadScope,
3 AuthTerminalOperateScope,
4 EnvironmentId,
5 ProjectId,
6 type WorktreeLayoutCommand,
7} from "@t3tools/contracts";
8import { describe, expect, it, vi } from "vite-plus/test";
9import { commands, page, userEvent } from "vite-plus/test/browser";
10
11// The popup sits above the dialog's backdrop only with the app's styles.
12import "~/index.css";
13
14const environmentId = EnvironmentId.make("environment:layout");
15const projectId = ProjectId.make("project:layout");
16
17const state = vi.hoisted(() => ({
18 commands: [] as WorktreeLayoutCommand[],
19 approved: [] as unknown[],
20}));
21
22vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => ({
23 ...(await original()),
24 runAtomCommand: (async (_registry: unknown, command: string, value: { input: unknown }) => {
25 if (command === "approve") state.approved.push(value.input);
26 return { _tag: "Success", value: { commands: state.commands } };
27 }) as never,
28}));
29vi.mock(
30 import("../state/filesystem"),
31 () =>
32 ({
33 filesystemEnvironment: { readLayoutCommands: "read", approveLayoutCommands: "approve" },
34 }) as never,
35);
36vi.mock(
37 import("../state/session"),
38 () =>
39 ({
40 readEnvironmentScope: (_: EnvironmentId, scope: string) =>
41 scope === AuthFilesystemReadScope || scope === AuthTerminalOperateScope,
42 }) as never,
43);
44vi.mock(
45 import("../state/presentation"),
46 () => ({ environmentPresentations: { serverConfigsAtom: "server-configs" } }) as never,
47);
48vi.mock(
49 import("../rpc/atomRegistry"),
50 () => ({ appAtomRegistry: { get: () => new Map() } }) as never,
51);
52
53import { act, render } from "../../test/render";
54import { ensureWorktreeLayoutApproved } from "../worktreeLayoutApproval";
55import { WorktreeLayoutApprovalDialogHost } from "./WorktreeLayoutApprovalDialogHost";
56
57const ask = (command: string) => {
58 state.commands = [{ kind: "create", command, approved: false }];
59 return ensureWorktreeLayoutApproved({ environmentId, projectId, projectTitle: "Shop" });
60};
61
62describe("worktree layout approval dialog", () => {
63 it("does not approve a request that replaced the one on screen when the press began", async () => {
64 state.approved = [];
65 await using _screen = await render(<WorktreeLayoutApprovalDialogHost />, { router: false });
66 let first: ReturnType<typeof ask> | undefined;
67 await act(() => {
68 first = ask("./scripts/new-workspace.sh");
69 });
70 await expect.element(page.getByText("./scripts/new-workspace.sh")).toBeVisible();
71
72 await userEvent.hover(page.getByRole("button", { name: "Approve and continue" }));
73 await commands.pointerDown();
74 let second: ReturnType<typeof ask> | undefined;
75 try {
76 await act(() => {
77 second = ask("./scripts/other.sh");
78 });
79 await expect.element(page.getByText("./scripts/other.sh")).toBeVisible();
80 } finally {
81 // A step that fails here must not leave the button down for the tests that follow.
82 await commands.pointerUp();
83 }
84 await expect(first).resolves.toEqual({ status: "cancelled" });
85
86 // The replacement is still waiting for its own answer.
87 await expect.element(page.getByText("./scripts/other.sh")).toBeVisible();
88 expect(state.approved).toEqual([]);
89
90 await userEvent.click(page.getByRole("button", { name: "Approve and continue" }));
91 await expect(second).resolves.toEqual({ status: "proceed" });
92 expect(state.approved).toEqual([
93 { projectId, approvals: [{ kind: "create", command: "./scripts/other.sh" }] },
94 ]);
95 });
96});