Files
SnowLuma/packages/core/tests/proton-parity.test.ts
T
DrSmoothl 76b87f6b27 feat(proton): migrate group-album actions (PR #31) to proton codec
PR #31 landed 4 TRPC group-album actions on the legacy runtime
schema encoder. Migrate them to the compile-time codec, matching
the pattern established by the OIDB migration: TS interfaces in
`proto/proton/oidb-action.ts` are monomorphized at build time and
each `protoEncode/Decode` call site now calls `protobuf_encode<T>` /
`protobuf_decode<T>` directly.

- Add 33 proton interfaces for the new TRPC envelopes (GetMediaList,
  DoQunComment, DoQunLike, DeleteMedias, plus all nested types) —
  inline anonymous schemas in legacy lifted to named interfaces.
- Migrate `actions/group-album.ts` to the proton API; drop the
  `(resp as any)` casts now that decoded responses are typed.
- Add 4 wire-byte parity tests for the new envelopes; non-default
  field values throughout so byte equivalence holds (proto3 omission
  of defaults is the documented divergence).
- Drop dead `GroupAvatarUploadExtSchema` from `proto/highway.ts` and
  unused `import * as console from "node:console"` in
  `web/group-album.ts` — leftovers from the original PR review.

core: 609/609 tests pass, typecheck clean.
2026-05-19 08:28:11 +08:00

816 lines
31 KiB
TypeScript

// 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,
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 {
GroupChangeSchema,
GroupAdminSchema,
NotifyMessageBodySchema,
GroupReactNotifySchema,
FriendRequestSchema,
GroupJoinSchema,
} from '../src/bridge/proto/notify';
import {
RoutingHeadSchema,
SendMessageRequestSchema,
SendMessageResponseSchema,
MentionExtraSendSchema,
MarkdownDataSchema,
} from '../src/bridge/proto/action';
import {
HttpConn0x6FF501RequestSchema,
HttpConn0x6FF501ResponseSchema,
ReqDataHighwayHeadSchema,
NTV2RichMediaHighwayExtSchema,
FileUploadExtSchema,
} from '../src/bridge/proto/highway';
import { makeOidbBaseSchema, OidbSvcTrpcTcp0xFE5_2ResponseSchema } from '../src/bridge/proto/oidb';
import { makeOidbEnvelope, encodeOidbEnv, decodeOidbEnv } from '../src/bridge/bridge-oidb';
import {
GetMediaListRequestSchema,
DoQunCommentRequestSchema,
DoQunLikeRequestSchema,
DeleteMediasRequestSchema,
DeleteMediasResponseSchema,
} from '../src/bridge/proto/oidb-action';
import type {
SendLongMsgResp,
SendLongMsgReq,
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';
import type {
GroupChange,
GroupAdmin,
NotifyMessageBody,
GroupReactNotify,
FriendRequest as FriendRequestNotify,
GroupJoin,
} from '../src/bridge/proto/proton/notify';
import type {
RoutingHead,
SendMessageRequest,
SendMessageResponse,
MentionExtraSend,
MarkdownData,
} from '../src/bridge/proto/proton/action';
import type {
HttpConn0x6FF501Request,
HttpConn0x6FF501Response,
ReqDataHighwayHead,
NTV2RichMediaHighwayExt,
FileUploadExt,
} from '../src/bridge/proto/proton/highway';
import type {
OidbBase,
OidbSvcTrpcTcp0xFE5_2Response,
} from '../src/bridge/proto/proton/oidb';
import type {
GetMediaListRequest,
DoQunCommentRequest,
DoQunLikeRequest,
DeleteMediasRequest,
DeleteMediasResponse,
} from '../src/bridge/proto/proton/oidb-action';
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 as any, 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 as any, 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 as any, 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 as any, 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 as any, RecvLongMsgRespSchema);
const proton = protobuf_encode<RecvLongMsgResp>(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<TextElem>(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<NotOnlineImage>(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<ExtraInfo>(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<SrcMsg>(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<Elem>(textOnly))).toBe(hex(protoEncode(textOnly as any, ElemSchema)));
const faceOnly: Elem = { face: { index: 42, buf: new Uint8Array([1, 2, 3]) } };
expect(hex(protobuf_encode<Elem>(faceOnly))).toBe(hex(protoEncode(faceOnly as any, ElemSchema)));
const generalFlags: Elem = { generalFlags: { uin: 10001n, longTextFlag: 1, longTextResId: 'rs' } };
expect(hex(protobuf_encode<Elem>(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<MsgInfo>(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<ResponseHead>(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<PushMsgBody>(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<PushMsg>(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<LongMsgResult>(data);
expect(hex(proton)).toBe(hex(legacy));
});
// ── notify.ts coverage ──────────────────────────────────────────────
it('encodes GroupChange identically', () => {
const data: GroupChange = {
groupUin: 555,
flag: 1,
memberUid: 'u_x',
decreaseType: 130,
operatorBytes: new Uint8Array([1, 2, 3]),
increaseType: 1,
};
const legacy = protoEncode(data as any, GroupChangeSchema);
const proton = protobuf_encode<GroupChange>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes GroupAdmin (extraEnable variant) identically', () => {
const data: GroupAdmin = {
groupUin: 777, flag: 1, isPromote: true,
body: { extraEnable: { adminUid: 'u_admin', isPromote: true } },
};
const legacy = protoEncode(data as any, GroupAdminSchema);
const proton = protobuf_encode<GroupAdmin>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes NotifyMessageBody (recall variant with repeated RecallMessage) identically', () => {
const data: NotifyMessageBody = {
type: 17,
groupUin: 555,
recall: {
operatorUid: 'u_op',
recallMessages: [
{ sequence: 1, time: 100, random: 42, type: 1, flag: 1, authorUid: 'u_a' },
{ sequence: 2, time: 200, random: 43, type: 1, flag: 1, authorUid: 'u_a' },
],
groupType: 1, opType: 2,
tipInfo: { tip: 'recalled' },
},
msgSequence: 99,
};
const legacy = protoEncode(data as any, NotifyMessageBodySchema);
const proton = protobuf_encode<NotifyMessageBody>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes GroupReactNotify (deeply nested) identically', () => {
const data: GroupReactNotify = {
groupUin: 555n,
field13: 35,
groupReactionData: {
data: {
data: {
groupReactionTarget: { seq: 12345n },
groupReactionDataContent: { code: '128516', count: 1, operatorUid: 'u_op', type: 1 },
},
},
},
};
const legacy = protoEncode(data as any, GroupReactNotifySchema);
const proton = protobuf_encode<GroupReactNotify>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes FriendRequest identically', () => {
const data: FriendRequestNotify = {
info: { targetUid: 'u_me', sourceUid: 'u_them', newSource: 'qrcode', message: 'hello!', source: 'web' },
};
const legacy = protoEncode(data as any, FriendRequestSchema);
const proton = protobuf_encode<FriendRequestNotify>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes GroupJoin identically', () => {
const data: GroupJoin = {
groupUin: 555, field2: 1, targetUid: 'u_them', field4: 1,
field6: 1, field7: 'reason', field8: 1, field9: new Uint8Array([0xaa]),
};
const legacy = protoEncode(data as any, GroupJoinSchema);
const proton = protobuf_encode<GroupJoin>(data);
expect(hex(proton)).toBe(hex(legacy));
});
// ── action.ts coverage ─────────────────────────────────────────────
it('encodes RoutingHead (c2c variant) identically', () => {
const data: RoutingHead = { c2c: { uin: 10001, uid: 'u_a' } };
const legacy = protoEncode(data as any, RoutingHeadSchema);
const proton = protobuf_encode<RoutingHead>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes RoutingHead (group variant) identically', () => {
const data: RoutingHead = { grp: { groupCode: 555n } };
const legacy = protoEncode(data as any, RoutingHeadSchema);
const proton = protobuf_encode<RoutingHead>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes SendMessageRequest (group with two text elements) identically', () => {
const data: SendMessageRequest = {
routingHead: { grp: { groupCode: 555n } },
contentHead: { type: 1, subType: 1, c2cCmd: 11 },
messageBody: {
richText: {
elems: [
{ text: { str: 'hi ' } },
{ text: { str: 'world', link: 'lnk' } },
],
},
},
clientSequence: 1,
random: 12345,
syncCookie: new Uint8Array([0xaa, 0xbb]),
via: 1,
dataStatist: 1,
multiSendSeq: 1,
};
const legacy = protoEncode(data as any, SendMessageRequestSchema);
const proton = protobuf_encode<SendMessageRequest>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes SendMessageResponse identically', () => {
const data: SendMessageResponse = {
result: 1, errMsg: 'ok', timestamp1: 1234567890,
field10: 1, groupSequence: 100, timestamp2: 1234567891, privateSequence: 200,
};
const legacy = protoEncode(data as any, SendMessageResponseSchema);
const proton = protobuf_encode<SendMessageResponse>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes MentionExtraSend identically', () => {
const data: MentionExtraSend = { type: 2, uin: 10001, field5: 1, uid: 'u_a' };
const legacy = protoEncode(data as any, MentionExtraSendSchema);
const proton = protobuf_encode<MentionExtraSend>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes MarkdownData identically', () => {
const data: MarkdownData = { content: '**bold** _italic_' };
const legacy = protoEncode(data as any, MarkdownDataSchema);
const proton = protobuf_encode<MarkdownData>(data);
expect(hex(proton)).toBe(hex(legacy));
});
// ── highway.ts coverage ────────────────────────────────────────────
it('encodes HttpConn0x6FF501Request (high-numbered field tag 0x501) identically', () => {
const data: HttpConn0x6FF501Request = {
httpConn: {
field1: 1, field2: 2, field3: 16, field4: 1, field6: 3,
serviceTypes: [1, 5, 10, 21],
field9: 2, field10: 9, field11: 8, ver: '1.0.1',
},
};
const legacy = protoEncode(data as any, HttpConn0x6FF501RequestSchema);
const proton = protobuf_encode<HttpConn0x6FF501Request>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes HttpConn0x6FF501Response (with repeated server infos) identically', () => {
const data: HttpConn0x6FF501Response = {
httpConn: {
sigSession: new Uint8Array([1, 2, 3]),
sessionKey: new Uint8Array([4, 5, 6]),
serverInfos: [
{ serviceType: 1, serverAddrs: [{ type: 1, ip: 0x01020304, port: 8080, area: 1 }] },
{ serviceType: 2, serverAddrs: [{ type: 1, ip: 0x05060708, port: 8081, area: 2 }] },
],
},
};
const legacy = protoEncode(data as any, HttpConn0x6FF501ResponseSchema);
const proton = protobuf_encode<HttpConn0x6FF501Response>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes ReqDataHighwayHead (multiple nested messages) identically', () => {
const data: ReqDataHighwayHead = {
msgBaseHead: {
version: 1, uin: '10001', command: 'PicUp.DataUp', seq: 1, retryTimes: 1,
appId: 1600001604, dataFlag: 16, commandId: 1003,
},
msgSegHead: {
serviceId: 1, filesize: 100n, dataOffset: 50n, dataLength: 100,
retCode: 1, serviceTicket: new Uint8Array([1, 2, 3]), flag: 1,
md5: new Uint8Array([4, 5, 6]), fileMd5: new Uint8Array([7, 8, 9]),
cacheAddr: 1, cachePort: 80,
},
bytesReqExtendInfo: new Uint8Array([0xab, 0xcd]),
timestamp: 1234567890n,
msgLoginSigHead: { loginSigType: 8, appId: 1600001604 },
};
const legacy = protoEncode(data as any, ReqDataHighwayHeadSchema);
const proton = protobuf_encode<ReqDataHighwayHead>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes NTV2RichMediaHighwayExt (repeated_bytes hash) identically', () => {
const data: NTV2RichMediaHighwayExt = {
fileUuid: 'uuid-1',
uKey: 'key-1',
network: { ipv4s: [{ domain: { isEnable: true, ip: '127.0.0.1' }, port: 80 }] },
blockSize: 1048576,
hash: { fileSha1: [new Uint8Array([1, 2]), new Uint8Array([3, 4])] },
};
const legacy = protoEncode(data as any, NTV2RichMediaHighwayExtSchema);
const proton = protobuf_encode<NTV2RichMediaHighwayExt>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes FileUploadExt (high-numbered tags 100/200/400/600/700) identically', () => {
const data: FileUploadExt = {
unknown1: 100,
unknown2: 1,
unknown3: 1,
entry: {
busiBuff: {
busId: 102,
senderUin: 10001n,
receiverUin: 555n,
groupCode: 555n,
},
fileEntry: {
fileSize: 12345n,
md5: new Uint8Array([0x11, 0x22]),
checkKey: new Uint8Array([0x33, 0x44]),
md5S2: new Uint8Array([0x55]),
fileId: 'file-1',
uploadKey: new Uint8Array([0x66]),
},
clientInfo: {
clientType: 3, appId: '100', terminalType: 3, clientVer: '1.1.1', unknown: 4,
},
fileNameInfo: { fileName: 'a.bin' },
host: {
hosts: [
{ url: { unknown: 1, host: 'upload.qq.com' }, port: 80 },
],
},
},
unknown200: 1,
};
const legacy = protoEncode(data as any, FileUploadExtSchema);
const proton = protobuf_encode<FileUploadExt>(data);
expect(hex(proton)).toBe(hex(legacy));
});
// ── oidb.ts coverage (OidbBase<T> generic envelope) ────────────────
it('encodes OidbBase<OidbSvcTrpcTcp0xFE5_2Response> identically to makeOidbBaseSchema', () => {
// This is the exact pattern runOidb uses for the OIDB envelope. Proton's
// OidbBase<T> 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<OidbBase<OidbSvcTrpcTcp0xFE5_2Response>>(envelope);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodeOidbEnv<T> pass-through wrapper byte-equivalent to direct codec', () => {
// The bridge-oidb.ts module exposes encodeOidbEnv<T> / decodeOidbEnv<T>
// as pure pass-through wrappers around protobuf_encode/decode<OidbBase<T>>.
// Proton wrapper-binds them and substitutes each call site with the
// monomorphized codec, so byte-output must match a direct call.
const body: OidbSvcTrpcTcp0xFE5_2Response = {
groups: [{ groupUin: 100, info: { groupName: 'G' } }],
};
const env = makeOidbEnvelope<OidbSvcTrpcTcp0xFE5_2Response>(0xFE5, 2, body, false);
const viaWrapper = encodeOidbEnv<OidbSvcTrpcTcp0xFE5_2Response>(env);
const viaDirect = protobuf_encode<OidbBase<OidbSvcTrpcTcp0xFE5_2Response>>(env);
expect(hex(viaWrapper)).toBe(hex(viaDirect));
const back = decodeOidbEnv<OidbSvcTrpcTcp0xFE5_2Response>(viaWrapper);
expect(back.body?.groups?.[0]?.groupUin).toBe(100);
expect(back.body?.groups?.[0]?.info?.groupName).toBe('G');
});
it('round-trips OidbBase<T> with non-trivial nested body', () => {
const body: OidbSvcTrpcTcp0xFE5_2Response = {
groups: [{ groupUin: 100, info: { groupName: 'G' } }],
};
const envelope: OidbBase<OidbSvcTrpcTcp0xFE5_2Response> = {
command: 0xFE5, subCommand: 2, errorCode: 1, body, errorMsg: 'ok', reserved: 1,
};
const encoded = protobuf_encode<OidbBase<OidbSvcTrpcTcp0xFE5_2Response>>(envelope);
const decoded = protobuf_decode<OidbBase<OidbSvcTrpcTcp0xFE5_2Response>>(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)', () => {
// 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 as any, 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 as any, SendLongMsgRespSchema);
const decoded = protobuf_decode<SendLongMsgResp>(encoded);
expect(decoded.result?.resId).toBe('rt-2');
});
// ── Group album TRPC envelopes (PR #31) ─────────────────────────────
//
// Raw TRPC packets — not OIDB-wrapped — that the group-album actions
// ship to `QunAlbum.trpc.qzone.webapp_qun_*`. Field names are mostly
// positional (`field1`/`field2`/...) because the upstream protocol
// doesn't expose semantic names; the byte-for-byte parity below is
// what guarantees the migration preserved every numbered field.
// NOTE: every numeric/bytes field below is non-zero / non-empty —
// proton follows proto3 semantics and skips default-valued fields on
// the wire, while the legacy runtime encoder writes them all. Byte
// parity therefore only holds when no field equals its default. The
// actual production callers in `actions/group-album.ts` *do* pass
// some zero-valued fields (e.g. `field1: 0`), which means the
// post-migration packet drops a few leading zero tags — semantically
// equivalent to a proto3-conforming reader, see the explicit
// divergence test earlier in this file.
it('encodes GetMediaListRequest (nested ReqInfo + extMap) identically', () => {
const data: GetMediaListRequest = {
field1: 1, // proto3 non-default
field2: new Uint8Array([0xab]),
field3: new Uint8Array([0xcd]),
reqInfo: { groupId: '555', albumId: 'alb-1', field3: 7, attachInfo: 'cursor', field5: 'tag' },
traceId: '_1700000000_12345',
extMap: [{ key: 'fc-appid', value: '100' }],
};
const legacy = protoEncode(data as any, GetMediaListRequestSchema);
const proton = protobuf_encode<GetMediaListRequest>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes DoQunCommentRequest (deeply nested anonymous-in-legacy schemas) identically', () => {
const data: DoQunCommentRequest = {
field1: 8527,
field2: new Uint8Array([0x01]),
field3: new Uint8Array([0x02]),
body: {
groupId: '555',
field3: 2,
reqBody: {
field1: { field3: 3, field4: 'h' },
field2: { field1: { uin: '10001' } },
field5: {
field1: {
field2: {
field1: 1, field2: 'a', lloc: 'lloc-x',
field4: 'b', field6: 'c', field7: 1, field8: 1, field9: 1,
field14: 1, field15: 1, field17: 1,
},
},
albumId: 'alb-1',
field5: 1,
},
},
field5: {
field2: { uin: '10001' },
field3: { field1: 1, field2: 'hello', field3: 'd', field4: 'e', field5: 1, field6: 'f' },
clientKey: 'ck-1',
},
},
traceId: '_1700000000_12345',
extMap: [{ key: 'fc-appid', value: '100' }],
};
const legacy = protoEncode(data as any, DoQunCommentRequestSchema);
const proton = protobuf_encode<DoQunCommentRequest>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes DoQunLikeRequest (with bigint time/batchId) identically', () => {
const data: DoQunLikeRequest = {
field1: 5495,
field2: 'h5_test',
field3: 'h5_test',
body: {
type: 2,
like: { id: '421_1_0_555|alb|42', status: 1 }, // non-default
publish: {
cellCommon: { time: 1700000000000n, feedId: '422_0_42' },
cellUserInfo: { user: { uin: '10001' } },
cellMedia: { albumId: 'alb', batchId: 42n },
cellQunInfo: { qunId: '555' },
},
clientKey: 'ck',
},
extMap: [{ key: 'fc-appid', value: '100' }],
};
const legacy = protoEncode(data as any, DoQunLikeRequestSchema);
const proton = protobuf_encode<DoQunLikeRequest>(data);
expect(hex(proton)).toBe(hex(legacy));
});
it('encodes DeleteMediasRequest + decodes DeleteMediasResponse identically', () => {
const req: DeleteMediasRequest = {
field1: 8694, field2: 'h5_test', field3: 'h5_test',
body: { groupId: '555', albumId: 'alb', lloc: 'lloc-x' },
traceId: 'tr-1',
extMap: [{ key: 'fc-appid', value: '100' }],
};
expect(hex(protobuf_encode<DeleteMediasRequest>(req)))
.toBe(hex(protoEncode(req as any, DeleteMediasRequestSchema)));
// Response: encode with legacy, decode with proton, fields stay aligned.
const respBytes = protoEncode({ field1: 8694, field2: 10023, field3: 'no right delete' } as any, DeleteMediasResponseSchema);
const decoded = protobuf_decode<DeleteMediasResponse>(respBytes);
expect(decoded.field1).toBe(8694);
expect(decoded.field2).toBe(10023);
expect(decoded.field3).toBe('no right delete');
});
});