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
@@ -14,6 +14,7 @@
|
|||||||
"typecheck": "tsc --noEmit"
|
"typecheck": "tsc --noEmit"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@snowluma/proton": "workspace:*",
|
||||||
"@snowluma/websocket": "workspace:*"
|
"@snowluma/websocket": "workspace:*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
// Proton (compile-time) form of the long-message schemas.
|
||||||
|
//
|
||||||
|
// Mirror of the self-contained subset of `bridge/proto/longmsg.ts`. Only the
|
||||||
|
// schemas that DON'T reach into `PushMsgBodySchema` (and thus into element.ts)
|
||||||
|
// are migrated here — `LongMsgContent` / `LongMsgAction` / `LongMsgResult`
|
||||||
|
// stay on the legacy runtime API until message.ts + element.ts get migrated.
|
||||||
|
//
|
||||||
|
// This file is the proof-of-concept target for the @snowluma/proton plugin.
|
||||||
|
// The parity test at `tests/proton-parity.test.ts` asserts that proton's
|
||||||
|
// generated codec produces the exact same wire bytes as `protoEncode`.
|
||||||
|
|
||||||
|
import type { pb, uint_32, bool, bytes } from '@snowluma/proton';
|
||||||
|
|
||||||
|
export interface LongMsgUid {
|
||||||
|
uid?: pb<2, string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LongMsgSettings {
|
||||||
|
field1?: pb<1, uint_32>;
|
||||||
|
field2?: pb<2, uint_32>;
|
||||||
|
field3?: pb<3, uint_32>;
|
||||||
|
field4?: pb<4, uint_32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SendLongMsgInfo {
|
||||||
|
type?: pb<1, uint_32>;
|
||||||
|
uid?: pb<2, LongMsgUid>;
|
||||||
|
groupUin?: pb<3, uint_32>;
|
||||||
|
payload?: pb<4, bytes>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SendLongMsgReq {
|
||||||
|
info?: pb<2, SendLongMsgInfo>;
|
||||||
|
settings?: pb<15, LongMsgSettings>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SendLongMsgRespResult {
|
||||||
|
resId?: pb<3, string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SendLongMsgResp {
|
||||||
|
result?: pb<2, SendLongMsgRespResult>;
|
||||||
|
settings?: pb<15, LongMsgSettings>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RecvLongMsgInfo {
|
||||||
|
uid?: pb<1, LongMsgUid>;
|
||||||
|
resId?: pb<2, string>;
|
||||||
|
acquire?: pb<3, bool>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RecvLongMsgReq {
|
||||||
|
info?: pb<1, RecvLongMsgInfo>;
|
||||||
|
settings?: pb<15, LongMsgSettings>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RecvLongMsgRespResult {
|
||||||
|
resId?: pb<3, string>;
|
||||||
|
payload?: pb<4, bytes>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RecvLongMsgResp {
|
||||||
|
result?: pb<1, RecvLongMsgRespResult>;
|
||||||
|
settings?: pb<15, LongMsgSettings>;
|
||||||
|
}
|
||||||
@@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
import { defineConfig } from 'vitest/config';
|
import { defineConfig } from 'vitest/config';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
|
import protobufVitePlugin from '@snowluma/proton/vite';
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
plugins: [protobufVitePlugin()],
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
'@': path.resolve(__dirname, 'src')
|
'@': path.resolve(__dirname, 'src')
|
||||||
|
|||||||
@@ -13,13 +13,15 @@
|
|||||||
},
|
},
|
||||||
"./vite": {
|
"./vite": {
|
||||||
"types": "./src/index.ts",
|
"types": "./src/index.ts",
|
||||||
"default": "./src/index.ts"
|
"default": "./dist/index.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"build": "tsup src/index.ts --format esm --out-dir dist --target node20 --external typescript --external vite",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
"test:watch": "vitest",
|
"test:watch": "vitest",
|
||||||
"typecheck": "tsc --noEmit"
|
"typecheck": "tsc --noEmit",
|
||||||
|
"prepare": "test -f dist/index.js || pnpm build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"typescript": "catalog:"
|
"typescript": "catalog:"
|
||||||
@@ -34,6 +36,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^24.12.2",
|
"@types/node": "^24.12.2",
|
||||||
|
"tsup": "^8.5.0",
|
||||||
"vite": "catalog:",
|
"vite": "catalog:",
|
||||||
"vitest": "^4.1.4"
|
"vitest": "^4.1.4"
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+6
@@ -29,6 +29,9 @@ importers:
|
|||||||
|
|
||||||
packages/core:
|
packages/core:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
'@snowluma/proton':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../proton
|
||||||
'@snowluma/websocket':
|
'@snowluma/websocket':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../websocket
|
version: link:../websocket
|
||||||
@@ -73,6 +76,9 @@ importers:
|
|||||||
'@types/node':
|
'@types/node':
|
||||||
specifier: ^24.12.2
|
specifier: ^24.12.2
|
||||||
version: 24.12.2
|
version: 24.12.2
|
||||||
|
tsup:
|
||||||
|
specifier: ^8.5.0
|
||||||
|
version: 8.5.1([email protected])([email protected])([email protected])([email protected])
|
||||||
vite:
|
vite:
|
||||||
specifier: 'catalog:'
|
specifier: 'catalog:'
|
||||||
version: 8.0.8(@types/[email protected])([email protected])([email protected])([email protected])([email protected])
|
version: 8.0.8(@types/[email protected])([email protected])([email protected])([email protected])([email protected])
|
||||||
|
|||||||
Reference in new issue
Block a user