d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (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 / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
130 lines
3.3 KiB
TypeScript
130 lines
3.3 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import type { ExecutionEvent } from '@/lib/workflows/executor/execution-events'
|
|
import { processSSEStream } from '@/hooks/use-execution-stream'
|
|
|
|
function streamEvents(events: ExecutionEvent[]): ReadableStream<Uint8Array> {
|
|
const encoder = new TextEncoder()
|
|
return new ReadableStream<Uint8Array>({
|
|
start(controller) {
|
|
for (const event of events) {
|
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`))
|
|
}
|
|
controller.enqueue(encoder.encode('data: [DONE]\n\n'))
|
|
controller.close()
|
|
},
|
|
})
|
|
}
|
|
|
|
describe('processSSEStream', () => {
|
|
it('acknowledges event ids only after the matching handler completes', async () => {
|
|
const order: string[] = []
|
|
const event: ExecutionEvent = {
|
|
type: 'block:started',
|
|
eventId: 5,
|
|
timestamp: new Date().toISOString(),
|
|
executionId: 'exec-1',
|
|
workflowId: 'wf-1',
|
|
data: {
|
|
blockId: 'block-1',
|
|
blockName: 'Block 1',
|
|
blockType: 'function',
|
|
executionOrder: 1,
|
|
},
|
|
}
|
|
|
|
await processSSEStream(
|
|
streamEvents([event]).getReader(),
|
|
{
|
|
onBlockStarted: async () => {
|
|
order.push('handler:start')
|
|
await Promise.resolve()
|
|
order.push('handler:end')
|
|
},
|
|
onEventId: vi.fn(async () => {
|
|
order.push('event-id')
|
|
}),
|
|
},
|
|
'test'
|
|
)
|
|
|
|
expect(order).toEqual(['handler:start', 'handler:end', 'event-id'])
|
|
})
|
|
|
|
it('propagates callback failures without acknowledging the event id', async () => {
|
|
const event: ExecutionEvent = {
|
|
type: 'block:started',
|
|
eventId: 6,
|
|
timestamp: new Date().toISOString(),
|
|
executionId: 'exec-1',
|
|
workflowId: 'wf-1',
|
|
data: {
|
|
blockId: 'block-1',
|
|
blockName: 'Block 1',
|
|
blockType: 'function',
|
|
executionOrder: 1,
|
|
},
|
|
}
|
|
const onEventId = vi.fn()
|
|
|
|
await expect(
|
|
processSSEStream(
|
|
streamEvents([event]).getReader(),
|
|
{
|
|
onBlockStarted: async () => {
|
|
throw new Error('handler failed')
|
|
},
|
|
onEventId,
|
|
},
|
|
'test'
|
|
)
|
|
).rejects.toThrow('handler failed')
|
|
|
|
expect(onEventId).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('releases the reader lock after the stream completes', async () => {
|
|
const stream = streamEvents([])
|
|
const reader = stream.getReader()
|
|
expect(stream.locked).toBe(true)
|
|
|
|
await processSSEStream(reader, {}, 'test')
|
|
|
|
expect(stream.locked).toBe(false)
|
|
})
|
|
|
|
it('releases the reader lock even when a handler throws', async () => {
|
|
const event: ExecutionEvent = {
|
|
type: 'block:started',
|
|
eventId: 7,
|
|
timestamp: new Date().toISOString(),
|
|
executionId: 'exec-1',
|
|
workflowId: 'wf-1',
|
|
data: {
|
|
blockId: 'block-1',
|
|
blockName: 'Block 1',
|
|
blockType: 'function',
|
|
executionOrder: 1,
|
|
},
|
|
}
|
|
const stream = streamEvents([event])
|
|
const reader = stream.getReader()
|
|
|
|
await expect(
|
|
processSSEStream(
|
|
reader,
|
|
{
|
|
onBlockStarted: () => {
|
|
throw new Error('boom')
|
|
},
|
|
},
|
|
'test'
|
|
)
|
|
).rejects.toThrow('boom')
|
|
|
|
expect(stream.locked).toBe(false)
|
|
})
|
|
})
|