mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-09-18 19:42:04 +00:00
fix(pokedex): open detail cards without a per-card round trip
Since the grid transport change, opening a Pokémon fetched /api/pokedexes/[id]/entries/[entryId]. That request re-scanned the whole dex to authorise one entry, on top of an auth call and an ownership query, and production runs its functions in a different region from the database — so each card took seconds to fill in. The page's 60-second reconcile also wiped the detail cache, making cards refetch about once a minute. Details are catalog text plus personal notes, so the dex is now read once per account/dex in the background at the interactive/idle boundary and fills that cache; a card opens straight from memory. That read reuses the existing combined-data endpoint, which gains an opt-out for its count query since a caller taking every row already knows the total. The per-entry endpoint stays as the fallback for a card opened before the background read lands, and now checks membership for the single entry instead of materialising the dex. Catch status still comes from the live grid row and any queued write, so a cached detail cannot show stale progress. The packed grid transport and virtualised rendering are unchanged.
This commit is contained in:
@@ -5,10 +5,14 @@ import { test, expect } from '../fixtures';
|
||||
const { When, Then } = createBdd(test);
|
||||
|
||||
// Per-page scratch values; scenarios run one at a time (workers: 1).
|
||||
const timings = new WeakMap<
|
||||
Page,
|
||||
{ entriesMs?: number; switchMs: number[]; entryRequests: number }
|
||||
>();
|
||||
type Timing = {
|
||||
entriesMs?: number;
|
||||
switchMs: number[];
|
||||
gridRequests: number;
|
||||
detailRequests: number;
|
||||
detailsPrimed: boolean;
|
||||
};
|
||||
const timings = new WeakMap<Page, Timing>();
|
||||
|
||||
function entriesVisible(page: Page) {
|
||||
return expect(page.getByRole('button', { name: /^View details for / }).first()).toBeVisible({
|
||||
@@ -18,21 +22,61 @@ function entriesVisible(page: Page) {
|
||||
|
||||
When('I load the Pokédex page directly', async ({ page, state }) => {
|
||||
if (!state.pokedexId) throw new Error('A Pokédex must exist before it can be opened');
|
||||
const record = { switchMs: [], entryRequests: 0 };
|
||||
const record: Timing = {
|
||||
switchMs: [],
|
||||
gridRequests: 0,
|
||||
detailRequests: 0,
|
||||
detailsPrimed: false
|
||||
};
|
||||
timings.set(page, record);
|
||||
// Only requests made while the page first loads matter; the page's 60s reconciliation refetch
|
||||
// can't fire within this window.
|
||||
const countEntryRequests = (request: { url(): string }) => {
|
||||
if (/\/api\/pokedexes\/[^/]+\/(?:grid|combined-data)/.test(request.url()))
|
||||
record.entryRequests++;
|
||||
const countGridRequests = (request: { url(): string }) => {
|
||||
if (/\/api\/pokedexes\/[^/]+\/grid/.test(request.url())) record.gridRequests++;
|
||||
};
|
||||
page.on('request', countEntryRequests);
|
||||
// The background detail read is deliberately allowed: it is what keeps cards from fetching
|
||||
// themselves one at a time. Remember when it lands so a later step can open a card.
|
||||
const notePrimedDetails = (response: { url(): string; status(): number }) => {
|
||||
if (/\/api\/pokedexes\/[^/]+\/combined-data/.test(response.url()) && response.status() === 200)
|
||||
record.detailsPrimed = true;
|
||||
};
|
||||
page.on('request', countGridRequests);
|
||||
page.on('response', notePrimedDetails);
|
||||
|
||||
const started = Date.now();
|
||||
await page.goto(`/pokedex/${state.pokedexId}`);
|
||||
await entriesVisible(page);
|
||||
(record as { entriesMs?: number }).entriesMs = Date.now() - started;
|
||||
page.off('request', countEntryRequests);
|
||||
record.entriesMs = Date.now() - started;
|
||||
page.off('request', countGridRequests);
|
||||
});
|
||||
|
||||
When('the Pokédex has finished loading its details in the background', async ({ page }) => {
|
||||
const record = timings.get(page);
|
||||
expect(record, 'the Pokédex page was never loaded').toBeDefined();
|
||||
await expect.poll(() => record!.detailsPrimed, { timeout: 30_000 }).toBe(true);
|
||||
});
|
||||
|
||||
When('I open the first Pokémon', async ({ page }) => {
|
||||
const record = timings.get(page);
|
||||
expect(record, 'the Pokédex page was never loaded').toBeDefined();
|
||||
const countDetailRequests = (request: { url(): string }) => {
|
||||
if (/\/api\/pokedexes\/[^/]+\/entries\//.test(request.url())) record!.detailRequests++;
|
||||
};
|
||||
page.on('request', countDetailRequests);
|
||||
await page
|
||||
.getByRole('button', { name: /^View details for / })
|
||||
.first()
|
||||
.click();
|
||||
// "Where to catch" comes from the detail row, so it only renders once details are present.
|
||||
await expect(page.getByText('Where to catch:')).toBeVisible({ timeout: 10_000 });
|
||||
// Give any stray per-entry request time to be made before asserting none was.
|
||||
await page.waitForTimeout(500);
|
||||
page.off('request', countDetailRequests);
|
||||
});
|
||||
|
||||
Then('its details were already in the browser', async ({ page }) => {
|
||||
await expect(page.getByRole('status')).toHaveCount(0);
|
||||
expect(timings.get(page)?.detailRequests).toBe(0);
|
||||
});
|
||||
|
||||
Then('its entries appear within {int} seconds', async ({ page }, seconds: number) => {
|
||||
@@ -44,12 +88,17 @@ Then('its entries appear within {int} seconds', async ({ page }, seconds: number
|
||||
Then('the browser did not request the grid separately', async ({ page }) => {
|
||||
// The server load includes the compact grid with the HTML, so the page must not make
|
||||
// the old hydrate-then-fetch round trip.
|
||||
expect(timings.get(page)?.entryRequests).toBe(0);
|
||||
expect(timings.get(page)?.gridRequests).toBe(0);
|
||||
});
|
||||
|
||||
When('I switch between my Pokédex list and the Pokédex', async ({ page, state }) => {
|
||||
if (!state.pokedexName) throw new Error('A Pokédex must exist before switching to it');
|
||||
const record = { switchMs: [] as number[], entryRequests: 0 };
|
||||
const record: Timing = {
|
||||
switchMs: [],
|
||||
gridRequests: 0,
|
||||
detailRequests: 0,
|
||||
detailsPrimed: false
|
||||
};
|
||||
timings.set(page, record);
|
||||
await page.goto('/my-pokedexes');
|
||||
const card = page.locator('.card').filter({ hasText: state.pokedexName }).first();
|
||||
|
||||
Reference in New Issue
Block a user