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

scheduledTaskDraft.test.ts

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