32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
|
|
const asModule = source => `data:text/javascript;base64,${Buffer.from(source).toString('base64')}`;
|
|
const api = asModule(await readFile(new URL('../src/lib/api.js', import.meta.url), 'utf8'));
|
|
const source = (await readFile(new URL('../src/lib/suggestions.js', import.meta.url), 'utf8'))
|
|
.replace("'./api'", JSON.stringify(api));
|
|
|
|
test('suggestions retry failed requests, share the pool, and respect exclusions', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
let calls = 0;
|
|
globalThis.fetch = async url => {
|
|
calls++;
|
|
if (calls === 1) throw new Error('offline');
|
|
assert.equal(url, '/api/suggestions');
|
|
return { ok: true, json: async () => ({ look: ['Bandana', 'Eyepatch'], smell: ['Tar'] }) };
|
|
};
|
|
try {
|
|
const { getSuggestion } = await import(asModule(source));
|
|
await assert.rejects(getSuggestion('look'), /offline/);
|
|
assert.deepEqual(await Promise.all([
|
|
getSuggestion('look', [' bandana ']), getSuggestion('smell')
|
|
]), ['Eyepatch', 'Tar']);
|
|
assert.equal(calls, 2);
|
|
assert.equal(await getSuggestion('unknown'), '');
|
|
assert.ok(['Bandana', 'Eyepatch'].includes(await getSuggestion('look', ['Bandana', 'Eyepatch'])));
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|