The mailroom is no longer the whole game. Around turn 50, a player who has
built standing and either Marlene's goodwill or the nerve to ask gets offered
the Dispatch post — and can take it, take it and put Trevor up for the mailroom,
or turn it down, which is a real strategy rather than a mistake and comes back
after a cooldown.
Dispatch is eleven events of its own. The money problem is largely solved up
there and replaced by other people's days depending on yours, including the
mailroom's — which you used to be. Trevor branches on whether you recommended
him: he either runs the mailroom and has opinions about how it used to be run,
or he stays put and starts addressing you by your full title, kindly, in front
of people.
None of this is a promotion mechanic in the engine. It is
`{ path: 'stage', op: 'set', value: 'dispatch' }` on an option, since `stage`
was already how events are filtered. The reserved-write rule now allows it
narrowly — `set` only, and only to a stage that has events, both checked by the
validator, so a typo cannot strand the player in an empty pool. Wages differ by
rank through two upkeep entries gated on `stage`. The hard-times events dropped
their stage entirely: rent is due wherever you work.
The second half of this is the thing the last playtest asked for. A 183-turn
session, and no way to see how much of it was new ground.
`npm run analyze <exported-log.json>` now reports turns against distinct
situations, how often each recurred, which options were most used, which locked
gates players kept meeting, and anything they typed. The retrospective shows the
player-facing version.
It found a regression in this very change on its first run. Dispatch started
with eight events against the mailroom's nineteen, so its two floor events were
46% of every promoted run — one every three turns. Promotion was moving the
player into *thinner* content. Three more events and a weight rebalance took the
top two to 24.5%, and the heaviest recurrence from every ~3 turns to every ~5.
A test now holds that line.
Also corrected: the coverage test was asking whether an archetype ever *took* an
option, which is a property of the bot, not the pack. It had flagged
`rent_bounced.ask_trevor` as dead content when it had been offered, unlocked,
419 times across a sweep and simply never chosen. Options are now checked on
availability; events are still checked on firing.
114 tests. Reasoning in docs/DECISIONS.md §22-24.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VMSFHyVPitUoosW5wyEADj
283 lines
12 KiB
JavaScript
283 lines
12 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { startGame, currentTurn, takeTurn, MoveError } from '../src/engine/game.js';
|
|
import { getPath, setPath } from '../src/engine/paths.js';
|
|
import { testPack } from './fixtures/test-pack.js';
|
|
|
|
/** Play `turns` turns, always taking the first available option. */
|
|
function playThrough(seed, turns, chooser = (options) => options.find((o) => o.available).option.id) {
|
|
let state = startGame(testPack, { seed });
|
|
const records = [];
|
|
for (let i = 0; i < turns; i++) {
|
|
const turn = currentTurn(testPack, state);
|
|
const result = takeTurn(testPack, state, chooser(turn.options, turn.event, state));
|
|
state = result.state;
|
|
records.push(result.record);
|
|
}
|
|
return { state, records };
|
|
}
|
|
|
|
test('a new game starts on turn one with the pack defaults', () => {
|
|
const state = startGame(testPack, { seed: 'start' });
|
|
assert.equal(state.turn, 1);
|
|
assert.equal(state.stage, 'floor');
|
|
assert.equal(state.money, 100);
|
|
assert.equal(state.skills.talking, 1);
|
|
assert.equal(state.relationships.chief.likes_you, 50);
|
|
assert.equal(state.relationships.pat.fears_you, 50);
|
|
assert.ok(state.meta.currentEventId, 'an event is waiting before the first turn');
|
|
});
|
|
|
|
test('relationship state is generated per character, so adding one adds its stats', () => {
|
|
const state = startGame(testPack, { seed: 'chars' });
|
|
assert.deepEqual(Object.keys(state.relationships).sort(), ['chief', 'pat']);
|
|
assert.deepEqual(Object.keys(state.relationships.chief).sort(), ['fears_you', 'likes_you']);
|
|
});
|
|
|
|
test('taking a turn applies effects and advances the clock', () => {
|
|
let state = startGame(testPack, { seed: 'turn' });
|
|
const before = state.money;
|
|
const result = takeTurn(testPack, state, currentTurn(testPack, state).options[0].option.id);
|
|
assert.equal(result.state.turn, 2);
|
|
assert.notEqual(result.state.meta.currentEventId, undefined);
|
|
assert.ok(result.state.money !== before || result.record.changes.length >= 0);
|
|
});
|
|
|
|
test('the same seed and the same choices replay identically', () => {
|
|
const a = playThrough('replay', 40);
|
|
const b = playThrough('replay', 40);
|
|
assert.deepEqual(a.state, b.state);
|
|
assert.deepEqual(a.records, b.records);
|
|
});
|
|
|
|
test('different seeds produce different runs', () => {
|
|
const a = playThrough('seed-a', 40);
|
|
const b = playThrough('seed-b', 40);
|
|
assert.notDeepEqual(a.records.map((r) => r.eventId), b.records.map((r) => r.eventId));
|
|
});
|
|
|
|
test('a saved game resumes into exactly the run it left', () => {
|
|
const uninterrupted = playThrough('save', 30);
|
|
|
|
let state = startGame(testPack, { seed: 'save' });
|
|
for (let i = 0; i < 15; i++) {
|
|
const turn = currentTurn(testPack, state);
|
|
state = takeTurn(testPack, state, turn.options.find((o) => o.available).option.id).state;
|
|
}
|
|
// The whole save is one JSON round trip; nothing lives outside it.
|
|
state = JSON.parse(JSON.stringify(state));
|
|
for (let i = 0; i < 15; i++) {
|
|
const turn = currentTurn(testPack, state);
|
|
state = takeTurn(testPack, state, turn.options.find((o) => o.available).option.id).state;
|
|
}
|
|
assert.deepEqual(state, uninterrupted.state);
|
|
});
|
|
|
|
test('a once-only event fires exactly once across a long run', () => {
|
|
const { records } = playThrough('once', 60);
|
|
assert.equal(records.filter((r) => r.eventId === 'first_day').length, 1);
|
|
assert.equal(records[0].eventId, 'first_day', 'its weight makes it the opener');
|
|
});
|
|
|
|
test('a locked option cannot be played', () => {
|
|
const state = startGame(testPack, { seed: 'locked' });
|
|
let probe = state;
|
|
// Walk to a turn that offers the gated option.
|
|
for (let i = 0; i < 10; i++) {
|
|
const turn = currentTurn(testPack, probe);
|
|
const charm = turn.options.find((o) => o.option.id === 'charm');
|
|
if (charm) {
|
|
assert.equal(charm.available, false);
|
|
assert.throws(() => takeTurn(testPack, probe, 'charm'), MoveError);
|
|
return;
|
|
}
|
|
probe = takeTurn(testPack, probe, turn.options.find((o) => o.available).option.id).state;
|
|
}
|
|
assert.fail('the gated option never came up');
|
|
});
|
|
|
|
test('an unknown option id is rejected', () => {
|
|
const state = startGame(testPack, { seed: 'bogus' });
|
|
assert.throws(() => takeTurn(testPack, state, 'no_such_option'), /has no option/);
|
|
});
|
|
|
|
test('a skill grows from choices and eventually unlocks its gate', () => {
|
|
const talkative = (options) => {
|
|
const practice = options.find((o) => o.option.id === 'practice' && o.available);
|
|
return (practice ?? options.find((o) => o.available)).option.id;
|
|
};
|
|
const { state } = playThrough('skill', 20, talkative);
|
|
assert.ok(state.skills.talking >= 3, `expected talking to grow, got ${state.skills.talking}`);
|
|
});
|
|
|
|
test('money going negative brings the hard-times branch into the pool', () => {
|
|
const spendy = (options) => {
|
|
const practice = options.find((o) => o.option.id === 'practice' && o.available);
|
|
return (practice ?? options.find((o) => o.available)).option.id;
|
|
};
|
|
let state = startGame(testPack, { seed: 'broke' });
|
|
let sawHardTimes = false;
|
|
for (let i = 0; i < 120; i++) {
|
|
const turn = currentTurn(testPack, state);
|
|
if (turn.event.id === 'hard_times') { sawHardTimes = true; break; }
|
|
state = takeTurn(testPack, state, spendy(turn.options, turn.event, state)).state;
|
|
}
|
|
assert.ok(state.money < 0 || sawHardTimes, 'the run should have gone broke');
|
|
assert.ok(sawHardTimes, 'hard times should surface once money is negative — the game never ends');
|
|
});
|
|
|
|
test('the turn record carries the diff and the options the player could not take', () => {
|
|
let state = startGame(testPack, { seed: 'record' });
|
|
state = takeTurn(testPack, state, currentTurn(testPack, state).options[0].option.id).state;
|
|
|
|
const turn = currentTurn(testPack, state);
|
|
const { record } = takeTurn(testPack, state, 'work');
|
|
|
|
assert.equal(record.eventId, turn.event.id);
|
|
assert.equal(record.optionId, 'work');
|
|
assert.deepEqual(record.changes.map((c) => c.path), ['money', 'relationships.chief.likes_you']);
|
|
assert.equal(record.changes[0].from + 10, record.changes[0].to);
|
|
|
|
const locked = record.offered.find((o) => o.id === 'charm');
|
|
assert.equal(locked.available, false);
|
|
assert.equal(locked.failed[0].path, 'skills.talking');
|
|
});
|
|
|
|
test('history accumulates every turn and is what the retrospective reads', () => {
|
|
const { state, records } = playThrough('history', 12);
|
|
assert.equal(state.history.length, 12);
|
|
assert.deepEqual(state.history, records);
|
|
});
|
|
|
|
test('a stat cannot be pushed past its declared bound', () => {
|
|
const { state } = playThrough('clamp', 80, (options) => {
|
|
const practice = options.find((o) => o.option.id === 'practice' && o.available);
|
|
return (practice ?? options.find((o) => o.available)).option.id;
|
|
});
|
|
assert.ok(state.skills.talking <= 10, 'talking is capped at 10');
|
|
assert.ok(state.relationships.pat.likes_you <= 100, 'relationship stats are capped at 100');
|
|
});
|
|
|
|
test('the run never ends on its own', () => {
|
|
const { state } = playThrough('endless', 200);
|
|
assert.equal(state.turn, 201);
|
|
assert.ok(currentTurn(testPack, state).options.some((o) => o.available), 'there is always something to do');
|
|
});
|
|
|
|
test('upkeep runs every turn and is recorded separately from the choice', () => {
|
|
const upkeepPack = structuredClone(testPack);
|
|
upkeepPack.upkeep = [
|
|
{ label: 'wage', effects: [{ path: 'money', op: 'add', value: 20 }] },
|
|
{
|
|
label: 'debt service',
|
|
requires: [{ path: 'flags.in_debt', op: '==', value: true }],
|
|
effects: [{ path: 'money', op: 'add', value: -5 }],
|
|
},
|
|
];
|
|
|
|
let state = startGame(upkeepPack, { seed: 'upkeep' });
|
|
const before = state.money;
|
|
const first = takeTurn(upkeepPack, state, currentTurn(upkeepPack, state).options[0].option.id);
|
|
|
|
assert.equal(first.record.upkeep.length, 1, 'the conditional entry is skipped while it does not apply');
|
|
assert.equal(first.record.upkeep[0].label, 'wage');
|
|
const fromChoice = first.record.changes
|
|
.filter((c) => c.path === 'money')
|
|
.reduce((sum, c) => sum + (c.to - c.from), 0);
|
|
assert.equal(first.state.money, before + fromChoice + 20);
|
|
|
|
// Once the flag is set, the conditional entry starts applying.
|
|
const nowIndebted = setPath(first.state, 'flags.in_debt', true);
|
|
const nextOption = currentTurn(upkeepPack, nowIndebted).options.find((o) => o.available).option.id;
|
|
const indebted = takeTurn(upkeepPack, nowIndebted, nextOption);
|
|
assert.deepEqual(indebted.record.upkeep.map((c) => c.label), ['wage', 'debt service']);
|
|
});
|
|
|
|
test('a character can start somewhere other than the pack default', () => {
|
|
const pack = structuredClone(testPack);
|
|
pack.characters.find((c) => c.id === 'pat').startingRelationship = { likes_you: 20 };
|
|
|
|
const state = startGame(pack, { seed: 'starts' });
|
|
assert.equal(state.relationships.pat.likes_you, 20, 'the override applies');
|
|
assert.equal(state.relationships.pat.fears_you, 50, 'unnamed stats keep the pack default');
|
|
assert.equal(state.relationships.chief.likes_you, 50, 'other characters are unaffected');
|
|
});
|
|
|
|
test('a pack with a calendar gets day-of-cycle state it can gate on', () => {
|
|
const dated = structuredClone(testPack);
|
|
dated.calendar = { cycleLength: 7, cycleName: 'Week', unitNames: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] };
|
|
|
|
let state = startGame(dated, { seed: 'calendar' });
|
|
assert.deepEqual(
|
|
{ index: state.calendar.index, cycle: state.calendar.cycle, name: state.calendar.name },
|
|
{ index: 0, cycle: 1, name: 'Mon' },
|
|
);
|
|
|
|
for (let i = 0; i < 7; i++) {
|
|
state = takeTurn(dated, state, currentTurn(dated, state).options.find((o) => o.available).option.id).state;
|
|
}
|
|
assert.equal(state.turn, 8);
|
|
assert.deepEqual(
|
|
{ index: state.calendar.index, cycle: state.calendar.cycle, name: state.calendar.name },
|
|
{ index: 0, cycle: 2, name: 'Mon' },
|
|
);
|
|
});
|
|
|
|
test('a pack with no calendar has none: the engine imposes no week', () => {
|
|
const state = startGame(testPack, { seed: 'no-calendar' });
|
|
assert.deepEqual(state.calendar, {});
|
|
});
|
|
|
|
test('scheduled upkeep falls due on its own period, not every turn', () => {
|
|
const scheduled = structuredClone(testPack);
|
|
scheduled.upkeep = [
|
|
{ label: 'daily', effects: [{ path: 'money', op: 'add', value: -1 }] },
|
|
{ label: 'weekly rent', every: 7, offset: 6, effects: [{ path: 'money', op: 'add', value: -50 }] },
|
|
{ label: 'fortnightly pay', every: 14, offset: 6, effects: [{ path: 'money', op: 'add', value: 200 }] },
|
|
];
|
|
|
|
let state = startGame(scheduled, { seed: 'schedule' });
|
|
const dueOn = [];
|
|
for (let turn = 1; turn <= 22; turn++) {
|
|
const result = takeTurn(scheduled, state, currentTurn(scheduled, state).options.find((o) => o.available).option.id);
|
|
dueOn.push(result.record.upkeep.map((c) => c.label));
|
|
state = result.state;
|
|
}
|
|
|
|
assert.deepEqual(dueOn[0], ['daily'], 'turn 1 is an ordinary day');
|
|
assert.deepEqual(dueOn[6], ['daily', 'weekly rent', 'fortnightly pay'], 'turn 7 is rent and payday');
|
|
assert.deepEqual(dueOn[13], ['daily', 'weekly rent'], 'turn 14 is rent only');
|
|
assert.deepEqual(dueOn[20], ['daily', 'weekly rent', 'fortnightly pay'], 'turn 21 is both again');
|
|
});
|
|
|
|
test('moving stage changes which events are drawn', () => {
|
|
const twoStage = structuredClone(testPack);
|
|
twoStage.events.push({
|
|
id: 'upstairs_only',
|
|
stage: 'attic',
|
|
weight: 1,
|
|
description: 'It is quieter up here.',
|
|
options: [{ id: 'wait', label: 'Wait.', effects: [] }],
|
|
});
|
|
twoStage.events.find((e) => e.id === 'routine').options.push({
|
|
id: 'climb',
|
|
label: 'Go up to the attic.',
|
|
effects: [{ path: 'stage', op: 'set', value: 'attic' }],
|
|
});
|
|
|
|
let state = startGame(twoStage, { seed: 'stages' });
|
|
// Walk to a turn offering the climb, then take it.
|
|
for (let i = 0; i < 20; i++) {
|
|
const turn = currentTurn(twoStage, state);
|
|
if (turn.event.id === 'routine') {
|
|
state = takeTurn(twoStage, state, 'climb').state;
|
|
break;
|
|
}
|
|
state = takeTurn(twoStage, state, turn.options.find((o) => o.available).option.id).state;
|
|
}
|
|
|
|
assert.equal(state.stage, 'attic');
|
|
assert.equal(currentTurn(twoStage, state).event.id, 'upstairs_only',
|
|
'only the new stage\'s events are reachable now');
|
|
});
|