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

scheduledTaskDraft.test.ts

271 lines9.0 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 DEFAULT_SERVER_SETTINGS,
4 type ServerConfig,
5 ProviderDriverKind,
6 ProviderInstanceId,
7 ProjectId,
8 ScheduledTaskId,
9 type ScheduledTask,
10} from "@t3tools/contracts";
11import {
12 changeDraftProject,
13 createDraft,
14 editDraft,
15 scheduledTaskDefaultModel,
16 DEFAULT_SCHEDULE,
17 scheduleDraftForTask,
18 scheduleFromDraft,
19} from "./scheduledTaskDraft";
20​
21describe("scheduleDraftForTask", () => {
22 it("opens legacy sub-minute schedules at the writable minimum", () => {
23 const draft = scheduleDraftForTask({ schedule: { type: "interval", everyMs: 30_000 } });
24 expect(draft.intervalMinutes).toBe("1");
25 expect(scheduleFromDraft(draft)).toEqual({ type: "interval", everyMs: 60_000 });
26 });
27​
28 it("round-trips dedupe settings and rejects invalid windows", () => {
29 const dedupe = {
30 replayKey: "{{headers.delivery-id}}",
31 coalesceKey: "{{body.id}}",
32 coalesceWindowSeconds: 3600,
33 };
34 const draft = scheduleDraftForTask({ schedule: { type: "webhook", signature: null, dedupe } });
35 expect(scheduleFromDraft(draft)).toMatchObject({ dedupe });
36 expect(
37 scheduleFromDraft({
38 ...draft,
39 replayKey: " \n ",
40 coalesceKey: " \t ",
41 coalesceWindowSeconds: "",
42 }),
43 ).toMatchObject({ dedupe: {} });
44 expect(
45 scheduleFromDraft({
46 ...draft,
47 replayKey: ` ${dedupe.replayKey} `,
48 coalesceKey: `\t${dedupe.coalesceKey}\n`,
49 }),
50 ).toMatchObject({ dedupe });
51 expect(
52 scheduleFromDraft({ ...draft, replayKey: "", coalesceKey: "", coalesceWindowSeconds: "" }),
53 ).toMatchObject({ dedupe: {} });
54 for (const coalesceWindowSeconds of ["9", "3601", "1.5", "abc"]) {
55 expect(scheduleFromDraft({ ...draft, coalesceWindowSeconds })).toBeNull();
56 }
57 });
58​
59 it("keeps a webhook signature on save without sending a secret", () => {
60 const signature = {
61 header: "x-hub-signature-256",
62 encoding: "hex" as const,
63 prefix: "sha256=",
64 };
65 const saved = scheduleFromDraft(
66 scheduleDraftForTask({ schedule: { type: "webhook", signature } }),
67 );
68 expect(saved).toEqual({ type: "webhook", signature, maxDeliveryAgeMinutes: null, dedupe: {} });
69 expect(saved?.type === "webhook" && saved.signature && "secret" in saved.signature).toBe(false);
70 });
71​
72 it("round-trips webhook filters and refuses an invalid one", () => {
73 const filters = [
74 { path: "headers.x-github-event", op: "equals", value: "pull_request" },
75 { path: "body.label.name", op: "in", value: ["needs review", "agent:fix"] },
76 ] as const;
77 const draft = scheduleDraftForTask({ schedule: { type: "webhook", signature: null, filters } });
78 // Untouched rows are left out, so the server keeps whatever is stored now.
79 expect(scheduleFromDraft(draft)).not.toHaveProperty("filters");
80 expect(scheduleFromDraft({ ...draft, filters: [] })).toMatchObject({ filters: [] });
81 expect(scheduleFromDraft({ ...draft, filters: draft.filters.slice(1) })).toMatchObject({
82 filters: filters.slice(1),
83 });
84 expect(
85 scheduleFromDraft({ ...draft, filters: [{ id: "f1", path: "", op: "exists", value: "" }] }),
86 ).toBeNull();
87 });
88});
89​
90describe("scheduleFromDraft", () => {
91 it("rejects an empty day selection rather than silently scheduling every day", () => {
92 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, weekdays: [] })).toBeNull();
93 });
94​
95 it.each([
96 ["1.5", 90_000],
97 ["2,5", 150_000],
98 [" 2,5 ", 150_000],
99 [String(60_001 / 60_000), 60_001],
100 ])("preserves a valid %s minute interval when saving", (intervalMinutes, everyMs) => {
101 expect(scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "interval", intervalMinutes })).toEqual({
102 type: "interval",
103 everyMs,
104 });
105 });
106​
107 it.each(["0,5", "2,5,0", "Infinity", "150119987580", ""])(
108 "rejects invalid interval minutes: %s",
109 (intervalMinutes) => {
110 expect(
111 scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "interval", intervalMinutes }),
112 ).toBeNull();
113 },
114 );
115});
116​
117describe("scheduled task model defaults", () => {
118 const instanceId = ProviderInstanceId.make("codex");
119 const projectId = ProjectId.make("project");
120 const environmentSelection = {
121 instanceId,
122 model: "environment-model",
123 options: [{ id: "reasoning", value: "high" }],
124 };
125 const projectSelection = { instanceId, model: "project-model" };
126 const config = {
127 settings: { ...DEFAULT_SERVER_SETTINGS, defaultModelSelection: environmentSelection },
128 providers: [
129 {
130 instanceId,
131 driver: "codex",
132 displayName: "Codex",
133 enabled: true,
134 installed: true,
135 status: "ready",
136 auth: { status: "authenticated" },
137 models: [
138 { slug: "first-model", name: "First", isCustom: false, capabilities: null },
139 {
140 slug: "catalog-default",
141 name: "Default",
142 isDefault: true,
143 isCustom: false,
144 capabilities: null,
145 },
146 { slug: "environment-model", name: "Environment", isCustom: false, capabilities: null },
147 { slug: "project-model", name: "Project", isCustom: false, capabilities: null },
148 ],
149 },
150 ],
151 } as unknown as ServerConfig;
152 const resolve = scheduledTaskDefaultModel;
153 it("prefers the project's configured model", () => {
154 expect(resolve(config, { id: projectId, defaultModelSelection: projectSelection })).toEqual(
155 projectSelection,
156 );
157 expect(
158 resolve(
159 {
160 ...config,
161 settings: {
162 ...config.settings,
163 projectSettingsOverrides: {
164 [projectId]: { defaultModelSelection: projectSelection },
165 },
166 },
167 },
168 { id: projectId },
169 ),
170 ).toEqual(projectSelection);
171 });
172 it("uses the advertised default instead of catalog order when no default is configured", () => {
173 expect(
174 resolve({ ...config, settings: { ...config.settings, defaultModelSelection: null } }, null),
175 ).toEqual({ instanceId, model: "catalog-default" });
176 });
177 it("falls back to the environment default when the project provider is unavailable", () => {
178 expect(
179 resolve(config, {
180 id: projectId,
181 defaultModelSelection: {
182 instanceId: ProviderInstanceId.make("unavailable"),
183 model: "missing",
184 },
185 }),
186 ).toEqual(environmentSelection);
187 });
188 it("does not choose an implicit model on a disabled provider", () => {
189 expect(
190 resolve(
191 {
192 ...config,
193 settings: {
194 ...config.settings,
195 providerInstances: {
196 [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: false },
197 },
198 },
199 providers: config.providers.map((provider) => ({ ...provider, enabled: false })),
200 },
201 null,
202 ),
203 ).toBeNull();
204 });
205});
206​
207it("rejects dedupe keys without valid request placeholders", () => {
208 expect(
209 scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "webhook", replayKey: "literal" }),
210 ).toBeNull();
211 expect(
212 scheduleFromDraft({ ...DEFAULT_SCHEDULE, mode: "webhook", coalesceKey: "{{body.bad" }),
213 ).toBeNull();
214});
215​
216describe("scheduled task worktree base", () => {
217 const rootTask = {
218 id: ScheduledTaskId.make("task"),
219 title: "Review issues",
220 prompt: "Review open issues",
221 enabled: true,
222 schedule: { type: "interval", everyMs: 60_000 },
223 projectId: ProjectId.make("project"),
224 threadId: null,
225 workspaceStrategy: { type: "root" },
226 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
227 runtimeMode: "full-access",
228 interactionMode: "default",
229 createdBy: "user",
230 creationSource: "mobile",
231 createdAt: "2026-09-17T00:00:00.000Z",
232 updatedAt: "2026-09-17T00:00:00.000Z",
233 nextRunAt: null,
234 lastRunAt: null,
235 lastRunStatus: "never",
236 lastRunError: null,
237 runCount: 0,
238 } satisfies ScheduledTask;
239​
240 it("leaves the base automatic for new tasks and tasks without a worktree", () => {
241 expect(createDraft(ProjectId.make("project"), null).baseRef).toBe("");
242 expect(editDraft(rootTask).baseRef).toBe("");
243 expect(
244 editDraft({ ...rootTask, workspaceStrategy: { type: "worktree", baseRef: "release" } })
245 .baseRef,
246 ).toBe("release");
247 });
248​
249 it("clears the old project's base branch and checkout when the project changes", () => {
250 const other = ProjectId.make("other");
251 const worktree = editDraft({
252 ...rootTask,
253 workspaceStrategy: { type: "worktree", baseRef: "release", startFromOrigin: false },
254 });
255 expect(changeDraftProject(worktree, rootTask.projectId)).toBe(worktree);
256 expect(changeDraftProject(worktree, other)).toMatchObject({
257 projectId: other,
258 workspace: "worktree",
259 baseRef: "",
260 startFromOrigin: false,
261 });
262 const checkout = editDraft({
263 ...rootTask,
264 workspaceStrategy: { type: "existing_worktree", worktreePath: "/work/project-a" },
265 });
266 expect(changeDraftProject(checkout, other)).toMatchObject({
267 workspace: "existing_worktree",
268 checkoutPath: "",
269 });
270 });
271});