The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { RegistryContext } from "@effect/atom-react"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { Atom, AtomRegistry } from "effect/reactivity"; |
| 4 | import { |
| 5 | DEFAULT_UNIFIED_SETTINGS, |
| 6 | EnvironmentId, |
| 7 | ProjectId, |
| 8 | ProviderInstanceId, |
| 9 | ScheduledTaskId, |
| 10 | ThreadId, |
| 11 | type ScheduledTask, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 14 | import { page, userEvent } from "vite-plus/test/browser"; |
| 15 | |
| 16 | import "~/index.css"; |
| 17 | import { act, render } from "../../../test/render"; |
| 18 | import { ScheduledTasksSettings } from "./ScheduledTasksSettings"; |
| 19 | import { ConfirmDialogHost } from "../ConfirmDialogHost"; |
| 20 | |
| 21 | const environmentId = EnvironmentId.make("laptop"); |
| 22 | const projectId = ProjectId.make("project"); |
| 23 | const task: ScheduledTask = { |
| 24 | id: ScheduledTaskId.make("nightly"), |
| 25 | title: "Review issues", |
| 26 | prompt: "Review open issues", |
| 27 | enabled: true, |
| 28 | schedule: { type: "interval", everyMs: 60_000 }, |
| 29 | projectId, |
| 30 | threadId: ThreadId.make("older-thread"), |
| 31 | workspaceStrategy: { type: "root" }, |
| 32 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 33 | runtimeMode: "full-access", |
| 34 | interactionMode: "default", |
| 35 | createdBy: "user", |
| 36 | creationSource: "web", |
| 37 | createdAt: "2026-09-17T00:00:00.000Z", |
| 38 | updatedAt: "2026-09-17T00:00:00.000Z", |
| 39 | nextRunAt: null, |
| 40 | lastRunAt: null, |
| 41 | lastRunStatus: "never", |
| 42 | lastRunError: null, |
| 43 | runCount: 0, |
| 44 | }; |
| 45 | |
| 46 | // Stands in for the thread family read the editor makes when the shell lacks the bound thread. |
| 47 | const threadFamilyRead = { |
| 48 | snapshotSequence: 1, |
| 49 | threads: [ |
| 50 | { |
| 51 | id: task.threadId, |
| 52 | title: "Fix login bug", |
| 53 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.5" }, |
| 54 | }, |
| 55 | ], |
| 56 | }; |
| 57 | |
| 58 | const state = vi.hoisted(() => ({ |
| 59 | connected: true, |
| 60 | tasksLoaded: true, |
| 61 | secondary: false, |
| 62 | remove: vi.fn(), |
| 63 | })); |
| 64 | const secondaryId = EnvironmentId.make("secondary"); |
| 65 | const permissions = new Map<string, Atom.Writable<boolean>>(); |
| 66 | |
| 67 | function setup() { |
| 68 | state.connected = true; |
| 69 | state.tasksLoaded = true; |
| 70 | state.remove.mockReset().mockReturnValue(Effect.succeed({ id: task.id })); |
| 71 | state.secondary = false; |
| 72 | permissions.clear(); |
| 73 | permissions.set(environmentId, Atom.make(true)); |
| 74 | permissions.set(secondaryId, Atom.make(true)); |
| 75 | permissions.set("", Atom.make(false)); |
| 76 | } |
| 77 | |
| 78 | function secondaryEnvironment() { |
| 79 | return { ...environment(), environmentId: secondaryId, label: "Secondary" }; |
| 80 | } |
| 81 | |
| 82 | vi.mock(import("../../state/session"), async (original) => ({ |
| 83 | ...(await original()), |
| 84 | readEnvironmentScope: () => true, |
| 85 | })); |
| 86 | |
| 87 | function environment() { |
| 88 | return { |
| 89 | environmentId, |
| 90 | label: "Laptop", |
| 91 | connection: { phase: state.connected ? "connected" : "disconnected" }, |
| 92 | serverConfig: state.connected |
| 93 | ? { environment: { platform: "linux", capabilities: { oneTimeSchedules: true } } } |
| 94 | : null, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | vi.mock(import("./SettingsScopeContext"), async (original) => { |
| 99 | const scope = () => { |
| 100 | const entry = environment(); |
| 101 | return { |
| 102 | scope: { kind: "all", label: "All", members: [], environmentIds: [environmentId] }, |
| 103 | environments: state.secondary ? [entry, secondaryEnvironment()] : [entry], |
| 104 | connectedEnvironments: state.connected |
| 105 | ? state.secondary |
| 106 | ? [entry, secondaryEnvironment()] |
| 107 | : [entry] |
| 108 | : [], |
| 109 | environment: entry, |
| 110 | target: null, |
| 111 | targets: [], |
| 112 | }; |
| 113 | }; |
| 114 | return { |
| 115 | ...(await original()), |
| 116 | useSettingsScope: scope as never, |
| 117 | }; |
| 118 | }); |
| 119 | vi.mock(import("../../state/server"), async (original) => { |
| 120 | const actual = await original(); |
| 121 | |
| 122 | return { |
| 123 | ...actual, |
| 124 | serverEnvironment: { |
| 125 | ...actual.serverEnvironment, |
| 126 | deleteScheduledTask: { |
| 127 | ...actual.serverEnvironment.deleteScheduledTask, |
| 128 | run: state.remove, |
| 129 | }, |
| 130 | upsertScheduledTask: { |
| 131 | ...actual.serverEnvironment.upsertScheduledTask, |
| 132 | permissionAtom: (id: string | null) => permissions.get(id ?? "")!, |
| 133 | }, |
| 134 | }, |
| 135 | }; |
| 136 | }); |
| 137 | vi.mock(import("../../state/threads"), async (original) => { |
| 138 | const actual = await original(); |
| 139 | return { |
| 140 | ...actual, |
| 141 | threadFamilies: { ...actual.threadFamilies, familyAtom: (() => threadFamilyRead) as never }, |
| 142 | }; |
| 143 | }); |
| 144 | // Mirrors the real query: a null request (disconnected environment) has no data. |
| 145 | vi.mock(import("../../state/query"), async (original) => ({ |
| 146 | ...(await original()), |
| 147 | useEnvironmentQuery: ((request: unknown) => ({ |
| 148 | data: |
| 149 | request === null || request === threadFamilyRead |
| 150 | ? request |
| 151 | : { tasks: state.tasksLoaded ? [task] : [] }, |
| 152 | error: null, |
| 153 | isPending: false, |
| 154 | refresh: vi.fn(), |
| 155 | })) as never, |
| 156 | })); |
| 157 | vi.mock(import("../../state/environments"), async (original) => ({ |
| 158 | ...(await original()), |
| 159 | useEnvironment: ((id: EnvironmentId) => |
| 160 | id === secondaryId ? secondaryEnvironment() : environment()) as never, |
| 161 | })); |
| 162 | vi.mock(import("../../state/entities"), async (original) => ({ |
| 163 | ...(await original()), |
| 164 | useProjects: (() => [{ id: projectId, environmentId, title: "Project" }]) as never, |
| 165 | })); |
| 166 | vi.mock(import("../../hooks/useSettings"), async (original) => ({ |
| 167 | ...(await original()), |
| 168 | useEnvironmentSettings: (() => DEFAULT_UNIFIED_SETTINGS) as never, |
| 169 | })); |
| 170 | |
| 171 | describe("ScheduledTasksSettings", () => { |
| 172 | it("only deletes a scheduled task after confirmation, including after cancelling", async () => { |
| 173 | setup(); |
| 174 | await using screen = await render( |
| 175 | <> |
| 176 | <ScheduledTasksSettings /> |
| 177 | <ConfirmDialogHost /> |
| 178 | </>, |
| 179 | ); |
| 180 | await userEvent.click(screen.getByRole("button", { name: "Actions for Review issues" })); |
| 181 | await userEvent.click(page.getByRole("menuitem", { name: "Delete", exact: true })); |
| 182 | await expect.element(page.getByRole("alertdialog")).toBeVisible(); |
| 183 | expect(state.remove).not.toHaveBeenCalled(); |
| 184 | await userEvent.click(page.getByRole("button", { name: "Cancel", exact: true })); |
| 185 | expect(state.remove).not.toHaveBeenCalled(); |
| 186 | await expect |
| 187 | .element(screen.getByRole("button", { name: "Actions for Review issues" })) |
| 188 | .toBeVisible(); |
| 189 | await userEvent.click(screen.getByRole("button", { name: "Actions for Review issues" })); |
| 190 | await userEvent.click(page.getByRole("menuitem", { name: "Delete", exact: true })); |
| 191 | await userEvent.click(page.getByRole("button", { name: "Confirm", exact: true })); |
| 192 | await vi.waitFor(() => |
| 193 | expect(state.remove).toHaveBeenCalledExactlyOnceWith(expect.anything(), { |
| 194 | environmentId, |
| 195 | input: { id: task.id }, |
| 196 | }), |
| 197 | ); |
| 198 | }); |
| 199 | |
| 200 | it("creates on a permitted secondary and reacts to permission changes", async () => { |
| 201 | setup(); |
| 202 | state.secondary = true; |
| 203 | const registry = AtomRegistry.make(); |
| 204 | using _registry = { [Symbol.dispose]: () => registry.dispose() }; |
| 205 | registry.set(permissions.get(environmentId)!, false); |
| 206 | await using screen = await render( |
| 207 | <RegistryContext.Provider value={registry}> |
| 208 | <ScheduledTasksSettings /> |
| 209 | </RegistryContext.Provider>, |
| 210 | ); |
| 211 | const create = screen.getByRole("button", { name: "New task" }); |
| 212 | await expect.element(create).toBeEnabled(); |
| 213 | await act(() => registry.set(permissions.get(secondaryId)!, false)); |
| 214 | await expect.element(create).toBeDisabled(); |
| 215 | await act(() => registry.set(permissions.get(secondaryId)!, true)); |
| 216 | await expect.element(create).toBeEnabled(); |
| 217 | await create.click(); |
| 218 | await expect.element(page.getByLabelText("Runs on")).toHaveTextContent("Secondary"); |
| 219 | }); |
| 220 | |
| 221 | it("keeps the permitted representative as the creation target", async () => { |
| 222 | setup(); |
| 223 | state.secondary = true; |
| 224 | await using screen = await render(<ScheduledTasksSettings />); |
| 225 | await screen.getByRole("button", { name: "New task" }).click(); |
| 226 | await expect.element(page.getByLabelText("Runs on")).toHaveTextContent("Laptop"); |
| 227 | }); |
| 228 | |
| 229 | it("disables creation without a connected target", async () => { |
| 230 | setup(); |
| 231 | state.connected = false; |
| 232 | await using screen = await render(<ScheduledTasksSettings />); |
| 233 | await expect.element(screen.getByRole("button", { name: "New task" })).toBeDisabled(); |
| 234 | }); |
| 235 | |
| 236 | it("keeps a linked task's unsaved edits across a disconnect and reconnect", async () => { |
| 237 | setup(); |
| 238 | await using screen = await render(<ScheduledTasksSettings taskId={task.id} />); |
| 239 | await expect.element(page.getByRole("dialog").getByText("Fix login bug")).toBeVisible(); |
| 240 | const name = page.getByRole("dialog").getByRole("textbox", { name: "Name" }); |
| 241 | await expect.element(name).toHaveValue("Review issues"); |
| 242 | await userEvent.fill(name, "Review stale issues"); |
| 243 | |
| 244 | state.connected = false; |
| 245 | await screen.rerender(<ScheduledTasksSettings taskId={task.id} />); |
| 246 | |
| 247 | await expect.element(name).toHaveValue("Review stale issues"); |
| 248 | |
| 249 | state.connected = true; |
| 250 | await screen.rerender(<ScheduledTasksSettings taskId={task.id} />); |
| 251 | await expect.element(name).toHaveValue("Review stale issues"); |
| 252 | }); |
| 253 | |
| 254 | it("offers no model choice for a task bound to a thread, whose runs use the thread's model", async () => { |
| 255 | setup(); |
| 256 | await using _screen = await render(<ScheduledTasksSettings taskId={task.id} />); |
| 257 | await expect.element(page.getByRole("dialog").getByText("Fix login bug")).toBeVisible(); |
| 258 | const enabledPicker = document.querySelector( |
| 259 | '[role="dialog"] [data-chat-provider-model-picker]:not(:disabled)', |
| 260 | ); |
| 261 | expect(enabledPicker).toBeNull(); |
| 262 | await expect |
| 263 | .element(page.getByRole("dialog").getByRole("button", { name: "gpt-5.5", exact: true })) |
| 264 | .toBeDisabled(); |
| 265 | }); |
| 266 | |
| 267 | it("keeps an editor the user opened when the linked task loads later", async () => { |
| 268 | setup(); |
| 269 | state.tasksLoaded = false; |
| 270 | await using screen = await render(<ScheduledTasksSettings taskId={task.id} />); |
| 271 | await userEvent.click(page.getByRole("button", { name: "New task" })); |
| 272 | const dialog = page.getByRole("dialog"); |
| 273 | const name = dialog.getByRole("textbox", { name: "Name" }); |
| 274 | await userEvent.fill(name, "My draft"); |
| 275 | |
| 276 | state.tasksLoaded = true; |
| 277 | await screen.rerender(<ScheduledTasksSettings taskId={task.id} />); |
| 278 | |
| 279 | await expect.element(dialog.getByRole("heading", { name: "New task" })).toBeInTheDocument(); |
| 280 | await expect.element(name).toHaveValue("My draft"); |
| 281 | }); |
| 282 | }); |