openclaw-knowledge-engine/test/http-client.test.ts
Claudia 8964d93c60 feat: knowledge-engine v0.1.0 — all Cerberus findings fixed
- 83/83 tests passing (was 32/45)
- New: src/http-client.ts (shared HTTP/HTTPS client, fixes C2+H1)
- Fixed: proper_noun regex exclusions (C6)
- Fixed: shutdown hooks registered in hooks.ts (C3)
- Fixed: all timers use .unref() (H6)
- Fixed: resolveConfig split into smaller functions (C4)
- Fixed: extract() split with processMatch helper (C5)
- Fixed: FactStore.addFact isLoaded guard (H3)
- Fixed: validateConfig split (H2)
- Fixed: type-safe config merge, removed as any (H4)
- Added: http-client tests, expanded coverage (H5)
- Fixed: LLM batch await (S1), fresh RegExp per call (S2)
- 1530 LOC source, 1298 LOC tests, strict TypeScript
2026-02-17 16:10:13 +01:00

68 lines
1.9 KiB
TypeScript

// test/http-client.test.ts
import { describe, it, afterEach } from 'node:test';
import * as assert from 'node:assert';
import * as http from 'node:http';
import { httpPost } from '../src/http-client.js';
describe('httpPost', () => {
let server: http.Server | null = null;
afterEach((_, done) => {
if (server) {
server.close(() => done());
server = null;
} else {
done();
}
});
it('should make a successful POST request', async () => {
let receivedBody = '';
server = http.createServer((req, res) => {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
receivedBody = body;
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end('{"ok":true}');
});
});
await new Promise<void>(resolve => server!.listen(0, resolve));
const port = (server.address() as { port: number }).port;
const result = await httpPost(`http://localhost:${port}/test`, { key: 'value' });
assert.strictEqual(result, '{"ok":true}');
assert.strictEqual(JSON.parse(receivedBody).key, 'value');
});
it('should reject on non-2xx status codes', async () => {
server = http.createServer((_req, res) => {
res.writeHead(500);
res.end('Internal Server Error');
});
await new Promise<void>(resolve => server!.listen(0, resolve));
const port = (server.address() as { port: number }).port;
await assert.rejects(
() => httpPost(`http://localhost:${port}/test`, {}),
(err: Error) => {
assert.ok(err.message.includes('500'));
return true;
}
);
});
it('should reject on connection error', async () => {
// Port that nothing is listening on
await assert.rejects(
() => httpPost('http://localhost:19999/test', {}),
(err: Error) => {
assert.ok(err.message.includes('request error'));
return true;
}
);
});
});