mirror of
https://github.com/motajs/template.git
synced 2026-11-13 23:04:32 +08:00
feat(03-01): add awaited replay movement tracer
- exercise source-aware event mutation on a fixed map - verify Node replay reaches its normal end state
This commit is contained in:
1 parent
a707be4856
commit
4eec08638e
5 files changed
+165
-6
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { createCoreState } from '../src/core';
|
||||
import { createClosedLoopFixture } from './fixtures/closed-loop';
|
||||
|
||||
describe('Node replay tracer', () => {
|
||||
// 验证 Node 宿主无需浏览器全局即可创建相互独立的内存状态
|
||||
it('creates independent Node-safe CoreState instances', () => {
|
||||
const first = createCoreState();
|
||||
const second = createCoreState();
|
||||
|
||||
expect(first).not.toBe(second);
|
||||
expect(first.saveSystem).not.toBe(second.saveSystem);
|
||||
expect(first.saveSystem.constructor.name).toBe('MemorySaveSystem');
|
||||
expect(second.saveSystem.constructor.name).toBe('MemorySaveSystem');
|
||||
});
|
||||
|
||||
// 验证 replay 等待真实移动与事件完成后才结束并改变事件层矩阵
|
||||
it('replays movement, awaits event mutation, and ends normally', async () => {
|
||||
const fixture = createClosedLoopFixture();
|
||||
|
||||
expect(fixture.eventLayer.getBlock(0, 0)).toBe(1);
|
||||
expect(fixture.eventLayer.getBlock(1, 0)).toBe(1);
|
||||
|
||||
fixture.sandbox.play();
|
||||
await waitForEnded(fixture.sandbox);
|
||||
|
||||
expect(fixture.sandbox.ended).toBe(true);
|
||||
expect(fixture.sandbox.getReplayed()).toBe(1);
|
||||
expect(fixture.state.hero.location.x).toBe(1);
|
||||
expect(fixture.state.hero.location.y).toBe(0);
|
||||
expect(fixture.eventCompleted()).toBe(true);
|
||||
expect(fixture.eventLayer.getBlock(1, 0)).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
async function waitForEnded(sandbox: {
|
||||
readonly ended: boolean;
|
||||
}): Promise<void> {
|
||||
for (let index = 0; index < 100 && !sandbox.ended; index++) {
|
||||
await new Promise<void>(resolve => setTimeout(resolve, 0));
|
||||
}
|
||||
if (!sandbox.ended) {
|
||||
throw new Error('replay did not reach its end state');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user