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
113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { writeExecutionsPatch } from '@/lib/table/rows/executions'
|
|
import type { RowExecutionMetadata } from '@/lib/table/types'
|
|
|
|
const EXECUTION_STATE: RowExecutionMetadata = {
|
|
status: 'running',
|
|
executionId: 'execution-1',
|
|
jobId: null,
|
|
workflowId: 'workflow-1',
|
|
error: null,
|
|
}
|
|
|
|
function renderCondition(value: unknown): string {
|
|
if (!value || typeof value !== 'object') return ''
|
|
const record = value as Record<string, unknown>
|
|
const strings = Array.isArray(record.strings)
|
|
? record.strings.filter((entry): entry is string => typeof entry === 'string').join('')
|
|
: ''
|
|
const conditions = Array.isArray(record.conditions)
|
|
? record.conditions.map(renderCondition).join(' ')
|
|
: ''
|
|
return `${strings} ${conditions}`.trim()
|
|
}
|
|
|
|
describe('writeExecutionsPatch guards', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
})
|
|
|
|
it('rejects a worker write when the atomic stale-or-cancel predicate returns no row', async () => {
|
|
dbChainMockFns.returning.mockResolvedValueOnce([])
|
|
|
|
await expect(
|
|
writeExecutionsPatch(
|
|
dbChainMock.db as unknown as Parameters<typeof writeExecutionsPatch>[0],
|
|
'table-1',
|
|
'row-1',
|
|
{ 'group-1': EXECUTION_STATE },
|
|
{ groupId: 'group-1', executionId: 'execution-1' }
|
|
)
|
|
).resolves.toBe('guard-rejected')
|
|
|
|
const conflict = dbChainMockFns.onConflictDoUpdate.mock.calls[0]?.[0] as
|
|
| { where?: unknown }
|
|
| undefined
|
|
const condition = renderCondition(conflict?.where)
|
|
expect(condition).toContain("<> 'cancelled'")
|
|
expect(condition).toContain('IS NULL OR')
|
|
})
|
|
|
|
it('keeps queued takeover and late-same-run protection in the SQL predicate', async () => {
|
|
dbChainMockFns.returning.mockResolvedValueOnce([])
|
|
|
|
await expect(
|
|
writeExecutionsPatch(
|
|
dbChainMock.db as unknown as Parameters<typeof writeExecutionsPatch>[0],
|
|
'table-1',
|
|
'row-1',
|
|
{ 'group-1': { ...EXECUTION_STATE, status: 'queued' } },
|
|
{
|
|
groupId: 'group-1',
|
|
executionId: 'execution-1',
|
|
allowNewExecution: true,
|
|
}
|
|
)
|
|
).resolves.toBe('guard-rejected')
|
|
|
|
const conflict = dbChainMockFns.onConflictDoUpdate.mock.calls[0]?.[0] as
|
|
| { where?: unknown }
|
|
| undefined
|
|
const condition = renderCondition(conflict?.where)
|
|
expect(condition).toContain('IS DISTINCT FROM')
|
|
expect(condition).toContain("= 'pending'")
|
|
})
|
|
|
|
it('guards usage-limit deletion against cancelled and newer execution rows', async () => {
|
|
dbChainMockFns.returning.mockResolvedValueOnce([])
|
|
|
|
await expect(
|
|
writeExecutionsPatch(
|
|
dbChainMock.db as unknown as Parameters<typeof writeExecutionsPatch>[0],
|
|
'table-1',
|
|
'row-1',
|
|
{ 'group-1': null },
|
|
{ groupId: 'group-1', executionId: 'execution-1' }
|
|
)
|
|
).resolves.toBe('guard-rejected')
|
|
|
|
const condition = renderCondition(dbChainMockFns.where.mock.calls.at(-1)?.[0])
|
|
expect(condition).toContain("<> 'cancelled'")
|
|
expect(condition).toContain('IS NULL OR')
|
|
})
|
|
|
|
it('deletes the usage-limit pre-stamp only when its execution guard wins', async () => {
|
|
dbChainMockFns.returning.mockResolvedValueOnce([{ rowId: 'row-1' }])
|
|
|
|
await expect(
|
|
writeExecutionsPatch(
|
|
dbChainMock.db as unknown as Parameters<typeof writeExecutionsPatch>[0],
|
|
'table-1',
|
|
'row-1',
|
|
{ 'group-1': null },
|
|
{ groupId: 'group-1', executionId: 'execution-1' }
|
|
)
|
|
).resolves.toBe('wrote')
|
|
})
|
|
})
|