test(06-04): cover ReplaySystem and ReplaySandbox pipeline (stage 3)

- system.test.ts:registerCommand/getCommand、重复注册 163、record + onRecordCommand、沙箱创建/reset、释放与实例独立性
- sandbox.test.ts:step/onStep、失败即停 158、157、notExecuted 175、156、startIndex、play/pause/resume/stop、setSpeed
- 06-COVERAGE-MAP.md 追加 06-04 小节(148–163、175)
- 06-TEST-FINDINGS.md 登记 #06-04-1..4
This commit is contained in:
AncTe committed 2026-09-14 15:21:49 +08:00
1 parent 8832159f8c
commit cb9c90abc1
4 files changed
+621

No files matched your search

@@ -0,0 +1,410 @@
// 测试 ReplaySandbox 播放器的步进、收尾与播放控制
import { logger } from '@motajs/common';
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
import { ReplaySystem } from './system';
import { IReplayCommand, IReplaySandbox, IReplayStepHandler } from './types';
vi.hoisted(() => {
vi.stubGlobal('main', { replayChecking: true });
vi.stubGlobal('location', { origin: 'http://localhost' });
});
afterEach(() => {
vi.restoreAllMocks();
});
afterAll(() => {
vi.unstubAllGlobals();
});
interface IManualReplaySandbox extends IReplaySandbox {
playing: boolean;
pausing: boolean;
}
// 构造一个可注入执行与收尾行为的录像命令
function createCommand(
execute: (step: IReplayStepHandler) => Promise<boolean>,
notExecuted?: () => Promise<boolean>
): IReplayCommand {
return notExecuted ? { execute, notExecuted } : { execute };
}
// 用录像系统构造一个可手动驱动的录像沙箱
function createSandbox(system: ReplaySystem): IManualReplaySandbox {
return system.createReplaySandbox({
route: system.route,
reseter: { reset: () => {} }
}) as IManualReplaySandbox;
}
// 将沙箱置为手动步进的播放状态
function start(sandbox: IManualReplaySandbox): void {
sandbox.playing = true;
sandbox.pausing = false;
}
// 有界等待播放结束,避免不终止的播放循环挂起测试
async function waitForEnded(sandbox: IReplaySandbox): Promise<void> {
for (let index = 0; index < 1000 && !sandbox.ended; index++) {
await new Promise<void>(resolve => setTimeout(resolve, 0));
}
}
// 生成一个可外部兑现的异步结果
function createDeferred() {
return Promise.withResolvers<boolean>();
}
describe('ReplaySandbox stepping', () => {
// 验证 step 执行下一步、触发 onStep 钩子并返回 true
it('steps one command and dispatches the step hook', async () => {
const executed: number[] = [];
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(async step => {
executed.push(step.index);
return true;
})
);
system.record(1, 5);
const sandbox = createSandbox(system);
const stepped: IReplayStepHandler[] = [];
sandbox
.addHook({
onStep: async step => {
stepped.push(step);
}
})
.load();
start(sandbox);
await expect(sandbox.step()).resolves.toBe(true);
expect(executed).toEqual([1]);
expect(stepped).toHaveLength(1);
expect(stepped[0].command).toBe(1);
expect(sandbox.getReplayed()).toBe(1);
});
// 验证执行失败的命令触发告警码 158 并停止后续步骤
it('warns code 158 and stops when a command returns false', async () => {
const warn = vi.spyOn(logger, 'warn').mockImplementation(() => {});
const executed: number[] = [];
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(async () => {
executed.push(1);
return false;
})
);
system.registerCommand(
2,
createCommand(async () => {
executed.push(2);
return true;
})
);
system.record(1);
system.record(2);
const sandbox = createSandbox(system);
start(sandbox);
await expect(sandbox.step()).resolves.toBe(false);
expect(executed).toEqual([1]);
expect(warn).toHaveBeenCalledWith(158, '1', '[]');
expect(sandbox.getReplayed()).toBe(1);
});
// 验证未知指令触发告警码 157 并返回 false
it('warns code 157 for an unknown command', async () => {
const warn = vi.spyOn(logger, 'warn').mockImplementation(() => {});
const system = new ReplaySystem();
system.record(9);
const sandbox = createSandbox(system);
start(sandbox);
await expect(sandbox.step()).resolves.toBe(false);
expect(warn).toHaveBeenCalledWith(157, '9');
});
// 验证切换指令时先成功收尾上一步再执行下一步
it('finalizes the previous command before executing a different one', async () => {
const order: string[] = [];
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(
async () => {
order.push('exec1');
return true;
},
async () => {
order.push('fin1');
return true;
}
)
);
system.registerCommand(
2,
createCommand(async () => {
order.push('exec2');
return true;
})
);
system.record(1);
system.record(2);
const sandbox = createSandbox(system);
start(sandbox);
await expect(sandbox.step()).resolves.toBe(true);
await expect(sandbox.step()).resolves.toBe(true);
expect(order).toEqual(['exec1', 'fin1', 'exec2']);
});
// 验证 notExecuted 收尾失败触发告警码 175 并停止
it('warns code 175 when notExecuted fails', async () => {
const warn = vi.spyOn(logger, 'warn').mockImplementation(() => {});
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(
async () => true,
async () => false
)
);
system.registerCommand(
2,
createCommand(async () => true)
);
system.record(1);
system.record(2);
const sandbox = createSandbox(system);
start(sandbox);
await expect(sandbox.step()).resolves.toBe(true);
await expect(sandbox.step()).resolves.toBe(false);
expect(warn).toHaveBeenCalledWith(175, '1');
});
// 验证读取流过期时触发告警码 156 并返回 false
it('warns code 156 when the read stream expired', async () => {
const warn = vi.spyOn(logger, 'warn').mockImplementation(() => {});
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(async () => true)
);
system.record(1);
const sandbox = createSandbox(system);
start(sandbox);
system.route.add(1, []);
await expect(sandbox.step()).resolves.toBe(false);
expect(warn).toHaveBeenCalledWith(156);
});
// 验证沙箱从指定起始索引开始读取并正确报告已播放步数
it('starts from the configured index and counts replayed steps', async () => {
const executed: number[] = [];
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(async step => {
executed.push(step.index);
return true;
})
);
system.record(1);
system.record(1);
system.record(1);
const sandbox = system.createReplaySandbox({
route: system.route,
reseter: { reset: () => {} },
startIndex: 2
}) as IManualReplaySandbox;
start(sandbox);
expect(sandbox.getReplayed()).toBe(2);
await expect(sandbox.step()).resolves.toBe(true);
expect(executed).toEqual([3]);
});
});
describe('ReplaySandbox playback control', () => {
// 验证 play 依次执行全部步骤并在结束时置位 ended
it('plays through every step and marks the sandbox ended', async () => {
const order: number[] = [];
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(async step => {
order.push(step.index);
return true;
})
);
system.record(1);
system.record(1);
const sandbox = createSandbox(system);
const startHook = vi.fn();
sandbox.addHook({ onStartReplay: startHook }).load();
sandbox.play();
await waitForEnded(sandbox);
expect(startHook).toHaveBeenCalledTimes(1);
expect(sandbox.playing).toBe(true);
expect(sandbox.ended).toBe(true);
expect(order).toEqual([1, 2]);
});
// 验证已在播放时再次 play 不重复触发 onStartReplay
it('does not restart an already playing sandbox', async () => {
const system = new ReplaySystem();
const deferred = createDeferred();
system.registerCommand(
1,
createCommand(() => deferred.promise)
);
system.record(1);
const sandbox = createSandbox(system);
const startHook = vi.fn();
sandbox.addHook({ onStartReplay: startHook }).load();
sandbox.play();
sandbox.play();
deferred.resolve(true);
await waitForEnded(sandbox);
expect(startHook).toHaveBeenCalledTimes(1);
});
// 验证播放结束后再次 play 是空操作
it('ignores play after the replay ended', async () => {
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(async () => true)
);
system.record(1);
const sandbox = createSandbox(system);
const startHook = vi.fn();
sandbox.addHook({ onStartReplay: startHook }).load();
sandbox.play();
await waitForEnded(sandbox);
sandbox.play();
expect(sandbox.ended).toBe(true);
expect(startHook).toHaveBeenCalledTimes(1);
});
// 验证 pause 在暂停点兑现且 resume 从暂停点继续执行
it('pauses at the next step boundary and resumes', async () => {
const order: number[] = [];
const first = createDeferred();
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(step => {
order.push(step.index);
return step.index === 1 ? first.promise : Promise.resolve(true);
})
);
system.record(1);
system.record(1);
const sandbox = createSandbox(system);
const pauseHook = vi.fn();
const resumeHook = vi.fn();
sandbox
.addHook({
onPauseReplay: pauseHook,
onResumeReplay: resumeHook
})
.load();
sandbox.play();
const paused = sandbox.pause();
first.resolve(true);
await paused;
expect(pauseHook).toHaveBeenCalledTimes(1);
expect(sandbox.pausing).toBe(true);
expect(order).toEqual([1]);
sandbox.resume();
await waitForEnded(sandbox);
expect(resumeHook).toHaveBeenCalledTimes(1);
expect(sandbox.ended).toBe(true);
expect(order).toEqual([1, 2]);
});
// 验证 stop 先暂停播放并触发 onStopReplay
it('pauses and dispatches onStopReplay on stop', async () => {
const first = createDeferred();
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(() => first.promise)
);
system.record(1);
const sandbox = createSandbox(system);
const pauseHook = vi.fn();
const stopHook = vi.fn();
sandbox
.addHook({
onPauseReplay: pauseHook,
onStopReplay: stopHook
})
.load();
sandbox.play();
const stopped = sandbox.stop();
first.resolve(true);
await stopped;
expect(pauseHook).toHaveBeenCalledTimes(1);
expect(stopHook).toHaveBeenCalledTimes(1);
expect(sandbox.pausing).toBe(true);
});
// 验证 play 遇到过期读取流时告警码 156 且不进入播放
it('warns code 156 when playing with an expired stream', async () => {
const warn = vi.spyOn(logger, 'warn').mockImplementation(() => {});
const system = new ReplaySystem();
system.registerCommand(
1,
createCommand(async () => true)
);
system.record(1);
const sandbox = createSandbox(system);
system.route.add(1, []);
sandbox.play();
await new Promise<void>(resolve => setTimeout(resolve, 0));
expect(warn).toHaveBeenCalledWith(156);
expect(sandbox.ended).toBe(false);
});
// 验证 setSpeed 触发 onSpeedSet 并更新播放倍率
it('dispatches onSpeedSet when the speed changes', () => {
const system = new ReplaySystem();
const sandbox = createSandbox(system);
const speedHook = vi.fn();
sandbox.addHook({ onSpeedSet: speedHook }).load();
sandbox.setSpeed(2);
expect(sandbox.speed).toBe(2);
expect(speedHook).toHaveBeenCalledWith(2);
});
});
@@ -0,0 +1,160 @@
// 测试 ReplaySystem 的指令注册、录像录制与沙箱生命周期
import { logger } from '@motajs/common';
import { afterAll, describe, expect, it, vi } from 'vitest';
import { ReplaySystem } from './system';
import {
IReplayCommand,
IReplaySandbox,
ReplayCommandWidth,
ReplayParamValue
} from './types';
vi.hoisted(() => {
vi.stubGlobal('main', { replayChecking: true });
vi.stubGlobal('location', { origin: 'http://localhost' });
});
afterAll(() => {
vi.unstubAllGlobals();
});
// 构造一个结果固定的录像命令
function createCommand(result: boolean = true): IReplayCommand {
return { execute: async () => result };
}
// 构造一个记录 reset 入参的状态重置对象
function createReseter() {
const calls: Array<Map<string, unknown> | undefined> = [];
return {
calls,
reseter: {
reset: (save?: Map<string, unknown>) => {
calls.push(save);
}
}
};
}
describe('ReplaySystem registration and recording', () => {
// 验证构造出的录像操作器为默认 uint8 位宽且初始为空
it('builds an empty uint8 route', () => {
const system = new ReplaySystem();
expect(system.route.commandWidth).toBe(ReplayCommandWidth.Uint8);
expect(system.route.length).toBe(0);
});
// 验证注册后可取回命令且未知码返回 null
it('registers a command and returns null for an unknown code', () => {
const system = new ReplaySystem();
const command = createCommand();
system.registerCommand(7, command);
expect(system.getCommand(7)).toBe(command);
expect(system.getCommand(99)).toBeNull();
});
// 验证重复注册触发告警码 163 且保留原命令
it('warns code 163 on a duplicate registration and keeps the original', () => {
const system = new ReplaySystem();
const first = createCommand(true);
const second = createCommand(false);
system.registerCommand(1, first);
const { info } = logger.catch(() => system.registerCommand(1, second));
expect(info.map(v => v.code)).toContain(163);
expect(system.getCommand(1)).toBe(first);
});
// 验证 record 追加录像步并触发 onRecordCommand 钩子
it('appends a replay step and dispatches the record hook', () => {
const system = new ReplaySystem();
const recorded: Array<
readonly [number, number, readonly ReplayParamValue[]]
> = [];
system
.addHook({
onRecordCommand: (code, index, params) => {
recorded.push([code, index, params]);
}
})
.load();
system.record(5, 1, true);
expect(system.route.length).toBe(1);
expect(system.route.get(0)).toEqual({
command: 5,
params: [1, true],
index: 0
});
expect(recorded).toEqual([[5, 1, [1, true]]]);
});
// 验证每个 ReplaySystem 实例的注册与录像相互独立
it('keeps commands and routes independent between instances', () => {
const first = new ReplaySystem();
const second = new ReplaySystem();
first.registerCommand(1, createCommand());
first.record(1);
expect(second.getCommand(1)).toBeNull();
expect(second.route.length).toBe(0);
expect(first.route.length).toBe(1);
});
});
describe('ReplaySystem sandbox lifecycle', () => {
// 验证创建沙箱时用存档重置状态、保存 sandbox 并触发 onCreateSandbox
it('creates a sandbox, resets state and dispatches onCreateSandbox', () => {
const system = new ReplaySystem();
const { calls, reseter } = createReseter();
const created: IReplaySandbox[] = [];
system
.addHook({
onCreateSandbox: sandbox => {
created.push(sandbox);
}
})
.load();
const save = new Map<string, unknown>([['hp', 10]]);
const sandbox = system.createReplaySandbox({
route: system.route,
reseter,
save
});
expect(calls).toEqual([save]);
expect(system.sandbox).toBe(sandbox);
expect(created).toEqual([sandbox]);
});
// 验证释放沙箱时停止播放并清空当前沙箱引用
it('stops and clears the active sandbox on release', () => {
const system = new ReplaySystem();
const { reseter } = createReseter();
const sandbox = system.createReplaySandbox({
route: system.route,
reseter
});
const stop = vi.spyOn(sandbox, 'stop').mockResolvedValue(undefined);
system.releaseSandbox();
expect(stop).toHaveBeenCalledTimes(1);
expect(system.sandbox).toBeNull();
});
// 验证没有活跃沙箱时释放操作安全地保持为空
it('releases safely when no sandbox is active', () => {
const system = new ReplaySystem();
system.releaseSandbox();
expect(system.sandbox).toBeNull();
});
});