import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { credentialMatches, createCredentialToken, hashCredentialToken, InMemorySeatCredentialStore, JsonFileSeatCredentialStore, } from "./credentials.js"; describe("seat credentials", () => { it("hashes bearer tokens and round-trips credential records through JSON storage", async () => { const inviteToken = createCredentialToken(); const seatToken = createCredentialToken(); const record = { matchId: "credential-match", inviteTokenHash: hashCredentialToken(inviteToken), hostTokenHash: hashCredentialToken(createCredentialToken()), seats: [{ player: "alice", tokenHash: hashCredentialToken(seatToken) }], } as const; expect(credentialMatches(inviteToken, record.inviteTokenHash)).toBe(true); expect(credentialMatches("wrong-token", record.inviteTokenHash)).toBe(false); const memory = new InMemorySeatCredentialStore(); await memory.save(record.matchId, record); expect(await memory.load(record.matchId)).toEqual(record); const directory = await mkdtemp(join(tmpdir(), "economancy-credentials-")); try { const files = new JsonFileSeatCredentialStore(directory); await files.save(record.matchId, record); expect(await files.load(record.matchId)).toEqual(record); expect(await files.load("missing-match")).toBeUndefined(); } finally { await rm(directory, { recursive: true, force: true }); } }); });