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).
This commit is contained in:
2 files changed
+55
-1
No files matched your search
@@ -77,6 +77,19 @@ export function loadRuntimeConfig(): RuntimeConfig {
|
|||||||
return { ...normalized, ...resolveRuntimeEnvOverrides(process.env) };
|
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>): RuntimeConfig {
|
||||||
|
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
||||||
|
const onDisk = normalizeRuntimeConfig(tryLoadRuntimeConfig() ?? {});
|
||||||
|
const next = normalizeRuntimeConfig({ ...onDisk, ...patch });
|
||||||
|
saveRuntimeConfig(next);
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
function tryLoadRuntimeConfig(): Record<string, unknown> | null {
|
function tryLoadRuntimeConfig(): Record<string, unknown> | null {
|
||||||
if (!fs.existsSync(RUNTIME_CONFIG_PATH)) return null;
|
if (!fs.existsSync(RUNTIME_CONFIG_PATH)) return null;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||||
import { normalizeRuntimeConfig, resolveRuntimeEnvOverrides } from '../src/runtime';
|
import { normalizeRuntimeConfig, resolveRuntimeEnvOverrides } from '../src/runtime';
|
||||||
|
|
||||||
describe('normalizeRuntimeConfig', () => {
|
describe('normalizeRuntimeConfig', () => {
|
||||||
@@ -74,3 +74,44 @@ describe('resolveRuntimeEnvOverrides', () => {
|
|||||||
expect(resolveRuntimeEnvOverrides({ SNOWLUMA_WEBUI_TRUST_PROXY: '0' })).toEqual({ trustProxy: '0' });
|
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
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user