Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions apps/builder/__tests__/channel-reconnect-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ const {
mockFindMessengerIntegration,
mockFindInstagramIntegration,
mockFindZaloIntegration,
mockFindThreadsIntegration,
mockResolveForOwner,
mockRedirect,
mockGenerateMessengerAuthUrl,
mockGenerateInstagramAuthUrl,
mockGenerateInstagramFacebookAuthUrl,
mockGenerateZaloAuthUrl,
mockGenerateThreadsAuthUrl,
} = vi.hoisted(() => ({
mockFindMessengerIntegration: vi.fn(),
mockFindInstagramIntegration: vi.fn(),
mockFindZaloIntegration: vi.fn(),
mockFindThreadsIntegration: vi.fn(),
mockResolveForOwner: vi.fn(),
mockRedirect: vi.fn(),
mockGenerateMessengerAuthUrl: vi.fn(() => "https://facebook.example/auth"),
Expand All @@ -43,6 +46,7 @@ const {
() => "https://facebook.example/instagram-auth",
),
mockGenerateZaloAuthUrl: vi.fn(() => "https://zalo.example/auth"),
mockGenerateThreadsAuthUrl: vi.fn(() => "https://threads.example/auth"),
}))

vi.mock("@/lib/safe-action", () => ({
Expand All @@ -59,6 +63,9 @@ vi.mock("@chatbotx.io/business", () => ({
zaloIntegrationService: {
findById: mockFindZaloIntegration,
},
integrationThreadsService: {
findByIdForWorkspace: mockFindThreadsIntegration,
},
platformCredentialService: {
resolveForOwner: mockResolveForOwner,
},
Expand Down Expand Up @@ -96,10 +103,18 @@ vi.mock("@chatbotx.io/integration-zalo", () => ({
generateAuthUrl: mockGenerateZaloAuthUrl,
}))

vi.mock("@chatbotx.io/integration-threads", () => ({
generateAuthUrl: mockGenerateThreadsAuthUrl,
}))

vi.mock("next/navigation", () => ({
redirect: mockRedirect,
}))

vi.mock("next-intl/server", () => ({
getTranslations: vi.fn(async () => (key: string) => key),
}))

vi.mock("@/lib/domain", () => ({
getOriginUrlFromHeader: vi.fn(async () => "https://app.example.com"),
}))
Expand All @@ -108,6 +123,7 @@ const BROKER_ORIGIN = "https://broker.example.com"

vi.mock("@/lib/oauth-broker", () => ({
getBrokerOrigin: () => BROKER_ORIGIN,
buildBrokerCallbackUrl: (path: string) => `${BROKER_ORIGIN}${path}`,
}))

const { mockFindActiveByTenantId, mockFindByOwner } = vi.hoisted(() => ({
Expand All @@ -120,11 +136,13 @@ vi.mock("@/env", () => ({ isCloud: () => true }))
await import("../src/features/integration-messenger/actions/reconnect.action")
await import("../src/features/integration-instagram/actions/reconnect.action")
await import("../src/features/integration-zalo/actions/reconnect.action")
await import("../src/features/integration-threads/actions/reconnect.action")

const [
reconnectMessengerHandler,
reconnectInstagramHandler,
reconnectZaloHandler,
reconnectThreadsHandler,
] = capturedActionHandlers

const executeMessengerReconnect = () =>
Expand All @@ -145,6 +163,12 @@ const executeZaloReconnect = () =>
ctx: { workspace: { id: "ws-1", ownerId: "owner-1" } },
})

const executeThreadsReconnect = () =>
reconnectThreadsHandler({
bindArgsParsedInputs: ["ws-1", "th-1"],
ctx: { workspace: { id: "ws-1", ownerId: "owner-1" } },
})

describe("reconnectMessengerAction", () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down Expand Up @@ -392,3 +416,59 @@ describe("reconnectZaloAction", () => {
)
})
})

describe("reconnectThreadsAction", () => {
beforeEach(() => {
vi.clearAllMocks()
mockResolveForOwner.mockResolvedValue({
config: {
clientId: "client-1",
clientSecret: "secret-1",
version: "v23.0",
},
})
})

test("redirects to the Threads dialog with reconnect state", async () => {
mockFindThreadsIntegration.mockResolvedValue({
id: "th-1",
threadsUserId: "threads-user-1",
})

await executeThreadsReconnect()

expect(mockGenerateThreadsAuthUrl).toHaveBeenCalledWith({
clientId: "client-1",
redirectUrl: "https://broker.example.com/integrations/threads/callback",
stateParams: {
workspaceId: "ws-1",
referer:
"https://app.example.com/space/ws-1/settings/channels?channel=threads",
reconnectIntegrationId: "th-1",
},
})
expect(mockRedirect).toHaveBeenCalledWith("https://threads.example/auth")
})

test("throws a translated not-found error when the integration is missing", async () => {
mockFindThreadsIntegration.mockResolvedValue(undefined)

await expect(executeThreadsReconnect()).rejects.toThrow(
"channels.reconnect.errors.notFound",
)
expect(mockRedirect).not.toHaveBeenCalled()
})

test("throws a translated app-settings error when the credential is missing", async () => {
mockFindThreadsIntegration.mockResolvedValue({
id: "th-1",
threadsUserId: "threads-user-1",
})
mockResolveForOwner.mockResolvedValue(null)

await expect(executeThreadsReconnect()).rejects.toThrow(
"messages.needToAddSettings",
)
expect(mockRedirect).not.toHaveBeenCalled()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ describe("channels/create — platform owner fan-out", () => {
"instagram",
"instagramFacebook",
"messenger",
"threads",
"tiktok",
"whatsapp",
"zalo",
Expand Down
49 changes: 49 additions & 0 deletions apps/builder/__tests__/create-message-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const contactInbox = {
id: "ci-1",
inboxId: "inbox-1",
contactId: "contact-1",
channel: "messenger",
}

describe("createMessage", () => {
Expand Down Expand Up @@ -198,4 +199,52 @@ describe("createMessage", () => {
},
})
})

test("uses attempts=1 for manual Threads comment replies", async () => {
await createMessage({
conversation: conversation as never,
contactInbox: { ...contactInbox, channel: "threads" } as never,
parsedInput: {
text: "hello",
replyToMessageId: "parent-1",
replyToMessageCreatedAt: new Date("2026-08-12T00:00:00Z"),
},
user: { id: "user-1" } as never,
})

expect(mockChatQueueAdd).toHaveBeenNthCalledWith(
2,
"sendChannelMessage",
expect.objectContaining({
data: expect.objectContaining({
message: expect.objectContaining({ type: "comment" }),
}),
}),
{ attempts: 1 },
)
})

test("keeps default queue options for non-Threads manual comment replies", async () => {
await createMessage({
conversation: conversation as never,
contactInbox: { ...contactInbox, channel: "messenger" } as never,
parsedInput: {
text: "hello",
replyToMessageId: "parent-1",
replyToMessageCreatedAt: new Date("2026-08-12T00:00:00Z"),
},
user: { id: "user-1" } as never,
})

expect(mockChatQueueAdd).toHaveBeenNthCalledWith(
2,
"sendChannelMessage",
expect.objectContaining({
data: expect.objectContaining({
message: expect.objectContaining({ type: "comment" }),
}),
}),
)
expect(mockChatQueueAdd.mock.calls[1]).toHaveLength(2)
})
})
Loading