2026-08-11 19:15:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 功能:验证工作人员关联字段过滤、资质来源校验和路由上下文传递契约。
|
2026-08-11 22:38:20 +08:00
|
|
|
|
* 版本:v1.1.0
|
2026-08-11 19:15:53 +08:00
|
|
|
|
*/
|
|
|
|
|
|
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/);
|
2026-08-11 22:38:20 +08:00
|
|
|
|
const listSearchSource = await readFile(
|
|
|
|
|
|
new URL('../src/views/shared/use-resource-list-search.ts', import.meta.url),
|
|
|
|
|
|
'utf8',
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.match(listSearchSource, /'staff_type'/);
|
2026-08-11 19:15:53 +08:00
|
|
|
|
|
|
|
|
|
|
console.log('工作人员关联与资质上下文检查通过');
|