From 9a455b30c60c3383a6c0c15fdfd56522d468d313 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 18 Jun 2026 14:28:31 +0800 Subject: [PATCH] feat(common): updateRuntimeConfig(patch) for the WebUI settings panel Persist a partial RuntimeConfig update, merging onto the on-disk config (not the env-merged view) so an env override is never baked into runtime.json. Tested: patch merge + the env-not-persisted invariant (chdir to a temp cwd). --- packages/common/src/runtime.ts | 13 ++++++++ packages/common/tests/runtime.test.ts | 43 ++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/common/src/runtime.ts b/packages/common/src/runtime.ts index ecde226e..18cbd1b5 100644 --- a/packages/common/src/runtime.ts +++ b/packages/common/src/runtime.ts @@ -77,6 +77,19 @@ export function loadRuntimeConfig(): RuntimeConfig { return { ...normalized, ...resolveRuntimeEnvOverrides(process.env) }; } +/** + * Persist a partial update. Merges onto the ON-DISK config (not the env-merged + * runtime view) so an env override (e.g. SNOWLUMA_WEBUI_PORT) is never baked + * into runtime.json. Returns the new persisted config (without env overrides). + */ +export function updateRuntimeConfig(patch: Partial): RuntimeConfig { + fs.mkdirSync(CONFIG_DIR, { recursive: true }); + const onDisk = normalizeRuntimeConfig(tryLoadRuntimeConfig() ?? {}); + const next = normalizeRuntimeConfig({ ...onDisk, ...patch }); + saveRuntimeConfig(next); + return next; +} + function tryLoadRuntimeConfig(): Record | null { if (!fs.existsSync(RUNTIME_CONFIG_PATH)) return null; try { diff --git a/packages/common/tests/runtime.test.ts b/packages/common/tests/runtime.test.ts index 32221cc1..8814c34f 100644 --- a/packages/common/tests/runtime.test.ts +++ b/packages/common/tests/runtime.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { normalizeRuntimeConfig, resolveRuntimeEnvOverrides } from '../src/runtime'; describe('normalizeRuntimeConfig', () => { @@ -74,3 +74,44 @@ describe('resolveRuntimeEnvOverrides', () => { expect(resolveRuntimeEnvOverrides({ SNOWLUMA_WEBUI_TRUST_PROXY: '0' })).toEqual({ trustProxy: '0' }); }); }); + +describe('updateRuntimeConfig (fs)', () => { + let prevCwd: string; + let dir: string; + let prevPortEnv: string | undefined; + beforeEach(async () => { + const fs = await import('fs'); const os = await import('os'); const path = await import('path'); + dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sl-rt-')); + prevCwd = process.cwd(); + process.chdir(dir); + prevPortEnv = process.env.SNOWLUMA_WEBUI_PORT; + delete process.env.SNOWLUMA_WEBUI_PORT; + }); + afterEach(async () => { + const fs = await import('fs'); + process.chdir(prevCwd); + if (prevPortEnv === undefined) delete process.env.SNOWLUMA_WEBUI_PORT; + else process.env.SNOWLUMA_WEBUI_PORT = prevPortEnv; + fs.rmSync(dir, { recursive: true, force: true }); + }); + + it('merges a patch over on-disk values and persists normalized', async () => { + const { updateRuntimeConfig } = await import('../src/runtime'); + const fs = await import('fs'); const path = await import('path'); + const out = updateRuntimeConfig({ webuiHost: '127.0.0.1', webuiTls: { enabled: true } }); + expect(out.webuiHost).toBe('127.0.0.1'); + expect(out.webuiTls).toEqual({ enabled: true }); + const onDisk = JSON.parse(fs.readFileSync(path.join('config', 'runtime.json'), 'utf8')); + expect(onDisk.webuiHost).toBe('127.0.0.1'); + expect(onDisk.webuiTls).toEqual({ enabled: true }); + }); + + it('does NOT bake an env-overridden port into the persisted file', async () => { + const { updateRuntimeConfig } = await import('../src/runtime'); + const fs = await import('fs'); const path = await import('path'); + process.env.SNOWLUMA_WEBUI_PORT = '9999'; + updateRuntimeConfig({ webuiHost: '127.0.0.1' }); + const onDisk = JSON.parse(fs.readFileSync(path.join('config', 'runtime.json'), 'utf8')); + expect(onDisk.webuiPort).toBe(5099); // default on-disk, NOT the env 9999 + }); +});