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

serverSettings.test.ts

280 lines8.6 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { DEFAULT_SERVER_SETTINGS, ServerSettingsPatch } from "@t3tools/contracts";
3import { assert, it } from "@effect/vitest";
4import { Effect, FileSystem, Layer, Schema } from "effect";
5import { ServerConfig } from "./config";
6import { ServerSettingsLive, ServerSettingsService } from "./serverSettings";
7​
8const makeServerSettingsLayer = () =>
9 ServerSettingsLive.pipe(
10 Layer.provideMerge(
11 Layer.fresh(
12 ServerConfig.layerTest(process.cwd(), {
13 prefix: "t3code-server-settings-test-",
14 }),
15 ),
16 ),
17 );
18​
19it.layer(NodeServices.layer)("server settings", (it) => {
20 it.effect("decodes nested settings patches", () =>
21 Effect.sync(() => {
22 const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch);
23​
24 assert.deepEqual(decodePatch({ providers: { codex: { binaryPath: "/tmp/codex" } } }), {
25 providers: { codex: { binaryPath: "/tmp/codex" } },
26 });
27​
28 assert.deepEqual(
29 decodePatch({
30 textGenerationModelSelection: {
31 options: {
32 fastMode: false,
33 },
34 },
35 }),
36 {
37 textGenerationModelSelection: {
38 options: {
39 fastMode: false,
40 },
41 },
42 },
43 );
44 }),
45 );
46​
47 it.effect("deep merges nested settings updates without dropping siblings", () =>
48 Effect.gen(function* () {
49 const serverSettings = yield* ServerSettingsService;
50​
51 yield* serverSettings.updateSettings({
52 providers: {
53 codex: {
54 binaryPath: "/usr/local/bin/codex",
55 homePath: "/Users/julius/.codex",
56 },
57 claudeAgent: {
58 binaryPath: "/usr/local/bin/claude",
59 customModels: ["claude-custom"],
60 },
61 },
62 textGenerationModelSelection: {
63 provider: "codex",
64 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
65 options: {
66 reasoningEffort: "high",
67 fastMode: true,
68 },
69 },
70 });
71​
72 const next = yield* serverSettings.updateSettings({
73 providers: {
74 codex: {
75 binaryPath: "/opt/homebrew/bin/codex",
76 },
77 },
78 textGenerationModelSelection: {
79 options: {
80 fastMode: false,
81 },
82 },
83 });
84​
85 assert.deepEqual(next.providers.codex, {
86 enabled: true,
87 binaryPath: "/opt/homebrew/bin/codex",
88 homePath: "/Users/julius/.codex",
89 customModels: [],
90 });
91 assert.deepEqual(next.providers.claudeAgent, {
92 enabled: true,
93 binaryPath: "/usr/local/bin/claude",
94 customModels: ["claude-custom"],
95 launchArgs: "",
96 });
97 assert.deepEqual(next.textGenerationModelSelection, {
98 provider: "codex",
99 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
100 options: {
101 reasoningEffort: "high",
102 fastMode: false,
103 },
104 });
105 }).pipe(Effect.provide(makeServerSettingsLayer())),
106 );
107​
108 it.effect("preserves model when switching providers via textGenerationModelSelection", () =>
109 Effect.gen(function* () {
110 const serverSettings = yield* ServerSettingsService;
111​
112 // Start with Claude text generation selection
113 yield* serverSettings.updateSettings({
114 textGenerationModelSelection: {
115 provider: "claudeAgent",
116 model: "claude-sonnet-4-6",
117 options: {
118 effort: "high",
119 },
120 },
121 });
122​
123 // Switch to Codex — the stale Claude "effort" in options must not
124 // cause the update to lose the selected model.
125 const next = yield* serverSettings.updateSettings({
126 textGenerationModelSelection: {
127 provider: "codex",
128 model: "gpt-5.4",
129 options: {
130 reasoningEffort: "high",
131 },
132 },
133 });
134​
135 assert.deepEqual(next.textGenerationModelSelection, {
136 provider: "codex",
137 model: "gpt-5.4",
138 options: {
139 reasoningEffort: "high",
140 },
141 });
142 }).pipe(Effect.provide(makeServerSettingsLayer())),
143 );
144​
145 it.effect("drops stale text generation options when resetting model selection", () =>
146 Effect.gen(function* () {
147 const serverSettings = yield* ServerSettingsService;
148​
149 yield* serverSettings.updateSettings({
150 textGenerationModelSelection: {
151 provider: "codex",
152 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
153 options: {
154 reasoningEffort: "high",
155 fastMode: true,
156 },
157 },
158 });
159​
160 const next = yield* serverSettings.updateSettings({
161 textGenerationModelSelection: {
162 provider: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.provider,
163 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
164 },
165 });
166​
167 assert.deepEqual(next.textGenerationModelSelection, {
168 provider: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.provider,
169 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
170 });
171 }).pipe(Effect.provide(makeServerSettingsLayer())),
172 );
173​
174 it.effect("trims provider path settings when updates are applied", () =>
175 Effect.gen(function* () {
176 const serverSettings = yield* ServerSettingsService;
177​
178 const next = yield* serverSettings.updateSettings({
179 providers: {
180 codex: {
181 binaryPath: " /opt/homebrew/bin/codex ",
182 homePath: " ",
183 },
184 claudeAgent: {
185 binaryPath: " /opt/homebrew/bin/claude ",
186 },
187 },
188 });
189​
190 assert.deepEqual(next.providers.codex, {
191 enabled: true,
192 binaryPath: "/opt/homebrew/bin/codex",
193 homePath: "",
194 customModels: [],
195 });
196 assert.deepEqual(next.providers.claudeAgent, {
197 enabled: true,
198 binaryPath: "/opt/homebrew/bin/claude",
199 customModels: [],
200 launchArgs: "",
201 });
202 }).pipe(Effect.provide(makeServerSettingsLayer())),
203 );
204​
205 it.effect("trims observability settings when updates are applied", () =>
206 Effect.gen(function* () {
207 const serverSettings = yield* ServerSettingsService;
208​
209 const next = yield* serverSettings.updateSettings({
210 addProjectBaseDirectory: " ~/Development ",
211 observability: {
212 otlpTracesUrl: " http://localhost:4318/v1/traces ",
213 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
214 },
215 });
216​
217 assert.equal(next.addProjectBaseDirectory, "~/Development");
218 assert.deepEqual(next.observability, {
219 otlpTracesUrl: "http://localhost:4318/v1/traces",
220 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
221 });
222 }).pipe(Effect.provide(makeServerSettingsLayer())),
223 );
224​
225 it.effect("defaults blank binary paths to provider executables", () =>
226 Effect.gen(function* () {
227 const serverSettings = yield* ServerSettingsService;
228​
229 const next = yield* serverSettings.updateSettings({
230 providers: {
231 codex: {
232 binaryPath: " ",
233 },
234 claudeAgent: {
235 binaryPath: "",
236 },
237 },
238 });
239​
240 assert.equal(next.providers.codex.binaryPath, "codex");
241 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
242 }).pipe(Effect.provide(makeServerSettingsLayer())),
243 );
244​
245 it.effect("writes only non-default server settings to disk", () =>
246 Effect.gen(function* () {
247 const serverSettings = yield* ServerSettingsService;
248 const serverConfig = yield* ServerConfig;
249 const fileSystem = yield* FileSystem.FileSystem;
250 const next = yield* serverSettings.updateSettings({
251 addProjectBaseDirectory: "~/Development",
252 observability: {
253 otlpTracesUrl: "http://localhost:4318/v1/traces",
254 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
255 },
256 providers: {
257 codex: {
258 binaryPath: "/opt/homebrew/bin/codex",
259 },
260 },
261 });
262​
263 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
264​
265 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
266 assert.deepEqual(JSON.parse(raw), {
267 addProjectBaseDirectory: "~/Development",
268 observability: {
269 otlpTracesUrl: "http://localhost:4318/v1/traces",
270 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
271 },
272 providers: {
273 codex: {
274 binaryPath: "/opt/homebrew/bin/codex",
275 },
276 },
277 });
278 }).pipe(Effect.provide(makeServerSettingsLayer())),
279 );
280});