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