Files
memind/news-engine-client.test.mjs
T
2026-09-22 10:19:54 +08:00

67 lines
2.2 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
formatRankedNewsCandidates,
rankNewsWithEngine,
resolveNewsEngineEndpoint,
} from './news-engine-client.mjs';
test('news engine client is disabled without an endpoint', async () => {
assert.equal(resolveNewsEngineEndpoint({}), '');
const ranking = await rankNewsWithEngine([], {
env: {},
fetchImpl: async () => { throw new Error('must not fetch'); },
});
assert.deepEqual(ranking.selected, []);
});
test('news engine client sends groups and authentication to the independent service', async () => {
const calls = [];
const ranking = await rankNewsWithEngine([{ id: 'tech', results: [] }], {
now: 123,
env: {
MEMIND_NEWS_ENGINE_URL: 'http://127.0.0.1:8092/',
MEMIND_NEWS_ENGINE_API_TOKEN: 'service-token',
MEMIND_NEWS_ENGINE_DAILY_LIMIT: '12',
},
fetchImpl: async (url, init) => {
calls.push({ url, init, body: JSON.parse(init.body) });
return {
ok: true,
async json() {
return {
candidateCount: 1,
eventCount: 1,
events: [],
selected: [{
rank: 1,
category: 'ai_tech',
canonicalTitle: '模型发布',
finalScore: 8.8,
sourceCount: 2,
mustInclude: true,
primaryArticle: { sourceDomain: 'example.com', url: 'https://example.com/a' },
}],
};
},
};
},
});
assert.equal(calls[0].url, 'http://127.0.0.1:8092/v1/rank');
assert.equal(calls[0].init.headers.authorization, 'Bearer service-token');
assert.equal(calls[0].body.options.limit, 12);
assert.equal(ranking.selected[0].mustInclude, true);
assert.match(formatRankedNewsCandidates(ranking), /模型发布/);
});
test('news engine client fails open when the service is unavailable', async () => {
const warnings = [];
const ranking = await rankNewsWithEngine([], {
env: { MEMIND_NEWS_ENGINE_URL: 'http://127.0.0.1:8092' },
fetchImpl: async () => { throw new Error('offline'); },
logger: { warn: (...args) => warnings.push(args) },
});
assert.deepEqual(ranking.selected, []);
assert.match(String(warnings[0]), /offline/);
});