2026-07-17 13:50:30 +08:00
|
|
|
import assert from 'node:assert/strict'
|
|
|
|
|
import test from 'node:test'
|
|
|
|
|
|
|
|
|
|
import { _initializeFromConfigFile, awaitInitialization } from '../src/sdk'
|
|
|
|
|
|
|
|
|
|
const CONFIG_VECTOR =
|
|
|
|
|
'XUQM-CONFIG-V1.AAECAwQFBgcICQoLDA0ODw.EBESExQVFhcYGRob.vhdKh3AhEdeftXCdXp9KusVySkrBNAzmdaA747uf1tVWGFnAsJeCLe2mHKk0dxxez8SRTknIbk1UuRibnXY0Gbot1fmkR-jSN2ssMJO0_zQ8dUu8'
|
|
|
|
|
|
2026-07-18 06:59:57 +08:00
|
|
|
test('awaitInitialization observes in-flight work and retries a transient config request failure', async () => {
|
2026-07-17 13:50:30 +08:00
|
|
|
const originalFetch = globalThis.fetch
|
|
|
|
|
let requestCount = 0
|
|
|
|
|
globalThis.fetch = async () => {
|
|
|
|
|
requestCount += 1
|
2026-07-18 06:59:57 +08:00
|
|
|
if (requestCount === 1) {
|
|
|
|
|
throw new TypeError('Network request failed')
|
|
|
|
|
}
|
2026-07-17 13:50:30 +08:00
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
data: {
|
|
|
|
|
apiUrl: 'https://api.example.test',
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
{
|
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
status: 200,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const autoInitialization = _initializeFromConfigFile(CONFIG_VECTOR, {
|
|
|
|
|
debug: true,
|
|
|
|
|
})
|
2026-07-18 06:59:57 +08:00
|
|
|
await assert.rejects(() => awaitInitialization(), /Network request failed/)
|
|
|
|
|
await assert.rejects(() => autoInitialization, /Network request failed/)
|
|
|
|
|
|
|
|
|
|
// 配置内容由 common 保存,扩展包再次等待时可以自行重试,无需宿主传递配置。
|
2026-07-17 13:50:30 +08:00
|
|
|
await assert.doesNotReject(() => awaitInitialization())
|
2026-07-18 06:59:57 +08:00
|
|
|
assert.equal(requestCount, 2)
|
2026-07-17 13:50:30 +08:00
|
|
|
} finally {
|
|
|
|
|
globalThis.fetch = originalFetch
|
|
|
|
|
}
|
|
|
|
|
})
|