import assert from 'node:assert/strict'; import { test } from 'node:test'; import type { Page } from 'playwright'; import { get, park, parkedCount, release, setReturnTo } from '../src/challenge/registry.ts'; /** Only the two methods the registry touches. */ function stubPage(): Page & { closed: boolean } { const page = { closed: false, close: async () => { page.closed = true; }, evaluate: async () => true, }; return page as unknown as Page & { closed: boolean }; } async function parkOne(page = stubPage()): Promise { return park({ page, platform: 'tiktok', platformLabel: 'TikTok', originalUrl: 'https://t/1' }); } test('a parked challenge round-trips, and its id is not guessable', async () => { const id = await parkOne(); assert.match(id, /^[A-Za-z0-9_-]{12}$/); assert.equal(get(id)?.originalUrl, 'https://t/1'); await release(id); }); test('releasing closes the tab and hands back where to go next', async () => { const page = stubPage(); const id = await parkOne(page); setReturnTo(id, '/tiktok/@a/video/1'); assert.equal(await release(id), '/tiktok/@a/video/1'); assert.equal(page.closed, true, 'the browser tab must not be left open'); assert.equal(get(id), undefined); assert.equal(await release(id), undefined, 'releasing twice is harmless'); }); test('parking more than the cap closes the oldest rather than hoarding tabs', async () => { const before = parkedCount(); const first = stubPage(); const ids = [await parkOne(first)]; for (let i = 0; i < 3; i += 1) ids.push(await parkOne()); assert.ok(parkedCount() <= 3, `expected at most 3 parked, got ${parkedCount()}`); assert.equal(first.closed, true, 'the evicted tab must be closed'); assert.equal(get(ids[0] as string), undefined); for (const id of ids) await release(id); assert.equal(parkedCount(), before); }); test('an unknown id is undefined rather than a throw', () => { assert.equal(get('nope'), undefined); });