tests: migrate Date.now patching to the pinned-clock parameter
The now parameter (and partialGraph.now) replaced the need for global Date.now patching in tests. The ttl-expiry parity campaign's module-scope patch and the relational-comparator Time-Based Decay tests' before/after patches now pass the clock explicitly through check options — no global mutation, no restore-order fragility, and the pinned-clock cache bypass keeps every evaluation honest. No stale references to the removed compiled evaluator or useCompiled option remain in the test suite.
This commit is contained in:
@@ -23,9 +23,7 @@ const TTL = 5000;
|
|||||||
const DOCS = 2;
|
const DOCS = 2;
|
||||||
const docKey = (i) => `doc:${i}`;
|
const docKey = (i) => `doc:${i}`;
|
||||||
|
|
||||||
const realNow = Date.now;
|
|
||||||
let engineNow = 1_000_000_000_000;
|
let engineNow = 1_000_000_000_000;
|
||||||
Date.now = () => engineNow;
|
|
||||||
|
|
||||||
function makeWrapper(disableCaching) {
|
function makeWrapper(disableCaching) {
|
||||||
const arbiter = new Arbiter(disableCaching ? { disableCaching: true } : {});
|
const arbiter = new Arbiter(disableCaching ? { disableCaching: true } : {});
|
||||||
@@ -69,7 +67,7 @@ function makeWrapper(disableCaching) {
|
|||||||
return { ok: true, now: engineNow };
|
return { ok: true, now: engineNow };
|
||||||
},
|
},
|
||||||
check(doc) {
|
check(doc) {
|
||||||
const result = arbiter.check('user:alice', 'premium', docKey(doc));
|
const result = arbiter.check('user:alice', 'premium', docKey(doc), { now: engineNow });
|
||||||
const b = balance.get(doc);
|
const b = balance.get(doc);
|
||||||
const p = price.get(doc);
|
const p = price.get(doc);
|
||||||
const bFresh = b !== undefined && engineNow - b.ts <= TTL;
|
const bFresh = b !== undefined && engineNow - b.ts <= TTL;
|
||||||
|
|||||||
@@ -294,8 +294,6 @@ describe('RelationalComparatorRule Tests', () => {
|
|||||||
describe('Time-Based Decay', () => {
|
describe('Time-Based Decay', () => {
|
||||||
it('ignores stale timestamps when no decay is applied', () => {
|
it('ignores stale timestamps when no decay is applied', () => {
|
||||||
const now = 1_000_000_000_000;
|
const now = 1_000_000_000_000;
|
||||||
const originalDateNow = Date.now;
|
|
||||||
Date.now = () => now;
|
|
||||||
|
|
||||||
const arbiter = setupRelationalComparatorTestGraph();
|
const arbiter = setupRelationalComparatorTestGraph();
|
||||||
arbiter.addNode('user:decay-test', 'user');
|
arbiter.addNode('user:decay-test', 'user');
|
||||||
@@ -334,16 +332,12 @@ describe('RelationalComparatorRule Tests', () => {
|
|||||||
marginOfSafety: 1.0,
|
marginOfSafety: 1.0,
|
||||||
fallbackBehavior: 'deny'
|
fallbackBehavior: 'deny'
|
||||||
});
|
});
|
||||||
const result = arbiter.check('user:decay-test', 'can_access_decay_test', 'feature:decay-test');
|
const result = arbiter.check('user:decay-test', 'can_access_decay_test', 'feature:decay-test', { now });
|
||||||
assert.equal(result.possibility, 1);
|
assert.equal(result.possibility, 1);
|
||||||
|
|
||||||
Date.now = originalDateNow;
|
|
||||||
});
|
});
|
||||||
it('keeps comparison crisp even for older timestamps', () => {
|
it('keeps comparison crisp even for older timestamps', () => {
|
||||||
const arbiter = setupRelationalComparatorTestGraph();
|
const arbiter = setupRelationalComparatorTestGraph();
|
||||||
const now = 1_000_000_000_000;
|
const now = 1_000_000_000_000;
|
||||||
const originalDateNow = Date.now;
|
|
||||||
Date.now = () => now;
|
|
||||||
|
|
||||||
arbiter.addNode('user:usage-test', 'user');
|
arbiter.addNode('user:usage-test', 'user');
|
||||||
arbiter.addNode('service:limited', 'service');
|
arbiter.addNode('service:limited', 'service');
|
||||||
@@ -383,10 +377,8 @@ describe('RelationalComparatorRule Tests', () => {
|
|||||||
comparator: '<=',
|
comparator: '<=',
|
||||||
fallbackBehavior: 'deny'
|
fallbackBehavior: 'deny'
|
||||||
});
|
});
|
||||||
const result = arbiter.check('user:usage-test', 'within_quota', 'service:limited');
|
const result = arbiter.check('user:usage-test', 'within_quota', 'service:limited', { now });
|
||||||
assert.equal(result.possibility, 1);
|
assert.equal(result.possibility, 1);
|
||||||
|
|
||||||
Date.now = originalDateNow;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user