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

ScheduledTasksSettings.browser.test.tsx

198 lines6.9 KB

1import { RegistryContext } from "@effect/atom-react";
2import { Atom, AtomRegistry } from "effect/reactivity";
3import {
4 DEFAULT_UNIFIED_SETTINGS,
5 EnvironmentId,
6 ProjectId,
7 ProviderInstanceId,
8 ScheduledTaskId,
9 type ScheduledTask,
10} from "@t3tools/contracts";
11import { describe, expect, it, vi } from "vite-plus/test";
12import { page, userEvent } from "vite-plus/test/browser";
13​
14import "~/index.css";
15import { act, render } from "../../../test/render";
16import { ScheduledTasksSettings } from "./ScheduledTasksSettings";
17​
18const environmentId = EnvironmentId.make("laptop");
19const projectId = ProjectId.make("project");
20const task: ScheduledTask = {
21 id: ScheduledTaskId.make("nightly"),
22 title: "Review issues",
23 prompt: "Review open issues",
24 enabled: true,
25 schedule: { type: "interval", everyMs: 60_000 },
26 projectId,
27 threadId: null,
28 workspaceStrategy: { type: "root" },
29 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
30 runtimeMode: "full-access",
31 interactionMode: "default",
32 createdBy: "user",
33 creationSource: "web",
34 createdAt: "2026-09-17T00:00:00.000Z",
35 updatedAt: "2026-09-17T00:00:00.000Z",
36 nextRunAt: null,
37 lastRunAt: null,
38 lastRunStatus: "never",
39 lastRunError: null,
40 runCount: 0,
41};
42​
43const state = vi.hoisted(() => ({ connected: true, tasksLoaded: true, secondary: false }));
44const secondaryId = EnvironmentId.make("secondary");
45const permissions = new Map<string, Atom.Writable<boolean>>();
46​
47function setup() {
48 state.connected = true;
49 state.tasksLoaded = true;
50 state.secondary = false;
51 permissions.clear();
52 permissions.set(environmentId, Atom.make(true));
53 permissions.set(secondaryId, Atom.make(true));
54 permissions.set("", Atom.make(false));
56​
57function secondaryEnvironment() {
58 return { ...environment(), environmentId: secondaryId, label: "Secondary" };
60​
61function environment() {
62 return {
63 environmentId,
64 label: "Laptop",
65 connection: { phase: state.connected ? "connected" : "disconnected" },
66 serverConfig: state.connected ? { environment: { platform: "linux" } } : null,
67 };
69​
70vi.mock(import("./SettingsScopeContext"), async (original) => {
71 const scope = () => {
72 const entry = environment();
73 return {
74 scope: { kind: "all", label: "All", members: [], environmentIds: [environmentId] },
75 environments: state.secondary ? [entry, secondaryEnvironment()] : [entry],
76 connectedEnvironments: state.connected
77 ? state.secondary
78 ? [entry, secondaryEnvironment()]
79 : [entry]
80 : [],
81 environment: entry,
82 target: null,
83 targets: [],
84 };
85 };
86 return {
87 ...(await original()),
88 useSettingsScope: scope as never,
89 };
90});
91vi.mock(import("../../state/server"), async (original) => {
92 const actual = await original();
93​
94 return {
95 ...actual,
96 serverEnvironment: {
97 ...actual.serverEnvironment,
98 upsertScheduledTask: {
99 ...actual.serverEnvironment.upsertScheduledTask,
100 permissionAtom: (id: string | null) => permissions.get(id ?? "")!,
101 },
102 },
103 };
104});
105// Mirrors the real query: a null request (disconnected environment) has no data.
106vi.mock(import("../../state/query"), async (original) => ({
107 ...(await original()),
108 useEnvironmentQuery: ((request: unknown) => ({
109 data: request === null ? null : { tasks: state.tasksLoaded ? [task] : [] },
110 error: null,
111 isPending: false,
112 refresh: vi.fn(),
113 })) as never,
114}));
115vi.mock(import("../../state/environments"), async (original) => ({
116 ...(await original()),
117 useEnvironment: ((id: EnvironmentId) =>
118 id === secondaryId ? secondaryEnvironment() : environment()) as never,
119}));
120vi.mock(import("../../state/entities"), async (original) => ({
121 ...(await original()),
122 useProjects: (() => [{ id: projectId, environmentId, title: "Project" }]) as never,
123}));
124vi.mock(import("../../hooks/useSettings"), async (original) => ({
125 ...(await original()),
126 useEnvironmentSettings: (() => DEFAULT_UNIFIED_SETTINGS) as never,
127}));
128​
129describe("ScheduledTasksSettings", () => {
130 it("creates on a permitted secondary and reacts to permission changes", async () => {
131 setup();
132 state.secondary = true;
133 const registry = AtomRegistry.make();
134 using _registry = { [Symbol.dispose]: () => registry.dispose() };
135 registry.set(permissions.get(environmentId)!, false);
136 await using screen = await render(
137 <RegistryContext.Provider value={registry}>
138 <ScheduledTasksSettings />
139 </RegistryContext.Provider>,
140 );
141 const create = screen.getByRole("button", { name: "New task" });
142 await expect.element(create).toBeEnabled();
143 await act(() => registry.set(permissions.get(secondaryId)!, false));
144 await expect.element(create).toBeDisabled();
145 await act(() => registry.set(permissions.get(secondaryId)!, true));
146 await expect.element(create).toBeEnabled();
147 await create.click();
148 await expect.element(page.getByLabelText("Runs on")).toHaveTextContent("Secondary");
149 });
150​
151 it("keeps the permitted representative as the creation target", async () => {
152 setup();
153 state.secondary = true;
154 await using screen = await render(<ScheduledTasksSettings />);
155 await screen.getByRole("button", { name: "New task" }).click();
156 await expect.element(page.getByLabelText("Runs on")).toHaveTextContent("Laptop");
157 });
158​
159 it("disables creation without a connected target", async () => {
160 setup();
161 state.connected = false;
162 await using screen = await render(<ScheduledTasksSettings />);
163 await expect.element(screen.getByRole("button", { name: "New task" })).toBeDisabled();
164 });
165​
166 it("keeps a linked task's unsaved edits across a disconnect and reconnect", async () => {
167 setup();
168 await using screen = await render(<ScheduledTasksSettings taskId={task.id} />);
169 const name = page.getByRole("dialog").getByRole("textbox", { name: "Name" });
170 await expect.element(name).toHaveValue("Review issues");
171 await userEvent.fill(name, "Review stale issues");
172​
173 state.connected = false;
174 await screen.rerender(<ScheduledTasksSettings taskId={task.id} />);
175​
176 await expect.element(name).toHaveValue("Review stale issues");
177​
178 state.connected = true;
179 await screen.rerender(<ScheduledTasksSettings taskId={task.id} />);
180 await expect.element(name).toHaveValue("Review stale issues");
181 });
182​
183 it("keeps an editor the user opened when the linked task loads later", async () => {
184 setup();
185 state.tasksLoaded = false;
186 await using screen = await render(<ScheduledTasksSettings taskId={task.id} />);
187 await userEvent.click(page.getByRole("button", { name: "New task" }));
188 const dialog = page.getByRole("dialog");
189 const name = dialog.getByRole("textbox", { name: "Name" });
190 await userEvent.fill(name, "My draft");
191​
192 state.tasksLoaded = true;
193 await screen.rerender(<ScheduledTasksSettings taskId={task.id} />);
194​
195 await expect.element(dialog.getByRole("heading", { name: "New task" })).toBeInTheDocument();
196 await expect.element(name).toHaveValue("My draft");
197 });
198});