桌面端
- 新增 LocalWsServer(ws://127.0.0.1:17301):hello / ping / get_channels /
start_preview / stop_preview / open_window / close_window / ptz / ptz_preset,
一条连接可同时推多路 JPEG 帧(帧头带 sid);慢命令并发派发,云台命令按连接串行,
页面断开自动清理会话与满帧率窗口
- LocalHttpServer 删除预览(MJPEG/快照/满帧率窗口)、/api/hik/channels、/api/hik/ptz*,
HTTP 17300 只保留 /api/query/{code}、/api/activate-login、/api/playback/download,
其余 POST 统一回 404 并提示已迁到 WS;不再依赖 HCNetSDK
- App 自行组装 HikStreamService -> HikPreviewService + HikNativePreviewService 注入 WS
- 后台服务加固:框架依赖(开发版)安装/启动时自动写服务环境变量 DOTNET_ROOT,
避免 LocalSystem 找不到用户目录里的 .NET 8 而报「后台服务启动超时」
前端
- 新增 api/localWs.ts(连接/断线/请求-响应/二进制帧分发),补 getChannels / openWindow /
closeWindow / ptz / ptzPreset
- CamerasView 预览只走 WS(保留「唤起本机软件 -> 等待 -> 提示安装」兜底),
满帧率窗口、云台、预置点改走 WS
- NvrsView 拉取通道改走 WS(连不上时自动唤起一次再重试)
- CameraLayoutCanvas 合成画面 4 路改 WS 推帧
- 删除 createHikPreview/closeHikPreview*/openHikWindow/closeHikWindow*/ptzControl/ptzPreset
与 isLocalServiceAlive/waitForLocalService
其他
- 移除 cameras.has_ptz(迁移 20261019233000)与相关接口/字段/UI
- 通道号按 struStreamMode 映射(IP 通道空槽导致 51 之后整体错位)
- 扫码 24 小时内只保留最后一次扫描(后上报时间更新时覆盖)
- 清理历史 DESKTOP 占位通道(迁移 20261019230000)
- README / desktop README 补 17301 与 WS 接口说明,修正已删除的 /api/health 描述
797 lines
27 KiB
JavaScript
797 lines
27 KiB
JavaScript
'use strict';
|
||
|
||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||
const jwt = require('jsonwebtoken');
|
||
const { Op } = require('sequelize');
|
||
|
||
/** 假实例:update 时把字段合并回自身 */
|
||
const instance = (data) => Object.assign(
|
||
{
|
||
toJSON: () => data,
|
||
update: async (fields) => Object.assign(data, fields),
|
||
},
|
||
data,
|
||
);
|
||
|
||
/** 假实例:额外回调 update 的字段(多次 update 时累积) */
|
||
const inst = (data, onUpdate) => Object.assign(
|
||
{
|
||
toJSON: () => data,
|
||
update: async fields => {
|
||
if (onUpdate) onUpdate(fields);
|
||
Object.assign(data, fields);
|
||
return data;
|
||
},
|
||
},
|
||
data,
|
||
);
|
||
|
||
/** 与服务端一致的密钥/签发者,造一个中控(扫描器)JWT */
|
||
function signToken(payload = {}, options = {}) {
|
||
return jwt.sign(
|
||
Object.assign({ sub: '1', sn: 'SN-HUB-01', typ: 'scanner', jti: 'jti-1' }, payload),
|
||
app.config.jwt.secret,
|
||
Object.assign({
|
||
algorithm: 'HS256',
|
||
expiresIn: '365d',
|
||
issuer: app.config.jwt.issuer,
|
||
audience: app.config.jwt.audience,
|
||
}, options),
|
||
);
|
||
}
|
||
|
||
function authScanner(overrides = {}) {
|
||
return Object.assign({ id: 1, sn: 'SN-HUB-01', status: 1, tokenJti: 'jti-1' }, overrides);
|
||
}
|
||
|
||
describe('test/app/controller/scan.test.js', () => {
|
||
const token = () => `Bearer ${signToken()}`;
|
||
|
||
it('四个扫码接口都必须带 JWT:无令牌一律 401', async () => {
|
||
await app.httpRequest().post('/api/scan/channels').send({ channels: [] }).expect(401);
|
||
await app.httpRequest().get('/api/scan/channels').expect(401);
|
||
await app.httpRequest().post('/api/scan/records').send({ records: [] }).expect(401);
|
||
await app.httpRequest().get('/api/scan/records').expect(401);
|
||
});
|
||
|
||
it('POST /api/scan/channels 首次上报:按 (中控, 通道 id) 建通道', async () => {
|
||
let created = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() {
|
||
return [];
|
||
},
|
||
async create(data) {
|
||
created = data;
|
||
return instance(Object.assign({ id: 9 }, data));
|
||
},
|
||
async update() {
|
||
return [ 1 ];
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/channels')
|
||
.set('Authorization', token())
|
||
.send({ channels: [{ channelId: '1', channelName: '1 号打包口' }] })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.created === 1 && res.body.data.updated === 0 && res.body.data.invalid === 0);
|
||
assert(created.scannerId === 1, '通道归属当前中控');
|
||
assert(created.channelId === '1');
|
||
assert(created.channelName === '1 号打包口');
|
||
assert(created.lastReportAt instanceof Date, '应记录最近上报时间');
|
||
assert(res.body.data.channels[0].channelId === '1');
|
||
});
|
||
|
||
it('POST /api/scan/channels 再次上报同通道:更新通道名(不新建)', async () => {
|
||
let args = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() {
|
||
return [ instance({ id: 7, scannerId: 1, channelId: '1', channelName: '旧名字', deletedAt: null }) ];
|
||
},
|
||
async update(fields, options) {
|
||
args = { fields, options };
|
||
return [ 1 ];
|
||
},
|
||
async create() {
|
||
throw new Error('不该新建通道');
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/channels')
|
||
.set('Authorization', token())
|
||
.send({ channelId: '1', channelName: '新名字' }) // 也支持单条上报
|
||
.expect(200);
|
||
|
||
assert(res.body.data.updated === 1 && res.body.data.created === 0);
|
||
assert(args.fields.channelName === '新名字');
|
||
assert(args.fields.lastReportAt instanceof Date);
|
||
assert(args.options.where.id === 7);
|
||
assert(args.options.paranoid === false, '软删除的通道也要能更新');
|
||
});
|
||
|
||
it('POST /api/scan/channels 曾软删除的通道会被复活(restore)', async () => {
|
||
let restored = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() {
|
||
return [ instance({ id: 3, scannerId: 1, channelId: 'CH-2', channelName: '停用过的口', deletedAt: new Date() }) ];
|
||
},
|
||
async restore(options) {
|
||
restored = options;
|
||
return 1;
|
||
},
|
||
async update() {
|
||
return [ 1 ];
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/channels')
|
||
.set('Authorization', token())
|
||
.send({ channels: [{ channelId: 'CH-2', channelName: '重新启用' }] })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.updated === 1);
|
||
assert(restored && restored.where.id === 3, '应先 restore 复活通道');
|
||
});
|
||
|
||
it('POST /api/scan/channels 同一批里重复通道只留最后一条;非法通道计入 invalid', async () => {
|
||
let created = null;
|
||
let createCount = 0;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() {
|
||
return [];
|
||
},
|
||
async create(data) {
|
||
created = data;
|
||
createCount++;
|
||
return instance(Object.assign({ id: 10 }, data));
|
||
},
|
||
async update() {
|
||
return [ 1 ];
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/channels')
|
||
.set('Authorization', token())
|
||
.send({
|
||
channels: [
|
||
{ channelId: '1', channelName: '第一次' },
|
||
{ channelId: '1', channelName: '最后一次' },
|
||
{ channelName: '没有通道 id' },
|
||
],
|
||
})
|
||
.expect(200);
|
||
|
||
assert(createCount === 1, '同一批里同 channelId 只处理一次');
|
||
assert(created.channelName === '最后一次', '应保留最后一次上报的名字');
|
||
assert(res.body.data.received === 3 && res.body.data.created === 1 && res.body.data.invalid === 1);
|
||
assert(res.body.data.errors[0].message.includes('channelId'));
|
||
});
|
||
|
||
it('POST /api/scan/channels 空数组 / 缺参 → 422', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {},
|
||
});
|
||
|
||
await app.httpRequest().post('/api/scan/channels').set('Authorization', token()).send({ channels: [] }).expect(422);
|
||
await app.httpRequest().post('/api/scan/channels').set('Authorization', token()).send({}).expect(422);
|
||
});
|
||
|
||
it('POST /api/scan/channels replace=true:列表里没有的通道被软删并解绑工作台', async () => {
|
||
let channelDestroy = null;
|
||
let linkDestroy = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll(options) {
|
||
// 第一次:按 channelId 查已存在;第二次:查本中控当前全部通道
|
||
if (options && options.attributes) {
|
||
return [ instance({ id: 1, channelId: '1' }), instance({ id: 2, channelId: '2' }) ];
|
||
}
|
||
return [ instance({ id: 1, scannerId: 1, channelId: '1', channelName: '旧名字', deletedAt: null }) ];
|
||
},
|
||
async update() {
|
||
return [ 1 ];
|
||
},
|
||
async destroy(options) {
|
||
channelDestroy = options;
|
||
return 1;
|
||
},
|
||
},
|
||
WorkstationScanChannel: {
|
||
async destroy(options) {
|
||
linkDestroy = options;
|
||
return 3;
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/channels')
|
||
.set('Authorization', token())
|
||
.send({ channels: [{ channelId: '1', channelName: '新名字' }], replace: true })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.updated === 1 && res.body.data.removed === 1 && res.body.data.unbound === 3);
|
||
assert(channelDestroy.where.scannerId === 1);
|
||
assert(channelDestroy.where.channelId[Op.in].join(',') === '2', '只删不在列表里的通道');
|
||
assert(linkDestroy.where.channelId[Op.notIn].join(',') === '1', '解绑不在列表里的工作台绑定');
|
||
});
|
||
|
||
it('POST /api/scan/channels replace=true + 空列表:允许上报并清空全部通道', async () => {
|
||
let channelDestroy = null;
|
||
let linkDestroy = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() {
|
||
return [ instance({ id: 1, channelId: '1' }), instance({ id: 2, channelId: '2' }) ];
|
||
},
|
||
async destroy(options) {
|
||
channelDestroy = options;
|
||
return 2;
|
||
},
|
||
},
|
||
WorkstationScanChannel: {
|
||
async destroy(options) {
|
||
linkDestroy = options;
|
||
return 4;
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/channels')
|
||
.set('Authorization', token())
|
||
.send({ channels: [], replace: true })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.received === 0 && res.body.data.removed === 2 && res.body.data.unbound === 4);
|
||
assert(channelDestroy.where.channelId[Op.in].sort().join(',') === '1,2', '两条都应被软删');
|
||
assert(linkDestroy.where.channelId[Op.ne] === null, '空列表 = 解绑该中控全部工作台绑定');
|
||
});
|
||
|
||
it('POST /api/scan/records 首次上报:入库并刷新通道/心跳', async () => {
|
||
let rows = null;
|
||
let scannerUpdated = null;
|
||
const scanner = inst(authScanner(), fields => { scannerUpdated = Object.assign(scannerUpdated || {}, fields); });
|
||
let channelBulk = null;
|
||
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return scanner; } },
|
||
ScanChannel: {
|
||
async findAll() {
|
||
return [ { channelId: '1' } ]; // 通道已登记
|
||
},
|
||
async bulkCreate(items) {
|
||
channelBulk = items;
|
||
return items;
|
||
},
|
||
async update() {
|
||
return [ 1 ];
|
||
},
|
||
},
|
||
ScanRecord: {
|
||
async findAll() {
|
||
return [];
|
||
},
|
||
async bulkCreate(items) {
|
||
rows = items;
|
||
return items;
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ barcode: '6901234567890', scanTime: '2026-10-16 10:20:30', channelId: '1' }] })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.received === 1 && res.body.data.inserted === 1 && res.body.data.duplicated === 0);
|
||
assert(rows[0].scannerId === 1, '记录要归属当前中控');
|
||
assert(rows[0].channelId === '1');
|
||
assert(rows[0].barcode === '6901234567890');
|
||
assert(rows[0].scanTime instanceof Date && rows[0].scanTime.getMilliseconds() === 0, '扫描时间截断到秒');
|
||
assert(rows[0].reportedAt instanceof Date, '应记录服务端接收时间');
|
||
assert(!channelBulk, '已登记的通道不该补建');
|
||
assert(scannerUpdated && scannerUpdated.lastSeenAt instanceof Date, '上报即视为心跳');
|
||
});
|
||
|
||
it('POST /api/scan/records 时间格式:支持 13 位 / 10 位时间戳,非法时间计入 invalid', async () => {
|
||
let rows = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll() { return []; },
|
||
async bulkCreate(items) {
|
||
rows = items;
|
||
return items;
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({
|
||
records: [
|
||
{ barcode: 'A', scanTime: 1792137600000, channelId: '1' }, // 13 位毫秒
|
||
{ barcode: 'B', scanTime: 1792137600, channelId: '1' }, // 10 位秒
|
||
{ barcode: 'C', scanTime: '不是时间', channelId: '1' },
|
||
{ barcode: 'D', scanTime: '2026-10-16T10:20:30', channelId: '1' },
|
||
],
|
||
})
|
||
.expect(200);
|
||
|
||
assert(rows.length === 3, '合法时间入库 3 条');
|
||
assert(res.body.data.invalid === 1 && res.body.data.errors[0].message.includes('scanTime'));
|
||
assert(rows[0].scanTime.getTime() === 1792137600000);
|
||
assert(rows[1].scanTime.getTime() === 1792137600000);
|
||
});
|
||
|
||
it('POST /api/scan/records 库里已存在 → 不入库、duplicated +1', async () => {
|
||
let inserted = false;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll() {
|
||
return [ { scannerId: 1, channelId: '1', barcode: 'X1', scanTime: new Date('2026-10-16T10:00:00') } ];
|
||
},
|
||
async bulkCreate() {
|
||
inserted = true;
|
||
return [];
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ barcode: 'X1', scanTime: '2026-10-16 10:00:00', channelId: '1' }] })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.inserted === 0 && res.body.data.duplicated === 1);
|
||
assert(!inserted, '完全相同的记录不应再次入库');
|
||
});
|
||
|
||
it('POST /api/scan/records 同一批内重复只入库一次', async () => {
|
||
let rows = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll() { return []; },
|
||
async bulkCreate(items) {
|
||
rows = items;
|
||
return items;
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({
|
||
records: [
|
||
{ barcode: 'A1', scanTime: '2026-10-16 10:00:00', channelId: '1' },
|
||
{ barcode: 'A1', scanTime: '2026-10-16 10:00:00', channelId: '1' },
|
||
],
|
||
})
|
||
.expect(200);
|
||
|
||
assert(rows.length === 1, '同批重复只留一条');
|
||
assert(res.body.data.received === 2 && res.body.data.inserted === 1 && res.body.data.duplicated === 1);
|
||
});
|
||
|
||
it('POST /api/scan/records 同一运单 24 小时内重复扫描:只入库最新一条(跨通道也算)', async () => {
|
||
let rows = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' }, { channelId: '2' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll() { return []; },
|
||
async bulkCreate(items) {
|
||
rows = items;
|
||
return items;
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({
|
||
records: [
|
||
{ barcode: 'R1', scanTime: '2026-10-19 16:30:52', channelId: '1' },
|
||
{ barcode: 'R1', scanTime: '2026-10-19 16:31:04', channelId: '1' },
|
||
{ barcode: 'R1', scanTime: '2026-10-19 17:12:37', channelId: '2' },
|
||
],
|
||
})
|
||
.expect(200);
|
||
|
||
assert(rows.length === 1, '同一运单只入库一条');
|
||
assert(rows[0].scanTime.getTime() === new Date('2026-10-19T17:12:37').getTime(), '取最后一次扫描时间');
|
||
assert(res.body.data.inserted === 1 && res.body.data.updated === 0 && res.body.data.suppressed === 2);
|
||
assert(res.body.data.duplicated === 0);
|
||
});
|
||
|
||
it('POST /api/scan/records 库里 24 小时内已有同一运单、本次更新 → 覆盖时间与工作台配置', async () => {
|
||
let inserted = false;
|
||
let updateArgs = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll(options) {
|
||
// 第二次查询(24 小时合并)返回库里的历史记录
|
||
if (options && options.where && options.where.barcode) {
|
||
return [ { id: 5, barcode: 'R2', scanTime: new Date('2026-10-19T08:00:00') } ];
|
||
}
|
||
return [];
|
||
},
|
||
async update(fields, options) {
|
||
updateArgs = { fields, options };
|
||
return [ 1 ];
|
||
},
|
||
async bulkCreate() {
|
||
inserted = true;
|
||
return [];
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ barcode: 'R2', scanTime: '2026-10-19 20:00:00', channelId: '1' }] })
|
||
.expect(200);
|
||
|
||
assert(!inserted, '已有记录时不应再入库');
|
||
assert(res.body.data.inserted === 0 && res.body.data.updated === 1 && res.body.data.suppressed === 0);
|
||
assert(updateArgs && updateArgs.options.where.id === 5, '覆盖的是库里那条记录');
|
||
assert(updateArgs.fields.scanTime.getTime() === new Date('2026-10-19T20:00:00').getTime(), '时间覆盖为最新');
|
||
assert(updateArgs.fields.reportedAt instanceof Date);
|
||
});
|
||
|
||
it('POST /api/scan/records 库里已有更晚的同一运单 → 旧上报不回退时间', async () => {
|
||
let inserted = false;
|
||
let updated = false;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll(options) {
|
||
if (options && options.where && options.where.barcode) {
|
||
return [ { id: 6, barcode: 'R4', scanTime: new Date('2026-10-19T21:00:00') } ];
|
||
}
|
||
return [];
|
||
},
|
||
async update() {
|
||
updated = true;
|
||
return [ 1 ];
|
||
},
|
||
async bulkCreate() {
|
||
inserted = true;
|
||
return [];
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ barcode: 'R4', scanTime: '2026-10-19 20:00:00', channelId: '1' }] })
|
||
.expect(200);
|
||
|
||
assert(!inserted && !updated, '更旧的上报既不入库也不覆盖');
|
||
assert(res.body.data.inserted === 0 && res.body.data.updated === 0 && res.body.data.suppressed === 1);
|
||
});
|
||
|
||
it('POST /api/scan/records 距上一条超过 24 小时 → 算新的上报,正常入库', async () => {
|
||
let rows = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll(options) {
|
||
if (options && options.where && options.where.barcode) {
|
||
// 假装数据库按 scanTime 范围过滤:24 小时外的老记录查不到
|
||
const old = new Date('2026-10-18T09:00:00');
|
||
const range = options.where.scanTime && options.where.scanTime[Op.between];
|
||
if (range && (old < range[0] || old > range[1])) return [];
|
||
return [ { id: 7, barcode: 'R3', scanTime: old } ];
|
||
}
|
||
return [];
|
||
},
|
||
async update() { return [ 1 ]; },
|
||
async bulkCreate(items) {
|
||
rows = items;
|
||
return items;
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ barcode: 'R3', scanTime: '2026-10-19 10:00:00', channelId: '1' }] })
|
||
.expect(200);
|
||
|
||
assert(rows.length === 1 && rows[0].barcode === 'R3');
|
||
assert(res.body.data.inserted === 1 && res.body.data.updated === 0 && res.body.data.suppressed === 0);
|
||
});
|
||
|
||
it('POST /api/scan/records 未知通道自动补建(通道名留空)', async () => {
|
||
let channelRows = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return []; }, // 通道从未登记
|
||
async bulkCreate(items) {
|
||
channelRows = items;
|
||
return items;
|
||
},
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll() { return []; },
|
||
async bulkCreate(items) { return items; },
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ barcode: 'B1', scanTime: '2026-10-16 11:00:00', channelId: '9' }] })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.unknownChannels.length === 1 && res.body.data.unknownChannels[0] === '9');
|
||
assert(channelRows[0].channelId === '9' && channelRows[0].channelName === '');
|
||
assert(channelRows[0].scannerId === 1);
|
||
});
|
||
|
||
it('POST /api/scan/records 唯一键冲突(并发重复)→ 退化成逐条插入,重复计数', async () => {
|
||
let createCount = 0;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll() { return []; },
|
||
async bulkCreate() {
|
||
const err = new Error('Duplicate entry');
|
||
err.name = 'SequelizeUniqueConstraintError';
|
||
throw err;
|
||
},
|
||
async create() {
|
||
createCount++;
|
||
if (createCount === 1) {
|
||
const err = new Error('Duplicate entry');
|
||
err.parent = { code: 'ER_DUP_ENTRY' };
|
||
throw err;
|
||
}
|
||
return {};
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({
|
||
records: [
|
||
{ barcode: 'C1', scanTime: '2026-10-16 12:00:00', channelId: '1' },
|
||
{ barcode: 'C2', scanTime: '2026-10-16 12:00:01', channelId: '1' },
|
||
],
|
||
})
|
||
.expect(200);
|
||
|
||
assert(res.body.data.inserted === 1 && res.body.data.duplicated === 1);
|
||
});
|
||
|
||
it('POST /api/scan/records 全部非法 / 空数组 → 分别返回 invalid 与 422', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {},
|
||
ScanRecord: {},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ channelId: '1' }] }) // 缺条码与时间
|
||
.expect(200);
|
||
assert(res.body.data.inserted === 0 && res.body.data.invalid === 1);
|
||
|
||
await app.httpRequest().post('/api/scan/records').set('Authorization', token()).send({ records: [] }).expect(422);
|
||
});
|
||
|
||
it('POST /api/scan/records 支持 JSON 字符串形式的上报体', async () => {
|
||
let rows = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner()); } },
|
||
ScanChannel: {
|
||
async findAll() { return [ { channelId: '1' } ]; },
|
||
async bulkCreate(items) { return items; },
|
||
async update() { return [ 1 ]; },
|
||
},
|
||
ScanRecord: {
|
||
async findAll() { return []; },
|
||
async bulkCreate(items) {
|
||
rows = items;
|
||
return items;
|
||
},
|
||
},
|
||
});
|
||
|
||
await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: JSON.stringify([{ barcode: 'S1', scanTime: '2026-10-16 13:00:00', channelId: '1' }]) })
|
||
.expect(200);
|
||
|
||
assert(rows.length === 1 && rows[0].barcode === 'S1');
|
||
});
|
||
|
||
it('GET /api/scan/channels 只返回当前中控的通道', async () => {
|
||
let options = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async findByPk() { return instance(authScanner()); },
|
||
async findAll() { return []; },
|
||
},
|
||
ScanChannel: {
|
||
async findAndCountAll(opts) {
|
||
options = opts;
|
||
return {
|
||
rows: [ instance({ id: 1, scannerId: 1, channelId: '1', channelName: '1 号口', scanner: { sn: 'SN-HUB-01' } }) ],
|
||
count: 1,
|
||
};
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest().get('/api/scan/channels?pageSize=5').set('Authorization', token()).expect(200);
|
||
assert(options.where.scannerId === 1, '必须按当前中控过滤');
|
||
assert(options.limit === 5);
|
||
assert(res.body.data.total === 1 && res.body.data.list[0].channelName === '1 号口');
|
||
});
|
||
|
||
it('GET /api/scan/records 支持通道/条码/时间过滤且只查当前中控', async () => {
|
||
let options = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async findByPk() { return instance(authScanner()); },
|
||
async findAll() { return []; },
|
||
},
|
||
ScanRecord: {
|
||
async findAndCountAll(opts) {
|
||
options = opts;
|
||
return {
|
||
rows: [ instance({ id: 3, scannerId: 1, channelId: '1', barcode: 'B1', scanTime: new Date() }) ],
|
||
count: 1,
|
||
};
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.get('/api/scan/records?channelId=1&barcode=B1&startTime=2026-10-16&endTime=2026-10-17')
|
||
.set('Authorization', token())
|
||
.expect(200);
|
||
|
||
assert(options.where.scannerId === 1);
|
||
assert(options.where.channelId === '1');
|
||
assert(String(options.where.barcode[Op.like]) === '%B1%');
|
||
assert(options.where.scanTime[Op.gte] instanceof Date && options.where.scanTime[Op.lte] instanceof Date);
|
||
assert(options.order[0][0] === 'scanTime' && options.order[0][1] === 'DESC');
|
||
assert(res.body.data.total === 1 && res.body.data.list[0].barcode === 'B1');
|
||
});
|
||
|
||
it('被禁用的中控调用上报接口 → 403', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner({ status: 0 })); } },
|
||
ScanChannel: {},
|
||
ScanRecord: {},
|
||
});
|
||
await app.httpRequest()
|
||
.post('/api/scan/channels')
|
||
.set('Authorization', token())
|
||
.send({ channels: [{ channelId: '1' }] })
|
||
.expect(403);
|
||
});
|
||
|
||
it('已被轮换的旧 JWT 调上报接口 → 401(与 scanner 接口一致)', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance(authScanner({ tokenJti: 'new-jti' })); } },
|
||
ScanChannel: {},
|
||
ScanRecord: {},
|
||
});
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/scan/records')
|
||
.set('Authorization', token())
|
||
.send({ records: [{ barcode: 'A', scanTime: '2026-10-16 10:00:00', channelId: '1' }] })
|
||
.expect(401);
|
||
assert(res.body.message.includes('已失效'));
|
||
});
|
||
|
||
it('老的 sn + secret 登录入口已下线(设备统一走扫码登录)', async () => {
|
||
const res = await app.httpRequest().post('/api/scanner/login').send({ sn: 'SN-HUB-01', secret: 'x' }).expect(404);
|
||
assert(res.body.ok === false && res.body.message === '接口不存在', JSON.stringify(res.body));
|
||
});
|
||
});
|