packServer/backend/test/app/controller/workstation-camera-label.test.js
liufumingren 2ed4fa8483 feat(workstation): 一台工作台支持多路面单摄像头 + 绑定管理可改画布尺寸
- 面单摄像头不再限制一台工作台一路:设置新的不清旧标记,取消最后一路时才清理虚拟通道
- 面单识别配置按 工作台+摄像头 逐路下发,多路共用上报通道;多路时虚拟通道名改用工作台名
- 绑定管理新增画布尺寸(宽×高)编辑:160~7680、自动取偶数;改尺寸时已摆放摄像头按比例缩放
- 画布组件在宽度变化时重新计算等比缩放,避免新画布溢出或缩不满
- 补充后端单测与相关文档

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-08 23:45:25 +08:00

262 lines
8.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const jwt = require('jsonwebtoken');
const { app, mock, assert } = require('egg-mock/bootstrap');
/** 伪造的 sequelize 实例(toJSON + update 直接改同一份数据) */
const instance = (data) => Object.assign(
{
toJSON: () => data,
update: async (fields) => Object.assign(data, fields),
},
data,
);
/**
* 工作台「面单摄像头」(`workstation_cameras.is_label_camera`)
*
* 1. 散单 / 团购工作台都可以设置、取消,**一台工作台可以设置多路**(互不清除);
* 2. 记录规则的区别在设备端(按工作台类型:散单取第一次、团购取最后一次),后端只存标记。
*/
describe('test/app/controller/workstation-camera-label.test.js', () => {
const me = { id: 1, username: 'admin', nickname: '小王', status: 1, passwordChangedAt: null };
const SUBJECT_ID = 5;
const WS_ID = 3;
const CAMERA_ID = 21;
const token = () => {
const cfg = app.config.jwt;
const signed = jwt.sign({ sub: String(me.id), typ: 'user' }, cfg.secret, {
algorithm: 'HS256',
expiresIn: 3600,
issuer: cfg.issuer,
audience: cfg.audience,
});
return `Bearer ${signed}`;
};
const withToken = (req) => req.set('Authorization', token());
const withSubject = (req) => withToken(req).set('X-Subject-Id', String(SUBJECT_ID));
const subjectInstance = () => instance({
id: SUBJECT_ID,
name: '测试主体',
adminUserId: 1,
status: 1,
expireAt: new Date(Date.now() + 86400000),
});
const memberInstance = () => instance({
id: 11,
subjectId: SUBJECT_ID,
userId: 1,
name: 'K0012',
role: 1,
status: 1,
});
const workstation = (wsType) => instance({
id: WS_ID,
subjectId: SUBJECT_ID,
code: 'WS-01',
name: '1号工作台',
wsType,
status: 1,
});
/** 已绑定的摄像头(详情里的 `cameras[]`) */
const cameraLink = () => instance({
id: 31,
workstationId: WS_ID,
cameraId: CAMERA_ID,
sortNo: 1,
streamType: 0,
isLabelCamera: 0,
camera: {
id: CAMERA_ID,
name: '面单特写',
channelNo: 2,
nvr: { id: 9, name: '录像机', host: '192.168.1.64', port: 8000 },
},
});
/** 主体 / 用户 / 成员三件套(鉴权与主体上下文都要用) */
function baseModels(extra = {}) {
return Object.assign({
User: { async findByPk() { return instance(me); } },
Subject: { async findByPk() { return subjectInstance(); } },
SubjectMember: { async findOne() { return memberInstance(); } },
}, extra);
}
it('PUT .../cameras/:cameraId/label:散单设置面单摄像头(不清掉其它已设的)', async () => {
const ws = workstation(1);
const link = cameraLink();
const cleared = [];
mock(app, 'model', baseModels({
Workstation: { async findByPk() { return ws; } },
WorkstationCamera: {
async findOne() { return link; },
async update(fields, options) {
cleared.push({ fields, options });
return [ 1 ];
},
async count() { return 2; },
async findAll() { return [ link ]; },
},
WorkstationScanChannel: { async findAll() { return []; } },
}));
const res = await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
.send({ isLabelCamera: true }))
.expect(200);
assert(res.body.data.cameras[0].isLabelCamera === 1, '设置后详情里要带 isLabelCamera=1');
assert(link.toJSON().isLabelCamera === 1, '绑定记录要落库为 1');
assert(cleared.length === 0, '多路面单摄像头互不影响,不能清掉其它路的标记');
});
it('PUT .../cameras/:cameraId/label:团购工作台也能设置(设备端按 wsType 决定记录规则)', async () => {
const ws = workstation(2);
const link = cameraLink();
const cleared = [];
mock(app, 'model', baseModels({
Workstation: { async findByPk() { return ws; } },
WorkstationCamera: {
async findOne() { return link; },
async update(fields, options) {
cleared.push({ fields, options });
return [ 1 ];
},
async count() { return 2; },
async findAll() { return [ link ]; },
},
WorkstationScanChannel: { async findAll() { return []; } },
}));
const res = await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
.send({ isLabelCamera: true }))
.expect(200);
assert(res.body.data.cameras[0].isLabelCamera === 1, '团购设置后详情里也要带 isLabelCamera=1');
assert(link.toJSON().isLabelCamera === 1);
assert(cleared.length === 0, '团购同样可以设置多路');
});
it('PUT .../cameras/:cameraId/label:不传 isLabelCamera = 设置;false = 取消', async () => {
const ws = workstation(1);
const link = cameraLink();
const released = [];
mock(app, 'model', baseModels({
Workstation: { async findByPk() { return ws; } },
WorkstationCamera: {
async findOne() { return link; },
async update() { return [ 1 ]; },
async count() { return 0; },
async findAll() { return [ link ]; },
},
WorkstationScanChannel: {
async findAll() { return []; },
async destroy(options) {
released.push(options.where);
return 1;
},
async count() { return 0; },
},
ScanChannel: { async destroy() { return 1; } },
}));
await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`))
.expect(200);
assert(link.toJSON().isLabelCamera === 1, '不传按「设置」处理');
assert(released.length === 0, '设置时不动通道');
const res = await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
.send({ isLabelCamera: false }))
.expect(200);
assert(link.toJSON().isLabelCamera === 0, '传 false 要取消面单');
assert(res.body.data.cameras[0].isLabelCamera === 0);
assert(released.length === 1, '取消面单要顺手清掉虚拟通道');
assert(released[0].channelId === `CAM-${WS_ID}`, JSON.stringify(released[0]));
});
it('设置/取消面单摄像头 → 立刻叫醒设备重拉配置(不能等兜底轮询)', async () => {
const ws = workstation(1);
const link = cameraLink();
const notified = [];
app.mockService('labelScan', 'notifyChanged', async subjectId => {
notified.push(subjectId);
return 1;
});
mock(app, 'model', baseModels({
Workstation: { async findByPk() { return ws; } },
WorkstationCamera: {
async findOne() { return link; },
async update() { return [ 1 ]; },
async count() { return 0; },
async findAll() { return [ link ]; },
},
WorkstationScanChannel: {
async findAll() { return []; },
async destroy() { return 1; },
async count() { return 0; },
},
ScanChannel: { async destroy() { return 1; } },
}));
await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
.send({ isLabelCamera: true }))
.expect(200);
await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
.send({ isLabelCamera: false }))
.expect(200);
assert(notified.length === 2, `设置和取消都要通知,实际 ${notified.length} 次`);
assert(notified.every(id => id === SUBJECT_ID), JSON.stringify(notified));
});
it('取消其中一路面单摄像头时,还有别的路在用 → 不清理虚拟通道', async () => {
const ws = workstation(1);
const link = cameraLink();
const released = [];
mock(app, 'model', baseModels({
Workstation: { async findByPk() { return ws; } },
WorkstationCamera: {
async findOne() { return link; },
async count() { return 1; }, // 还有另一路面单摄像头
async findAll() { return [ link ]; },
},
WorkstationScanChannel: {
async findAll() { return []; },
async destroy(options) {
released.push(options.where);
return 1;
},
async count() { return 0; },
},
ScanChannel: { async destroy() { return 1; } },
}));
const res = await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
.send({ isLabelCamera: false }))
.expect(200);
assert(link.toJSON().isLabelCamera === 0, '这一路要取消');
assert(res.body.data.cameras[0].isLabelCamera === 0);
assert(released.length === 0, '其它面单摄像头还在用虚拟通道,不能清掉');
});
});