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

65 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { STARTUP_CONFIG } from '../../YouleNexus/assets/framework/config/profiles.ts';
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { HttpConfigFetcher } from '../../YouleNexus/assets/framework/config/remote-config-fetcher.ts';
import { resolveRuntimeConfig } from '../../YouleNexus/assets/framework/config/runtime-config.ts';
import { ConfigFetchError } from '../../YouleNexus/assets/framework/config/remote-config.ts';
test('HttpConfigFetcher:POST 空 body,并无条件以 ? 拼接 cache bust', async () => {
const calls: Array<{ input: string; init?: RequestInit }> = [];
const fetcher = new HttpConfigFetcher(async (input, init) => {
calls.push({ input: String(input), init });
return new Response('{"player_server_tcp":"10.0.0.1:3088"}');
});
await fetcher.fetch('https://config.example/file.txt?existing=1', () => '123');
assert.equal(calls.length, 1);
assert.equal(calls[0]?.input, 'https://config.example/file.txt?existing=1?123');
assert.deepEqual(calls[0]?.init, { method: 'POST', body: '' });
});
test('resolveRuntimeConfig:把 cacheBust 注入唯一的 HTTP 请求构造点', async () => {
const calls: Array<{ input: string; init?: RequestInit }> = [];
const fetcher = new HttpConfigFetcher(async (input, init) => {
calls.push({ input: String(input), init });
return new Response('{"player_server_tcp":"10.0.0.1:3088"}');
});
await resolveRuntimeConfig({
startupConfig: {...STARTUP_CONFIG,mode:'release',useLocalServer:false},
hostKind: 'h5',
win: {},
search: '?agentid=A&channelid=C',
fetcher,
cacheBust: () => '123',
});
assert.equal(calls.length, 1);
assert.equal(calls[0]?.input, 'https://tsgames.daoqi88.cn/config/update_jsonv2.txt?123');
assert.deepEqual(calls[0]?.init, { method: 'POST', body: '' });
});
test('HttpConfigFetcher:非法 JSON 显式抛 ConfigParseError', async () => {
const fetcher = new HttpConfigFetcher(async () => new Response('not-json'));
await assert.rejects(
fetcher.fetch('https://config.example/file.txt', () => '123'),
/远程配置 JSON 解析失败/,
);
});
test('HttpConfigFetcher:非 200 响应即使 body 是合法 JSON 也拒绝', async () => {
const fetcher = new HttpConfigFetcher(async () => new Response(
'{"player_server_tcp":"10.0.0.1:3088"}',
{ status: 503 },
));
await assert.rejects(
fetcher.fetch('https://config.example/file.txt', () => '123'),
(error: unknown) => {
assert.equal(error instanceof ConfigFetchError, true);
assert.match((error as Error).message, /status 503/);
return true;
},
);
});