89 lines
3.2 KiB
JavaScript
89 lines
3.2 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
init,
|
|
createAnalysisScenario,
|
|
move,
|
|
legalMoves,
|
|
currentPlayer,
|
|
canLaunchAnalysisMode,
|
|
} from "../engine/index.js";
|
|
test("scenario does not depend on hidden decks, RNG or undo and stays playable", () => {
|
|
const source = init(3, [], { newGenes: true }, "secret");
|
|
const before = structuredClone(source);
|
|
const changed = structuredClone(source);
|
|
changed._seed = "different";
|
|
changed._counter += 100;
|
|
changed._deck.reverse();
|
|
changed.newGeneMarket._deck.reverse();
|
|
changed._undo = [{ player: 0, state: before }];
|
|
changed._pendingMoves = [{ secret: true }];
|
|
const a = createAnalysisScenario(source, { player: 0, seed: "fake" });
|
|
assert.deepEqual(
|
|
a,
|
|
createAnalysisScenario(changed, { player: 0, seed: "fake" }),
|
|
);
|
|
assert.deepEqual(source, before);
|
|
assert.equal(a._deck.length, source.deckRemaining);
|
|
let state = a;
|
|
for (let i = 0; i < 100 && !state.finished; i++)
|
|
state = move(state, legalMoves(state)[0], currentPlayer(state));
|
|
assert.notEqual(state, a);
|
|
});
|
|
test("own foresight is preserved; other knowledge and seeds are simulated", () => {
|
|
const source = init(3, [], { newGenes: true }, "knowledge");
|
|
source._knowledge = {
|
|
0: structuredClone(source._deck[0]),
|
|
1: structuredClone(source._deck[0]),
|
|
};
|
|
const a = createAnalysisScenario(source, { player: 0, seed: "sandbox" });
|
|
assert.deepEqual(a._deck[0], source._knowledge[0]);
|
|
assert.deepEqual(a._knowledge, { 0: source._knowledge[0] });
|
|
const changed = structuredClone(source);
|
|
changed._knowledge[1] = { ozone: 100, direction: 4 };
|
|
assert.deepEqual(
|
|
a,
|
|
createAnalysisScenario(changed, { player: 0, seed: "sandbox" }),
|
|
);
|
|
assert.notDeepEqual(
|
|
a._deck,
|
|
createAnalysisScenario(source, { player: 0, seed: "another" })._deck,
|
|
);
|
|
assert.deepEqual(
|
|
createAnalysisScenario(source, { seed: "public" }),
|
|
createAnalysisScenario(changed, { seed: "public" }),
|
|
);
|
|
});
|
|
|
|
test("initial public environment stays excluded after later reveals and resampling", () => {
|
|
const source = init(3, [], {}, "initial-environment");
|
|
const initial = structuredClone(source.environment);
|
|
source.environment = source._deck.shift();
|
|
source.deckRemaining = source._deck.length;
|
|
const directions = ["west", "north", "east", "south", "still"];
|
|
source.log.push({
|
|
text: `The current is ${directions[source.environment.direction]}. Ozone layer: ${source.environment.ozone}.`,
|
|
});
|
|
for (const seed of ["a", "b", "c"]) {
|
|
const scenario = createAnalysisScenario(source, { seed });
|
|
assert.equal(scenario._deck.length, source.deckRemaining);
|
|
assert.ok(
|
|
!scenario._deck.some(
|
|
(c) => c.direction === initial.direction && c.ozone === initial.ozone,
|
|
),
|
|
);
|
|
assert.deepEqual(scenario.initialEnvironment, initial);
|
|
const again = createAnalysisScenario(scenario, { seed: "resample" });
|
|
assert.ok(
|
|
!again._deck.some(
|
|
(c) => c.direction === initial.direction && c.ozone === initial.ozone,
|
|
),
|
|
);
|
|
}
|
|
delete source.initialEnvironment;
|
|
assert.equal(canLaunchAnalysisMode(source), false);
|
|
assert.throws(
|
|
() => createAnalysisScenario(source, { seed: "safe" }),
|
|
/initial environment/,
|
|
);
|
|
});
|