File size: 3,063 Bytes
fb4d8fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import type { MsgContext } from "../../auto-reply/templating.js";
import { expectInboundContextContract } from "../../../test/helpers/inbound-contract.js";

let capturedCtx: MsgContext | undefined;

vi.mock("../../auto-reply/dispatch.js", async (importOriginal) => {
  const actual = await importOriginal<typeof import("../../auto-reply/dispatch.js")>();
  const dispatchInboundMessage = vi.fn(async (params: { ctx: MsgContext }) => {
    capturedCtx = params.ctx;
    return { queuedFinal: false, counts: { tool: 0, block: 0, final: 0 } };
  });
  return {
    ...actual,
    dispatchInboundMessage,
    dispatchInboundMessageWithDispatcher: dispatchInboundMessage,
    dispatchInboundMessageWithBufferedDispatcher: dispatchInboundMessage,
  };
});

import { processDiscordMessage } from "./message-handler.process.js";

describe("discord processDiscordMessage inbound contract", () => {
  it("passes a finalized MsgContext to dispatchInboundMessage", async () => {
    capturedCtx = undefined;

    const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-discord-"));
    const storePath = path.join(dir, "sessions.json");

    await processDiscordMessage({
      cfg: { messages: {}, session: { store: storePath } } as any,
      discordConfig: {} as any,
      accountId: "default",
      token: "token",
      runtime: { log: () => {}, error: () => {} } as any,
      guildHistories: new Map(),
      historyLimit: 0,
      mediaMaxBytes: 1024,
      textLimit: 4000,
      sender: { label: "user" },
      replyToMode: "off",
      ackReactionScope: "direct",
      groupPolicy: "open",
      data: { guild: null } as any,
      client: { rest: {} } as any,
      message: {
        id: "m1",
        channelId: "c1",
        timestamp: new Date().toISOString(),
        attachments: [],
      } as any,
      author: {
        id: "U1",
        username: "alice",
        discriminator: "0",
        globalName: "Alice",
      } as any,
      channelInfo: null,
      channelName: undefined,
      isGuildMessage: false,
      isDirectMessage: true,
      isGroupDm: false,
      commandAuthorized: true,
      baseText: "hi",
      messageText: "hi",
      wasMentioned: false,
      shouldRequireMention: false,
      canDetectMention: false,
      effectiveWasMentioned: false,
      threadChannel: null,
      threadParentId: undefined,
      threadParentName: undefined,
      threadParentType: undefined,
      threadName: undefined,
      displayChannelSlug: "",
      guildInfo: null,
      guildSlug: "",
      channelConfig: null,
      baseSessionKey: "agent:main:discord:dm:u1",
      route: {
        agentId: "main",
        channel: "discord",
        accountId: "default",
        sessionKey: "agent:main:discord:dm:u1",
        mainSessionKey: "agent:main:main",
      } as any,
    } as any);

    expect(capturedCtx).toBeTruthy();
    expectInboundContextContract(capturedCtx!);
  });
});