Improve character suggestions and serve pools from the API

This commit is contained in:
2026-09-04 19:58:35 -07:00
parent 1dac2141e6
commit 0ae1701b82
11 changed files with 255 additions and 1255 deletions
+31
View File
@@ -0,0 +1,31 @@
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;
}
});