fix(sdk): inject assertions into Creator-loaded contract harness

This commit is contained in:
2026-09-05 18:58:44 +08:00
parent 690fdef78a
commit 266be19e76
3 changed files with 40 additions and 6 deletions
@@ -1,6 +1,9 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { test } from 'node:test';
import { runInNewContext } from 'node:vm';
import ts from 'typescript';
import * as gameSessionHost from '../../YouleNexus/assets/framework/sdk/runtime/game-session-host.ts';
import type { RuntimeConfig } from '../../YouleNexus/assets/framework/config/runtime-config.ts';
import type { OutboundEnvelope } from '../../YouleNexus/assets/framework/core/types/envelope.ts';
import { createGameHostAdapter } from '../../YouleNexus/assets/framework/platform/game-host-adapter.ts';
@@ -21,6 +24,29 @@ const GAME_ROOMTYPE = JSON.parse(readFileSync(
new URL('../fixtures/contracts/player-login-room.json', import.meta.url), 'utf8',
)).data.roomtype as readonly unknown[];
test('browser-like loader evaluates the emitted harness without Node builtin modules', () => {
const source = readFileSync(new URL(
'../../YouleNexus/assets/framework/sdk/testing/game-contract-harness.ts', import.meta.url,
), 'utf8');
const { outputText } = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
});
const exports: Record<string, unknown> = {};
const loaded: string[] = [];
assert.doesNotThrow(() => runInNewContext(outputText, {
exports,
require(specifier: string) {
if (specifier !== '../runtime/game-session-host.ts') {
throw new Error(`Creator cannot load runtime module: ${specifier}`);
}
loaded.push(specifier);
return gameSessionHost;
},
}));
assert.equal(typeof exports.assertGameContract, 'function');
assert.deepEqual(loaded, ['../runtime/game-session-host.ts']);
});
test('assertGameContract accepts a fixed-four game with its real nested roomtype and host fixture', () => {
let invalidations = 0;
let nextModuleId = 0;
@@ -40,6 +66,7 @@ test('assertGameContract accepts a fixed-four game with its real nested roomtype
expectedSeatCount: 4,
makeHost: makeConformanceHost(entry, () => { invalidations++; }, restoredDeskinfo),
}],
assert,
));
assert.equal(invalidations, 2);
assert.equal(restoredDeskinfo.length, 0);