diff --git a/packages/core/src/bridge/actions/forward.ts b/packages/core/src/bridge/actions/forward.ts index 9ade79e2..8a85336a 100644 --- a/packages/core/src/bridge/actions/forward.ts +++ b/packages/core/src/bridge/actions/forward.ts @@ -5,19 +5,19 @@ import type { Bridge } from '../bridge'; import { gunzipSync, gzipSync } from 'zlib'; -import { protoDecode, protoEncode } from '../../protobuf/decode'; +import { protobuf_encode, protobuf_decode } from '@snowluma/proton'; import { buildSendElems } from '../element-builder'; import { parseMsgPush } from '../msg-push'; import type { ForwardNodePayload } from '../events'; import type { PacketInfo } from '../../protocol/types'; -import { - LongMsgResultSchema, - RecvLongMsgReqSchema, - RecvLongMsgRespSchema, - SendLongMsgReqSchema, - SendLongMsgRespSchema, -} from '../proto/longmsg'; -import { PushMsgSchema } from '../proto/message'; +import type { + LongMsgResult, + RecvLongMsgReq, + RecvLongMsgResp, + SendLongMsgReq, + SendLongMsgResp, +} from '../proto/proton/longmsg'; +import type { PushMsg, PushMsgBody } from '../proto/proton/message'; import { resolveSelfUid, toInt } from './shared'; // Module-scoped cache, keyed by res_id. Survives only for the lifetime @@ -30,7 +30,7 @@ async function buildForwardPushBody( node: ForwardNodePayload, groupId?: number, userUid?: string, -): Promise> { +): Promise { const fromUin = node.userUin > 0 ? node.userUin : toInt(bridge.identity.uin); if (fromUin <= 0) throw new Error('forward node user uin is invalid'); @@ -99,24 +99,24 @@ export async function uploadForwardNodes( } const msgBody = await Promise.all(nodes.map(node => buildForwardPushBody(bridge, node, groupId, userUid))); - const longMsgResult = protoEncode({ + const longMsgResult = protobuf_encode({ action: [ { actionCommand: 'MultiMsg', - actionData: { msgBody: msgBody as any }, + actionData: { msgBody }, }, ], - }, LongMsgResultSchema); + }); const selfUid = await resolveSelfUid(bridge); - const info: any = { + const info: SendLongMsgReq['info'] = { type: groupId ? 3 : 1, uid: { uid: groupId ? String(groupId) : selfUid }, payload: gzipSync(Buffer.from(longMsgResult)), }; if (groupId) info.groupUin = groupId; - const request = protoEncode({ + const request = protobuf_encode({ info, settings: { field1: 4, @@ -124,14 +124,14 @@ export async function uploadForwardNodes( field3: 7, field4: 0, }, - }, SendLongMsgReqSchema); + }); const result = await bridge.sendRawPacket('trpc.group.long_msg_interface.MsgService.SsoSendLongMsg', request); if (!result.success || !result.gotResponse || !result.responseData) { throw new Error(result.errorMessage || 'upload forward message failed'); } - const resp = protoDecode(result.responseData, SendLongMsgRespSchema); + const resp = protobuf_decode(result.responseData); const resId = typeof resp?.result?.resId === 'string' ? resp.result.resId : ''; if (!resId) { throw new Error('upload forward message response missing res_id'); @@ -169,7 +169,7 @@ export async function fetchForwardNodes(bridge: Bridge, resId: string): Promise< } const selfUid = await resolveSelfUid(bridge); - const request = protoEncode({ + const request = protobuf_encode({ info: { uid: { uid: selfUid }, resId, @@ -181,27 +181,27 @@ export async function fetchForwardNodes(bridge: Bridge, resId: string): Promise< field3: 0, field4: 0, }, - }, RecvLongMsgReqSchema); + }); const result = await bridge.sendRawPacket('trpc.group.long_msg_interface.MsgService.SsoRecvLongMsg', request); if (!result.success || !result.gotResponse || !result.responseData) { throw new Error(result.errorMessage || 'download forward message failed'); } - const resp = protoDecode(result.responseData, RecvLongMsgRespSchema); + const resp = protobuf_decode(result.responseData); const payload = resp?.result?.payload; if (!(payload instanceof Uint8Array) || payload.length === 0) { throw new Error('download forward message payload is empty'); } const inflate = gunzipSync(Buffer.from(payload)); - const longMsg = protoDecode(inflate, LongMsgResultSchema); - const action = longMsg?.action?.find((item: any) => item?.actionCommand === 'MultiMsg'); + const longMsg = protobuf_decode(inflate); + const action = longMsg?.action?.find((item) => item?.actionCommand === 'MultiMsg'); const msgBodyList = Array.isArray(action?.actionData?.msgBody) ? action.actionData.msgBody : []; const nodes: ForwardNodePayload[] = []; for (const msgBody of msgBodyList) { - const wrapped = protoEncode({ message: msgBody }, PushMsgSchema); + const wrapped = protobuf_encode({ message: msgBody }); const pkt: PacketInfo = { pid: 0, uin: bridge.identity.uin, diff --git a/packages/core/src/bridge/proto/proton/element.ts b/packages/core/src/bridge/proto/proton/element.ts new file mode 100644 index 00000000..3da2ac6f --- /dev/null +++ b/packages/core/src/bridge/proto/proton/element.ts @@ -0,0 +1,421 @@ +// Proton (compile-time) form of bridge/proto/element.ts. +// +// One-to-one mirror of every schema in element.ts. The legacy `*Schema` +// constants stay in place for back-compat; both forms produce wire-equivalent +// output (modulo the proto3 default-value omission — see README). + +import type { pb, pb_repeated, int_32, uint_32, uint_64, bool, bytes } from '@snowluma/proton'; + +// ── Nested / helper schemas ───────────────────────────────────────── + +export interface CustomFacePbReserve { + subType?: pb<1, int_32>; + summary?: pb<9, string>; +} + +export interface NotOnlineImagePbReserve2 { + field1?: pb<1, int_32>; + field2?: pb<2, string>; + field3?: pb<3, int_32>; + field4?: pb<4, int_32>; + field5?: pb<5, int_32>; + field7?: pb<7, string>; +} + +export interface NotOnlineImagePbReserve { + subType?: pb<1, int_32>; + field3?: pb<3, int_32>; + field4?: pb<4, int_32>; + summary?: pb<8, string>; + field10?: pb<10, int_32>; + field20?: pb<20, NotOnlineImagePbReserve2>; + url?: pb<30, string>; + md5Str?: pb<31, string>; +} + +// ── Core element schemas ──────────────────────────────────────────── + +export interface TextElem { + str?: pb<1, string>; + link?: pb<2, string>; + attr6Buf?: pb<3, bytes>; + attr7Buf?: pb<4, bytes>; + buf?: pb<11, bytes>; + pbReserve?: pb<12, bytes>; +} + +export interface FaceElem { + index?: pb<1, int_32>; + oldData?: pb<2, bytes>; + buf?: pb<11, bytes>; +} + +export interface OnlineImage { + guid?: pb<1, bytes>; + filePath?: pb<2, bytes>; + oldVerSendFile?: pb<3, bytes>; +} + +export interface NotOnlineImage { + filePath?: pb<1, string>; + fileLen?: pb<2, uint_32>; + downloadPath?: pb<3, string>; + oldVerSendFile?:pb<4, bytes>; + imgType?: pb<5, int_32>; + previewsImage?: pb<6, bytes>; + picMd5?: pb<7, bytes>; + picHeight?: pb<8, uint_32>; + picWidth?: pb<9, uint_32>; + resId?: pb<10, string>; + flag?: pb<11, bytes>; + thumbUrl?: pb<12, string>; + original?: pb<13, int_32>; + bigUrl?: pb<14, string>; + origUrl?: pb<15, string>; + bizType?: pb<16, int_32>; + result?: pb<17, int_32>; + index?: pb<18, int_32>; + opFaceBuf?: pb<19, bytes>; + oldPicMd5?: pb<20, bool>; + thumbWidth?: pb<21, int_32>; + thumbHeight?: pb<22, int_32>; + fileId?: pb<23, int_32>; + showLen?: pb<24, uint_32>; + downloadLen?: pb<25, uint_32>; + x400Url?: pb<26, string>; + x400Width?: pb<27, int_32>; + x400Height?: pb<28, int_32>; + pbRes?: pb<29, NotOnlineImagePbReserve>; +} + +export interface TransElem { + elemType?: pb<1, int_32>; + elemValue?: pb<2, bytes>; +} + +export interface MarketFace { + faceName?: pb<1, string>; + itemType?: pb<2, uint_32>; + faceInfo?: pb<3, uint_32>; + faceId?: pb<4, bytes>; + tabId?: pb<5, uint_32>; + subType?: pb<6, uint_32>; + key?: pb<7, bytes>; + param?: pb<8, bytes>; + mediaType?: pb<9, uint_32>; + imageWidth?: pb<10, uint_32>; + imageHeight?: pb<11, uint_32>; + mobileParam?: pb<12, bytes>; + pbReserve?: pb<13, bytes>; +} + +export interface CustomFace { + guid?: pb<1, bytes>; + filePath?: pb<2, string>; + shortcut?: pb<3, string>; + buffer?: pb<4, bytes>; + flag?: pb<5, bytes>; + oldData?: pb<6, bytes>; + fileId?: pb<7, uint_32>; + serverIp?: pb<8, int_32>; + serverPort?: pb<9, int_32>; + fileType?: pb<10, int_32>; + signature?: pb<11, bytes>; + useful?: pb<12, int_32>; + md5?: pb<13, bytes>; + thumbUrl?: pb<14, string>; + bigUrl?: pb<15, string>; + origUrl?: pb<16, string>; + bizType?: pb<17, int_32>; + repeatIndex?: pb<18, int_32>; + repeatImage?: pb<19, int_32>; + imageType?: pb<20, int_32>; + index?: pb<21, int_32>; + width?: pb<22, int_32>; + height?: pb<23, int_32>; + source?: pb<24, int_32>; + size?: pb<25, uint_32>; + origin?: pb<26, int_32>; + thumbWidth?: pb<27, int_32>; + thumbHeight?: pb<28, int_32>; + showLen?: pb<29, int_32>; + downloadLen?: pb<30, int_32>; + x400Url?: pb<31, string>; + x400Width?: pb<32, int_32>; + x400Height?: pb<33, int_32>; + pbRes?: pb<34, CustomFacePbReserve>; +} + +export interface RichMsg { + template1?: pb<1, bytes>; + serviceId?: pb<2, int_32>; + msgResId?: pb<3, bytes>; + rand?: pb<4, int_32>; + seq?: pb<5, uint_32>; +} + +export interface GroupFileElem { + filename?: pb<1, string>; + fileSize?: pb<2, uint_64>; + fileId?: pb<3, string>; + batchId?: pb<4, string>; + fileKey?: pb<5, string>; + mark?: pb<6, bytes>; + sequence?: pb<7, uint_64>; + batchItemId?: pb<8, bytes>; + feedMsgTime?: pb<9, int_32>; + pbReserve?: pb<10, bytes>; +} + +export interface ExtraInfo { + nick?: pb<1, bytes>; + groupCard?: pb<2, bytes>; + level?: pb<3, int_32>; + flags?: pb<4, int_32>; + groupMask?: pb<5, int_32>; + msgTailId?: pb<6, int_32>; + senderTitle?: pb<7, bytes>; + apnsTips?: pb<8, bytes>; + uin?: pb<9, uint_64>; + msgStateFlag?: pb<10, int_32>; + apnsSoundType?: pb<11, int_32>; + newGroupFlag?: pb<12, int_32>; +} + +export interface VideoFile { + fileUuid?: pb<1, string>; + fileMd5?: pb<2, bytes>; + fileName?: pb<3, string>; + fileFormat?: pb<4, int_32>; + fileTime?: pb<5, int_32>; + fileSize?: pb<6, int_32>; + thumbWidth?: pb<7, int_32>; + thumbHeight?: pb<8, int_32>; + thumbFileMd5?: pb<9, bytes>; + source?: pb<10, bytes>; + thumbFileSize?: pb<11, int_32>; + busiType?: pb<12, int_32>; + fromChatType?: pb<13, int_32>; + toChatType?: pb<14, int_32>; + supportProgressive?: pb<15, bool>; + fileWidth?: pb<16, int_32>; + fileHeight?: pb<17, int_32>; + subBusiType?: pb<18, int_32>; + videoAttr?: pb<19, int_32>; + pbReserve?: pb<24, bytes>; +} + +export interface SrcMsg { + origSeqs?: pb_repeated<1, uint_32>; + senderUin?: pb<2, uint_64>; + time?: pb<3, int_32>; + flag?: pb<4, int_32>; + elemsRaw?: pb_repeated<5, bytes>; + type?: pb<6, int_32>; + richMsg?: pb<7, bytes>; + pbReserve?: pb<8, bytes>; + sourceMsg?: pb<9, bytes>; + toUin?: pb<10, uint_64>; + troopName?: pb<11, bytes>; +} + +export interface LightAppElem { + data?: pb<1, bytes>; + msgResid?: pb<2, bytes>; +} + +export interface CommonElem { + serviceType?: pb<1, int_32>; + pbElem?: pb<2, bytes>; + businessType?: pb<3, uint_32>; +} + +export interface GeneralFlags { + bubbleDiyTextId?: pb<1, int_32>; + groupFlagNew?: pb<2, int_32>; + uin?: pb<3, uint_64>; + longTextFlag?: pb<6, int_32>; + longTextResId?: pb<7, string>; +} + +// ── Elem (union of all element types) ─────────────────────────────── + +export interface Elem { + text?: pb<1, TextElem>; + face?: pb<2, FaceElem>; + onlineImage?: pb<3, OnlineImage>; + notOnlineImage?: pb<4, NotOnlineImage>; + transElem?: pb<5, TransElem>; + marketFace?: pb<6, MarketFace>; + customFace?: pb<8, CustomFace>; + richMsg?: pb<12, RichMsg>; + groupFile?: pb<13, GroupFileElem>; + extraInfo?: pb<16, ExtraInfo>; + videoFile?: pb<19, VideoFile>; + generalFlags?: pb<37, GeneralFlags>; + srcMsg?: pb<45, SrcMsg>; + lightApp?: pb<51, LightAppElem>; + commonElem?: pb<53, CommonElem>; +} + +// ── Extra decode types (for CommonElem.pbElem sub-messages) ───────── + +export interface MentionExtra { + type?: pb<3, int_32>; + uin?: pb<4, uint_32>; + field5?: pb<5, int_32>; + uid?: pb<9, string>; +} + +export interface QFaceExtra { + packId?: pb<1, string>; + stickerId?: pb<2, string>; + qsid?: pb<3, int_32>; + sourceType?: pb<4, int_32>; + stickerType?: pb<5, int_32>; + resultId?: pb<6, string>; + text?: pb<7, string>; + randomType?: pb<9, int_32>; +} + +export interface QSmallFaceExtra { + faceId?: pb<1, uint_32>; + preview?: pb<2, string>; + preview2?: pb<3, string>; +} + +// ── NTQQ MsgInfo types (CommonElem service_type 48) ───────────────── + +export interface FileType { + type?: pb<1, uint_32>; + picFormat?: pb<2, uint_32>; + videoFormat?: pb<3, uint_32>; + voiceFormat?: pb<4, uint_32>; +} + +export interface FileInfo { + fileSize?: pb<1, uint_32>; + fileHash?: pb<2, string>; + fileSha1?: pb<3, string>; + fileName?: pb<4, string>; + type?: pb<5, FileType>; + width?: pb<6, uint_32>; + height?: pb<7, uint_32>; + time?: pb<8, uint_32>; + original?: pb<9, uint_32>; +} + +export interface IndexNode { + info?: pb<1, FileInfo>; + fileUuid?: pb<2, string>; + storeId?: pb<3, uint_32>; + uploadTime?: pb<4, uint_32>; + ttl?: pb<5, uint_32>; + subType?: pb<6, uint_32>; +} + +export interface PicUrlExtInfo { + originalParameter?: pb<1, string>; + bigParameter?: pb<2, string>; + thumbParameter?: pb<3, string>; +} + +export interface PictureInfo { + urlPath?: pb<1, string>; + ext?: pb<2, PicUrlExtInfo>; + domain?: pb<3, string>; +} + +export interface PicExtData { + subType?: pb<1, uint_32>; + textSummary?: pb<9, string>; +} + +export interface PicExtBizInfo { + bizType?: pb<1, uint_32>; + textSummary?: pb<2, string>; + bytesPbReserveC2c?: pb<11, bytes>; + extData?: pb<12, PicExtData>; + fromScene?: pb<1001, uint_32>; + toScene?: pb<1002, uint_32>; + oldFileId?: pb<1003, uint_32>; +} + +export interface VideoExtBizInfo { + fromScene?: pb<1, uint_32>; + toScene?: pb<2, uint_32>; + bytesPbReserve?: pb<3, bytes>; +} + +export interface PttExtBizInfo { + srcUin?: pb<1, uint_64>; + pttScene?: pb<2, uint_32>; + pttType?: pb<3, uint_32>; + changeVoice?: pb<4, uint_32>; + waveform?: pb<5, bytes>; + autoConvertText?: pb<6, uint_32>; + bytesReserve?: pb<11, bytes>; + bytesPbReserve?: pb<12, bytes>; + bytesGeneralFlags?: pb<13, bytes>; +} + +export interface ExtBizInfo { + pic?: pb<1, PicExtBizInfo>; + video?: pb<2, VideoExtBizInfo>; + ptt?: pb<3, PttExtBizInfo>; + busiType?: pb<10, uint_32>; +} + +export interface C2cSource { + friendUid?: pb<2, string>; +} + +export interface TroopSource { + groupUin?: pb<1, uint_32>; +} + +export interface HashSum { + bytesPbReserveC2c?: pb<201, C2cSource>; + troopSource?: pb<202, TroopSource>; +} + +export interface MsgInfoBody { + index?: pb<1, IndexNode>; + picture?: pb<2, PictureInfo>; + fileExist?: pb<5, bool>; + hashSum?: pb<6, HashSum>; +} + +export interface MsgInfo { + msgInfoBody?: pb_repeated<1, MsgInfoBody>; + extBizInfo?: pb<2, ExtBizInfo>; +} + +// ── GroupFileExtra (TransElem type=24) ────────────────────────────── + +export interface GroupFileInfo { + busId?: pb<1, uint_32>; + fileId?: pb<2, string>; + fileSize?: pb<3, uint_64>; + fileName?: pb<4, string>; + fileSha?: pb<5, bytes>; + extInfoString?: pb<6, string>; + fileMd5?: pb<7, bytes>; +} + +export interface GroupFileExtraInner { + info?: pb<2, GroupFileInfo>; +} + +export interface GroupFileExtra { + inner?: pb<7, GroupFileExtraInner>; +} + +// ── Preserve (for message receipt info) ───────────────────────────── + +export interface Preserve { + messageId?: pb<3, uint_64>; + senderUid?: pb<6, string>; + receiverUid?: pb<7, string>; + clientSequence?: pb<8, uint_32>; +} diff --git a/packages/core/src/bridge/proto/proton/longmsg.ts b/packages/core/src/bridge/proto/proton/longmsg.ts index 6f8fa331..ef634a96 100644 --- a/packages/core/src/bridge/proto/proton/longmsg.ts +++ b/packages/core/src/bridge/proto/proton/longmsg.ts @@ -1,15 +1,12 @@ // 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`. +// One-to-one mirror of `bridge/proto/longmsg.ts`. All 12 schemas are now +// migrated — the three (`LongMsgContent` / `LongMsgAction` / `LongMsgResult`) +// that reach into `PushMsgBody` resolve via the proton-form `message.ts` +// migration alongside this file. -import type { pb, uint_32, bool, bytes } from '@snowluma/proton'; +import type { pb, pb_repeated, uint_32, bool, bytes } from '@snowluma/proton'; +import type { PushMsgBody } from './message'; export interface LongMsgUid { uid?: pb<2, string>; @@ -63,3 +60,18 @@ export interface RecvLongMsgResp { result?: pb<1, RecvLongMsgRespResult>; settings?: pb<15, LongMsgSettings>; } + +// ── Schemas that reach into PushMsgBody (via message.ts) ──────────── + +export interface LongMsgContent { + msgBody?: pb_repeated<1, PushMsgBody>; +} + +export interface LongMsgAction { + actionCommand?: pb<1, string>; + actionData?: pb<2, LongMsgContent>; +} + +export interface LongMsgResult { + action?: pb_repeated<2, LongMsgAction>; +} diff --git a/packages/core/src/bridge/proto/proton/message.ts b/packages/core/src/bridge/proto/proton/message.ts new file mode 100644 index 00000000..1727c90a --- /dev/null +++ b/packages/core/src/bridge/proto/proton/message.ts @@ -0,0 +1,113 @@ +// Proton (compile-time) form of bridge/proto/message.ts. +// One-to-one mirror; legacy `*Schema` constants stay alongside for back-compat. + +import type { pb, pb_repeated, int_32, uint_32, uint_64, bytes } from '@snowluma/proton'; +import type { Elem } from './element'; + +// ── ResponseHead.Grp ──────────────────────────────────────────────── + +export interface ResponseGrp { + groupUin?: pb<1, uint_32>; + memberName?: pb<2, string>; + groupName?: pb<4, string>; +} + +export interface ResponseForward { + friendName?: pb<6, string>; +} + +// ── ResponseHead ──────────────────────────────────────────────────── + +export interface ResponseHead { + fromUin?: pb<1, uint_32>; + fromUid?: pb<2, string>; + type?: pb<3, uint_32>; + sigMap?: pb<4, uint_32>; + toUin?: pb<5, uint_32>; + toUid?: pb<6, string>; + forward?: pb<7, ResponseForward>; + grp?: pb<8, ResponseGrp>; +} + +// ── ContentHead ───────────────────────────────────────────────────── + +export interface ContentHead { + msgType?: pb<1, uint_32>; + subType?: pb<2, uint_32>; + divSeq?: pb<3, uint_32>; + msgId?: pb<4, uint_32>; + sequence?: pb<5, uint_32>; + timestamp?: pb<6, uint_32>; + field7?: pb<7, uint_64>; + newId?: pb<12, uint_64>; +} + +// ── Ptt (voice) ───────────────────────────────────────────────────── + +export interface Ptt { + fileType?: pb<1, uint_32>; + fileId?: pb<2, uint_64>; + fileUuid?: pb<3, bytes>; + fileMd5?: pb<4, bytes>; + fileName?: pb<5, string>; + fileSize?: pb<6, uint_32>; + groupFileKey?: pb<10, string>; + fileKey?: pb<14, bytes>; + time?: pb<19, uint_32>; + format?: pb<29, uint_32>; +} + +// ── NotOnlineFile (C2C file) ──────────────────────────────────────── + +export interface NotOnlineFile { + fileType?: pb<1, uint_32>; + fileUuid?: pb<3, string>; + fileMd5?: pb<4, bytes>; + fileName?: pb<5, string>; + fileSize?: pb<6, uint_64>; + fileHash?: pb<57, string>; +} + +// ── RichText ──────────────────────────────────────────────────────── + +export interface RichText { + elems?: pb_repeated<2, Elem>; + notOnlineFile?: pb<3, NotOnlineFile>; + ptt?: pb<4, Ptt>; +} + +// ── MessageBody ───────────────────────────────────────────────────── + +export interface MessageBody { + richText?: pb<1, RichText>; + msgContent?: pb<2, bytes>; +} + +// ── FileExtra (for C2C file in msg_content) ───────────────────────── + +export interface FileExtraInfo { + fileSize?: pb<1, uint_64>; + fileName?: pb<2, string>; + fileMd5?: pb<3, bytes>; + fileUuid?: pb<4, string>; + fileHash?: pb<5, string>; +} + +export interface FileExtra { + file?: pb<1, FileExtraInfo>; +} + +// ── PushMsgBody ───────────────────────────────────────────────────── + +export interface PushMsgBody { + responseHead?: pb<1, ResponseHead>; + contentHead?: pb<2, ContentHead>; + body?: pb<3, MessageBody>; +} + +// ── PushMsg (top-level) ───────────────────────────────────────────── + +export interface PushMsg { + message?: pb<1, PushMsgBody>; + status?: pb<3, int_32>; +} diff --git a/packages/core/tests/proton-parity.test.ts b/packages/core/tests/proton-parity.test.ts index e4f07898..9b356973 100644 --- a/packages/core/tests/proton-parity.test.ts +++ b/packages/core/tests/proton-parity.test.ts @@ -13,7 +13,21 @@ import { RecvLongMsgReqSchema, RecvLongMsgRespSchema, LongMsgSettingsSchema, + LongMsgResultSchema, } from '../src/bridge/proto/longmsg'; +import { + TextElemSchema, + NotOnlineImageSchema, + ExtraInfoSchema, + SrcMsgSchema, + ElemSchema, + MsgInfoSchema, +} from '../src/bridge/proto/element'; +import { + ResponseHeadSchema, + PushMsgBodySchema, + PushMsgSchema, +} from '../src/bridge/proto/message'; import type { SendLongMsgResp, @@ -21,7 +35,21 @@ import type { RecvLongMsgReq, RecvLongMsgResp, LongMsgSettings, + LongMsgResult, } from '../src/bridge/proto/proton/longmsg'; +import type { + TextElem, + NotOnlineImage, + ExtraInfo, + SrcMsg, + Elem, + MsgInfo, +} from '../src/bridge/proto/proton/element'; +import type { + ResponseHead, + PushMsgBody, + PushMsg, +} from '../src/bridge/proto/proton/message'; function hex(buf: Uint8Array): string { return Buffer.from(buf).toString('hex'); @@ -30,14 +58,14 @@ function hex(buf: Uint8Array): string { 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 legacy = protoEncode(data as any, 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 legacy = protoEncode(data as any, SendLongMsgRespSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); @@ -56,7 +84,7 @@ describe('proton ↔ legacy parity (longmsg subset)', () => { }, settings: { field1: 4, field2: 1, field3: 7, field4: 2 }, }; - const legacy = protoEncode(data, SendLongMsgReqSchema); + const legacy = protoEncode(data as any, SendLongMsgReqSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); @@ -66,7 +94,7 @@ describe('proton ↔ legacy parity (longmsg subset)', () => { 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 legacy = protoEncode(data as any, RecvLongMsgReqSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); @@ -76,11 +104,188 @@ describe('proton ↔ legacy parity (longmsg subset)', () => { 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 legacy = protoEncode(data as any, RecvLongMsgRespSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(hex(legacy)); }); + // ── element.ts coverage ───────────────────────────────────────────── + + it('encodes TextElem (only primitive bytes + strings) identically', () => { + const data: TextElem = { + str: 'hello', + link: 'https://example.com', + attr6Buf: new Uint8Array([1, 2, 3]), + buf: new Uint8Array([0xff, 0xee]), + }; + const legacy = protoEncode(data as any, TextElemSchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + it('encodes NotOnlineImage with nested PbReserve identically', () => { + const data: NotOnlineImage = { + filePath: 'pic.jpg', + fileLen: 12345, + picMd5: new Uint8Array([0x11, 0x22, 0x33, 0x44]), + picHeight: 600, + picWidth: 800, + resId: 'res-abc', + original: 1, + bigUrl: 'https://q.qq.com/big/abc', + bizType: 5, + pbRes: { + subType: 1, + summary: '[picture]', + field10: 7, + field20: { field1: 1, field2: 'inner', field3: 2, field4: 3, field5: 4, field7: 'tail' }, + url: 'https://q.qq.com/abc', + md5Str: 'abc123', + }, + }; + const legacy = protoEncode(data as any, NotOnlineImageSchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + it('encodes ExtraInfo (mix of bytes + int32 + uint64) identically', () => { + const data: ExtraInfo = { + nick: new Uint8Array([0x41, 0x42]), + groupCard: new Uint8Array([0x43]), + level: 7, + flags: 1, + groupMask: 2, + msgTailId: 3, + uin: 10001n, + msgStateFlag: 4, + }; + const legacy = protoEncode(data as any, ExtraInfoSchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + it('encodes SrcMsg with repeated_uint32 + repeated_bytes identically', () => { + const data: SrcMsg = { + origSeqs: [100, 200, 300, 400], + senderUin: 10001n, + time: 1234567890, + flag: 1, + elemsRaw: [new Uint8Array([1, 2]), new Uint8Array([3, 4]), new Uint8Array([5, 6])], + type: 1, + toUin: 20002n, + }; + const legacy = protoEncode(data as any, SrcMsgSchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + it('encodes Elem (oneOf-style union) identically across multiple shapes', () => { + const textOnly: Elem = { text: { str: 'hi', link: 'lnk' } }; + expect(hex(protobuf_encode(textOnly))).toBe(hex(protoEncode(textOnly as any, ElemSchema))); + + const faceOnly: Elem = { face: { index: 42, buf: new Uint8Array([1, 2, 3]) } }; + expect(hex(protobuf_encode(faceOnly))).toBe(hex(protoEncode(faceOnly as any, ElemSchema))); + + const generalFlags: Elem = { generalFlags: { uin: 10001n, longTextFlag: 1, longTextResId: 'rs' } }; + expect(hex(protobuf_encode(generalFlags))).toBe(hex(protoEncode(generalFlags as any, ElemSchema))); + }); + + it('encodes MsgInfo with repeated_message + deeply nested ExtBizInfo identically', () => { + const data: MsgInfo = { + msgInfoBody: [ + { + index: { + info: { fileSize: 1024, fileHash: 'sha-1', fileSha1: 'sha-2', fileName: 'a.png', type: { type: 1, picFormat: 2, videoFormat: 3, voiceFormat: 4 }, width: 100, height: 200, time: 99, original: 1 }, + fileUuid: 'uuid-1', storeId: 5, uploadTime: 1000, ttl: 86400, subType: 1, + }, + picture: { + urlPath: '/p/1', + ext: { originalParameter: 'op', bigParameter: 'bp', thumbParameter: 'tp' }, + domain: 'q.qq.com', + }, + fileExist: true, + }, + ], + extBizInfo: { + pic: { + bizType: 5, textSummary: '[pic]', + extData: { subType: 2, textSummary: 'sum' }, + fromScene: 1, toScene: 2, oldFileId: 999, + }, + busiType: 3, + }, + }; + const legacy = protoEncode(data as any, MsgInfoSchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + // ── message.ts coverage ───────────────────────────────────────────── + + it('encodes ResponseHead (nested forward + grp) identically', () => { + const data: ResponseHead = { + fromUin: 10001, fromUid: 'u_a', type: 1, sigMap: 2, toUin: 20002, toUid: 'u_b', + forward: { friendName: 'Alice' }, + grp: { groupUin: 555, memberName: 'Bob', groupName: 'TestGroup' }, + }; + const legacy = protoEncode(data as any, ResponseHeadSchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + it('encodes PushMsgBody (with RichText holding Elem[]) identically', () => { + const data: PushMsgBody = { + responseHead: { fromUin: 1, toUin: 2, type: 1, sigMap: 3, fromUid: 'a', toUid: 'b' }, + contentHead: { msgType: 1, subType: 1, divSeq: 1, msgId: 99, sequence: 100, timestamp: 1234567890 }, + body: { + richText: { + elems: [ + { text: { str: 'hi ' } }, + { text: { str: 'world', link: 'lnk' } }, + ], + }, + }, + }; + const legacy = protoEncode(data as any, PushMsgBodySchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + it('encodes PushMsg (top-level) identically', () => { + const data: PushMsg = { + message: { + responseHead: { fromUin: 1, toUin: 2, type: 1, sigMap: 1, fromUid: 'a', toUid: 'b' }, + contentHead: { msgType: 1, subType: 1, divSeq: 1, msgId: 1, sequence: 1, timestamp: 1 }, + body: { richText: { elems: [{ text: { str: 'x' } }] } }, + }, + status: 1, + }; + const legacy = protoEncode(data as any, PushMsgSchema); + const proton = protobuf_encode(data); + expect(hex(proton)).toBe(hex(legacy)); + }); + + // ── longmsg.ts: full migration (Content / Action / Result) ───────── + + it('encodes LongMsgResult (repeated Action → Content → PushMsgBody[]) identically', () => { + const inner: PushMsgBody = { + responseHead: { fromUin: 1, toUin: 2, type: 1, sigMap: 1, fromUid: 'a', toUid: 'b' }, + contentHead: { msgType: 1, subType: 1, divSeq: 1, msgId: 1, sequence: 1, timestamp: 1 }, + body: { richText: { elems: [{ text: { str: 'forwarded' } }] } }, + }; + const data: LongMsgResult = { + action: [ + { + actionCommand: 'MultiMsg', + actionData: { msgBody: [inner, inner] }, + }, + ], + }; + const legacy = protoEncode(data as any, LongMsgResultSchema); + 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)', () => { @@ -89,7 +294,7 @@ describe('proton ↔ legacy parity (longmsg subset)', () => { // 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 legacy = protoEncode(data as any, LongMsgSettingsSchema); const proton = protobuf_encode(data); expect(hex(proton)).toBe(''); // nothing on the wire expect(legacy.length).toBeGreaterThan(0); // legacy emits all zeros @@ -113,7 +318,7 @@ describe('proton ↔ legacy parity (longmsg subset)', () => { it('round-trips SendLongMsgResp through legacy encode → proton decode', () => { const data: SendLongMsgResp = { result: { resId: 'rt-2' } }; - const encoded = protoEncode(data, SendLongMsgRespSchema); + const encoded = protoEncode(data as any, SendLongMsgRespSchema); const decoded = protobuf_decode(encoded); expect(decoded.result?.resId).toBe('rt-2'); });