/** * v1.4.13: DEVICE 注册必带 physicalParams 校验 */ import { MessageValidator } from '../message-validator'; import { ClientType, MessageType, ProgramDirection } from '../index'; const baseMsg = { type: MessageType.REGISTER, timestamp: '2026-05-09T07:30:00.000Z', version: '1.0', clientId: 'TD-test-001', }; describe('v1.4.13 RegisterMessage.clientInfo.physicalParams', () => { describe('DEVICE clientType', () => { const validParams = { width: 800, height: 480, direction: ProgramDirection.LEFT_TO_RIGHT, maxProgramSlots: 10, }; test('合法 physicalParams 通过', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: validParams } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(true); }); test('未带 physicalParams 拒收', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(false); expect(r.errors.some((e) => e.includes('physicalParams is required'))).toBe(true); }); test('clientInfo 存在但 physicalParams 缺失 拒收', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { capabilities: ['PROGRAM_PLAY'] } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(false); }); test('width=0 拒收', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: { ...validParams, width: 0 } } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(false); expect(r.errors.some((e) => e.includes('width'))).toBe(true); }); test('height 非整数 拒收', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: { ...validParams, height: 480.5 } } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(false); }); test('direction 非法值 拒收', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: { ...validParams, direction: 'TOP_TO_BOTTOM' } } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(false); expect(r.errors.some((e) => e.includes('direction'))).toBe(true); }); test('maxProgramSlots=0 拒收', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: { ...validParams, maxProgramSlots: 0 } } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(false); }); test('maxProgramSlots=11 拒收', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: { ...validParams, maxProgramSlots: 11 } } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(false); }); test('maxProgramSlots=1 通过 (lower bound)', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: { ...validParams, maxProgramSlots: 1 } } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(true); }); test('direction RIGHT_TO_LEFT 通过', () => { const msg = { ...baseMsg, clientType: ClientType.DEVICE, clientInfo: { physicalParams: { ...validParams, direction: ProgramDirection.RIGHT_TO_LEFT } } }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(true); }); }); describe('非 DEVICE clientType 不强制 physicalParams', () => { test('EDGE 不带 physicalParams 通过', () => { const msg = { ...baseMsg, clientId: 'edge-001', clientType: ClientType.EDGE }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(true); }); test('BACKEND 不带 physicalParams 通过', () => { const msg = { ...baseMsg, clientId: 'backend-001', clientType: ClientType.BACKEND }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(true); }); test('GATEWAY 不带 physicalParams 通过', () => { const msg = { ...baseMsg, clientId: 'gateway-001', clientType: ClientType.GATEWAY }; const r = MessageValidator.validateRegisterMessage(msg); expect(r.valid).toBe(true); }); }); });