SEC_Warehouse/tests/unit/ble/packet.spec.ts

38 lines
1.4 KiB
TypeScript
Raw 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.

import { describe, it, expect } from 'vitest'
// @ts-ignore
import { buildA7Packets, concatA7PacketData } from '@/ble/core/packet'
describe('A7 packet 分片', () => {
it('短 payload(≤18 字节)单包:包头 0xA7、总包数/序号打包进第 2 字节', () => {
const data = new Uint8Array([1, 2, 3, 4, 5])
const packets = buildA7Packets(data)
expect(packets).toHaveLength(1)
expect(packets[0][0]).toBe(0xa7)
// 总包数 1 高 4 位、序号 1 低 4 位 → 0x11
expect(packets[0][1]).toBe((1 << 4) | 1)
expect(Array.from(packets[0].slice(2))).toEqual([1, 2, 3, 4, 5])
})
it('长 payload(50 字节)按 chunkSize=18 分 3 包,序号递增', () => {
const data = new Uint8Array(50)
for (let i = 0; i < 50; i++) data[i] = i
const packets = buildA7Packets(data)
expect(packets).toHaveLength(3)
expect(packets[0][1]).toBe((3 << 4) | 1)
expect(packets[1][1]).toBe((3 << 4) | 2)
expect(packets[2][1]).toBe((3 << 4) | 3)
})
it('超出 maxChunk=15*18=270 字节时抛错', () => {
expect(() => buildA7Packets(new Uint8Array(271))).toThrow()
})
it('concatA7PacketData 按 A7 规则重组分包还原原始 payload', () => {
const data = new Uint8Array(40)
for (let i = 0; i < 40; i++) data[i] = (i * 7) % 256
const packets = buildA7Packets(data)
const restored = concatA7PacketData(packets)
expect(Array.from(restored)).toEqual(Array.from(data))
})
})