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

ThreadAutomationsPanel.permissions.browser.test.tsx

86 lines3.2 KB

1import { EnvironmentId, ThreadId, ScheduledTaskId, type ScheduledTask } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5​
6const state = vi.hoisted(() => ({ allowed: true, toggle: vi.fn(), run: vi.fn() }));
7vi.mock(import("../../state/session"), async (importOriginal) => ({
8 ...(await importOriginal()),
9 useEnvironmentScope: () => state.allowed,
10 readEnvironmentScope: () => state.allowed,
11}));
12// The panel reads the run permission through an atom; the grant is the boundary under test.
13vi.mock(import("@effect/atom-react"), async (importOriginal) => ({
14 ...(await importOriginal()),
15 useAtomValue: (() => state.allowed) as never,
16}));
17vi.mock(import("@tanstack/react-router"), async (importOriginal) => ({
18 ...(await importOriginal()),
19 useNavigate: () => vi.fn(),
20}));
21vi.mock(import("../../state/use-atom-command"), async (importOriginal) => {
22 const { serverEnvironment } = await import("../../state/server");
23 return {
24 ...(await importOriginal()),
25 useAtomCommand: ((command: unknown) =>
26 command === serverEnvironment.setScheduledTaskEnabled ? state.toggle : state.run) as never,
27 };
28});
29vi.mock(import("../../state/query"), async (importOriginal) => ({
30 ...(await importOriginal()),
31 useEnvironmentQuery: (() => ({ data: { tasks: [task] }, error: null })) as never,
32}));
33​
34import { ThreadAutomationsPanel } from "./ThreadAutomationsPanel";
35const environmentId = EnvironmentId.make("remote");
36const threadId = ThreadId.make("thread");
37const task = {
38 id: ScheduledTaskId.make("task"),
39 threadId,
40 title: "Daily review",
41 enabled: true,
42 schedule: { type: "interval", everyMs: 60000 },
43 lastRunStatus: "never",
44} as ScheduledTask;
45let root: Root;
46let container: HTMLDivElement;
47beforeEach(() => {
48 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
49 state.allowed = true;
50 state.toggle.mockReset().mockResolvedValue({ _tag: "Success" });
51 state.run.mockReset().mockResolvedValue({ _tag: "Success" });
52 container = document.createElement("div");
53 document.body.append(container);
54 root = createRoot(container);
55});
56afterEach(async () => {
57 await act(async () => root.unmount());
58 container.remove();
59 vi.unstubAllGlobals();
60});
61​
62async function renderPanel() {
63 await act(async () =>
64 root.render(<ThreadAutomationsPanel environmentId={environmentId} threadId={threadId} />),
65 );
67​
68const byLabel = (label: string) => container.querySelector<HTMLElement>(`[aria-label="${label}"]`)!;
69​
70it.each([true, false])("runs scheduled work only with task permission (%s)", async (allowed) => {
71 state.allowed = allowed;
72 await renderPanel();
73 await act(async () => byLabel("Run Daily review now").click());
74 expect(state.run).toHaveBeenCalledTimes(allowed ? 1 : 0);
75});
76​
77it("rejects a retained toggle after task permission is revoked", async () => {
78 await renderPanel();
79 state.allowed = false;
80 await act(async () => byLabel("Pause Daily review").click());
81 expect(state.toggle).not.toHaveBeenCalled();
82​
83 state.allowed = true;
84 await act(async () => byLabel("Pause Daily review").click());
85 expect(state.toggle).toHaveBeenCalledOnce();
86});