feat(onebot): add HTTP event streaming

Refs #593
This commit is contained in:
DrSmoothl committed 2026-11-01 23:15:51 +08:00
1 parent 54038c775a
commit 5f1700fc1b
13 files changed
+866 -73

No files matched your search

@@ -10,44 +10,75 @@ import {
saveOneBotConfig,
} from '../src/config';
describe('HTTP server WebSocket configuration', () => {
let previousCwd: string;
let tempDir: string;
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);
});
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 });
});
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].enableWebSocket).toBe(false);
config.networks.httpServers[0].enableWebSocket = 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);
saveOneBotConfig('10001', config);
const onDisk = JSON.parse(
fs.readFileSync(path.join(tempDir, 'config', 'onebot_10001.json'), 'utf8'),
) as { networks: { httpServers: Array<{ enableWebSocket?: boolean }> } };
expect(onDisk.networks.httpServers[0].enableWebSocket).toBe(true);
expect(loadOneBotConfig('10001').networks.httpServers[0].enableWebSocket).toBe(true);
});
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<string, unknown>).enableWebSocket =
'yes';
expect(() => assertValidOneBotConfig(config)).toThrow(/enableWebSocket must be a boolean/);
it('rejects non-boolean values at public save and restore boundaries', () => {
const config = makeDefaultOneBotConfig();
(config.networks.httpServers[0] as unknown as Record<string, unknown>)[switchName] =
'yes';
expect(() => assertValidOneBotConfig(config)).toThrow(
`${switchName} must be a boolean`,
);
expect(() =>
prepareOneBotConfigForRestore(
{
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: [
{
@@ -55,40 +86,18 @@ describe('HTTP server WebSocket configuration', () => {
host: '127.0.0.1',
port: 3000,
path: '/',
enableWebSocket: 'yes',
[switchName]: 'yes',
},
],
httpClients: [],
wsServers: [],
wsClients: [],
},
},
'per-uin',
),
).toThrow(/enableWebSocket must be a boolean/);
});
}),
'utf8',
);
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: '/',
enableWebSocket: 'yes',
},
],
httpClients: [],
wsServers: [],
wsClients: [],
},
}),
'utf8',
);
expect(() => loadOneBotConfig('10002')).toThrow(/enableWebSocket must be a boolean/);
});
});
expect(() => loadOneBotConfig('10002')).toThrow(`${switchName} must be a boolean`);
});
},
);