Open History to see the last commit
1import { describe, expect, it } from "vitest";
2import * as Schema from "effect/Schema";
3
4import { ProviderInstanceId } from "./providerInstance.ts";
5import { DEFAULT_SERVER_SETTINGS, ServerSettings, ServerSettingsPatch } from "./settings.ts";
6
7const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
8const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
9const encodeServerSettings = Schema.encodeSync(ServerSettings);
10
11describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
12 it("defaults to an empty record so legacy configs without the key still decode", () => {
13 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
14 });
15
16 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
17 const decoded = decodeServerSettings({});
18 expect(decoded.providerInstances).toEqual({});
19 // Legacy `providers` struct is still hydrated with its per-driver defaults
20 // so existing call sites keep working through the migration.
21 expect(decoded.providers.codex.enabled).toBe(true);
22 });
23
24 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
25 const decoded = decodeServerSettings({
26 providerInstances: {
27 codex_personal: {
28 driver: "codex",
29 displayName: "Codex (personal)",
30 config: { homePath: "~/.codex_personal" },
31 },
32 codex_work: {
33 driver: "codex",
34 config: { homePath: "~/.codex_work" },
35 },
36 ollama_local: {
37 driver: "ollama",
38 displayName: "Ollama (local)",
39 config: { endpoint: "http://localhost:11434" },
40 },
41 },
42 });
43 const personalId = ProviderInstanceId.make("codex_personal");
44 const workId = ProviderInstanceId.make("codex_work");
45 const ollamaId = ProviderInstanceId.make("ollama_local");
46
47 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
48 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
49 // Critical: a config naming a driver this build does not know about
50 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
51 // The runtime handles "driver not installed" — the schema must not.
52 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
53 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
54 endpoint: "http://localhost:11434",
55 });
56 });
57
58 it("rejects instance keys that violate the slug pattern", () => {
59 expect(() =>
60 decodeServerSettings({
61 providerInstances: { "1bad": { driver: "codex" } },
62 }),
63 ).toThrow();
64 });
65});
66
67describe("ServerSettingsPatch.providerInstances", () => {
68 it("treats providerInstances as an optional whole-map replacement", () => {
69 const patch = decodeServerSettingsPatch({});
70 expect(patch.providerInstances).toBeUndefined();
71
72 const replacement = decodeServerSettingsPatch({
73 providerInstances: {
74 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
75 },
76 });
77 expect(replacement.providerInstances).toBeDefined();
78 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
79 "codex",
80 );
81 });
82
83 it("preserves a fork-defined driver entry through patch decoding", () => {
84 const patch = decodeServerSettingsPatch({
85 providerInstances: {
86 ollama_local: {
87 driver: "ollama",
88 config: { endpoint: "http://localhost:11434" },
89 },
90 },
91 });
92 const ollamaId = ProviderInstanceId.make("ollama_local");
93 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
94 });
95});
96
97describe("ServerSettingsPatch string normalization", () => {
98 it("trims string settings while decoding patches", () => {
99 const patch = decodeServerSettingsPatch({
100 addProjectBaseDirectory: " ~/Development ",
101 textGenerationModelSelection: { model: " gpt-5.4-mini " },
102 observability: {
103 otlpTracesUrl: " http://localhost:4318/v1/traces ",
104 },
105 providers: {
106 codex: {
107 binaryPath: " /opt/homebrew/bin/codex ",
108 homePath: " ~/.codex ",
109 },
110 },
111 providerInstances: {
112 codex_personal: {
113 driver: " codex ",
114 displayName: " Codex Personal ",
115 config: { homePath: " ~/.codex-personal " },
116 },
117 },
118 });
119
120 expect(patch.addProjectBaseDirectory).toBe("~/Development");
121 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
122 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
123 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
124 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
125 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
126 "codex",
127 );
128 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
129 "Codex Personal",
130 );
131 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
132 homePath: " ~/.codex-personal ",
133 });
134 });
135
136 it("trims encoded server settings values before validation", () => {
137 const defaultSettings = decodeServerSettings({});
138 const encoded = encodeServerSettings({
139 ...defaultSettings,
140 addProjectBaseDirectory: " ~/Development ",
141 providers: {
142 ...defaultSettings.providers,
143 codex: {
144 ...defaultSettings.providers.codex,
145 binaryPath: " /opt/homebrew/bin/codex ",
146 },
147 },
148 });
149
150 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
151 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
152 });
153});