// Proof-of-concept: verify that codec generated by @snowluma/proton's Vite // plugin produces wire bytes byte-identical to the legacy runtime schema // encoder for the same input. If this passes, the migration pattern is sound // for the schemas covered here. import { describe, it, expect } from 'vitest'; import { protobuf_encode, protobuf_decode } from '@snowluma/proton'; import { protoEncode, protoDecode } from '../src/protobuf/decode'; import { SendLongMsgRespSchema, SendLongMsgReqSchema, RecvLongMsgReqSchema, RecvLongMsgRespSchema, LongMsgSettingsSchema, } from '../src/bridge/proto/longmsg'; import type { SendLongMsgResp, SendLongMsgReq, RecvLongMsgReq, RecvLongMsgResp, LongMsgSettings, } from '../src/bridge/proto/proton/longmsg'; function hex(buf: Uint8Array): string { return Buffer.from(buf).toString('hex'); } describe('proton ↔ legacy parity (longmsg subset)', () => { it('encodes LongMsgSettings identically', () => { const data: LongMsgSettings = { field1: 1, field2: 2, field3: 3, field4: 4 }; const legacy = protoEncode(data, LongMsgSettingsSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); it('encodes SendLongMsgResp (nested message) identically', () => { const data: SendLongMsgResp = { result: { resId: 'res-001' } }; const legacy = protoEncode(data, SendLongMsgRespSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); it('encodes SendLongMsgReq with bytes + nested + settings identically', () => { // NOTE: all numeric fields are non-zero — proton follows proto3 semantics // and omits default-valued fields from the wire, whereas the legacy runtime // encoder always writes them. Parity is therefore only expected when no // numeric field equals its default. See the explicit divergence test below. const data: SendLongMsgReq = { info: { type: 1, uid: { uid: 'u_test' }, groupUin: 123456, payload: new Uint8Array([0xde, 0xad, 0xbe, 0xef]), }, settings: { field1: 4, field2: 1, field3: 7, field4: 2 }, }; const legacy = protoEncode(data, SendLongMsgReqSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); it('encodes RecvLongMsgReq with bool field identically', () => { const data: RecvLongMsgReq = { info: { uid: { uid: 'u_recv' }, resId: 'r-9', acquire: true }, settings: { field1: 4, field2: 1, field3: 7, field4: 2 }, }; const legacy = protoEncode(data, RecvLongMsgReqSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); it('encodes RecvLongMsgResp (with bytes payload) identically', () => { const data: RecvLongMsgResp = { result: { resId: 'r-1', payload: new Uint8Array([1, 2, 3, 4, 5]) }, settings: { field1: 4, field2: 1, field3: 7, field4: 2 }, }; const legacy = protoEncode(data, RecvLongMsgRespSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); // ── Documented divergence: proto3 default-value omission ──────────── it('proton omits default-valued numeric fields (legacy emits them)', () => { // proto3 wire format says fields equal to their default value MUST NOT be // emitted. Proton follows that; the legacy runtime encoder always emits. // Either form is round-trippable through a conforming proto3 reader — the // missing field decodes as its default. This test pins that behaviour. const data: LongMsgSettings = { field1: 0, field2: 0, field3: 0, field4: 0 }; const legacy = protoEncode(data, LongMsgSettingsSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(''); // nothing on the wire expect(legacy.length).toBeGreaterThan(0); // legacy emits all zeros // Legacy emits explicit zero values; a proto3-conforming reader sees them // back when decoding. (Decoding the empty wire returns null because the // legacy decoder treats an empty buffer as "no message at all" — that's // also semantically equivalent to "all-default values" in proto3.) expect(protoDecode(legacy, LongMsgSettingsSchema)).toMatchObject({ field1: 0, field2: 0, field3: 0, field4: 0, }); }); // ── round-trip via decode ─────────────────────────────────────────── it('round-trips SendLongMsgResp through proton encode → legacy decode', () => { const data: SendLongMsgResp = { result: { resId: 'rt-1' } }; const encoded = protobuf_encode(data); const decoded = protoDecode(encoded, SendLongMsgRespSchema); expect(decoded?.result?.resId).toBe('rt-1'); }); it('round-trips SendLongMsgResp through legacy encode → proton decode', () => { const data: SendLongMsgResp = { result: { resId: 'rt-2' } }; const encoded = protoEncode(data, SendLongMsgRespSchema); const decoded = protobuf_decode(encoded); expect(decoded.result?.resId).toBe('rt-2'); }); });