Files
youle_cocos/cocoscreator_projects/framework-tests/config/remote-config.test.ts
T

77 lines
4.8 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
import { test } from 'node:test';
import assert from 'node:assert/strict';
import * as api from '../../YouleNexus/assets/framework/config/remote-config.ts';
const identity = { agentid: '11', gameid: '22', channelid: '33', marketid: 44, version: 1 };
const source = readFileSync(new URL('../../../projects/Game_Surface_3/js/00_Surface/12_Logic.js', import.meta.url), 'utf8');
const start = source.indexOf('function get_paravalue(o_config, paraname){');
const legacySource = source.slice(start, source.indexOf('function getConfig_Succ', start));
function view(raw: unknown, selected = identity) {
assert.equal(typeof api.createRemoteConfig, 'function', 'remote memory view must be exported');
return api.createRemoteConfig(raw, selected);
}
function legacy(raw: unknown, name: string, selected = identity) {
return runInNewContext(`${legacySource}; get_paravalue(wrapper, name)`, {
wrapper: { data: raw }, name,
GameData: { AgentId: selected.agentid, GameId: selected.gameid, ChannelId: selected.channelid, marketID: selected.marketid },
});
}
test('memory view agrees with actual legacy function across all five scopes and preserves raw objects', () => {
const scopes: Record<string, unknown>[] = [
{ hall_config: { root: true }, rootOnly: 'root', falsy: 'inherited' },
{ agentid: 11, hall_config: { agent: true }, agentOnly: 'agent', falsy: 0 },
{ gameid: 22, hall_config: { game: true }, gameOnly: 'game', falsy: false },
{ channelid: 33, hall_config: { channel: true }, channelOnly: 'channel', falsy: '' },
{ marketid: '44', hall_config: ['market'], marketOnly: 'market', falsy: null },
];
const lists = ['agentlist', 'gamelist', 'channellist', 'marketlist'];
for (let depth = 0; depth < scopes.length; depth++) {
if (depth > 0) scopes[depth - 1][lists[depth - 1]] = [scopes[depth], { ...scopes[depth], hall_config: 'duplicate must lose' }];
const result = view(scopes[0]);
assert.strictEqual(result.raw, scopes[0]);
for (const name of ['hall_config', 'rootOnly', 'agentOnly', 'gameOnly', 'channelOnly', 'marketOnly', 'falsy', 'missing']) {
assert.deepEqual(result.getValue(name), legacy(scopes[0], name));
}
assert.strictEqual(result.getValue('hall_config'), scopes[depth].hall_config);
assert.equal(result.getValue('falsy'), 'inherited');
}
assert.equal(view(scopes[0], { ...identity, marketid: 0 }).getValue('marketOnly'), null);
assert.deepEqual(view(scopes[0], { ...identity, agentid: 'missing' }).getValue('hall_config'), { root: true });
});
test('falsy root values remain null and missing identity levels stop inheritance', () => {
for (const value of [0, false, '', null]) assert.equal(view({ x: value }).getValue('x'), null);
assert.equal(view({ x: 'root', agentlist: [{ agentid: 'other', x: 'wrong' }] }).getValue('x'), 'root');
});
test('invalid root and present hierarchy fail explicitly', () => {
for (const raw of [null, [], 'json', { agentlist: {} }, { agentlist: [null] },
{ agentlist: [{ agentid: 11, gamelist: {} }] }, { agentlist: [{ agentid: 11, gamelist: [{ gameid: 22, channellist: [false] }] }] }]) {
assert.throws(() => view(raw), api.ConfigParseError);
}
});
test('server selection prefers scoped game then player and preserves order and duplicates', () => {
for (const [raw, expected] of [
[{ player_server_tcp: 'remote.example:3088' }, ['ws://remote.example:3088']],
[{ player_server_tcp: 'unused', game_server_tcp: ['same:1', 'same:1', 'wss://last/ws'] }, ['ws://same:1', 'ws://same:1', 'wss://last/ws']],
[{ game_server_tcp: '', player_server_tcp: 'player:1' }, ['ws://player:1']],
[{ game_server_tcp: 'global:1', agentlist: [{ agentid: 11, game_server_tcp: 'agent:2' }] }, ['ws://agent:2']],
] as const) {
const servers = api.parseUrlServers(view(raw));
assert.deepEqual(servers, expected); assert.ok(Object.isFrozen(servers));
}
});
test('missing or invalid chosen servers never fall back to obsolete envelope or player candidates', () => {
for (const raw of [{ data: { urlserver: 'old:1' } }, {},
...[[], ['ok:1', ''], ['ok:1', 2], true, ' ', 'http://wrong/ws', 'ws://', 'ws://user:pass@host:1', 'host:bad'].map(game_server_tcp => ({ game_server_tcp, player_server_tcp: 'valid:1' }))]) {
assert.throws(() => api.parseUrlServers(view(raw)), api.ConfigParseError);
}
});
test('WebSocket fragments including an empty trailing marker fail at the config boundary', () => {
for (const address of ['ws://127.0.0.1:3088/#invalid', 'wss://example.test/#', 'example.test:3088#']) {
assert.throws(() => api.parseUrlServers(view({ player_server_tcp: address })), api.ConfigParseError);
}
assert.deepEqual(api.parseUrlServers(view({ player_server_tcp: 'wss://example.test/path%23part' })),
['wss://example.test/path%23part']);
});