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

407 lines
12 KiB
TypeScript

/**
* @vitest-environment node
*/
import { dbChainMockFns, resetDbChainMock } from '@sim/testing'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
import type { RowExecutionMetadata, TableDefinition, WorkflowGroup } from '@/lib/table/types'
const { mockAppendTableEvent, mockDecryptSecret, mockUpdateRow, mockWriteExecutionsPatch } =
vi.hoisted(() => ({
mockAppendTableEvent: vi.fn(),
mockDecryptSecret: vi.fn(),
mockUpdateRow: vi.fn(),
mockWriteExecutionsPatch: vi.fn(),
}))
vi.mock('@/lib/core/security/encryption', () => ({
decryptSecret: mockDecryptSecret,
}))
vi.mock('@/lib/table/events', () => ({
appendTableEvent: mockAppendTableEvent,
}))
vi.mock('@/lib/table/rows/executions', () => ({
writeExecutionsPatch: mockWriteExecutionsPatch,
}))
vi.mock('@/lib/table/rows/service', () => ({
updateRow: mockUpdateRow,
}))
import { createWorkflowCellProgressWriter, writeWorkflowGroupState } from '@/lib/table/cell-write'
const TABLE: TableDefinition = {
id: 'table-1',
name: 'Leads',
schema: {
columns: [
{ id: 'first-output', name: 'First output', type: 'string' },
{ id: 'second-output', name: 'Second output', type: 'string' },
{
id: 'status-output',
name: 'Status output',
type: 'select',
options: [
{ id: 'opt_open', name: 'Open' },
{ id: 'opt_closed', name: 'Closed' },
],
},
],
},
rowCount: 1,
maxRows: 100,
workspaceId: 'workspace-1',
createdBy: 'user-1',
locks: { schemaLocked: false, insertLocked: false, updateLocked: false, deleteLocked: false },
createdAt: new Date('2026-01-01T00:00:00.000Z'),
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
}
const GROUP: WorkflowGroup = {
id: 'group-1',
workflowId: 'workflow-1',
outputs: [
{ blockId: 'block-1', path: 'value', columnName: 'first-output' },
{ blockId: 'block-2', path: 'value', columnName: 'second-output' },
],
}
const CONTEXT = {
tableId: TABLE.id,
rowId: 'row-1',
workspaceId: TABLE.workspaceId,
groupId: GROUP.id,
executionId: 'execution-1',
requestId: 'request-1',
table: TABLE,
}
const RUNNING_STATE: RowExecutionMetadata = {
status: 'running',
executionId: CONTEXT.executionId,
jobId: null,
workflowId: GROUP.workflowId,
error: null,
}
describe('writeWorkflowGroupState', () => {
afterAll(() => {
resetDbChainMock()
})
beforeEach(() => {
vi.clearAllMocks()
resetDbChainMock()
mockWriteExecutionsPatch.mockResolvedValue('wrote')
mockUpdateRow.mockResolvedValue({})
mockAppendTableEvent.mockResolvedValue(null)
mockDecryptSecret.mockImplementation(async (encryptedValue: string) => ({
decrypted: encryptedValue === 'encrypted-secret' ? 'secret-value' : encryptedValue,
}))
})
it('persists a status-only transition with one guarded execution write', async () => {
await expect(writeWorkflowGroupState(CONTEXT, { executionState: RUNNING_STATE })).resolves.toBe(
'wrote'
)
expect(dbChainMockFns.transaction).toHaveBeenCalledOnce()
expect(mockWriteExecutionsPatch).toHaveBeenCalledWith(
expect.anything(),
TABLE.id,
CONTEXT.rowId,
{ [GROUP.id]: RUNNING_STATE },
{ groupId: GROUP.id, executionId: CONTEXT.executionId }
)
expect(mockUpdateRow).not.toHaveBeenCalled()
expect(mockAppendTableEvent).toHaveBeenCalledWith({
kind: 'cell',
tableId: TABLE.id,
rowId: CONTEXT.rowId,
groupId: GROUP.id,
status: 'running',
executionId: CONTEXT.executionId,
jobId: null,
error: null,
})
})
it('writes only changed data while emitting cumulative outputs', async () => {
const secretProvenance = {
complete: true,
columns: {
'second-output': { version: 1 as const, complete: true, entries: [] },
},
}
await expect(
writeWorkflowGroupState(CONTEXT, {
executionState: RUNNING_STATE,
dataPatch: { 'second-output': 'second' },
secretProvenance,
eventOutputs: {
'first-output': 'first',
'second-output': 'second',
},
})
).resolves.toBe('wrote')
expect(mockUpdateRow).toHaveBeenCalledWith(
{
tableId: TABLE.id,
rowId: CONTEXT.rowId,
data: { 'second-output': 'second' },
workspaceId: TABLE.workspaceId,
executionsPatch: { [GROUP.id]: RUNNING_STATE },
cancellationGuard: { groupId: GROUP.id, executionId: CONTEXT.executionId },
secretProvenance,
},
TABLE,
CONTEXT.requestId,
{ computedWrite: true }
)
expect(mockAppendTableEvent).toHaveBeenCalledWith(
expect.objectContaining({
outputs: {
'first-output': 'first',
'second-output': 'second',
},
})
)
})
it('emits the stored option id for a select column, not the raw workflow value', async () => {
// The workflow returns the option *name*; storage keys on the option id.
// Emitting the raw name would make the grid resolve it as an id, find
// nothing, and render the cell empty until the next refetch.
const dataPatch = { 'status-output': 'Open' }
await expect(
writeWorkflowGroupState(CONTEXT, { executionState: RUNNING_STATE, dataPatch })
).resolves.toBe('wrote')
expect(mockAppendTableEvent).toHaveBeenCalledWith(
expect.objectContaining({ outputs: { 'status-output': 'opt_open' } })
)
// The patch object feeds the progress writer's identity-compared retry
// bookkeeping, so it must survive untouched.
expect(dataPatch).toEqual({ 'status-output': 'Open' })
})
it('resolves select values in a cumulative event snapshot with no data patch', async () => {
await expect(
writeWorkflowGroupState(CONTEXT, {
executionState: RUNNING_STATE,
eventOutputs: { 'first-output': 'first', 'status-output': 'Closed' },
})
).resolves.toBe('wrote')
expect(mockAppendTableEvent).toHaveBeenCalledWith(
expect.objectContaining({
outputs: { 'first-output': 'first', 'status-output': 'opt_closed' },
})
)
})
it('suppresses events when stale or cancelled SQL guards reject writes', async () => {
mockWriteExecutionsPatch.mockResolvedValueOnce('guard-rejected')
await expect(writeWorkflowGroupState(CONTEXT, { executionState: RUNNING_STATE })).resolves.toBe(
'skipped'
)
mockUpdateRow.mockResolvedValueOnce(null)
await expect(
writeWorkflowGroupState(CONTEXT, {
executionState: RUNNING_STATE,
dataPatch: { 'first-output': 'late' },
eventOutputs: { 'first-output': 'late' },
})
).resolves.toBe('skipped')
expect(mockAppendTableEvent).not.toHaveBeenCalled()
})
})
describe('createWorkflowCellProgressWriter', () => {
it('keeps callback count and cumulative event order while bounding DB patches to each block', async () => {
const writeProgress = vi.fn().mockResolvedValue('wrote')
const progress = createWorkflowCellProgressWriter({
group: GROUP,
writeProgress,
onWriteError: vi.fn(),
})
await progress.onBlockStart('block-1')
await progress.onBlockComplete('block-1', { output: { value: 'first' } })
await progress.onBlockStart('block-2')
await progress.onBlockComplete('block-2', { output: { value: 'second' } })
await progress.waitForPendingWrites()
expect(writeProgress).toHaveBeenCalledTimes(4)
expect(writeProgress.mock.calls.map(([write]) => write)).toEqual([
{
dataPatch: undefined,
eventOutputs: {},
runningBlockIds: ['block-1'],
blockErrors: {},
},
{
dataPatch: { 'first-output': 'first' },
eventOutputs: { 'first-output': 'first' },
secretProvenance: { complete: false, columns: {} },
runningBlockIds: [],
blockErrors: {},
},
{
dataPatch: undefined,
eventOutputs: { 'first-output': 'first' },
runningBlockIds: ['block-2'],
blockErrors: {},
},
{
dataPatch: { 'second-output': 'second' },
eventOutputs: {
'first-output': 'first',
'second-output': 'second',
},
secretProvenance: { complete: false, columns: {} },
runningBlockIds: [],
blockErrors: {},
},
])
expect(progress.getPendingDataPatch()).toEqual({})
expect(progress.getEventOutputs()).toEqual({
'first-output': 'first',
'second-output': 'second',
})
expect(progress.getPendingSecretProvenance()).toEqual({ complete: true, columns: {} })
await progress.finish()
})
it('retains only failed changed fields for the terminal recovery write', async () => {
const writeProgress = vi.fn().mockRejectedValueOnce(new Error('temporary write failure'))
const progress = createWorkflowCellProgressWriter({
group: GROUP,
writeProgress,
onWriteError: vi.fn(),
})
await progress.onBlockComplete('block-1', { output: { value: 'first' } })
await progress.waitForPendingWrites()
expect(progress.getPendingDataPatch()).toEqual({ 'first-output': 'first' })
expect(progress.getEventOutputs()).toEqual({ 'first-output': 'first' })
await progress.finish()
})
it('retries a failed changed patch on the next ordered progress write', async () => {
const writeProgress = vi
.fn()
.mockRejectedValueOnce(new Error('temporary write failure'))
.mockResolvedValue('wrote')
const progress = createWorkflowCellProgressWriter({
group: GROUP,
writeProgress,
onWriteError: vi.fn(),
})
await progress.onBlockComplete('block-1', { output: { value: 'first' } })
await progress.waitForPendingWrites()
await progress.onBlockStart('block-2')
await progress.waitForPendingWrites()
expect(writeProgress).toHaveBeenNthCalledWith(2, {
dataPatch: { 'first-output': 'first' },
eventOutputs: { 'first-output': 'first' },
secretProvenance: { complete: false, columns: {} },
runningBlockIds: ['block-2'],
blockErrors: {},
})
expect(progress.getPendingDataPatch()).toEqual({})
await progress.finish()
})
it('persists only the secret provenance present in each mapped output cell', async () => {
const writeProgress = vi.fn().mockResolvedValue('wrote')
const progress = createWorkflowCellProgressWriter({
group: GROUP,
writeProgress,
onWriteError: vi.fn(),
})
await progress.onBlockComplete('block-1', {
output: { value: 'secret-value' },
resolvedSecretTraceProvenance: {
version: 1,
complete: true,
entries: [{ name: 'API_KEY', encryptedValue: 'encrypted-secret' }],
scope: { userId: 'user-1', workspaceId: 'workspace-1' },
},
})
await progress.waitForPendingWrites()
expect(writeProgress).toHaveBeenCalledWith(
expect.objectContaining({
dataPatch: { 'first-output': 'secret-value' },
secretProvenance: {
complete: true,
columns: {
'first-output': {
version: 1,
complete: true,
entries: [{ name: 'API_KEY', encryptedValue: 'encrypted-secret' }],
scope: { userId: 'user-1', workspaceId: 'workspace-1' },
},
},
},
})
)
})
it('certifies a current exact-empty callback but keeps a legacy callback unknown', async () => {
const writeProgress = vi.fn().mockResolvedValue('wrote')
const progress = createWorkflowCellProgressWriter({
group: GROUP,
writeProgress,
onWriteError: vi.fn(),
})
await progress.onBlockComplete('block-1', {
output: { value: 'public' },
resolvedSecretTraceProvenance: {
version: 1,
complete: true,
entries: [],
scope: { userId: 'user-1', workspaceId: 'workspace-1' },
},
})
await progress.onBlockComplete('block-2', { output: { value: 'legacy-public' } })
await progress.waitForPendingWrites()
expect(writeProgress).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
secretProvenance: {
complete: true,
columns: {
'first-output': {
version: 1,
complete: true,
entries: [],
scope: { userId: 'user-1', workspaceId: 'workspace-1' },
},
},
},
})
)
expect(writeProgress).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
secretProvenance: { complete: false, columns: {} },
})
)
})
})