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 { |
| 2 | EnvironmentId, |
| 3 | ProjectId, |
| 4 | DEFAULT_SERVER_SETTINGS, |
| 5 | type ServerConfig, |
| 6 | ProviderInstanceId, |
| 7 | ScheduledTaskId, |
| 8 | type ScheduledTask, |
| 9 | ThreadId, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 12 | |
| 13 | import type { |
| 14 | SidebarProjectGroupMember, |
| 15 | SidebarProjectSnapshot, |
| 16 | } from "../../sidebarProjectGrouping"; |
| 17 | import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope"; |
| 18 | |
| 19 | import { deriveProviderInstanceEntries } from "../../providerInstances"; |
| 20 | import { |
| 21 | scheduledTaskDefaultModel, |
| 22 | defaultScheduledTaskProjectId, |
| 23 | matchesScheduledTaskScope, |
| 24 | scheduleFromDraft, |
| 25 | scheduleLabel, |
| 26 | taskToDraft, |
| 27 | unbindDraftFromThread, |
| 28 | } from "./scheduledTasksSettings.logic"; |
| 29 | |
| 30 | const laptopId = EnvironmentId.make("laptop"); |
| 31 | const serverId = EnvironmentId.make("server"); |
| 32 | const environments = [ |
| 33 | { environmentId: laptopId, label: "Laptop" }, |
| 34 | { environmentId: serverId, label: "Server" }, |
| 35 | ]; |
| 36 | |
| 37 | function member(id: string, environmentId: EnvironmentId): SidebarProjectGroupMember { |
| 38 | return { |
| 39 | id: ProjectId.make(id), |
| 40 | environmentId, |
| 41 | title: "T3 Code", |
| 42 | workspaceRoot: `/repos/${id}`, |
| 43 | physicalProjectKey: `${environmentId}:/repos/${id}`, |
| 44 | environmentLabel: |
| 45 | environments.find((environment) => environment.environmentId === environmentId)?.label ?? |
| 46 | null, |
| 47 | defaultModelSelection: null, |
| 48 | scripts: [], |
| 49 | createdAt: "2026-09-07T00:00:00.000Z", |
| 50 | updatedAt: "2026-09-07T00:00:00.000Z", |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | const first = member("first", laptopId); |
| 55 | const second = member("second", laptopId); |
| 56 | const third = member("third", serverId); |
| 57 | const other = member("other", serverId); |
| 58 | |
| 59 | function group( |
| 60 | projectKey: string, |
| 61 | members: readonly SidebarProjectGroupMember[], |
| 62 | ): SidebarProjectSnapshot { |
| 63 | return { |
| 64 | ...members[0]!, |
| 65 | projectKey, |
| 66 | displayName: projectKey, |
| 67 | memberProjects: members, |
| 68 | memberProjectRefs: members.map((project) => ({ |
| 69 | environmentId: project.environmentId, |
| 70 | projectId: project.id, |
| 71 | })), |
| 72 | groupedProjectCount: members.length, |
| 73 | environmentPresence: "mixed", |
| 74 | allRemoteMembersAreDesktopLocal: false, |
| 75 | allRemoteMembersAreWsl: false, |
| 76 | remoteEnvironmentLabels: [], |
| 77 | pinned: false, |
| 78 | archived: false, |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | const sameIdElsewhere = member("first", serverId); |
| 83 | const groups = [group("t3code", [first, second, third]), group("other", [other, sameIdElsewhere])]; |
| 84 | const tasks = [first, second, third, other, sameIdElsewhere].map((project, index) => ({ |
| 85 | id: `task-${index}`, |
| 86 | environmentId: project.environmentId, |
| 87 | projectId: project.id, |
| 88 | })); |
| 89 | |
| 90 | describe("scheduled task settings scope", () => { |
| 91 | it.each<{ search: SettingsScopeSearch; expected: string[] }>([ |
| 92 | { search: {}, expected: ["task-0", "task-1", "task-2", "task-3", "task-4"] }, |
| 93 | { search: { machine: laptopId }, expected: ["task-0", "task-1"] }, |
| 94 | { search: { project: "t3code" }, expected: ["task-0", "task-1", "task-2"] }, |
| 95 | { search: { project: "t3code", machine: serverId }, expected: ["task-2"] }, |
| 96 | { search: { project: "t3code", checkout: second.physicalProjectKey }, expected: ["task-1"] }, |
| 97 | { search: { project: "missing" }, expected: [] }, |
| 98 | { search: { machine: "removed" }, expected: [] }, |
| 99 | { search: { project: "t3code", checkout: "removed" }, expected: [] }, |
| 100 | { search: { project: "other", machine: laptopId }, expected: [] }, |
| 101 | ])("lists only matching tasks for $search", ({ search, expected }) => { |
| 102 | const scope = resolveSettingsScope(search, groups, environments); |
| 103 | expect( |
| 104 | tasks |
| 105 | .filter((task) => matchesScheduledTaskScope(scope, task.environmentId, task.projectId)) |
| 106 | .map((task) => task.id), |
| 107 | ).toEqual(expected); |
| 108 | }); |
| 109 | |
| 110 | it("defaults a new task to a pinned open project, and to an archived one only when scoped to it", () => { |
| 111 | const at = "2026-01-01T00:00:00.000Z"; |
| 112 | const project = (id: string, pinnedAt: string | null, archivedAt: string | null) => ({ |
| 113 | id: ProjectId.make(id), |
| 114 | pinnedAt, |
| 115 | archivedAt, |
| 116 | }); |
| 117 | const all = resolveSettingsScope({}, groups, environments); |
| 118 | const scoped = resolveSettingsScope({ project: "t3code" }, groups, environments); |
| 119 | expect( |
| 120 | defaultScheduledTaskProjectId([project("a", null, null), project("b", at, null)], all), |
| 121 | ).toBe("b"); |
| 122 | expect(defaultScheduledTaskProjectId([project("x", null, at)], all)).toBe(""); |
| 123 | expect(defaultScheduledTaskProjectId([project("x", null, at)], scoped)).toBe("x"); |
| 124 | }); |
| 125 | |
| 126 | it("keeps tasks with removed projects manageable at environment scope", () => { |
| 127 | const removedProject = ProjectId.make("removed"); |
| 128 | expect( |
| 129 | matchesScheduledTaskScope( |
| 130 | resolveSettingsScope({}, groups, environments), |
| 131 | laptopId, |
| 132 | removedProject, |
| 133 | ), |
| 134 | ).toBe(true); |
| 135 | expect( |
| 136 | matchesScheduledTaskScope( |
| 137 | resolveSettingsScope({ project: "t3code" }, groups, environments), |
| 138 | laptopId, |
| 139 | removedProject, |
| 140 | ), |
| 141 | ).toBe(false); |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | const legacyTask: ScheduledTask = { |
| 146 | id: ScheduledTaskId.make("legacy-task"), |
| 147 | title: "Review issues", |
| 148 | prompt: "Review open issues", |
| 149 | enabled: true, |
| 150 | schedule: { type: "interval", everyMs: 60_000 }, |
| 151 | projectId: ProjectId.make("project"), |
| 152 | threadId: null, |
| 153 | workspaceStrategy: { type: "worktree", baseRef: "release" }, |
| 154 | modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, |
| 155 | runtimeMode: "full-access", |
| 156 | interactionMode: "default", |
| 157 | createdBy: "user", |
| 158 | creationSource: "web", |
| 159 | createdAt: "2026-09-17T00:00:00.000Z", |
| 160 | updatedAt: "2026-09-17T00:00:00.000Z", |
| 161 | nextRunAt: null, |
| 162 | lastRunAt: null, |
| 163 | lastRunStatus: "never", |
| 164 | lastRunError: null, |
| 165 | runCount: 0, |
| 166 | }; |
| 167 | |
| 168 | describe("editing scheduled task branch settings", () => { |
| 169 | it("keeps an omitted origin flag on the local base branch", () => { |
| 170 | const draft = taskToDraft(legacyTask); |
| 171 | expect(draft.baseRef).toBe("release"); |
| 172 | expect(draft.startFromOrigin).toBe(false); |
| 173 | }); |
| 174 | |
| 175 | it("leaves the base automatic when a task without a worktree switches to one", () => { |
| 176 | const draft = taskToDraft({ ...legacyTask, workspaceStrategy: { type: "root" } }); |
| 177 | expect(draft.baseRef).toBe(""); |
| 178 | }); |
| 179 | }); |
| 180 | |
| 181 | describe("fixed-time scheduled tasks", () => { |
| 182 | it("pads a stored single-digit hour so the time input shows it", () => { |
| 183 | const draft = taskToDraft({ |
| 184 | ...legacyTask, |
| 185 | schedule: { type: "fixed_time", timeOfDay: "9:05" }, |
| 186 | }); |
| 187 | expect(draft.timeOfDay).toBe("09:05"); |
| 188 | }); |
| 189 | |
| 190 | it("keeps a fixed run time and rejects a cleared one", () => { |
| 191 | const draft = taskToDraft({ |
| 192 | ...legacyTask, |
| 193 | schedule: { type: "fixed_time", timeOfDay: "14:30" }, |
| 194 | }); |
| 195 | expect(scheduleFromDraft(draft)).toEqual({ type: "fixed_time", timeOfDay: "14:30" }); |
| 196 | // A native time input reports "" when it is cleared or only partly filled. |
| 197 | expect(scheduleFromDraft({ ...draft, timeOfDay: "" })).toBeNull(); |
| 198 | }); |
| 199 | }); |
| 200 | |
| 201 | describe("scheduled task destination", () => { |
| 202 | it("unbinding leaves the shared checkout for a new worktree from the remote", () => { |
| 203 | const rootBound = taskToDraft({ |
| 204 | ...legacyTask, |
| 205 | threadId: ThreadId.make("thread:bound"), |
| 206 | workspaceStrategy: { type: "root" }, |
| 207 | }); |
| 208 | expect(unbindDraftFromThread(rootBound)).toMatchObject({ |
| 209 | threadId: "", |
| 210 | workspaceMode: "worktree", |
| 211 | startFromOrigin: true, |
| 212 | }); |
| 213 | }); |
| 214 | }); |
| 215 | |
| 216 | describe("one-time scheduled tasks", () => { |
| 217 | afterEach(() => vi.unstubAllEnvs()); |
| 218 | |
| 219 | it("round-trips the run time through the local date-time input", () => { |
| 220 | const runAt = "2026-11-03T17:00:42.000Z"; |
| 221 | const draft = taskToDraft({ ...legacyTask, schedule: { type: "once", runAt } }); |
| 222 | expect(draft.scheduleMode).toBe("once"); |
| 223 | expect(scheduleFromDraft(draft)).toEqual({ type: "once", runAt }); |
| 224 | const moved = scheduleFromDraft({ ...draft, runAt: "2026-11-04T09:30" }); |
| 225 | expect(moved).toEqual({ type: "once", runAt: new Date("2026-11-04T09:30").toISOString() }); |
| 226 | expect(scheduleFromDraft({ ...draft, runAt: "" })).toBeNull(); |
| 227 | }); |
| 228 | |
| 229 | it("rejects a time skipped by daylight saving", () => { |
| 230 | vi.stubEnv("TZ", "America/Los_Angeles"); |
| 231 | const draft = taskToDraft({ |
| 232 | ...legacyTask, |
| 233 | schedule: { type: "once", runAt: "2027-03-14T18:00:00.000Z" }, |
| 234 | }); |
| 235 | expect(scheduleFromDraft({ ...draft, runAt: "2027-03-14T02:30" })).toBeNull(); |
| 236 | }); |
| 237 | |
| 238 | it("keeps the saved offset when an edit stays in an hour that repeats", () => { |
| 239 | vi.stubEnv("TZ", "America/Los_Angeles"); |
| 240 | // 01:30 PST, the second 01:30 on 2026-11-01. |
| 241 | const draft = taskToDraft({ |
| 242 | ...legacyTask, |
| 243 | schedule: { type: "once", runAt: "2026-11-01T09:30:00.000Z" }, |
| 244 | }); |
| 245 | expect(scheduleFromDraft({ ...draft, runAt: "2026-11-01T01:35" })).toEqual({ |
| 246 | type: "once", |
| 247 | runAt: "2026-11-01T09:35:00.000Z", |
| 248 | }); |
| 249 | }); |
| 250 | }); |
| 251 | |
| 252 | describe("webhook scheduled tasks", () => { |
| 253 | const signature = { header: "x-signature", encoding: "base64", prefix: "" } as const; |
| 254 | const webhookTask: ScheduledTask = { |
| 255 | ...legacyTask, |
| 256 | schedule: { type: "webhook", signature }, |
| 257 | webhook: { path: "/api/hooks/legacy-task/token", url: null, hasSecret: true }, |
| 258 | }; |
| 259 | |
| 260 | it("round-trips dedupe settings, clears them, and rejects invalid windows", () => { |
| 261 | const dedupe = { |
| 262 | replayKey: "{{headers.delivery-id}}", |
| 263 | coalesceKey: "{{body.id}}", |
| 264 | coalesceWindowSeconds: 10, |
| 265 | }; |
| 266 | const draft = taskToDraft({ |
| 267 | ...webhookTask, |
| 268 | schedule: { type: "webhook", signature: null, dedupe }, |
| 269 | }); |
| 270 | expect(scheduleFromDraft(draft)).toMatchObject({ dedupe }); |
| 271 | expect( |
| 272 | scheduleFromDraft({ |
| 273 | ...draft, |
| 274 | replayKey: " \n ", |
| 275 | coalesceKey: " \t ", |
| 276 | coalesceWindowSeconds: "", |
| 277 | }), |
| 278 | ).toMatchObject({ dedupe: {} }); |
| 279 | expect( |
| 280 | scheduleFromDraft({ |
| 281 | ...draft, |
| 282 | replayKey: ` ${dedupe.replayKey} `, |
| 283 | coalesceKey: `\t${dedupe.coalesceKey}\n`, |
| 284 | }), |
| 285 | ).toMatchObject({ dedupe }); |
| 286 | expect( |
| 287 | scheduleFromDraft({ ...draft, replayKey: "", coalesceKey: "", coalesceWindowSeconds: "" }), |
| 288 | ).toMatchObject({ dedupe: {} }); |
| 289 | expect(scheduleFromDraft({ ...draft, coalesceWindowSeconds: "" })).toMatchObject({ |
| 290 | dedupe: { replayKey: dedupe.replayKey, coalesceKey: dedupe.coalesceKey }, |
| 291 | }); |
| 292 | for (const coalesceWindowSeconds of ["9", "3601", "1.5", "abc"]) { |
| 293 | expect(scheduleFromDraft({ ...draft, coalesceWindowSeconds })).toBeNull(); |
| 294 | } |
| 295 | }); |
| 296 | |
| 297 | it("keeps a stored secret when the secret field is left blank", () => { |
| 298 | const draft = taskToDraft(webhookTask); |
| 299 | expect(draft.scheduleMode).toBe("webhook"); |
| 300 | expect(draft.signatureSecret).toBe(""); |
| 301 | expect(scheduleFromDraft(draft)).toEqual({ |
| 302 | type: "webhook", |
| 303 | signature, |
| 304 | maxDeliveryAgeMinutes: null, |
| 305 | dedupe: {}, |
| 306 | }); |
| 307 | expect(scheduleFromDraft({ ...draft, signatureSecret: " new " })).toEqual({ |
| 308 | type: "webhook", |
| 309 | signature: { ...signature, secret: "new" }, |
| 310 | maxDeliveryAgeMinutes: null, |
| 311 | dedupe: {}, |
| 312 | }); |
| 313 | }); |
| 314 | |
| 315 | it("round-trips the max age, treats blank as no limit, and rejects an invalid limit", () => { |
| 316 | const draft = taskToDraft({ |
| 317 | ...webhookTask, |
| 318 | schedule: { type: "webhook", signature: null, maxDeliveryAgeMinutes: 90 }, |
| 319 | }); |
| 320 | expect(draft.maxDeliveryAgeMinutes).toBe("90"); |
| 321 | expect(scheduleFromDraft(draft)).toMatchObject({ maxDeliveryAgeMinutes: 90 }); |
| 322 | for (const blank of ["", " "]) { |
| 323 | expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: blank })).toMatchObject({ |
| 324 | maxDeliveryAgeMinutes: null, |
| 325 | dedupe: {}, |
| 326 | }); |
| 327 | } |
| 328 | for (const invalid of ["0", "-5", "1.5", "abc", "1441"]) { |
| 329 | expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: invalid })).toBeNull(); |
| 330 | } |
| 331 | }); |
| 332 | |
| 333 | it("sends filters only when the form changed them", () => { |
| 334 | const filters = [ |
| 335 | { path: "headers.x-github-event", op: "equals", value: "pull_request" }, |
| 336 | { path: "body.action", op: "in", value: ["opened", "reopened"] }, |
| 337 | ] as const; |
| 338 | const draft = taskToDraft({ |
| 339 | ...webhookTask, |
| 340 | schedule: { type: "webhook", signature, filters }, |
| 341 | }); |
| 342 | expect(draft.filters.map((filter) => filter.op)).toEqual(["equals", "in"]); |
| 343 | // A title-only save leaves the key out, so the server keeps whatever is stored now. |
| 344 | expect(scheduleFromDraft({ ...draft, title: "Renamed" })).not.toHaveProperty("filters"); |
| 345 | expect(scheduleFromDraft({ ...draft, filters: draft.filters.slice(0, 1) })).toMatchObject({ |
| 346 | filters: filters.slice(0, 1), |
| 347 | }); |
| 348 | // Removing every row is a change, so it clears the stored filters. |
| 349 | expect(scheduleFromDraft({ ...draft, filters: [] })).toMatchObject({ filters: [] }); |
| 350 | expect( |
| 351 | scheduleFromDraft({ |
| 352 | ...draft, |
| 353 | filters: [{ id: "f1", path: "body.action", op: "in", value: " , " }], |
| 354 | }), |
| 355 | ).toBeNull(); |
| 356 | }); |
| 357 | }); |
| 358 | |
| 359 | describe("scheduled task model defaults", () => { |
| 360 | const instanceId = ProviderInstanceId.make("codex"); |
| 361 | const projectId = ProjectId.make("project"); |
| 362 | const environmentSelection = { |
| 363 | instanceId, |
| 364 | model: "environment-model", |
| 365 | options: [{ id: "reasoning", value: "high" }], |
| 366 | }; |
| 367 | const projectSelection = { instanceId, model: "project-model" }; |
| 368 | const config = { |
| 369 | settings: { ...DEFAULT_SERVER_SETTINGS, defaultModelSelection: environmentSelection }, |
| 370 | providers: [ |
| 371 | { |
| 372 | instanceId, |
| 373 | driver: "codex", |
| 374 | displayName: "Codex", |
| 375 | enabled: true, |
| 376 | installed: true, |
| 377 | status: "ready", |
| 378 | auth: { status: "authenticated" }, |
| 379 | models: [ |
| 380 | { slug: "first-model", name: "First", isCustom: false, capabilities: null }, |
| 381 | { |
| 382 | slug: "catalog-default", |
| 383 | name: "Default", |
| 384 | isDefault: true, |
| 385 | isCustom: false, |
| 386 | capabilities: null, |
| 387 | }, |
| 388 | { slug: "environment-model", name: "Environment", isCustom: false, capabilities: null }, |
| 389 | { slug: "project-model", name: "Project", isCustom: false, capabilities: null }, |
| 390 | ], |
| 391 | }, |
| 392 | ], |
| 393 | } as unknown as ServerConfig; |
| 394 | const resolve = ( |
| 395 | value: ServerConfig, |
| 396 | project: { id: typeof projectId; defaultModelSelection?: typeof projectSelection } | null, |
| 397 | ) => |
| 398 | scheduledTaskDefaultModel( |
| 399 | value.settings, |
| 400 | project, |
| 401 | deriveProviderInstanceEntries(value.providers), |
| 402 | ); |
| 403 | it("uses the advertised default instead of catalog order when no default is configured", () => { |
| 404 | expect( |
| 405 | resolve({ ...config, settings: { ...config.settings, defaultModelSelection: null } }, null), |
| 406 | ).toEqual({ instanceId, model: "catalog-default" }); |
| 407 | }); |
| 408 | it("falls back to the environment default when the project provider is unavailable", () => { |
| 409 | expect( |
| 410 | resolve(config, { |
| 411 | id: projectId, |
| 412 | defaultModelSelection: { |
| 413 | instanceId: ProviderInstanceId.make("unavailable"), |
| 414 | model: "missing", |
| 415 | }, |
| 416 | }), |
| 417 | ).toEqual(environmentSelection); |
| 418 | }); |
| 419 | }); |
| 420 | |
| 421 | it("rejects dedupe templates without a request placeholder", () => { |
| 422 | const draft = taskToDraft({ ...legacyTask, schedule: { type: "webhook", signature: null } }); |
| 423 | expect(scheduleFromDraft({ ...draft, replayKey: "literal" })).toBeNull(); |
| 424 | expect(scheduleFromDraft({ ...draft, coalesceKey: "{{body.bad" })).toBeNull(); |
| 425 | }); |
| 426 | |
| 427 | it("appends the server zone only to fixed-time schedule labels", () => { |
| 428 | expect(scheduleLabel({ type: "fixed_time", timeOfDay: "16:31" }, "Etc/UTC")).toBe( |
| 429 | "Daily at 16:31 UTC", |
| 430 | ); |
| 431 | expect(scheduleLabel({ type: "fixed_time", timeOfDay: "09:00" })).toBe("Daily at 09:00"); |
| 432 | expect(scheduleLabel({ type: "interval", everyMs: 60_000 }, "Etc/UTC")).toBe("Every 1 min"); |
| 433 | expect(scheduleLabel({ type: "webhook", signature: null }, "Etc/UTC")).toBe("On webhook"); |
| 434 | }); |