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

serverSettings.test.ts

755 lines27.0 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 });
228 expect(
229 parsePersistedServerObservabilitySettings(
230 JSON.stringify({ observability: { otlpTracesUrl: " ", otlpMetricsUrl: "" } }),
231 ),
232 ).toEqual({
233 otlpTracesUrl: undefined,
234 otlpMetricsUrl: undefined,
235 });
236 });
237​
238 it("parses lenient persisted settings JSON and trims observability URLs", () => {
239 expect(
240 parsePersistedServerObservabilitySettings(
241 JSON.stringify({
242 observability: {
243 otlpTracesUrl: " http://localhost:4318/v1/traces ",
244 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
245 },
246 }),
247 ),
248 ).toEqual({
249 otlpTracesUrl: "http://localhost:4318/v1/traces",
250 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
251 });
252 });
253​
254 it("falls back cleanly when persisted settings are invalid", () => {
255 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
256 otlpTracesUrl: undefined,
257 otlpMetricsUrl: undefined,
258 });
259 });
260​
261 it("replaces text generation selection when provider/model are provided", () => {
262 const current = {
263 ...DEFAULT_SERVER_SETTINGS,
264 textGenerationModelSelection: createModelSelection(
265 ProviderInstanceId.make("codex"),
266 "gpt-5.4-mini",
267 [
268 { id: "reasoningEffort", value: "high" },
269 { id: "fastMode", value: true },
270 ],
271 ),
272 };
273​
274 expect(
275 applyServerSettingsPatch(current, {
276 textGenerationModelSelection: {
277 instanceId: ProviderInstanceId.make("codex"),
278 model: "gpt-5.4-mini",
279 },
280 }).textGenerationModelSelection,
281 ).toEqual({
282 instanceId: "codex",
283 model: "gpt-5.4-mini",
284 });
285 });
286​
287 it("still deep merges text generation selection when only options are provided", () => {
288 const current = {
289 ...DEFAULT_SERVER_SETTINGS,
290 textGenerationModelSelection: createModelSelection(
291 ProviderInstanceId.make("codex"),
292 "gpt-5.4-mini",
293 [
294 { id: "reasoningEffort", value: "high" },
295 { id: "fastMode", value: true },
296 ],
297 ),
298 };
299​
300 expect(
301 applyServerSettingsPatch(current, {
302 textGenerationModelSelection: {
303 options: [{ id: "fastMode", value: false }],
304 },
305 }).textGenerationModelSelection,
306 ).toEqual({
307 instanceId: "codex",
308 model: "gpt-5.4-mini",
309 options: [
310 { id: "reasoningEffort", value: "high" },
311 { id: "fastMode", value: false },
312 ],
313 });
314 });
315​
316 it("replaces text generation selection across providers without leaking stale options", () => {
317 const current = {
318 ...DEFAULT_SERVER_SETTINGS,
319 textGenerationModelSelection: createModelSelection(
320 ProviderInstanceId.make("codex"),
321 "gpt-5.4-mini",
322 [
323 { id: "reasoningEffort", value: "high" },
324 { id: "fastMode", value: true },
325 ],
326 ),
327 };
328​
329 expect(
330 applyServerSettingsPatch(current, {
331 textGenerationModelSelection: {
332 instanceId: ProviderInstanceId.make("opencode"),
333 model: "openai/gpt-5",
334 },
335 }).textGenerationModelSelection,
336 ).toEqual({
337 instanceId: "opencode",
338 model: "openai/gpt-5",
339 });
340 });
341​
342 it("accepts array-based text generation selection patches", () => {
343 expect(
344 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
345 textGenerationModelSelection: {
346 instanceId: ProviderInstanceId.make("opencode"),
347 model: "openai/gpt-5",
348 options: [
349 { id: "variant", value: "prod" },
350 { id: "agent", value: "build" },
351 ],
352 },
353 }).textGenerationModelSelection,
354 ).toEqual({
355 instanceId: "opencode",
356 model: "openai/gpt-5",
357 options: [
358 { id: "variant", value: "prod" },
359 { id: "agent", value: "build" },
360 ],
361 });
362 });
363​
364 it("replaces source control writer selection without retaining stale options", () => {
365 const current = {
366 ...DEFAULT_SERVER_SETTINGS,
367 sourceControlWriterModelSelection: createModelSelection(
368 ProviderInstanceId.make("codex"),
369 "gpt-5.4-mini",
370 [{ id: "reasoningEffort", value: "high" }],
371 ),
372 };
373​
374 expect(
375 applyServerSettingsPatch(current, {
376 sourceControlWriterModelSelection: {
377 instanceId: ProviderInstanceId.make("opencode"),
378 model: "openai/gpt-5",
379 },
380 }).sourceControlWriterModelSelection,
381 ).toEqual({
382 instanceId: "opencode",
383 model: "openai/gpt-5",
384 });
385 });
386​
387 it("clears source control writer selection with null", () => {
388 const current = {
389 ...DEFAULT_SERVER_SETTINGS,
390 sourceControlWriterModelSelection: createModelSelection(
391 ProviderInstanceId.make("codex"),
392 "gpt-5.4-mini",
393 ),
394 };
395​
396 expect(
397 applyServerSettingsPatch(current, {
398 sourceControlWriterModelSelection: null,
399 }).sourceControlWriterModelSelection,
400 ).toBeNull();
401 });
402​
403 it("falls back from a disabled source control writer provider without clearing its selection", () => {
404 const instanceId = ProviderInstanceId.make("codex_writer");
405 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
406 const settings = {
407 ...DEFAULT_SERVER_SETTINGS,
408 providerInstances: {
409 [instanceId]: {
410 driver: ProviderDriverKind.make("codex"),
411 enabled: false,
412 config: {},
413 },
414 },
415 sourceControlWriterModelSelection,
416 };
417​
418 expect(isModelSelectionProviderEnabled(settings, sourceControlWriterModelSelection)).toBe(
419 false,
420 );
421 expect(resolveSourceControlWriterModelSelection(settings)).toBe(
422 settings.textGenerationModelSelection,
423 );
424 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
425 });
426​
427 it("falls back from an unavailable source control writer provider", () => {
428 const instanceId = ProviderInstanceId.make("missing_writer");
429 const sourceControlWriterModelSelection = createModelSelection(instanceId, "missing-model");
430 const settings = {
431 ...DEFAULT_SERVER_SETTINGS,
432 providerInstances: {
433 [instanceId]: {
434 driver: ProviderDriverKind.make("missing-driver"),
435 config: {},
436 },
437 },
438 sourceControlWriterModelSelection,
439 };
440 const unavailableProvider = {
441 instanceId,
442 driver: ProviderDriverKind.make("missing-driver"),
443 enabled: false,
444 installed: false,
445 version: null,
446 status: "disabled",
447 auth: { status: "unknown" },
448 checkedAt: "2026-07-27T00:00:00.000Z",
449 availability: "unavailable",
450 unavailableReason: "This provider driver is not available in this build.",
451 models: [],
452 slashCommands: [],
453 skills: [],
454 } satisfies ServerProvider;
455​
456 expect(resolveSourceControlWriterModelSelection(settings, [unavailableProvider])).toBe(
457 settings.textGenerationModelSelection,
458 );
459 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
460 });
461​
462 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
463 const codexId = ProviderInstanceId.make("codex");
464 const current = {
465 ...DEFAULT_SERVER_SETTINGS,
466 providerInstances: {
467 [codexId]: {
468 driver: ProviderDriverKind.make("codex"),
469 displayName: "Codex Work",
470 accentColor: "#7c3aed",
471 enabled: true,
472 config: { homePath: "~/.codex" },
473 },
474 },
475 };
476​
477 expect(
478 applyServerSettingsPatch(current, {
479 providerInstances: {
480 [codexId]: {
481 driver: ProviderDriverKind.make("codex"),
482 displayName: "Codex Work",
483 enabled: true,
484 config: { homePath: "~/.codex" },
485 },
486 },
487 }).providerInstances[codexId],
488 ).toEqual({
489 driver: ProviderDriverKind.make("codex"),
490 displayName: "Codex Work",
491 enabled: true,
492 config: { homePath: "~/.codex" },
493 });
494 });
495​
496 it("upserts and removes usageLimitSources per entry so concurrent edits cannot clobber", () => {
497 const hubA = UsageLimitSourceId.make("cliproxy-a");
498 const hubB = UsageLimitSourceId.make("cliproxy-b");
499 const source = (url: string) => ({
500 kind: "cliproxy" as const,
501 url,
502 managementKey: "secret",
503 enabled: true,
504 });
505 const current = {
506 ...DEFAULT_SERVER_SETTINGS,
507 usageLimitSources: { [hubA]: source("http://a:8318") },
508 };
509​
510 const added = applyServerSettingsPatch(current, {
511 usageLimitSources: { [hubB]: source("http://b:8318") },
512 });
513 expect(Object.keys(added.usageLimitSources)).toEqual([hubA, hubB]);
514​
515 const removed = applyServerSettingsPatch(added, { usageLimitSources: { [hubA]: null } });
516 expect(Object.keys(removed.usageLimitSources)).toEqual([hubB]);
517 });
518​
519 it("replaces and removes individual usage prices without clobbering other models", () => {
520 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
521 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
522 usagePriceOverrides: { "example-model": { ...prices, cacheReadCostPerMillionTokens: 0.5 } },
523 });
524 const added = applyServerSettingsPatch(current, {
525 usagePriceOverrides: { "other-model": prices },
526 });
527 const replaced = applyServerSettingsPatch(added, {
528 usagePriceOverrides: { "example-model": prices },
529 });
530 expect(replaced.usagePriceOverrides).toEqual({
531 "example-model": prices,
532 "other-model": prices,
533 });
534 const removed = applyServerSettingsPatch(replaced, {
535 usagePriceOverrides: { "example-model": null },
536 });
537 expect(removed.usagePriceOverrides).toEqual({ "other-model": prices });
538 expect(current.usagePriceOverrides["example-model"]?.cacheReadCostPerMillionTokens).toBe(0.5);
539 });
540​
541 it("stores background activity profiles as a versioned object and syncs legacy aliases", () => {
542 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
543 backgroundActivity: {
544 schemaVersion: 1,
545 profile: "battery-saver",
546 overrides: {},
547 },
548 });
549​
550 expect(next.backgroundActivity).toEqual({
551 schemaVersion: 1,
552 profile: "battery-saver",
553 overrides: {},
554 });
555 expect(next.backgroundActivityProfile).toBe("battery-saver");
556 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
557 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(
558 Duration.toMillis(Duration.minutes(15)),
559 );
560 });
561​
562 it("turns legacy interval patches into custom background activity overrides", () => {
563 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
564 automaticGitFetchInterval: Duration.seconds(15),
565 });
566​
567 expect(next.backgroundActivity).toEqual({
568 schemaVersion: 1,
569 profile: "custom",
570 baseProfile: "balanced",
571 overrides: {
572 automaticGitFetchInterval: Duration.seconds(15),
573 },
574 });
575 expect(resolveServerBackgroundActivitySettings(next).profile).toBe("balanced");
576 expect(
577 Duration.toMillis(resolveServerBackgroundActivitySettings(next).automaticGitFetchInterval),
578 ).toBe(15_000);
579 });
580​
581 it("preserves legacy background activity settings when applying an unrelated patch", () => {
582 const current = {
583 ...DEFAULT_SERVER_SETTINGS,
584 backgroundActivityProfile: "performance" as const,
585 automaticGitFetchInterval: Duration.seconds(7),
586 providerHealthRefreshInterval: Duration.minutes(4),
587 };
588​
589 const next = applyServerSettingsPatch(current, {
590 sourceControlWriterModelSelection: createModelSelection(
591 ProviderInstanceId.make("codex"),
592 "gpt-5.4-mini",
593 ),
594 });
595​
596 expect(next.backgroundActivity).toEqual({
597 schemaVersion: 1,
598 profile: "custom",
599 baseProfile: "performance",
600 overrides: {
601 automaticGitFetchInterval: Duration.seconds(7),
602 providerHealthRefreshInterval: Duration.minutes(4),
603 },
604 });
605 expect(next.backgroundActivityProfile).toBe("performance");
606 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(7_000);
607 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(240_000);
608 });
609​
610 it("does not reactivate dormant overrides from a concrete profile", () => {
611 const current = {
612 ...DEFAULT_SERVER_SETTINGS,
613 backgroundActivity: {
614 schemaVersion: 1 as const,
615 profile: "battery-saver" as const,
616 overrides: {
617 providerHealthRefreshInterval: Duration.seconds(5),
618 },
619 },
620 };
621​
622 const next = applyServerSettingsPatch(current, {
623 automaticGitFetchInterval: Duration.seconds(15),
624 });
625​
626 expect(next.backgroundActivity).toEqual({
627 schemaVersion: 1,
628 profile: "custom",
629 baseProfile: "battery-saver",
630 overrides: {
631 automaticGitFetchInterval: Duration.seconds(15),
632 },
633 });
634 });
635​
636 it("prefers structured background activity settings over legacy aliases", () => {
637 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
638 backgroundActivity: {
639 schemaVersion: 1,
640 profile: "battery-saver",
641 overrides: {},
642 },
643 automaticGitFetchInterval: Duration.seconds(5),
644 backgroundActivityProfile: "performance",
645 });
646​
647 expect(next.backgroundActivity).toEqual({
648 schemaVersion: 1,
649 profile: "battery-saver",
650 overrides: {},
651 });
652 expect(next.backgroundActivityProfile).toBe("battery-saver");
653 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
654 });
655​
656 it("reconciles custom background activity back to a preset when overrides match the preset", () => {
657 const custom = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
658 automaticGitFetchInterval: Duration.seconds(15),
659 });
660 const next = applyServerSettingsPatch(custom, {
661 automaticGitFetchInterval: Duration.seconds(30),
662 });
663​
664 expect(next.backgroundActivity).toEqual({
665 schemaVersion: 1,
666 profile: "balanced",
667 overrides: {},
668 });
669 expect(next.backgroundActivityProfile).toBe("balanced");
670 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(30_000);
671 });
672​
673 it("drops custom overrides that duplicate the base profile", () => {
674 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
675 backgroundActivity: {
676 schemaVersion: 1,
677 profile: "custom",
678 baseProfile: "balanced",
679 overrides: {
680 automaticGitFetchInterval: Duration.seconds(30),
681 },
682 },
683 });
684​
685 expect(next.backgroundActivity).toEqual({
686 schemaVersion: 1,
687 profile: "balanced",
688 overrides: {},
689 });
690 });
691​
692 it("replaces the complete background override record", () => {
693 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
694 backgroundActivity: {
695 schemaVersion: 1,
696 profile: "custom",
697 baseProfile: "balanced",
698 overrides: {
699 automaticGitFetchInterval: Duration.seconds(15),
700 providerHealthRefreshInterval: Duration.minutes(3),
701 },
702 },
703 });
704​
705 const next = applyServerSettingsPatch(current, {
706 backgroundActivity: {
707 overrides: {
708 automaticGitFetchInterval: Duration.seconds(10),
709 },
710 },
711 });
712​
713 expect(next.backgroundActivity).toEqual({
714 schemaVersion: 1,
715 profile: "custom",
716 baseProfile: "balanced",
717 overrides: {
718 automaticGitFetchInterval: Duration.seconds(10),
719 },
720 });
721 });
722​
723 it("keeps interval overrides supplied with a profile patch", () => {
724 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
725 backgroundActivityProfile: "performance",
726 automaticGitFetchInterval: Duration.seconds(0),
727 providerHealthRefreshInterval: Duration.minutes(4),
728 });
729​
730 expect(next.backgroundActivity).toEqual({
731 schemaVersion: 1,
732 profile: "custom",
733 baseProfile: "performance",
734 overrides: {
735 automaticGitFetchInterval: Duration.seconds(0),
736 providerHealthRefreshInterval: Duration.minutes(4),
737 },
738 });
739 });
740​
741 it("ignores overrides attached to a concrete background profile", () => {
742 const resolved = resolveServerBackgroundActivitySettings({
743 ...DEFAULT_SERVER_SETTINGS,
744 backgroundActivity: {
745 schemaVersion: 1,
746 profile: "balanced",
747 overrides: {
748 pauseWhenOnBattery: true,
749 },
750 },
751 });
752​
753 expect(resolved.pauseWhenOnBattery).toBe(false);
754 });
755});