feat(config): unify startup mode and server selection
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { STARTUP_CONFIG } from '../../YouleNexus/assets/framework/config/profiles.ts';
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { BUILD_IDENTITY } from '../../YouleNexus/assets/framework/config/sources/defaults.ts';
|
||||
@@ -18,7 +19,7 @@ test('h5:只读 query 身份,不访问原生宿主,并生成严格 remote
|
||||
const fetcher = returning(REMOTE);
|
||||
const throwingSettings = new Proxy({}, { get() { throw new Error('native access forbidden'); } });
|
||||
const result = await resolveRuntimeConfig({
|
||||
mode: 'release',
|
||||
startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false},
|
||||
hostKind: 'h5',
|
||||
win: { settings: throwingSettings },
|
||||
search: '?agentid=HA&channelid=HC',
|
||||
@@ -41,7 +42,7 @@ test('native-settings:使用四个原生方法,gameconfig 是唯一 gameserv
|
||||
const calls: string[] = [];
|
||||
const fetcher = returning(REMOTE);
|
||||
const result = await resolveRuntimeConfig({
|
||||
mode: 'release',
|
||||
startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false},
|
||||
hostKind: 'native-settings',
|
||||
win: {
|
||||
settings: {
|
||||
@@ -65,7 +66,7 @@ test('native-settings:使用四个原生方法,gameconfig 是唯一 gameserv
|
||||
test('uAgent_3:只读 app_<name>,settings 不参与身份或 gameserver', async () => {
|
||||
const fetcher = returning(REMOTE);
|
||||
const result = await resolveRuntimeConfig({
|
||||
mode: 'release',
|
||||
startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false},
|
||||
hostKind: 'uAgent_3',
|
||||
win: {
|
||||
settings: new Proxy({}, { get() { throw new Error('settings forbidden'); } }),
|
||||
@@ -84,7 +85,7 @@ test('uAgent_3:只读 app_<name>,settings 不参与身份或 gameserver', as
|
||||
test('native-settings:不完整最终身份显式拒绝,不拿 build identity 兜底', async () => {
|
||||
await assert.rejects(
|
||||
resolveRuntimeConfig({
|
||||
mode: 'release', hostKind: 'native-settings', win: {}, search: '', fetcher: returning(REMOTE),
|
||||
startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false}, hostKind: 'native-settings', win: {}, search: '', fetcher: returning(REMOTE),
|
||||
}),
|
||||
/identity.*agentid.*channelid/i,
|
||||
);
|
||||
@@ -103,14 +104,14 @@ test('uAgent_3:所有允许 number 的身份字段都拒绝 NaN 和无穷值',
|
||||
};
|
||||
await assert.rejects(
|
||||
resolveRuntimeConfig({
|
||||
mode: 'release', hostKind: 'uAgent_3', win, search: '', fetcher: returning(REMOTE),
|
||||
startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false}, hostKind: 'uAgent_3', win, search: '', fetcher: returning(REMOTE),
|
||||
}),
|
||||
new RegExp(`identity.*${sample.field}.*finite`, 'i'),
|
||||
);
|
||||
}
|
||||
await assert.rejects(
|
||||
resolveRuntimeConfig({
|
||||
mode: 'release',
|
||||
startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false},
|
||||
hostKind: 'h5',
|
||||
win: {},
|
||||
search: '?agentid=A&channelid=C&version=NaN',
|
||||
@@ -123,7 +124,7 @@ test('uAgent_3:所有允许 number 的身份字段都拒绝 NaN 和无穷值',
|
||||
test('uAgent_3:拒绝宿主提供的非法身份字段类型', async () => {
|
||||
await assert.rejects(
|
||||
resolveRuntimeConfig({
|
||||
mode: 'release',
|
||||
startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false},
|
||||
hostKind: 'uAgent_3',
|
||||
win: { app_agent: true, app_channel: 'C', app_market: 4, app_gameconfig: '' },
|
||||
search: '',
|
||||
@@ -136,7 +137,7 @@ test('uAgent_3:拒绝宿主提供的非法身份字段类型', async () => {
|
||||
test('显式 local debug:远程配置保留内存,连接服务器来自 profile', async () => {
|
||||
const fetcher = returning(REMOTE);
|
||||
const result = await resolveRuntimeConfig({
|
||||
mode: 'debug', hostKind: 'h5', win: {}, search: '?profile=local', fetcher,
|
||||
startupConfig: STARTUP_CONFIG, hostKind: 'h5', win: {}, search: '?profile=local', fetcher,
|
||||
});
|
||||
assert.equal(result.source, 'remote');
|
||||
assert.ok(result.gameserver);
|
||||
@@ -149,7 +150,7 @@ test('显式 local debug:远程配置保留内存,连接服务器来自 prof
|
||||
test('远程抓取失败显式报错,不降级 direct', async () => {
|
||||
const fetcher: ConfigFetcher = { async fetch() { throw new Error('net down'); } };
|
||||
await assert.rejects(
|
||||
resolveRuntimeConfig({ mode: 'release', hostKind: 'h5', win: {}, search: '', fetcher }),
|
||||
resolveRuntimeConfig({ startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false}, hostKind: 'h5', win: {}, search: '', fetcher }),
|
||||
/远程配置抓取失败/,
|
||||
);
|
||||
});
|
||||
@@ -157,7 +158,7 @@ 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 });
|
||||
const result = await resolveRuntimeConfig({ startupConfig: {...STARTUP_CONFIG, mode: 'release', useLocalServer: false}, 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);
|
||||
@@ -168,14 +169,14 @@ test('JSON parse errors retain their classification through the runtime boundary
|
||||
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 }),
|
||||
await assert.rejects(resolveRuntimeConfig({ startupConfig: STARTUP_CONFIG, 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 });
|
||||
const result = await resolveRuntimeConfig({ startupConfig: STARTUP_CONFIG, 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);
|
||||
@@ -185,7 +186,33 @@ test('local consumes explicit loopback targets after valid remote JSON even when
|
||||
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/);
|
||||
await assert.rejects(resolveRuntimeConfig({ startupConfig: {...STARTUP_CONFIG, mode, useLocalServer: false}, hostKind: 'h5', win: {}, search: '', fetcher }), /game_server_tcp\/player_server_tcp/);
|
||||
assert.equal(fetcher.calls.length, 1);
|
||||
}
|
||||
});
|
||||
|
||||
for (const mode of ['debug','release'] as const) for (const useLocalServer of [true,false]) test('independent source '+mode+'/'+useLocalServer+' ignores URL selectors', async () => {
|
||||
const fetcher = returning(useLocalServer ? {visitor_server_tcp:'visitor:1'} : REMOTE);
|
||||
const source = {...STARTUP_CONFIG,mode,useLocalServer};
|
||||
const result = await resolveRuntimeConfig({startupConfig:source,hostKind:'h5',win:{},search:'?mode=debug&mode=release&profile=prod&profile=unknown',fetcher});
|
||||
assert.equal(fetcher.calls.length,1); assert.equal(result.mode,mode); assert.equal(result.isDebugger,mode==='debug');
|
||||
assert.deepEqual(result.servers,useLocalServer ? STARTUP_CONFIG.localServers : ['ws://5.5.5.5:5000','ws://5.5.5.5:5000']);
|
||||
assert.deepEqual(result.startupConfig,source); assert.ok(Object.isFrozen(result.startupConfig));
|
||||
source.localServers=['ws://localhost:9999']; assert.deepEqual(result.startupConfig.localServers,STARTUP_CONFIG.localServers);
|
||||
});
|
||||
test('default runtime starts without query configuration', async () => {
|
||||
const result=await resolveRuntimeConfig({hostKind:'h5',win:{},search:'',fetcher:returning({notice:'kept'})});
|
||||
assert.deepEqual(result.startupConfig,STARTUP_CONFIG); assert.deepEqual(result.servers,STARTUP_CONFIG.localServers);
|
||||
});
|
||||
|
||||
test('invalid source fails before remote fetch, including null and missing fields', async () => {
|
||||
for (const patch of [null, {useLocalServer: undefined}, {gameserver: ''}, {localServers: ['ws://elsewhere:3088']}]) {
|
||||
const fetcher=returning(REMOTE);
|
||||
await assert.rejects(resolveRuntimeConfig({startupConfig: patch === null ? null as any : {...STARTUP_CONFIG,...patch} as any,hostKind:'h5',win:{},search:'',fetcher}),/startup config/i);
|
||||
assert.equal(fetcher.calls.length,0);
|
||||
}
|
||||
});
|
||||
for (const mode of ['debug','release'] as const) for (const useLocalServer of [true,false]) test('remote failure never falls back '+mode+'/'+useLocalServer, async () => {
|
||||
const fetcher={async fetch(){throw new Error('remote unavailable');}};
|
||||
await assert.rejects(resolveRuntimeConfig({startupConfig:{...STARTUP_CONFIG,mode,useLocalServer},hostKind:'h5',win:{},search:'',fetcher}),/远程配置抓取失败/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user