deadhead-space-freight-tycoon/packages/engine/test/ported/lease-bay.test.ts

74 lines
3.1 KiB
TypeScript

// Ported from apps/server/test (CR-014 Step 14.5): now runs on the engine.
import { content } from "@deadhead/content";
import { bayResponseSchema, errorResponseSchema } from "@deadhead/schema";
import { describe, expect, it } from "vitest";
import { useTestApp, signUp, call, getState, setCredits, signUpOwner } from "../support";
const testApp = useTestApp();
const errorCode = (response: { json(): unknown }) =>
errorResponseSchema.parse(response.json()).error.code;
describe("lease a bay", () => {
it("leases a bay at the Moon and deducts the price", async () => {
const { app } = testApp();
const owner = await signUpOwner(app);
const price = content.location("moon")!.bayLeasePrice;
await setCredits(testApp().app, price + 100);
const response = await call(app, owner, "POST", "/api/bays", { locationId: "moon" });
expect(response.statusCode).toBe(200);
const { bay } = bayResponseSchema.parse(response.json());
expect(bay.locationId).toBe("moon");
const state = await getState(app, owner);
expect(state.company.credits).toBe(100);
expect(state.bays.map((b) => b.locationId).sort()).toEqual(["earth", "moon"]);
});
it("refuses an undiscovered location", async () => {
const { app } = testApp();
const owner = await signUpOwner(app);
await setCredits(testApp().app, 1_000_000);
const response = await call(app, owner, "POST", "/api/bays", { locationId: "mars" });
expect(response.statusCode).toBe(409);
expect(errorCode(response)).toBe("location-undiscovered");
expect((await getState(app, owner)).company.credits).toBe(1_000_000);
});
it("refuses a second bay at the same location", async () => {
const { app } = testApp();
const owner = await signUpOwner(app);
await setCredits(testApp().app, 1_000_000);
const response = await call(app, owner, "POST", "/api/bays", { locationId: "earth" });
expect(response.statusCode).toBe(409);
expect(errorCode(response)).toBe("bay-already-leased");
const state = await getState(app, owner);
expect(state.bays).toHaveLength(1);
expect(state.company.credits).toBe(1_000_000);
});
it("refuses when the player cannot afford it, and unknown locations", async () => {
const { app } = testApp();
const owner = await signUpOwner(app);
await setCredits(testApp().app, 0);
expect(errorCode(await call(app, owner, "POST", "/api/bays", { locationId: "moon" }))).toBe(
"insufficient-funds",
);
const unknown = await call(app, owner, "POST", "/api/bays", { locationId: "pluto" });
expect(unknown.statusCode).toBe(404);
});
it("returns tutorial-active during the tutorial and changes nothing", async () => {
const { app } = testApp();
const guest = await signUp(app);
await setCredits(testApp().app, 1_000_000);
const response = await call(app, guest, "POST", "/api/bays", { locationId: "earth" });
expect(response.statusCode).toBe(409);
expect(errorCode(response)).toBe("tutorial-active");
const state = await getState(app, guest);
expect(state.bays).toEqual([]);
expect(state.company.credits).toBe(1_000_000);
});
});