fix(config): resolve remote hierarchy in memory for every profile

This commit is contained in:
2026-09-05 21:21:24 +08:00
parent 62328b6fd6
commit 053302c5df
27 changed files with 286 additions and 143 deletions
@@ -5,12 +5,12 @@ import type { ConfigFetcher } from '../../YouleNexus/assets/framework/config/rem
test('resolveBootstrap:显式 hostKind 透传严格 RuntimeConfig,并保留 profile 元数据', async () => {
const fetcher: ConfigFetcher = {
async fetch() { return { data: { urlserver: '5.5.5.5:5000' } }; },
async fetch() { return { player_server_tcp: '5.5.5.5:5000' }; },
};
const result = await resolveBootstrap({
mode: 'debug', hostKind: 'h5', win: {}, search: '?profile=local', fetcher,
});
assert.equal(result.source, 'direct');
assert.equal(result.source, 'remote');
assert.equal(result.profile.name, 'local');
assert.deepEqual(result.servers, ['ws://127.0.0.1:3088']);
});
@@ -1,3 +1,4 @@
import { remoteFixture } from '../fixtures/remote-config.ts';
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
@@ -16,15 +17,15 @@ function options(search = '?profile=local'): ResolveRuntimeConfigOptions & {
} {
const fetcher = {
calls: 0,
async fetch(): Promise<never> {
async fetch() {
this.calls += 1;
throw new Error('forbidden fetch');
return { player_server_tcp: 'remote.example:3088' };
},
};
return { mode: 'debug', hostKind: 'h5', win: {}, search, fetcher };
}
test('local debug resolves the authoritative direct config without fetching', async () => {
test('local debug fetches remote config before choosing authoritative local servers', async () => {
const input = options();
const context = await resolveLocalStartup(input);
@@ -32,7 +33,8 @@ test('local debug resolves the authoritative direct config without fetching', as
assert.equal(context.profile, 'local');
assert.equal(context.config.identity.gameid, BUILD_IDENTITY.gameid);
assert.deepEqual(context.config.servers, PROFILES.local!.servers);
assert.equal(input.fetcher.calls, 0);
assert.equal(input.fetcher.calls, 1);
assert.equal(context.config.source, 'remote');
assert.equal(Object.isFrozen(context), true);
});
@@ -107,9 +109,9 @@ function validContext(): LocalStartupContext {
isDebugger: true,
identity: BUILD_IDENTITY,
servers: PROFILES.local!.servers!,
source: 'direct',
gameserver: null,
rawConfig: null,
source: 'remote',
gameserver: PROFILES.local!.gameserver,
...remoteFixture(BUILD_IDENTITY),
},
};
}
@@ -119,7 +121,7 @@ test('context validator rejects forged config fields and malformed identity', ()
const configCases: Array<[keyof RuntimeConfig, unknown]> = [
['mode', 'release'],
['isDebugger', false],
['source', 'remote'],
['source', 'direct'],
['gameserver', 'https://example.com/config.txt'],
['rawConfig', {}],
['servers', ['ws://127.0.0.1:9999']],
@@ -23,10 +23,11 @@ test('resolveGameServer:空 gameconfig 使用配置源,非空值使用解码
);
});
test('默认 profile 是 ACTIVE_PROFILE,local 是显式 direct 源', () => {
test('默认 profile 是 ACTIVE_PROFILE,local 保持 remote 源与独立连接目标', () => {
assert.equal(resolveActiveProfile('').name, ACTIVE_PROFILE);
const local = resolveActiveProfile('?profile=local');
assert.equal(local.source, 'direct');
assert.equal(local.source, 'remote');
assert.ok(local.gameserver);
assert.deepEqual(local.servers, ['ws://127.0.0.1:3088']);
});
@@ -8,7 +8,7 @@ test('HttpConfigFetcher:POST 空 body,并无条件以 ? 拼接 cache bust',
const calls: Array<{ input: string; init?: RequestInit }> = [];
const fetcher = new HttpConfigFetcher(async (input, init) => {
calls.push({ input: String(input), init });
return new Response('{"data":{"urlserver":"10.0.0.1:3088"}}');
return new Response('{"player_server_tcp":"10.0.0.1:3088"}');
});
await fetcher.fetch('https://config.example/file.txt?existing=1', () => '123');
@@ -22,7 +22,7 @@ test('resolveRuntimeConfig:把 cacheBust 注入唯一的 HTTP 请求构造点'
const calls: Array<{ input: string; init?: RequestInit }> = [];
const fetcher = new HttpConfigFetcher(async (input, init) => {
calls.push({ input: String(input), init });
return new Response('{"data":{"urlserver":"10.0.0.1:3088"}}');
return new Response('{"player_server_tcp":"10.0.0.1:3088"}');
});
await resolveRuntimeConfig({
@@ -49,7 +49,7 @@ test('HttpConfigFetcher:非法 JSON 显式抛 ConfigParseError', async () => {
test('HttpConfigFetcher:非 200 响应即使 body 是合法 JSON 也拒绝', async () => {
const fetcher = new HttpConfigFetcher(async () => new Response(
'{"data":{"urlserver":"10.0.0.1:3088"}}',
'{"player_server_tcp":"10.0.0.1:3088"}',
{ status: 503 },
));
await assert.rejects(
@@ -1,54 +1,68 @@
import { readFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseUrlServers } from '../../YouleNexus/assets/framework/config/remote-config.ts';
const single = JSON.parse(readFileSync(
new URL('../fixtures/contracts/remote-config-single.json', import.meta.url),
'utf8',
)) as unknown;
const array = JSON.parse(readFileSync(
new URL('../fixtures/contracts/remote-config-array.json', import.meta.url),
'utf8',
)) as unknown;
test('parseUrlServers:读取 data.urlserver 字符串并补 ws://', () => {
assert.deepEqual(parseUrlServers(single), ['ws://10.0.0.1:3088']);
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('parseUrlServers:读取 data.urlserver 数组并保留已有 wss://', () => {
assert.deepEqual(parseUrlServers(array), ['ws://10.0.0.1:3088', 'wss://backup.example/ws']);
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('parseUrlServers:拒绝旧的 *_server_tcp 字段', () => {
assert.throws(
() => parseUrlServers({ data: { player_server_tcp: 'wrong:1' } }),
/data\.urlserver/,
);
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('parseUrlServers:保留原顺序和重复项,并冻结结果', () => {
const servers = parseUrlServers({ data: { urlserver: ['same:1', 'same:1', 'ws://last:2'] } });
assert.deepEqual(servers, ['ws://same:1', 'ws://same:1', 'ws://last:2']);
assert.equal(Object.isFrozen(servers), true);
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('parseUrlServers:拒绝空字符串、空数组和非字符串数组项', () => {
assert.throws(() => parseUrlServers({ data: { urlserver: '' } }), /data\.urlserver/);
assert.throws(() => parseUrlServers({ data: { urlserver: [] } }), /data\.urlserver/);
assert.throws(() => parseUrlServers({ data: { urlserver: ['ok:1', ''] } }), /data\.urlserver/);
assert.throws(() => parseUrlServers({ data: { urlserver: ['ok:1', 2] } }), /data\.urlserver/);
});
test('parseUrlServers:显式拒绝已有的非 WebSocket scheme', () => {
for (const value of [
'http://config.example/ws',
'https://config.example/ws',
'ftp://config.example/ws',
]) {
assert.throws(
() => parseUrlServers({ data: { urlserver: value } }),
/不支持.*scheme/i,
);
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);
}
});
@@ -12,7 +12,7 @@ function returning(value: unknown): ConfigFetcher & { calls: string[] } {
};
}
const REMOTE = { data: { urlserver: ['5.5.5.5:5000', '5.5.5.5:5000'] } };
const REMOTE = { player_server_tcp: ['5.5.5.5:5000', '5.5.5.5:5000'] };
test('h5:只读 query 身份,不访问原生宿主,并生成严格 remote 配置', async () => {
const fetcher = returning(REMOTE);
@@ -133,16 +133,17 @@ test('uAgent_3:拒绝宿主提供的非法身份字段类型', async () => {
);
});
test('显式 local debug:direct 字段不靠 null 推断,完整服务器来自 profile', async () => {
test('显式 local debug:远程配置保留内存,连接服务器来自 profile', async () => {
const fetcher = returning(REMOTE);
const result = await resolveRuntimeConfig({
mode: 'debug', hostKind: 'h5', win: {}, search: '?profile=local', fetcher,
});
assert.equal(result.source, 'direct');
assert.equal(result.gameserver, null);
assert.equal(result.rawConfig, null);
assert.equal(result.source, 'remote');
assert.ok(result.gameserver);
assert.equal(result.rawConfig, REMOTE);
assert.strictEqual(result.remoteConfig.raw, result.rawConfig);
assert.deepEqual(result.servers, ['ws://127.0.0.1:3088']);
assert.deepEqual(fetcher.calls, []);
assert.equal(fetcher.calls.length, 1);
});
test('远程抓取失败显式报错,不降级 direct', async () => {
@@ -152,3 +153,39 @@ test('远程抓取失败显式报错,不降级 direct', async () => {
/远程配置抓取失败/,
);
});
test('release ignores local profile but still fetches and selects remote servers', async () => {
const fetcher = returning(REMOTE);
const result = await resolveRuntimeConfig({ mode: 'release', hostKind: 'h5', win: {}, search: '?profile=local', fetcher });
assert.equal(fetcher.calls.length, 1);
assert.deepEqual(result.servers, ['ws://5.5.5.5:5000', 'ws://5.5.5.5:5000']);
assert.equal(result.isDebugger, false);
});
test('JSON parse errors retain their classification through the runtime boundary', async () => {
const { ConfigParseError } = await import('../../YouleNexus/assets/framework/config/remote-config.ts');
const error = new ConfigParseError('invalid JSON');
const fetcher = { async fetch() { throw error; } };
await assert.rejects(resolveRuntimeConfig({ mode: 'debug', hostKind: 'h5', win: {}, search: '?profile=local', fetcher }),
actual => actual === error);
});
test('local consumes explicit loopback targets after valid remote JSON even when remote candidates are absent', async () => {
const raw = { visitor_server_tcp: 'unused.example:1234', notice: 'kept' };
const fetcher = returning(raw);
const result = await resolveRuntimeConfig({ mode: 'debug', hostKind: 'h5', win: {}, search: '?profile=local', fetcher });
assert.equal(fetcher.calls.length, 1);
assert.deepEqual(result.servers, ['ws://127.0.0.1:3088']);
assert.strictEqual(result.rawConfig, raw);
assert.equal(result.remoteConfig.getValue('notice'), 'kept');
});
test('remote profiles reject missing game/player addresses without selecting visitor', async () => {
for (const mode of ['debug', 'release'] as const) {
const fetcher = returning({ visitor_server_tcp: 'unused.example:1234' });
await assert.rejects(resolveRuntimeConfig({ mode, hostKind: 'h5', win: {}, search: '', fetcher }), /game_server_tcp\/player_server_tcp/);
assert.equal(fetcher.calls.length, 1);
}
});