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

serverSettings.test.ts

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