packServer/backend/test/app/controller/workstation-canvas.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

156 lines
5.3 KiB
JavaScript
Raw Permalink 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,
);
/**
* 工作台合成画布尺寸(`workstations.canvas_width/height`)
*
* 1. 绑定管理里可以改画布宽高(默认 1920×1080 不再是死的);
* 2. 改尺寸时已摆放的摄像头**按比例缩放**,保持原来的合成构图;
* 3. 宽高 160~7680、自动取偶数(ffmpeg 合成对奇数宽高不友好)。
*/
describe('test/app/controller/workstation-canvas.test.js', () => {
const me = { id: 1, username: 'admin', nickname: '小王', status: 1, passwordChangedAt: null };
const SUBJECT_ID = 5;
const WS_ID = 3;
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 withSubject = (req) => req.set('Authorization', token()).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 = (width = 1920, height = 1080) => instance({
id: WS_ID,
subjectId: SUBJECT_ID,
code: 'WS-01',
name: '1号工作台',
wsType: 1,
status: 1,
canvasWidth: width,
canvasHeight: height,
});
const cameraLink = (id, cameraId, layout) => instance(Object.assign({
id,
workstationId: WS_ID,
cameraId,
streamType: 0,
}, layout));
function baseModels(extra = {}) {
return Object.assign({
User: { async findByPk() { return instance(me); } },
Subject: { async findByPk() { return subjectInstance(); } },
SubjectMember: { async findOne() { return memberInstance(); } },
}, extra);
}
function mockWorkstation(ws, links = []) {
mock(app, 'model', baseModels({
Workstation: { async findByPk() { return ws; } },
WorkstationCamera: { async findAll() { return links; } },
WorkstationScanChannel: { async findAll() { return []; } },
}));
}
it('改画布尺寸 → 工作台落库 + 已摆放摄像头按比例缩放', async () => {
const ws = workstation();
const full = cameraLink(31, 21, { x: 0, y: 0, width: 1920, height: 1080 });
const corner = cameraLink(32, 22, { x: 1200, y: 0, width: 720, height: 405 });
mockWorkstation(ws, [ full, corner ]);
app.mockService('labelScan', 'notifyChanged', async () => 1);
const res = await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}`)
.send({ canvasWidth: 1280, canvasHeight: 720 }))
.expect(200);
assert(res.body.data.canvasWidth === 1280 && res.body.data.canvasHeight === 720);
assert(ws.toJSON().canvasWidth === 1280 && ws.toJSON().canvasHeight === 720, '新尺寸要落库');
assert(full.toJSON().width === 1280 && full.toJSON().height === 720, JSON.stringify(full.toJSON()));
assert(full.toJSON().x === 0 && full.toJSON().y === 0);
assert(corner.toJSON().x === 800 && corner.toJSON().y === 0, JSON.stringify(corner.toJSON()));
assert(corner.toJSON().width === 480 && corner.toJSON().height === 270, JSON.stringify(corner.toJSON()));
});
it('只改宽度 → 高度保持不变', async () => {
const ws = workstation();
const link = cameraLink(31, 21, { x: 960, y: 0, width: 960, height: 1080 });
mockWorkstation(ws, [ link ]);
app.mockService('labelScan', 'notifyChanged', async () => 1);
await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}`)
.send({ canvasWidth: 960 }))
.expect(200);
assert(ws.toJSON().canvasWidth === 960 && ws.toJSON().canvasHeight === 1080);
assert(link.toJSON().x === 480 && link.toJSON().width === 480, JSON.stringify(link.toJSON()));
assert(link.toJSON().y === 0 && link.toJSON().height === 1080, '高度不受宽度变化影响');
});
it('奇数宽高自动取偶数(ffmpeg 合成要求)', async () => {
const ws = workstation();
mockWorkstation(ws, []);
app.mockService('labelScan', 'notifyChanged', async () => 1);
await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}`)
.send({ canvasWidth: 1281, canvasHeight: 721 }))
.expect(200);
assert(ws.toJSON().canvasWidth === 1280, ws.toJSON().canvasWidth);
assert(ws.toJSON().canvasHeight === 720, ws.toJSON().canvasHeight);
});
it('画布尺寸超出范围 → 422', async () => {
const ws = workstation();
mockWorkstation(ws, []);
app.mockService('labelScan', 'notifyChanged', async () => 1);
const res = await withSubject(app.httpRequest()
.put(`/api/web/workstations/${WS_ID}`)
.send({ canvasWidth: 0 }))
.expect(422);
assert(/画布宽/.test(res.body.message), JSON.stringify(res.body));
assert(ws.toJSON().canvasWidth === 1920, '校验失败不能改库');
});
});