mirror of
https://github.com/tiennm99/vngeoguessr.git
synced 2026-10-20 00:15:57 +00:00
A round now carries two regions. The one the player picked is public and comes back in every response; the one the panorama actually sits in is a secret, and it is what the leaderboard fans out from. Revealing the second before the guess would collapse a country-wide round to a single district, so it joins the exact coordinates on the never-serialized list -- including in the session lookup handler, which echoes fields back to whoever holds the session id, and that is the player. Session consumption is now a claim rather than a courtesy. Reading a session and then deleting it is not a guard: ten concurrent submits all read it alive, all delete it, and all score. DEL is atomic and returns how many keys it removed, so exactly one caller sees a 1 -- the route scores only if it won that. Consuming before the writes also closes the sequential case, where a failure partway through the fan-out would otherwise leave the session alive for half an hour and let a retry re-credit every level that already succeeded. A guess lost to a mid-write failure is the accepted cost. Region parsing lives in one place. Four routes accept a region, and four slightly different ideas about casing and defaulting is how a typo becomes a leaderboard key nobody reads. Unknown and uncovered codes are rejected with a 400 that names the region, rather than served as an empty board that looks exactly like a region nobody has played yet. Sessions created before this change still score, at province level, since they carry no district. They expire within half an hour, so the fallback can go a release from now. The debug coverage route serves any region with an outline rather than only the five provinces. Its directory keeps the city-coverage name for now: the page that calls it cannot be read in this environment, and renaming the route without updating its caller would break it.
69 lines
3.0 KiB
JavaScript
69 lines
3.0 KiB
JavaScript
import { describe, it, expect } from 'vitest';
|
|
import { GET } from '../src/app/api/debug/city-coverage/route.js';
|
|
import { countPanos } from '../src/lib/pano-index.js';
|
|
import { provinceOf } from '../src/lib/regions.js';
|
|
|
|
// The coverage route now serves any region with an outline, not just the five
|
|
// province indexes. It reaches getRegionPanos, which throws for the country --
|
|
// guarded today only by the country having no boundary, which is a coupling
|
|
// between two generated files worth pinning.
|
|
|
|
const request = (query) => new Request(`http://localhost/api/debug/coverage?${query}`);
|
|
|
|
describe('GET debug coverage', () => {
|
|
it('serves a province', async () => {
|
|
const body = await (await GET(request('region=DN'))).json();
|
|
expect(body.success).toBe(true);
|
|
expect(body.counts.total).toBe(countPanos('DN'));
|
|
expect(body.region.level).toBe('province');
|
|
});
|
|
|
|
it('serves a district, scoped to that district', async () => {
|
|
const body = await (await GET(request('region=DN-HAICHAU'))).json();
|
|
expect(body.success).toBe(true);
|
|
expect(body.counts.total).toBe(countPanos('DN-HAICHAU'));
|
|
expect(body.counts.total).toBeLessThan(countPanos('DN'));
|
|
expect(body.region.province).toBe('DN');
|
|
});
|
|
|
|
it('rejects the country, which has no outline to draw', async () => {
|
|
// getRegionPanos throws for a country-level code. This must surface as a
|
|
// 400 from the boundary check, not an unhandled throw.
|
|
const response = await GET(request('region=VN'));
|
|
expect(response.status).toBe(400);
|
|
expect((await response.json()).error).toMatch(/Unknown or unmapped region/);
|
|
});
|
|
|
|
it('rejects a district with no boundary', async () => {
|
|
// Cu Chi is in the tree but has no OSM relation left.
|
|
expect((await GET(request('region=TPHCM-CUCHI'))).status).toBe(400);
|
|
});
|
|
|
|
it('rejects an unknown code', async () => {
|
|
expect((await GET(request('region=NOPE'))).status).toBe(400);
|
|
});
|
|
|
|
it('still accepts ?city= from the existing debug page', async () => {
|
|
expect((await (await GET(request('city=DN'))).json()).success).toBe(true);
|
|
});
|
|
|
|
it('reports the generation stamp its consumer may render', async () => {
|
|
// The debug page cannot be read from this session, so the field it might
|
|
// depend on is asserted here instead.
|
|
const body = await (await GET(request('region=DN-HAICHAU'))).json();
|
|
expect(body.generatedAt).toBeTruthy();
|
|
expect(provinceOf('DN-HAICHAU')).toBe('DN');
|
|
});
|
|
|
|
it('sends the outline once, not on every viewport query', async () => {
|
|
// Returning it with each pan gave the client a new object every time, which
|
|
// made the map refit and cancel whatever the user had zoomed into.
|
|
const first = await (await GET(request('region=DN'))).json();
|
|
expect(first.boundary).toBeTruthy();
|
|
|
|
const panned = await (await GET(request('region=DN&bbox=108.1,16.0,108.3,16.1'))).json();
|
|
expect(panned.boundary).toBeUndefined();
|
|
expect(panned.counts.inView).toBeLessThanOrEqual(panned.counts.total);
|
|
});
|
|
});
|