d25d482dc2
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
217 lines
5.5 KiB
TypeScript
217 lines
5.5 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
isContractStreamEventEnvelope,
|
|
isSyntheticFilePreviewEventEnvelope,
|
|
parsePersistedStreamEventEnvelope,
|
|
parsePersistedStreamEventEnvelopeJson,
|
|
} from './contract'
|
|
|
|
const BASE_ENVELOPE = {
|
|
v: 1 as const,
|
|
seq: 1,
|
|
ts: '2026-04-11T00:00:00.000Z',
|
|
stream: {
|
|
streamId: 'stream-1',
|
|
cursor: '1',
|
|
},
|
|
trace: {
|
|
requestId: 'req-1',
|
|
},
|
|
}
|
|
|
|
describe('stream session contract parser', () => {
|
|
it('accepts contract text events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
trace: {
|
|
...BASE_ENVELOPE.trace,
|
|
goTraceId: 'go-trace-1',
|
|
},
|
|
type: 'text' as const,
|
|
payload: {
|
|
channel: 'assistant' as const,
|
|
text: 'hello',
|
|
},
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
|
|
const parsed = parsePersistedStreamEventEnvelope(event)
|
|
expect(parsed).toEqual({
|
|
ok: true,
|
|
event,
|
|
})
|
|
})
|
|
|
|
it('accepts contract session chat events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'session' as const,
|
|
payload: { kind: 'chat' as const, chatId: 'chat-1' },
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
expect(parsePersistedStreamEventEnvelope(event).ok).toBe(true)
|
|
})
|
|
|
|
it('accepts contract complete events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'complete' as const,
|
|
payload: { status: 'complete' as const },
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
expect(parsePersistedStreamEventEnvelope(event).ok).toBe(true)
|
|
})
|
|
|
|
it('accepts contract error events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'error' as const,
|
|
payload: { message: 'something went wrong' },
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
expect(parsePersistedStreamEventEnvelope(event).ok).toBe(true)
|
|
})
|
|
|
|
it('accepts contract tool call events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'tool' as const,
|
|
payload: {
|
|
toolCallId: 'tc-1',
|
|
toolName: 'read',
|
|
phase: 'call' as const,
|
|
executor: 'sim' as const,
|
|
mode: 'sync' as const,
|
|
},
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
expect(parsePersistedStreamEventEnvelope(event).ok).toBe(true)
|
|
})
|
|
|
|
it('accepts contract span events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'span' as const,
|
|
payload: {
|
|
kind: 'subagent' as const,
|
|
event: 'start' as const,
|
|
agent: 'file',
|
|
},
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
expect(parsePersistedStreamEventEnvelope(event).ok).toBe(true)
|
|
})
|
|
|
|
it('accepts contract resource events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'resource' as const,
|
|
payload: {
|
|
op: 'upsert' as const,
|
|
resource: { id: 'r-1', type: 'file', title: 'test.md' },
|
|
},
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
expect(parsePersistedStreamEventEnvelope(event).ok).toBe(true)
|
|
})
|
|
|
|
it('accepts contract run events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'run' as const,
|
|
payload: { kind: 'compaction_start' as const },
|
|
}
|
|
|
|
expect(isContractStreamEventEnvelope(event)).toBe(true)
|
|
expect(parsePersistedStreamEventEnvelope(event).ok).toBe(true)
|
|
})
|
|
|
|
it('accepts synthetic file preview events', () => {
|
|
const event = {
|
|
...BASE_ENVELOPE,
|
|
type: 'tool' as const,
|
|
payload: {
|
|
toolCallId: 'preview-1',
|
|
toolName: 'workspace_file' as const,
|
|
previewPhase: 'file_preview_content' as const,
|
|
content: 'draft body',
|
|
contentMode: 'snapshot' as const,
|
|
previewVersion: 2,
|
|
fileName: 'draft.md',
|
|
},
|
|
}
|
|
|
|
expect(isSyntheticFilePreviewEventEnvelope(event)).toBe(true)
|
|
|
|
const parsed = parsePersistedStreamEventEnvelope(event)
|
|
expect(parsed).toEqual({
|
|
ok: true,
|
|
event,
|
|
})
|
|
})
|
|
|
|
it('rejects invalid tool events with structured validation errors', () => {
|
|
const parsed = parsePersistedStreamEventEnvelope({
|
|
...BASE_ENVELOPE,
|
|
type: 'tool',
|
|
payload: {
|
|
toolCallId: 'tool-1',
|
|
toolName: 'read',
|
|
},
|
|
})
|
|
|
|
expect(parsed.ok).toBe(false)
|
|
if (parsed.ok) {
|
|
throw new Error('expected invalid result')
|
|
}
|
|
expect(parsed.reason).toBe('invalid_stream_event')
|
|
})
|
|
|
|
it('rejects unknown event types', () => {
|
|
const parsed = parsePersistedStreamEventEnvelope({
|
|
...BASE_ENVELOPE,
|
|
type: 'unknown_type',
|
|
payload: {},
|
|
})
|
|
|
|
expect(parsed.ok).toBe(false)
|
|
if (parsed.ok) {
|
|
throw new Error('expected invalid result')
|
|
}
|
|
expect(parsed.reason).toBe('invalid_stream_event')
|
|
expect(parsed.errors).toContain('unknown type="unknown_type"')
|
|
})
|
|
|
|
it('rejects non-object values', () => {
|
|
const parsed = parsePersistedStreamEventEnvelope('not an object')
|
|
|
|
expect(parsed.ok).toBe(false)
|
|
if (parsed.ok) {
|
|
throw new Error('expected invalid result')
|
|
}
|
|
expect(parsed.reason).toBe('invalid_stream_event')
|
|
expect(parsed.errors).toContain('value is not an object')
|
|
})
|
|
|
|
it('reports invalid JSON separately from schema failures', () => {
|
|
const parsed = parsePersistedStreamEventEnvelopeJson('{')
|
|
|
|
expect(parsed.ok).toBe(false)
|
|
if (parsed.ok) {
|
|
throw new Error('expected invalid json result')
|
|
}
|
|
expect(parsed.reason).toBe('invalid_json')
|
|
})
|
|
})
|