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