feat(proton): proof-of-concept migration — longmsg parity test
Migrates a self-contained subset of bridge/proto/longmsg.ts to the new
@snowluma/proton compile-time DSL form and verifies wire-format parity
against the legacy runtime ProtoSchema encoder. End-to-end proof that
the toolchain works in core's vitest pipeline.
What's covered:
- LongMsgUid / LongMsgSettings / SendLongMsgInfo / SendLongMsgReq /
SendLongMsgResp / SendLongMsgRespResult / RecvLongMsgInfo /
RecvLongMsgReq / RecvLongMsgResp / RecvLongMsgRespResult
- The three schemas that pull in PushMsgBody (LongMsgContent /
LongMsgAction / LongMsgResult) stay on the legacy API for now, since
migrating them requires also migrating message.ts and element.ts.
Toolchain changes:
- @snowluma/proton gets a tsup build step. The ./vite subpath now
points at the built dist/index.js so it is loadable as plain Node
ESM from vite.config.ts / vitest.config.ts (the source-only .ts
layout can't be consumed from outside the Vite transform pipeline).
The . (runtime) subpath stays at TS source — it's only ever
imported from user code, which IS Vite-transformed.
- prepare script ensures dist/ exists after pnpm install.
- @snowluma/core depends on @snowluma/proton (workspace:*) and adds
protobufVitePlugin() to its vitest config so test files get
transformed.
Documented divergence:
proton follows proto3 semantics: fields equal to their default value
(0 / false / "" / empty bytes) are NOT emitted on the wire. The legacy
runtime encoder always emits them. Both forms are round-trippable
through a conforming proto3 reader. The parity test pins this
behaviour explicitly.
8/8 parity tests pass.
This commit is contained in:
6 files changed
+199
-2
No files matched your search
@@ -0,0 +1,120 @@
|
||||
// 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<LongMsgSettings>(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<SendLongMsgResp>(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<SendLongMsgReq>(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<RecvLongMsgReq>(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<RecvLongMsgResp>(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<LongMsgSettings>(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<SendLongMsgResp>(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<SendLongMsgResp>(encoded);
|
||||
expect(decoded.result?.resId).toBe('rt-2');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user