// 사이트 화면: 처음 보는 유저 아이디가 오면 유저 목록을 다시 받는 규칙 (site/web/src/lib/unknown-users.ts) import { test } from 'node:test'; import assert from 'node:assert/strict'; import { makeUnknownUserCheck } from '../../web/src/lib/unknown-users.ts'; const tick = () => new Promise((r) => setTimeout(r, 0)); test('모르는 유저: 받는 중에는 또 부르지 않고, 실패하면 다음 이벤트에서 다시 받는다', async () => { const known = new Set(['a']); let calls = 0; let fail = true; let release!: () => void; const check = makeUnknownUserCheck(() => { calls++; return new Promise((ok, no) => { release = () => (fail ? no(new Error('500')) : (known.add('late'), ok())); }); }); const has = (id: string) => known.has(id); check(['a'], has); assert.equal(calls, 0, '아는 사람은 부르지 않음'); check(['late'], has); check(['late'], has); assert.equal(calls, 1, '받는 중에는 한 번만'); release(); // 500 await tick(); check(['late'], has); assert.equal(calls, 2, '실패 뒤 다음 발화에서 다시 받음'); fail = false; release(); await tick(); assert.ok(known.has('late')); check(['late'], has); assert.equal(calls, 2, '받은 뒤에는 부르지 않음'); }); test('모르는 유저: 받았는데도 목록에 없는 아이디는 발화마다 다시 부르지 않는다', async () => { let calls = 0; const check = makeUnknownUserCheck(async () => { calls++; }); const has = () => false; check(['ghost'], has); await tick(); check(['ghost'], has); check(['ghost', ''], has); assert.equal(calls, 1); check(['other'], has); assert.equal(calls, 2, '다른 새 아이디는 다시 받음'); });