From 36e9a811cc94e235b8eb6a543e8aad090ee3db56 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 22:15:54 +0000 Subject: [PATCH] test: dedupe discord auto thread harness --- .../monitor/threading.auto-thread.test.ts | 145 ++++++------------ 1 file changed, 50 insertions(+), 95 deletions(-) diff --git a/src/discord/monitor/threading.auto-thread.test.ts b/src/discord/monitor/threading.auto-thread.test.ts index 2affabcae44..4759de9df28 100644 --- a/src/discord/monitor/threading.auto-thread.test.ts +++ b/src/discord/monitor/threading.auto-thread.test.ts @@ -2,72 +2,78 @@ import { ChannelType } from "@buape/carbon"; import { describe, it, expect, vi, beforeEach } from "vitest"; import { maybeCreateDiscordAutoThread } from "./threading.js"; +const postMock = vi.fn(); +const getMock = vi.fn(); +const mockClient = { + rest: { post: postMock, get: getMock }, +} as unknown as Parameters[0]["client"]; +const mockMessage = { + id: "msg1", + timestamp: "123", +} as unknown as Parameters[0]["message"]; + +async function runAutoThread( + overrides: Partial[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", () => { - const postMock = vi.fn(); - const getMock = vi.fn(); - const mockClient = { - rest: { post: postMock, get: getMock }, - } as unknown as Parameters[0]["client"]; - const mockMessage = { - id: "msg1", - timestamp: "123", - } as unknown as Parameters[0]["message"]; + beforeEach(() => { + postMock.mockReset(); + getMock.mockReset(); + }); it("skips auto-thread if channelType is GuildForum", async () => { - const result = await maybeCreateDiscordAutoThread({ - client: mockClient, - message: mockMessage, + const result = await runAutoThread({ messageChannelId: "forum1", - isGuildMessage: true, - channelConfig: { allowed: true, autoThread: true }, channelType: ChannelType.GuildForum, - baseText: "test", - combinedBody: "test", }); expect(result).toBeUndefined(); expect(postMock).not.toHaveBeenCalled(); }); it("skips auto-thread if channelType is GuildMedia", async () => { - const result = await maybeCreateDiscordAutoThread({ - client: mockClient, - message: mockMessage, + const result = await runAutoThread({ messageChannelId: "media1", - isGuildMessage: true, - channelConfig: { allowed: true, autoThread: true }, channelType: ChannelType.GuildMedia, - baseText: "test", - combinedBody: "test", }); expect(result).toBeUndefined(); expect(postMock).not.toHaveBeenCalled(); }); it("skips auto-thread if channelType is GuildVoice", async () => { - const result = await maybeCreateDiscordAutoThread({ - client: mockClient, - message: mockMessage, + const result = await runAutoThread({ messageChannelId: "voice1", - isGuildMessage: true, - channelConfig: { allowed: true, autoThread: true }, channelType: ChannelType.GuildVoice, - baseText: "test", - combinedBody: "test", }); expect(result).toBeUndefined(); expect(postMock).not.toHaveBeenCalled(); }); it("skips auto-thread if channelType is GuildStageVoice", async () => { - const result = await maybeCreateDiscordAutoThread({ - client: mockClient, - message: mockMessage, + const result = await runAutoThread({ messageChannelId: "stage1", - isGuildMessage: true, - channelConfig: { allowed: true, autoThread: true }, channelType: ChannelType.GuildStageVoice, - baseText: "test", - combinedBody: "test", }); expect(result).toBeUndefined(); expect(postMock).not.toHaveBeenCalled(); @@ -75,32 +81,13 @@ describe("maybeCreateDiscordAutoThread", () => { it("creates auto-thread if channelType is GuildText", async () => { postMock.mockResolvedValueOnce({ id: "thread1" }); - const result = await maybeCreateDiscordAutoThread({ - client: mockClient, - message: mockMessage, - messageChannelId: "text1", - isGuildMessage: true, - channelConfig: { allowed: true, autoThread: true }, - channelType: ChannelType.GuildText, - baseText: "test", - combinedBody: "test", - }); + const result = await runAutoThread(); expect(result).toBe("thread1"); expect(postMock).toHaveBeenCalled(); }); }); describe("maybeCreateDiscordAutoThread autoArchiveDuration", () => { - const postMock = vi.fn(); - const getMock = vi.fn(); - const mockClient = { - rest: { post: postMock, get: getMock }, - } as unknown as Parameters[0]["client"]; - const mockMessage = { - id: "msg1", - timestamp: "123", - } as unknown as Parameters[0]["message"]; - beforeEach(() => { postMock.mockReset(); getMock.mockReset(); @@ -108,55 +95,23 @@ describe("maybeCreateDiscordAutoThread autoArchiveDuration", () => { it("uses configured autoArchiveDuration", async () => { postMock.mockResolvedValueOnce({ id: "thread1" }); - await maybeCreateDiscordAutoThread({ - client: mockClient, - message: mockMessage, - messageChannelId: "text1", - isGuildMessage: true, + await runAutoThread({ channelConfig: { allowed: true, autoThread: true, autoArchiveDuration: "10080" }, - channelType: ChannelType.GuildText, - baseText: "test", - combinedBody: "test", }); - expect(postMock).toHaveBeenCalledWith( - expect.any(String), - expect.objectContaining({ body: expect.objectContaining({ auto_archive_duration: 10080 }) }), - ); + expectAutoArchiveDuration(10080); }); it("accepts numeric autoArchiveDuration", async () => { postMock.mockResolvedValueOnce({ id: "thread1" }); - await maybeCreateDiscordAutoThread({ - client: mockClient, - message: mockMessage, - messageChannelId: "text1", - isGuildMessage: true, + await runAutoThread({ channelConfig: { allowed: true, autoThread: true, autoArchiveDuration: 4320 }, - channelType: ChannelType.GuildText, - baseText: "test", - combinedBody: "test", }); - expect(postMock).toHaveBeenCalledWith( - expect.any(String), - expect.objectContaining({ body: expect.objectContaining({ auto_archive_duration: 4320 }) }), - ); + expectAutoArchiveDuration(4320); }); it("defaults to 60 when autoArchiveDuration not set", async () => { postMock.mockResolvedValueOnce({ id: "thread1" }); - await maybeCreateDiscordAutoThread({ - client: mockClient, - 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 }) }), - ); + await runAutoThread(); + expectAutoArchiveDuration(60); }); });