Files
baiyue-firstleg/scripts/admin-test-env-request-retry-contract.test.mjs
T

46 lines
1.4 KiB
JavaScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { AcceptanceRun } from './admin-test-env-acceptance.mjs'
function runContext() {
return new AcceptanceRun({ gateway: 'http://acceptance.invalid', overseasGateway: 'http://overseas.invalid' })
}
test('read-only HTTP requests retry transient network failures', async () => {
const originalFetch = globalThis.fetch
let attempts = 0
globalThis.fetch = async () => {
attempts += 1
if (attempts < 3) throw new TypeError('fetch failed')
return new Response(JSON.stringify({ code: '200', data: { ready: true } }), {
status: 200,
headers: { 'content-type': 'application/json' },
})
}
try {
const response = await runContext().request('GET', '/api/readiness')
assert.equal(attempts, 3)
assert.equal(response.http, 200)
assert.equal(response.body?.data?.ready, true)
} finally {
globalThis.fetch = originalFetch
}
})
test('mutating HTTP requests never retry an ambiguous network failure', async () => {
const originalFetch = globalThis.fetch
let attempts = 0
globalThis.fetch = async () => {
attempts += 1
throw new TypeError('fetch failed')
}
try {
const response = await runContext().request('POST', '/api/orders', { value: 1 })
assert.equal(attempts, 1)
assert.equal(response.http, 0)
assert.equal(response.raw, 'fetch failed')
} finally {
globalThis.fetch = originalFetch
}
})