63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
/**
|
|
* Test fixtures for the relay.
|
|
*
|
|
* These sign with `@noble/curves` and `submissionBytes` from `@ludus/schema` —
|
|
* which is the SAME function the player signs with, because `@ludus/identity`
|
|
* re-exports it. So the canonical encoding under test here is the real one, not
|
|
* a restatement of it, and a change to the field order breaks both sides at
|
|
* once instead of silently rejecting every honest submission.
|
|
*
|
|
* No test here touches a real key, a real relay or anyone's data. Every database
|
|
* is `:memory:` and every socket is on `127.0.0.1` with a port the OS chose.
|
|
*/
|
|
import { ed25519 } from '@noble/curves/ed25519.js';
|
|
import { submissionBytes, type LeagueSubmission } from '@ludus/schema';
|
|
|
|
export function toBase64Url(bytes: Uint8Array): string {
|
|
return Buffer.from(bytes).toString('base64url');
|
|
}
|
|
|
|
export interface TestKey {
|
|
readonly privateKey: Uint8Array;
|
|
readonly pubkey: string;
|
|
}
|
|
|
|
/**
|
|
* A deterministic keypair from a one-byte label.
|
|
*
|
|
* Deterministic on purpose: a failing assertion names the same key every run,
|
|
* and `keyFor(1)` always sorts the same way against `keyFor(2)` — which the
|
|
* board's tie-break test depends on.
|
|
*/
|
|
export function keyFor(label: number): TestKey {
|
|
const seed = new Uint8Array(32);
|
|
seed.fill(label);
|
|
return { privateKey: seed, pubkey: toBase64Url(ed25519.getPublicKey(seed)) };
|
|
}
|
|
|
|
export interface SubmissionOverrides {
|
|
readonly displayName?: string;
|
|
readonly leagueId?: string;
|
|
readonly weekId?: string;
|
|
readonly xp?: number;
|
|
}
|
|
|
|
/** A correctly signed submission. */
|
|
export function signedSubmission(
|
|
key: TestKey,
|
|
overrides: SubmissionOverrides = {}
|
|
): LeagueSubmission {
|
|
const fields = {
|
|
pubkey: key.pubkey,
|
|
displayName: overrides.displayName ?? 'tereza',
|
|
leagueId: overrides.leagueId ?? 'domus.eng',
|
|
weekId: overrides.weekId ?? '2026-W34',
|
|
xp: overrides.xp ?? 1240
|
|
};
|
|
const sig = ed25519.sign(submissionBytes(fields), key.privateKey);
|
|
return { ...fields, sig: toBase64Url(sig) };
|
|
}
|
|
|
|
/** Epoch ms inside ISO week 2026-W34 (Wednesday 2026-08-19, midday UTC). */
|
|
export const IN_WEEK_34 = Date.UTC(2026, 7, 19, 12, 0, 0);
|