Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

168 lines
5.7 KiB
TypeScript

/**
* @vitest-environment node
*/
import { account } from '@sim/db/schema'
import { dbChainMockFns, queueTableRows, resetDbChainMock } from '@sim/testing'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
const { sqlCalls } = vi.hoisted(() => ({
sqlCalls: [] as Array<{ strings: readonly string[]; values: unknown[] }>,
}))
vi.mock('drizzle-orm', () => {
const sql = (strings: readonly string[], ...values: unknown[]) => {
const node = { strings, values }
sqlCalls.push(node)
return node
}
// Identity, so an interpolated value still shows up verbatim in `values`.
sql.param = (value: unknown) => value
sql.join = (fragments: unknown[], separator: unknown) => ({ fragments, separator })
return {
sql,
and: vi.fn(),
eq: vi.fn((field: unknown, value: unknown) => ({ field, value })),
isNull: vi.fn(),
ne: vi.fn(),
or: vi.fn(),
}
})
vi.mock('@/app/api/auth/oauth/utils', () => ({
getOAuthToken: vi.fn(),
refreshAccessTokenIfNeeded: vi.fn(),
resolveOAuthAccountId: vi.fn(),
}))
vi.mock('@/triggers/constants', () => ({ MAX_CONSECUTIVE_FAILURES: 5 }))
import type { WebhookRecord } from '@/lib/webhooks/polling/types'
import { resolveOAuthCredential, updateWebhookProviderConfig } from '@/lib/webhooks/polling/utils'
import {
getOAuthToken,
refreshAccessTokenIfNeeded,
resolveOAuthAccountId,
} from '@/app/api/auth/oauth/utils'
afterAll(resetDbChainMock)
const logger = { error: vi.fn() } as never
function allInterpolatedValues(): unknown[] {
return sqlCalls.flatMap((c) => c.values)
}
function allSqlText(): string {
return sqlCalls.map((c) => c.strings.join('')).join(' ')
}
describe('updateWebhookProviderConfig (atomic jsonb merge)', () => {
beforeEach(() => {
vi.clearAllMocks()
resetDbChainMock()
sqlCalls.length = 0
})
it('merges defined keys (null preserved) and removes undefined keys', async () => {
await updateWebhookProviderConfig(
'wh-1',
{ historyId: 'h1', cleared: undefined, nulled: null },
logger
)
expect(dbChainMockFns.update).toHaveBeenCalledTimes(1)
expect(allInterpolatedValues()).toContain(JSON.stringify({ historyId: 'h1', nulled: null }))
expect(allInterpolatedValues()).toContain('cleared')
})
it('uses merge only (no key-removal expression) when nothing is undefined', async () => {
await updateWebhookProviderConfig('wh-1', { historyId: 'h1' }, logger)
expect(allInterpolatedValues()).toContain(JSON.stringify({ historyId: 'h1' }))
expect(allInterpolatedValues().some((v) => Array.isArray(v))).toBe(false)
})
it('casts the json column to jsonb for the merge and back to json for storage', async () => {
await updateWebhookProviderConfig('wh-1', { historyId: 'h1', cleared: undefined }, logger)
const sqlText = allSqlText()
// Column (interpolated as a value) is cast to jsonb: `COALESCE(<col>::jsonb, ...)`
expect(sqlText).toContain('COALESCE(::jsonb')
// Merge runs in jsonb space, result cast back to the json column: `(<expr>)::json`
expect(sqlText).toContain(')::json')
})
})
describe('resolveOAuthCredential (single-credential polling)', () => {
const makeWebhook = (providerConfig: Record<string, unknown>): WebhookRecord =>
({ id: 'wh-1', providerConfig }) as unknown as WebhookRecord
beforeEach(() => {
vi.clearAllMocks()
resetDbChainMock()
})
it('resolves via credentialId: account lookup then token refresh', async () => {
vi.mocked(resolveOAuthAccountId).mockResolvedValue({
accountId: 'acc-1',
} as Awaited<ReturnType<typeof resolveOAuthAccountId>>)
queueTableRows(account, [{ userId: 'owner-1' }])
vi.mocked(refreshAccessTokenIfNeeded).mockResolvedValue('tok-abc')
const token = await resolveOAuthCredential(
makeWebhook({ credentialId: 'cred-1' }),
'google-email',
'req-1'
)
expect(token).toBe('tok-abc')
expect(resolveOAuthAccountId).toHaveBeenCalledWith('cred-1')
expect(refreshAccessTokenIfNeeded).toHaveBeenCalledWith('acc-1', 'owner-1', 'req-1')
expect(getOAuthToken).not.toHaveBeenCalled()
})
it('throws when the credential cannot be resolved to an OAuth account', async () => {
vi.mocked(resolveOAuthAccountId).mockResolvedValue(null)
await expect(
resolveOAuthCredential(makeWebhook({ credentialId: 'cred-gone' }), 'google-email', 'req-1')
).rejects.toThrow('Failed to resolve OAuth account for credential cred-gone')
})
it('throws when the resolved account row does not exist', async () => {
vi.mocked(resolveOAuthAccountId).mockResolvedValue({
accountId: 'acc-missing',
} as Awaited<ReturnType<typeof resolveOAuthAccountId>>)
await expect(
resolveOAuthCredential(makeWebhook({ credentialId: 'cred-1' }), 'google-email', 'req-1')
).rejects.toThrow('Credential cred-1 not found for webhook wh-1')
})
it('falls back to the legacy userId path via getOAuthToken', async () => {
vi.mocked(getOAuthToken).mockResolvedValue('tok-legacy')
const token = await resolveOAuthCredential(
makeWebhook({ userId: 'user-1' }),
'outlook',
'req-1'
)
expect(token).toBe('tok-legacy')
expect(getOAuthToken).toHaveBeenCalledWith('user-1', 'outlook')
expect(resolveOAuthAccountId).not.toHaveBeenCalled()
})
it('throws when neither credentialId nor userId is present', async () => {
await expect(resolveOAuthCredential(makeWebhook({}), 'gmail', 'req-1')).rejects.toThrow(
'Missing credential info for webhook wh-1'
)
})
it('throws when the legacy userId path yields no token', async () => {
vi.mocked(getOAuthToken).mockResolvedValue(null)
await expect(
resolveOAuthCredential(makeWebhook({ userId: 'user-1' }), 'outlook', 'req-1')
).rejects.toThrow('Failed to get outlook access token for webhook wh-1')
})
})