mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
common/ had become the backend's library: 30 of its 41 modules were imported by nothing in the frontend, so the name no longer said what belonged there. It now holds only what the frontend or the build imports too; backend-only code moved to server/, flat except for datasets/ (the dataset engine and its readers). The *-db/ dataset directories stay in common/: self-hosting docs and Docker volumes mount those paths. frontend/utils/ keeps its app-wide modules at the root and gains ip/ (IP / ASN shaping and validation), report/ (the report pipeline) and features/ (the pure layer of a single feature). Paths only — no logic changes. The Dockerfile copies server/, and the AGENTS.md files say where a new module goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
2.5 KiB
JavaScript
53 lines
2.5 KiB
JavaScript
// Tests for server/cache-control.js — the cacheable() middleware: the full
|
|
// TTL on a 2xx JSON body, nothing on an error, the cacheIf veto and the
|
|
// degraded TTL it can fall back to.
|
|
|
|
import assert from 'node:assert/strict';
|
|
import { describe, it } from 'node:test';
|
|
|
|
import { cacheable } from '../server/cache-control.js';
|
|
|
|
// Run the middleware, then answer `body` with `status`; returns the
|
|
// Cache-Control header it left (undefined = the /api no-store default).
|
|
const answer = (middleware, { status = 200, body = {}, req = {} } = {}) => {
|
|
const headers = {};
|
|
const res = {
|
|
statusCode: status,
|
|
locals: {},
|
|
setHeader: (name, value) => { headers[name] = value; },
|
|
json: () => res,
|
|
};
|
|
middleware(req, res, () => {});
|
|
res.json(body);
|
|
return headers['Cache-Control'];
|
|
};
|
|
|
|
describe('cacheable', () => {
|
|
it('stamps the full TTL on a 2xx body, nothing on an error', () => {
|
|
assert.equal(answer(cacheable(60)), 'public, max-age=60');
|
|
assert.equal(answer(cacheable(60), { status: 502 }), undefined);
|
|
});
|
|
|
|
it('resolves the TTL per request; falsy keeps no-store', () => {
|
|
const ttl = (req) => ({ a: 30 })[req.query.view];
|
|
assert.equal(answer(cacheable(ttl), { req: { query: { view: 'a' } } }), 'public, max-age=30');
|
|
assert.equal(answer(cacheable(ttl), { req: { query: { view: 'b' } } }), undefined);
|
|
});
|
|
|
|
it('leaves a vetoed body uncached without a degraded TTL', () => {
|
|
assert.equal(answer(cacheable(60, { cacheIf: (body) => body.ok }), { body: { ok: false } }), undefined);
|
|
assert.equal(answer(cacheable(60, { cacheIf: (body) => body.ok }), { body: { ok: true } }), 'public, max-age=60');
|
|
});
|
|
|
|
it('gives a vetoed body the degraded TTL, resolved when the body is sent', () => {
|
|
let booting = true;
|
|
const middleware = () => cacheable(604800, { cacheIf: (body) => body.ok, degradedMaxAge: () => (booting ? 0 : 86400) });
|
|
assert.equal(answer(middleware(), { body: { ok: false } }), undefined, 'nothing degraded while booting');
|
|
booting = false;
|
|
assert.equal(answer(middleware(), { body: { ok: false } }), 'public, max-age=86400');
|
|
assert.equal(answer(middleware(), { body: { ok: true } }), 'public, max-age=604800');
|
|
assert.equal(answer(cacheable(604800, { cacheIf: () => false, degradedMaxAge: 3600 })), 'public, max-age=3600');
|
|
assert.equal(answer(middleware(), { status: 502, body: { ok: false } }), undefined, 'never on an error');
|
|
});
|
|
});
|