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

serverSettings.test.ts

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