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