59 lines
2.2 KiB
JavaScript
59 lines
2.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import { safeFetch, safeFetchText, getFetchMetrics } from '../apis/utils/fetch.mjs';
|
|
|
|
test('safeFetch reports HTML as degraded JSON response', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get: () => 'text/html' },
|
|
text: async () => '<html>not json</html>',
|
|
});
|
|
try {
|
|
const data = await safeFetch('https://example.test/json', { retries: 0, source: 'unit' });
|
|
assert.match(data.error, /Expected JSON/);
|
|
assert.ok(getFetchMetrics().bySource.unit.requests >= 1);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('safeFetchText returns text and byte count', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
text: async () => 'hello',
|
|
});
|
|
try {
|
|
const data = await safeFetchText('https://example.test/rss', { retries: 0, source: 'rss-unit' });
|
|
assert.equal(data.text, 'hello');
|
|
assert.equal(data.bytes, 5);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('intelligence store defines durable memory and prediction lifecycle tables', () => {
|
|
const store = readFileSync(new URL('../lib/intelligence-store.mjs', import.meta.url), 'utf8');
|
|
assert.match(store, /CREATE TABLE IF NOT EXISTS events/);
|
|
assert.match(store, /stable_id TEXT NOT NULL UNIQUE/);
|
|
assert.match(store, /hypothesis TEXT/);
|
|
assert.match(store, /evidence_json TEXT/);
|
|
assert.match(store, /outcome_state TEXT DEFAULT 'open'/);
|
|
assert.match(store, /evaluatePredictions/);
|
|
assert.match(store, /queryMemory/);
|
|
assert.match(store, /listPredictions/);
|
|
});
|
|
|
|
test('server exposes memory-backed query APIs and dashboard memory action', () => {
|
|
const server = readFileSync(new URL('../server.mjs', import.meta.url), 'utf8');
|
|
const html = readFileSync(new URL('../dashboard/public/jarvis.html', import.meta.url), 'utf8');
|
|
assert.match(server, /\/api\/memory\/search/);
|
|
assert.match(server, /\/api\/memory\/predictions/);
|
|
assert.match(server, /action === 'memory'/);
|
|
assert.match(html, /runTerminalAction\('memory'\)/);
|
|
});
|