Open History to see the last commit
1import {
2 EnvironmentId,
3 ProjectId,
4 DEFAULT_SERVER_SETTINGS,
5 type ServerConfig,
6 ProviderInstanceId,
7 ScheduledTaskId,
8 type ScheduledTask,
9} from "@t3tools/contracts";
10import { describe, expect, it } from "vite-plus/test";
11
12import type {
13 SidebarProjectGroupMember,
14 SidebarProjectSnapshot,
15} from "../../sidebarProjectGrouping";
16import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope";
17
18import { deriveProviderInstanceEntries } from "../../providerInstances";
19import {
20 scheduledTaskDefaultModel,
21 matchesScheduledTaskScope,
22 scheduleFromDraft,
23 taskToDraft,
24} from "./scheduledTasksSettings.logic";
25
26const laptopId = EnvironmentId.make("laptop");
27const serverId = EnvironmentId.make("server");
28const environments = [
29 { environmentId: laptopId, label: "Laptop" },
30 { environmentId: serverId, label: "Server" },
31];
32
33function member(id: string, environmentId: EnvironmentId): SidebarProjectGroupMember {
34 return {
35 id: ProjectId.make(id),
36 environmentId,
37 title: "T3 Code",
38 workspaceRoot: `/repos/${id}`,
39 physicalProjectKey: `${environmentId}:/repos/${id}`,
40 environmentLabel:
41 environments.find((environment) => environment.environmentId === environmentId)?.label ??
42 null,
43 defaultModelSelection: null,
44 scripts: [],
45 createdAt: "2026-09-07T00:00:00.000Z",
46 updatedAt: "2026-09-07T00:00:00.000Z",
47 };
48}
49
50const first = member("first", laptopId);
51const second = member("second", laptopId);
52const third = member("third", serverId);
53const other = member("other", serverId);
54
55function group(
56 projectKey: string,
57 members: readonly SidebarProjectGroupMember[],
58): SidebarProjectSnapshot {
59 return {
60 ...members[0]!,
61 projectKey,
62 displayName: projectKey,
63 memberProjects: members,
64 memberProjectRefs: members.map((project) => ({
65 environmentId: project.environmentId,
66 projectId: project.id,
67 })),
68 groupedProjectCount: members.length,
69 environmentPresence: "mixed",
70 allRemoteMembersAreDesktopLocal: false,
71 allRemoteMembersAreWsl: false,
72 remoteEnvironmentLabels: [],
73 };
74}
75
76const sameIdElsewhere = member("first", serverId);
77const groups = [group("t3code", [first, second, third]), group("other", [other, sameIdElsewhere])];
78const tasks = [first, second, third, other, sameIdElsewhere].map((project, index) => ({
79 id: `task-${index}`,
80 environmentId: project.environmentId,
81 projectId: project.id,
82}));
83
84describe("scheduled task settings scope", () => {
85 it.each<{ search: SettingsScopeSearch; expected: string[] }>([
86 { search: {}, expected: ["task-0", "task-1", "task-2", "task-3", "task-4"] },
87 { search: { machine: laptopId }, expected: ["task-0", "task-1"] },
88 { search: { project: "t3code" }, expected: ["task-0", "task-1", "task-2"] },
89 { search: { project: "t3code", machine: serverId }, expected: ["task-2"] },
90 { search: { project: "t3code", checkout: second.physicalProjectKey }, expected: ["task-1"] },
91 { search: { project: "missing" }, expected: [] },
92 { search: { machine: "removed" }, expected: [] },
93 { search: { project: "t3code", checkout: "removed" }, expected: [] },
94 { search: { project: "other", machine: laptopId }, expected: [] },
95 ])("lists only matching tasks for $search", ({ search, expected }) => {
96 const scope = resolveSettingsScope(search, groups, environments);
97 expect(
98 tasks
99 .filter((task) => matchesScheduledTaskScope(scope, task.environmentId, task.projectId))
100 .map((task) => task.id),
101 ).toEqual(expected);
102 });
103
104 it("keeps tasks with removed projects manageable at environment scope", () => {
105 const removedProject = ProjectId.make("removed");
106 expect(
107 matchesScheduledTaskScope(
108 resolveSettingsScope({}, groups, environments),
109 laptopId,
110 removedProject,
111 ),
112 ).toBe(true);
113 expect(
114 matchesScheduledTaskScope(
115 resolveSettingsScope({ project: "t3code" }, groups, environments),
116 laptopId,
117 removedProject,
118 ),
119 ).toBe(false);
120 });
121
122 it("does not offer an unrelated environment's same-ID project when creating a task", () => {
123 const scope = resolveSettingsScope({ project: "t3code" }, groups, environments);
124 const serverProjects = [third, other, sameIdElsewhere];
125 expect(
126 serverProjects.filter((project) => matchesScheduledTaskScope(scope, serverId, project.id)),
127 ).toEqual([third]);
128 });
129});
130
131const legacyTask: ScheduledTask = {
132 id: ScheduledTaskId.make("legacy-task"),
133 title: "Review issues",
134 prompt: "Review open issues",
135 enabled: true,
136 schedule: { type: "interval", everyMs: 60_000 },
137 projectId: ProjectId.make("project"),
138 threadId: null,
139 workspaceStrategy: { type: "worktree", baseRef: "release" },
140 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
141 runtimeMode: "full-access",
142 interactionMode: "default",
143 createdBy: "user",
144 creationSource: "web",
145 createdAt: "2026-09-17T00:00:00.000Z",
146 updatedAt: "2026-09-17T00:00:00.000Z",
147 nextRunAt: null,
148 lastRunAt: null,
149 lastRunStatus: "never",
150 lastRunError: null,
151 runCount: 0,
152};
153
154describe("editing scheduled task branch settings", () => {
155 it("keeps an omitted origin flag on the local base branch", () => {
156 const draft = taskToDraft(legacyTask);
157 expect(draft.baseRef).toBe("release");
158 expect(draft.startFromOrigin).toBe(false);
159 });
160
161 it.each([true, false])("preserves an explicit origin flag of %s", (startFromOrigin) => {
162 const draft = taskToDraft({
163 ...legacyTask,
164 workspaceStrategy: { type: "worktree", baseRef: "release", startFromOrigin },
165 });
166 expect(draft.startFromOrigin).toBe(startFromOrigin);
167 });
168});
169
170describe("webhook scheduled tasks", () => {
171 const signature = { header: "x-signature", encoding: "base64", prefix: "" } as const;
172 const webhookTask: ScheduledTask = {
173 ...legacyTask,
174 schedule: { type: "webhook", signature },
175 webhook: { path: "/api/hooks/legacy-task/token", url: null, hasSecret: true },
176 };
177
178 it("keeps a stored secret when the secret field is left blank", () => {
179 const draft = taskToDraft(webhookTask);
180 expect(draft.scheduleMode).toBe("webhook");
181 expect(draft.signatureSecret).toBe("");
182 expect(scheduleFromDraft(draft)).toEqual({
183 type: "webhook",
184 signature,
185 maxDeliveryAgeMinutes: null,
186 });
187 expect(scheduleFromDraft({ ...draft, signatureSecret: " new " })).toEqual({
188 type: "webhook",
189 signature: { ...signature, secret: "new" },
190 maxDeliveryAgeMinutes: null,
191 });
192 });
193
194 it("drops the signature when it is switched off and offers GitHub's settings", () => {
195 const draft = taskToDraft({ ...webhookTask, schedule: { type: "webhook", signature: null } });
196 expect(draft.signatureEnabled).toBe(false);
197 expect(draft.signatureHeader).toBe("x-hub-signature-256");
198 expect(scheduleFromDraft(draft)).toEqual({
199 type: "webhook",
200 signature: null,
201 maxDeliveryAgeMinutes: null,
202 });
203 });
204
205 it("round-trips the max age, treats blank as no limit, and rejects an invalid limit", () => {
206 const draft = taskToDraft({
207 ...webhookTask,
208 schedule: { type: "webhook", signature: null, maxDeliveryAgeMinutes: 90 },
209 });
210 expect(draft.maxDeliveryAgeMinutes).toBe("90");
211 expect(scheduleFromDraft(draft)).toMatchObject({ maxDeliveryAgeMinutes: 90 });
212 for (const blank of ["", " "]) {
213 expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: blank })).toMatchObject({
214 maxDeliveryAgeMinutes: null,
215 });
216 }
217 for (const invalid of ["0", "-5", "1.5", "abc", "1441"]) {
218 expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: invalid })).toBeNull();
219 }
220 });
221});
222
223describe("scheduled task model defaults", () => {
224 const instanceId = ProviderInstanceId.make("codex");
225 const projectId = ProjectId.make("project");
226 const environmentSelection = {
227 instanceId,
228 model: "environment-model",
229 options: [{ id: "reasoning", value: "high" }],
230 };
231 const projectSelection = { instanceId, model: "project-model" };
232 const config = {
233 settings: { ...DEFAULT_SERVER_SETTINGS, defaultModelSelection: environmentSelection },
234 providers: [
235 {
236 instanceId,
237 driver: "codex",
238 displayName: "Codex",
239 enabled: true,
240 installed: true,
241 status: "ready",
242 auth: { status: "authenticated" },
243 models: [
244 { slug: "first-model", name: "First", isCustom: false, capabilities: null },
245 {
246 slug: "catalog-default",
247 name: "Default",
248 isDefault: true,
249 isCustom: false,
250 capabilities: null,
251 },
252 { slug: "environment-model", name: "Environment", isCustom: false, capabilities: null },
253 { slug: "project-model", name: "Project", isCustom: false, capabilities: null },
254 ],
255 },
256 ],
257 } as unknown as ServerConfig;
258 const resolve = (
259 value: ServerConfig,
260 project: { id: typeof projectId; defaultModelSelection?: typeof projectSelection } | null,
261 ) =>
262 scheduledTaskDefaultModel(
263 value.settings,
264 project,
265 deriveProviderInstanceEntries(value.providers),
266 );
267 it("uses the environment default with its provider options", () => {
268 expect(resolve(config, { id: projectId })).toEqual(environmentSelection);
269 });
270 it("prefers the project's configured model", () => {
271 expect(resolve(config, { id: projectId, defaultModelSelection: projectSelection })).toEqual(
272 projectSelection,
273 );
274 expect(
275 resolve(
276 {
277 ...config,
278 settings: {
279 ...config.settings,
280 projectSettingsOverrides: {
281 [projectId]: { defaultModelSelection: projectSelection },
282 },
283 },
284 },
285 { id: projectId },
286 ),
287 ).toEqual(projectSelection);
288 });
289 it("uses the advertised default instead of catalog order when no default is configured", () => {
290 expect(
291 resolve({ ...config, settings: { ...config.settings, defaultModelSelection: null } }, null),
292 ).toEqual({ instanceId, model: "catalog-default" });
293 });
294 it("falls back to the environment default when the project provider is unavailable", () => {
295 expect(
296 resolve(config, {
297 id: projectId,
298 defaultModelSelection: {
299 instanceId: ProviderInstanceId.make("unavailable"),
300 model: "missing",
301 },
302 }),
303 ).toEqual(environmentSelection);
304 });
305 it("does not choose an implicit model on a disabled provider", () => {
306 expect(
307 resolve(
308 {
309 ...config,
310 providers: config.providers.map((provider) => ({ ...provider, enabled: false })),
311 },
312 null,
313 ),
314 ).toBeNull();
315 });
316});