Files
wxapp-guantong/uniapp/tests/api.test.mjs
T
Rogee ea6811b295
Build and push backend image / backend-image (push) Successful in 45s
fix: use verified WeChat login and recover sessions after cache clearing
2026-09-26 18:47:38 +08:00

61 lines
2.2 KiB
JavaScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { registerHooks, stripTypeScriptTypes } from 'node:module'
import { test } from 'node:test'
// Execute the request module with mocked uni APIs; no production requests are sent.
registerHooks({
load(url, context, nextLoad) {
if (url.endsWith('/utils/api.uts')) {
return {
format: 'module',
source: stripTypeScriptTypes(readFileSync(new URL(url), 'utf8')),
shortCircuit: true,
}
}
return nextLoad(url, context)
},
})
const { apiBase, request, post } = await import('../utils/api.uts')
const production = 'https://app01.min.wooo.host'
for (const envVersion of ['develop', 'trial', 'release']) {
test(`${envVersion} requests use the production domain`, async () => {
globalThis.uni = {
getAccountInfoSync: () => ({ miniProgram: { envVersion } }),
request(options) {
assert.equal(options.url, `${production}/api/example`)
assert.equal(options.method, 'POST')
assert.deepEqual(options.data, { value: 1 })
assert.equal(options.header['content-type'], 'application/json')
options.success({ statusCode: 200, data: { ok: true } })
},
}
assert.equal(apiBase(), production)
assert.deepEqual(await post('/api/example', { value: 1 }), { ok: true })
})
}
test('HTTP failures reject with the backend error', async () => {
globalThis.uni = {
request: (options) => options.success({ statusCode: 500, data: { error: 'backend unavailable' } }),
}
await assert.rejects(request('GET', '/api/example'), /backend unavailable/)
})
for (const data of [null, {}]) {
test(`HTTP failures without an error message reject (${JSON.stringify(data)})`, async () => {
globalThis.uni = {
request: (options) => options.success({ statusCode: 400, data }),
}
await assert.rejects(request('GET', '/api/example'), /request failed/)
})
}
test('network failures retain their diagnostic message', async () => {
globalThis.uni = {
request: (options) => options.fail({ errMsg: 'request:fail timeout' }),
}
await assert.rejects(request('GET', '/api/example'), /request:fail timeout/)
})