71 行
2.3 KiB
TypeScript
71 行
2.3 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
|
|
import { apiRequest } from '../src/http'
|
|
import { getConfig, getInitializationSnapshot } from '../src/config'
|
|
import { _initializeFromResolvedConfig, awaitInitialization, XuqmSDK } from '../src/sdk'
|
|
|
|
test('public SDK has no second manual initialization entry', () => {
|
|
assert.equal('initialize' in XuqmSDK, false)
|
|
})
|
|
|
|
test('awaitInitialization retries a transient config request without leaking failure to the host', async () => {
|
|
const originalFetch = globalThis.fetch
|
|
let requestCount = 0
|
|
const requestedUrls: string[] = []
|
|
globalThis.fetch = async input => {
|
|
requestCount += 1
|
|
requestedUrls.push(String(input))
|
|
if (requestCount === 1) {
|
|
throw new TypeError('Network request failed')
|
|
}
|
|
if (requestCount === 3) {
|
|
return new Response(JSON.stringify({ data: { available: true } }), {
|
|
headers: { 'content-type': 'application/json' },
|
|
status: 200,
|
|
})
|
|
}
|
|
return new Response(
|
|
JSON.stringify({
|
|
data: {
|
|
apiUrl: 'https://api.example.test',
|
|
},
|
|
}),
|
|
{
|
|
headers: { 'content-type': 'application/json' },
|
|
status: 200,
|
|
},
|
|
)
|
|
}
|
|
|
|
try {
|
|
const autoInitialization = _initializeFromResolvedConfig(
|
|
{
|
|
appKey: 'test-app',
|
|
appName: '测试应用',
|
|
configId: '123e4567-e89b-42d3-a456-426614174000',
|
|
issuedAt: '2026-07-26T00:00:00Z',
|
|
revision: 1,
|
|
schemaVersion: 2,
|
|
serverUrl: 'https://dev.xuqinmin.com',
|
|
signingKey: 'secret',
|
|
},
|
|
{ debug: true },
|
|
)
|
|
await assert.doesNotReject(() => autoInitialization)
|
|
await assert.doesNotReject(() => awaitInitialization())
|
|
assert.equal(requestCount, 2)
|
|
assert.deepEqual(getInitializationSnapshot(), { state: 'ready', source: 'remote' })
|
|
assert.equal(getConfig().updateRequiresLogin, true)
|
|
|
|
const result = await apiRequest<{ available: boolean }>('/api/v1/rn/release-set/check', {
|
|
skipAuth: true,
|
|
})
|
|
assert.deepEqual(result, { available: true })
|
|
assert.equal(requestedUrls[2], 'https://dev.xuqinmin.com/api/v1/rn/release-set/check')
|
|
assert.equal(requestedUrls[2]?.includes('api.example.test'), false)
|
|
} finally {
|
|
globalThis.fetch = originalFetch
|
|
}
|
|
})
|