Open History to see the last commit
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("trims provider path settings when updates are applied", () =>
146 Effect.gen(function* () {
147 const serverSettings = yield* ServerSettingsService;
148
149 const next = yield* serverSettings.updateSettings({
150 providers: {
151 codex: {
152 binaryPath: " /opt/homebrew/bin/codex ",
153 homePath: " ",
154 },
155 claudeAgent: {
156 binaryPath: " /opt/homebrew/bin/claude ",
157 },
158 },
159 });
160
161 assert.deepEqual(next.providers.codex, {
162 enabled: true,
163 binaryPath: "/opt/homebrew/bin/codex",
164 homePath: "",
165 customModels: [],
166 });
167 assert.deepEqual(next.providers.claudeAgent, {
168 enabled: true,
169 binaryPath: "/opt/homebrew/bin/claude",
170 customModels: [],
171 launchArgs: "",
172 });
173 }).pipe(Effect.provide(makeServerSettingsLayer())),
174 );
175
176 it.effect("trims observability settings when updates are applied", () =>
177 Effect.gen(function* () {
178 const serverSettings = yield* ServerSettingsService;
179
180 const next = yield* serverSettings.updateSettings({
181 addProjectBaseDirectory: " ~/Development ",
182 observability: {
183 otlpTracesUrl: " http://localhost:4318/v1/traces ",
184 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
185 },
186 });
187
188 assert.equal(next.addProjectBaseDirectory, "~/Development");
189 assert.deepEqual(next.observability, {
190 otlpTracesUrl: "http://localhost:4318/v1/traces",
191 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
192 });
193 }).pipe(Effect.provide(makeServerSettingsLayer())),
194 );
195
196 it.effect("defaults blank binary paths to provider executables", () =>
197 Effect.gen(function* () {
198 const serverSettings = yield* ServerSettingsService;
199
200 const next = yield* serverSettings.updateSettings({
201 providers: {
202 codex: {
203 binaryPath: " ",
204 },
205 claudeAgent: {
206 binaryPath: "",
207 },
208 },
209 });
210
211 assert.equal(next.providers.codex.binaryPath, "codex");
212 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
213 }).pipe(Effect.provide(makeServerSettingsLayer())),
214 );
215
216 it.effect("writes only non-default server settings to disk", () =>
217 Effect.gen(function* () {
218 const serverSettings = yield* ServerSettingsService;
219 const serverConfig = yield* ServerConfig;
220 const fileSystem = yield* FileSystem.FileSystem;
221 const next = yield* serverSettings.updateSettings({
222 addProjectBaseDirectory: "~/Development",
223 observability: {
224 otlpTracesUrl: "http://localhost:4318/v1/traces",
225 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
226 },
227 providers: {
228 codex: {
229 binaryPath: "/opt/homebrew/bin/codex",
230 },
231 },
232 });
233
234 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
235
236 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
237 assert.deepEqual(JSON.parse(raw), {
238 addProjectBaseDirectory: "~/Development",
239 observability: {
240 otlpTracesUrl: "http://localhost:4318/v1/traces",
241 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
242 },
243 providers: {
244 codex: {
245 binaryPath: "/opt/homebrew/bin/codex",
246 },
247 },
248 });
249 }).pipe(Effect.provide(makeServerSettingsLayer())),
250 );
251});