Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import { Readable } from "node:stream";import type { IncomingMessage, ServerResponse } from "node:http";import { describe, expect, it } from "vitest";import { EconomancyMatchService, handleEconomancyRequest } from "./server.js";import { createEconomancyMatch, InMemoryMatchStore, persistEconomancyMatch } from "@instlang/economancy-host";
describe("economancy HTTP server", () => { it("creates matches, exposes player views, accepts commands, and returns public events", async () => { const service = new EconomancyMatchService({ store: new InMemoryMatchStore() });
const created = await request(service, "POST", "/matches", { matchId: "web-match", players: ["alice", "bob"], player: "alice" }); expect(created.status).toBe(201); const credentials = created.body as { view: { matchId: string }; inviteToken: string; hostToken: string; seatToken: string }; expect(credentials.view.matchId).toBe("web-match"); expect(credentials.inviteToken).not.toBe(credentials.seatToken); expect(credentials.inviteToken).not.toBe(credentials.hostToken);
await expect(service.getView("web-match", undefined)).rejects.toThrow("seat_token_required");
const playerView = await request(service, "GET", "/matches/web-match", undefined, bearer(credentials.seatToken)); expect(playerView.status).toBe(200); expect((playerView.body as { view: { player: string } }).view.player).toBe("alice");
const joined = await request(service, "POST", "/matches/web-match/join", { player: "bob", inviteToken: credentials.inviteToken }); expect(joined.status).toBe(200); const bobCredentials = joined.body as { seatToken: string; player: string }; expect(bobCredentials.player).toBe("bob");
await expect(service.dispatch("web-match", { commandId: "web-wrong-host-token", actor: "system", command: { kind: "advance_income" } }, credentials.inviteToken)).rejects.toThrow("seat_token_invalid"); const advanced = await request(service, "POST", "/matches/web-match/commands", { commandId: "web-income", actor: "system", command: { kind: "advance_income" } }, bearer(credentials.hostToken)); expect(advanced.status).toBe(200); expect((advanced.body as { kind: string; revision: number }).kind).toBe("accepted");
await expect(service.dispatch("web-match", { commandId: "web-impersonation", actor: "bob", command: { kind: "submit_investment_commitment", commitment: { investmentId: "investment-1", player: "bob", commitment: `sha256:${"0".repeat(64)}` } } }, credentials.seatToken)).rejects.toThrow("seat_actor_mismatch");
const events = await request(service, "GET", "/matches/web-match/events?since=0"); expect(events.status).toBe(200); expect((events.body as { events: readonly { event: { kind: string } }[] }).events.map((event) => event.event.kind)).toEqual(["income_advanced"]);
const caughtUp = await request(service, "GET", "/matches/web-match/events?since=1"); expect(caughtUp.status).toBe(200); expect((caughtUp.body as { events: readonly unknown[] }).events).toEqual([]); });
it("reports persisted matches created before seat credentials existed", async () => { const store = new InMemoryMatchStore(); const created = createEconomancyMatch("legacy-match", ["alice", "bob"]); expect(created.kind).toBe("accepted"); if (created.kind !== "accepted") return; const persisted = persistEconomancyMatch(created.session); expect(persisted.kind).toBe("accepted"); if (persisted.kind !== "accepted") return; await store.save("legacy-match", persisted.serialized);
const service = new EconomancyMatchService({ store }); await expect(service.getView("legacy-match", "legacy-token")).rejects.toThrow("match_credentials_missing"); });});
function bearer(token: string): { readonly headers: { readonly authorization: string } } { return { headers: { authorization: `Bearer ${token}` } };}
async function request(service: EconomancyMatchService, method: string, url: string, body?: unknown, options: { readonly headers?: Record<string, string> } = {}): Promise<{ status: number; body: unknown }> { const request = Readable.from(body === undefined ? [] : [JSON.stringify(body)]) as unknown as IncomingMessage; Object.assign(request, { method, url, headers: options.headers ?? {} }); let status = 200; let serialized = ""; const response = { statusCode: status, setHeader: () => undefined, end: (value?: string) => { serialized = value ?? ""; }, } as unknown as ServerResponse; Object.defineProperty(response, "statusCode", { get: () => status, set: (value: number) => { status = value; }, }); await handleEconomancyRequest(service, request, response); return { status, body: JSON.parse(serialized) as unknown };}