deadhead-space-freight-tycoon/apps/server/test/reposition.test.ts

56 lines
2.2 KiB
TypeScript

import { dispatchResponseSchema, errorResponseSchema } from "@deadhead/schema";
import { describe, expect, it } from "vitest";
import { setCredits, useTestDatabase } from "./db";
import { call, getState, signUpOwner, useTestApp } from "./game";
const database = useTestDatabase();
const testApp = useTestApp(database);
/** An owner with only the Earth bay, a crewed Courier at Earth and 10,000 credits. */
async function owner() {
const guest = await signUpOwner(testApp().app, database().db);
await setCredits(database().db, guest.playerId, 10_000);
return guest;
}
describe("deadhead dispatch", () => {
it("flies the ship empty to a discovered location without a bay, costing only fuel", async () => {
const { app, clock } = testApp();
const guest = await owner();
const response = await call(app, guest, "POST", "/api/dispatches", {
kind: "reposition",
shipId: guest.shipId,
pilotId: guest.avatarId,
destinationId: "moon",
});
expect(response.statusCode).toBe(200);
const { dispatch } = dispatchResponseSchema.parse(response.json());
expect(dispatch).toMatchObject({ contract: null, expectedPayout: 0, destinationId: "moon" });
expect(dispatch.fuelCost).toBeGreaterThan(0);
clock.now = dispatch.arrivalAt;
const state = await getState(app, guest);
expect(state.bays.map((b) => b.locationId)).toEqual(["earth"]);
expect(state.ships[0]).toMatchObject({ status: "idle", locationId: "moon" });
expect(state.pilots[0]).toMatchObject({ status: "idle", locationId: "moon", xp: 0 });
expect(state.company.credits).toBe(10_000 - dispatch.fuelCost);
});
it("refuses an undiscovered destination and changes nothing", async () => {
const { app } = testApp();
const guest = await owner();
const response = await call(app, guest, "POST", "/api/dispatches", {
kind: "reposition",
shipId: guest.shipId,
pilotId: guest.avatarId,
destinationId: "mars",
});
expect(response.statusCode).toBe(409);
expect(errorResponseSchema.parse(response.json()).error.code).toBe("location-undiscovered");
const state = await getState(app, guest);
expect(state.company.credits).toBe(10_000);
expect(state.ships[0]!.status).toBe("idle");
});
});