Files

179 lines
7.4 KiB
TypeScript

/**
* The database.
*
* The first two tests are the most valuable in this service and are the reason
* the file exists: they read the schema back out of SQLite and assert that it
* has SIX columns in ONE table. CLAUDE.md §4 says a seventh column is a design
* discussion rather than a migration, and a rule with no test behind it is a
* rule that erodes on a busy afternoon.
*
* Every database here is `:memory:`. Nothing touches a real deployment, a real
* key, or anyone's history (CLAUDE.md §5.5).
*/
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { PARTICIPANT_COLUMNS, RelayStore } from '../src/store.js';
import type { AcceptedSubmission } from '../src/verify.js';
import { keyFor } from './helpers.js';
let store: RelayStore;
beforeEach(() => {
store = RelayStore.open(':memory:');
});
afterEach(() => {
// In `afterEach` rather than at the end of each test: a failing assertion
// returns first, so cleanup on the happy path leaks exactly when it fails
// (CLAUDE.md §5.7).
store.close();
});
function accepted(overrides: Partial<AcceptedSubmission> = {}): AcceptedSubmission {
return {
pubkey: keyFor(1).pubkey,
displayName: 'tereza',
leagueId: 'domus.eng',
weekId: '2026-W34',
xp: 1240,
clamped: false,
...overrides
};
}
describe('the schema is the promise', () => {
it('has exactly the six columns CLAUDE.md §4 names, and no seventh', () => {
expect(store.columns()).toEqual([...PARTICIPANT_COLUMNS]);
expect(store.columns()).toHaveLength(6);
});
it('has exactly one table, so there is nothing to join to', () => {
expect(store.tables()).toEqual(['participants']);
});
it('names no column that could identify a connection', () => {
// Stated as a deny-list as well as an allow-list, because the allow-list
// test passes if someone renames a column rather than adding one.
const forbidden = ['ip', 'addr', 'address', 'agent', 'referer', 'referrer', 'host', 'session'];
for (const column of store.columns()) {
for (const word of forbidden) {
expect(column.includes(word), `column \`${column}\` looks like ${word}`).toBe(false);
}
}
});
it('refuses a value of the wrong type, because the table is STRICT', () => {
// Without STRICT, SQLite puts the string "lots" in an INTEGER column
// happily — which is exactly what untrusted input turns up to do.
expect(() =>
store.record({ ...accepted(), xp: 'lots' as unknown as number }, 1)
).toThrow();
});
});
describe('recording a standing', () => {
it('keeps ONE row per participant, however many times they submit', () => {
const key = keyFor(1);
store.record(accepted({ pubkey: key.pubkey, xp: 100 }), 1);
store.record(accepted({ pubkey: key.pubkey, xp: 400 }), 2);
store.record(accepted({ pubkey: key.pubkey, xp: 900 }), 3);
expect(store.count()).toBe(1);
expect(store.board('domus.eng', '2026-W34', 10).rows[0]?.xp).toBe(900);
});
it('accumulates no history — last week is overwritten, not archived', () => {
const key = keyFor(1);
store.record(accepted({ pubkey: key.pubkey, weekId: '2026-W34', xp: 1200 }), 1);
store.record(accepted({ pubkey: key.pubkey, weekId: '2026-W35', xp: 30 }), 2);
expect(store.count()).toBe(1);
// The old week is GONE. This is the privacy property: the relay's disk
// never holds a record of which weeks a person studied.
expect(store.board('domus.eng', '2026-W34', 10).rows).toHaveLength(0);
expect(store.board('domus.eng', '2026-W35', 10).rows).toHaveLength(1);
});
it('accepts a lower XP than last time', () => {
// A learner who moves to a second device legitimately submits a smaller
// number. A monotonicity rule would break that honest case to slow down a
// dishonest one who can simply submit the larger number instead.
const key = keyFor(1);
store.record(accepted({ pubkey: key.pubkey, xp: 900 }), 1);
store.record(accepted({ pubkey: key.pubkey, xp: 120 }), 2);
expect(store.board('domus.eng', '2026-W34', 10).rows[0]?.xp).toBe(120);
});
});
describe('reading a board', () => {
it('ranks by XP, highest first', () => {
store.record(accepted({ pubkey: keyFor(1).pubkey, displayName: 'a', xp: 300 }), 1);
store.record(accepted({ pubkey: keyFor(2).pubkey, displayName: 'b', xp: 900 }), 1);
store.record(accepted({ pubkey: keyFor(3).pubkey, displayName: 'c', xp: 600 }), 1);
const board = store.board('domus.eng', '2026-W34', 10);
expect(board.rows.map((r) => r.displayName)).toEqual(['b', 'c', 'a']);
expect(board.rows.map((r) => r.rank)).toEqual([1, 2, 3]);
});
it('breaks ties deterministically, so ranks do not shuffle between reads', () => {
store.record(accepted({ pubkey: keyFor(1).pubkey, displayName: 'one', xp: 500 }), 1);
store.record(accepted({ pubkey: keyFor(2).pubkey, displayName: 'two', xp: 500 }), 1);
const first = store.board('domus.eng', '2026-W34', 10);
const second = store.board('domus.eng', '2026-W34', 10);
expect(first.rows.map((r) => r.pubkey)).toEqual(second.rows.map((r) => r.pubkey));
});
it('never serves the last-submission timestamp to other participants', () => {
// Stored so the relay can forget rows that stopped arriving; NOT served,
// because "she last studied at 02:14" is a fact about a person's night that
// a leaderboard has no business publishing.
store.record(accepted(), 1_755_600_000_000);
const row = store.board('domus.eng', '2026-W34', 10).rows[0];
expect(row).toBeDefined();
expect(Object.keys(row as object).sort()).toEqual(['displayName', 'pubkey', 'rank', 'xp']);
});
it('shows only the league that was asked for', () => {
store.record(accepted({ pubkey: keyFor(1).pubkey, leagueId: 'domus.eng' }), 1);
store.record(accepted({ pubkey: keyFor(2).pubkey, leagueId: 'somewhere.else' }), 1);
expect(store.board('domus.eng', '2026-W34', 10).rows).toHaveLength(1);
});
it('shows only the week that was asked for, mid-rollover', () => {
// In the first hours of a new week the table holds a mixture: people who
// have submitted, and people who have not and still carry last week's row.
// Filtering is what makes that mixture harmless.
store.record(accepted({ pubkey: keyFor(1).pubkey, weekId: '2026-W34', xp: 5000 }), 1);
store.record(accepted({ pubkey: keyFor(2).pubkey, weekId: '2026-W35', xp: 20 }), 1);
const board = store.board('domus.eng', '2026-W35', 10);
expect(board.rows).toHaveLength(1);
expect(board.rows[0]?.xp).toBe(20);
});
it('honours the row limit', () => {
for (let i = 1; i <= 5; i++) {
store.record(accepted({ pubkey: keyFor(i).pubkey, xp: i * 10 }), 1);
}
expect(store.board('domus.eng', '2026-W34', 3).rows).toHaveLength(3);
});
it('returns an empty board rather than failing, for a league nobody joined', () => {
const board = store.board('nobody.here', '2026-W34', 10);
expect(board.rows).toEqual([]);
expect(board.leagueId).toBe('nobody.here');
});
});
describe('forgetting', () => {
it('drops rows for every week but the one named', () => {
store.record(accepted({ pubkey: keyFor(1).pubkey, weekId: '2026-W33' }), 1);
store.record(accepted({ pubkey: keyFor(2).pubkey, weekId: '2026-W34' }), 1);
store.record(accepted({ pubkey: keyFor(3).pubkey, weekId: '2026-W35' }), 1);
expect(store.forgetOtherWeeks('2026-W34')).toBe(2);
expect(store.count()).toBe(1);
});
});