refactor(protocol): decide message bubbles after decode

Live push and history now share one keep-or-drop decision. Control
signals and empty bodies stay hidden; undecodable content is kept and
logged on both paths.
This commit is contained in:
DrSmoothl committed 2026-08-30 13:45:22 +08:00
1 parent f6adadcd12
commit a0f152229d
7 files changed
+252 -90

No files matched your search

@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest';
import {
classifyMessageSurvival,
isBlankMessage,
isC2cControlPush,
} from '../../src/msg-push/blank-filter';
const privateHead = { msgType: 166, c2cCmd: 0 };
const controlHead = { msgType: 166, c2cCmd: 75 };
const groupHead = { msgType: 82, c2cCmd: 0 };
describe('classifyMessageSurvival', () => {
it('drops C2C control even when elements decoded', () => {
expect(isC2cControlPush(controlHead)).toBe(true);
expect(classifyMessageSurvival(controlHead, [{ type: 'text' }], {
richText: { elems: [{ text: { str: 'noise' } }] },
})).toBe('drop-control');
});
it('drops a genuinely empty non-control body', () => {
expect(isBlankMessage([], { richText: { elems: [] } })).toBe(true);
expect(classifyMessageSurvival(privateHead, [], { richText: { elems: [] } })).toBe('drop-blank');
expect(classifyMessageSurvival(groupHead, [], { richText: { elems: [] } })).toBe('drop-blank');
});
it('keeps an empty decode when the body still carried content', () => {
const body = { richText: { elems: [{ commonElem: { serviceType: 999, businessType: 0 } }] } };
expect(isBlankMessage([], body)).toBe(false);
expect(classifyMessageSurvival(privateHead, [], body)).toBe('keep-undecoded');
});
it('keeps a message that decoded to elements', () => {
expect(classifyMessageSurvival(privateHead, [{ type: 'text' }], {
richText: { elems: [{ text: { str: 'hi' } }] },
})).toBe('keep');
});
});
@@ -3,7 +3,13 @@
// re-uses the regular friend decoder).
import { deflateSync } from 'node:zlib';
import { describe, expect, it } from 'vitest';
import {
getLogLevel,
setLogLevel,
subscribeLogs,
type LogEntry,
} from '@snowluma/common/logger';
import { afterEach, describe, expect, it } from 'vitest';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type {
@@ -24,6 +30,11 @@ import {
} from '../../src/msg-push';
const identity = { findFriend: () => undefined } as unknown as IdentityService;
const previousLogLevel = getLogLevel();
afterEach(() => {
setLogLevel(previousLogLevel);
});
function okResult(data: Uint8Array): SendPacketResult {
return { success: true, gotResponse: true, responseData: data } as SendPacketResult;
@@ -124,6 +135,37 @@ describe('fetchC2cMessageRange / SsoGetC2cMsg', () => {
expect(out[0]).toMatchObject({ senderUin: 222, elements: [{ type: 'text', text: 'hi' }] });
});
it('keeps and warns on a body that had content but decoded to no elements', async () => {
const resp = protobuf_encode<SsoGetC2cMsgResponse>({
friendUid: 'u_friend',
messages: [{
responseHead: { fromUin: 222, forward: { friendName: 'Bob' } },
contentHead: { msgType: 166, sequence: 9120, ntMsgSeq: 120, timestamp: 1700000120, msgId: 7120 },
body: { richText: { elems: [{ commonElem: { serviceType: 999, businessType: 0 } }] } },
}],
});
const warnings: string[] = [];
setLogLevel('warn');
const unsubscribe = subscribeLogs((entry: LogEntry) => {
if (entry.level === 'warn') warnings.push(entry.message);
});
try {
const out = await fetchC2cMessageRange(
{ sendRawPacket: async () => okResult(resp) },
identity,
10001,
'u_friend',
100,
120,
);
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ kind: 'friend_message', ntMsgSeq: 120, elements: [] });
expect(warnings.some((message) => message.includes('missing decoder'))).toBe(true);
} finally {
unsubscribe();
}
});
it('rejects a visible message without the canonical NT sequence', async () => {
const resp = protobuf_encode<SsoGetC2cMsgResponse>({
friendUid: 'u_friend',
@@ -2,7 +2,13 @@
// field tags) and the fetched-history decode path (each returned PushMsgBody
// re-uses the regular group decoder).
import { describe, expect, it } from 'vitest';
import {
getLogLevel,
setLogLevel,
subscribeLogs,
type LogEntry,
} from '@snowluma/common/logger';
import { afterEach, describe, expect, it } from 'vitest';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { SsoGetGroupMsg, SsoGetGroupMsgResponse } from '@snowluma/proto-defs/get-group-msg';
@@ -10,6 +16,11 @@ import type { IdentityService } from '../../src/identity-service';
import { SSO_GET_GROUP_MSG_CMD, fetchGroupMessageRange } from '../../src/msg-push';
const identity = { findGroupMember: () => undefined, findGroup: () => null } as unknown as IdentityService;
const previousLogLevel = getLogLevel();
afterEach(() => {
setLogLevel(previousLogLevel);
});
function okResult(data: Uint8Array): SendPacketResult {
return { success: true, gotResponse: true, responseData: data } as SendPacketResult;
@@ -119,6 +130,39 @@ describe('fetchGroupMessageRange / SsoGetGroupMsg', () => {
expect(out[0]).toMatchObject({ senderUin: 222, elements: [{ type: 'text', text: 'hi' }] });
});
it('keeps and warns on a body that had content but decoded to no elements', async () => {
const resp = protobuf_encode<SsoGetGroupMsgResponse>({
body: {
groupUin: 9999,
messages: [{
responseHead: { fromUin: 222, grp: { groupUin: 9999, memberName: 'Bob' } },
contentHead: { msgType: 82, sequence: 120, timestamp: 1700000120, msgId: 5120 },
body: { richText: { elems: [{ commonElem: { serviceType: 999, businessType: 0 } }] } },
}],
},
});
const warnings: string[] = [];
setLogLevel('warn');
const unsubscribe = subscribeLogs((entry: LogEntry) => {
if (entry.level === 'warn') warnings.push(entry.message);
});
try {
const out = await fetchGroupMessageRange(
{ sendRawPacket: async () => okResult(resp) },
identity,
10001,
9999,
100,
120,
);
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ kind: 'group_message', msgSeq: 120, elements: [] });
expect(warnings.some((message) => message.includes('missing decoder'))).toBe(true);
} finally {
unsubscribe();
}
});
it('drops QQ null roam records even when they carry a deleted placeholder (#254)', async () => {
const resp = protobuf_encode<SsoGetGroupMsgResponse>({
body: {