tests/sims/cdn.test.ts
The automated tests for the /simulators/cloudfront-cache simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- Edge TTLs follow the rules in the CloudFront Developer Guide ("Managing how long content stays in the cache").
- One cache layer misses at the renewal-theory rate λ / (1 + λ·TTL).
- The layered model agrees with a request-by-request simulation.
// /simulators/cloudfront-cache: CloudFront TTL rules and the edge / regional / Origin Shield hierarchy.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseCacheControl, cloudfrontTtl, hierarchy, hierarchyBrute, ttlMissRate } from '../../src/lib/sims/cdn.ts';
const Y = 31536000, D = 86400;
const ttl = (cc: string, min: number, def: number, max: number, expires: number | null = null) => cloudfrontTtl(parseCacheControl(cc).d, expires, min, def, max);
test('edge TTLs follow the rules in the CloudFront Developer Guide ("Managing how long content stays in the cache")', () => {
// [Cache-Control, min, default, max, Expires in s, expected edge TTL, expected mode]
const cases: [string, number, number, number, number | null, number, string][] = [
['max-age=3600', 0, D, Y, null, 3600, 'cache'], // within min..max: used as is
['max-age=0', 1, D, Y, null, 1, 'cache'], // below the minimum: the minimum
['max-age=99999999', 0, D, Y, null, Y, 'cache'], // above the maximum: the maximum
['', 0, D, Y, null, D, 'cache'], // no header: the default TTL
['', 0, 0, Y, null, 0, 'revalidate'],
['', 0, D, Y, 300, 300, 'cache'], // Expires when there is no max-age
['', 600, D, Y, 300, 600, 'cache'],
['max-age=60, s-maxage=600', 0, D, Y, null, 600, 'cache'], // s-maxage wins for CloudFront
['no-cache', 0, D, Y, null, 0, 'revalidate'], // minimum 0: CloudFront respects no-cache
['no-store', 0, D, Y, null, 0, 'nostore'],
['private, max-age=60', 0, 0, Y, null, 0, 'nostore'],
['no-store', 1, D, Y, null, 1, 'cache'], // minimum above 0: cached for the minimum regardless
['max-age=3600', 0, 0, 0, null, 0, 'revalidate']
];
for (const [cc, min, def, max, exp, edge, mode] of cases) {
const r = ttl(cc, min, def, max, exp);
assert.equal(r.edge, edge, `${cc || '(none)'} [${min}/${def}/${max}] edge`);
assert.equal(r.edgeMode, mode, `${cc || '(none)'} [${min}/${def}/${max}] mode`);
}
assert.ok(ttl('max-age=60', 100, 10, 1000).error, 'minimum above default is rejected');
const swr = ttl('max-age=3600, stale-while-revalidate=600, stale-if-error=86400', 0, D, Y);
assert.equal(swr.swr, 600);
assert.equal(swr.sie, 86400);
});
test('one cache layer misses at the renewal-theory rate λ / (1 + λ·TTL)', () => {
for (const [rate, t] of [[0.5, 10], [5, 60], [0.01, 300], [100, 1]]) {
const h = hierarchy({ rate, ttl: t, revalidate: false, edges: 1, regionals: 1, shield: false, variants: 1, seed: 3 });
const want = ttlMissRate(rate, t) * 3600;
// closed form, independent of the code under test
assert.ok(Math.abs(want - (rate / (1 + rate * t)) * 3600) < 1e-9);
assert.ok(Math.abs(h.layers[0].out - want) / want < 0.05, `rate ${rate} ttl ${t}: ${h.layers[0].out} vs ${want}`);
}
});
test('the layered model agrees with a request-by-request simulation', () => {
for (const p of [
{ rate: 20, ttl: 30, edges: 10, regionals: 3, shield: true, variants: 4 },
{ rate: 2, ttl: 120, edges: 20, regionals: 4, shield: true, variants: 1 },
{ rate: 50, ttl: 5, edges: 8, regionals: 2, shield: false, variants: 10 }
]) {
const q = { ...p, revalidate: false, seed: 4 };
const h = hierarchy(q);
const secs = 20000, k = 3600 / secs;
const b = hierarchyBrute(q, secs);
const brute = [b.edgeMiss * k, b.regMiss * k, b.shMiss * k];
h.layers.forEach((l, i) => {
if (brute[i] === undefined || (i === 2 && !p.shield)) return;
assert.ok(Math.abs(l.out - brute[i]) / Math.max(brute[i], 1) < 0.08, `${JSON.stringify(p)} layer ${l.name}: ${l.out} vs ${brute[i]}`);
});
}
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.