template/packages-user/data-common/src/replay/__test__/saveLoad.test.ts
unanmed 36d8b4fb3c test(08-01): migrate data-layer tests into __test__ dirs
Move 65 *.test.ts + 6 *.perf.ts across data-{common,base,system,state} into sibling __test__/ (data-state/test -> data-state/__test__), incl. fixtures/floors.json and non-test helpers; rewrite Rule A relative specifiers (+1 depth), keep Rule B unchanged; sync script/test-data-node.ts require paths. Gates: test:ci=66 files, test:perf=6 files, eslint clean, zero config/production changes.
2026-10-04 17:34:09 +08:00

88 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 测试录像存读档:ReplaySystem 同实例往返、多类型参数读回与活跃读流跨读档过期
import { logger } from '@motajs/common';
import { afterAll, describe, expect, it, vi } from 'vitest';
import { ReplaySystem } from '../system';
vi.hoisted(() => {
vi.stubGlobal('main', { replayChecking: true });
vi.stubGlobal('location', { origin: 'http://localhost' });
Map.prototype.getOrInsertComputed ??= function <K, V>(
this: Map<K, V>,
key: K,
callback: (key: K) => V
): V {
const existing = this.get(key);
if (existing !== undefined) return existing;
const value = callback(key);
this.set(key, value);
return value;
};
});
afterAll(() => {
vi.unstubAllGlobals();
});
/** 构造一个独立的录像系统实例 */
function createSystem(): ReplaySystem {
return new ReplaySystem();
}
describe('ReplaySystem save and load round trips', () => {
// 验证已注册指令与录像步在同实例上恢复到存档点
it('restores the route and registered commands on the same instance', () => {
const system = createSystem();
system.registerCommand(3, { execute: async () => true });
system.record(3, 1, true, 'param');
const snapshot = system.saveState();
system.record(4, 2);
system.replaying = true;
system.loadState(snapshot);
expect(system.array.length).toBe(1);
expect(system.array.commandWidth).toBe(snapshot.commandWidth);
expect(system.array.get(0)).toEqual({
command: 3,
params: [1, true, 'param'],
index: 0
});
expect(system.getCommand(3)).not.toBeNull();
expect(system.getCommand(4)).toBeNull();
});
// 验证读档后命令与参数数组与快照关键内容一致
it('keeps the command and param arrays consistent with the snapshot', () => {
const system = createSystem();
system.record(2, 7);
const snapshot = system.saveState();
system.record(5, 9);
system.loadState(snapshot);
expect(system.array.getCommandArray()).toBe(snapshot.commandArray);
expect(system.array.getParamArray()).toBe(snapshot.paramArray);
expect(system.array.get(0).command).toBe(2);
});
// 验证读档整体替换缓冲区后,此前创建的活跃沙箱读流被标记过期并触发告警码 156(#06-17-3)
it('expires an active sandbox read stream when a snapshot is loaded', () => {
const system = createSystem();
system.registerCommand(1, { execute: async () => true });
system.record(1, 7);
const snapshot = system.saveState();
system.record(1, 9);
const sandbox = system.createReplaySandbox({
route: system.array,
reseter: { reset: () => {} }
});
system.loadState(snapshot);
const { info } = logger.catch(() => sandbox.play());
expect(info.map(v => v.code)).toContain(156);
});
});