Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import {
3 DEFAULT_SERVER_SETTINGS,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 ServerSettings,
7 ServerSettingsPatch,
8} from "@t3tools/contracts";
9import { createModelSelection } from "@t3tools/shared/model";
10import { assert, it } from "@effect/vitest";
11import * as Effect from "effect/Effect";
12import * as Duration from "effect/Duration";
13import * as FileSystem from "effect/FileSystem";
14import * as Layer from "effect/Layer";
15import * as Schema from "effect/Schema";
16import { ServerConfig } from "./config.ts";
17import { ServerSettingsLive, ServerSettingsService } from "./serverSettings.ts";
18
19const decodeSettingsPatch = Schema.decodeUnknownEffect(ServerSettingsPatch);
20const decodeServerSettings = Schema.decodeUnknownEffect(ServerSettings);
21
22const makeServerSettingsLayer = () =>
23 ServerSettingsLive.pipe(
24 Layer.provideMerge(
25 Layer.fresh(
26 ServerConfig.layerTest(process.cwd(), {
27 prefix: "t3code-server-settings-test-",
28 }),
29 ),
30 ),
31 );
32
33it.layer(NodeServices.layer)("server settings", (it) => {
34 it.effect("decodes nested settings patches", () =>
35 Effect.gen(function* () {
36 assert.deepEqual(
37 yield* decodeSettingsPatch({ providers: { codex: { binaryPath: "/tmp/codex" } } }),
38 {
39 providers: { codex: { binaryPath: "/tmp/codex" } },
40 },
41 );
42
43 assert.deepEqual(
44 yield* decodeSettingsPatch({
45 textGenerationModelSelection: {
46 options: [{ id: "fastMode", value: false }],
47 },
48 }),
49 {
50 textGenerationModelSelection: {
51 options: [{ id: "fastMode", value: false }],
52 },
53 },
54 );
55 }),
56 );
57
58 it.effect(
59 "decodes legacy object-shaped textGenerationModelSelection.options from settings.json",
60 () =>
61 Effect.gen(function* () {
62 const decoded = yield* decodeServerSettings({
63 textGenerationModelSelection: {
64 provider: ProviderDriverKind.make("codex"),
65 model: "gpt-5.4-mini",
66 options: { reasoningEffort: "low" },
67 },
68 });
69
70 assert.deepEqual(decoded.textGenerationModelSelection, {
71 instanceId: ProviderInstanceId.make("codex"),
72 model: "gpt-5.4-mini",
73 options: [{ id: "reasoningEffort", value: "low" }],
74 });
75 }),
76 );
77
78 it.effect("deep merges nested settings updates without dropping siblings", () =>
79 Effect.gen(function* () {
80 const serverSettings = yield* ServerSettingsService;
81
82 yield* serverSettings.updateSettings({
83 providers: {
84 codex: {
85 binaryPath: "/usr/local/bin/codex",
86 homePath: "/Users/julius/.codex",
87 },
88 claudeAgent: {
89 binaryPath: "/usr/local/bin/claude",
90 customModels: ["claude-custom"],
91 },
92 },
93 textGenerationModelSelection: {
94 instanceId: ProviderInstanceId.make("codex"),
95 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
96 options: createModelSelection(
97 ProviderInstanceId.make("codex"),
98 DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
99 [
100 { id: "reasoningEffort", value: "high" },
101 { id: "fastMode", value: true },
102 ],
103 ).options!,
104 },
105 });
106
107 const next = yield* serverSettings.updateSettings({
108 providers: {
109 codex: {
110 binaryPath: "/opt/homebrew/bin/codex",
111 },
112 },
113 textGenerationModelSelection: {
114 options: [{ id: "fastMode", value: false }],
115 },
116 });
117
118 assert.deepEqual(next.providers.codex, {
119 enabled: true,
120 binaryPath: "/opt/homebrew/bin/codex",
121 homePath: "/Users/julius/.codex",
122 shadowHomePath: "",
123 customModels: [],
124 });
125 assert.deepEqual(next.providers.claudeAgent, {
126 enabled: true,
127 binaryPath: "/usr/local/bin/claude",
128 homePath: "",
129 customModels: ["claude-custom"],
130 launchArgs: "",
131 });
132 assert.deepEqual(
133 next.textGenerationModelSelection,
134 createModelSelection(
135 ProviderInstanceId.make("codex"),
136 DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
137 [
138 { id: "reasoningEffort", value: "high" },
139 { id: "fastMode", value: false },
140 ],
141 ),
142 );
143 }).pipe(Effect.provide(makeServerSettingsLayer())),
144 );
145
146 it.effect("preserves model when switching providers via textGenerationModelSelection", () =>
147 Effect.gen(function* () {
148 const serverSettings = yield* ServerSettingsService;
149
150 // Start with Claude text generation selection
151 yield* serverSettings.updateSettings({
152 textGenerationModelSelection: {
153 instanceId: ProviderInstanceId.make("claudeAgent"),
154 model: "claude-sonnet-4-6",
155 options: createModelSelection(
156 ProviderInstanceId.make("claudeAgent"),
157 "claude-sonnet-4-6",
158 [{ id: "effort", value: "high" }],
159 ).options!,
160 },
161 });
162
163 // Switch to Codex — the stale Claude "effort" in options must not
164 // cause the update to lose the selected model.
165 const next = yield* serverSettings.updateSettings({
166 textGenerationModelSelection: {
167 instanceId: ProviderInstanceId.make("codex"),
168 model: "gpt-5.4",
169 options: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
170 { id: "reasoningEffort", value: "high" },
171 ]).options!,
172 },
173 });
174
175 assert.deepEqual(
176 next.textGenerationModelSelection,
177 createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
178 { id: "reasoningEffort", value: "high" },
179 ]),
180 );
181 }).pipe(Effect.provide(makeServerSettingsLayer())),
182 );
183
184 it.effect("preserves custom provider instance text generation selections", () =>
185 Effect.gen(function* () {
186 const serverSettings = yield* ServerSettingsService;
187
188 const next = yield* serverSettings.updateSettings({
189 providerInstances: {
190 [ProviderInstanceId.make("claude_openrouter")]: {
191 driver: ProviderDriverKind.make("claudeAgent"),
192 enabled: true,
193 config: { customModels: ["openai/gpt-5.5"] },
194 },
195 },
196 textGenerationModelSelection: {
197 instanceId: ProviderInstanceId.make("claude_openrouter"),
198 model: "openai/gpt-5.5",
199 },
200 });
201
202 assert.deepEqual(next.textGenerationModelSelection, {
203 instanceId: ProviderInstanceId.make("claude_openrouter"),
204 model: "openai/gpt-5.5",
205 });
206 }).pipe(Effect.provide(makeServerSettingsLayer())),
207 );
208
209 it.effect(
210 "uses explicit provider instance enabled state over legacy provider enabled state",
211 () =>
212 Effect.gen(function* () {
213 const serverSettings = yield* ServerSettingsService;
214 const instanceId = ProviderInstanceId.make("claude_openrouter");
215
216 const next = yield* serverSettings.updateSettings({
217 providers: {
218 claudeAgent: {
219 enabled: false,
220 },
221 },
222 providerInstances: {
223 [instanceId]: {
224 driver: ProviderDriverKind.make("claudeAgent"),
225 enabled: true,
226 config: { customModels: ["openai/gpt-5.5"] },
227 },
228 },
229 textGenerationModelSelection: {
230 instanceId,
231 model: "openai/gpt-5.5",
232 },
233 });
234
235 assert.deepEqual(next.textGenerationModelSelection, {
236 instanceId,
237 model: "openai/gpt-5.5",
238 });
239 }).pipe(Effect.provide(makeServerSettingsLayer())),
240 );
241
242 it.effect("preserves enabled text generation selections for non-built-in drivers", () =>
243 Effect.gen(function* () {
244 const serverSettings = yield* ServerSettingsService;
245 const instanceId = ProviderInstanceId.make("openrouter_text");
246
247 const next = yield* serverSettings.updateSettings({
248 providerInstances: {
249 [instanceId]: {
250 driver: ProviderDriverKind.make("openrouter"),
251 enabled: true,
252 config: { customModels: ["openai/gpt-5.5"] },
253 },
254 },
255 textGenerationModelSelection: {
256 instanceId,
257 model: "openai/gpt-5.5",
258 },
259 });
260
261 assert.deepEqual(next.textGenerationModelSelection, {
262 instanceId,
263 model: "openai/gpt-5.5",
264 });
265 }).pipe(Effect.provide(makeServerSettingsLayer())),
266 );
267
268 it.effect("drops stale text generation options when resetting model selection", () =>
269 Effect.gen(function* () {
270 const serverSettings = yield* ServerSettingsService;
271
272 yield* serverSettings.updateSettings({
273 textGenerationModelSelection: {
274 instanceId: ProviderInstanceId.make("codex"),
275 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
276 options: createModelSelection(
277 ProviderInstanceId.make("codex"),
278 DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
279 [
280 { id: "reasoningEffort", value: "high" },
281 { id: "fastMode", value: true },
282 ],
283 ).options!,
284 },
285 });
286
287 const next = yield* serverSettings.updateSettings({
288 textGenerationModelSelection: {
289 instanceId: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.instanceId,
290 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
291 },
292 });
293
294 assert.deepEqual(next.textGenerationModelSelection, {
295 instanceId: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.instanceId,
296 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
297 });
298 }).pipe(Effect.provide(makeServerSettingsLayer())),
299 );
300
301 it.effect("replaces provider instance maps when clearing optional fields", () =>
302 Effect.gen(function* () {
303 const serverSettings = yield* ServerSettingsService;
304 const codexId = ProviderInstanceId.make("codex");
305
306 yield* serverSettings.updateSettings({
307 providerInstances: {
308 [codexId]: {
309 driver: ProviderDriverKind.make("codex"),
310 displayName: "Codex Work",
311 accentColor: "#7c3aed",
312 enabled: true,
313 config: { homePath: "~/.codex" },
314 },
315 },
316 });
317
318 const next = yield* serverSettings.updateSettings({
319 providerInstances: {
320 [codexId]: {
321 driver: ProviderDriverKind.make("codex"),
322 displayName: "Codex Work",
323 enabled: true,
324 config: { homePath: "~/.codex" },
325 },
326 },
327 });
328
329 assert.deepEqual(next.providerInstances[codexId], {
330 driver: ProviderDriverKind.make("codex"),
331 displayName: "Codex Work",
332 enabled: true,
333 config: { homePath: "~/.codex" },
334 });
335 }).pipe(Effect.provide(makeServerSettingsLayer())),
336 );
337
338 it.effect("trims provider path settings when updates are applied", () =>
339 Effect.gen(function* () {
340 const serverSettings = yield* ServerSettingsService;
341
342 const next = yield* serverSettings.updateSettings({
343 providers: {
344 codex: {
345 binaryPath: " /opt/homebrew/bin/codex ",
346 homePath: " ",
347 },
348 claudeAgent: {
349 binaryPath: " /opt/homebrew/bin/claude ",
350 },
351 opencode: {
352 binaryPath: " /opt/homebrew/bin/opencode ",
353 serverUrl: " http://127.0.0.1:4096 ",
354 serverPassword: " secret-password ",
355 },
356 },
357 });
358
359 assert.deepEqual(next.providers.codex, {
360 enabled: true,
361 binaryPath: "/opt/homebrew/bin/codex",
362 homePath: "",
363 shadowHomePath: "",
364 customModels: [],
365 });
366 assert.deepEqual(next.providers.claudeAgent, {
367 enabled: true,
368 binaryPath: "/opt/homebrew/bin/claude",
369 homePath: "",
370 customModels: [],
371 launchArgs: "",
372 });
373 assert.deepEqual(next.providers.opencode, {
374 enabled: true,
375 binaryPath: "/opt/homebrew/bin/opencode",
376 serverUrl: "http://127.0.0.1:4096",
377 serverPassword: "secret-password",
378 customModels: [],
379 });
380 }).pipe(Effect.provide(makeServerSettingsLayer())),
381 );
382
383 it.effect("trims observability settings when updates are applied", () =>
384 Effect.gen(function* () {
385 const serverSettings = yield* ServerSettingsService;
386
387 const next = yield* serverSettings.updateSettings({
388 addProjectBaseDirectory: " ~/Development ",
389 observability: {
390 otlpTracesUrl: " http://localhost:4318/v1/traces ",
391 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
392 },
393 });
394
395 assert.equal(next.addProjectBaseDirectory, "~/Development");
396 assert.deepEqual(next.observability, {
397 otlpTracesUrl: "http://localhost:4318/v1/traces",
398 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
399 });
400 }).pipe(Effect.provide(makeServerSettingsLayer())),
401 );
402
403 it.effect("defaults blank binary paths to provider executables", () =>
404 Effect.gen(function* () {
405 const serverSettings = yield* ServerSettingsService;
406
407 const next = yield* serverSettings.updateSettings({
408 providers: {
409 codex: {
410 binaryPath: " ",
411 },
412 claudeAgent: {
413 binaryPath: "",
414 },
415 },
416 });
417
418 assert.equal(next.providers.codex.binaryPath, "codex");
419 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
420 }).pipe(Effect.provide(makeServerSettingsLayer())),
421 );
422
423 it.effect("writes only non-default server settings to disk", () =>
424 Effect.gen(function* () {
425 const serverSettings = yield* ServerSettingsService;
426 const serverConfig = yield* ServerConfig;
427 const fileSystem = yield* FileSystem.FileSystem;
428 const next = yield* serverSettings.updateSettings({
429 addProjectBaseDirectory: "~/Development",
430 observability: {
431 otlpTracesUrl: "http://localhost:4318/v1/traces",
432 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
433 },
434 providers: {
435 codex: {
436 binaryPath: "/opt/homebrew/bin/codex",
437 },
438 opencode: {
439 serverUrl: "http://127.0.0.1:4096",
440 serverPassword: "secret-password",
441 },
442 },
443 automaticGitFetchInterval: Duration.seconds(10),
444 });
445
446 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
447
448 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
449 // @effect-diagnostics-next-line preferSchemaOverJson:off
450 assert.deepEqual(JSON.parse(raw), {
451 addProjectBaseDirectory: "~/Development",
452 observability: {
453 otlpTracesUrl: "http://localhost:4318/v1/traces",
454 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
455 },
456 providers: {
457 codex: {
458 binaryPath: "/opt/homebrew/bin/codex",
459 },
460 opencode: {
461 serverUrl: "http://127.0.0.1:4096",
462 serverPassword: "secret-password",
463 },
464 },
465 automaticGitFetchInterval: 10_000,
466 });
467 }).pipe(Effect.provide(makeServerSettingsLayer())),
468 );
469
470 it.effect("stores sensitive provider instance environment values outside settings.json", () =>
471 Effect.gen(function* () {
472 const serverSettings = yield* ServerSettingsService;
473 const serverConfig = yield* ServerConfig;
474 const fileSystem = yield* FileSystem.FileSystem;
475 const instanceId = ProviderInstanceId.make("codex_personal");
476
477 const next = yield* serverSettings.updateSettings({
478 providerInstances: {
479 [instanceId]: {
480 driver: ProviderDriverKind.make("codex"),
481 environment: [
482 { name: "OPENROUTER_API_KEY", value: "sk-or-secret", sensitive: true },
483 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
484 ],
485 config: {},
486 },
487 },
488 });
489
490 assert.deepEqual(next.providerInstances[instanceId]?.environment, [
491 {
492 name: "OPENROUTER_API_KEY",
493 value: "sk-or-secret",
494 sensitive: true,
495 valueRedacted: true,
496 },
497 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
498 ]);
499
500 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
501 assert.notInclude(raw, "sk-or-secret");
502 // @effect-diagnostics-next-line preferSchemaOverJson:off
503 assert.deepEqual(JSON.parse(raw).providerInstances.codex_personal.environment, [
504 {
505 name: "OPENROUTER_API_KEY",
506 value: "",
507 sensitive: true,
508 valueRedacted: true,
509 },
510 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
511 ]);
512
513 const roundTripped = yield* serverSettings.updateSettings({
514 providerInstances: {
515 [instanceId]: {
516 driver: ProviderDriverKind.make("codex"),
517 displayName: "Codex Personal",
518 environment: [
519 { name: "OPENROUTER_API_KEY", value: "", sensitive: true, valueRedacted: true },
520 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
521 ],
522 config: {},
523 },
524 },
525 });
526
527 assert.equal(
528 roundTripped.providerInstances[instanceId]?.environment?.[0]?.value,
529 "sk-or-secret",
530 );
531 }).pipe(Effect.provide(makeServerSettingsLayer())),
532 );
533});