From 3d739c255a83fd60715861c51fe4d6caa77f843f Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 18 May 2026 23:14:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(proton):=20proof-of-concept=20migration=20?= =?UTF-8?q?=E2=80=94=20longmsg=20parity=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- packages/core/package.json | 1 + .../core/src/bridge/proto/proton/longmsg.ts | 65 ++++++++++ packages/core/tests/proton-parity.test.ts | 120 ++++++++++++++++++ packages/core/vitest.config.ts | 2 + packages/proton/package.json | 7 +- pnpm-lock.yaml | 6 + 6 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 packages/core/src/bridge/proto/proton/longmsg.ts create mode 100644 packages/core/tests/proton-parity.test.ts diff --git a/packages/core/package.json b/packages/core/package.json index c832ad06..7d7d855e 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -14,6 +14,7 @@ "typecheck": "tsc --noEmit" }, "dependencies": { + "@snowluma/proton": "workspace:*", "@snowluma/websocket": "workspace:*" }, "devDependencies": { diff --git a/packages/core/src/bridge/proto/proton/longmsg.ts b/packages/core/src/bridge/proto/proton/longmsg.ts new file mode 100644 index 00000000..6f8fa331 --- /dev/null +++ b/packages/core/src/bridge/proto/proton/longmsg.ts @@ -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>; +} diff --git a/packages/core/tests/proton-parity.test.ts b/packages/core/tests/proton-parity.test.ts new file mode 100644 index 00000000..e4f07898 --- /dev/null +++ b/packages/core/tests/proton-parity.test.ts @@ -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(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'); + }); +}); diff --git a/packages/core/vitest.config.ts b/packages/core/vitest.config.ts index cae60282..a7afd6b4 100644 --- a/packages/core/vitest.config.ts +++ b/packages/core/vitest.config.ts @@ -1,10 +1,12 @@ import { defineConfig } from 'vitest/config'; import path from 'path'; import { fileURLToPath } from 'url'; +import protobufVitePlugin from '@snowluma/proton/vite'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); export default defineConfig({ + plugins: [protobufVitePlugin()], resolve: { alias: { '@': path.resolve(__dirname, 'src') diff --git a/packages/proton/package.json b/packages/proton/package.json index d300cecb..8edd7e33 100644 --- a/packages/proton/package.json +++ b/packages/proton/package.json @@ -13,13 +13,15 @@ }, "./vite": { "types": "./src/index.ts", - "default": "./src/index.ts" + "default": "./dist/index.js" } }, "scripts": { + "build": "tsup src/index.ts --format esm --out-dir dist --target node20 --external typescript --external vite", "test": "vitest run", "test:watch": "vitest", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "prepare": "test -f dist/index.js || pnpm build" }, "dependencies": { "typescript": "catalog:" @@ -34,6 +36,7 @@ }, "devDependencies": { "@types/node": "^24.12.2", + "tsup": "^8.5.0", "vite": "catalog:", "vitest": "^4.1.4" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 822219b5..4efb92fd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,9 @@ importers: packages/core: dependencies: + '@snowluma/proton': + specifier: workspace:* + version: link:../proton '@snowluma/websocket': specifier: workspace:* version: link:../websocket @@ -73,6 +76,9 @@ importers: '@types/node': specifier: ^24.12.2 version: 24.12.2 + tsup: + specifier: ^8.5.0 + version: 8.5.1(jiti@2.6.1)(postcss@8.5.10)(tsx@4.21.0)(typescript@6.0.2) vite: specifier: 'catalog:' version: 8.0.8(@types/node@24.12.2)(esbuild@0.27.7)(jiti@2.6.1)(sass@1.99.0)(tsx@4.21.0)