Browse Source

test(chase): cover retry limit failure path in round generation

Made-with: Cursor
master
npmrun 2 weeks ago
parent
commit
654ce85d2c
  1. 12
      src/game/chase/generator.ts
  2. 33
      tests/chase/chaseGenerator.test.ts

12
src/game/chase/generator.ts

@ -30,6 +30,10 @@ export interface ChaseRound {
};
}
export interface GenerateChaseRoundOptions {
generateAttempt?: (seed: number, difficulty: Difficulty) => ChaseRound;
}
function coordKey(q: number, r: number): CoordKey {
return `${q},${r}`;
}
@ -202,13 +206,17 @@ function generateGraphAttempt(seed: number, difficulty: Difficulty): ChaseRound
};
}
export function generateChaseRound(input: GenerateChaseRoundInput): ChaseRound {
export function generateChaseRound(
input: GenerateChaseRoundInput,
options: GenerateChaseRoundOptions = {},
): ChaseRound {
const baseSeed = normalizeSeed(input.seed);
const minPathLength = minimumPathByDifficulty(input.difficulty);
const generateAttempt = options.generateAttempt ?? generateGraphAttempt;
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt += 1) {
const attemptSeed = normalizeSeed(baseSeed + attempt * 0x9e3779b9);
const round = generateGraphAttempt(attemptSeed, input.difficulty);
const round = generateAttempt(attemptSeed, input.difficulty);
const isPlayable =
isConnected(round.snapshot.graph) &&
round.meta.hasEscapePath &&

33
tests/chase/chaseGenerator.test.ts

@ -90,4 +90,37 @@ describe("chase round generator", () => {
);
}
});
it("throws after retry limit when all attempts are unplayable", () => {
let attemptCount = 0;
const unplayableStrategy = (seed: number, difficulty: Difficulty) => {
attemptCount += 1;
return {
snapshot: {
seed,
difficulty,
graph: { nodes: {}, edgeList: [] },
thiefStartNodeId: "A",
guardStartNodeId: "A",
thiefNodeId: "A",
guardNodeId: "A",
exitNodeId: "A",
status: "playing" as const,
},
meta: {
hasEscapePath: false,
pathLength: 0,
attemptsUsed: 1,
},
};
};
expect(() =>
generateChaseRound(
{ seed: 7, difficulty: "normal" },
{ generateAttempt: unplayableStrategy },
),
).toThrow("Unable to generate playable chase round within retry limit");
expect(attemptCount).toBe(40);
});
});

Loading…
Cancel
Save