Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import { ProviderInstanceId } from "./providerInstance.ts";
5import {
6 ClientSettingsSchema,
7 DEFAULT_SERVER_SETTINGS,
8 ServerSettings,
9 ServerSettingsPatch,
10} from "./settings.ts";
11
12const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
13const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
14const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
15const encodeServerSettings = Schema.encodeSync(ServerSettings);
16
17describe("ClientSettings word wrap", () => {
18 it("defaults word wrap on", () => {
19 expect(decodeClientSettings({}).wordWrap).toBe(true);
20 });
21
22 it("ignores obsolete wrapping preferences", () => {
23 const decoded = decodeClientSettings({
24 chatWordWrap: false,
25 diffWordWrap: false,
26 });
27
28 expect(decoded.wordWrap).toBe(true);
29 expect(decoded).not.toHaveProperty("chatWordWrap");
30 expect(decoded).not.toHaveProperty("diffWordWrap");
31 });
32});
33
34describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
35 it("defaults to an empty record so legacy configs without the key still decode", () => {
36 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
37 });
38
39 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
40 const decoded = decodeServerSettings({});
41 expect(decoded.providerInstances).toEqual({});
42 // Legacy `providers` struct is still hydrated with its per-driver defaults
43 // so existing call sites keep working through the migration.
44 expect(decoded.providers.codex.enabled).toBe(true);
45 });
46
47 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
48 const decoded = decodeServerSettings({
49 providerInstances: {
50 codex_personal: {
51 driver: "codex",
52 displayName: "Codex (personal)",
53 config: { homePath: "~/.codex_personal" },
54 },
55 codex_work: {
56 driver: "codex",
57 config: { homePath: "~/.codex_work" },
58 },
59 ollama_local: {
60 driver: "ollama",
61 displayName: "Ollama (local)",
62 config: { endpoint: "http://localhost:11434" },
63 },
64 },
65 });
66 const personalId = ProviderInstanceId.make("codex_personal");
67 const workId = ProviderInstanceId.make("codex_work");
68 const ollamaId = ProviderInstanceId.make("ollama_local");
69
70 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
71 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
72 // Critical: a config naming a driver this build does not know about
73 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
74 // The runtime handles "driver not installed" — the schema must not.
75 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
76 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
77 endpoint: "http://localhost:11434",
78 });
79 });
80
81 it("rejects instance keys that violate the slug pattern", () => {
82 expect(() =>
83 decodeServerSettings({
84 providerInstances: { "1bad": { driver: "codex" } },
85 }),
86 ).toThrow();
87 });
88});
89
90describe("ServerSettings worktree defaults", () => {
91 it("defaults start-from-origin off for legacy configs", () => {
92 expect(decodeServerSettings({}).newWorktreesStartFromOrigin).toBe(false);
93 });
94
95 it("accepts start-from-origin updates", () => {
96 expect(
97 decodeServerSettingsPatch({ newWorktreesStartFromOrigin: true }).newWorktreesStartFromOrigin,
98 ).toBe(true);
99 });
100});
101
102describe("ServerSettings Cursor legacy settings", () => {
103 it("ignores obsolete Cursor CLI settings when reading server settings", () => {
104 const decoded = decodeServerSettings({
105 providers: {
106 cursor: {
107 enabled: true,
108 binaryPath: "cursor-agent",
109 apiEndpoint: "http://127.0.0.1:3774",
110 },
111 },
112 });
113
114 expect(decoded.providers.cursor.enabled).toBe(true);
115 expect(decoded.providers.cursor).not.toHaveProperty("binaryPath");
116 expect(decoded.providers.cursor).not.toHaveProperty("apiEndpoint");
117 });
118
119 it("ignores obsolete Cursor CLI settings in patches", () => {
120 const patch = decodeServerSettingsPatch({
121 providers: {
122 cursor: {
123 enabled: true,
124 binaryPath: "cursor-agent",
125 apiEndpoint: "http://127.0.0.1:3774",
126 },
127 },
128 });
129
130 expect(patch.providers?.cursor?.enabled).toBe(true);
131 expect(patch.providers?.cursor).not.toHaveProperty("binaryPath");
132 expect(patch.providers?.cursor).not.toHaveProperty("apiEndpoint");
133 });
134});
135
136describe("ServerSettingsPatch.providerInstances", () => {
137 it("treats providerInstances as an optional whole-map replacement", () => {
138 const patch = decodeServerSettingsPatch({});
139 expect(patch.providerInstances).toBeUndefined();
140
141 const replacement = decodeServerSettingsPatch({
142 providerInstances: {
143 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
144 },
145 });
146 expect(replacement.providerInstances).toBeDefined();
147 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
148 "codex",
149 );
150 });
151
152 it("preserves a fork-defined driver entry through patch decoding", () => {
153 const patch = decodeServerSettingsPatch({
154 providerInstances: {
155 ollama_local: {
156 driver: "ollama",
157 config: { endpoint: "http://localhost:11434" },
158 },
159 },
160 });
161 const ollamaId = ProviderInstanceId.make("ollama_local");
162 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
163 });
164});
165
166describe("ServerSettingsPatch string normalization", () => {
167 it("trims string settings while decoding patches", () => {
168 const patch = decodeServerSettingsPatch({
169 addProjectBaseDirectory: " ~/Development ",
170 textGenerationModelSelection: { model: " gpt-5.4-mini " },
171 observability: {
172 otlpTracesUrl: " http://localhost:4318/v1/traces ",
173 },
174 providers: {
175 codex: {
176 binaryPath: " /opt/homebrew/bin/codex ",
177 homePath: " ~/.codex ",
178 },
179 },
180 providerInstances: {
181 codex_personal: {
182 driver: " codex ",
183 displayName: " Codex Personal ",
184 config: { homePath: " ~/.codex-personal " },
185 },
186 },
187 });
188
189 expect(patch.addProjectBaseDirectory).toBe("~/Development");
190 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
191 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
192 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
193 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
194 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
195 "codex",
196 );
197 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
198 "Codex Personal",
199 );
200 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
201 homePath: " ~/.codex-personal ",
202 });
203 });
204
205 it("trims encoded server settings values before validation", () => {
206 const defaultSettings = decodeServerSettings({});
207 const encoded = encodeServerSettings({
208 ...defaultSettings,
209 addProjectBaseDirectory: " ~/Development ",
210 providers: {
211 ...defaultSettings.providers,
212 codex: {
213 ...defaultSettings.providers.codex,
214 binaryPath: " /opt/homebrew/bin/codex ",
215 },
216 },
217 });
218
219 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
220 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
221 });
222});