import { content } from "@deadhead/content"; import { describe, expect, it } from "vitest"; import { makeOwner, setCredits, useTestDatabase } from "./db"; import { call, getState, putBoard, signUp, testContract, useTestApp } from "./game"; const database = useTestDatabase(); const testApp = useTestApp(database); describe("concurrency and money safety", () => { it("lets only one of two simultaneous purchases through when only one is affordable", async () => { const { app } = testApp(); const guest = await signUp(app); const price = content.shipModel("courier")!.price; await setCredits(database().db, guest.playerId, price + price / 2); const responses = await Promise.all([ call(app, guest, "POST", "/api/ships", { modelId: "courier" }), call(app, guest, "POST", "/api/ships", { modelId: "courier" }), ]); expect(responses.map((r) => r.statusCode).sort()).toEqual([200, 409]); const state = await getState(app, guest); expect(state.ships).toHaveLength(1); expect(state.company.credits).toBe(price / 2); }); it("never goes negative under many simultaneous purchases", async () => { const { app } = testApp(); const guest = await signUp(app); const price = content.shipModel("courier")!.price; await setCredits(database().db, guest.playerId, price * 3); const responses = await Promise.all( Array.from({ length: 8 }, () => call(app, guest, "POST", "/api/ships", { modelId: "courier" }), ), ); expect(responses.filter((r) => r.statusCode === 200)).toHaveLength(3); const state = await getState(app, guest); expect(state.ships).toHaveLength(3); expect(state.company.credits).toBe(0); }); it("accepts a contract only once when dispatched twice at the same moment", async () => { const { app } = testApp(); const { db } = database(); const guest = await signUp(app); const shipId = await makeOwner(db, guest.playerId, guest.avatarId); await setCredits(db, guest.playerId, 10_000); const contract = testContract(); await putBoard(db, guest.playerId, [contract]); const body = { kind: "contract", contractId: contract.id, shipId, pilotId: guest.avatarId }; const responses = await Promise.all([ call(app, guest, "POST", "/api/dispatches", body), call(app, guest, "POST", "/api/dispatches", body), ]); expect(responses.map((r) => r.statusCode).sort()).toEqual([200, 409]); const state = await getState(app, guest); expect(state.activeDispatches).toHaveLength(1); expect(state.company.credits).toBe(10_000 - state.activeDispatches[0]!.fuelCost); }); it("leases one bay when the same lease is sent twice at once", async () => { const { app } = testApp(); const guest = await signUp(app); await setCredits(database().db, guest.playerId, 100_000); const responses = await Promise.all([ call(app, guest, "POST", "/api/bays", { locationId: "moon" }), call(app, guest, "POST", "/api/bays", { locationId: "moon" }), ]); expect(responses.map((r) => r.statusCode).sort()).toEqual([200, 409]); const state = await getState(app, guest); expect(state.bays).toHaveLength(1); expect(state.company.credits).toBe(100_000 - content.location("moon")!.bayLeasePrice); }); });