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
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ vi.mock("@chatbotx.io/business", () => ({
platformCredentialService: { resolveForOwner: mockResolveForOwner },
workspaceService: { create: vi.fn() },
resolveTenantSettings: vi.fn(),
updateInstagramIntegrationUserInfo: vi.fn(),
updateMessengerIntegrationUserInfo: vi.fn(),
messengerIntegrationService: { updateUserInfo: vi.fn() },
instagramIntegrationService: { updateUserInfo: vi.fn() },
tagSyncService: { enqueueChannelScan: vi.fn() },
userQuotaService: { getAccessState: vi.fn(async () => ({ blocked: false })) },
connectChannelIntegration: vi.fn(),
connectMessengerPage: vi.fn(),
connectInstagramAccount: vi.fn(),
}))

vi.mock("@chatbotx.io/business/errors", () => ({
Expand Down
160 changes: 58 additions & 102 deletions apps/builder/__tests__/disconnect-meta-actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,40 @@

import { beforeEach, describe, expect, test, vi } from "vitest"

const mocks = vi.hoisted(() => {
const txChain = {
set: vi.fn(),
where: vi.fn(),
}
txChain.set.mockReturnValue(txChain)
txChain.where.mockResolvedValue(undefined)

const tx = {
update: vi.fn(() => txChain),
delete: vi.fn(() => txChain),
}

return {
auditRecord: vi.fn().mockResolvedValue(undefined),
dbTransaction: vi.fn(async (callback: (tx: unknown) => Promise<void>) =>
callback(tx),
),
coexistTearDownForIntegration: vi.fn().mockResolvedValue(undefined),
findOrFail: vi.fn(),
inboxDisconnect: vi.fn().mockResolvedValue(undefined),
instagramExists: vi.fn().mockResolvedValue(false),
metaCapiDeleteByIntegration: vi.fn().mockResolvedValue(undefined),
loggerWarn: vi.fn(),
messengerDisconnect: vi.fn().mockResolvedValue(undefined),
messengerExists: vi.fn().mockResolvedValue(false),
instagramDisconnect: vi.fn().mockResolvedValue(undefined),
instagramFacebookDisconnect: vi.fn().mockResolvedValue(undefined),
subscribePageToAppWebhook: vi.fn().mockResolvedValue(undefined),
tx,
txChain,
workspaceFindById: vi.fn(),
}
})
const mocks = vi.hoisted(() => ({
auditRecord: vi.fn().mockResolvedValue(undefined),
deleteMessengerIntegrationWithCleanup: vi.fn().mockResolvedValue(undefined),
deleteInstagramIntegrationWithCleanup: vi.fn().mockResolvedValue(undefined),
instagramExists: vi.fn().mockResolvedValue(false),
loggerWarn: vi.fn(),
messengerDisconnect: vi.fn().mockResolvedValue(undefined),
messengerExists: vi.fn().mockResolvedValue(false),
messengerFindByIdForWorkspace: vi.fn(),
instagramFindByIdForWorkspace: vi.fn(),
instagramDisconnect: vi.fn().mockResolvedValue(undefined),
instagramFacebookDisconnect: vi.fn().mockResolvedValue(undefined),
subscribePageToAppWebhook: vi.fn().mockResolvedValue(undefined),
workspaceFindById: vi.fn(),
}))

vi.mock("@chatbotx.io/business/audit", () => ({
auditService: { record: mocks.auditRecord },
}))

vi.mock("@chatbotx.io/business", () => ({
coexistService: {
tearDownForIntegration: mocks.coexistTearDownForIntegration,
},
inboxService: { disconnect: mocks.inboxDisconnect },
instagramIntegrationService: { existsForPage: mocks.instagramExists },
messengerIntegrationService: { existsForPage: mocks.messengerExists },
workspaceService: { findById: mocks.workspaceFindById },
}))

vi.mock("@chatbotx.io/database/client", () => ({
db: { transaction: mocks.dbTransaction },
and: vi.fn((...conditions: unknown[]) => ({ conditions })),
eq: vi.fn((field: unknown, value: unknown) => ({ field, value })),
findOrFail: mocks.findOrFail,
inArray: vi.fn((field: unknown, values: unknown[]) => ({ field, values })),
}))

vi.mock("@chatbotx.io/database/repositories", () => ({
metaCapiEventRepository: {
deleteByIntegration: mocks.metaCapiDeleteByIntegration,
deleteMessengerIntegrationWithCleanup:
mocks.deleteMessengerIntegrationWithCleanup,
deleteInstagramIntegrationWithCleanup:
mocks.deleteInstagramIntegrationWithCleanup,
instagramIntegrationService: {
existsForPage: mocks.instagramExists,
findByIdForWorkspace: mocks.instagramFindByIdForWorkspace,
},
}))

vi.mock("@chatbotx.io/database/partials", () => ({
channelTypes: { enum: { messenger: "messenger" } },
}))

vi.mock("@chatbotx.io/database/schema", () => ({
coexistSyncRunModel: {
finishedAt: "finishedAt",
integrationId: "integrationId",
status: "status",
},
integrationInstagramModel: { id: "instagramId" },
integrationMessengerModel: { id: "messengerId" },
tagChannelModel: {
channelType: "channelType",
integrationId: "tagIntegrationId",
messengerIntegrationService: {
existsForPage: mocks.messengerExists,
findByIdForWorkspace: mocks.messengerFindByIdForWorkspace,
},
workspaceService: { findById: mocks.workspaceFindById },
}))

vi.mock("@chatbotx.io/integration-messenger", () => ({
Expand Down Expand Up @@ -157,23 +112,22 @@ const instagramFacebookRow = {
describe("Meta disconnect actions", () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.dbTransaction.mockImplementation(
async (callback: (tx: unknown) => Promise<void>) => callback(mocks.tx),
)
mocks.instagramExists.mockResolvedValue(false)
mocks.messengerExists.mockResolvedValue(false)
mocks.messengerDisconnect.mockResolvedValue(undefined)
mocks.instagramDisconnect.mockResolvedValue(undefined)
mocks.instagramFacebookDisconnect.mockResolvedValue(undefined)
mocks.subscribePageToAppWebhook.mockResolvedValue(undefined)
mocks.deleteMessengerIntegrationWithCleanup.mockResolvedValue(undefined)
mocks.deleteInstagramIntegrationWithCleanup.mockResolvedValue(undefined)
mocks.workspaceFindById.mockResolvedValue({
id: "workspace-1",
ownerId: "owner-1",
})
})

test("messenger disconnect preserves a shared Instagram page subscription", async () => {
mocks.findOrFail.mockResolvedValueOnce(messengerRow)
mocks.messengerFindByIdForWorkspace.mockResolvedValueOnce(messengerRow)
mocks.instagramExists.mockResolvedValueOnce(true)

await disconnectMessenger({ workspaceId: "workspace-1", id: "messenger-1" })
Expand All @@ -185,33 +139,33 @@ describe("Meta disconnect actions", () => {
version: "v99.0",
subscribedFields: "general_info",
})
expect(mocks.inboxDisconnect).toHaveBeenCalledWith({
expect(mocks.deleteMessengerIntegrationWithCleanup).toHaveBeenCalledWith({
workspaceId: "workspace-1",
id: "messenger-1",
inboxId: "inbox-1",
ownerId: "owner-1",
workspaceId: "workspace-1",
tx: mocks.tx,
})
})

test("messenger disconnect calls the integration when no sibling exists", async () => {
mocks.findOrFail.mockResolvedValueOnce(messengerRow)
mocks.messengerFindByIdForWorkspace.mockResolvedValueOnce(messengerRow)

await disconnectMessenger({ workspaceId: "workspace-1", id: "messenger-1" })

expect(mocks.messengerDisconnect).toHaveBeenCalledWith(messengerRow.auth)
expect(mocks.subscribePageToAppWebhook).not.toHaveBeenCalled()
expect(mocks.metaCapiDeleteByIntegration).toHaveBeenCalledWith(
{
workspaceId: "workspace-1",
channel: "messenger",
integrationId: "messenger-1",
},
mocks.tx,
)
expect(mocks.deleteMessengerIntegrationWithCleanup).toHaveBeenCalledWith({
workspaceId: "workspace-1",
id: "messenger-1",
inboxId: "inbox-1",
ownerId: "owner-1",
})
})

test("facebook-backed Instagram disconnect skips app unsubscribe when Messenger sibling exists", async () => {
mocks.findOrFail.mockResolvedValueOnce(instagramFacebookRow)
mocks.instagramFindByIdForWorkspace.mockResolvedValueOnce(
instagramFacebookRow,
)
mocks.messengerExists.mockResolvedValueOnce(true)

await disconnectInstagram({
Expand All @@ -220,16 +174,19 @@ describe("Meta disconnect actions", () => {
})

expect(mocks.instagramFacebookDisconnect).not.toHaveBeenCalled()
expect(mocks.inboxDisconnect).toHaveBeenCalledWith({
expect(mocks.deleteInstagramIntegrationWithCleanup).toHaveBeenCalledWith({
workspaceId: "workspace-1",
id: "instagram-1",
inboxId: "inbox-2",
ownerId: "owner-1",
workspaceId: "workspace-1",
tx: mocks.tx,
isFacebook: true,
})
})

test("facebook-backed Instagram disconnect calls app unsubscribe when no Messenger sibling exists", async () => {
mocks.findOrFail.mockResolvedValueOnce(instagramFacebookRow)
mocks.instagramFindByIdForWorkspace.mockResolvedValueOnce(
instagramFacebookRow,
)

await disconnectInstagram({
workspaceId: "workspace-1",
Expand All @@ -239,23 +196,22 @@ describe("Meta disconnect actions", () => {
expect(mocks.instagramFacebookDisconnect).toHaveBeenCalledWith(
instagramFacebookRow.auth,
)
expect(mocks.metaCapiDeleteByIntegration).toHaveBeenCalledWith(
{
workspaceId: "workspace-1",
channel: "instagram",
integrationId: "instagram-1",
},
mocks.tx,
)
expect(mocks.deleteInstagramIntegrationWithCleanup).toHaveBeenCalledWith({
workspaceId: "workspace-1",
id: "instagram-1",
inboxId: "inbox-2",
ownerId: "owner-1",
isFacebook: true,
})
expect(mocks.auditRecord).toHaveBeenCalledWith({
workspaceId: "workspace-1",
action: "disconnect",
detail: "disconnected the Instagram channel (#instagram-1)",
})
})

test("messenger disconnect records a disconnect audit event after the transaction resolves", async () => {
mocks.findOrFail.mockResolvedValueOnce(messengerRow)
test("messenger disconnect records a disconnect audit event after cleanup resolves", async () => {
mocks.messengerFindByIdForWorkspace.mockResolvedValueOnce(messengerRow)

await disconnectMessenger({ workspaceId: "workspace-1", id: "messenger-1" })

Expand Down
Loading
Loading