Merge pull request #1244 from KnottyDyes/fix/terrain-test-cache-isolation

test(terrain): give every terrain-heights proxy in the bounds tests its own cache
This commit is contained in:
Sameh Khamis authored and GitHub committed 2026-10-24 21:26:11 -07:00
commit 2a48e1d186
2 files changed
+41 -12

No files matched your search

@@ -0,0 +1,4 @@
- The terrain-heights bounds tests no longer read the checkout's
`.gev-cache/terrain-heights.json`. Each test gets a cache file of its own, so
`npm test` no longer fails after the dev server has cached terrain near
Austin (KnottyDyes, #1244).
+37 -12
View File
@@ -99,6 +99,18 @@ function stubTerrainUpstream(t) {
return asked; return asked;
} }
/**
* A cache file of the test's own, removed when the test ends. Without one the
* proxy falls back to this checkout's `.gev-cache/terrain-heights.json`, and a
* point the app has already cached there would answer a request the test
* expects to reach the stubbed upstream (#1242).
*/
async function isolatedCachePath(t) {
const dir = await mkdtemp(path.join(tmpdir(), 'gev-terrain-'));
t.after(() => rm(dir, { recursive: true, force: true }));
return path.join(dir, 'terrain-heights.json');
}
test('a coordinate off the globe is not a cache key', () => { test('a coordinate off the globe is not a cache key', () => {
// Finite but not on Earth — the old guard accepted every one of these. // Finite but not on Earth — the old guard accepted every one of these.
for (const raw of [ for (const raw of [
@@ -122,7 +134,9 @@ test('an out-of-range point is refused before it reaches upstream or the cache',
upstreamCalls += 1; upstreamCalls += 1;
return new Response('[]', { status: 200 }); return new Response('[]', { status: 200 });
}); });
const route = install(terrainHeightsProxy()).get('/api/terrain/heights'); const route = install(
terrainHeightsProxy({ cachePath: await isolatedCachePath(t) }),
).get('/api/terrain/heights');
const refused = await request(route, { url: '/?points=0,91' }); const refused = await request(route, { url: '/?points=0,91' });
assert.equal(refused.statusCode, 400); assert.equal(refused.statusCode, 400);
@@ -135,7 +149,9 @@ test('an out-of-range point is refused before it reaches upstream or the cache',
test('one out-of-range point rejects the batch it is hidden in', async (t) => { test('one out-of-range point rejects the batch it is hidden in', async (t) => {
stubFetch(t, async () => new Response('[]', { status: 200 })); stubFetch(t, async () => new Response('[]', { status: 200 }));
const route = install(terrainHeightsProxy()).get('/api/terrain/heights'); const route = install(
terrainHeightsProxy({ cachePath: await isolatedCachePath(t) }),
).get('/api/terrain/heights');
const mixed = await request(route, { const mixed = await request(route, {
url: '/?points=-97.74,30.26;0,999;-97.75,30.27', url: '/?points=-97.74,30.26;0,999;-97.75,30.27',
}); });
@@ -145,9 +161,12 @@ test('one out-of-range point rejects the batch it is hidden in', async (t) => {
test('the point cache stops growing, and an evicted point is simply refetched', async (t) => { test('the point cache stops growing, and an evicted point is simply refetched', async (t) => {
const asked = stubTerrainUpstream(t); const asked = stubTerrainUpstream(t);
// A ceiling of 3 exercises the same code path 50,000 does, in four requests. // A ceiling of 3 exercises the same code path 50,000 does, in four requests.
const route = install(terrainHeightsProxy({ cacheMaxEntries: 3 })).get( const route = install(
'/api/terrain/heights', terrainHeightsProxy({
); cachePath: await isolatedCachePath(t),
cacheMaxEntries: 3,
}),
).get('/api/terrain/heights');
const point = (i) => `-97.7${i}000,30.26000`; const point = (i) => `-97.7${i}000,30.26000`;
for (let i = 1; i <= 4; i += 1) for (let i = 1; i <= 4; i += 1)
@@ -168,9 +187,7 @@ test('the point cache stops growing, and an evicted point is simply refetched',
test('an over-large cache file is not read back into memory', async (t) => { test('an over-large cache file is not read back into memory', async (t) => {
const asked = stubTerrainUpstream(t); const asked = stubTerrainUpstream(t);
const point = '-97.74000,30.26000'; const point = '-97.74000,30.26000';
const dir = await mkdtemp(path.join(tmpdir(), 'gev-terrain-')); const cachePath = await isolatedCachePath(t);
const cachePath = path.join(dir, 'terrain-heights.json');
t.after(() => rm(dir, { recursive: true, force: true }));
// A cache file holding the answer, but larger than the ceiling allows. // A cache file holding the answer, but larger than the ceiling allows.
const padded = { const padded = {
@@ -201,7 +218,9 @@ test('an over-large cache file is not read back into memory', async (t) => {
test('the route refuses a flood of requests from one client', async (t) => { test('the route refuses a flood of requests from one client', async (t) => {
stubTerrainUpstream(t); stubTerrainUpstream(t);
const route = install(terrainHeightsProxy()).get('/api/terrain/heights'); const route = install(
terrainHeightsProxy({ cachePath: await isolatedCachePath(t) }),
).get('/api/terrain/heights');
let refusedAt = 0; let refusedAt = 0;
for (let i = 1; i <= 95; i += 1) { for (let i = 1; i <= 95; i += 1) {
@@ -218,7 +237,9 @@ test('the route refuses a flood of requests from one client', async (t) => {
test('a cross-site page cannot fill the cache or spend the visitor quota', async (t) => { test('a cross-site page cannot fill the cache or spend the visitor quota', async (t) => {
const asked = stubTerrainUpstream(t); const asked = stubTerrainUpstream(t);
const route = install(terrainHeightsProxy()).get('/api/terrain/heights'); const route = install(
terrainHeightsProxy({ cachePath: await isolatedCachePath(t) }),
).get('/api/terrain/heights');
const host = 'localhost:5173'; const host = 'localhost:5173';
const refused = [ const refused = [
{ host, 'sec-fetch-site': 'cross-site' }, // <img src> from any web page { host, 'sec-fetch-site': 'cross-site' }, // <img src> from any web page
@@ -253,7 +274,9 @@ test('a cross-site page cannot fill the cache or spend the visitor quota', async
test('the app served behind a reverse proxy still gets terrain', async (t) => { test('the app served behind a reverse proxy still gets terrain', async (t) => {
const asked = stubTerrainUpstream(t); const asked = stubTerrainUpstream(t);
const route = install(terrainHeightsProxy()).get('/api/terrain/heights'); const route = install(
terrainHeightsProxy({ cachePath: await isolatedCachePath(t) }),
).get('/api/terrain/heights');
const host = 'globe.example.lan'; const host = 'globe.example.lan';
// What nginx/Caddy/Cloudflare add in front of a same-origin app fetch. The // What nginx/Caddy/Cloudflare add in front of a same-origin app fetch. The
// cost-bearing routes refuse these; terrain is keyless and needed to render, // cost-bearing routes refuse these; terrain is keyless and needed to render,
@@ -347,7 +370,9 @@ test('a second client keeps its own quota', async (t) => {
stubFetch(t, async () => stubFetch(t, async () =>
Response.json([{ lng: -97.74, lat: 30.26, height: 150 }]), Response.json([{ lng: -97.74, lat: 30.26, height: 150 }]),
); );
const route = install(terrainHeightsProxy()).get('/api/terrain/heights'); const route = install(
terrainHeightsProxy({ cachePath: await isolatedCachePath(t) }),
).get('/api/terrain/heights');
for (let i = 0; i < 90; i += 1) for (let i = 0; i < 90; i += 1)
await request(route, { await request(route, {