feat(proto): implement deflated payload for JSON and XML elements in message builder
This commit is contained in:
3 files changed
+77
-22
No files matched your search
@@ -35,6 +35,43 @@ function commonElem(elem: any): { serviceType: number; businessType: number; pbE
|
||||
return elem.commonElem;
|
||||
}
|
||||
|
||||
function inflatePrefixedPayload(data: Uint8Array): string {
|
||||
expect(data[0]).toBe(0x01);
|
||||
return inflateSync(Buffer.from(data.subarray(1))).toString('utf8');
|
||||
}
|
||||
|
||||
describe('element-builder / rich card encoding', () => {
|
||||
it('encodes json segments as deflated LightApp payloads and preserves non-ASCII text', async () => {
|
||||
const card = JSON.stringify({
|
||||
app: 'com.tencent.structmsg',
|
||||
view: 'music',
|
||||
prompt: '[音乐]起风了',
|
||||
meta: { music: { title: '起风了', desc: '买辣椒也用券' } },
|
||||
});
|
||||
|
||||
const [elem] = await buildSendElems(
|
||||
[{ type: 'json', text: card } as any],
|
||||
{ bridge: fakeBridge, groupId: 12345 },
|
||||
);
|
||||
|
||||
expect(elem.lightApp).toBeDefined();
|
||||
expect((elem as any).richMsg).toBeUndefined();
|
||||
expect(inflatePrefixedPayload(elem.lightApp!.data!)).toBe(card);
|
||||
});
|
||||
|
||||
it('encodes xml richMsg payloads by UTF-8 bytes, not JavaScript string length', async () => {
|
||||
const xml = '<msg serviceID="35" brief="[分享]中文卡片"></msg>';
|
||||
|
||||
const [elem] = await buildSendElems(
|
||||
[{ type: 'xml', text: xml, subType: 35 } as any],
|
||||
{ bridge: fakeBridge, groupId: 12345 },
|
||||
);
|
||||
|
||||
expect(elem.richMsg?.serviceId).toBe(35);
|
||||
expect(inflatePrefixedPayload(elem.richMsg!.template1!)).toBe(xml);
|
||||
});
|
||||
});
|
||||
|
||||
describe('element-builder / commonElem.businessType per scene', () => {
|
||||
describe('image', () => {
|
||||
it('c2c → businessType 10', async () => {
|
||||
@@ -129,10 +166,7 @@ describe('element-builder / forward preview (com.tencent.multimsg LightApp)', ()
|
||||
// the outer's LongMsgResult — without uniseq the QQ-NT client has
|
||||
// no way to walk the tree and has to re-fetch each inner resId.
|
||||
function decodeLightApp(elem: any): unknown {
|
||||
const data: Uint8Array = elem.lightApp.data;
|
||||
expect(data[0]).toBe(0x01); // deflate prefix
|
||||
const inflated = inflateSync(Buffer.from(data.subarray(1))).toString('utf8');
|
||||
return JSON.parse(inflated);
|
||||
return JSON.parse(inflatePrefixedPayload(elem.lightApp.data));
|
||||
}
|
||||
|
||||
it('emits a lightApp.data blob with prefix=0x01 (deflated JSON, not XML/serviceID=35)', async () => {
|
||||
|
||||
@@ -413,6 +413,33 @@ describe('parseMessage', () => {
|
||||
const parsed = JSON.parse(result[0].text!);
|
||||
expect(parsed.meta.contact.type).toBe('group');
|
||||
});
|
||||
|
||||
it('parses signed music cards and buildSendElems preserves non-ASCII JSON', async () => {
|
||||
const card = JSON.stringify({
|
||||
app: 'com.tencent.structmsg',
|
||||
view: 'music',
|
||||
prompt: '[音乐]风中有朵雨做的云',
|
||||
meta: { music: { title: '风中有朵雨做的云', desc: '孟庭苇' } },
|
||||
});
|
||||
vi.stubGlobal('fetch', vi.fn(async () => ({
|
||||
ok: true,
|
||||
text: async () => card,
|
||||
})));
|
||||
|
||||
try {
|
||||
const result = await parseMessage(
|
||||
[{ type: 'music', data: { type: '163', id: '123456' } }] as any,
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([{ type: 'json', text: card }]);
|
||||
|
||||
const protoElems = await buildSendElems(result);
|
||||
expect(protoElems).toHaveLength(1);
|
||||
expect(protoElems[0].lightApp?.data?.[0]).toBe(0x01);
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('single segment object', () => {
|
||||
|
||||
@@ -105,32 +105,31 @@ function makeReplyElem(element: MessageElement): ProtoElem {
|
||||
return { srcMsg };
|
||||
}
|
||||
|
||||
function makeDeflatedPayload(content: string): Uint8Array {
|
||||
const deflated = deflateSync(Buffer.from(content, 'utf8'));
|
||||
const payload = new Uint8Array(deflated.length + 1);
|
||||
payload[0] = 0x01;
|
||||
payload.set(deflated, 1);
|
||||
return payload;
|
||||
}
|
||||
|
||||
function makeJsonElem(element: MessageElement): ProtoElem {
|
||||
const content = element.text ?? '';
|
||||
const payload = new Uint8Array(content.length + 1);
|
||||
payload[0] = 0x00;
|
||||
const encoded = new TextEncoder().encode(content);
|
||||
payload.set(encoded, 1);
|
||||
|
||||
return {
|
||||
richMsg: {
|
||||
serviceId: 1,
|
||||
template1: payload,
|
||||
lightApp: {
|
||||
data: makeDeflatedPayload(content),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function makeXmlElem(element: MessageElement): ProtoElem {
|
||||
const content = element.text ?? '';
|
||||
const payload = new Uint8Array(content.length + 1);
|
||||
payload[0] = 0x00;
|
||||
const encoded = new TextEncoder().encode(content);
|
||||
payload.set(encoded, 1);
|
||||
|
||||
return {
|
||||
richMsg: {
|
||||
serviceId: element.subType === 0 ? 35 : (element.subType ?? 35),
|
||||
template1: payload,
|
||||
template1: makeDeflatedPayload(content),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -208,14 +207,9 @@ function makeForwardElem(element: MessageElement): ProtoElem {
|
||||
};
|
||||
|
||||
const json = JSON.stringify(lightApp);
|
||||
const deflated = deflateSync(Buffer.from(json, 'utf8'));
|
||||
const payload = new Uint8Array(deflated.length + 1);
|
||||
payload[0] = 0x01; // 0x01 prefix = deflated JSON (vs 0x00 = uncompressed XML)
|
||||
payload.set(deflated, 1);
|
||||
|
||||
return {
|
||||
lightApp: {
|
||||
data: payload,
|
||||
data: makeDeflatedPayload(json),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user