mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 10:24:02 +00:00
test(terrain): give every terrain-heights proxy in the bounds tests its own cache
Seven tests built terrainHeightsProxy() with no cachePath, so the proxy fell back to this checkout's .gev-cache/terrain-heights.json. A point the app had already cached there answered a request the test expected to reach the stubbed upstream: after running the dev server near Austin, the cross-site test failed with 1 !== 2. isolatedCachePath(t) gives each test a mkdtemp cache file removed in t.after, as the over-large cache test already did; that test now uses it too. Fixes #1242. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BNTtHAa65nobzJ7jp2Ltr4
This commit is contained in:
1 parent
0f51eb21ba
commit
657fd589d7
1 file changed
+37
-12
@@ -99,6 +99,18 @@ function stubTerrainUpstream(t) {
|
||||
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', () => {
|
||||
// Finite but not on Earth — the old guard accepted every one of these.
|
||||
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;
|
||||
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' });
|
||||
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) => {
|
||||
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, {
|
||||
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) => {
|
||||
const asked = stubTerrainUpstream(t);
|
||||
// A ceiling of 3 exercises the same code path 50,000 does, in four requests.
|
||||
const route = install(terrainHeightsProxy({ cacheMaxEntries: 3 })).get(
|
||||
'/api/terrain/heights',
|
||||
);
|
||||
const route = install(
|
||||
terrainHeightsProxy({
|
||||
cachePath: await isolatedCachePath(t),
|
||||
cacheMaxEntries: 3,
|
||||
}),
|
||||
).get('/api/terrain/heights');
|
||||
|
||||
const point = (i) => `-97.7${i}000,30.26000`;
|
||||
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) => {
|
||||
const asked = stubTerrainUpstream(t);
|
||||
const point = '-97.74000,30.26000';
|
||||
const dir = await mkdtemp(path.join(tmpdir(), 'gev-terrain-'));
|
||||
const cachePath = path.join(dir, 'terrain-heights.json');
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const cachePath = await isolatedCachePath(t);
|
||||
|
||||
// A cache file holding the answer, but larger than the ceiling allows.
|
||||
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) => {
|
||||
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;
|
||||
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) => {
|
||||
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 refused = [
|
||||
{ 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) => {
|
||||
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';
|
||||
// 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,
|
||||
@@ -347,7 +370,9 @@ test('a second client keeps its own quota', async (t) => {
|
||||
stubFetch(t, async () =>
|
||||
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)
|
||||
await request(route, {
|
||||
|
||||
Reference in new issue
Block a user