fix(sdk): release lifecycle validation modules

This commit is contained in:
2026-09-05 01:07:20 +08:00
parent f2823c398a
commit f25211c64e
4 changed files with 88 additions and 12 deletions
@@ -40,15 +40,21 @@ function makeEntry(createModule: () => GameModule): GameEntry {
}
function makeModule(log: string[], onRestore?: (deskinfo: unknown) => void): GameModule {
let attached = false;
return {
attach: () => log.push('attach'),
attach: () => {
attached = true;
log.push('attach');
},
handlePlatformEvent: (event) => log.push(`event:${event.type}`),
handleGameMessage: (message) => log.push(`message:${message.rpc}`),
restore: (deskinfo) => {
log.push('restore');
onRestore?.(deskinfo);
},
dispose: () => log.push('dispose'),
dispose: () => {
if (attached) log.push('dispose');
},
};
}
@@ -65,6 +71,18 @@ test('assertGameEntry rejects invalid identity, routes, seat counts, and module
assert.throws(() => assertGameEntry({ ...entry, createModule: () => undefined as never }), /module/);
});
test('assertGameEntry disposes its validation module exactly once', () => {
let disposals = 0;
const entry = makeEntry(() => ({
...makeModule([]),
dispose: () => { disposals++; },
}));
assert.doesNotThrow(() => assertGameEntry(entry));
assert.equal(disposals, 1);
});
test('GameSessionHost delivers room events and deskinfo to one active module in order', () => {
const log: string[] = [];
const deskinfo = { turn: 7 };
@@ -120,6 +138,21 @@ test('GameSessionHost rejects illegal state transitions and mismatched game rout
assert.deepEqual(log, ['attach', 'message:draw']);
});
test('GameSessionHost disposes a module when attachment throws and returns to idle', () => {
let disposals = 0;
const sessions = new GameSessionHost(makeEntry(() => ({
...makeModule([]),
attach: () => { throw new Error('attach failed'); },
dispose: () => { disposals++; },
})));
disposals = 0;
assert.throws(() => sessions.open(makeHost()), /attach failed/);
assert.equal(disposals, 1);
assert.equal(sessions.state, 'idle');
});
test('GameSessionHost disposes a module exactly once and becomes terminal after dispose', () => {
const log: string[] = [];
const sessions = new GameSessionHost(makeEntry(() => makeModule(log)));