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

61 lines
2.5 KiB
TypeScript

import { content } from "@deadhead/content";
import { eq } from "drizzle-orm";
import { describe, expect, it } from "vitest";
import { bays, companies, discoveredLocations, pilots, players, ships } from "../src/db/schema";
import { seedPlayer, useTestDatabase } from "./db";
const database = useTestDatabase();
describe("test database harness", () => {
it("isolates tests: first test sees only its own player", async () => {
const { db } = database();
await seedPlayer(db, "tutorial");
expect(await db.select().from(players)).toHaveLength(1);
});
it("isolates tests: second test also sees only its own player", async () => {
const { db } = database();
await seedPlayer(db, "tutorial");
expect(await db.select().from(players)).toHaveLength(1);
});
it("seeds a new tutorial player", async () => {
const { db } = database();
const { playerId, avatarId } = await seedPlayer(db, "tutorial");
const [player] = await db.select().from(players).where(eq(players.id, playerId));
const [company] = await db.select().from(companies).where(eq(companies.playerId, playerId));
const discovered = await db
.select()
.from(discoveredLocations)
.where(eq(discoveredLocations.playerId, playerId));
const [avatar] = await db.select().from(pilots).where(eq(pilots.id, avatarId));
expect(player).toMatchObject({ tutorialActive: true, loanerLocationId: "earth" });
expect(company!.credits).toBe(content.config.startingCredits);
expect(discovered.map((d) => d.locationId).sort()).toEqual(["earth", "moon"]);
expect(avatar).toMatchObject({ isAvatar: true, xp: 0, assignedShipId: null });
expect(await db.select().from(ships)).toHaveLength(0);
});
it("seeds an owner with one crewed Courier and an Earth bay", async () => {
const { db } = database();
const { playerId, avatarId, shipId } = await seedPlayer(db, "owner", { credits: 777 });
const [player] = await db.select().from(players).where(eq(players.id, playerId));
const [avatar] = await db.select().from(pilots).where(eq(pilots.id, avatarId));
expect(player!.tutorialActive).toBe(false);
expect(await db.select().from(ships)).toEqual([
expect.objectContaining({
id: shipId,
modelId: "courier",
locationId: "earth",
status: "idle",
}),
]);
expect(avatar!.assignedShipId).toBe(shipId);
expect((await db.select().from(bays)).map((b) => b.locationId)).toEqual(["earth"]);
const [company] = await db.select().from(companies);
expect(company!.credits).toBe(777);
});
});