mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:38:38 +00:00
test: dedupe discord auto thread harness
This commit is contained in:
@@ -2,72 +2,78 @@ import { ChannelType } from "@buape/carbon";
|
|||||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||||
import { maybeCreateDiscordAutoThread } from "./threading.js";
|
import { maybeCreateDiscordAutoThread } from "./threading.js";
|
||||||
|
|
||||||
|
const postMock = vi.fn();
|
||||||
|
const getMock = vi.fn();
|
||||||
|
const mockClient = {
|
||||||
|
rest: { post: postMock, get: getMock },
|
||||||
|
} as unknown as Parameters<typeof maybeCreateDiscordAutoThread>[0]["client"];
|
||||||
|
const mockMessage = {
|
||||||
|
id: "msg1",
|
||||||
|
timestamp: "123",
|
||||||
|
} as unknown as Parameters<typeof maybeCreateDiscordAutoThread>[0]["message"];
|
||||||
|
|
||||||
|
async function runAutoThread(
|
||||||
|
overrides: Partial<Parameters<typeof maybeCreateDiscordAutoThread>[0]> = {},
|
||||||
|
) {
|
||||||
|
return maybeCreateDiscordAutoThread({
|
||||||
|
client: mockClient,
|
||||||
|
message: mockMessage,
|
||||||
|
messageChannelId: "text1",
|
||||||
|
isGuildMessage: true,
|
||||||
|
channelConfig: { allowed: true, autoThread: true },
|
||||||
|
channelType: ChannelType.GuildText,
|
||||||
|
baseText: "test",
|
||||||
|
combinedBody: "test",
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectAutoArchiveDuration(autoArchiveDuration: number) {
|
||||||
|
expect(postMock).toHaveBeenCalledWith(
|
||||||
|
expect.any(String),
|
||||||
|
expect.objectContaining({
|
||||||
|
body: expect.objectContaining({ auto_archive_duration: autoArchiveDuration }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
describe("maybeCreateDiscordAutoThread", () => {
|
describe("maybeCreateDiscordAutoThread", () => {
|
||||||
const postMock = vi.fn();
|
beforeEach(() => {
|
||||||
const getMock = vi.fn();
|
postMock.mockReset();
|
||||||
const mockClient = {
|
getMock.mockReset();
|
||||||
rest: { post: postMock, get: getMock },
|
});
|
||||||
} as unknown as Parameters<typeof maybeCreateDiscordAutoThread>[0]["client"];
|
|
||||||
const mockMessage = {
|
|
||||||
id: "msg1",
|
|
||||||
timestamp: "123",
|
|
||||||
} as unknown as Parameters<typeof maybeCreateDiscordAutoThread>[0]["message"];
|
|
||||||
|
|
||||||
it("skips auto-thread if channelType is GuildForum", async () => {
|
it("skips auto-thread if channelType is GuildForum", async () => {
|
||||||
const result = await maybeCreateDiscordAutoThread({
|
const result = await runAutoThread({
|
||||||
client: mockClient,
|
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "forum1",
|
messageChannelId: "forum1",
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true },
|
|
||||||
channelType: ChannelType.GuildForum,
|
channelType: ChannelType.GuildForum,
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
});
|
||||||
expect(result).toBeUndefined();
|
expect(result).toBeUndefined();
|
||||||
expect(postMock).not.toHaveBeenCalled();
|
expect(postMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips auto-thread if channelType is GuildMedia", async () => {
|
it("skips auto-thread if channelType is GuildMedia", async () => {
|
||||||
const result = await maybeCreateDiscordAutoThread({
|
const result = await runAutoThread({
|
||||||
client: mockClient,
|
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "media1",
|
messageChannelId: "media1",
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true },
|
|
||||||
channelType: ChannelType.GuildMedia,
|
channelType: ChannelType.GuildMedia,
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
});
|
||||||
expect(result).toBeUndefined();
|
expect(result).toBeUndefined();
|
||||||
expect(postMock).not.toHaveBeenCalled();
|
expect(postMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips auto-thread if channelType is GuildVoice", async () => {
|
it("skips auto-thread if channelType is GuildVoice", async () => {
|
||||||
const result = await maybeCreateDiscordAutoThread({
|
const result = await runAutoThread({
|
||||||
client: mockClient,
|
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "voice1",
|
messageChannelId: "voice1",
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true },
|
|
||||||
channelType: ChannelType.GuildVoice,
|
channelType: ChannelType.GuildVoice,
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
});
|
||||||
expect(result).toBeUndefined();
|
expect(result).toBeUndefined();
|
||||||
expect(postMock).not.toHaveBeenCalled();
|
expect(postMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips auto-thread if channelType is GuildStageVoice", async () => {
|
it("skips auto-thread if channelType is GuildStageVoice", async () => {
|
||||||
const result = await maybeCreateDiscordAutoThread({
|
const result = await runAutoThread({
|
||||||
client: mockClient,
|
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "stage1",
|
messageChannelId: "stage1",
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true },
|
|
||||||
channelType: ChannelType.GuildStageVoice,
|
channelType: ChannelType.GuildStageVoice,
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
});
|
||||||
expect(result).toBeUndefined();
|
expect(result).toBeUndefined();
|
||||||
expect(postMock).not.toHaveBeenCalled();
|
expect(postMock).not.toHaveBeenCalled();
|
||||||
@@ -75,32 +81,13 @@ describe("maybeCreateDiscordAutoThread", () => {
|
|||||||
|
|
||||||
it("creates auto-thread if channelType is GuildText", async () => {
|
it("creates auto-thread if channelType is GuildText", async () => {
|
||||||
postMock.mockResolvedValueOnce({ id: "thread1" });
|
postMock.mockResolvedValueOnce({ id: "thread1" });
|
||||||
const result = await maybeCreateDiscordAutoThread({
|
const result = await runAutoThread();
|
||||||
client: mockClient,
|
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "text1",
|
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true },
|
|
||||||
channelType: ChannelType.GuildText,
|
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
|
||||||
expect(result).toBe("thread1");
|
expect(result).toBe("thread1");
|
||||||
expect(postMock).toHaveBeenCalled();
|
expect(postMock).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("maybeCreateDiscordAutoThread autoArchiveDuration", () => {
|
describe("maybeCreateDiscordAutoThread autoArchiveDuration", () => {
|
||||||
const postMock = vi.fn();
|
|
||||||
const getMock = vi.fn();
|
|
||||||
const mockClient = {
|
|
||||||
rest: { post: postMock, get: getMock },
|
|
||||||
} as unknown as Parameters<typeof maybeCreateDiscordAutoThread>[0]["client"];
|
|
||||||
const mockMessage = {
|
|
||||||
id: "msg1",
|
|
||||||
timestamp: "123",
|
|
||||||
} as unknown as Parameters<typeof maybeCreateDiscordAutoThread>[0]["message"];
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
postMock.mockReset();
|
postMock.mockReset();
|
||||||
getMock.mockReset();
|
getMock.mockReset();
|
||||||
@@ -108,55 +95,23 @@ describe("maybeCreateDiscordAutoThread autoArchiveDuration", () => {
|
|||||||
|
|
||||||
it("uses configured autoArchiveDuration", async () => {
|
it("uses configured autoArchiveDuration", async () => {
|
||||||
postMock.mockResolvedValueOnce({ id: "thread1" });
|
postMock.mockResolvedValueOnce({ id: "thread1" });
|
||||||
await maybeCreateDiscordAutoThread({
|
await runAutoThread({
|
||||||
client: mockClient,
|
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "text1",
|
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true, autoArchiveDuration: "10080" },
|
channelConfig: { allowed: true, autoThread: true, autoArchiveDuration: "10080" },
|
||||||
channelType: ChannelType.GuildText,
|
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
});
|
||||||
expect(postMock).toHaveBeenCalledWith(
|
expectAutoArchiveDuration(10080);
|
||||||
expect.any(String),
|
|
||||||
expect.objectContaining({ body: expect.objectContaining({ auto_archive_duration: 10080 }) }),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("accepts numeric autoArchiveDuration", async () => {
|
it("accepts numeric autoArchiveDuration", async () => {
|
||||||
postMock.mockResolvedValueOnce({ id: "thread1" });
|
postMock.mockResolvedValueOnce({ id: "thread1" });
|
||||||
await maybeCreateDiscordAutoThread({
|
await runAutoThread({
|
||||||
client: mockClient,
|
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "text1",
|
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true, autoArchiveDuration: 4320 },
|
channelConfig: { allowed: true, autoThread: true, autoArchiveDuration: 4320 },
|
||||||
channelType: ChannelType.GuildText,
|
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
});
|
||||||
expect(postMock).toHaveBeenCalledWith(
|
expectAutoArchiveDuration(4320);
|
||||||
expect.any(String),
|
|
||||||
expect.objectContaining({ body: expect.objectContaining({ auto_archive_duration: 4320 }) }),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("defaults to 60 when autoArchiveDuration not set", async () => {
|
it("defaults to 60 when autoArchiveDuration not set", async () => {
|
||||||
postMock.mockResolvedValueOnce({ id: "thread1" });
|
postMock.mockResolvedValueOnce({ id: "thread1" });
|
||||||
await maybeCreateDiscordAutoThread({
|
await runAutoThread();
|
||||||
client: mockClient,
|
expectAutoArchiveDuration(60);
|
||||||
message: mockMessage,
|
|
||||||
messageChannelId: "text1",
|
|
||||||
isGuildMessage: true,
|
|
||||||
channelConfig: { allowed: true, autoThread: true },
|
|
||||||
channelType: ChannelType.GuildText,
|
|
||||||
baseText: "test",
|
|
||||||
combinedBody: "test",
|
|
||||||
});
|
|
||||||
expect(postMock).toHaveBeenCalledWith(
|
|
||||||
expect.any(String),
|
|
||||||
expect.objectContaining({ body: expect.objectContaining({ auto_archive_duration: 60 }) }),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user