61 lines
2.7 KiB
JavaScript
61 lines
2.7 KiB
JavaScript
const { test } = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
const fs = require('node:fs')
|
|
const path = require('node:path')
|
|
|
|
const source = fs.readFileSync(path.join(__dirname, '../../src/api/goauto/syb-products.js'), 'utf8')
|
|
const names = [...source.matchAll(/export function (\w+)/g)].map(match => match[1])
|
|
const load = request => new Function('request', source.replace(/^import .+\n/gm, '').replace(/export function /g, 'function ') + `\nreturn { ${names.join(',')} }`)(request)
|
|
|
|
test('search, pagination and page-size changes use the list-only 60-second timeout', () => {
|
|
const api = load(config => config)
|
|
for (const params of [
|
|
{ page: 1, pageSize: 200, shopName: '', processStage: 'return_pending', purchaseType: 'all' },
|
|
{ page: 2, pageSize: 200 },
|
|
{ page: 1, pageSize: 500 }
|
|
]) {
|
|
const config = api.listSybProducts(params)
|
|
assert.equal(config.timeout, 60000)
|
|
assert.equal(config.params, params)
|
|
assert.equal(config.url, '/api/admin/v1/syb-products')
|
|
assert.equal(config.method, 'get')
|
|
}
|
|
})
|
|
|
|
test('existing cancellation and error options are preserved', () => {
|
|
const controller = new AbortController()
|
|
const config = load(config => config).listSybProducts({}, { signal: controller.signal, suppressNetworkError: true })
|
|
assert.equal(config.timeout, 60000)
|
|
assert.equal(config.signal, controller.signal)
|
|
assert.equal(config.suppressNetworkError, true)
|
|
controller.abort()
|
|
assert.equal(config.signal.aborted, true)
|
|
})
|
|
|
|
test('multi-shop serialization and explicit timeout override survive integration', () => {
|
|
const api = load(config => config)
|
|
const config = api.listSybProducts({ shopIds: [1, 2], page: 1 })
|
|
assert.equal(config.timeout, 60000)
|
|
assert.equal(config.paramsSerializer.serialize(config.params), 'shopIds=1&shopIds=2&page=1')
|
|
assert.equal(api.listSybProducts({}, { timeout: 15000 }).timeout, 15000)
|
|
})
|
|
|
|
test('other SYB API functions do not acquire the list timeout; global timeout stays 10 seconds', () => {
|
|
const api = load(config => config)
|
|
for (const name of names.filter(name => name !== 'listSybProducts')) {
|
|
assert.equal(api[name](1, {}).timeout, undefined, name)
|
|
}
|
|
const globalRequest = fs.readFileSync(path.join(__dirname, '../../src/utils/request.js'), 'utf8')
|
|
assert.match(globalRequest, /timeout:\s*10000\b/)
|
|
})
|
|
|
|
test('timeout and cancellation failures propagate without new retries', async () => {
|
|
for (const code of ['ECONNABORTED', 'ERR_CANCELED']) {
|
|
let calls = 0
|
|
const failure = Object.assign(new Error(code), { code })
|
|
const api = load(() => { calls++; return Promise.reject(failure) })
|
|
await assert.rejects(api.listSybProducts({}), error => error === failure)
|
|
assert.equal(calls, 1)
|
|
}
|
|
})
|