From c543ce922780e1829d924edeafdb26af0aa457ef Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 18 May 2026 23:47:42 +0800 Subject: [PATCH] feat(proton): add proton/oidb.ts with generic OidbBase envelope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 1 of the OIDB migration: stages the proton-side type definitions and pins their wire equivalence to the legacy makeOidbBaseSchema() runtime factory. No call sites are switched yet — that lands in phase 2 along with the proton/oidb-action.ts schema mirror and the runOidb refactor. The legacy bridge/proto/oidb.ts exports a `makeOidbBaseSchema(body)` factory function that synthesizes the OidbSvcTrpcTcpBase envelope ProtoSchema at runtime. Proton is compile-time, so the natural form is the generic interface OidbBase { command, subCommand, errorCode, body: T, errorMsg, properties, reserved } which the plugin monomorphizes per-instantiation. This is exactly the "generic template with generic-instantiated field" usage that this package's earlier edge-case fixes #2 and #3 were designed to support, and the new parity test pins that they actually work on the real shape (a fully populated OidbSvcTrpcTcp0xFE5_2Response with nested groupOwner/customInfo). Also added: - OidbProperty (for the envelope.properties repeated field) - OidbBaseMeta — envelope-only view for fire-and-check OIDB calls that just need to peek at errorCode without decoding the body - All 8 concrete schemas from oidb.ts (FriendList, GroupList, GroupMemberList, GroupRequest families) Verification: - 2 new parity tests * OidbBase ↔ makeOidbBaseSchema byte equivalence * round-trip encode → decode preserves nested body - 602+2 = 604 core tests pass (network-manager file still env-fails on missing native binary) - tsc --noEmit clean --- packages/core/src/bridge/proto/proton/oidb.ts | 180 ++++++++++++++++++ packages/core/tests/proton-parity.test.ts | 54 ++++++ 2 files changed, 234 insertions(+) create mode 100644 packages/core/src/bridge/proto/proton/oidb.ts diff --git a/packages/core/src/bridge/proto/proton/oidb.ts b/packages/core/src/bridge/proto/proton/oidb.ts new file mode 100644 index 00000000..43fc14a6 --- /dev/null +++ b/packages/core/src/bridge/proto/proton/oidb.ts @@ -0,0 +1,180 @@ +// Proton (compile-time) form of bridge/proto/oidb.ts. +// +// The legacy file exports a `makeOidbBaseSchema(body)` factory that builds the +// OidbSvcTrpcTcpBase envelope at runtime; here that wrapper is expressed +// once as the generic `OidbBase` interface. Every existing +// `makeOidbBaseSchema(InnerSchema)` call site becomes `OidbBase` +// at the type level, and the proton plugin monomorphizes the codec +// per-instantiation at build time. +// +// `OidbBaseMeta` is the envelope-only view used by fire-and-check calls +// that want to peek at the error code without decoding the body. + +import type { pb, pb_repeated, int_32, uint_32, uint_64, bytes } from '@snowluma/proton'; + +// ── OidbSvcTrpcTcpBase wrapper ────────────────────────────────────── + +export interface OidbProperty { + key?: pb<1, string>; + value?: pb<2, bytes>; +} + +export interface OidbBase { + command?: pb<1, uint_32>; + subCommand?: pb<2, uint_32>; + errorCode?: pb<3, uint_32>; + body?: pb<4, TBody>; + errorMsg?: pb<5, string>; + properties?: pb_repeated<11, OidbProperty>; + reserved?: pb<12, int_32>; +} + +/** + * Envelope-only view used by fire-and-check calls — proto3 wire format + * silently skips unknown fields, so decoding any OidbBase message with + * this shape returns just the metadata and ignores the inner body. + */ +export interface OidbBaseMeta { + command?: pb<1, uint_32>; + subCommand?: pb<2, uint_32>; + errorCode?: pb<3, uint_32>; + errorMsg?: pb<5, string>; + reserved?: pb<12, int_32>; +} + +// ── Friend list (0xFD4_1) ─────────────────────────────────────────── + +export interface OidbFriendProperty { + code?: pb<1, uint_32>; + value?: pb<2, string>; +} + +export interface OidbFriendLayer1 { + properties?: pb_repeated<2, OidbFriendProperty>; +} + +export interface OidbFriendAdditional { + type?: pb<1, uint_32>; + layer1?: pb<2, OidbFriendLayer1>; +} + +export interface OidbFriend { + uid?: pb<1, string>; + customGroup?: pb<2, uint_32>; + uin?: pb<3, uint_32>; + additional?: pb_repeated<10001, OidbFriendAdditional>; +} + +export interface OidbSvcTrpcTcp0xFD4_1ResponseUin { + uin?: pb<1, uint_32>; +} + +export interface OidbSvcTrpcTcp0xFD4_1Response { + next?: pb<2, OidbSvcTrpcTcp0xFD4_1ResponseUin>; + displayFriendCount?: pb<3, uint_32>; + timestamp?: pb<6, uint_32>; + selfUin?: pb<7, uint_32>; + friends?: pb_repeated<101, OidbFriend>; + groups?: pb_repeated<102, OidbFriendProperty>; +} + +// ── Group list (0xFE5_2) ──────────────────────────────────────────── + +export interface OidbSvcTrpcTcp0xFE5_2Member { + uid?: pb<2, string>; +} + +export interface OidbSvcTrpcTcp0xFE5_2GroupInfo { + groupOwner?: pb<1, OidbSvcTrpcTcp0xFE5_2Member>; + createdTime?: pb<2, uint_32>; + memberMax?: pb<3, uint_32>; + memberCount?: pb<4, uint_32>; + groupName?: pb<5, string>; + description?: pb<18, string>; + question?: pb<19, string>; + announcement?: pb<30, string>; +} + +export interface OidbSvcTrpcTcp0xFE5_2CustomInfo { + remark?: pb<3, string>; +} + +export interface OidbSvcTrpcTcp0xFE5_2Group { + groupUin?: pb<3, uint_32>; + info?: pb<4, OidbSvcTrpcTcp0xFE5_2GroupInfo>; + customInfo?: pb<5, OidbSvcTrpcTcp0xFE5_2CustomInfo>; +} + +export interface OidbSvcTrpcTcp0xFE5_2Response { + groups?: pb_repeated<2, OidbSvcTrpcTcp0xFE5_2Group>; +} + +// ── Group member list (0xFE7_3) ───────────────────────────────────── + +export interface OidbSvcTrpcTcp0xFE7_3Uin { + uid?: pb<2, string>; + uin?: pb<4, uint_32>; +} + +export interface OidbSvcTrpcTcp0xFE7_3Card { + memberCard?: pb<2, string>; +} + +export interface OidbSvcTrpcTcp0xFE7_3Level { + infos?: pb_repeated<1, uint_32>; + level?: pb<2, uint_32>; +} + +export interface OidbSvcTrpcTcp0xFE7_3Member { + uin?: pb<1, OidbSvcTrpcTcp0xFE7_3Uin>; + memberName?: pb<10, string>; + specialTitle?: pb<17, string>; + memberCard?: pb<11, OidbSvcTrpcTcp0xFE7_3Card>; + level?: pb<12, OidbSvcTrpcTcp0xFE7_3Level>; + joinTimestamp?: pb<100, uint_32>; + lastMsgTimestamp?: pb<101, uint_32>; + shutUpTimestamp?: pb<102, uint_32>; + permission?: pb<107, uint_32>; +} + +export interface OidbSvcTrpcTcp0xFE7_3Response { + groupUin?: pb<1, uint_32>; + members?: pb_repeated<2, OidbSvcTrpcTcp0xFE7_3Member>; + field3?: pb<3, uint_32>; + memberChangeSeq?: pb<5, uint_32>; + memberCardChangeSeq?: pb<6, uint_32>; + token?: pb<15, string>; +} + +// ── Group request (0x10C0) ────────────────────────────────────────── + +export interface OidbSvcTrpcTcp0x10C0ResponseUser { + uid?: pb<1, string>; + name?: pb<2, string>; +} + +export interface OidbSvcTrpcTcp0x10C0ResponseGroup { + groupUin?: pb<1, uint_32>; + groupName?: pb<2, string>; +} + +export interface OidbSvcTrpcTcp0x10C0ResponseRequest { + sequence?: pb<1, uint_64>; + eventType?: pb<2, uint_32>; + state?: pb<3, uint_32>; + group?: pb<4, OidbSvcTrpcTcp0x10C0ResponseGroup>; + target?: pb<5, OidbSvcTrpcTcp0x10C0ResponseUser>; + invitor?: pb<6, OidbSvcTrpcTcp0x10C0ResponseUser>; + operatorUser?: pb<7, OidbSvcTrpcTcp0x10C0ResponseUser>; + field9?: pb<9, string>; + comment?: pb<10, string>; +} + +export interface OidbSvcTrpcTcp0x10C0Response { + requests?: pb_repeated<1, OidbSvcTrpcTcp0x10C0ResponseRequest>; + field2?: pb<2, uint_64>; + newLatestSeq?: pb<3, uint_64>; + field4?: pb<4, uint_32>; + field5?: pb<5, uint_64>; + field6?: pb<6, uint_32>; +} diff --git a/packages/core/tests/proton-parity.test.ts b/packages/core/tests/proton-parity.test.ts index 6459730c..85e966c2 100644 --- a/packages/core/tests/proton-parity.test.ts +++ b/packages/core/tests/proton-parity.test.ts @@ -50,6 +50,7 @@ import { NTV2RichMediaHighwayExtSchema, FileUploadExtSchema, } from '../src/bridge/proto/highway'; +import { makeOidbBaseSchema, OidbSvcTrpcTcp0xFE5_2ResponseSchema } from '../src/bridge/proto/oidb'; import type { SendLongMsgResp, @@ -94,6 +95,10 @@ import type { NTV2RichMediaHighwayExt, FileUploadExt, } from '../src/bridge/proto/proton/highway'; +import type { + OidbBase, + OidbSvcTrpcTcp0xFE5_2Response, +} from '../src/bridge/proto/proton/oidb'; function hex(buf: Uint8Array): string { return Buffer.from(buf).toString('hex'); @@ -579,6 +584,55 @@ describe('proton ↔ legacy parity (longmsg subset)', () => { expect(hex(proton)).toBe(hex(legacy)); }); + // ── oidb.ts coverage (OidbBase generic envelope) ──────────────── + + it('encodes OidbBase identically to makeOidbBaseSchema', () => { + // This is the exact pattern runOidb uses for the OIDB envelope. Proton's + // OidbBase generic interface should monomorphize to a codec that is + // byte-equivalent to the legacy makeOidbBaseSchema(InnerSchema) factory. + // The inner Body type used here is a real schema (group-list response). + const body: OidbSvcTrpcTcp0xFE5_2Response = { + groups: [ + { + groupUin: 555, + info: { + groupOwner: { uid: 'u_owner' }, + createdTime: 1234567890, memberMax: 100, memberCount: 50, + groupName: 'TestGroup', description: 'desc', question: 'q?', + announcement: 'announcement', + }, + customInfo: { remark: 'remark' }, + }, + ], + }; + const envelope = { + command: 0xFE5, + subCommand: 2, + errorCode: 1, // non-zero so it isn't proto3-omitted + body, + errorMsg: 'ok', + reserved: 1, + }; + const legacy = protoEncode(envelope as any, makeOidbBaseSchema(OidbSvcTrpcTcp0xFE5_2ResponseSchema)); + const proton = protobuf_encode>(envelope); + expect(hex(proton)).toBe(hex(legacy)); + }); + + it('round-trips OidbBase with non-trivial nested body', () => { + const body: OidbSvcTrpcTcp0xFE5_2Response = { + groups: [{ groupUin: 100, info: { groupName: 'G' } }], + }; + const envelope: OidbBase = { + command: 0xFE5, subCommand: 2, errorCode: 1, body, errorMsg: 'ok', reserved: 1, + }; + const encoded = protobuf_encode>(envelope); + const decoded = protobuf_decode>(encoded); + expect(decoded.command).toBe(0xFE5); + expect(decoded.errorCode).toBe(1); + expect(decoded.body?.groups?.[0]?.groupUin).toBe(100); + expect(decoded.body?.groups?.[0]?.info?.groupName).toBe('G'); + }); + // ── Documented divergence: proto3 default-value omission ──────────── it('proton omits default-valued numeric fields (legacy emits them)', () => {