import type { Page } from '@playwright/test'; /** * Waits until the player's sunset town is written to IndexedDB. The garden shows a * choice before the write commits, so a test that reloads or closes the tab at once * races the write: it passed and failed on alternate runs before this existed. */ export async function storedTown(page: Page, name: string): Promise { await page.waitForFunction( (expected) => new Promise((resolve) => { const req = indexedDB.open('rosea'); req.onerror = () => resolve(false); req.onsuccess = () => { const db = req.result; if (!db.objectStoreNames.contains('profile')) { db.close(); resolve(false); return; } const get = db.transaction('profile').objectStore('profile').get('sunsetPlace'); get.onsuccess = () => { db.close(); const value = get.result as { value?: { town?: { name?: string } } } | undefined; resolve(value?.value?.town?.name === expected); }; get.onerror = () => { db.close(); resolve(false); }; }; }), name, { timeout: 5_000 } ); } /** Waits until the care log holds at least `count` events, for the same reason as above. */ export async function storedCare(page: Page, count: number): Promise { await page.waitForFunction( (expected) => new Promise((resolve) => { const req = indexedDB.open('rosea'); req.onerror = () => resolve(false); req.onsuccess = () => { const db = req.result; if (!db.objectStoreNames.contains('events')) { db.close(); resolve(false); return; } const n = db.transaction('events').objectStore('events').count(); n.onsuccess = () => { db.close(); resolve(n.result >= expected); }; n.onerror = () => { db.close(); resolve(false); }; }; }), count, { timeout: 5_000 } ); }