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

serverSettings.test.ts

797 lines28.4 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 ProjectId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 UsageLimitSourceId,
7 type ServerProvider,
8} from "@t3tools/contracts";
9import * as Duration from "effect/Duration";
10import { describe, expect, it } from "vite-plus/test";
11import { resolveServerBackgroundActivitySettings } from "./backgroundActivitySettings.ts";
12import { createModelSelection } from "./model.ts";
13import { resolveProjectScripts, projectScriptsInheritDefaults } from "./projectScripts.ts";
14import {
15 applyServerSettingsPatch,
16 isModelSelectionProviderEnabled,
17 parsePersistedServerObservabilitySettings,
18 resolveSourceControlWriterModelSelection,
19 resolveProjectAgentBrowserAccess,
20 resolveProjectAutoPull,
21} from "./serverSettings.ts";
22​
23/** Settings after the server has folded legacy per-project fields into `projectSettingsOverrides`. */
24const FOLDED_SERVER_SETTINGS = { ...DEFAULT_SERVER_SETTINGS, projectSettingsFolded: true };
25​
26describe("serverSettings helpers", () => {
27 it("changes a cleanup rule without replacing the machine's other rules", () => {
28 const enabled = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
29 storageCleanup: { worktreeAfterDays: 8, worktreeOnMerge: true, logsAfterDays: 30 },
30 });
31 expect(
32 applyServerSettingsPatch(enabled, {
33 storageCleanup: { worktreeAfterDays: null },
34 }).storageCleanup,
35 ).toEqual({
36 worktreeAfterDays: null,
37 worktreeOnMerge: true,
38 worktreeOnDelete: false,
39 worktreeUnchanged: false,
40 browserArtifactsAfterDays: null,
41 logsAfterDays: 30,
42 });
43 });
44 it("replaces SSH host lists when saving, editing, and removing hosts", () => {
45 const host = { id: "mini", label: "Mac mini", target: "mini" };
46 const saved = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { deviceHosts: [host] });
47 expect(saved.deviceHosts).toEqual([host]);
48 const replacement = { ...host, target: "other-mini" };
49 const edited = applyServerSettingsPatch(saved, { deviceHosts: [replacement] });
50 expect(edited.deviceHosts).toEqual([replacement]);
51 expect(applyServerSettingsPatch(edited, { deviceHosts: [] }).deviceHosts).toEqual([]);
52 });
53​
54 it("inherits actions, preserves existing actions, and supports empty overrides and reset", () => {
55 const project = { id: ProjectId.make("project-actions"), scripts: [] };
56 const action = {
57 id: "check",
58 name: "Check",
59 command: "npm test",
60 icon: "play" as const,
61 runOnWorktreeCreate: false,
62 };
63 const existing = { ...project, scripts: [{ ...action, command: "npm run lint" }] };
64 // Before the one-time fold, scripts stored on the project aggregate still apply.
65 const unfolded = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
66 defaultProjectScripts: [action],
67 });
68 expect(resolveProjectScripts(unfolded, existing)).toEqual(existing.scripts);
69 expect(projectScriptsInheritDefaults(unfolded, existing)).toBe(false);
70 const defaults = applyServerSettingsPatch(FOLDED_SERVER_SETTINGS, {
71 defaultProjectScripts: [action],
72 });
73 expect(resolveProjectScripts(defaults, project)).toEqual([action]);
74 expect(projectScriptsInheritDefaults(defaults, project)).toBe(true);
75 expect(resolveProjectScripts(defaults, existing)).toEqual([action]);
76 const disabled = applyServerSettingsPatch(defaults, {
77 projectScriptOverrides: { [project.id]: [] },
78 });
79 expect(resolveProjectScripts(disabled, project)).toEqual([]);
80 expect(projectScriptsInheritDefaults(disabled, project)).toBe(false);
81 const changedDefault = applyServerSettingsPatch(disabled, {
82 defaultProjectScripts: [{ ...action, command: "npm run build" }],
83 });
84 expect(resolveProjectScripts(changedDefault, project)).toEqual([]);
85 const reset = applyServerSettingsPatch(changedDefault, {
86 projectScriptOverrides: { [project.id]: null },
87 });
88 expect(resolveProjectScripts(reset, existing)).toEqual(changedDefault.defaultProjectScripts);
89 expect(projectScriptsInheritDefaults(reset, existing)).toBe(true);
90 expect(
91 resolveProjectScripts(
92 applyServerSettingsPatch(reset, { defaultProjectScripts: [] }),
93 existing,
94 ),
95 ).toEqual([]);
96 });
97​
98 it("preserves other projects' actions when overriding, clearing, or resetting one project", () => {
99 const firstProject = { id: ProjectId.make("first-project"), scripts: [] };
100 const secondProject = { id: ProjectId.make("second-project"), scripts: [] };
101 const defaultAction = {
102 id: "check",
103 name: "Check",
104 command: "npm test",
105 icon: "play" as const,
106 runOnWorktreeCreate: false,
107 };
108 const firstAction = { ...defaultAction, command: "npm run lint" };
109 const secondAction = { ...defaultAction, command: "npm run build" };
110 const firstUpdate = applyServerSettingsPatch(FOLDED_SERVER_SETTINGS, {
111 defaultProjectScripts: [defaultAction],
112 projectScriptOverrides: { [firstProject.id]: [firstAction] },
113 });
114 const secondUpdate = applyServerSettingsPatch(firstUpdate, {
115 projectScriptOverrides: { [secondProject.id]: [secondAction] },
116 });
117 expect(resolveProjectScripts(secondUpdate, firstProject)).toEqual([firstAction]);
118 expect(resolveProjectScripts(secondUpdate, secondProject)).toEqual([secondAction]);
119​
120 const cleared = applyServerSettingsPatch(secondUpdate, {
121 projectScriptOverrides: { [firstProject.id]: [] },
122 });
123 expect(resolveProjectScripts(cleared, firstProject)).toEqual([]);
124 expect(resolveProjectScripts(cleared, secondProject)).toEqual([secondAction]);
125​
126 const reset = applyServerSettingsPatch(cleared, {
127 projectScriptOverrides: { [firstProject.id]: null },
128 });
129 expect(resolveProjectScripts(reset, { ...firstProject, scripts: [firstAction] })).toEqual([
130 defaultAction,
131 ]);
132 expect(resolveProjectScripts(reset, secondProject)).toEqual([secondAction]);
133 expect(resolveProjectScripts(secondUpdate, firstProject)).toEqual([firstAction]);
134 });
135​
136 it("inherits automatic pull while preserving legacy opt-ins and explicit overrides", () => {
137 const projectId = ProjectId.make("project-pull");
138 expect(resolveProjectAutoPull(DEFAULT_SERVER_SETTINGS, projectId, false)).toBe(false);
139 expect(resolveProjectAutoPull(DEFAULT_SERVER_SETTINGS, projectId, true)).toBe(true);
140 const enabled = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { defaultAutoPull: true });
141 expect(resolveProjectAutoPull(enabled, projectId, false)).toBe(true);
142 const overridden = applyServerSettingsPatch(enabled, {
143 projectAutoPullOverrides: { [projectId]: false },
144 });
145 expect(resolveProjectAutoPull(overridden, projectId, true)).toBe(false);
146 const reset = applyServerSettingsPatch(overridden, {
147 projectAutoPullOverrides: { [projectId]: null },
148 });
149 expect(resolveProjectAutoPull(reset, projectId, false)).toBe(true);
150 const disabled = applyServerSettingsPatch(reset, {
151 defaultAutoPull: false,
152 projectAutoPullOverrides: { [projectId]: true },
153 });
154 expect(resolveProjectAutoPull(disabled, projectId, false)).toBe(true);
155 expect(resolveProjectAutoPull(disabled, ProjectId.make("other-project"), false)).toBe(false);
156 });
157​
158 it("inherits browser access and restores inheritance when a project override is removed", () => {
159 const projectId = ProjectId.make("project-browser");
160 const otherProjectId = ProjectId.make("other-project");
161 const overridden = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
162 projectAgentBrowserAccessOverrides: { [projectId]: false },
163 });
164 expect(resolveProjectAgentBrowserAccess(overridden, projectId)).toBe(false);
165 expect(resolveProjectAgentBrowserAccess(overridden, otherProjectId)).toBe(true);
166 const reset = applyServerSettingsPatch(overridden, {
167 projectAgentBrowserAccessOverrides: { [projectId]: null },
168 });
169 expect(resolveProjectAgentBrowserAccess(reset, projectId)).toBe(true);
170 const enabled = applyServerSettingsPatch(reset, {
171 enableAgentBrowserAccess: false,
172 projectAgentBrowserAccessOverrides: { [projectId]: true },
173 });
174 expect(resolveProjectAgentBrowserAccess(enabled, projectId)).toBe(true);
175 expect(resolveProjectAgentBrowserAccess(enabled, otherProjectId)).toBe(false);
176 });
177​
178 it("preserves other projects' boolean overrides across separate updates and resets", () => {
179 const firstProjectId = ProjectId.make("first-project");
180 const secondProjectId = ProjectId.make("second-project");
181 const firstUpdate = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
182 defaultAutoPull: true,
183 projectAutoPullOverrides: { [firstProjectId]: false },
184 projectAgentBrowserAccessOverrides: { [firstProjectId]: false },
185 });
186 const secondUpdate = applyServerSettingsPatch(firstUpdate, {
187 projectAutoPullOverrides: { [secondProjectId]: false },
188 projectAgentBrowserAccessOverrides: { [secondProjectId]: false },
189 });
190 for (const projectId of [firstProjectId, secondProjectId]) {
191 expect(resolveProjectAutoPull(secondUpdate, projectId, false)).toBe(false);
192 expect(resolveProjectAgentBrowserAccess(secondUpdate, projectId)).toBe(false);
193 }
194​
195 const reset = applyServerSettingsPatch(secondUpdate, {
196 projectAutoPullOverrides: { [firstProjectId]: null },
197 projectAgentBrowserAccessOverrides: { [firstProjectId]: null },
198 });
199 expect(resolveProjectAutoPull(reset, firstProjectId, false)).toBe(true);
200 expect(resolveProjectAgentBrowserAccess(reset, firstProjectId)).toBe(true);
201 expect(resolveProjectAutoPull(reset, secondProjectId, false)).toBe(false);
202 expect(resolveProjectAgentBrowserAccess(reset, secondProjectId)).toBe(false);
203 expect(reset.projectAutoPullOverrides[firstProjectId]).toBeUndefined();
204 expect(reset.projectAgentBrowserAccessOverrides[firstProjectId]).toBeUndefined();
205 expect(resolveProjectAutoPull(secondUpdate, firstProjectId, false)).toBe(false);
206 expect(resolveProjectAgentBrowserAccess(secondUpdate, firstProjectId)).toBe(false);
207 });
208​
209 it("replaces and clears conversation model defaults without retaining old options", () => {
210 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
211 defaultModelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
212 { id: "reasoningEffort", value: "high" },
213 ]),
214 });
215 const selection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "sonnet");
216 const updated = applyServerSettingsPatch(current, { defaultModelSelection: selection });
217 expect(updated.defaultModelSelection).toEqual(selection);
218 expect(
219 applyServerSettingsPatch(updated, { defaultModelSelection: null }).defaultModelSelection,
220 ).toBeNull();
221 });
222​
223 it("ignores missing and blank persisted observability URLs", () => {
224 expect(parsePersistedServerObservabilitySettings("{}")).toEqual({
225 otlpTracesUrl: undefined,
226 otlpMetricsUrl: undefined,
227 otlpLogsUrl: undefined,
228 });
229 expect(
230 parsePersistedServerObservabilitySettings(
231 JSON.stringify({
232 observability: { otlpTracesUrl: " ", otlpMetricsUrl: "", otlpLogsUrl: " " },
233 }),
234 ),
235 ).toEqual({
236 otlpTracesUrl: undefined,
237 otlpMetricsUrl: undefined,
238 otlpLogsUrl: undefined,
239 });
240 });
241​
242 it("parses lenient persisted settings JSON and trims observability URLs", () => {
243 expect(
244 parsePersistedServerObservabilitySettings(
245 JSON.stringify({
246 observability: {
247 otlpTracesUrl: " http://localhost:4318/v1/traces ",
248 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
249 otlpLogsUrl: " http://localhost:4318/v1/logs ",
250 },
251 }),
252 ),
253 ).toEqual({
254 otlpTracesUrl: "http://localhost:4318/v1/traces",
255 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
256 otlpLogsUrl: "http://localhost:4318/v1/logs",
257 });
258 });
259​
260 it("falls back cleanly when persisted settings are invalid", () => {
261 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
262 otlpTracesUrl: undefined,
263 otlpMetricsUrl: undefined,
264 otlpLogsUrl: undefined,
265 });
266 });
267​
268 it("replaces text generation selection when provider/model are provided", () => {
269 const current = {
270 ...DEFAULT_SERVER_SETTINGS,
271 textGenerationModelSelection: createModelSelection(
272 ProviderInstanceId.make("codex"),
273 "gpt-5.4-mini",
274 [
275 { id: "reasoningEffort", value: "high" },
276 { id: "fastMode", value: true },
277 ],
278 ),
279 };
280​
281 expect(
282 applyServerSettingsPatch(current, {
283 textGenerationModelSelection: {
284 instanceId: ProviderInstanceId.make("codex"),
285 model: "gpt-5.4-mini",
286 },
287 }).textGenerationModelSelection,
288 ).toEqual({
289 instanceId: "codex",
290 model: "gpt-5.4-mini",
291 });
292 });
293​
294 it("still deep merges text generation selection when only options are provided", () => {
295 const current = {
296 ...DEFAULT_SERVER_SETTINGS,
297 textGenerationModelSelection: createModelSelection(
298 ProviderInstanceId.make("codex"),
299 "gpt-5.4-mini",
300 [
301 { id: "reasoningEffort", value: "high" },
302 { id: "fastMode", value: true },
303 ],
304 ),
305 };
306​
307 expect(
308 applyServerSettingsPatch(current, {
309 textGenerationModelSelection: {
310 options: [{ id: "fastMode", value: false }],
311 },
312 }).textGenerationModelSelection,
313 ).toEqual({
314 instanceId: "codex",
315 model: "gpt-5.4-mini",
316 options: [
317 { id: "reasoningEffort", value: "high" },
318 { id: "fastMode", value: false },
319 ],
320 });
321 });
322​
323 it("replaces text generation selection across providers without leaking stale options", () => {
324 const current = {
325 ...DEFAULT_SERVER_SETTINGS,
326 textGenerationModelSelection: createModelSelection(
327 ProviderInstanceId.make("codex"),
328 "gpt-5.4-mini",
329 [
330 { id: "reasoningEffort", value: "high" },
331 { id: "fastMode", value: true },
332 ],
333 ),
334 };
335​
336 expect(
337 applyServerSettingsPatch(current, {
338 textGenerationModelSelection: {
339 instanceId: ProviderInstanceId.make("opencode"),
340 model: "openai/gpt-5",
341 },
342 }).textGenerationModelSelection,
343 ).toEqual({
344 instanceId: "opencode",
345 model: "openai/gpt-5",
346 });
347 });
348​
349 it("accepts array-based text generation selection patches", () => {
350 expect(
351 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
352 textGenerationModelSelection: {
353 instanceId: ProviderInstanceId.make("opencode"),
354 model: "openai/gpt-5",
355 options: [
356 { id: "variant", value: "prod" },
357 { id: "agent", value: "build" },
358 ],
359 },
360 }).textGenerationModelSelection,
361 ).toEqual({
362 instanceId: "opencode",
363 model: "openai/gpt-5",
364 options: [
365 { id: "variant", value: "prod" },
366 { id: "agent", value: "build" },
367 ],
368 });
369 });
370​
371 it("replaces source control writer selection without retaining stale options", () => {
372 const current = {
373 ...DEFAULT_SERVER_SETTINGS,
374 sourceControlWriterModelSelection: createModelSelection(
375 ProviderInstanceId.make("codex"),
376 "gpt-5.4-mini",
377 [{ id: "reasoningEffort", value: "high" }],
378 ),
379 };
380​
381 expect(
382 applyServerSettingsPatch(current, {
383 sourceControlWriterModelSelection: {
384 instanceId: ProviderInstanceId.make("opencode"),
385 model: "openai/gpt-5",
386 },
387 }).sourceControlWriterModelSelection,
388 ).toEqual({
389 instanceId: "opencode",
390 model: "openai/gpt-5",
391 });
392 });
393​
394 it("clears source control writer selection with null", () => {
395 const current = {
396 ...DEFAULT_SERVER_SETTINGS,
397 sourceControlWriterModelSelection: createModelSelection(
398 ProviderInstanceId.make("codex"),
399 "gpt-5.4-mini",
400 ),
401 };
402​
403 expect(
404 applyServerSettingsPatch(current, {
405 sourceControlWriterModelSelection: null,
406 }).sourceControlWriterModelSelection,
407 ).toBeNull();
408 });
409​
410 it("falls back from a disabled source control writer provider without clearing its selection", () => {
411 const instanceId = ProviderInstanceId.make("codex_writer");
412 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
413 const settings = {
414 ...DEFAULT_SERVER_SETTINGS,
415 providerInstances: {
416 [instanceId]: {
417 driver: ProviderDriverKind.make("codex"),
418 enabled: false,
419 config: {},
420 },
421 },
422 sourceControlWriterModelSelection,
423 };
424​
425 expect(isModelSelectionProviderEnabled(settings, sourceControlWriterModelSelection)).toBe(
426 false,
427 );
428 expect(resolveSourceControlWriterModelSelection(settings)).toBe(
429 settings.textGenerationModelSelection,
430 );
431 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
432 });
433​
434 it("falls back from an unavailable source control writer provider", () => {
435 const instanceId = ProviderInstanceId.make("missing_writer");
436 const sourceControlWriterModelSelection = createModelSelection(instanceId, "missing-model");
437 const settings = {
438 ...DEFAULT_SERVER_SETTINGS,
439 providerInstances: {
440 [instanceId]: {
441 driver: ProviderDriverKind.make("missing-driver"),
442 config: {},
443 },
444 },
445 sourceControlWriterModelSelection,
446 };
447 const unavailableProvider = {
448 instanceId,
449 driver: ProviderDriverKind.make("missing-driver"),
450 enabled: false,
451 installed: false,
452 version: null,
453 status: "disabled",
454 auth: { status: "unknown" },
455 checkedAt: "2026-07-27T00:00:00.000Z",
456 availability: "unavailable",
457 unavailableReason: "This provider driver is not available in this build.",
458 models: [],
459 slashCommands: [],
460 skills: [],
461 } satisfies ServerProvider;
462​
463 expect(resolveSourceControlWriterModelSelection(settings, [unavailableProvider])).toBe(
464 settings.textGenerationModelSelection,
465 );
466 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
467 });
468​
469 it("falls back from a writer provider that cannot generate application text", () => {
470 const instanceId = ProviderInstanceId.make("acp_writer");
471 const sourceControlWriterModelSelection = createModelSelection(instanceId, "default");
472 const settings = {
473 ...DEFAULT_SERVER_SETTINGS,
474 providerInstances: {
475 [instanceId]: {
476 driver: ProviderDriverKind.make("acpRegistry"),
477 enabled: true,
478 config: {},
479 },
480 },
481 sourceControlWriterModelSelection,
482 };
483 const incapableProvider = {
484 instanceId,
485 driver: ProviderDriverKind.make("acpRegistry"),
486 supportsTextGeneration: false,
487 enabled: true,
488 installed: true,
489 version: null,
490 status: "ready",
491 auth: { status: "authenticated" },
492 checkedAt: "2026-07-27T00:00:00.000Z",
493 models: [],
494 slashCommands: [],
495 skills: [],
496 } satisfies ServerProvider;
497​
498 expect(resolveSourceControlWriterModelSelection(settings, [incapableProvider])).toBe(
499 settings.textGenerationModelSelection,
500 );
501 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
502 });
503​
504 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
505 const codexId = ProviderInstanceId.make("codex");
506 const current = {
507 ...DEFAULT_SERVER_SETTINGS,
508 providerInstances: {
509 [codexId]: {
510 driver: ProviderDriverKind.make("codex"),
511 displayName: "Codex Work",
512 accentColor: "#7c3aed",
513 enabled: true,
514 config: { homePath: "~/.codex" },
515 },
516 },
517 };
518​
519 expect(
520 applyServerSettingsPatch(current, {
521 providerInstances: {
522 [codexId]: {
523 driver: ProviderDriverKind.make("codex"),
524 displayName: "Codex Work",
525 enabled: true,
526 config: { homePath: "~/.codex" },
527 },
528 },
529 }).providerInstances[codexId],
530 ).toEqual({
531 driver: ProviderDriverKind.make("codex"),
532 displayName: "Codex Work",
533 enabled: true,
534 config: { homePath: "~/.codex" },
535 });
536 });
537​
538 it("upserts and removes usageLimitSources per entry so concurrent edits cannot clobber", () => {
539 const hubA = UsageLimitSourceId.make("cliproxy-a");
540 const hubB = UsageLimitSourceId.make("cliproxy-b");
541 const source = (url: string) => ({
542 kind: "cliproxy" as const,
543 url,
544 managementKey: "secret",
545 enabled: true,
546 });
547 const current = {
548 ...DEFAULT_SERVER_SETTINGS,
549 usageLimitSources: { [hubA]: source("http://a:8318") },
550 };
551​
552 const added = applyServerSettingsPatch(current, {
553 usageLimitSources: { [hubB]: source("http://b:8318") },
554 });
555 expect(Object.keys(added.usageLimitSources)).toEqual([hubA, hubB]);
556​
557 const removed = applyServerSettingsPatch(added, { usageLimitSources: { [hubA]: null } });
558 expect(Object.keys(removed.usageLimitSources)).toEqual([hubB]);
559 });
560​
561 it("replaces and removes individual usage prices without clobbering other models", () => {
562 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
563 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
564 usagePriceOverrides: { "example-model": { ...prices, cacheReadCostPerMillionTokens: 0.5 } },
565 });
566 const added = applyServerSettingsPatch(current, {
567 usagePriceOverrides: { "other-model": prices },
568 });
569 const replaced = applyServerSettingsPatch(added, {
570 usagePriceOverrides: { "example-model": prices },
571 });
572 expect(replaced.usagePriceOverrides).toEqual({
573 "example-model": prices,
574 "other-model": prices,
575 });
576 const removed = applyServerSettingsPatch(replaced, {
577 usagePriceOverrides: { "example-model": null },
578 });
579 expect(removed.usagePriceOverrides).toEqual({ "other-model": prices });
580 expect(current.usagePriceOverrides["example-model"]?.cacheReadCostPerMillionTokens).toBe(0.5);
581 });
582​
583 it("stores background activity profiles as a versioned object and syncs legacy aliases", () => {
584 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
585 backgroundActivity: {
586 schemaVersion: 1,
587 profile: "battery-saver",
588 overrides: {},
589 },
590 });
591​
592 expect(next.backgroundActivity).toEqual({
593 schemaVersion: 1,
594 profile: "battery-saver",
595 overrides: {},
596 });
597 expect(next.backgroundActivityProfile).toBe("battery-saver");
598 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
599 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(
600 Duration.toMillis(Duration.minutes(15)),
601 );
602 });
603​
604 it("turns legacy interval patches into custom background activity overrides", () => {
605 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
606 automaticGitFetchInterval: Duration.seconds(15),
607 });
608​
609 expect(next.backgroundActivity).toEqual({
610 schemaVersion: 1,
611 profile: "custom",
612 baseProfile: "balanced",
613 overrides: {
614 automaticGitFetchInterval: Duration.seconds(15),
615 },
616 });
617 expect(resolveServerBackgroundActivitySettings(next).profile).toBe("balanced");
618 expect(
619 Duration.toMillis(resolveServerBackgroundActivitySettings(next).automaticGitFetchInterval),
620 ).toBe(15_000);
621 });
622​
623 it("preserves legacy background activity settings when applying an unrelated patch", () => {
624 const current = {
625 ...DEFAULT_SERVER_SETTINGS,
626 backgroundActivityProfile: "performance" as const,
627 automaticGitFetchInterval: Duration.seconds(7),
628 providerHealthRefreshInterval: Duration.minutes(4),
629 };
630​
631 const next = applyServerSettingsPatch(current, {
632 sourceControlWriterModelSelection: createModelSelection(
633 ProviderInstanceId.make("codex"),
634 "gpt-5.4-mini",
635 ),
636 });
637​
638 expect(next.backgroundActivity).toEqual({
639 schemaVersion: 1,
640 profile: "custom",
641 baseProfile: "performance",
642 overrides: {
643 automaticGitFetchInterval: Duration.seconds(7),
644 providerHealthRefreshInterval: Duration.minutes(4),
645 },
646 });
647 expect(next.backgroundActivityProfile).toBe("performance");
648 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(7_000);
649 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(240_000);
650 });
651​
652 it("does not reactivate dormant overrides from a concrete profile", () => {
653 const current = {
654 ...DEFAULT_SERVER_SETTINGS,
655 backgroundActivity: {
656 schemaVersion: 1 as const,
657 profile: "battery-saver" as const,
658 overrides: {
659 providerHealthRefreshInterval: Duration.seconds(5),
660 },
661 },
662 };
663​
664 const next = applyServerSettingsPatch(current, {
665 automaticGitFetchInterval: Duration.seconds(15),
666 });
667​
668 expect(next.backgroundActivity).toEqual({
669 schemaVersion: 1,
670 profile: "custom",
671 baseProfile: "battery-saver",
672 overrides: {
673 automaticGitFetchInterval: Duration.seconds(15),
674 },
675 });
676 });
677​
678 it("prefers structured background activity settings over legacy aliases", () => {
679 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
680 backgroundActivity: {
681 schemaVersion: 1,
682 profile: "battery-saver",
683 overrides: {},
684 },
685 automaticGitFetchInterval: Duration.seconds(5),
686 backgroundActivityProfile: "performance",
687 });
688​
689 expect(next.backgroundActivity).toEqual({
690 schemaVersion: 1,
691 profile: "battery-saver",
692 overrides: {},
693 });
694 expect(next.backgroundActivityProfile).toBe("battery-saver");
695 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
696 });
697​
698 it("reconciles custom background activity back to a preset when overrides match the preset", () => {
699 const custom = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
700 automaticGitFetchInterval: Duration.seconds(15),
701 });
702 const next = applyServerSettingsPatch(custom, {
703 automaticGitFetchInterval: Duration.seconds(30),
704 });
705​
706 expect(next.backgroundActivity).toEqual({
707 schemaVersion: 1,
708 profile: "balanced",
709 overrides: {},
710 });
711 expect(next.backgroundActivityProfile).toBe("balanced");
712 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(30_000);
713 });
714​
715 it("drops custom overrides that duplicate the base profile", () => {
716 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
717 backgroundActivity: {
718 schemaVersion: 1,
719 profile: "custom",
720 baseProfile: "balanced",
721 overrides: {
722 automaticGitFetchInterval: Duration.seconds(30),
723 },
724 },
725 });
726​
727 expect(next.backgroundActivity).toEqual({
728 schemaVersion: 1,
729 profile: "balanced",
730 overrides: {},
731 });
732 });
733​
734 it("replaces the complete background override record", () => {
735 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
736 backgroundActivity: {
737 schemaVersion: 1,
738 profile: "custom",
739 baseProfile: "balanced",
740 overrides: {
741 automaticGitFetchInterval: Duration.seconds(15),
742 providerHealthRefreshInterval: Duration.minutes(3),
743 },
744 },
745 });
746​
747 const next = applyServerSettingsPatch(current, {
748 backgroundActivity: {
749 overrides: {
750 automaticGitFetchInterval: Duration.seconds(10),
751 },
752 },
753 });
754​
755 expect(next.backgroundActivity).toEqual({
756 schemaVersion: 1,
757 profile: "custom",
758 baseProfile: "balanced",
759 overrides: {
760 automaticGitFetchInterval: Duration.seconds(10),
761 },
762 });
763 });
764​
765 it("keeps interval overrides supplied with a profile patch", () => {
766 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
767 backgroundActivityProfile: "performance",
768 automaticGitFetchInterval: Duration.seconds(0),
769 providerHealthRefreshInterval: Duration.minutes(4),
770 });
771​
772 expect(next.backgroundActivity).toEqual({
773 schemaVersion: 1,
774 profile: "custom",
775 baseProfile: "performance",
776 overrides: {
777 automaticGitFetchInterval: Duration.seconds(0),
778 providerHealthRefreshInterval: Duration.minutes(4),
779 },
780 });
781 });
782​
783 it("ignores overrides attached to a concrete background profile", () => {
784 const resolved = resolveServerBackgroundActivitySettings({
785 ...DEFAULT_SERVER_SETTINGS,
786 backgroundActivity: {
787 schemaVersion: 1,
788 profile: "balanced",
789 overrides: {
790 pauseWhenOnBattery: true,
791 },
792 },
793 });
794​
795 expect(resolved.pauseWhenOnBattery).toBe(false);
796 });
797});