gitworld / tests /world.test.mjs
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/gitworld
6281708 verified
Raw
History Blame Contribute Delete
2.93 kB
import test from 'node:test';
import assert from 'node:assert/strict';
import { EVENT_TYPES, commandTranscript } from '../src/shared.js';
import {
DISTRICTS,
advanceWorldState,
createWorldState,
getNearbyDistrict,
worldReducer
} from '../src/world-architect.js';
test('initial world is deterministic and contains all repository districts', () => {
const first = createWorldState();
const second = createWorldState();
assert.deepEqual(first, second);
assert.equal(first.repository.simulated, true);
assert.deepEqual(first.districts.map((district) => district.id), ['src', 'tests', 'docs', 'issues', 'prs', 'actions']);
assert.equal(DISTRICTS.length, 6);
assert.equal(first.agents.length, 3);
});
test('movement is bounded and building collision preserves the previous position', () => {
let state = createWorldState();
const initial = { ...state.player };
for (let index = 0; index < 120; index += 1) state = worldReducer(state, { type: EVENT_TYPES.PLAYER_MOVE, dx: 0, dy: 1 });
assert.ok(state.player.y <= 850);
for (let index = 0; index < 120; index += 1) state = worldReducer(state, { type: EVENT_TYPES.PLAYER_MOVE, dx: -1, dy: 0 });
assert.ok(state.player.x >= 70);
const atStart = createWorldState();
const blocked = worldReducer(atStart, { type: EVENT_TYPES.PLAYER_MOVE, dx: 0, dy: -1 });
assert.notDeepEqual(blocked.player, initial);
assert.equal(blocked.player.step, 1);
});
test('the world emits a deterministic construction event on each simulation step', () => {
const start = createWorldState();
const first = advanceWorldState(start);
const replay = advanceWorldState(createWorldState());
assert.equal(first.state.tick, 1);
assert.deepEqual(first.event, replay.event);
assert.equal(first.event.type, EVENT_TYPES.AGENT_PROGRESS);
assert.ok(first.state.events[0].detail.message.includes('WORLD ARCHITECT'));
});
test('entering and inspecting a repository district updates the shared state', () => {
let state = createWorldState();
state = worldReducer(state, { type: EVENT_TYPES.SELECT_DISTRICT, districtId: 'docs' });
state = worldReducer(state, { type: EVENT_TYPES.OPEN_INSPECTOR });
assert.equal(state.selectedDistrictId, 'docs');
assert.equal(state.activePanel, 'inspector');
state = worldReducer(state, { type: EVENT_TYPES.ENTER_DISTRICT, districtId: 'docs' });
assert.equal(state.view, 'district');
assert.equal(state.events[0].type, EVENT_TYPES.ENTER_DISTRICT);
});
test('nearby district detection and terminal transcript are browser-local', () => {
const state = createWorldState();
const nearby = getNearbyDistrict(state);
assert.equal(nearby.inRange, false);
const transcript = commandTranscript('cargo test', state);
assert.ok(transcript.some((line) => line.includes('142 passed')));
assert.ok(!transcript.some((line) => line.includes('network')));
});