refactor(web): update API service, view script and add tests

Frontend:
- services/api.js: update API service client
- views/scripts/EmployeeManagementView.js: update employee management view script
- tests/api-request.test.mjs: update API request tests

Scripts:
- create_employee.sh: add employee creation script
This commit is contained in:
caoxiaozhu
2026-05-14 02:25:15 +00:00
parent 3965c1ec42
commit c0401dbd0d
4 changed files with 200 additions and 44 deletions

View File

@@ -45,7 +45,7 @@ async function testSupportsBlobResponses() {
assert.equal(payload, blob)
}
async function testInjectsAuthenticatedUserHeaders() {
async function testInjectsAuthenticatedUserHeaders() {
const sessionStorage = new Map([
[
'x-financial-auth-user',
@@ -82,16 +82,48 @@ async function testInjectsAuthenticatedUserHeaders() {
assert.equal(capturedOptions.headers['x-auth-username'], 'admin')
assert.equal(capturedOptions.headers['x-auth-name'], '系统管理员')
assert.equal(capturedOptions.headers['x-auth-role-codes'], 'manager')
assert.equal(capturedOptions.headers['x-auth-is-admin'], 'true')
}
async function run() {
await testUsesCustomContentTypeHeader()
await testSupportsBlobResponses()
await testInjectsAuthenticatedUserHeaders()
console.log('api-request tests passed')
}
assert.equal(capturedOptions.headers['x-auth-role-codes'], 'manager')
assert.equal(capturedOptions.headers['x-auth-is-admin'], 'true')
}
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 addresspassword: String should have at least 5 characters'
)
return true
}
)
}
async function run() {
await testUsesCustomContentTypeHeader()
await testSupportsBlobResponses()
await testInjectsAuthenticatedUserHeaders()
await testFormatsValidationErrors()
console.log('api-request tests passed')
}
run().catch((error) => {
console.error(error)