Compare commits

...
Author SHA1 Message Date
Mos ea806262c8 test(gateway): size the enrollment clamp tolerance to CI jitter, not to a fast machine
ci/woodpecker/pr/ci Pipeline was successful
Closes #1090.

`clamps ttlSeconds to 900` asserted a wall-clock delta against a fixed 100 ms
budget:

    expect(expiresMs - before).toBeLessThanOrEqual(900_000 + 100);

That budget covers everything between `Date.now()` and the service computing
`expiresAt`. On a loaded CI agent — this step runs alongside a Postgres service
container and several other workspaces — it came back at 900_106 and failed by
6 ms (#1090, observed on pipeline 2258).

The property under test is CLAMPING: a 9999s request must come back as 900s.
The gap between clamped and unclamped is 9_099_000 ms, so the tolerance only has
to exceed scheduler jitter to stay discriminating:

    clamped   ~900_000 ms   <= 905_000 ✓   < 1_000_000 ✓
    unclamped ~9_999_000 ms <= 905_000 ✗   < 1_000_000 ✗

A 5s allowance consumes 0.055% of that margin, and an unclamped implementation
still misses by three orders of magnitude.

Also pins the clamp explicitly with `toBeLessThan(1_000_000)`, so the property
is asserted independently of any timing allowance — widening the jitter budget
later cannot silently weaken it.

NOT DONE: I could not execute vitest here (no node_modules; a full install is
minutes). The discrimination argument above is arithmetic, not an executed
red/green. A reviewer should run the suite, and if it is cheap, confirm that
removing the clamp in the service still fails this test.
2026-08-07 00:18:50 -05:00
@@ -245,9 +245,21 @@ describe('EnrollmentService.createToken', () => {
const after = Date.now(); const after = Date.now();
const expiresMs = new Date(result.expiresAt).getTime(); const expiresMs = new Date(result.expiresAt).getTime();
// Should be at most 900s from now
expect(expiresMs - before).toBeLessThanOrEqual(900_000 + 100); // The property under test is CLAMPING: a 9999s request must come back as 900s.
// The gap between clamped and unclamped is 9_099_000 ms, so the tolerance below
// only has to exceed CI scheduling jitter — it does not need to be tight to keep
// the assertion discriminating. A 5s allowance consumes 0.05% of that margin and
// an unclamped result still misses by three orders of magnitude.
//
// It was 100ms and failed on a loaded agent at 900_106 — 6ms over (#1090). A
// wall-clock budget sized to a fast machine is a flake, not a tighter test.
const CI_JITTER_MS = 5_000;
expect(expiresMs - before).toBeLessThanOrEqual(900_000 + CI_JITTER_MS);
expect(expiresMs - after).toBeGreaterThanOrEqual(0); expect(expiresMs - after).toBeGreaterThanOrEqual(0);
// Explicitly pin the clamp itself, independent of any timing allowance:
// unclamped (9999s) would exceed this by ~9_099_000 ms.
expect(expiresMs - before).toBeLessThan(1_000_000);
}); });
}); });