68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import { acknowledgeSummariesResponseSchema } from "@deadhead/schema";
|
|
import { describe, expect, it } from "vitest";
|
|
import { makeOwner, setCredits, useTestDatabase } from "./db";
|
|
import { call, getState, insertDispatch, signUp, useTestApp } from "./game";
|
|
|
|
const database = useTestDatabase();
|
|
const testApp = useTestApp(database);
|
|
|
|
describe("acknowledging summaries", () => {
|
|
it("clears unacknowledged summaries while keeping the resolved effects", async () => {
|
|
const { app, clock } = testApp();
|
|
const { db } = database();
|
|
const guest = await signUp(app);
|
|
const { shipId } = await makeOwner(db, guest.playerId, guest.avatarId);
|
|
await setCredits(db, guest.playerId, 1000);
|
|
const record = await insertDispatch(db, {
|
|
playerId: guest.playerId,
|
|
shipId,
|
|
pilotId: guest.avatarId,
|
|
departAt: clock.now,
|
|
});
|
|
clock.now = record.arrivalAt;
|
|
|
|
const before = await getState(app, guest);
|
|
expect(before.summaries.map((s) => s.dispatchId)).toEqual([record.id]);
|
|
|
|
const response = await call(app, guest, "POST", "/api/summaries/acknowledge", {
|
|
dispatchIds: [record.id, "not-a-real-dispatch"],
|
|
});
|
|
expect(response.statusCode).toBe(200);
|
|
expect(acknowledgeSummariesResponseSchema.parse(response.json())).toEqual({ acknowledged: 1 });
|
|
|
|
const after = await getState(app, guest);
|
|
expect(after.summaries).toEqual([]);
|
|
expect(after.company.credits).toBe(before.company.credits);
|
|
expect(after.pilots[0]!.xp).toBe(before.pilots[0]!.xp);
|
|
expect(after.ships[0]!.locationId).toBe("moon");
|
|
|
|
// Acknowledging again changes nothing.
|
|
const again = await call(app, guest, "POST", "/api/summaries/acknowledge", {
|
|
dispatchIds: [record.id],
|
|
});
|
|
expect(again.json()).toEqual({ acknowledged: 0 });
|
|
});
|
|
|
|
it("does not acknowledge another player's dispatches", async () => {
|
|
const { app, clock } = testApp();
|
|
const { db } = database();
|
|
const owner = await signUp(app);
|
|
const { shipId } = await makeOwner(db, owner.playerId, owner.avatarId);
|
|
const record = await insertDispatch(db, {
|
|
playerId: owner.playerId,
|
|
shipId,
|
|
pilotId: owner.avatarId,
|
|
departAt: clock.now,
|
|
});
|
|
clock.now = record.arrivalAt;
|
|
await getState(app, owner);
|
|
|
|
const stranger = await signUp(app);
|
|
const response = await call(app, stranger, "POST", "/api/summaries/acknowledge", {
|
|
dispatchIds: [record.id],
|
|
});
|
|
expect(response.json()).toEqual({ acknowledged: 0 });
|
|
expect((await getState(app, owner)).summaries).toHaveLength(1);
|
|
});
|
|
});
|