Files
SnowLuma/packages/onebot/tests/temp-session-send.test.ts
T
DrSmoothl 3c73d1d5b0 feat(onebot): reply into group temp sessions (临时会话) (#212)
send_private_msg / send_msg can now reply to a group temp-session message
by passing the source group_id. Inbound temp messages surface their
source group on sender.group_id; a reply is only made into a temp session
the bot has an inbound record of. Group temp replies route through
RoutingHead.grpTmp (field 3).
2026-07-10 02:29:18 +08:00

44 lines
2.0 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { sendPrivateMessage } from '../src/modules/message-actions';
import { TempSessionStore } from '../src/temp-session-store';
import type { OneBotInstanceContext } from '../src/instance-context';
// The passive gate lives at the very top of sendPrivateMessage, before any
// message parsing or network call. These tests exercise that gate directly:
// a temp reply (tempGroupId set) into a session the peer never opened MUST be
// refused, without ever reaching the send path. This is the structural
// guarantee that the public build cannot initiate a temp session.
function refWith(store: TempSessionStore): OneBotInstanceContext {
const sendGroupTempMessage = vi.fn();
const sendPrivate = vi.fn();
return {
tempSessions: store,
// If the gate ever let a send through, these would be called — the tests
// assert they are NOT for the refused case.
bridge: { apis: { message: { sendGroupTempMessage, sendPrivate } } },
} as unknown as OneBotInstanceContext;
}
describe('sendPrivateMessage temp-session gate (passive-only)', () => {
it('refuses a temp reply to an unrecorded (user, group) and never sends', async () => {
const store = new TempSessionStore();
const ref = refWith(store);
await expect(
sendPrivateMessage(ref, 1001, [{ type: 'text', data: { text: 'hi' } }], false, /* tempGroupId */ 700),
).rejects.toThrow(/passive-only|temp session/i);
// The gate threw before any wire call.
expect((ref.bridge.apis.message as { sendGroupTempMessage: ReturnType<typeof vi.fn> }).sendGroupTempMessage)
.not.toHaveBeenCalled();
});
it('refuses when a DIFFERENT group is recorded (exact (user, group) match required)', async () => {
const store = new TempSessionStore();
store.record(1001, 701); // user opened a session from group 701, not 700
const ref = refWith(store);
await expect(
sendPrivateMessage(ref, 1001, [{ type: 'text', data: { text: 'hi' } }], false, 700),
).rejects.toThrow(/passive-only|temp session/i);
});
});