import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { assertValidOneBotConfig, loadOneBotConfig, makeDefaultOneBotConfig, prepareOneBotConfigForRestore, saveOneBotConfig, } from '../src/config'; describe.each(['enableWebSocket', 'enableSse'] as const)( 'HTTP server %s configuration', (switchName) => { let previousCwd: string; let tempDir: string; beforeEach(() => { previousCwd = process.cwd(); tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-http-ws-config-')); process.chdir(tempDir); }); afterEach(() => { process.chdir(previousCwd); fs.rmSync(tempDir, { recursive: true, force: true }); }); it('persists the opt-in switch through save and load', () => { const config = makeDefaultOneBotConfig(); expect(config.networks.httpServers[0][switchName]).toBe(false); config.networks.httpServers[0][switchName] = true; saveOneBotConfig('10001', config); const onDisk = JSON.parse( fs.readFileSync(path.join(tempDir, 'config', 'onebot_10001.json'), 'utf8'), ) as { networks: { httpServers: Array<{ enableWebSocket?: boolean; enableSse?: boolean }>; }; }; expect(onDisk.networks.httpServers[0][switchName]).toBe(true); expect(loadOneBotConfig('10001').networks.httpServers[0][switchName]).toBe(true); }); it('rejects non-boolean values at public save and restore boundaries', () => { const config = makeDefaultOneBotConfig(); (config.networks.httpServers[0] as unknown as Record)[switchName] = 'yes'; expect(() => assertValidOneBotConfig(config)).toThrow( `${switchName} must be a boolean`, ); expect(() => prepareOneBotConfigForRestore( { networks: { httpServers: [ { name: 'http', host: '127.0.0.1', port: 3000, path: '/', [switchName]: 'yes', }, ], }, }, 'per-uin', ), ).toThrow(`${switchName} must be a boolean`); }); it('fails fast when a hand-written config contains a non-boolean switch', () => { fs.mkdirSync(path.join(tempDir, 'config'), { recursive: true }); fs.writeFileSync( path.join(tempDir, 'config', 'onebot_10002.json'), JSON.stringify({ mode: 'snapshot', networks: { httpServers: [ { name: 'http', host: '127.0.0.1', port: 3000, path: '/', [switchName]: 'yes', }, ], httpClients: [], wsServers: [], wsClients: [], }, }), 'utf8', ); expect(() => loadOneBotConfig('10002')).toThrow(`${switchName} must be a boolean`); }); }, );