2026-05-09 05:59:46 +00:00
|
|
|
|
import assert from 'node:assert/strict'
|
|
|
|
|
|
|
|
|
|
|
|
import { apiRequest } from '../src/services/api.js'
|
|
|
|
|
|
|
|
|
|
|
|
async function testUsesCustomContentTypeHeader() {
|
|
|
|
|
|
let capturedOptions = null
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async (_url, options) => {
|
|
|
|
|
|
capturedOptions = options
|
|
|
|
|
|
return {
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return { ok: true }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await apiRequest('/knowledge/documents', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
body: 'payload',
|
|
|
|
|
|
contentType: 'application/octet-stream'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(capturedOptions.headers['Content-Type'], 'application/octet-stream')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function testSupportsBlobResponses() {
|
|
|
|
|
|
const blob = new Blob(['preview'])
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async () => ({
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async blob() {
|
|
|
|
|
|
return blob
|
|
|
|
|
|
},
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
throw new Error('json parser should not be used for blob responses')
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const payload = await apiRequest('/knowledge/documents/demo/content', {
|
|
|
|
|
|
responseType: 'blob',
|
|
|
|
|
|
contentType: null
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(payload, blob)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-14 02:25:15 +00:00
|
|
|
|
async function testInjectsAuthenticatedUserHeaders() {
|
2026-05-09 05:59:46 +00:00
|
|
|
|
const sessionStorage = new Map([
|
|
|
|
|
|
[
|
2026-05-17 08:38:41 +00:00
|
|
|
|
'x-financial-auth-user',
|
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
|
username: 'admin',
|
|
|
|
|
|
name: 'Admin User',
|
2026-06-01 17:07:14 +08:00
|
|
|
|
employeePosition: 'System Manager',
|
|
|
|
|
|
employeeGrade: 'M5',
|
|
|
|
|
|
employeeNo: 'E-001',
|
|
|
|
|
|
managerName: 'Approver User',
|
2026-05-17 08:38:41 +00:00
|
|
|
|
roleCodes: ['manager'],
|
|
|
|
|
|
isAdmin: true
|
|
|
|
|
|
})
|
2026-05-09 05:59:46 +00:00
|
|
|
|
]
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
global.window = {
|
|
|
|
|
|
sessionStorage: {
|
|
|
|
|
|
getItem(key) {
|
|
|
|
|
|
return sessionStorage.get(key) ?? null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let capturedOptions = null
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async (_url, options) => {
|
|
|
|
|
|
capturedOptions = options
|
|
|
|
|
|
return {
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return { ok: true }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await apiRequest('/knowledge/library')
|
2026-05-17 08:38:41 +00:00
|
|
|
|
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-username'], 'admin')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-name'], 'Admin User')
|
2026-06-01 17:07:14 +08:00
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-position'], 'System Manager')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-grade'], 'M5')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-employee-no'], 'E-001')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-manager-name'], 'Approver User')
|
2026-05-14 02:25:15 +00:00
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-role-codes'], 'manager')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-is-admin'], 'true')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-20 14:42:04 +08:00
|
|
|
|
async function testInjectsLegacyAdminHeaderFromSnakeCaseFlag() {
|
|
|
|
|
|
const sessionStorage = new Map([
|
|
|
|
|
|
[
|
|
|
|
|
|
'x-financial-auth-user',
|
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
|
username: 'superadmin',
|
|
|
|
|
|
name: 'superadmin',
|
|
|
|
|
|
roleCodes: ['manager'],
|
|
|
|
|
|
is_admin: true
|
|
|
|
|
|
})
|
|
|
|
|
|
]
|
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
global.window = {
|
|
|
|
|
|
sessionStorage: {
|
|
|
|
|
|
getItem(key) {
|
|
|
|
|
|
return sessionStorage.get(key) ?? null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let capturedOptions = null
|
|
|
|
|
|
|
|
|
|
|
|
global.fetch = async (_url, options) => {
|
|
|
|
|
|
capturedOptions = options
|
|
|
|
|
|
return {
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return { ok: true }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await apiRequest('/reimbursements/claims/demo', { method: 'DELETE' })
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-username'], 'superadmin')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-role-codes'], 'manager')
|
|
|
|
|
|
assert.equal(capturedOptions.headers['x-auth-is-admin'], 'true')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-14 02:25:15 +00:00
|
|
|
|
async function testFormatsValidationErrors() {
|
|
|
|
|
|
global.fetch = async () => ({
|
|
|
|
|
|
ok: false,
|
|
|
|
|
|
async json() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
detail: [
|
|
|
|
|
|
{
|
|
|
|
|
|
loc: ['body', 'email'],
|
|
|
|
|
|
msg: 'value is not a valid email address'
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
loc: ['body', 'password'],
|
|
|
|
|
|
msg: 'String should have at least 5 characters'
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
await assert.rejects(
|
|
|
|
|
|
() => apiRequest('/employees/demo', { method: 'PATCH', body: '{}' }),
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
assert.equal(
|
|
|
|
|
|
error.message,
|
|
|
|
|
|
'email: value is not a valid email address;password: String should have at least 5 characters'
|
|
|
|
|
|
)
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-17 08:38:41 +00:00
|
|
|
|
async function testRejectsWithCustomTimeoutMessage() {
|
|
|
|
|
|
global.fetch = async (_url, options) =>
|
|
|
|
|
|
new Promise((_, reject) => {
|
|
|
|
|
|
options.signal.addEventListener('abort', () => {
|
|
|
|
|
|
const error = new Error('aborted')
|
|
|
|
|
|
error.name = 'AbortError'
|
|
|
|
|
|
reject(error)
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
await assert.rejects(
|
|
|
|
|
|
() =>
|
|
|
|
|
|
apiRequest('/knowledge/library', {
|
|
|
|
|
|
timeoutMs: 1,
|
|
|
|
|
|
timeoutMessage: '知识问答整理超时,已停止等待。'
|
|
|
|
|
|
}),
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
assert.equal(error.message, '知识问答整理超时,已停止等待。')
|
2026-05-21 23:53:03 +08:00
|
|
|
|
assert.equal(error.code, 'REQUEST_TIMEOUT')
|
2026-05-17 08:38:41 +00:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-14 02:25:15 +00:00
|
|
|
|
async function run() {
|
|
|
|
|
|
await testUsesCustomContentTypeHeader()
|
|
|
|
|
|
await testSupportsBlobResponses()
|
|
|
|
|
|
await testInjectsAuthenticatedUserHeaders()
|
2026-06-20 14:42:04 +08:00
|
|
|
|
await testInjectsLegacyAdminHeaderFromSnakeCaseFlag()
|
2026-05-14 02:25:15 +00:00
|
|
|
|
await testFormatsValidationErrors()
|
2026-05-17 08:38:41 +00:00
|
|
|
|
await testRejectsWithCustomTimeoutMessage()
|
2026-05-14 02:25:15 +00:00
|
|
|
|
console.log('api-request tests passed')
|
|
|
|
|
|
}
|
2026-05-09 05:59:46 +00:00
|
|
|
|
|
|
|
|
|
|
run().catch((error) => {
|
|
|
|
|
|
console.error(error)
|
|
|
|
|
|
process.exit(1)
|
|
|
|
|
|
})
|