64 lines
2.7 KiB
TypeScript
64 lines
2.7 KiB
TypeScript
import { content } from "@deadhead/content";
|
|
import { bayResponseSchema, errorResponseSchema } from "@deadhead/schema";
|
|
import { describe, expect, it } from "vitest";
|
|
import { setCredits, useTestDatabase } from "./db";
|
|
import { call, getState, signUp, useTestApp } from "./game";
|
|
|
|
const database = useTestDatabase();
|
|
const testApp = useTestApp(database);
|
|
|
|
const errorCode = (response: { json(): unknown }) =>
|
|
errorResponseSchema.parse(response.json()).error.code;
|
|
|
|
describe("lease a bay", () => {
|
|
it("leases a bay at Earth and deducts the price", async () => {
|
|
const { app } = testApp();
|
|
const guest = await signUp(app);
|
|
const price = content.location("earth")!.bayLeasePrice;
|
|
await setCredits(database().db, guest.playerId, price + 100);
|
|
|
|
const response = await call(app, guest, "POST", "/api/bays", { locationId: "earth" });
|
|
expect(response.statusCode).toBe(200);
|
|
const { bay } = bayResponseSchema.parse(response.json());
|
|
expect(bay.locationId).toBe("earth");
|
|
|
|
const state = await getState(app, guest);
|
|
expect(state.company.credits).toBe(100);
|
|
expect(state.bays).toEqual([bay]);
|
|
});
|
|
|
|
it("refuses an undiscovered location", async () => {
|
|
const { app } = testApp();
|
|
const guest = await signUp(app);
|
|
await setCredits(database().db, guest.playerId, 1_000_000);
|
|
const response = await call(app, guest, "POST", "/api/bays", { locationId: "mars" });
|
|
expect(response.statusCode).toBe(409);
|
|
expect(errorCode(response)).toBe("location-undiscovered");
|
|
expect((await getState(app, guest)).company.credits).toBe(1_000_000);
|
|
});
|
|
|
|
it("refuses a second bay at the same location", async () => {
|
|
const { app } = testApp();
|
|
const guest = await signUp(app);
|
|
await setCredits(database().db, guest.playerId, 1_000_000);
|
|
await call(app, guest, "POST", "/api/bays", { locationId: "moon" });
|
|
const response = await call(app, guest, "POST", "/api/bays", { locationId: "moon" });
|
|
expect(response.statusCode).toBe(409);
|
|
expect(errorCode(response)).toBe("bay-already-leased");
|
|
const state = await getState(app, guest);
|
|
expect(state.bays).toHaveLength(1);
|
|
expect(state.company.credits).toBe(1_000_000 - content.location("moon")!.bayLeasePrice);
|
|
});
|
|
|
|
it("refuses when the player cannot afford it, and unknown locations", async () => {
|
|
const { app } = testApp();
|
|
const guest = await signUp(app);
|
|
await setCredits(database().db, guest.playerId, 0);
|
|
expect(errorCode(await call(app, guest, "POST", "/api/bays", { locationId: "earth" }))).toBe(
|
|
"insufficient-funds",
|
|
);
|
|
const unknown = await call(app, guest, "POST", "/api/bays", { locationId: "pluto" });
|
|
expect(unknown.statusCode).toBe(404);
|
|
});
|
|
});
|