123 lines
3.2 KiB
JavaScript
123 lines
3.2 KiB
JavaScript
/**
|
||
* 功能:验证工作人员关联字段过滤、资质来源校验和路由上下文传递契约。
|
||
* 版本:v1.0.0
|
||
*/
|
||
import assert from 'node:assert/strict';
|
||
import { readFile } from 'node:fs/promises';
|
||
import { transformWithOxc } from 'vite';
|
||
|
||
const policyURL = new URL(
|
||
'../src/api/resource-staff-relation.ts',
|
||
import.meta.url,
|
||
);
|
||
const source = await readFile(policyURL, 'utf8');
|
||
const transformed = await transformWithOxc(source, policyURL.pathname);
|
||
const moduleURL = `data:text/javascript;base64,${Buffer.from(transformed.code).toString('base64')}`;
|
||
const {
|
||
credentialOwnerValidationMessage,
|
||
resolveCredentialStaffRole,
|
||
staffRelationFilters,
|
||
staffRoleFromReturnPath,
|
||
} = await import(moduleURL);
|
||
|
||
assert.deepEqual(
|
||
staffRelationFilters(
|
||
{
|
||
relation: '/staff_account',
|
||
staffRelation: {
|
||
roles: ['delivery'],
|
||
enabledOnly: true,
|
||
workStatus: 'on_duty',
|
||
},
|
||
},
|
||
{},
|
||
),
|
||
{ role_code: 'delivery', status: '1', work_status: 'on_duty' },
|
||
'订单分配必须只查询启用且在岗的配送人员',
|
||
);
|
||
|
||
assert.deepEqual(
|
||
staffRelationFilters(
|
||
{
|
||
relation: '/staff_account',
|
||
staffRelation: {
|
||
roles: ['installer', 'delivery', 'operations'],
|
||
enabledOnly: true,
|
||
},
|
||
},
|
||
{},
|
||
),
|
||
{ role_codes: 'installer,delivery,operations', status: '1' },
|
||
'用户服务关系必须查询全部角色中的启用人员',
|
||
);
|
||
|
||
assert.deepEqual(
|
||
staffRelationFilters(
|
||
{
|
||
relation: '/staff_account',
|
||
staffRelation: { roles: 'context' },
|
||
},
|
||
{ staffType: 'installer' },
|
||
),
|
||
{ role_code: 'installer' },
|
||
'人员资质必须继承当前工作人员菜单角色',
|
||
);
|
||
|
||
assert.throws(
|
||
() => staffRelationFilters({ relation: '/staff_account' }, {}),
|
||
/缺少显式过滤策略/,
|
||
'未声明策略的工作人员关系必须立即失败',
|
||
);
|
||
|
||
assert.equal(
|
||
staffRoleFromReturnPath(
|
||
'/staff/credential?return_to=%2Fstaff%2Foperations%3Fpage%3D2',
|
||
),
|
||
'operations',
|
||
'返回路径必须能回退解析工作人员角色',
|
||
);
|
||
assert.equal(
|
||
resolveCredentialStaffRole(
|
||
'installer',
|
||
'/staff/credential?staff_type=delivery',
|
||
).error,
|
||
'工作人员角色与来源菜单不一致',
|
||
);
|
||
assert.equal(
|
||
credentialOwnerValidationMessage('staff-a', 'installer', {
|
||
identity: 'staff-a',
|
||
role_code: 'delivery',
|
||
status: 1,
|
||
}),
|
||
'工作人员角色已变化,请从当前角色菜单重新进入',
|
||
);
|
||
|
||
const resourcesSource = await readFile(
|
||
new URL('../src/api/resources.ts', import.meta.url),
|
||
'utf8',
|
||
);
|
||
assert.equal(
|
||
(resourcesSource.match(/staffRelation:/g) ?? []).length,
|
||
3,
|
||
'三个工作人员关联场景都必须显式声明策略',
|
||
);
|
||
|
||
const relationsSource = await readFile(
|
||
new URL('../src/views/resource/use-resource-relations.ts', import.meta.url),
|
||
'utf8',
|
||
);
|
||
assert.doesNotMatch(
|
||
relationsSource,
|
||
/resource === ['"]\/staff_account['"].*delivery/,
|
||
'关系加载器不得恢复全局配送人员特例',
|
||
);
|
||
|
||
const listSource = await readFile(
|
||
new URL('../src/views/shared/CrudListPage.vue', import.meta.url),
|
||
'utf8',
|
||
);
|
||
assert.match(listSource, /staff_type: staffType\.value/);
|
||
assert.match(listSource, /'staff_type'/);
|
||
|
||
console.log('工作人员关联与资质上下文检查通过');
|