The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { RegistryContext } from "@effect/atom-react"; |
| 2 | import { |
| 3 | EnvironmentId, |
| 4 | ProjectId, |
| 5 | ProviderInstanceId, |
| 6 | ScheduledTaskId, |
| 7 | type ScheduledTask, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { Atom } from "effect/reactivity"; |
| 10 | import { act, type ReactNode } from "react"; |
| 11 | import { createRoot, type Root } from "react-dom/client"; |
| 12 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 13 | |
| 14 | const { Children, mocks } = vi.hoisted(() => ({ |
| 15 | Children: ({ children }: { children?: ReactNode }) => children, |
| 16 | mocks: { upsert: vi.fn(), alert: vi.fn(), tasks: [] as ScheduledTask[] }, |
| 17 | })); |
| 18 | vi.mock("react-native", () => ({ |
| 19 | Platform: { OS: "ios" }, |
| 20 | View: Children, |
| 21 | TextInput: () => null, |
| 22 | Pressable: ({ children, onPress }: { children?: ReactNode; onPress: () => void }) => ( |
| 23 | <button onClick={onPress}>{children}</button> |
| 24 | ), |
| 25 | })); |
| 26 | vi.mock("@react-navigation/native", () => ({ |
| 27 | useNavigation: () => ({ isFocused: () => false }), |
| 28 | usePreventRemove: () => {}, |
| 29 | })); |
| 30 | vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) })); |
| 31 | vi.mock("@expo/ui/community/datetime-picker", () => ({ DateTimePicker: () => null })); |
| 32 | vi.mock("../../components/ui/Dialog", () => ({ alert: mocks.alert })); |
| 33 | vi.mock("../../components/ui/AppText", () => ({ AppText: Children })); |
| 34 | vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null })); |
| 35 | vi.mock("../../components/EnvironmentMachineSymbol", () => ({ |
| 36 | EnvironmentMachineSymbol: () => null, |
| 37 | ENVIRONMENT_MACHINE_SYMBOLS: {}, |
| 38 | })); |
| 39 | vi.mock("../../components/ui/Menu", () => ({ |
| 40 | Menu: ({ |
| 41 | actions, |
| 42 | onPressAction, |
| 43 | }: { |
| 44 | actions: readonly { id: string; title: string }[]; |
| 45 | onPressAction: (event: { nativeEvent: { event: string } }) => void; |
| 46 | }) => |
| 47 | actions.map(({ id, title }) => ( |
| 48 | <button key={id} onClick={() => onPressAction({ nativeEvent: { event: id } })}> |
| 49 | Select {title} |
| 50 | </button> |
| 51 | )), |
| 52 | })); |
| 53 | vi.mock("../../components/ui/ScreenScrollView", () => ({ ScreenScrollView: Children })); |
| 54 | vi.mock("./components/SettingsScreen", () => ({ SettingsScreen: Children })); |
| 55 | vi.mock("./components/SettingsSection", () => ({ SettingsSection: Children })); |
| 56 | vi.mock("./components/SettingsEnvironmentFilterHeader", () => ({ |
| 57 | useSettingsEnvironmentMenu: () => null, |
| 58 | })); |
| 59 | vi.mock("./components/ScheduledTaskPromptField", () => ({ ScheduledTaskPromptField: () => null })); |
| 60 | vi.mock("../layout/workspace/useSelectWorkspaceDetail", () => ({ |
| 61 | useSelectWorkspaceDetail: () => vi.fn(), |
| 62 | })); |
| 63 | vi.mock("../../state/threads", () => ({ threadFamilies: {} })); |
| 64 | vi.mock("../../lib/copyTextWithHaptic", () => ({ tryCopyTextWithHaptic: vi.fn() })); |
| 65 | vi.mock("../voice-input/useVoiceInputController", () => ({ |
| 66 | useVoiceInputController: () => ({ blocksSubmission: false }), |
| 67 | })); |
| 68 | vi.mock("../../components/ui/Button", () => ({ |
| 69 | Button: ({ label, onPress }: { label: string; onPress: () => void }) => ( |
| 70 | <button onClick={onPress}>{label}</button> |
| 71 | ), |
| 72 | })); |
| 73 | vi.mock("../../components/ui/SegmentedControl", () => ({ |
| 74 | SegmentedControl: ({ |
| 75 | options, |
| 76 | onSelect, |
| 77 | }: { |
| 78 | options: readonly { value: string; label: string }[]; |
| 79 | onSelect: (value: string) => void; |
| 80 | }) => |
| 81 | options.map(({ value, label }) => ( |
| 82 | <button key={value} onClick={() => onSelect(value)}> |
| 83 | {label} |
| 84 | </button> |
| 85 | )), |
| 86 | })); |
| 87 | vi.mock("../../components/ui/ThemedSwitch", () => ({ |
| 88 | ThemedSwitch: ({ |
| 89 | value, |
| 90 | onValueChange, |
| 91 | }: { |
| 92 | value: boolean; |
| 93 | onValueChange: (value: boolean) => void; |
| 94 | }) => <button onClick={() => onValueChange(!value)}>Toggle enabled</button>, |
| 95 | })); |
| 96 | vi.mock("../../state/entities", () => ({ |
| 97 | useProjects: () => [ |
| 98 | { environmentId: "environment", id: "project", title: "Project" }, |
| 99 | { environmentId: "target", id: "target-project", title: "Target project" }, |
| 100 | ], |
| 101 | useServerConfigs: () => new Map(), |
| 102 | useEnvironmentServerConfig: () => null, |
| 103 | useThreadShell: () => null, |
| 104 | })); |
| 105 | vi.mock("./settings-environment-filter", () => ({ |
| 106 | useSettingsEnvironmentFilter: () => ({ |
| 107 | selectedTargets: [{ environmentId: "environment", label: "Environment" }], |
| 108 | availableTargets: [ |
| 109 | { environmentId: "environment", label: "Environment" }, |
| 110 | { environmentId: "target", label: "Target" }, |
| 111 | ], |
| 112 | selectedProjectKey: null, |
| 113 | projectGroups: [], |
| 114 | }), |
| 115 | })); |
| 116 | vi.mock("../../lib/modelOptions", () => ({ |
| 117 | buildModelOptions: () => [], |
| 118 | resolveDefaultableModelSelection: () => null, |
| 119 | resolveNewTaskModelSelection: () => null, |
| 120 | })); |
| 121 | vi.mock("../../state/session", () => ({ |
| 122 | readEnvironmentScope: () => true, |
| 123 | usePreparedConnection: () => ({ _tag: "None" }), |
| 124 | })); |
| 125 | vi.mock("../../state/server", () => ({ |
| 126 | serverEnvironment: { |
| 127 | scheduledTasksLive: () => ({}), |
| 128 | upsertScheduledTask: { permissionAtom: () => Atom.make(true) }, |
| 129 | }, |
| 130 | })); |
| 131 | vi.mock("../../state/query", () => ({ |
| 132 | useEnvironmentQuery: () => ({ data: { tasks: mocks.tasks, threads: [] } }), |
| 133 | })); |
| 134 | vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => mocks.upsert })); |
| 135 | vi.mock("../../state/queries", () => ({ |
| 136 | usePaginatedBranches: () => ({ refs: [], data: null, error: null }), |
| 137 | })); |
| 138 | |
| 139 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 140 | import { editDraft } from "./scheduledTaskDraft"; |
| 141 | import { |
| 142 | scheduledTaskEditorSessionAtom, |
| 143 | startScheduledTaskEditor, |
| 144 | } from "./scheduled-task-editor-state"; |
| 145 | import { SettingsScheduledTaskEditRouteScreen } from "./SettingsScheduledTasksRouteScreen"; |
| 146 | |
| 147 | let root: Root; |
| 148 | let container: HTMLDivElement; |
| 149 | beforeEach(() => { |
| 150 | (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 151 | container = document.createElement("div"); |
| 152 | root = createRoot(container); |
| 153 | mocks.alert.mockReset(); |
| 154 | mocks.upsert.mockReset(); |
| 155 | mocks.upsert.mockResolvedValue({ _tag: "Success" }); |
| 156 | const task: ScheduledTask = { |
| 157 | id: ScheduledTaskId.make("task"), |
| 158 | title: "Task", |
| 159 | prompt: "Prompt", |
| 160 | enabled: true, |
| 161 | schedule: { type: "fixed_time", timeOfDay: "05:00" }, |
| 162 | projectId: ProjectId.make("project"), |
| 163 | threadId: null, |
| 164 | workspaceStrategy: { type: "root" }, |
| 165 | modelSelection: { |
| 166 | instanceId: ProviderInstanceId.make("codex"), |
| 167 | model: "model", |
| 168 | }, |
| 169 | runtimeMode: "full-access", |
| 170 | interactionMode: "default", |
| 171 | createdBy: "user", |
| 172 | creationSource: "mobile", |
| 173 | createdAt: "2026-10-22T00:00:00.000Z", |
| 174 | updatedAt: "2026-10-22T00:00:00.000Z", |
| 175 | nextRunAt: null, |
| 176 | lastRunAt: null, |
| 177 | lastRunStatus: "never", |
| 178 | lastRunError: null, |
| 179 | runCount: 0, |
| 180 | }; |
| 181 | mocks.tasks = [task]; |
| 182 | const draft = editDraft(task); |
| 183 | startScheduledTaskEditor({ |
| 184 | environmentId: EnvironmentId.make("environment"), |
| 185 | environmentLabel: "Environment", |
| 186 | draft: { ...draft, schedule: { ...draft.schedule, intervalMinutes: "60" } }, |
| 187 | }); |
| 188 | act(() => |
| 189 | root.render( |
| 190 | <RegistryContext.Provider value={appAtomRegistry}> |
| 191 | <SettingsScheduledTaskEditRouteScreen /> |
| 192 | </RegistryContext.Provider>, |
| 193 | ), |
| 194 | ); |
| 195 | }); |
| 196 | afterEach(() => { |
| 197 | act(() => root.unmount()); |
| 198 | appAtomRegistry.set(scheduledTaskEditorSessionAtom, null); |
| 199 | }); |
| 200 | function button(label: string) { |
| 201 | const element = Array.from(container.querySelectorAll("button")).find( |
| 202 | (button) => button.textContent === label, |
| 203 | ); |
| 204 | if (!element) throw new Error(`Missing button: ${label}`); |
| 205 | return element; |
| 206 | } |
| 207 | describe("scheduled task batched edits", () => { |
| 208 | it("submits the selected interval when saved before React commits", async () => { |
| 209 | await act(async () => { |
| 210 | button("Interval").click(); |
| 211 | button("Save changes").click(); |
| 212 | }); |
| 213 | expect(mocks.alert).not.toHaveBeenCalled(); |
| 214 | expect(mocks.upsert).toHaveBeenCalledExactlyOnceWith({ |
| 215 | environmentId: "environment", |
| 216 | input: expect.objectContaining({ |
| 217 | id: "task", |
| 218 | requireExisting: true, |
| 219 | schedule: { type: "interval", everyMs: 3_600_000 }, |
| 220 | }), |
| 221 | }); |
| 222 | }); |
| 223 | it("retains the interval when enabled changes before React commits", async () => { |
| 224 | act(() => { |
| 225 | button("Interval").click(); |
| 226 | button("Toggle enabled").click(); |
| 227 | }); |
| 228 | expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current?.draft.schedule.mode).toBe( |
| 229 | "interval", |
| 230 | ); |
| 231 | await act(async () => button("Save changes").click()); |
| 232 | expect(mocks.alert).not.toHaveBeenCalled(); |
| 233 | expect(mocks.upsert).toHaveBeenCalledExactlyOnceWith({ |
| 234 | environmentId: "environment", |
| 235 | input: expect.objectContaining({ |
| 236 | id: "task", |
| 237 | requireExisting: true, |
| 238 | enabled: false, |
| 239 | schedule: { type: "interval", everyMs: 3_600_000 }, |
| 240 | }), |
| 241 | }); |
| 242 | }); |
| 243 | it("keeps every webhook filter added before React commits", () => { |
| 244 | act(() => button("On webhook").click()); |
| 245 | act(() => { |
| 246 | button("Add filter").click(); |
| 247 | button("Add filter").click(); |
| 248 | }); |
| 249 | expect( |
| 250 | appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current?.draft.schedule.filters, |
| 251 | ).toHaveLength(2); |
| 252 | }); |
| 253 | it("keeps the target project when an old picker fires after changing environments", () => { |
| 254 | const editor = appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current; |
| 255 | if (!editor) throw new Error("Missing editor"); |
| 256 | act(() => startScheduledTaskEditor({ ...editor, draft: { ...editor.draft, task: null } })); |
| 257 | act(() => { |
| 258 | button("Select Target").click(); |
| 259 | button("Interval").click(); |
| 260 | button("Select Project").click(); |
| 261 | }); |
| 262 | expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current).toMatchObject({ |
| 263 | environmentId: "target", |
| 264 | draft: { projectId: "target-project", schedule: { mode: "interval" } }, |
| 265 | }); |
| 266 | }); |
| 267 | }); |