Files
TREK/server/tests/unit/systemNotices/conditions.test.ts
T
Maurice 49c3c57c31 Give the ephemeral tokens a provider, and clear the next wave of type errors
ephemeral-tokens.ts was free functions, and AuthService owned the cleanup
timer only because it happened to be the domain that mints tokens. Issuing
them and sweeping them are different jobs, and FilesService and TokenService
mint or consume them too.

EphemeralTokenService owns the lifecycle and hands the three operations out.
The store and the interval stay module state next door, and that is
load-bearing rather than a leftover: src/websocket.ts consumes ws tokens on
the upgrade handshake, outside the container, before any injector is in
reach. Per-instance state would hand it an empty Map and close every
connection with 4001, which no test would catch because the ws suites drive
the transport directly. WS-027 through WS-029 pin the session gate that runs
through exactly that path.

The MAX_STORE_SIZE back-pressure is the second reason: createEphemeralToken
returns null at 10,000 live tokens and TokenService turns that into a 503. A
second store would raise the real ceiling with nobody noticing.

Its own leaf module rather than a provider in AuthModule: TokensModule mints
download and ws tokens as well, AuthModule already imports TokensModule, and
TokensModule is documented as a deliberate leaf that three graphs reach for.
Same shape as MailerModule, which broke AuthModule <-> NotificationsModule.

Twelve hand-wired construction sites in tests needed the new collaborator.
The typecheck named all twelve; before this branch they would have been
undefined at runtime.

Sixteen more test files are cleared here too, each at its root cause. Four of
them were passing for the wrong reason:

files.controller.test.ts drove two handlers with shifted arguments, a string
tripId where the trip row belongs and undefined where tripId and id belong.
It stayed green because FilesService is fully stubbed and the stubs ignore
what they are given, so DELETE /:id/permanent and DELETE /trash/empty never
covered that the trip row reaches the permission check.

conflictUpdate.test.ts was missing real collaborators on PlacesService and
PackingService. None of the exercised paths reach them, so it passed by luck.

assignments.service.test.ts builds its service by hand, and the one consumer
of the collaborator it was missing swallows every throw in a bare catch. A
broken journey reconciliation would still have shown 28 green tests.

scheduler.test.ts mocks two modules with paths that resolve one directory
above the server root, so they mock nothing at all. Harmless only because
src/scheduler.ts imports neither.
2026-08-10 19:54:46 +02:00

103 lines
4.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { evaluate } from '../../../src/systemNotices/conditions.js';
import type { NoticeCondition, SystemNotice } from '../../../src/systemNotices/types.js';
const baseNotice: SystemNotice = {
id: 'test',
display: 'modal',
severity: 'info',
titleKey: 'k.title',
bodyKey: 'k.body',
dismissible: true,
conditions: [],
publishedAt: '2026-01-01T00:00:00Z',
};
// Conditions have to be contextually typed as NoticeCondition, otherwise a literal like
// `roles: ['user']` widens to string[] and no longer matches the union's
// Array<'admin' | 'user'>. Going through this helper types every case at once.
const noticeWith = (...conditions: NoticeCondition[]): SystemNotice => ({ ...baseNotice, conditions });
const baseCtx = {
// noTrips joined the context when the 'noTrips' condition kind landed. 1 keeps
// every case below reading as it did: the condition is `noTrips === 0`, so a
// user WITH trips leaves it false and the other kinds decide on their own.
user: { login_count: 5, first_seen_version: '1.0.0', role: 'user', noTrips: 1 },
currentAppVersion: '2.0.0',
now: new Date('2026-06-01T00:00:00Z'),
};
describe('firstLogin', () => {
const notice = noticeWith({ kind: 'firstLogin' });
it('passes when login_count <= 1', () => {
expect(evaluate(notice, { ...baseCtx, user: { ...baseCtx.user, login_count: 1 } })).toBe(true);
});
it('fails when login_count > 1', () => {
expect(evaluate(notice, baseCtx)).toBe(false);
});
});
describe('existingUserBeforeVersion', () => {
const notice = noticeWith({ kind: 'existingUserBeforeVersion', version: '2.0.0' });
it('passes for user with first_seen_version < notice version when current >= notice version', () => {
expect(evaluate(notice, baseCtx)).toBe(true);
});
it('fails for new user (first_seen_version >= notice version)', () => {
expect(evaluate(notice, { ...baseCtx, user: { ...baseCtx.user, first_seen_version: '2.0.0' } })).toBe(false);
});
it('fails when current app version < notice version', () => {
expect(evaluate(notice, { ...baseCtx, currentAppVersion: '1.5.0' })).toBe(false);
});
it('passes when current app version is a prerelease of the notice version', () => {
expect(evaluate(notice, { ...baseCtx, currentAppVersion: '2.0.0-pre.42' })).toBe(true);
});
it('passes when current app version is a prerelease beyond the notice version', () => {
expect(evaluate(notice, { ...baseCtx, currentAppVersion: '2.1.0-pre.1' })).toBe(true);
});
});
describe('dateWindow', () => {
it('passes when now is inside window', () => {
const notice = noticeWith({ kind: 'dateWindow', startsAt: '2026-05-01T00:00:00Z', endsAt: '2026-07-01T00:00:00Z' });
expect(evaluate(notice, baseCtx)).toBe(true);
});
it('fails when now is before start', () => {
const notice = noticeWith({ kind: 'dateWindow', startsAt: '2026-07-01T00:00:00Z' });
expect(evaluate(notice, baseCtx)).toBe(false);
});
it('passes when no endsAt', () => {
const notice = noticeWith({ kind: 'dateWindow', startsAt: '2026-01-01T00:00:00Z' });
expect(evaluate(notice, baseCtx)).toBe(true);
});
});
describe('role', () => {
it('passes for matching role', () => {
const notice = noticeWith({ kind: 'role', roles: ['user'] });
expect(evaluate(notice, baseCtx)).toBe(true);
});
it('fails for non-matching role', () => {
const notice = noticeWith({ kind: 'role', roles: ['admin'] });
expect(evaluate(notice, baseCtx)).toBe(false);
});
});
describe('AND logic', () => {
it('requires all conditions to pass', () => {
const notice = noticeWith(
{ kind: 'firstLogin' },
{ kind: 'role', roles: ['user'] },
);
// login_count=1 passes firstLogin, role=user passes role → true
expect(evaluate(notice, { ...baseCtx, user: { ...baseCtx.user, login_count: 1 } })).toBe(true);
// login_count=2 fails firstLogin → false
expect(evaluate(notice, baseCtx)).toBe(false);
});
});
describe('empty conditions', () => {
it('always passes when conditions array is empty', () => {
expect(evaluate(baseNotice, baseCtx)).toBe(true);
});
});