feat(platform): expose restricted game host and commands
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { test } from 'node:test';
|
||||
import type { RuntimeConfig } from '../../YouleNexus/assets/framework/config/runtime-config.ts';
|
||||
import type { OutboundEnvelope } from '../../YouleNexus/assets/framework/core/types/envelope.ts';
|
||||
import { PlatformCommands } from '../../YouleNexus/assets/framework/platform/commands.ts';
|
||||
import { PlatformStore } from '../../YouleNexus/assets/framework/platform/stores/platform-store.ts';
|
||||
import {
|
||||
parseLoginResponse,
|
||||
} from '../../YouleNexus/assets/framework/protocol/contracts/index.ts';
|
||||
|
||||
const RUNTIME_CONFIG: RuntimeConfig = Object.freeze({
|
||||
mode: 'release',
|
||||
isDebugger: false,
|
||||
identity: Object.freeze({
|
||||
agentid: 'agent-from-config',
|
||||
channelid: 'channel-from-config',
|
||||
gameid: 'game-from-config',
|
||||
marketid: 'market-from-config',
|
||||
version: 41,
|
||||
}),
|
||||
servers: Object.freeze(['ws://fixture']),
|
||||
source: 'remote',
|
||||
gameserver: 'https://fixture/config',
|
||||
rawConfig: Object.freeze({ data: Object.freeze({ urlserver: 'fixture' }) }),
|
||||
});
|
||||
|
||||
function fixture(name: string): { data: Record<string, unknown> } {
|
||||
return JSON.parse(readFileSync(new URL(`../fixtures/contracts/${name}`, import.meta.url), 'utf8'));
|
||||
}
|
||||
|
||||
function makeStore(location: 'outside' | 'inside', stage = 0): PlatformStore {
|
||||
const store = new PlatformStore();
|
||||
const source = fixture(location === 'inside' ? 'player-login-room.json' : 'player-login-success.json');
|
||||
if (location === 'inside') source.data.isbattle = stage;
|
||||
store.applyLoginSuccess(parseLoginResponse(source.data));
|
||||
return store;
|
||||
}
|
||||
|
||||
function makeCommands(store: PlatformStore): {
|
||||
readonly commands: PlatformCommands;
|
||||
readonly sent: OutboundEnvelope[];
|
||||
} {
|
||||
const sent: OutboundEnvelope[] = [];
|
||||
return {
|
||||
commands: new PlatformCommands({
|
||||
runtimeConfig: RUNTIME_CONFIG,
|
||||
store,
|
||||
send: { send: (envelope) => { sent.push(envelope); } },
|
||||
}),
|
||||
sent,
|
||||
};
|
||||
}
|
||||
|
||||
test('prepare and exitRoom use the first-slice builders with authoritative config and room state', () => {
|
||||
const { commands, sent } = makeCommands(makeStore('inside', 0));
|
||||
|
||||
commands.prepare();
|
||||
commands.exitRoom();
|
||||
|
||||
assert.deepEqual(sent, [
|
||||
{
|
||||
app: 'youle',
|
||||
route: 'room',
|
||||
rpc: 'player_prepare',
|
||||
data: {
|
||||
agentid: 'agent-from-config',
|
||||
playerid: 430511,
|
||||
gameid: 'game-from-config',
|
||||
roomcode: '100001',
|
||||
},
|
||||
},
|
||||
{
|
||||
app: 'youle',
|
||||
route: 'room',
|
||||
rpc: 'self_exit_room',
|
||||
data: {
|
||||
agentid: 'agent-from-config',
|
||||
playerid: 430511,
|
||||
gameid: 'game-from-config',
|
||||
roomcode: '100001',
|
||||
},
|
||||
},
|
||||
]);
|
||||
assert.equal(commands.prepare.length, 0);
|
||||
assert.equal(commands.exitRoom.length, 0);
|
||||
});
|
||||
|
||||
test('joinRoom uses authenticated Store identity and requires every non-optional environment field', () => {
|
||||
const { commands, sent } = makeCommands(makeStore('outside'));
|
||||
const location = { latitude: 28.7, longitude: 115.8 };
|
||||
|
||||
commands.joinRoom({
|
||||
roomcode: '654321',
|
||||
location,
|
||||
ip: '203.0.113.7',
|
||||
vipMatch: 1,
|
||||
match_id: 'match-9',
|
||||
});
|
||||
|
||||
assert.deepEqual(sent, [{
|
||||
app: 'youle',
|
||||
route: 'agent',
|
||||
rpc: 'self_join_room',
|
||||
data: {
|
||||
agentid: 'agent-from-config',
|
||||
playerid: 430511,
|
||||
gameid: 'game-from-config',
|
||||
roomcode: '654321',
|
||||
location,
|
||||
ip: '203.0.113.7',
|
||||
vipMatch: 1,
|
||||
match_id: 'match-9',
|
||||
},
|
||||
}]);
|
||||
assert.equal((sent[0]!.data as { location: unknown }).location, location);
|
||||
assert.equal(commands.joinRoom.length, 1);
|
||||
|
||||
assert.throws(
|
||||
() => commands.joinRoom({ roomcode: '654321', ip: '203.0.113.7' } as never),
|
||||
/location/i,
|
||||
);
|
||||
assert.throws(
|
||||
() => commands.joinRoom({ roomcode: '654321', location } as never),
|
||||
/ip/i,
|
||||
);
|
||||
assert.equal(sent.length, 1);
|
||||
});
|
||||
|
||||
test('command preconditions reject missing authentication, wrong room location, and battle exit without sending', () => {
|
||||
const unauthenticated = makeCommands(new PlatformStore());
|
||||
assert.throws(
|
||||
() => unauthenticated.commands.joinRoom({ roomcode: '654321', location: null, ip: '' }),
|
||||
/authenticated|player/i,
|
||||
);
|
||||
|
||||
const outside = makeCommands(makeStore('outside'));
|
||||
assert.throws(() => outside.commands.prepare(), /inside.*room|room.*inside/i);
|
||||
assert.throws(() => outside.commands.exitRoom(), /inside.*room|room.*inside/i);
|
||||
|
||||
const inside = makeCommands(makeStore('inside', 0));
|
||||
assert.throws(
|
||||
() => inside.commands.joinRoom({ roomcode: '654321', location: null, ip: '' }),
|
||||
/outside.*room|already.*room/i,
|
||||
);
|
||||
|
||||
const battle = makeCommands(makeStore('inside', 1));
|
||||
assert.throws(() => battle.commands.exitRoom(), /room\.exit.*stage=0/i);
|
||||
|
||||
assert.equal(unauthenticated.sent.length, 0);
|
||||
assert.equal(outside.sent.length, 0);
|
||||
assert.equal(inside.sent.length, 0);
|
||||
assert.equal(battle.sent.length, 0);
|
||||
});
|
||||
@@ -0,0 +1,363 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { test } from 'node:test';
|
||||
import type { RuntimeConfig } from '../../YouleNexus/assets/framework/config/runtime-config.ts';
|
||||
import type { OutboundEnvelope } from '../../YouleNexus/assets/framework/core/types/envelope.ts';
|
||||
import {
|
||||
createGameHostAdapter,
|
||||
GameSessionDisposedError,
|
||||
} from '../../YouleNexus/assets/framework/platform/game-host-adapter.ts';
|
||||
import { PlatformCommands } from '../../YouleNexus/assets/framework/platform/commands.ts';
|
||||
import { PlatformStore } from '../../YouleNexus/assets/framework/platform/stores/platform-store.ts';
|
||||
import type {
|
||||
PlatformState,
|
||||
} from '../../YouleNexus/assets/framework/platform/stores/platform-types.ts';
|
||||
import {
|
||||
parseLoginResponse,
|
||||
parseOtherJoinRoomPayload,
|
||||
parsePlayerPreparePayload,
|
||||
} from '../../YouleNexus/assets/framework/protocol/contracts/index.ts';
|
||||
import type {
|
||||
GameEntry,
|
||||
GameHostCommand,
|
||||
} from '../../YouleNexus/assets/framework/sdk/contracts/index.ts';
|
||||
|
||||
const RUNTIME_CONFIG: RuntimeConfig = Object.freeze({
|
||||
mode: 'release',
|
||||
isDebugger: false,
|
||||
identity: Object.freeze({
|
||||
agentid: 17,
|
||||
channelid: 18,
|
||||
gameid: 'game-41',
|
||||
marketid: 19,
|
||||
version: 41,
|
||||
}),
|
||||
servers: Object.freeze(['ws://fixture']),
|
||||
source: 'remote',
|
||||
gameserver: 'https://fixture/config',
|
||||
rawConfig: Object.freeze({ data: Object.freeze({ urlserver: 'fixture' }) }),
|
||||
});
|
||||
|
||||
function fixture(name: string): { data: Record<string, unknown> } {
|
||||
return JSON.parse(readFileSync(new URL(`../fixtures/contracts/${name}`, import.meta.url), 'utf8'));
|
||||
}
|
||||
|
||||
function roomStore(seatCount = 4, selfSeat = 1, stage = 0): PlatformStore {
|
||||
const source = fixture('player-login-room.json');
|
||||
const self = (source.data.players as Array<Record<string, unknown> | null>)[1]!;
|
||||
const players = Array.from({ length: seatCount }, () => null) as Array<Record<string, unknown> | null>;
|
||||
players[selfSeat] = { ...self };
|
||||
source.data.players = players;
|
||||
source.data.seat = selfSeat;
|
||||
source.data.roomtype = [seatCount, { nested: [1, 2] }];
|
||||
source.data.isbattle = stage;
|
||||
const store = new PlatformStore();
|
||||
store.applyLoginSuccess(parseLoginResponse(source.data));
|
||||
return store;
|
||||
}
|
||||
|
||||
function entry(route = 'fixture-route'): GameEntry {
|
||||
return {
|
||||
key: 'fixture',
|
||||
gameId: 'game-41',
|
||||
route,
|
||||
resolveSeatCount: (roomtype) => roomtype[0] as number,
|
||||
createModule: () => ({
|
||||
attach() {}, handlePlatformEvent() {}, handleGameMessage() {}, restore() {}, dispose() {},
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function makeLease(options: {
|
||||
readonly store?: PlatformStore;
|
||||
readonly gameEntry?: GameEntry;
|
||||
} = {}) {
|
||||
const store = options.store ?? roomStore();
|
||||
const sent: OutboundEnvelope[] = [];
|
||||
const send = { send: (envelope: OutboundEnvelope) => { sent.push(envelope); } };
|
||||
const commands = new PlatformCommands({ runtimeConfig: RUNTIME_CONFIG, store, send });
|
||||
const lease = createGameHostAdapter({
|
||||
gameEntry: options.gameEntry ?? entry(),
|
||||
store,
|
||||
commands,
|
||||
send,
|
||||
});
|
||||
return { ...lease, store, sent };
|
||||
}
|
||||
|
||||
test('sendGameMessage binds app and GameEntry route, rejects empty rpc, and preserves data identity', () => {
|
||||
const { host, sent } = makeLease();
|
||||
const data = { card: 7 };
|
||||
|
||||
host.sendGameMessage('play-card', data);
|
||||
|
||||
assert.deepEqual(sent[0], {
|
||||
app: 'youle', route: 'fixture-route', rpc: 'play-card', data: { card: 7 },
|
||||
});
|
||||
assert.equal(sent[0]!.data, data);
|
||||
assert.equal(host.sendGameMessage.length, 2, 'Host API 不接受 route');
|
||||
assert.throws(() => host.sendGameMessage('', {}), /rpc.*non-empty/i);
|
||||
assert.throws(() => host.sendGameMessage(' ', {}), /rpc.*non-empty/i);
|
||||
assert.equal(sent.length, 1);
|
||||
});
|
||||
|
||||
test('adapter rejects empty and reserved GameEntry routes before exposing a Host', () => {
|
||||
for (const route of ['', ' ', 'platform', 'agent', 'room']) {
|
||||
assert.throws(
|
||||
() => makeLease({ gameEntry: entry(route) }),
|
||||
route.trim() === '' ? /route.*non-empty/i : /reserved.*route/i,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('execute exposes only the GameHost command union and delegates semantic commands', () => {
|
||||
const { host, sent } = makeLease({ store: roomStore(4, 1, 0) });
|
||||
|
||||
host.execute({ type: 'room.prepare' });
|
||||
host.execute({ type: 'room.exit' });
|
||||
|
||||
assert.equal(sent[0]!.rpc, 'player_prepare');
|
||||
assert.equal(sent[1]!.rpc, 'self_exit_room');
|
||||
assert.throws(
|
||||
() => host.execute({ type: 'room.join' } as unknown as GameHostCommand),
|
||||
/unsupported.*command/i,
|
||||
);
|
||||
assert.equal(sent.length, 2);
|
||||
});
|
||||
|
||||
test('seat mapper binds each 2, 4, and 10 seat session and round-trips every server seat', () => {
|
||||
for (const seatCount of [2, 4, 10]) {
|
||||
const selfSeat = seatCount - 1;
|
||||
const { host } = makeLease({ store: roomStore(seatCount, selfSeat) });
|
||||
assert.equal(host.seat.toView(selfSeat), 0);
|
||||
for (let serverSeat = 0; serverSeat < seatCount; serverSeat++) {
|
||||
const viewSeat = (serverSeat - selfSeat + seatCount) % seatCount;
|
||||
assert.equal(host.seat.toView(serverSeat), viewSeat);
|
||||
assert.equal(host.seat.toServer(viewSeat), serverSeat);
|
||||
}
|
||||
for (const invalid of [-1, seatCount, 0.5, Number.NaN, Number.POSITIVE_INFINITY, '0']) {
|
||||
assert.throws(() => host.seat.toView(invalid as number), /seat.*range|integer/i);
|
||||
assert.throws(() => host.seat.toServer(invalid as number), /seat.*range|integer/i);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('adapter validates descriptor seat count against the canonical room session', () => {
|
||||
const store = roomStore(4, 1);
|
||||
for (const seatCount of [0, -1, 2.5, Number.NaN, 2]) {
|
||||
assert.throws(
|
||||
() => makeLease({
|
||||
store,
|
||||
gameEntry: { ...entry(), resolveSeatCount: () => seatCount },
|
||||
}),
|
||||
/seat count/i,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('getSnapshot exposes only recursively frozen SDK DTOs and the canonical roomtype', () => {
|
||||
const { host, store } = makeLease();
|
||||
const state = store.getState();
|
||||
assert.equal(state.room.kind, 'inside');
|
||||
if (state.room.kind !== 'inside') return;
|
||||
|
||||
const snapshot = host.getSnapshot();
|
||||
|
||||
assert.deepEqual(Object.keys(snapshot).sort(), ['connection', 'room', 'seats', 'self']);
|
||||
assert.equal(snapshot.room.roomtype, state.room.roomtype);
|
||||
assert.equal(Object.isFrozen(snapshot), true);
|
||||
assert.equal(Object.isFrozen(snapshot.connection), true);
|
||||
assert.equal(Object.isFrozen(snapshot.self), true);
|
||||
assert.equal(Object.isFrozen(snapshot.room), true);
|
||||
assert.equal(Object.isFrozen(snapshot.room.roomtype), true);
|
||||
assert.equal(Object.isFrozen(snapshot.room.roomtype[1]), true);
|
||||
assert.equal(Object.isFrozen((snapshot.room.roomtype[1] as { nested: unknown[] }).nested), true);
|
||||
assert.equal(Object.isFrozen(snapshot.seats), true);
|
||||
assert.equal(Object.isFrozen(snapshot.seats[0]), true);
|
||||
assert.equal('getState' in snapshot, false);
|
||||
assert.equal('subscribe' in snapshot, false);
|
||||
assert.equal('state' in snapshot, false);
|
||||
});
|
||||
|
||||
class ObservableStore extends PlatformStore {
|
||||
private readonly observed = new Set<(state: PlatformState, previous: PlatformState) => void>();
|
||||
|
||||
override subscribe(listener: (state: PlatformState, previous: PlatformState) => void): () => void {
|
||||
const unsubscribeStore = super.subscribe(listener);
|
||||
this.observed.add(listener);
|
||||
return () => {
|
||||
this.observed.delete(listener);
|
||||
unsubscribeStore();
|
||||
};
|
||||
}
|
||||
|
||||
emitCurrent(): void {
|
||||
const state = this.getState();
|
||||
for (const listener of [...this.observed]) listener(state, state);
|
||||
}
|
||||
}
|
||||
|
||||
test('subscribe is immediate, suppresses identical public snapshots, and isolates listener failures', () => {
|
||||
const store = new ObservableStore();
|
||||
const source = fixture('player-login-room.json');
|
||||
source.data.isbattle = 0;
|
||||
source.data.roomtype = [4];
|
||||
store.applyLoginSuccess(parseLoginResponse(source.data));
|
||||
const { host } = makeLease({ store });
|
||||
const received: unknown[] = [];
|
||||
|
||||
host.subscribe((snapshot) => { received.push(snapshot); });
|
||||
store.emitCurrent();
|
||||
assert.equal(received.length, 1);
|
||||
|
||||
const listenerFailure = new Error('listener failed');
|
||||
const reports: unknown[][] = [];
|
||||
const originalConsoleError = console.error;
|
||||
console.error = (...args: unknown[]) => { reports.push(args); };
|
||||
try {
|
||||
assert.throws(
|
||||
() => host.subscribe(() => { throw listenerFailure; }),
|
||||
(error) => error === listenerFailure,
|
||||
);
|
||||
store.playerJoin(parseOtherJoinRoomPayload({
|
||||
seat: 2, playerid: 77, nickname: 'other', avatar: 'http://b',
|
||||
sex: 1, bean: 90, ip: '127.0.0.2', onstate: 0, isprepare: 0,
|
||||
}));
|
||||
assert.equal(received.length, 2);
|
||||
assert.equal(reports.length, 0, 'failed initial subscriptions must be removed');
|
||||
} finally {
|
||||
console.error = originalConsoleError;
|
||||
}
|
||||
});
|
||||
|
||||
test('a later listener failure does not block other Host subscribers', () => {
|
||||
const { host, store } = makeLease();
|
||||
const listenerFailure = new Error('later listener failed');
|
||||
let firstCalls = 0;
|
||||
let secondCalls = 0;
|
||||
const reports: unknown[][] = [];
|
||||
const originalConsoleError = console.error;
|
||||
console.error = (...args: unknown[]) => { reports.push(args); };
|
||||
try {
|
||||
host.subscribe(() => {
|
||||
firstCalls++;
|
||||
if (firstCalls > 1) throw listenerFailure;
|
||||
});
|
||||
host.subscribe(() => { secondCalls++; });
|
||||
|
||||
store.playerJoin(parseOtherJoinRoomPayload({
|
||||
seat: 2, playerid: 77, nickname: 'other', avatar: 'http://b',
|
||||
sex: 1, bean: 90, ip: '127.0.0.2', onstate: 0, isprepare: 0,
|
||||
}));
|
||||
|
||||
assert.equal(firstCalls, 2);
|
||||
assert.equal(secondCalls, 2);
|
||||
assert.equal(reports.length, 1);
|
||||
assert.equal(reports[0]!.includes(listenerFailure), true);
|
||||
} finally {
|
||||
console.error = originalConsoleError;
|
||||
}
|
||||
});
|
||||
|
||||
class ThrowingUnsubscribeStore extends PlatformStore {
|
||||
unsubscribeCalls = 0;
|
||||
|
||||
override subscribe(listener: (state: PlatformState, previous: PlatformState) => void): () => void {
|
||||
const unsubscribeStore = super.subscribe(listener);
|
||||
return () => {
|
||||
unsubscribeStore();
|
||||
this.unsubscribeCalls++;
|
||||
if (this.unsubscribeCalls === 1) throw undefined;
|
||||
throw null;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
test('invalidate clears every subscription and reference before reporting all cleanup failures', () => {
|
||||
const store = new ThrowingUnsubscribeStore();
|
||||
const source = fixture('player-login-room.json');
|
||||
source.data.isbattle = 0;
|
||||
source.data.roomtype = [4];
|
||||
store.applyLoginSuccess(parseLoginResponse(source.data));
|
||||
const { host, invalidate } = makeLease({ store });
|
||||
let callbacks = 0;
|
||||
const firstUnsubscribe = host.subscribe(() => { callbacks++; });
|
||||
const secondUnsubscribe = host.subscribe(() => { callbacks++; });
|
||||
|
||||
let cleanupError: unknown;
|
||||
try {
|
||||
invalidate();
|
||||
} catch (error) {
|
||||
cleanupError = error;
|
||||
}
|
||||
|
||||
assert.equal(store.unsubscribeCalls, 2);
|
||||
assert.deepEqual(
|
||||
(cleanupError as { errors?: readonly unknown[] })?.errors,
|
||||
[undefined, null],
|
||||
);
|
||||
assert.throws(() => host.getSnapshot(), GameSessionDisposedError);
|
||||
assert.doesNotThrow(() => firstUnsubscribe());
|
||||
assert.doesNotThrow(() => secondUnsubscribe());
|
||||
assert.doesNotThrow(() => invalidate());
|
||||
store.playerJoin(parseOtherJoinRoomPayload({
|
||||
seat: 2, playerid: 77, nickname: 'other', avatar: 'http://b',
|
||||
sex: 1, bean: 90, ip: '127.0.0.2', onstate: 0, isprepare: 0,
|
||||
}));
|
||||
assert.equal(callbacks, 2);
|
||||
});
|
||||
|
||||
test('unsubscribe and invalidate are reentry-safe and prevent callbacks already snapshotted by Store', () => {
|
||||
const { host, invalidate, store } = makeLease();
|
||||
const calls: string[] = [];
|
||||
let unsubscribeSecond = (): void => undefined;
|
||||
host.subscribe(() => {
|
||||
calls.push('first');
|
||||
if (calls.length > 2) {
|
||||
unsubscribeSecond();
|
||||
invalidate();
|
||||
}
|
||||
});
|
||||
unsubscribeSecond = host.subscribe(() => { calls.push('second'); });
|
||||
assert.deepEqual(calls, ['first', 'second']);
|
||||
|
||||
store.playerJoin(parseOtherJoinRoomPayload({
|
||||
seat: 2, playerid: 77, nickname: 'other', avatar: 'http://b',
|
||||
sex: 1, bean: 90, ip: '127.0.0.2', onstate: 0, isprepare: 0,
|
||||
}));
|
||||
|
||||
assert.deepEqual(calls, ['first', 'second', 'first']);
|
||||
assert.doesNotThrow(() => unsubscribeSecond());
|
||||
assert.doesNotThrow(() => unsubscribeSecond());
|
||||
assert.doesNotThrow(() => invalidate());
|
||||
store.playerReady(parsePlayerPreparePayload({ seat: 2 }));
|
||||
assert.deepEqual(calls, ['first', 'second', 'first']);
|
||||
});
|
||||
|
||||
test('invalidate is idempotent, old unsubscribe stays safe, and every stale Host method fails predictably', () => {
|
||||
const { host, invalidate, store, sent } = makeLease();
|
||||
let callbacks = 0;
|
||||
const unsubscribe = host.subscribe(() => { callbacks++; });
|
||||
assert.equal(callbacks, 1);
|
||||
|
||||
invalidate();
|
||||
invalidate();
|
||||
|
||||
const disposed = (call: () => unknown) => assert.throws(call, GameSessionDisposedError);
|
||||
disposed(() => host.getSnapshot());
|
||||
disposed(() => host.subscribe(() => { callbacks++; }));
|
||||
disposed(() => host.subscribe(null as never));
|
||||
disposed(() => host.sendGameMessage('play-card', {}));
|
||||
disposed(() => host.execute({ type: 'room.prepare' }));
|
||||
disposed(() => host.seat.toView(0));
|
||||
disposed(() => host.seat.toServer(0));
|
||||
assert.doesNotThrow(() => unsubscribe());
|
||||
assert.doesNotThrow(() => unsubscribe());
|
||||
|
||||
store.playerJoin(parseOtherJoinRoomPayload({
|
||||
seat: 2, playerid: 77, nickname: 'other', avatar: 'http://b',
|
||||
sex: 1, bean: 90, ip: '127.0.0.2', onstate: 0, isprepare: 0,
|
||||
}));
|
||||
assert.equal(callbacks, 1);
|
||||
assert.equal(sent.length, 0);
|
||||
});
|
||||
@@ -1,10 +1,16 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { test } from 'node:test';
|
||||
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';
|
||||
import { PlatformCommands } from '../../YouleNexus/assets/framework/platform/commands.ts';
|
||||
import { PlatformStore } from '../../YouleNexus/assets/framework/platform/stores/platform-store.ts';
|
||||
import { parseLoginResponse } from '../../YouleNexus/assets/framework/protocol/contracts/index.ts';
|
||||
import type {
|
||||
GameEntry,
|
||||
GameHost,
|
||||
GameModule,
|
||||
PlatformGameSnapshot,
|
||||
} from '../../YouleNexus/assets/framework/sdk/contracts/index.ts';
|
||||
import {
|
||||
assertGameContract,
|
||||
@@ -29,13 +35,26 @@ test('assertGameContract runs the reusable isolated session conformance suite',
|
||||
|
||||
assert.doesNotThrow(() => assertGameContract(
|
||||
entry,
|
||||
makeConformanceHost(() => { invalidations++; }, restoredDeskinfo),
|
||||
makeConformanceHost(entry, () => { invalidations++; }, restoredDeskinfo),
|
||||
));
|
||||
assert.equal(invalidations, 2);
|
||||
assert.equal(restoredDeskinfo.length, 0);
|
||||
assert.ok(nextModuleId >= 2);
|
||||
});
|
||||
|
||||
const RUNTIME_CONFIG: RuntimeConfig = Object.freeze({
|
||||
mode: 'release',
|
||||
isDebugger: false,
|
||||
identity: Object.freeze({
|
||||
agentid: 'fixture-agent', channelid: 'fixture-channel', gameid: '41',
|
||||
marketid: 'fixture-market', version: 41,
|
||||
}),
|
||||
servers: Object.freeze(['ws://fixture']),
|
||||
source: 'remote',
|
||||
gameserver: 'https://fixture/config',
|
||||
rawConfig: Object.freeze({ data: Object.freeze({ urlserver: 'fixture' }) }),
|
||||
});
|
||||
|
||||
function makeModule(id: number, onRestore: (deskinfo: unknown) => void): GameModule {
|
||||
let host: GameHost | undefined;
|
||||
let restored: unknown;
|
||||
@@ -77,25 +96,29 @@ function deskinfoFor(cycle: number): { readonly cycle: number } {
|
||||
}
|
||||
|
||||
function makeConformanceHost(
|
||||
entry: GameEntry,
|
||||
onInvalidate: () => void,
|
||||
restoredDeskinfo: unknown[],
|
||||
): () => GameContractHost {
|
||||
return () => {
|
||||
const snapshot: PlatformGameSnapshot = {
|
||||
connection: { phase: 'connected' },
|
||||
self: { playerId: 1, seat: 0 },
|
||||
room: { roomcode: 'fixture', roomtype: [], stage: 0, needprepare: 0, infinite: 0 },
|
||||
seats: [],
|
||||
};
|
||||
const login = JSON.parse(readFileSync(
|
||||
new URL('../fixtures/contracts/player-login-room.json', import.meta.url),
|
||||
'utf8',
|
||||
)) as { data: Record<string, unknown> };
|
||||
login.data.roomtype = [4];
|
||||
login.data.isbattle = 0;
|
||||
const store = new PlatformStore();
|
||||
store.applyLoginSuccess(parseLoginResponse(login.data));
|
||||
const sent: OutboundEnvelope[] = [];
|
||||
const send = { send: (envelope: OutboundEnvelope) => { sent.push(envelope); } };
|
||||
const commands = new PlatformCommands({ runtimeConfig: RUNTIME_CONFIG, store, send });
|
||||
const lease = createGameHostAdapter({ gameEntry: entry, store, commands, send });
|
||||
return {
|
||||
host: {
|
||||
seat: { toView: (seat) => seat, toServer: (seat) => seat },
|
||||
getSnapshot: () => snapshot,
|
||||
subscribe: () => () => {},
|
||||
sendGameMessage: () => {},
|
||||
execute: () => {},
|
||||
host: lease.host,
|
||||
invalidate() {
|
||||
lease.invalidate();
|
||||
onInvalidate();
|
||||
},
|
||||
invalidate: onInvalidate,
|
||||
assertRestored(deskinfo) {
|
||||
assert.equal(restoredDeskinfo.shift(), deskinfo);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user