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

213 lines
6.4 KiB
TypeScript

/**
* @vitest-environment node
*
* `fireTableTrigger` is fire-and-forget — it swallows every error (trigger.ts).
* An `expect` thrown inside a mock would therefore be silently eaten and the
* test would pass green, so every assertion runs on the captured payload AFTER
* the `await`, never inside a mock factory.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockFetchActiveWebhooks, mockProcessPolledWebhookEvent } = vi.hoisted(() => ({
mockFetchActiveWebhooks: vi.fn(),
mockProcessPolledWebhookEvent: vi.fn(),
}))
vi.mock('@/lib/webhooks/polling/utils', () => ({
fetchActiveWebhooks: mockFetchActiveWebhooks,
}))
vi.mock('@/lib/webhooks/processor', () => ({
processPolledWebhookEvent: mockProcessPolledWebhookEvent,
}))
import { fireTableTrigger } from '@/lib/table/trigger'
import type { RowData, TableSchema } from '@/lib/table/types'
const schema: TableSchema = {
columns: [
{ id: 'col_title', name: 'Title', type: 'string' },
{
id: 'col_status',
name: 'Status',
type: 'select',
options: [
{ id: 'opt_open', name: 'Open' },
{ id: 'opt_closed', name: 'Closed' },
],
},
{
id: 'col_tags',
name: 'Tags',
type: 'select',
multiple: true,
options: [
{ id: 'opt_a', name: 'Alpha' },
{ id: 'opt_b', name: 'Beta' },
],
},
],
}
interface Payload {
row: Record<string, unknown> | null
rawRow: Record<string, unknown>
previousRow: Record<string, unknown> | null
changedColumns: string[]
headers: string[]
rowId: string
}
function webhookEntry(config: Record<string, unknown> = {}) {
return {
webhook: { id: 'wh_1', providerConfig: { tableId: 'tbl_1', eventType: 'insert', ...config } },
workflow: { id: 'wf_1' },
}
}
function firedPayloads(): Payload[] {
return mockProcessPolledWebhookEvent.mock.calls.map((call) => call[2] as Payload)
}
async function fire(
eventType: 'insert' | 'update',
data: RowData,
oldRows: Map<string, RowData> | null = null
) {
await fireTableTrigger(
'tbl_1',
'Issues',
eventType,
[{ id: 'row_1', data } as never],
oldRows,
schema,
'req_1'
)
}
describe('fireTableTrigger — select values', () => {
beforeEach(() => {
vi.clearAllMocks()
mockProcessPolledWebhookEvent.mockResolvedValue({ success: true })
mockFetchActiveWebhooks.mockResolvedValue([webhookEntry()])
})
it('emits option names, not stored option ids, in row and rawRow', async () => {
await fire('insert', { col_title: 'Login broken', col_status: 'opt_open', col_tags: ['opt_a'] })
const [payload] = firedPayloads()
expect(payload.rawRow).toEqual({
Title: 'Login broken',
Status: 'Open',
Tags: ['Alpha'],
})
expect(payload.row).toEqual({ Title: 'Login broken', Status: 'Open', Tags: ['Alpha'] })
})
it('emits option names in previousRow on an update', async () => {
mockFetchActiveWebhooks.mockResolvedValue([webhookEntry({ eventType: 'update' })])
await fire(
'update',
{ col_status: 'opt_closed' },
new Map([['row_1', { col_status: 'opt_open' }]])
)
const [payload] = firedPayloads()
expect(payload.previousRow).toEqual({ Status: 'Open' })
expect(payload.rawRow).toEqual({ Status: 'Closed' })
})
it('drops an option id whose option was deleted', async () => {
await fire('insert', { col_status: 'opt_gone' })
const [payload] = firedPayloads()
expect(payload.rawRow.Status).toBeNull()
})
})
describe('fireTableTrigger — payload shape', () => {
beforeEach(() => {
vi.clearAllMocks()
mockProcessPolledWebhookEvent.mockResolvedValue({ success: true })
mockFetchActiveWebhooks.mockResolvedValue([webhookEntry()])
})
it('row fills every column while rawRow omits absent ones', async () => {
await fire('insert', { col_title: 'only this' })
const [payload] = firedPayloads()
expect(payload.rawRow).toEqual({ Title: 'only this' })
expect(payload.row).toEqual({ Title: 'only this', Status: null, Tags: null })
})
it('row key set matches headers exactly', async () => {
await fire('insert', { col_title: 'x' })
const [payload] = firedPayloads()
expect(Object.keys(payload.row as Record<string, unknown>)).toEqual(payload.headers)
})
it('omits row when includeHeaders is false', async () => {
mockFetchActiveWebhooks.mockResolvedValue([webhookEntry({ includeHeaders: false })])
await fire('insert', { col_title: 'x' })
const [payload] = firedPayloads()
expect(payload.row).toBeNull()
expect(payload.rawRow).toEqual({ Title: 'x' })
})
it('reports changedColumns by display name', async () => {
mockFetchActiveWebhooks.mockResolvedValue([webhookEntry({ eventType: 'update' })])
await fire(
'update',
{ col_status: 'opt_closed' },
new Map([['row_1', { col_status: 'opt_open' }]])
)
const [payload] = firedPayloads()
expect(payload.changedColumns).toEqual(['Status'])
})
})
describe('fireTableTrigger — gating', () => {
beforeEach(() => {
vi.clearAllMocks()
mockProcessPolledWebhookEvent.mockResolvedValue({ success: true })
})
it('fires nothing for a different table', async () => {
mockFetchActiveWebhooks.mockResolvedValue([webhookEntry({ tableId: 'tbl_other' })])
await fire('insert', { col_title: 'x' })
expect(mockProcessPolledWebhookEvent).not.toHaveBeenCalled()
})
it('fires nothing when the event type does not match', async () => {
mockFetchActiveWebhooks.mockResolvedValue([webhookEntry({ eventType: 'update' })])
await fire('insert', { col_title: 'x' })
expect(mockProcessPolledWebhookEvent).not.toHaveBeenCalled()
})
it('skips a row whose watched column did not change', async () => {
mockFetchActiveWebhooks.mockResolvedValue([
webhookEntry({ eventType: 'update', watchColumns: 'Tags' }),
])
await fire(
'update',
{ col_status: 'opt_closed' },
new Map([['row_1', { col_status: 'opt_open' }]])
)
expect(mockProcessPolledWebhookEvent).not.toHaveBeenCalled()
})
it('fires when a watched column changed', async () => {
mockFetchActiveWebhooks.mockResolvedValue([
webhookEntry({ eventType: 'update', watchColumns: 'Status' }),
])
await fire(
'update',
{ col_status: 'opt_closed' },
new Map([['row_1', { col_status: 'opt_open' }]])
)
expect(mockProcessPolledWebhookEvent).toHaveBeenCalledTimes(1)
})
})