Files
simstudioai--sim/apps/sim/lib/webhooks/providers/tiktok-targets.test.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

120 lines
3.6 KiB
TypeScript

/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockCredentialExpression, mockEq, mockSelect, queryRows } = vi.hoisted(() => ({
mockCredentialExpression: vi.fn(() => 'webhook.credentialId'),
mockEq: vi.fn((left: unknown, right: unknown) => ({ left, right })),
mockSelect: vi.fn(),
queryRows: {
rows: [] as Array<{
accountId: string
webhook: Record<string, unknown>
workflow: Record<string, unknown>
}>,
},
}))
vi.mock('@sim/db', () => {
const chain = {
from: vi.fn(() => chain),
innerJoin: vi.fn(() => chain),
leftJoin: vi.fn(() => chain),
where: vi.fn(() => Promise.resolve(queryRows.rows)),
}
mockSelect.mockImplementation(() => chain)
return {
account: {
id: 'account.id',
accountId: 'account.accountId',
providerId: 'account.providerId',
},
credential: {
id: 'credential.id',
accountId: 'credential.accountId',
providerId: 'credential.providerId',
type: 'credential.type',
workspaceId: 'credential.workspaceId',
},
db: { select: mockSelect },
webhookCredentialIdExpression: mockCredentialExpression,
webhook: {
deploymentVersionId: 'webhook.deploymentVersionId',
isActive: 'webhook.isActive',
archivedAt: 'webhook.archivedAt',
provider: 'webhook.provider',
providerConfig: 'webhook.providerConfig',
workflowId: 'webhook.workflowId',
},
workflow: {
id: 'workflow.id',
workspaceId: 'workflow.workspaceId',
archivedAt: 'workflow.archivedAt',
},
workflowDeploymentVersion: {
id: 'workflowDeploymentVersion.id',
workflowId: 'workflowDeploymentVersion.workflowId',
isActive: 'workflowDeploymentVersion.isActive',
},
}
})
vi.mock('drizzle-orm', () => ({
and: vi.fn((...conditions: unknown[]) => conditions),
eq: mockEq,
isNull: vi.fn((value: unknown) => ({ isNull: value })),
like: vi.fn((left: unknown, right: unknown) => ({ left, right })),
or: vi.fn((...conditions: unknown[]) => conditions),
}))
import { findTikTokWebhookTargets } from '@/lib/webhooks/providers/tiktok-targets'
describe('findTikTokWebhookTargets', () => {
beforeEach(() => {
vi.clearAllMocks()
queryRows.rows = []
})
it('returns only rows whose stored account ID exactly matches user_openid', async () => {
queryRows.rows = [
{
accountId: 'act.user-11111111-2222-3333-4444-555555555555',
webhook: { id: 'webhook-1' },
workflow: { id: 'workflow-1' },
},
{
accountId: 'act.user-other-11111111-2222-3333-4444-555555555555',
webhook: { id: 'webhook-2' },
workflow: { id: 'workflow-2' },
},
]
const targets = await findTikTokWebhookTargets('act.user', 'request-1')
expect(targets).toEqual([
{
webhook: { id: 'webhook-1' },
workflow: { id: 'workflow-1' },
},
])
})
it('enforces provider and workspace bindings in the database query', async () => {
await findTikTokWebhookTargets('act.user', 'request-2')
expect(mockEq).toHaveBeenCalledWith('credential.providerId', 'tiktok')
expect(mockEq).toHaveBeenCalledWith('webhook.provider', 'tiktok')
expect(mockEq).toHaveBeenCalledWith('workflow.workspaceId', 'credential.workspaceId')
expect(mockCredentialExpression).toHaveBeenCalledWith('webhook.providerConfig')
expect(mockEq).toHaveBeenCalledWith('webhook.credentialId', 'credential.id')
})
it('does not query for an empty user_openid', async () => {
expect(await findTikTokWebhookTargets('', 'request-3')).toEqual([])
expect(mockSelect).not.toHaveBeenCalled()
})
})