✨Feature: implement room entry flow with encounter resolution and logging
This commit is contained in:
115
src/rules/roomEntry.test.ts
Normal file
115
src/rules/roomEntry.test.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||
|
||||
import { expandLevelFromExit, initializeDungeonLevel } from "./dungeon";
|
||||
import { createRoomStateFromTemplate } from "./rooms";
|
||||
import { enterRoom } from "./roomEntry";
|
||||
|
||||
function createSequenceRoller(values: number[]) {
|
||||
let index = 0;
|
||||
|
||||
return () => {
|
||||
const next = values[index];
|
||||
index += 1;
|
||||
return next;
|
||||
};
|
||||
}
|
||||
|
||||
describe("room entry flow", () => {
|
||||
it("marks a newly generated room as entered and resolves an empty encounter", () => {
|
||||
const levelState = initializeDungeonLevel({ content: sampleContentPack });
|
||||
const expanded = expandLevelFromExit({
|
||||
content: sampleContentPack,
|
||||
levelState,
|
||||
fromRoomId: "room.level1.start",
|
||||
exitDirection: "north",
|
||||
roomTableCode: "L1SR",
|
||||
roller: createSequenceRoller([1, 1]),
|
||||
});
|
||||
|
||||
const result = enterRoom({
|
||||
content: sampleContentPack,
|
||||
levelState: expanded.levelState,
|
||||
roomId: expanded.createdRoom.id,
|
||||
at: "2026-03-15T12:00:00.000Z",
|
||||
});
|
||||
|
||||
expect(result.firstEntry).toBe(true);
|
||||
expect(result.encounterResolved).toBe(true);
|
||||
expect(result.room.discovery.entered).toBe(true);
|
||||
expect(result.room.discovery.cleared).toBe(true);
|
||||
expect(result.room.encounter?.resultLabel).toBe("No encounter");
|
||||
expect(result.logEntries.map((entry) => entry.text)).toEqual([
|
||||
"Entered Empty Space.",
|
||||
"Empty Space is quiet.",
|
||||
]);
|
||||
});
|
||||
|
||||
it("resolves a guard encounter once and records the lookup roll", () => {
|
||||
const levelState = initializeDungeonLevel({ content: sampleContentPack });
|
||||
const guardRoom = createRoomStateFromTemplate(
|
||||
sampleContentPack,
|
||||
"room.level1.guard-room",
|
||||
1,
|
||||
"room.level1.normal.guard-room",
|
||||
{ x: 0, y: -1 },
|
||||
);
|
||||
|
||||
levelState.rooms[guardRoom.id] = guardRoom;
|
||||
levelState.discoveredRoomOrder.push(guardRoom.id);
|
||||
|
||||
const result = enterRoom({
|
||||
content: sampleContentPack,
|
||||
levelState,
|
||||
roomId: guardRoom.id,
|
||||
roller: createSequenceRoller([6]),
|
||||
at: "2026-03-15T12:00:00.000Z",
|
||||
});
|
||||
|
||||
expect(result.room.templateId).toBe("room.level1.normal.guard-room");
|
||||
expect(result.lookup?.entry.label).toBe("Guard and Warrior");
|
||||
expect(result.room.encounter?.creatureNames).toEqual(["Guard", "Warrior"]);
|
||||
expect(result.room.discovery.cleared).toBe(false);
|
||||
expect(result.logEntries.map((entry) => entry.type)).toEqual(["room", "roll", "room"]);
|
||||
expect(result.logEntries[1]?.text).toContain("Guard and Warrior");
|
||||
});
|
||||
|
||||
it("does not reroll an encounter when re-entering a room", () => {
|
||||
const levelState = initializeDungeonLevel({ content: sampleContentPack });
|
||||
const guardRoom = createRoomStateFromTemplate(
|
||||
sampleContentPack,
|
||||
"room.level1.guard-room",
|
||||
1,
|
||||
"room.level1.normal.guard-room",
|
||||
{ x: 0, y: -1 },
|
||||
);
|
||||
|
||||
levelState.rooms[guardRoom.id] = guardRoom;
|
||||
levelState.discoveredRoomOrder.push(guardRoom.id);
|
||||
|
||||
const firstEntry = enterRoom({
|
||||
content: sampleContentPack,
|
||||
levelState,
|
||||
roomId: guardRoom.id,
|
||||
roller: createSequenceRoller([4]),
|
||||
at: "2026-03-15T12:00:00.000Z",
|
||||
});
|
||||
const secondEntry = enterRoom({
|
||||
content: sampleContentPack,
|
||||
levelState: firstEntry.levelState,
|
||||
roomId: guardRoom.id,
|
||||
roller: createSequenceRoller([6]),
|
||||
at: "2026-03-15T12:05:00.000Z",
|
||||
});
|
||||
|
||||
expect(firstEntry.room.encounter?.resultLabel).toBe("Guard");
|
||||
expect(secondEntry.room.encounter?.resultLabel).toBe("Guard");
|
||||
expect(secondEntry.lookup).toBeUndefined();
|
||||
expect(secondEntry.firstEntry).toBe(false);
|
||||
expect(secondEntry.logEntries.map((entry) => entry.text)).toEqual([
|
||||
"Re-entered Guard Room.",
|
||||
"Encounter in Guard Room: Guard.",
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user