import * as NodeServices from "@effect/platform-node/NodeServices"; import { DEFAULT_SERVER_SETTINGS, ServerSettingsPatch } from "@t3tools/contracts"; import { assert, it } from "@effect/vitest"; import { Effect, Layer, Schema } from "effect"; import { ServerConfig } from "./config"; import { ServerSettingsLive, ServerSettingsService } from "./serverSettings"; const makeServerSettingsLayer = () => ServerSettingsLive.pipe( Layer.provideMerge( Layer.fresh( ServerConfig.layerTest(process.cwd(), { prefix: "t3code-server-settings-test-", }), ), ), ); it.layer(NodeServices.layer)("server settings", (it) => { it.effect("decodes nested settings patches", () => Effect.sync(() => { const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch); assert.deepEqual(decodePatch({ providers: { codex: { binaryPath: "/tmp/codex" } } }), { providers: { codex: { binaryPath: "/tmp/codex" } }, }); assert.deepEqual( decodePatch({ textGenerationModelSelection: { options: { fastMode: false, }, }, }), { textGenerationModelSelection: { options: { fastMode: false, }, }, }, ); }), ); it.effect("deep merges nested settings updates without dropping siblings", () => Effect.gen(function* () { const serverSettings = yield* ServerSettingsService; yield* serverSettings.updateSettings({ providers: { codex: { binaryPath: "/usr/local/bin/codex", homePath: "/Users/julius/.codex", }, claudeAgent: { binaryPath: "/usr/local/bin/claude", customModels: ["claude-custom"], }, }, textGenerationModelSelection: { provider: "codex", model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model, options: { reasoningEffort: "high", fastMode: true, }, }, }); const next = yield* serverSettings.updateSettings({ providers: { codex: { binaryPath: "/opt/homebrew/bin/codex", }, }, textGenerationModelSelection: { options: { fastMode: false, }, }, }); assert.deepEqual(next.providers.codex, { binaryPath: "/opt/homebrew/bin/codex", homePath: "/Users/julius/.codex", customModels: [], }); assert.deepEqual(next.providers.claudeAgent, { binaryPath: "/usr/local/bin/claude", customModels: ["claude-custom"], }); assert.deepEqual(next.textGenerationModelSelection, { provider: "codex", model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model, options: { reasoningEffort: "high", fastMode: false, }, }); }).pipe(Effect.provide(makeServerSettingsLayer())), ); });