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
274 lines
8.6 KiB
TypeScript
274 lines
8.6 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { resetEnvFlagsMock, setEnvFlags } from '@sim/testing'
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type {
|
|
RowExecutionMetadata,
|
|
TableDefinition,
|
|
TableRow,
|
|
WorkflowGroup,
|
|
} from '@/lib/table/types'
|
|
|
|
const {
|
|
mockResolveBillingAttribution,
|
|
mockResolveSystemBillingAttribution,
|
|
mockRunsCancel,
|
|
mockRunsList,
|
|
} = vi.hoisted(() => ({
|
|
mockResolveBillingAttribution: vi.fn(),
|
|
mockResolveSystemBillingAttribution: vi.fn(),
|
|
mockRunsCancel: vi.fn(),
|
|
mockRunsList: vi.fn(),
|
|
}))
|
|
|
|
const SYSTEM_BILLING_ATTRIBUTION = {
|
|
actorUserId: 'owner-after-transfer',
|
|
workspaceId: 'workspace-1',
|
|
organizationId: 'org-after-transfer',
|
|
billedAccountUserId: 'owner-after-transfer',
|
|
billingEntity: { type: 'organization' as const, id: 'org-after-transfer' },
|
|
billingPeriod: {
|
|
start: '2026-07-01T00:00:00.000Z',
|
|
end: '2026-08-01T00:00:00.000Z',
|
|
},
|
|
payerSubscription: null,
|
|
}
|
|
|
|
vi.mock('@/lib/billing/core/billing-attribution', () => ({
|
|
assertBillingAttributionSnapshot: vi.fn((value) => value),
|
|
resolveBillingAttribution: mockResolveBillingAttribution,
|
|
resolveSystemBillingAttribution: mockResolveSystemBillingAttribution,
|
|
}))
|
|
|
|
vi.mock('@trigger.dev/sdk', () => ({
|
|
runs: {
|
|
cancel: mockRunsCancel,
|
|
list: mockRunsList,
|
|
},
|
|
}))
|
|
|
|
import {
|
|
buildEnqueueItems,
|
|
cancelCellRunsByTags,
|
|
pickNextEligibleGroupForRow,
|
|
type WorkflowGroupCellPayload,
|
|
} from '@/lib/table/workflow-columns'
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockResolveBillingAttribution.mockImplementation(
|
|
({ actorUserId, workspaceId }: { actorUserId: string; workspaceId: string }) =>
|
|
Promise.resolve({
|
|
actorUserId,
|
|
workspaceId,
|
|
organizationId: 'org-1',
|
|
billedAccountUserId: 'workspace-owner',
|
|
billingEntity: { type: 'organization', id: 'org-1' },
|
|
billingPeriod: {
|
|
start: '2026-07-01T00:00:00.000Z',
|
|
end: '2026-08-01T00:00:00.000Z',
|
|
},
|
|
payerSubscription: null,
|
|
})
|
|
)
|
|
mockResolveSystemBillingAttribution.mockResolvedValue(SYSTEM_BILLING_ATTRIBUTION)
|
|
})
|
|
|
|
function makeGroup(overrides: Partial<WorkflowGroup> & { id: string }): WorkflowGroup {
|
|
return {
|
|
workflowId: `wf-${overrides.id}`,
|
|
outputs: [{ blockId: 'b1', path: 'out', columnName: `${overrides.id}_out` }],
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function makeTable(groups: WorkflowGroup[]): TableDefinition {
|
|
return {
|
|
id: 'tbl1',
|
|
name: 'T',
|
|
schema: { columns: [], workflowGroups: groups },
|
|
rowCount: 1,
|
|
maxRows: 1000,
|
|
workspaceId: 'ws1',
|
|
createdBy: 'u1',
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}
|
|
}
|
|
|
|
function makeRow(
|
|
executions: Record<string, RowExecutionMetadata>,
|
|
data: Record<string, unknown> = {}
|
|
): TableRow {
|
|
return {
|
|
id: 'row1',
|
|
data: data as TableRow['data'],
|
|
executions,
|
|
position: 0,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}
|
|
}
|
|
|
|
/** The dispatcher's "queued marker" pre-stamp: pending with no executionId. */
|
|
function queuedMarker(workflowId: string): RowExecutionMetadata {
|
|
return { status: 'pending', executionId: null, jobId: null, workflowId, error: null }
|
|
}
|
|
|
|
beforeAll(() => {
|
|
setEnvFlags({ isTriggerDevEnabled: true, isBillingEnabled: true })
|
|
})
|
|
|
|
afterAll(resetEnvFlagsMock)
|
|
|
|
describe('pickNextEligibleGroupForRow — queued-marker handoff', () => {
|
|
it('runs an autoRun:false group that carries a queued marker (explicit request)', () => {
|
|
const group = makeGroup({ id: 'g1', autoRun: false })
|
|
const table = makeTable([group])
|
|
const row = makeRow({ g1: queuedMarker('wf-g1') })
|
|
|
|
expect(pickNextEligibleGroupForRow(table, row)?.id).toBe('g1')
|
|
})
|
|
|
|
it('does NOT run an autoRun:false group with no marker (auto-cascade respects autoRun)', () => {
|
|
const group = makeGroup({ id: 'g1', autoRun: false })
|
|
const table = makeTable([group])
|
|
const row = makeRow({})
|
|
|
|
expect(pickNextEligibleGroupForRow(table, row)).toBeNull()
|
|
})
|
|
|
|
it('does NOT run an autoRun:true marker whose deps are unmet (no spin)', () => {
|
|
const group = makeGroup({ id: 'g1', autoRun: true, dependencies: { columns: ['need'] } })
|
|
const table = makeTable([group])
|
|
// marker present, but the dep column is empty → deps-unmet
|
|
const row = makeRow({ g1: queuedMarker('wf-g1') }, { need: '' })
|
|
|
|
expect(pickNextEligibleGroupForRow(table, row)).toBeNull()
|
|
})
|
|
|
|
it('still runs a normal autoRun:true group whose deps are satisfied (no marker)', () => {
|
|
const group = makeGroup({ id: 'g1', autoRun: true })
|
|
const table = makeTable([group])
|
|
const row = makeRow({})
|
|
|
|
expect(pickNextEligibleGroupForRow(table, row)?.id).toBe('g1')
|
|
})
|
|
|
|
it('skips excludeGroupId so the just-finished group does not self-retrigger', () => {
|
|
const group = makeGroup({ id: 'g1', autoRun: true })
|
|
const table = makeTable([group])
|
|
const row = makeRow({})
|
|
|
|
expect(pickNextEligibleGroupForRow(table, row, 'g1')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('buildEnqueueItems billing attribution', () => {
|
|
const run: WorkflowGroupCellPayload = {
|
|
tableId: 'table-1',
|
|
tableName: 'Table',
|
|
rowId: 'row-1',
|
|
groupId: 'group-1',
|
|
workflowId: 'workflow-1',
|
|
workspaceId: 'workspace-1',
|
|
executionId: 'execution-1',
|
|
}
|
|
|
|
it('serializes the triggering actor and workspace payer before queueing', async () => {
|
|
const [item] = await buildEnqueueItems([{ ...run, triggeredByUserId: 'external-actor' }])
|
|
|
|
expect(item.payload.billingAttribution).toMatchObject({
|
|
actorUserId: 'external-actor',
|
|
workspaceId: 'workspace-1',
|
|
billingEntity: { type: 'organization', id: 'org-1' },
|
|
})
|
|
expect(mockResolveBillingAttribution).toHaveBeenCalledWith({
|
|
actorUserId: 'external-actor',
|
|
workspaceId: 'workspace-1',
|
|
})
|
|
expect(mockResolveSystemBillingAttribution).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('uses one atomic system actor and payer snapshot for headless runs', async () => {
|
|
const [item] = await buildEnqueueItems([run])
|
|
|
|
expect(item.payload.billingAttribution).toMatchObject({
|
|
actorUserId: 'owner-after-transfer',
|
|
billedAccountUserId: 'owner-after-transfer',
|
|
billingEntity: { type: 'organization', id: 'org-after-transfer' },
|
|
})
|
|
expect(mockResolveSystemBillingAttribution).toHaveBeenCalledWith('workspace-1')
|
|
expect(mockResolveBillingAttribution).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('caps the cascade carrier and serializes each workflow attempt budget', async () => {
|
|
const [item] = await buildEnqueueItems([run])
|
|
|
|
expect(item.payload).toHaveProperty('executionTimeoutMs')
|
|
expect(item.options.maxDurationSeconds).toBe(5_700)
|
|
expect(item.options.metadata?.correlation).toEqual({
|
|
executionId: 'execution-1',
|
|
requestId: 'wfgrp-execution-1',
|
|
source: 'workflow_group',
|
|
workflowId: 'workflow-1',
|
|
triggerType: 'table',
|
|
tableId: 'table-1',
|
|
rowId: 'row-1',
|
|
groupId: 'group-1',
|
|
})
|
|
})
|
|
|
|
it('preserves an existing immutable attribution snapshot without re-resolving', async () => {
|
|
const billingAttribution = {
|
|
actorUserId: 'external-actor',
|
|
workspaceId: 'workspace-1',
|
|
organizationId: 'org-original',
|
|
billedAccountUserId: 'owner-original',
|
|
billingEntity: { type: 'organization' as const, id: 'org-original' },
|
|
billingPeriod: {
|
|
start: '2026-07-01T00:00:00.000Z',
|
|
end: '2026-08-01T00:00:00.000Z',
|
|
},
|
|
payerSubscription: null,
|
|
}
|
|
|
|
const [item] = await buildEnqueueItems([{ ...run, billingAttribution }])
|
|
|
|
expect(item.payload.billingAttribution).toEqual(billingAttribution)
|
|
expect(mockResolveBillingAttribution).not.toHaveBeenCalled()
|
|
expect(mockResolveSystemBillingAttribution).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('cancelCellRunsByTags', () => {
|
|
it('bounds Trigger.dev cancellation concurrency and limits the retained scan window', async () => {
|
|
mockRunsList.mockReturnValue({
|
|
async *[Symbol.asyncIterator]() {
|
|
for (let index = 0; index < 25; index++) yield { id: `run-${index}` }
|
|
},
|
|
})
|
|
let activeCancellations = 0
|
|
let maxActiveCancellations = 0
|
|
mockRunsCancel.mockImplementation(async () => {
|
|
activeCancellations++
|
|
maxActiveCancellations = Math.max(maxActiveCancellations, activeCancellations)
|
|
await Promise.resolve()
|
|
activeCancellations--
|
|
})
|
|
|
|
await cancelCellRunsByTags(['tableId:table-1'])
|
|
|
|
expect(mockRunsCancel).toHaveBeenCalledTimes(25)
|
|
expect(maxActiveCancellations).toBeLessThanOrEqual(10)
|
|
expect(mockRunsList).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
tag: ['tableId:table-1'],
|
|
limit: 100,
|
|
from: expect.any(Date),
|
|
})
|
|
)
|
|
})
|
|
})
|