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

serverSettings.test.ts

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