|
| 1 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import { ScheduledTaskUpsertInput } from "@t3tools/contracts"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | +import * as Schema from "effect/Schema"; |
| 7 | +import * as TestClock from "effect/testing/TestClock"; |
| 8 | +import { afterEach, vi } from "vite-plus/test"; |
| 9 | + |
| 10 | +import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts"; |
| 11 | +import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; |
| 12 | +import * as SqlitePersistence from "../persistence/Sqlite.ts"; |
| 13 | +import * as Scheduler from "../scheduling/Scheduler.ts"; |
| 14 | +import * as SecretRequests from "../secrets/SecretRequests.ts"; |
| 15 | +import { ServerActivation } from "../serverActivation.ts"; |
| 16 | +import * as ScheduledTaskService from "./ScheduledTaskService.ts"; |
| 17 | + |
| 18 | +afterEach(() => vi.unstubAllEnvs()); |
| 19 | + |
| 20 | +const dependencies = Layer.mergeAll( |
| 21 | + NodeCrypto.layer, |
| 22 | + Scheduler.layer, |
| 23 | + Layer.mock(ThreadLaunchService.ThreadLaunchService)({ |
| 24 | + launch: () => Effect.die("synthetic dispatch failure"), |
| 25 | + }), |
| 26 | + Layer.mock(ThreadManagementService.ThreadManagementService)({}), |
| 27 | + Layer.mock(SecretRequests.SecretRequests)({}), |
| 28 | +); |
| 29 | +const serviceLayer = ScheduledTaskService.layer.pipe(Layer.provide(dependencies)); |
| 30 | +const decodeInput = Schema.decodeUnknownEffect(ScheduledTaskUpsertInput); |
| 31 | +const input = { |
| 32 | + commandId: "interval-duration", |
| 33 | + title: "Interval review", |
| 34 | + prompt: "Review the open pull requests.", |
| 35 | + enabled: true, |
| 36 | + projectId: "project-interval-duration", |
| 37 | + workspaceStrategy: { type: "root" }, |
| 38 | + modelSelection: { instanceId: "codex", model: "gpt-5.4" }, |
| 39 | + runtimeMode: "full-access", |
| 40 | + interactionMode: "default", |
| 41 | +}; |
| 42 | + |
| 43 | +it.effect.each([ |
| 44 | + { |
| 45 | + zone: "America/New_York", |
| 46 | + from: "2026-03-08T06:30:00.000Z", |
| 47 | + everyMs: 7_200_000, |
| 48 | + expected: "2026-03-08T08:30:00.000Z", |
| 49 | + }, |
| 50 | + { |
| 51 | + zone: "America/New_York", |
| 52 | + from: "2026-11-01T05:30:00.000Z", |
| 53 | + everyMs: 7_200_000, |
| 54 | + expected: "2026-11-01T07:30:00.000Z", |
| 55 | + }, |
| 56 | + { |
| 57 | + zone: "America/New_York", |
| 58 | + from: "2026-11-01T06:30:00.000Z", |
| 59 | + everyMs: 60_000, |
| 60 | + expected: "2026-11-01T06:31:00.000Z", |
| 61 | + }, |
| 62 | + { |
| 63 | + zone: "UTC", |
| 64 | + from: "2026-11-01T06:30:00.000Z", |
| 65 | + everyMs: 60_000, |
| 66 | + expected: "2026-11-01T06:31:00.000Z", |
| 67 | + }, |
| 68 | + { |
| 69 | + zone: "Asia/Tokyo", |
| 70 | + from: "2026-03-08T06:30:00.000Z", |
| 71 | + everyMs: 7_200_000, |
| 72 | + expected: "2026-03-08T08:30:00.000Z", |
| 73 | + }, |
| 74 | +] as const)("persists elapsed interval $zone from $from", ({ zone, from, everyMs, expected }) => |
| 75 | + Effect.gen(function* () { |
| 76 | + vi.stubEnv("TZ", zone); |
| 77 | + yield* TestClock.setTime(Date.parse(from)); |
| 78 | + yield* Effect.gen(function* () { |
| 79 | + const service = yield* ScheduledTaskService.ScheduledTaskService; |
| 80 | + const decoded = yield* decodeInput({ ...input, schedule: { type: "interval", everyMs } }); |
| 81 | + const created = yield* service.upsert(decoded); |
| 82 | + expect(created.task.nextRunAt).toBe(expected); |
| 83 | + expect((yield* service.list()).tasks[0]?.nextRunAt).toBe(expected); |
| 84 | + |
| 85 | + const paused = yield* service.setEnabled({ id: created.task.id, enabled: false }); |
| 86 | + expect(paused.task.nextRunAt).toBeNull(); |
| 87 | + const resumed = yield* service.setEnabled({ id: created.task.id, enabled: true }); |
| 88 | + expect(resumed.task.nextRunAt).toBe(expected); |
| 89 | + |
| 90 | + // Exercise completion rescheduling without starting any provider process. |
| 91 | + const completed = yield* service.runNow({ id: created.task.id }); |
| 92 | + expect(completed.task.lastRunStatus).toBe("failed"); |
| 93 | + expect(completed.task.runCount).toBe(1); |
| 94 | + expect(completed.task.nextRunAt).toBe(expected); |
| 95 | + expect((yield* service.list()).tasks[0]?.nextRunAt).toBe(expected); |
| 96 | + }).pipe(Effect.provide(serviceLayer)); |
| 97 | + }).pipe( |
| 98 | + // Keep the real scheduler parked while exercising finite service actions. |
| 99 | + Effect.provideService(ServerActivation, Effect.never), |
| 100 | + Effect.provide(SqlitePersistence.layerMemory), |
| 101 | + ), |
| 102 | +); |
| 103 | + |
| 104 | +it.effect.each([ |
| 105 | + { from: "2026-03-07T23:30:00.000Z", expected: "2026-03-08T13:00:00.000Z" }, |
| 106 | + { from: "2026-10-31T23:30:00.000Z", expected: "2026-11-01T14:00:00.000Z" }, |
| 107 | +] as const)("keeps fixed-time schedules on the local calendar from $from", ({ from, expected }) => |
| 108 | + Effect.gen(function* () { |
| 109 | + vi.stubEnv("TZ", "America/New_York"); |
| 110 | + yield* TestClock.setTime(Date.parse(from)); |
| 111 | + yield* Effect.gen(function* () { |
| 112 | + const service = yield* ScheduledTaskService.ScheduledTaskService; |
| 113 | + const decoded = yield* decodeInput({ |
| 114 | + ...input, |
| 115 | + schedule: { type: "fixed_time", timeOfDay: "09:00" }, |
| 116 | + }); |
| 117 | + const created = yield* service.upsert(decoded); |
| 118 | + expect(created.task.nextRunAt).toBe(expected); |
| 119 | + expect((yield* service.list()).tasks[0]?.nextRunAt).toBe(expected); |
| 120 | + }).pipe(Effect.provide(serviceLayer)); |
| 121 | + }).pipe( |
| 122 | + Effect.provideService(ServerActivation, Effect.never), |
| 123 | + Effect.provide(SqlitePersistence.layerMemory), |
| 124 | + ), |
| 125 | +); |
0 commit comments