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

scheduledTaskDraft.test.ts

348 lines11.4 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 DEFAULT_SERVER_SETTINGS,
4 type ServerConfig,
5 ProviderInstanceId,
6 ProjectId,
7 ScheduledTaskId,
8 type ScheduledTask,
9} from "@t3tools/contracts";
10import {
11 scheduledTaskDefaultModel,
12 createDraft,
13 editDraft,
14 DEFAULT_SCHEDULE,
15 hasScheduledTaskDraftChanges,
16 scheduleDraftForTask,
17 scheduleFromDraft,
18} from "./scheduledTaskDraft";
19​
20describe("scheduleDraftForTask", () => {
21 it("opens legacy sub-minute schedules at the writable minimum", () => {
22 const draft = scheduleDraftForTask({ schedule: { type: "interval", everyMs: 30_000 } });
23 expect(draft.intervalMinutes).toBe("1");
24 expect(scheduleFromDraft(draft)).toEqual({ type: "interval", everyMs: 60_000 });
25 });
26​
27 it("preserves valid fractional-minute schedules through an edit", () => {
28 const schedule = { type: "interval" as const, everyMs: 65_000 };
29 expect(scheduleFromDraft(scheduleDraftForTask({ schedule }))).toEqual(schedule);
30 });
31​
32 it("round-trips a webhook schedule without a signature", () => {
33 const draft = scheduleDraftForTask({ schedule: { type: "webhook", signature: null } });
34 expect(draft.mode).toBe("webhook");
35 expect(scheduleFromDraft(draft)).toEqual({
36 type: "webhook",
37 signature: null,
38 maxDeliveryAgeMinutes: null,
39 });
40 });
41​
42 it("round-trips a webhook max age and treats blank input as no limit", () => {
43 const draft = scheduleDraftForTask({
44 schedule: { type: "webhook", signature: null, maxDeliveryAgeMinutes: 45 },
45 });
46 expect(draft.maxDeliveryAgeMinutes).toBe("45");
47 expect(scheduleFromDraft(draft)).toMatchObject({ maxDeliveryAgeMinutes: 45 });
48 // An invalid limit is an invalid schedule, never a silently removed one.
49 expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: "1.5" })).toBeNull();
50 expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: "0" })).toBeNull();
51 expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: "" })).toMatchObject({
52 maxDeliveryAgeMinutes: null,
53 });
54 });
55​
56 it("keeps a webhook signature on save without sending a secret", () => {
57 const signature = {
58 header: "x-hub-signature-256",
59 encoding: "hex" as const,
60 prefix: "sha256=",
61 };
62 const saved = scheduleFromDraft(
63 scheduleDraftForTask({ schedule: { type: "webhook", signature } }),
64 );
65 expect(saved).toEqual({ type: "webhook", signature, maxDeliveryAgeMinutes: null });
66 expect(saved?.type === "webhook" && saved.signature && "secret" in saved.signature).toBe(false);
67 });
68});
69​
70describe("hasScheduledTaskDraftChanges", () => {
71 it("leaves an untouched form clean and clears changes when edits are reverted", () => {
72 const initial = createDraft(null, null);
73 const edited = { ...initial, prompt: "Review open issues" };
74 expect(hasScheduledTaskDraftChanges(initial, createDraft(null, null))).toBe(false);
75 expect(hasScheduledTaskDraftChanges(initial, edited)).toBe(true);
76 expect(hasScheduledTaskDraftChanges(initial, { ...edited, prompt: initial.prompt })).toBe(
77 false,
78 );
79 });
80​
81 it("keeps invalid, unsaved schedule input dirty", () => {
82 const initial = createDraft(null, null);
83 expect(
84 hasScheduledTaskDraftChanges(initial, {
85 ...initial,
86 schedule: { ...initial.schedule, weekdays: [] },
87 }),
88 ).toBe(true);
89 });
90​
91 it("does not warn after deselecting and reselecting the same weekdays", () => {
92 const initial = createDraft(null, null);
93 expect(
94 hasScheduledTaskDraftChanges(initial, {
95 ...initial,
96 schedule: { ...initial.schedule, weekdays: [2, 3, 4, 5, 1] },
97 }),
98 ).toBe(false);
99 });
100​
101 it("detects changes from the branch and model pickers", () => {
102 const initial = createDraft(null, {
103 instanceId: ProviderInstanceId.make("codex"),
104 model: "gpt-5.4",
105 options: [
106 { id: "effort", value: "high" },
107 { id: "fastMode", value: false },
108 ],
109 });
110 expect(hasScheduledTaskDraftChanges(initial, { ...initial, baseRef: "release" })).toBe(true);
111 expect(hasScheduledTaskDraftChanges(initial, { ...initial, startFromOrigin: false })).toBe(
112 true,
113 );
114 expect(
115 hasScheduledTaskDraftChanges(initial, {
116 ...initial,
117 modelSelection: {
118 ...initial.modelSelection!,
119 options: [
120 { id: "fastMode", value: false },
121 { id: "effort", value: "high" },
122 ],
123 },
124 }),
125 ).toBe(false);
126 expect(
127 hasScheduledTaskDraftChanges(initial, {
128 ...initial,
129 modelSelection: {
130 ...initial.modelSelection!,
131 options: [
132 { id: "effort", value: "low" },
133 { id: "fastMode", value: false },
134 ],
135 },
136 }),
137 ).toBe(true);
138 });
139});
140​
141describe("scheduleFromDraft", () => {
142 it("rejects an empty day selection rather than silently scheduling every day", () => {
143 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, weekdays: [] })).toBeNull();
144 });
145​
146 it("accepts a selected local time and sorts weekdays", () => {
147 expect(
148 scheduleFromDraft({ ...DEFAULT_SCHEDULE, timeOfDay: "18:30", weekdays: [5, 1] }),
149 ).toEqual({
150 type: "fixed_time",
151 timeOfDay: "18:30",
152 weekdays: [1, 5],
153 });
154 });
155​
156 it("stores every day without a weekday restriction", () => {
157 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, weekdays: [1, 2, 3, 4, 5, 6, 0] })).toEqual({
158 type: "fixed_time",
159 timeOfDay: "09:00",
160 });
161 });
162​
163 it("does not add a missing run day when an existing schedule contains duplicates", () => {
164 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, weekdays: [1, 2, 3, 4, 5, 6, 6] })).toEqual({
165 type: "fixed_time",
166 timeOfDay: "09:00",
167 weekdays: [1, 2, 3, 4, 5, 6],
168 });
169 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, weekdays: [0, 1, 2, 3, 4, 5, 6, 6] })).toEqual({
170 type: "fixed_time",
171 timeOfDay: "09:00",
172 });
173 });
174​
175 it.each([-1, 7, 1.5, NaN])(
176 "rejects invalid weekday %s instead of scheduling every day",
177 (day) => {
178 expect(
179 scheduleFromDraft({ ...DEFAULT_SCHEDULE, weekdays: [0, 1, 2, 3, 4, 5, day] }),
180 ).toBeNull();
181 },
182 );
183​
184 it("rejects malformed times and sub-minute intervals", () => {
185 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, timeOfDay: "25:00" })).toBeNull();
186 expect(
187 scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "interval", intervalMinutes: "0" }),
188 ).toBeNull();
189 expect(
190 scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "interval", intervalMinutes: "15" }),
191 ).toEqual({
192 type: "interval",
193 everyMs: 900_000,
194 });
195 });
196​
197 it.each([
198 ["1.5", 90_000],
199 ["1000001", 60_000_060_000],
200 [String(60_001 / 60_000), 60_001],
201 [String(65_000 / 60_000), 65_000],
202 [String(123_456 / 60_000), 123_456],
203 ])("preserves a valid %s minute interval when saving", (intervalMinutes, everyMs) => {
204 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "interval", intervalMinutes })).toEqual({
205 type: "interval",
206 everyMs,
207 });
208 });
209​
210 it.each(["NaN", "Infinity", "9007199254740991", "0.5"])(
211 "rejects intervals that cannot be written as safe whole milliseconds: %s",
212 (intervalMinutes) => {
213 expect(
214 scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "interval", intervalMinutes }),
215 ).toBeNull();
216 },
217 );
218});
219​
220const legacyTask: ScheduledTask = {
221 id: ScheduledTaskId.make("legacy-task"),
222 title: "Review issues",
223 prompt: "Review open issues",
224 enabled: true,
225 schedule: { type: "interval", everyMs: 60_000 },
226 projectId: ProjectId.make("project"),
227 threadId: null,
228 workspaceStrategy: { type: "worktree", baseRef: "release" },
229 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
230 runtimeMode: "full-access",
231 interactionMode: "default",
232 createdBy: "user",
233 creationSource: "web",
234 createdAt: "2026-09-17T00:00:00.000Z",
235 updatedAt: "2026-09-17T00:00:00.000Z",
236 nextRunAt: null,
237 lastRunAt: null,
238 lastRunStatus: "never",
239 lastRunError: null,
240 runCount: 0,
241};
242​
243describe("editing scheduled task branch settings", () => {
244 it("keeps an omitted origin flag on the local base branch", () => {
245 const draft = editDraft(legacyTask);
246 expect(draft.baseRef).toBe("release");
247 expect(draft.startFromOrigin).toBe(false);
248 });
249​
250 it.each([true, false])("preserves an explicit origin flag of %s", (startFromOrigin) => {
251 const draft = editDraft({
252 ...legacyTask,
253 workspaceStrategy: { type: "worktree", baseRef: "release", startFromOrigin },
254 });
255 expect(draft.startFromOrigin).toBe(startFromOrigin);
256 });
257});
258​
259it("continues to default newly created tasks to origin", () => {
260 expect(createDraft(null, null).startFromOrigin).toBe(true);
261});
262​
263describe("scheduled task model defaults", () => {
264 const instanceId = ProviderInstanceId.make("codex");
265 const projectId = ProjectId.make("project");
266 const environmentSelection = {
267 instanceId,
268 model: "environment-model",
269 options: [{ id: "reasoning", value: "high" }],
270 };
271 const projectSelection = { instanceId, model: "project-model" };
272 const config = {
273 settings: { ...DEFAULT_SERVER_SETTINGS, defaultModelSelection: environmentSelection },
274 providers: [
275 {
276 instanceId,
277 driver: "codex",
278 displayName: "Codex",
279 enabled: true,
280 installed: true,
281 status: "ready",
282 auth: { status: "authenticated" },
283 models: [
284 { slug: "first-model", name: "First", isCustom: false, capabilities: null },
285 {
286 slug: "catalog-default",
287 name: "Default",
288 isDefault: true,
289 isCustom: false,
290 capabilities: null,
291 },
292 { slug: "environment-model", name: "Environment", isCustom: false, capabilities: null },
293 { slug: "project-model", name: "Project", isCustom: false, capabilities: null },
294 ],
295 },
296 ],
297 } as unknown as ServerConfig;
298 const resolve = scheduledTaskDefaultModel;
299 it("uses the environment default with its provider options", () => {
300 expect(resolve(config, { id: projectId })).toEqual(environmentSelection);
301 });
302 it("prefers the project's configured model", () => {
303 expect(resolve(config, { id: projectId, defaultModelSelection: projectSelection })).toEqual(
304 projectSelection,
305 );
306 expect(
307 resolve(
308 {
309 ...config,
310 settings: {
311 ...config.settings,
312 projectSettingsOverrides: {
313 [projectId]: { defaultModelSelection: projectSelection },
314 },
315 },
316 },
317 { id: projectId },
318 ),
319 ).toEqual(projectSelection);
320 });
321 it("uses the advertised default instead of catalog order when no default is configured", () => {
322 expect(
323 resolve({ ...config, settings: { ...config.settings, defaultModelSelection: null } }, null),
324 ).toEqual({ instanceId, model: "catalog-default" });
325 });
326 it("falls back to the environment default when the project provider is unavailable", () => {
327 expect(
328 resolve(config, {
329 id: projectId,
330 defaultModelSelection: {
331 instanceId: ProviderInstanceId.make("unavailable"),
332 model: "missing",
333 },
334 }),
335 ).toEqual(environmentSelection);
336 });
337 it("does not choose an implicit model on a disabled provider", () => {
338 expect(
339 resolve(
340 {
341 ...config,
342 providers: config.providers.map((provider) => ({ ...provider, enabled: false })),
343 },
344 null,
345 ),
346 ).toBeNull();
347 });
348});