Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
61 lines
2.7 KiB
TypeScript
61 lines
2.7 KiB
TypeScript
import { test } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { resolveBootstrap } from '../../YouleNexus/assets/framework/config/bootstrap.ts';
|
||
import { BUILD_IDENTITY } from '../../YouleNexus/assets/framework/config/sources/defaults.ts';
|
||
import type { ConfigFetcher, RemoteConfig } from '../../YouleNexus/assets/framework/config/remote-config.ts';
|
||
|
||
function fetcherReturning(config: RemoteConfig): ConfigFetcher & { calls: string[] } {
|
||
const calls: string[] = [];
|
||
return { calls, fetch: async (url: string) => { calls.push(url); return config; } };
|
||
}
|
||
const throwingFetcher: ConfigFetcher = {
|
||
fetch: async () => { throw new Error('net down'); },
|
||
};
|
||
|
||
const REMOTE: RemoteConfig = { data: { player_server_tcp: '5.5.5.5:5000' } };
|
||
|
||
test('profile=local:用 profile.server 直连,不抓远程', async () => {
|
||
const fetcher = fetcherReturning(REMOTE);
|
||
const r = await resolveBootstrap({ win: {}, search: '?profile=local', fetcher, isNative: false });
|
||
assert.deepEqual(r.servers, ['ws://127.0.0.1:3088']);
|
||
assert.equal(r.rawConfig, null);
|
||
assert.equal(fetcher.calls.length, 0);
|
||
});
|
||
|
||
test('prod + H5:抓远程配置产出 servers,identity 来自 defaults', async () => {
|
||
const fetcher = fetcherReturning(REMOTE);
|
||
const r = await resolveBootstrap({ win: {}, search: '', fetcher, isNative: false });
|
||
assert.deepEqual(r.servers, ['ws://5.5.5.5:5000']);
|
||
assert.equal(r.identity.agentid, BUILD_IDENTITY.agentid);
|
||
assert.equal(r.identity.gameid, BUILD_IDENTITY.gameid);
|
||
assert.equal(fetcher.calls.length, 1);
|
||
});
|
||
|
||
test('H5 显式 URL query 覆盖身份(最高优先级)', async () => {
|
||
const fetcher = fetcherReturning(REMOTE);
|
||
const r = await resolveBootstrap({ win: {}, search: '?agentid=999&channelid=C9', fetcher, isNative: false });
|
||
assert.equal(r.identity.agentid, '999');
|
||
assert.equal(r.identity.channelid, 'C9');
|
||
});
|
||
|
||
test('原生模式:identity 来自 window.settings', async () => {
|
||
const fetcher = fetcherReturning(REMOTE);
|
||
const win = { settings: { getothername: () => 'NA', getchannelName: () => 'NC', getmarketname: () => 3 } };
|
||
const r = await resolveBootstrap({ win, search: '', fetcher, isNative: true });
|
||
assert.equal(r.identity.agentid, 'NA');
|
||
assert.equal(r.identity.channelid, 'NC');
|
||
assert.equal(r.identity.marketid, 3);
|
||
});
|
||
|
||
test('远程失败 + 有 fallbackServers → 降级使用', async () => {
|
||
const r = await resolveBootstrap({ win: {}, search: '', fetcher: throwingFetcher, isNative: false, fallbackServers: ['ws://fb:1'] });
|
||
assert.deepEqual(r.servers, ['ws://fb:1']);
|
||
});
|
||
|
||
test('远程失败 + 无 fallback → 抛 ConfigFetchError', async () => {
|
||
await assert.rejects(
|
||
resolveBootstrap({ win: {}, search: '', fetcher: throwingFetcher, isNative: false }),
|
||
/远程配置抓取失败/,
|
||
);
|
||
});
|