Files
Rosea/e2e/stored.ts
T

37 lines
1.2 KiB
TypeScript

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<void> {
await page.waitForFunction(
(expected) =>
new Promise<boolean>((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 }
);
}