Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settings.test.ts

256 lines9.2 KB

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