File size: 3,893 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { beforeEach, describe, expect, it, vi } from "vitest";

const hoisted = vi.hoisted(() => {
  const resolveByConversationMock = vi.fn();
  const touchMock = vi.fn();
  return {
    resolveByConversationMock,
    touchMock,
  };
});

vi.mock("../infra/outbound/session-binding-service.js", async (importOriginal) => {
  const actual =
    await importOriginal<typeof import("../infra/outbound/session-binding-service.js")>();
  return {
    ...actual,
    getSessionBindingService: () => ({
      bind: vi.fn(),
      getCapabilities: vi.fn(),
      listBySession: vi.fn(),
      resolveByConversation: (ref: unknown) => hoisted.resolveByConversationMock(ref),
      touch: (bindingId: string, at?: number) => hoisted.touchMock(bindingId, at),
      unbind: vi.fn(),
    }),
  };
});

const { buildTelegramMessageContextForTest } =
  await import("./bot-message-context.test-harness.js");

describe("buildTelegramMessageContext bound conversation override", () => {
  beforeEach(() => {
    hoisted.resolveByConversationMock.mockReset().mockReturnValue(null);
    hoisted.touchMock.mockReset();
  });

  it("routes forum topic messages to the bound session", async () => {
    hoisted.resolveByConversationMock.mockReturnValue({
      bindingId: "default:-100200300:topic:77",
      targetSessionKey: "agent:codex-acp:session-1",
    });

    const ctx = await buildTelegramMessageContextForTest({
      message: {
        message_id: 1,
        chat: { id: -100200300, type: "supergroup", is_forum: true },
        message_thread_id: 77,
        date: 1_700_000_000,
        text: "hello",
        from: { id: 42, first_name: "Alice" },
      },
      options: { forceWasMentioned: true },
      resolveGroupActivation: () => true,
    });

    expect(hoisted.resolveByConversationMock).toHaveBeenCalledWith({
      channel: "telegram",
      accountId: "default",
      conversationId: "-100200300:topic:77",
    });
    expect(ctx?.ctxPayload?.SessionKey).toBe("agent:codex-acp:session-1");
    expect(hoisted.touchMock).toHaveBeenCalledWith("default:-100200300:topic:77", undefined);
  });

  it("treats named-account bound conversations as explicit route matches", async () => {
    hoisted.resolveByConversationMock.mockReturnValue({
      bindingId: "work:-100200300:topic:77",
      targetSessionKey: "agent:codex-acp:session-2",
    });

    const ctx = await buildTelegramMessageContextForTest({
      accountId: "work",
      message: {
        message_id: 1,
        chat: { id: -100200300, type: "supergroup", is_forum: true },
        message_thread_id: 77,
        date: 1_700_000_000,
        text: "hello",
        from: { id: 42, first_name: "Alice" },
      },
      options: { forceWasMentioned: true },
      resolveGroupActivation: () => true,
    });

    expect(ctx).not.toBeNull();
    expect(ctx?.route.accountId).toBe("work");
    expect(ctx?.route.matchedBy).toBe("binding.channel");
    expect(ctx?.ctxPayload?.SessionKey).toBe("agent:codex-acp:session-2");
    expect(hoisted.touchMock).toHaveBeenCalledWith("work:-100200300:topic:77", undefined);
  });

  it("routes dm messages to the bound session", async () => {
    hoisted.resolveByConversationMock.mockReturnValue({
      bindingId: "default:1234",
      targetSessionKey: "agent:codex-acp:session-dm",
    });

    const ctx = await buildTelegramMessageContextForTest({
      message: {
        message_id: 1,
        chat: { id: 1234, type: "private" },
        date: 1_700_000_000,
        text: "hello",
        from: { id: 42, first_name: "Alice" },
      },
    });

    expect(hoisted.resolveByConversationMock).toHaveBeenCalledWith({
      channel: "telegram",
      accountId: "default",
      conversationId: "1234",
    });
    expect(ctx?.ctxPayload?.SessionKey).toBe("agent:codex-acp:session-dm");
    expect(hoisted.touchMock).toHaveBeenCalledWith("default:1234", undefined);
  });
});