'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, '校验失败不能改库'); }); });