Open History to see the last commit
1import {
2 EnvironmentId,
3 ThreadId,
4 ScheduledTaskId,
5 type ScheduledTask,
6 type ScheduledTaskListResult,
7} from "@t3tools/contracts";
8import { act } from "react";
9import { createRoot, type Root } from "react-dom/client";
10import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
11
12const state = vi.hoisted(() => ({ allowed: true, toggle: vi.fn(), run: vi.fn() }));
13vi.mock(import("../../state/session"), async (importOriginal) => ({
14 ...(await importOriginal()),
15 useEnvironmentScope: () => state.allowed,
16 readEnvironmentScope: () => state.allowed,
17}));
18// The panel reads the run permission through an atom; the grant is the boundary under test.
19vi.mock(import("@effect/atom-react"), async (importOriginal) => ({
20 ...(await importOriginal()),
21 useAtomValue: (() => state.allowed) as never,
22}));
23vi.mock(import("@tanstack/react-router"), async (importOriginal) => ({
24 ...(await importOriginal()),
25 useNavigate: () => vi.fn(),
26}));
27vi.mock(import("../../state/use-atom-command"), async (importOriginal) => {
28 const { serverEnvironment } = await import("../../state/server");
29 return {
30 ...(await importOriginal()),
31 useAtomCommand: ((command: unknown) =>
32 command === serverEnvironment.setScheduledTaskEnabled ? state.toggle : state.run) as never,
33 };
34});
35vi.mock(import("../../state/query"), async (importOriginal) => ({
36 ...(await importOriginal()),
37 useEnvironmentQuery: (() => ({ data: listResult, error: null })) as never,
38}));
39
40import { ThreadAutomationsPanel } from "./ThreadAutomationsPanel";
41const environmentId = EnvironmentId.make("remote");
42const threadId = ThreadId.make("thread");
43const task = {
44 id: ScheduledTaskId.make("task"),
45 threadId,
46 title: "Daily review",
47 enabled: true,
48 schedule: { type: "interval", everyMs: 60000 },
49 lastRunStatus: "never",
50 nextRunAt: null,
51} as ScheduledTask;
52let listResult: ScheduledTaskListResult & { timeZone?: string };
53let root: Root;
54let container: HTMLDivElement;
55beforeEach(() => {
56 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
57 state.allowed = true;
58 listResult = { tasks: [task] };
59 state.toggle.mockReset().mockResolvedValue({ _tag: "Success" });
60 state.run.mockReset().mockResolvedValue({ _tag: "Success" });
61 container = document.createElement("div");
62 document.body.append(container);
63 root = createRoot(container);
64});
65afterEach(async () => {
66 await act(async () => root.unmount());
67 container.remove();
68 vi.unstubAllGlobals();
69});
70
71async function renderPanel() {
72 await act(async () =>
73 root.render(<ThreadAutomationsPanel environmentId={environmentId} threadId={threadId} />),
74 );
75}
76
77const byLabel = (label: string) => container.querySelector<HTMLElement>(`[aria-label="${label}"]`)!;
78
79it.each([true, false])("runs scheduled work only with task permission (%s)", async (allowed) => {
80 state.allowed = allowed;
81 await renderPanel();
82 await act(async () => byLabel("Run Daily review now").click());
83 expect(state.run).toHaveBeenCalledTimes(allowed ? 1 : 0);
84});
85
86it("rejects a retained toggle after task permission is revoked", async () => {
87 await renderPanel();
88 state.allowed = false;
89 await act(async () => byLabel("Pause Daily review").click());
90 expect(state.toggle).not.toHaveBeenCalled();
91
92 state.allowed = true;
93 await act(async () => byLabel("Pause Daily review").click());
94 expect(state.toggle).toHaveBeenCalledOnce();
95});
96
97it("labels fixed times with the server zone and preserves labels from older servers", async () => {
98 const tasks: ScheduledTask[] = [
99 { ...task, schedule: { type: "fixed_time", timeOfDay: "09:00" } },
100 { ...task, id: ScheduledTaskId.make("interval"), title: "Interval review" },
101 {
102 ...task,
103 id: ScheduledTaskId.make("webhook"),
104 title: "Webhook review",
105 schedule: { type: "webhook", signature: null },
106 },
107 ];
108 const labels = () => Array.from(container.querySelectorAll("li p"), (row) => row.textContent);
109
110 listResult = { tasks, timeZone: "Asia/Kolkata" };
111 await renderPanel();
112 expect(labels()).toEqual(["Daily at 09:00 Asia/Kolkata", "Every 1 min", "On webhook"]);
113
114 listResult = { tasks, timeZone: "Etc/UTC" };
115 await renderPanel();
116 expect(labels()).toEqual(["Daily at 09:00 UTC", "Every 1 min", "On webhook"]);
117
118 listResult = { tasks, timeZone: "UTC" };
119 await renderPanel();
120 expect(labels()).toEqual(["Daily at 09:00 UTC", "Every 1 min", "On webhook"]);
121
122 listResult = { tasks };
123 await renderPanel();
124 expect(labels()).toEqual(["Daily at 09:00", "Every 1 min", "On webhook"]);
125});