File size: 5,801 Bytes
eb3f11e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Register thread tests cover message thread command registration and option wiring.
import { Command } from "commander";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { setActivePluginRegistry } from "../../../plugins/runtime.js";
import {
  createChannelTestPluginBase,
  createTestRegistry,
} from "../../../test-utils/channel-plugins.js";
import type { MessageCliHelpers } from "./helpers.js";
import { registerMessageThreadCommands } from "./register.thread.js";

function createHelpers(runMessageAction: MessageCliHelpers["runMessageAction"]): MessageCliHelpers {
  return {
    withMessageBase: (command) => command.option("--channel <channel>", "Channel"),
    withMessageTarget: (command) => command.option("-t, --target <dest>", "Target"),
    withRequiredMessageTarget: (command) => command.requiredOption("-t, --target <dest>", "Target"),
    runMessageAction,
  };
}

function firstMessageActionCall(runMessageAction: { mock: { calls: unknown[][] } }) {
  return runMessageAction.mock.calls[0] as [string, Record<string, unknown>] | undefined;
}

describe("registerMessageThreadCommands", () => {
  const runMessageAction = vi.fn(
    async (_action: string, _opts: Record<string, unknown>) => undefined,
  );

  beforeEach(() => {
    setActivePluginRegistry(
      createTestRegistry([
        {
          pluginId: "topic-chat",
          source: "test",
          plugin: {
            ...createChannelTestPluginBase({ id: "topic-chat", label: "Topic chat" }),
            actions: {
              resolveCliActionRequest: ({
                action,
                args,
              }: {
                action: string;
                args: Record<string, unknown>;
              }) => {
                if (action !== "thread-create") {
                  return null;
                }
                const { threadName, ...rest } = args;
                return {
                  action: "topic-create",
                  args: {
                    ...rest,
                    name: threadName,
                  },
                };
              },
            },
          },
        },
        {
          pluginId: "plain-chat",
          source: "test",
          plugin: createChannelTestPluginBase({ id: "plain-chat", label: "Plain chat" }),
        },
      ]),
    );
    runMessageAction.mockClear();
  });

  it("routes plugin-remapped thread create actions through channel hooks", async () => {
    const message = new Command().exitOverride();
    registerMessageThreadCommands(message, createHelpers(runMessageAction));

    await message.parseAsync(
      [
        "thread",
        "create",
        "--channel",
        " topic-chat ",
        "-t",
        "room-1",
        "--thread-name",
        "Build Updates",
        "-m",
        "hello",
      ],
      { from: "user" },
    );

    const remappedCall = firstMessageActionCall(runMessageAction);
    expect(remappedCall?.[0]).toBe("topic-create");
    expect(remappedCall?.[1]?.channel).toBe(" topic-chat ");
    expect(remappedCall?.[1]?.target).toBe("room-1");
    expect(remappedCall?.[1]?.name).toBe("Build Updates");
    expect(remappedCall?.[1]?.message).toBe("hello");
    expect(remappedCall?.[1]).not.toHaveProperty("threadName");
  });

  it.each([
    {
      description: "infers the action owner from a registered channel-prefixed target",
      channel: undefined,
      target: "topic-chat:room-1",
      action: "topic-create",
    },
    {
      description: "prefers an explicit channel over a conflicting target prefix",
      channel: "plain-chat",
      target: "topic-chat:room-1",
      action: "thread-create",
    },
    {
      description: "keeps prefixed channels without an action remap unchanged",
      channel: undefined,
      target: "plain-chat:room-1",
      action: "thread-create",
    },
    {
      description: "keeps an explicit action owner over a conflicting target prefix",
      channel: "topic-chat",
      target: "plain-chat:room-1",
      action: "topic-create",
    },
  ])("$description", async ({ action, channel, target }) => {
    const message = new Command().exitOverride();
    registerMessageThreadCommands(message, createHelpers(runMessageAction));

    await message.parseAsync(
      [
        "thread",
        "create",
        ...(channel ? ["--channel", channel] : []),
        "--target",
        target,
        "--thread-name",
        "Build Updates",
      ],
      { from: "user" },
    );

    const call = firstMessageActionCall(runMessageAction);
    expect(call?.[0]).toBe(action);
    expect(call?.[1]?.channel).toBe(channel);
    expect(call?.[1]?.target).toBe(target);
    expect(call?.[1]?.[action === "topic-create" ? "name" : "threadName"]).toBe("Build Updates");
    expect(call?.[1]).not.toHaveProperty(action === "topic-create" ? "threadName" : "name");
  });

  it("keeps default thread create params when the channel does not remap the action", async () => {
    const message = new Command().exitOverride();
    registerMessageThreadCommands(message, createHelpers(runMessageAction));

    await message.parseAsync(
      [
        "thread",
        "create",
        "--channel",
        "plain-chat",
        "-t",
        "channel:123",
        "--thread-name",
        "Build Updates",
        "-m",
        "hello",
      ],
      { from: "user" },
    );

    const defaultCall = firstMessageActionCall(runMessageAction);
    expect(defaultCall?.[0]).toBe("thread-create");
    expect(defaultCall?.[1]?.channel).toBe("plain-chat");
    expect(defaultCall?.[1]?.target).toBe("channel:123");
    expect(defaultCall?.[1]?.threadName).toBe("Build Updates");
    expect(defaultCall?.[1]?.message).toBe("hello");
    expect(defaultCall?.[1]).not.toHaveProperty("name");
  });
});