Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 UsageLimitSourceId,
6 type ServerProvider,
7} from "@t3tools/contracts";
8import * as Duration from "effect/Duration";
9import { describe, expect, it } from "vite-plus/test";
10import { resolveServerBackgroundActivitySettings } from "./backgroundActivitySettings.ts";
11import { createModelSelection } from "./model.ts";
12import {
13 applyServerSettingsPatch,
14 extractPersistedServerObservabilitySettings,
15 isModelSelectionProviderEnabled,
16 normalizePersistedServerSettingString,
17 parsePersistedServerObservabilitySettings,
18 resolveSourceControlWriterModelSelection,
19} from "./serverSettings.ts";
20
21describe("serverSettings helpers", () => {
22 it("normalizes optional persisted strings", () => {
23 expect(normalizePersistedServerSettingString(undefined)).toBeUndefined();
24 expect(normalizePersistedServerSettingString(" ")).toBeUndefined();
25 expect(normalizePersistedServerSettingString(" http://localhost:4318/v1/traces ")).toBe(
26 "http://localhost:4318/v1/traces",
27 );
28 });
29
30 it("extracts persisted observability settings", () => {
31 expect(
32 extractPersistedServerObservabilitySettings({
33 observability: {
34 otlpTracesUrl: " http://localhost:4318/v1/traces ",
35 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
36 },
37 }),
38 ).toEqual({
39 otlpTracesUrl: "http://localhost:4318/v1/traces",
40 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
41 });
42 });
43
44 it("parses lenient persisted settings JSON", () => {
45 expect(
46 parsePersistedServerObservabilitySettings(
47 JSON.stringify({
48 observability: {
49 otlpTracesUrl: "http://localhost:4318/v1/traces",
50 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
51 },
52 }),
53 ),
54 ).toEqual({
55 otlpTracesUrl: "http://localhost:4318/v1/traces",
56 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
57 });
58 });
59
60 it("falls back cleanly when persisted settings are invalid", () => {
61 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
62 otlpTracesUrl: undefined,
63 otlpMetricsUrl: undefined,
64 });
65 });
66
67 it("replaces text generation selection when provider/model are provided", () => {
68 const current = {
69 ...DEFAULT_SERVER_SETTINGS,
70 textGenerationModelSelection: createModelSelection(
71 ProviderInstanceId.make("codex"),
72 "gpt-5.4-mini",
73 [
74 { id: "reasoningEffort", value: "high" },
75 { id: "fastMode", value: true },
76 ],
77 ),
78 };
79
80 expect(
81 applyServerSettingsPatch(current, {
82 textGenerationModelSelection: {
83 instanceId: ProviderInstanceId.make("codex"),
84 model: "gpt-5.4-mini",
85 },
86 }).textGenerationModelSelection,
87 ).toEqual({
88 instanceId: "codex",
89 model: "gpt-5.4-mini",
90 });
91 });
92
93 it("still deep merges text generation selection when only options are provided", () => {
94 const current = {
95 ...DEFAULT_SERVER_SETTINGS,
96 textGenerationModelSelection: createModelSelection(
97 ProviderInstanceId.make("codex"),
98 "gpt-5.4-mini",
99 [
100 { id: "reasoningEffort", value: "high" },
101 { id: "fastMode", value: true },
102 ],
103 ),
104 };
105
106 expect(
107 applyServerSettingsPatch(current, {
108 textGenerationModelSelection: {
109 options: [{ id: "fastMode", value: false }],
110 },
111 }).textGenerationModelSelection,
112 ).toEqual({
113 instanceId: "codex",
114 model: "gpt-5.4-mini",
115 options: [
116 { id: "reasoningEffort", value: "high" },
117 { id: "fastMode", value: false },
118 ],
119 });
120 });
121
122 it("replaces text generation selection across providers without leaking stale options", () => {
123 const current = {
124 ...DEFAULT_SERVER_SETTINGS,
125 textGenerationModelSelection: createModelSelection(
126 ProviderInstanceId.make("codex"),
127 "gpt-5.4-mini",
128 [
129 { id: "reasoningEffort", value: "high" },
130 { id: "fastMode", value: true },
131 ],
132 ),
133 };
134
135 expect(
136 applyServerSettingsPatch(current, {
137 textGenerationModelSelection: {
138 instanceId: ProviderInstanceId.make("opencode"),
139 model: "openai/gpt-5",
140 },
141 }).textGenerationModelSelection,
142 ).toEqual({
143 instanceId: "opencode",
144 model: "openai/gpt-5",
145 });
146 });
147
148 it("accepts array-based text generation selection patches", () => {
149 expect(
150 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
151 textGenerationModelSelection: {
152 instanceId: ProviderInstanceId.make("opencode"),
153 model: "openai/gpt-5",
154 options: [
155 { id: "variant", value: "prod" },
156 { id: "agent", value: "build" },
157 ],
158 },
159 }).textGenerationModelSelection,
160 ).toEqual({
161 instanceId: "opencode",
162 model: "openai/gpt-5",
163 options: [
164 { id: "variant", value: "prod" },
165 { id: "agent", value: "build" },
166 ],
167 });
168 });
169
170 it("replaces source control writer selection without retaining stale options", () => {
171 const current = {
172 ...DEFAULT_SERVER_SETTINGS,
173 sourceControlWriterModelSelection: createModelSelection(
174 ProviderInstanceId.make("codex"),
175 "gpt-5.4-mini",
176 [{ id: "reasoningEffort", value: "high" }],
177 ),
178 };
179
180 expect(
181 applyServerSettingsPatch(current, {
182 sourceControlWriterModelSelection: {
183 instanceId: ProviderInstanceId.make("opencode"),
184 model: "openai/gpt-5",
185 },
186 }).sourceControlWriterModelSelection,
187 ).toEqual({
188 instanceId: "opencode",
189 model: "openai/gpt-5",
190 });
191 });
192
193 it("clears source control writer selection with null", () => {
194 const current = {
195 ...DEFAULT_SERVER_SETTINGS,
196 sourceControlWriterModelSelection: createModelSelection(
197 ProviderInstanceId.make("codex"),
198 "gpt-5.4-mini",
199 ),
200 };
201
202 expect(
203 applyServerSettingsPatch(current, {
204 sourceControlWriterModelSelection: null,
205 }).sourceControlWriterModelSelection,
206 ).toBeNull();
207 });
208
209 it("falls back from a disabled source control writer provider without clearing its selection", () => {
210 const instanceId = ProviderInstanceId.make("codex_writer");
211 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
212 const settings = {
213 ...DEFAULT_SERVER_SETTINGS,
214 providerInstances: {
215 [instanceId]: {
216 driver: ProviderDriverKind.make("codex"),
217 enabled: false,
218 config: {},
219 },
220 },
221 sourceControlWriterModelSelection,
222 };
223
224 expect(isModelSelectionProviderEnabled(settings, sourceControlWriterModelSelection)).toBe(
225 false,
226 );
227 expect(resolveSourceControlWriterModelSelection(settings)).toBe(
228 settings.textGenerationModelSelection,
229 );
230 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
231 });
232
233 it("falls back from an unavailable source control writer provider", () => {
234 const instanceId = ProviderInstanceId.make("missing_writer");
235 const sourceControlWriterModelSelection = createModelSelection(instanceId, "missing-model");
236 const settings = {
237 ...DEFAULT_SERVER_SETTINGS,
238 providerInstances: {
239 [instanceId]: {
240 driver: ProviderDriverKind.make("missing-driver"),
241 config: {},
242 },
243 },
244 sourceControlWriterModelSelection,
245 };
246 const unavailableProvider = {
247 instanceId,
248 driver: ProviderDriverKind.make("missing-driver"),
249 enabled: false,
250 installed: false,
251 version: null,
252 status: "disabled",
253 auth: { status: "unknown" },
254 checkedAt: "2026-07-27T00:00:00.000Z",
255 availability: "unavailable",
256 unavailableReason: "This provider driver is not available in this build.",
257 models: [],
258 slashCommands: [],
259 skills: [],
260 } satisfies ServerProvider;
261
262 expect(resolveSourceControlWriterModelSelection(settings, [unavailableProvider])).toBe(
263 settings.textGenerationModelSelection,
264 );
265 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
266 });
267
268 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
269 const codexId = ProviderInstanceId.make("codex");
270 const current = {
271 ...DEFAULT_SERVER_SETTINGS,
272 providerInstances: {
273 [codexId]: {
274 driver: ProviderDriverKind.make("codex"),
275 displayName: "Codex Work",
276 accentColor: "#7c3aed",
277 enabled: true,
278 config: { homePath: "~/.codex" },
279 },
280 },
281 };
282
283 expect(
284 applyServerSettingsPatch(current, {
285 providerInstances: {
286 [codexId]: {
287 driver: ProviderDriverKind.make("codex"),
288 displayName: "Codex Work",
289 enabled: true,
290 config: { homePath: "~/.codex" },
291 },
292 },
293 }).providerInstances[codexId],
294 ).toEqual({
295 driver: ProviderDriverKind.make("codex"),
296 displayName: "Codex Work",
297 enabled: true,
298 config: { homePath: "~/.codex" },
299 });
300 });
301
302 it("upserts and removes usageLimitSources per entry so concurrent edits cannot clobber", () => {
303 const hubA = UsageLimitSourceId.make("cliproxy-a");
304 const hubB = UsageLimitSourceId.make("cliproxy-b");
305 const source = (url: string) => ({
306 kind: "cliproxy" as const,
307 url,
308 managementKey: "secret",
309 enabled: true,
310 });
311 const current = {
312 ...DEFAULT_SERVER_SETTINGS,
313 usageLimitSources: { [hubA]: source("http://a:8318") },
314 };
315
316 const added = applyServerSettingsPatch(current, {
317 usageLimitSources: { [hubB]: source("http://b:8318") },
318 });
319 expect(Object.keys(added.usageLimitSources)).toEqual([hubA, hubB]);
320
321 const removed = applyServerSettingsPatch(added, { usageLimitSources: { [hubA]: null } });
322 expect(Object.keys(removed.usageLimitSources)).toEqual([hubB]);
323 });
324
325 it("replaces and removes individual usage prices without clobbering other models", () => {
326 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
327 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
328 usagePriceOverrides: { "example-model": { ...prices, cacheReadCostPerMillionTokens: 0.5 } },
329 });
330 const added = applyServerSettingsPatch(current, {
331 usagePriceOverrides: { "other-model": prices },
332 });
333 const replaced = applyServerSettingsPatch(added, {
334 usagePriceOverrides: { "example-model": prices },
335 });
336 expect(replaced.usagePriceOverrides).toEqual({
337 "example-model": prices,
338 "other-model": prices,
339 });
340 const removed = applyServerSettingsPatch(replaced, {
341 usagePriceOverrides: { "example-model": null },
342 });
343 expect(removed.usagePriceOverrides).toEqual({ "other-model": prices });
344 expect(current.usagePriceOverrides["example-model"]?.cacheReadCostPerMillionTokens).toBe(0.5);
345 });
346
347 it("stores background activity profiles as a versioned object and syncs legacy aliases", () => {
348 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
349 backgroundActivity: {
350 schemaVersion: 1,
351 profile: "battery-saver",
352 overrides: {},
353 },
354 });
355
356 expect(next.backgroundActivity).toEqual({
357 schemaVersion: 1,
358 profile: "battery-saver",
359 overrides: {},
360 });
361 expect(next.backgroundActivityProfile).toBe("battery-saver");
362 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
363 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(
364 Duration.toMillis(Duration.minutes(15)),
365 );
366 });
367
368 it("turns legacy interval patches into custom background activity overrides", () => {
369 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
370 automaticGitFetchInterval: Duration.seconds(15),
371 });
372
373 expect(next.backgroundActivity).toEqual({
374 schemaVersion: 1,
375 profile: "custom",
376 baseProfile: "balanced",
377 overrides: {
378 automaticGitFetchInterval: Duration.seconds(15),
379 },
380 });
381 expect(resolveServerBackgroundActivitySettings(next).profile).toBe("balanced");
382 expect(
383 Duration.toMillis(resolveServerBackgroundActivitySettings(next).automaticGitFetchInterval),
384 ).toBe(15_000);
385 });
386
387 it("preserves legacy background activity settings when applying an unrelated patch", () => {
388 const current = {
389 ...DEFAULT_SERVER_SETTINGS,
390 backgroundActivityProfile: "performance" as const,
391 automaticGitFetchInterval: Duration.seconds(7),
392 providerHealthRefreshInterval: Duration.minutes(4),
393 };
394
395 const next = applyServerSettingsPatch(current, {
396 sourceControlWriterModelSelection: createModelSelection(
397 ProviderInstanceId.make("codex"),
398 "gpt-5.4-mini",
399 ),
400 });
401
402 expect(next.backgroundActivity).toEqual({
403 schemaVersion: 1,
404 profile: "custom",
405 baseProfile: "performance",
406 overrides: {
407 automaticGitFetchInterval: Duration.seconds(7),
408 providerHealthRefreshInterval: Duration.minutes(4),
409 },
410 });
411 expect(next.backgroundActivityProfile).toBe("performance");
412 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(7_000);
413 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(240_000);
414 });
415
416 it("does not reactivate dormant overrides from a concrete profile", () => {
417 const current = {
418 ...DEFAULT_SERVER_SETTINGS,
419 backgroundActivity: {
420 schemaVersion: 1 as const,
421 profile: "battery-saver" as const,
422 overrides: {
423 providerHealthRefreshInterval: Duration.seconds(5),
424 },
425 },
426 };
427
428 const next = applyServerSettingsPatch(current, {
429 automaticGitFetchInterval: Duration.seconds(15),
430 });
431
432 expect(next.backgroundActivity).toEqual({
433 schemaVersion: 1,
434 profile: "custom",
435 baseProfile: "battery-saver",
436 overrides: {
437 automaticGitFetchInterval: Duration.seconds(15),
438 },
439 });
440 });
441
442 it("prefers structured background activity settings over legacy aliases", () => {
443 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
444 backgroundActivity: {
445 schemaVersion: 1,
446 profile: "battery-saver",
447 overrides: {},
448 },
449 automaticGitFetchInterval: Duration.seconds(5),
450 backgroundActivityProfile: "performance",
451 });
452
453 expect(next.backgroundActivity).toEqual({
454 schemaVersion: 1,
455 profile: "battery-saver",
456 overrides: {},
457 });
458 expect(next.backgroundActivityProfile).toBe("battery-saver");
459 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
460 });
461
462 it("reconciles custom background activity back to a preset when overrides match the preset", () => {
463 const custom = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
464 automaticGitFetchInterval: Duration.seconds(15),
465 });
466 const next = applyServerSettingsPatch(custom, {
467 automaticGitFetchInterval: Duration.seconds(30),
468 });
469
470 expect(next.backgroundActivity).toEqual({
471 schemaVersion: 1,
472 profile: "balanced",
473 overrides: {},
474 });
475 expect(next.backgroundActivityProfile).toBe("balanced");
476 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(30_000);
477 });
478
479 it("drops custom overrides that duplicate the base profile", () => {
480 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
481 backgroundActivity: {
482 schemaVersion: 1,
483 profile: "custom",
484 baseProfile: "balanced",
485 overrides: {
486 automaticGitFetchInterval: Duration.seconds(30),
487 },
488 },
489 });
490
491 expect(next.backgroundActivity).toEqual({
492 schemaVersion: 1,
493 profile: "balanced",
494 overrides: {},
495 });
496 });
497
498 it("replaces the complete background override record", () => {
499 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
500 backgroundActivity: {
501 schemaVersion: 1,
502 profile: "custom",
503 baseProfile: "balanced",
504 overrides: {
505 automaticGitFetchInterval: Duration.seconds(15),
506 providerHealthRefreshInterval: Duration.minutes(3),
507 },
508 },
509 });
510
511 const next = applyServerSettingsPatch(current, {
512 backgroundActivity: {
513 overrides: {
514 automaticGitFetchInterval: Duration.seconds(10),
515 },
516 },
517 });
518
519 expect(next.backgroundActivity).toEqual({
520 schemaVersion: 1,
521 profile: "custom",
522 baseProfile: "balanced",
523 overrides: {
524 automaticGitFetchInterval: Duration.seconds(10),
525 },
526 });
527 });
528
529 it("keeps interval overrides supplied with a profile patch", () => {
530 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
531 backgroundActivityProfile: "performance",
532 automaticGitFetchInterval: Duration.seconds(0),
533 providerHealthRefreshInterval: Duration.minutes(4),
534 });
535
536 expect(next.backgroundActivity).toEqual({
537 schemaVersion: 1,
538 profile: "custom",
539 baseProfile: "performance",
540 overrides: {
541 automaticGitFetchInterval: Duration.seconds(0),
542 providerHealthRefreshInterval: Duration.minutes(4),
543 },
544 });
545 });
546
547 it("ignores overrides attached to a concrete background profile", () => {
548 const resolved = resolveServerBackgroundActivitySettings({
549 ...DEFAULT_SERVER_SETTINGS,
550 backgroundActivity: {
551 schemaVersion: 1,
552 profile: "balanced",
553 overrides: {
554 pauseWhenOnBattery: true,
555 },
556 },
557 });
558
559 expect(resolved.pauseWhenOnBattery).toBe(false);
560 });
561});