From 987ac09896d2dee8f5053a92c19d318934d83b1e Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 14 Jun 2026 19:11:03 +0800 Subject: [PATCH] feat(onebot): fetch real group history from server (#99) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit get_group_msg_history was served only from the local observed-message store, so it could never return more than what SnowLuma saw live (the "only 100 messages" symptom). It now fetches real history from the server. - protocol: new SsoGetGroupMsg packet (trpc.msg.register_proxy.RegisterProxy .SsoGetGroupMsg, sequence-range fetch; fields per Lagrange). Each returned PushMsgBody is decoded by the existing group decoder via a factored-out buildContextFromMessage. - core: MessageApi.getGroupHistory walks backward in small sequence windows with rate-limiting + data-volume guards — a process-wide throttle (≥300ms between sends), 30-seq chunks, ≤12 requests, ≤200 messages per call — so a hammering client can't make us flood the server and get kicked. - onebot: get_group_msg_history resolves the anchor sequence (requested message_id or latest observed), fetches from the server, converts + returns, and persists each fetched message. Falls back to the local store on unknown anchor / empty / error. Persisting fetched history also fixes replying to pre-injection messages: the reply path resolves message_id→sequence via the store, which now contains the fetched messages. Tests: SsoGetGroupMsg request/response round-trip + decode reuse (protocol); walk-back chunking/dedup/cap/floor (core). --- packages/core/src/bridge/apis/message.ts | 69 +++++++++++++++- .../core/tests/group-history-walkback.test.ts | 78 ++++++++++++++++++ packages/onebot/src/instance-context.ts | 4 +- .../onebot/src/modules/message-actions.ts | 77 +++++++++++++++++- packages/proto-defs/package.json | 4 + packages/proto-defs/src/get-group-msg.ts | 30 +++++++ packages/proto-defs/src/index.ts | 1 + packages/protocol/src/msg-push/context.ts | 30 +++++-- .../src/msg-push/fetch-group-history.ts | 56 +++++++++++++ packages/protocol/src/msg-push/index.ts | 2 + .../msg-push/fetch-group-history.test.ts | 80 +++++++++++++++++++ 11 files changed, 419 insertions(+), 12 deletions(-) create mode 100644 packages/core/tests/group-history-walkback.test.ts create mode 100644 packages/proto-defs/src/get-group-msg.ts create mode 100644 packages/protocol/src/msg-push/fetch-group-history.ts create mode 100644 packages/protocol/tests/msg-push/fetch-group-history.test.ts diff --git a/packages/core/src/bridge/apis/message.ts b/packages/core/src/bridge/apis/message.ts index 7b7cbc9b..ccfa2e6d 100644 --- a/packages/core/src/bridge/apis/message.ts +++ b/packages/core/src/bridge/apis/message.ts @@ -9,7 +9,8 @@ import type { SsoReadedReportReq, } from '@snowluma/proto-defs/oidb-actions/base'; import { buildSendElems } from '@snowluma/protocol/element-builder'; -import type { MessageElement } from '@snowluma/protocol/events'; +import type { MessageElement, QQEventVariant } from '@snowluma/protocol/events'; +import { fetchGroupMessageRange } from '@snowluma/protocol/msg-push'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import type { BridgeContext } from '../bridge-context'; // `Bridge` is imported as a type only so we can narrow `ctx` back to @@ -24,6 +25,31 @@ import type { Bridge, SendMessageReceipt } from '../bridge'; const SEND_MSG_CMD = 'MessageSvc.PbSendMsg'; +type GroupMessage = Extract; + +// ── Group-history fetch guards (the server frequency-limits / kicks abusive +// pulls, so keep each request small, bound the loop, and space the sends). ── +const HISTORY_MAX_COUNT = 200; // hard cap on messages returned per call +const HISTORY_CHUNK_SEQ = 30; // sequence span per server request (small packet) +const HISTORY_MAX_REQUESTS = 12; // bound the walk-back loop +const HISTORY_MIN_GAP_MS = 300; // minimum spacing between SsoGetGroupMsg sends + +// Serialized throttle gate: chains so concurrent callers queue, and enforces +// at least HISTORY_MIN_GAP_MS between actual sends (the first send isn't +// penalized). Process-wide — shared across all groups AND accounts in this +// process — so no client can make us flood the server regardless of fan-out. +let historyLastSendAt = 0; +let historyGate: Promise = Promise.resolve(); +function throttleHistory(): Promise { + const run = historyGate.then(async () => { + const wait = historyLastSendAt + HISTORY_MIN_GAP_MS - Date.now(); + if (wait > 0) await new Promise((resolve) => setTimeout(resolve, wait)); + historyLastSendAt = Date.now(); + }); + historyGate = run.catch(() => undefined); + return run; +} + export class MessageApi { constructor(private readonly ctx: BridgeContext) { } @@ -328,5 +354,46 @@ export class MessageApi { throw new Error(result.errorMessage || 'mark private message read failed'); } } + + /** + * Fetch real group history from the server (`SsoGetGroupMsg`), ending at and + * including `endSeq`, walking backward in small windows until `count` + * messages are gathered or the floor is reached. Rate-limited + capped so a + * hammering client can't make us flood the server (and get the account + * kicked). Returns decoded `group_message` events oldest→newest. + */ + async getGroupHistory(groupUin: number, endSeq: number, count: number, selfUin = 0): Promise { + if (!(groupUin > 0) || !(endSeq > 0)) return []; + const want = Math.min(Math.max(1, Math.trunc(count) || 0), HISTORY_MAX_COUNT); + + const collected = new Map(); // dedup by sequence + let curEnd = endSeq; + for (let i = 0; i < HISTORY_MAX_REQUESTS && collected.size < want && curEnd >= 1; i++) { + const start = Math.max(1, curEnd - HISTORY_CHUNK_SEQ + 1); + await throttleHistory(); + let batch: GroupMessage[]; + try { + batch = await fetchGroupMessageRange(this.ctx, this.ctx.identity, selfUin, groupUin, start, curEnd); + } catch { + break; // network/decode error — return whatever we have so far + } + if (batch.length > 0) { + let minSeq = curEnd; + for (const ev of batch) { + collected.set(ev.msgSeq, ev); + if (ev.msgSeq < minSeq) minSeq = ev.msgSeq; + } + // Continue strictly below the OLDEST sequence the server actually + // returned — covers the case where the server caps a window short. + curEnd = minSeq - 1; + } else { + // Empty window (a gap of recalled/absent seqs) — skip past it. + curEnd = start - 1; + } + } + + const all = [...collected.values()].sort((a, b) => a.msgSeq - b.msgSeq); + return all.slice(-want); // newest `want`, oldest→newest + } } diff --git a/packages/core/tests/group-history-walkback.test.ts b/packages/core/tests/group-history-walkback.test.ts new file mode 100644 index 00000000..541d73fc --- /dev/null +++ b/packages/core/tests/group-history-walkback.test.ts @@ -0,0 +1,78 @@ +// MessageApi.getGroupHistory walk-back loop: chunking, dedup, server-short-cap +// handling, floor termination, slice(-want). fetchGroupMessageRange is mocked +// so this exercises the loop/cursor logic, not the wire decode (covered in +// packages/protocol/tests/msg-push/fetch-group-history.test.ts). + +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +vi.mock('@snowluma/protocol/msg-push', async (importOriginal) => { + const actual = await importOriginal(); + return { ...actual, fetchGroupMessageRange: vi.fn() }; +}); + +import { fetchGroupMessageRange } from '@snowluma/protocol/msg-push'; +import { MessageApi } from '../src/bridge/apis/message'; + +const mockFetch = vi.mocked(fetchGroupMessageRange); + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function gm(seq: number): any { + return { + kind: 'group_message', time: 0, selfUin: 0, senderUin: 1, msgSeq: seq, + msgId: seq, elements: [], groupId: 9999, senderNick: '', senderCard: '', senderRole: '', + }; +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const ctx = { identity: {}, sendRawPacket: vi.fn() } as any; + +describe('MessageApi.getGroupHistory walk-back', () => { + beforeEach(() => mockFetch.mockReset()); + + it('walks back across short-capped windows, dedups, returns newest `want` ascending', async () => { + // Server caps every window to its newest 10 sequences → forces walk-back. + mockFetch.mockImplementation(async (_s, _id, _self, _g, start, end) => { + const out = []; + for (let s = Math.max(start, end - 9); s <= end; s++) out.push(gm(s)); + return out; + }); + + const res = await new MessageApi(ctx).getGroupHistory(9999, 200, 50); + + expect(res).toHaveLength(50); + expect(res[0].msgSeq).toBe(151); // newest 50 ending at 200 → 151..200 + expect(res[res.length - 1].msgSeq).toBe(200); + for (let i = 1; i < res.length; i++) { + expect(res[i].msgSeq).toBeGreaterThan(res[i - 1].msgSeq); // strictly ascending, no dups + } + }); + + it('stops at the sequence floor when fewer messages exist than requested', async () => { + mockFetch.mockImplementation(async (_s, _id, _self, _g, start, end) => { + const out = []; + for (let s = start; s <= end; s++) out.push(gm(s)); + return out; + }); + + const res = await new MessageApi(ctx).getGroupHistory(9999, 20, 100); // only seq 1..20 exist + expect(res.map((m) => m.msgSeq)).toEqual(Array.from({ length: 20 }, (_, i) => i + 1)); + }); + + it('caps the request count and bounds the request loop', async () => { + let calls = 0; + mockFetch.mockImplementation(async (_s, _id, _self, _g, _start, end) => { + calls++; + return [gm(end)]; // one message per window → never reaches a large `want` + }); + + const res = await new MessageApi(ctx).getGroupHistory(9999, 100_000, 9999); + expect(calls).toBeLessThanOrEqual(12); // HISTORY_MAX_REQUESTS + expect(res.length).toBeLessThanOrEqual(200); // HISTORY_MAX_COUNT + }); + + it('returns [] for an invalid anchor without sending', async () => { + expect(await new MessageApi(ctx).getGroupHistory(0, 100, 10)).toEqual([]); + expect(await new MessageApi(ctx).getGroupHistory(9999, 0, 10)).toEqual([]); + expect(mockFetch).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/onebot/src/instance-context.ts b/packages/onebot/src/instance-context.ts index e0047947..92f5d371 100644 --- a/packages/onebot/src/instance-context.ts +++ b/packages/onebot/src/instance-context.ts @@ -27,7 +27,7 @@ import { forwardSingleMessage, getForwardMessage, getFriendMsgHistory, - getGroupMsgHistory, + getGroupHistory, sendGroupForwardMessage, sendGroupMessage, sendPrivateForwardMessage, @@ -76,7 +76,7 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext { getStrangerInfo: (userId) => getStrangerInfo(bridge, userId), getGroupFiles: (groupId, folderId) => getGroupFiles(bridge, groupId, folderId), handleGroupRequest: (flag, _subType, approve, reason) => handleGroupAddRequest(bridge, flag, approve, reason), - getGroupMsgHistory: (groupId, messageId, count) => getGroupMsgHistory(messageStore, groupId, messageId, count), + getGroupMsgHistory: (groupId, messageId, count) => getGroupHistory(ref, groupId, messageId, count), getFriendMsgHistory: (userId, messageId, count) => getFriendMsgHistory(messageStore, userId, messageId, count), handleGetGroupSystemMsg: () => getGroupSystemMessages(bridge), getDownloadRKeys: () => getDownloadRKeys(bridge), diff --git a/packages/onebot/src/modules/message-actions.ts b/packages/onebot/src/modules/message-actions.ts index d8db7760..ec06ac81 100644 --- a/packages/onebot/src/modules/message-actions.ts +++ b/packages/onebot/src/modules/message-actions.ts @@ -2,7 +2,7 @@ import { createLogger } from '@snowluma/common/logger'; import type { BridgeInterface } from '@snowluma/core/bridge-interface'; import type { ForwardNodePayload, MessageElement } from '@snowluma/protocol/events'; import type { MessageSendResult } from '../api-handler'; -import { elementsToOneBotSegments } from '../event-converter'; +import { convertEvent, elementsToOneBotSegments, type ConverterContext } from '../event-converter'; import { segmentsToRawMessage } from '../helper/cq'; import type { OneBotInstanceContext } from '../instance-context'; import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id'; @@ -64,6 +64,81 @@ export async function getFriendMsgHistory( .map(sanitizeMessageEventForApi); } +// Deps the server-backed history fetch needs from the instance context. +interface GroupHistoryRef { + bridge: BridgeInterface; + messageStore: MessageStore; + converterCtx: ConverterContext; + selfId: number; +} + +/** + * Group history, fetched from the server (`SsoGetGroupMsg`) instead of only the + * local observed-message store — so it can return messages SnowLuma never saw + * live, and (because each fetched message is persisted) reply / get_msg on old + * messages start working too. Resolves the anchor sequence from the requested + * message_id (or the latest observed message), then asks the bridge for the + * `count` messages ending there. Falls back to the local store if the server + * fetch is unavailable or empty. + */ +export async function getGroupHistory( + ref: GroupHistoryRef, + groupId: number, + messageId: number | undefined, + count: number | undefined, +): Promise { + if (!Number.isInteger(groupId) || groupId <= 0) return []; + const want = normalizeHistoryCount(count); + + let anchorSeq = 0; + if (Number.isInteger(messageId) && messageId !== 0) { + const meta = ref.messageStore.findMeta(messageId as number); + if (!meta || !meta.isGroup || meta.targetId !== groupId || meta.sequence <= 0) { + // Anchor we don't know — best effort from the local store. + return getGroupMsgHistory(ref.messageStore, groupId, messageId, count); + } + anchorSeq = meta.sequence; + } else { + const latest = ref.messageStore.listSessionEvents(true, groupId, 1); + anchorSeq = latest.length ? toHistInt(latest[latest.length - 1].message_seq) : 0; + } + + if (anchorSeq > 0) { + try { + const events = await ref.bridge.apis.message.getGroupHistory(groupId, anchorSeq, want, ref.selfId); + const out: JsonObject[] = []; + for (const ev of events) { + const json = await convertEvent(ref.converterCtx, ev); + if (!json || json.message_type !== 'group') continue; + persistGroupHistoryEvent(ref.messageStore, json); // full event → reply/get_msg + future listing + out.push(sanitizeMessageEventForApi(json)); // sanitized for the API (matches the local path) + } + if (out.length > 0) return out; + } catch (err) { + log.warn('group history server fetch failed (%s); using local store', err instanceof Error ? err.message : String(err)); + } + } + + return getGroupMsgHistory(ref.messageStore, groupId, messageId, count); +} + +function persistGroupHistoryEvent(store: MessageStore, event: JsonObject): void { + const messageId = toHistInt(event.message_id); + const sessionId = toHistInt(event.group_id); + const sequence = toHistInt(event.message_seq); + if (messageId === 0 || sessionId === 0) return; + store.storeEvent(messageId, true, sessionId, sequence, GROUP_MESSAGE_EVENT, event); +} + +function toHistInt(value: unknown): number { + if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value); + if (typeof value === 'string' && value.trim()) { + const n = Number(value); + if (Number.isFinite(n)) return Math.trunc(n); + } + return 0; +} + export async function deleteMessage(bridge: BridgeInterface, meta: MessageMeta): Promise { if (meta.isGroup) { await bridge.apis.message.recallGroup(meta.targetId, meta.sequence); diff --git a/packages/proto-defs/package.json b/packages/proto-defs/package.json index 6689afa8..31560d77 100644 --- a/packages/proto-defs/package.json +++ b/packages/proto-defs/package.json @@ -31,6 +31,10 @@ "types": "./src/message.ts", "default": "./src/message.ts" }, + "./get-group-msg": { + "types": "./src/get-group-msg.ts", + "default": "./src/get-group-msg.ts" + }, "./notify": { "types": "./src/notify.ts", "default": "./src/notify.ts" diff --git a/packages/proto-defs/src/get-group-msg.ts b/packages/proto-defs/src/get-group-msg.ts new file mode 100644 index 00000000..ef97f12e --- /dev/null +++ b/packages/proto-defs/src/get-group-msg.ts @@ -0,0 +1,30 @@ +import type { pb, pb_repeated, uint_32, bool } from '@snowluma/proton'; +import type { PushMsgBody } from './message'; + +// trpc.msg.register_proxy.RegisterProxy.SsoGetGroupMsg — fetch group message +// history from the server by sequence range. Reference: Lagrange.Core +// Internal/Service/Message/GetGroupMessageService.cs + Packets/Message/Action/ +// SsoGetGroupMsg(.Response).cs. The response carries the same `PushMsgBody` +// shape as a live OlPush message, so the existing msg-push decoders parse it. + +export interface SsoGetGroupMsgInfo { + groupUin?: pb<1, uint_32>; + startSequence?: pb<2, uint_32>; + endSequence?: pb<3, uint_32>; +} + +export interface SsoGetGroupMsg { + info?: pb<1, SsoGetGroupMsgInfo>; + direction?: pb<2, bool>; // true (Lagrange hard-codes this) +} + +export interface SsoGetGroupMsgResponseBody { + groupUin?: pb<3, uint_32>; + startSequence?: pb<4, uint_32>; + endSequence?: pb<5, uint_32>; + messages?: pb_repeated<6, PushMsgBody>; +} + +export interface SsoGetGroupMsgResponse { + body?: pb<3, SsoGetGroupMsgResponseBody>; +} diff --git a/packages/proto-defs/src/index.ts b/packages/proto-defs/src/index.ts index 614f5e73..0f92fba6 100644 --- a/packages/proto-defs/src/index.ts +++ b/packages/proto-defs/src/index.ts @@ -25,6 +25,7 @@ export type * from './element.js'; // NOTE: `highway.ts` intentionally NOT re-exported here — see note above. export type * from './longmsg.js'; export type * from './message.js'; +export type * from './get-group-msg.js'; export type * from './notify.js'; export type * from './oidb.js'; export type * from './oidb-action.js'; diff --git a/packages/protocol/src/msg-push/context.ts b/packages/protocol/src/msg-push/context.ts index 463fc087..6c5f0db5 100644 --- a/packages/protocol/src/msg-push/context.ts +++ b/packages/protocol/src/msg-push/context.ts @@ -1,7 +1,7 @@ import { protobuf_decode } from '@snowluma/proton'; import type { PacketInfo } from '@snowluma/common/protocol-types'; import type { IdentityService } from '../identity-service'; -import type { ContentHead, MessageBody, PushMsg, ResponseHead } from '@snowluma/proto-defs/message'; +import type { ContentHead, MessageBody, PushMsg, PushMsgBody as ProtoMessage, ResponseHead } from '@snowluma/proto-defs/message'; export type PushMsgBody = MessageBody; export type PushMsgResponseHead = ResponseHead; @@ -37,7 +37,27 @@ export function buildContext(pkt: PacketInfo, identity: IdentityService): MsgPus const push = protobuf_decode(pkt.body); if (!push?.message) return null; - const msg = push.message; + let selfUin = 0; + if (pkt.uin) { + const n = parseInt(pkt.uin, 10); + if (!isNaN(n)) selfUin = n; + } + + return buildContextFromMessage(push.message, selfUin, identity); +} + +/** + * Build a push context from an already-extracted per-message struct (the same + * `PushMsgBody` shape that `PushMsg.message` carries). Used both by + * {@link buildContext} (live OlPush) and by the group-history fetch, whose + * `SsoGetGroupMsgResponse.body.messages` are these same structs — so the + * regular msg-push decoders parse fetched history unchanged. + */ +export function buildContextFromMessage( + msg: ProtoMessage, + selfUin: number, + identity: IdentityService, +): MsgPushContext | null { if (!msg.contentHead) return null; const head: MsgPushHead = { @@ -55,12 +75,6 @@ export function buildContext(pkt: PacketInfo, identity: IdentityService): MsgPus fromUid = msg.responseHead.fromUid ?? ''; } - let selfUin = 0; - if (pkt.uin) { - const n = parseInt(pkt.uin, 10); - if (!isNaN(n)) selfUin = n; - } - const content = msg.body?.msgContent ?? new Uint8Array(0); return { diff --git a/packages/protocol/src/msg-push/fetch-group-history.ts b/packages/protocol/src/msg-push/fetch-group-history.ts new file mode 100644 index 00000000..df201cec --- /dev/null +++ b/packages/protocol/src/msg-push/fetch-group-history.ts @@ -0,0 +1,56 @@ +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'; +import type { QQEventVariant } from '../events'; +import type { IdentityService } from '../identity-service'; +import { buildContextFromMessage } from './context'; +import { decodeGroupMessage } from './decoders/group-message'; + +export const SSO_GET_GROUP_MSG_CMD = 'trpc.msg.register_proxy.RegisterProxy.SsoGetGroupMsg'; + +type GroupMessage = Extract; + +interface RawSender { + sendRawPacket(serviceCmd: string, body: Uint8Array, timeoutMs?: number): Promise; +} + +/** + * Fetch one [startSeq, endSeq] window of group history from the server via + * `SsoGetGroupMsg`, decoding each returned message with the regular group + * decoder. Returns `group_message` events sorted oldest→newest by sequence. + * + * One packet per call — the caller (MessageApi.getGroupHistory) owns the + * chunking/throttling so the server's frequency limits aren't tripped. + */ +export async function fetchGroupMessageRange( + sender: RawSender, + identity: IdentityService, + selfUin: number, + groupUin: number, + startSeq: number, + endSeq: number, +): Promise { + if (!(groupUin > 0) || !(endSeq > 0) || startSeq > endSeq) return []; + + const req = protobuf_encode({ + info: { groupUin, startSequence: startSeq, endSequence: endSeq }, + direction: true, + }); + + const res = await sender.sendRawPacket(SSO_GET_GROUP_MSG_CMD, req); + if (!res.success || !res.gotResponse || !res.responseData) return []; + + const decoded = protobuf_decode(res.responseData); + const messages = decoded?.body?.messages ?? []; + + const out: GroupMessage[] = []; + for (const msg of messages) { + const ctx = buildContextFromMessage(msg, selfUin, identity); + if (!ctx) continue; + for (const ev of decodeGroupMessage(ctx)) { + if (ev.kind === 'group_message' && ev.msgSeq > 0) out.push(ev); + } + } + out.sort((a, b) => a.msgSeq - b.msgSeq); + return out; +} diff --git a/packages/protocol/src/msg-push/index.ts b/packages/protocol/src/msg-push/index.ts index d69f00cd..d9132170 100644 --- a/packages/protocol/src/msg-push/index.ts +++ b/packages/protocol/src/msg-push/index.ts @@ -18,6 +18,8 @@ import { decodeTempMessage } from './decoders/temp-message'; import { PkgType } from './enums'; import { MsgPushRegistry } from './registry'; +export { SSO_GET_GROUP_MSG_CMD, fetchGroupMessageRange } from './fetch-group-history'; + export const MSG_PUSH_CMD = 'trpc.msg.olpush.OlPushService.MsgPush'; const registry = new MsgPushRegistry(); diff --git a/packages/protocol/tests/msg-push/fetch-group-history.test.ts b/packages/protocol/tests/msg-push/fetch-group-history.test.ts new file mode 100644 index 00000000..422a1e91 --- /dev/null +++ b/packages/protocol/tests/msg-push/fetch-group-history.test.ts @@ -0,0 +1,80 @@ +// Round-trips the SsoGetGroupMsg request/response protos (proton codegen + +// field tags) and the fetched-history decode path (each returned PushMsgBody +// re-uses the regular group decoder). + +import { 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'; +import type { IdentityService } from '../../src/identity-service'; +import { SSO_GET_GROUP_MSG_CMD, fetchGroupMessageRange } from '../../src/msg-push'; + +const identity = { findGroupMember: () => undefined } as unknown as IdentityService; + +function okResult(data: Uint8Array): SendPacketResult { + return { success: true, gotResponse: true, responseData: data } as SendPacketResult; +} + +describe('fetchGroupMessageRange / SsoGetGroupMsg', () => { + it('sends the right command and request field tags', async () => { + let captured: { cmd: string; body: Uint8Array } | null = null; + const sender = { + sendRawPacket: async (cmd: string, body: Uint8Array) => { + captured = { cmd, body }; + return okResult(protobuf_encode({ body: { groupUin: 9999, messages: [] } })); + }, + }; + + await fetchGroupMessageRange(sender, identity, 10001, 9999, 100, 120); + + expect(captured!.cmd).toBe(SSO_GET_GROUP_MSG_CMD); + const req = protobuf_decode(captured!.body); + expect(req.info?.groupUin).toBe(9999); + expect(req.info?.startSequence).toBe(100); + expect(req.info?.endSequence).toBe(120); + expect(req.direction).toBe(true); + }); + + it('decodes returned group messages (seq/group/sender, self uin), oldest→newest', async () => { + const resp = protobuf_encode({ + body: { + groupUin: 9999, + startSequence: 100, + endSequence: 120, + messages: [ + { + responseHead: { fromUin: 222, grp: { groupUin: 9999, memberName: 'Bob' } }, + contentHead: { msgType: 82, sequence: 120, timestamp: 1700000120, msgId: 5120 }, + body: { richText: { elems: [] } }, + }, + { + responseHead: { fromUin: 111, grp: { groupUin: 9999, memberName: 'Alice' } }, + contentHead: { msgType: 82, sequence: 110, timestamp: 1700000110, msgId: 5110 }, + body: { richText: { elems: [] } }, + }, + ], + }, + }); + const sender = { sendRawPacket: async () => okResult(resp) }; + + const out = await fetchGroupMessageRange(sender, identity, 10001, 9999, 100, 120); + + expect(out.map((m) => m.msgSeq)).toEqual([110, 120]); // sorted ascending + expect(out.every((m) => m.kind === 'group_message')).toBe(true); + expect(out.every((m) => m.groupId === 9999)).toBe(true); + expect(out.every((m) => m.selfUin === 10001)).toBe(true); + expect(out[0]).toMatchObject({ msgSeq: 110, senderUin: 111, senderNick: 'Alice' }); + expect(out[1]).toMatchObject({ msgSeq: 120, senderUin: 222, senderNick: 'Bob' }); + }); + + it('returns [] on a failed packet or out-of-range request', async () => { + const failSender = { sendRawPacket: async () => ({ success: false, gotResponse: false } as SendPacketResult) }; + expect(await fetchGroupMessageRange(failSender, identity, 10001, 9999, 100, 120)).toEqual([]); + + // start > end is rejected before any send + let sent = false; + const guardSender = { sendRawPacket: async () => { sent = true; return okResult(new Uint8Array()); } }; + expect(await fetchGroupMessageRange(guardSender, identity, 10001, 9999, 200, 100)).toEqual([]); + expect(sent).toBe(false); + }); +});