Files
Web_BLS_Register_Server/bls-register-backend/tests/processor.test.js

64 lines
2.1 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import { buildRowsFromPayload } from '../src/processor/index.js';
describe('Register Processor', () => {
const basePayload = {
ts_ms: 1770000235000,
upgrade_ts_ms: 1770001235000,
hotel_id: 1085,
room_id: '8888',
device_id: '091123987456',
ip: '10.1.2.3:5678',
is_send: 0,
app_version: '2.1.0',
launcher_version: '1.0.0',
config_version: 'cfg-v8'
};
it('should map payload into register and room-status rows', () => {
const rows = buildRowsFromPayload(basePayload);
expect(rows.registerRows).toHaveLength(1);
expect(rows.roomStatusRows).toHaveLength(1);
expect(rows.registerRows[0].hotel_id).toBe(1085);
expect(rows.registerRows[0].room_id).toBe('8888');
expect(rows.registerRows[0].device_id).toBe('091123987456');
expect(rows.roomStatusRows[0].ip).toBe('10.1.2.3:5678');
expect(rows.roomStatusRows[0].register_ts_ms).toBe(1770000235000);
expect(rows.roomStatusRows[0].upgrade_ts_ms).toBe(1770001235000);
});
it('should force hotel_id to 0 when out of int2 range', () => {
const rows = buildRowsFromPayload({ ...basePayload, hotel_id: 60000 });
expect(rows.registerRows[0].hotel_id).toBe(0);
expect(rows.roomStatusRows[0].hotel_id).toBe(0);
});
it('should skip room_status updates when is_send is not 0', () => {
const rows = buildRowsFromPayload({ ...basePayload, is_send: 1 });
expect(rows.registerRows).toHaveLength(1);
expect(rows.registerRows[0].is_send).toBe(1);
expect(rows.roomStatusRows).toHaveLength(0);
});
it('should convert udp_raw byte array to base64 text', () => {
const rows = buildRowsFromPayload({
...basePayload,
udp_raw: [1, 2, 3, 4]
});
expect(rows.registerRows[0].udp_raw).toBe('AQIDBA==');
});
it('should strip NUL bytes from text fields', () => {
const rows = buildRowsFromPayload({
...basePayload,
app_version: 'v1\u0000\u0000.2',
room_num_remark: 'A\u0000B'
});
expect(rows.registerRows[0].app_version).toBe('v1.2');
expect(rows.registerRows[0].room_num_remark).toBe('AB');
});
});