@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

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