Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions browser_tests/fixtures/agentRequestResponseFixture.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { describe, expect, it } from 'vitest'

import { AgentRequestResponseQueue } from '@e2e/fixtures/agentRequestResponseFixture'
import type { AgentRequestResponseScenario } from '@e2e/fixtures/data/agentRequestResponse'

const scenarios = [
{
request: { content: 'Set the seed, then explain what changed.' },
responses: [
{
kind: 'agent_event',
event: {
type: 'agent_message_delta',
data: {
delta: 'I will update the graph first.',
message_id: 'message-1',
thread_id: 'thread-1'
}
}
},
{
kind: 'graph_operations',
operations: [
{
op: 'set_widget',
node_id: '7',
widget: 'seed',
value: 42,
old: 3
},
{
op: 'set_widget',
node_id: '7',
widget: 'steps',
value: 20,
old: 12
}
]
},
{
kind: 'agent_event',
event: {
type: 'agent_message_done',
data: {
message_id: 'message-1',
thread_id: 'thread-1'
}
}
}
]
},
{
request: {
content: 'Change the prompt.',
workflowId: 'workflow-1',
selection: { node_ids: ['7'] }
},
responses: []
}
] as const satisfies readonly AgentRequestResponseScenario[]

describe('AgentRequestResponseQueue', () => {
it('consumes requests and interleaved responses in declaration order', () => {
const queue = new AgentRequestResponseQueue(scenarios)

const responses = queue.take({
content: 'Set the seed, then explain what changed.'
})

expect(responses).toBe(scenarios[0].responses)
expect(responses.map((response) => response.kind)).toEqual([
'agent_event',
'graph_operations',
'agent_event'
])
expect(responses[1]).toEqual({
kind: 'graph_operations',
operations: [
{
op: 'set_widget',
node_id: '7',
widget: 'seed',
value: 42,
old: 3
},
{
op: 'set_widget',
node_id: '7',
widget: 'steps',
value: 20,
old: 12
}
]
})

expect(
queue.take({
content: 'Change the prompt.',
workflowId: 'workflow-1',
selection: { node_ids: ['7'] }
})
).toBe(scenarios[1].responses)
expect(() => queue.assertComplete()).not.toThrow()
})

it('rejects an out-of-order request without consuming its scenario', () => {
const queue = new AgentRequestResponseQueue(scenarios)

expect(() => queue.take(scenarios[1].request)).toThrow(
'Agent request 1 did not match the declared scenario'
)
expect(queue.take(scenarios[0].request)).toBe(scenarios[0].responses)
})

it('rejects extra requests and reports unconsumed scenarios', () => {
const queue = new AgentRequestResponseQueue(scenarios)

expect(() => queue.assertComplete()).toThrow(
'Agent request queue has 2 unconsumed scenarios'
)
queue.take(scenarios[0].request)
queue.take(scenarios[1].request)
expect(() => queue.take({ content: 'Unexpected request' })).toThrow(
'Unexpected agent request 3: no scenarios remain'
)
})
})
47 changes: 47 additions & 0 deletions browser_tests/fixtures/agentRequestResponseFixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { deepStrictEqual } from 'node:assert/strict'

import type { PostMessageInput } from '@/workbench/extensions/agent/services/agent/agentRestClient'

import type {
AgentRequestResponseScenario,
AgentResponseStep
} from '@e2e/fixtures/data/agentRequestResponse'

export class AgentRequestResponseQueue {
private nextScenarioIndex = 0

constructor(
private readonly scenarios: readonly AgentRequestResponseScenario[]
) {}

take(request: PostMessageInput): readonly AgentResponseStep[] {
const scenario = this.scenarios[this.nextScenarioIndex]
const requestNumber = this.nextScenarioIndex + 1
if (!scenario) {
throw new Error(
`Unexpected agent request ${requestNumber}: no scenarios remain`
)
}

try {
deepStrictEqual(request, scenario.request)
} catch (cause) {
throw new Error(
`Agent request ${requestNumber} did not match the declared scenario`,
{ cause }
)
}

this.nextScenarioIndex++
return scenario.responses
}

assertComplete(): void {
const remaining = this.scenarios.length - this.nextScenarioIndex
if (remaining > 0) {
throw new Error(
`Agent request queue has ${remaining} unconsumed scenarios`
)
}
}
}
18 changes: 18 additions & 0 deletions browser_tests/fixtures/data/agentRequestResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { GraphOperation } from '@/workbench/extensions/agent/crdt/graphOperations'
import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema'
import type { PostMessageInput } from '@/workbench/extensions/agent/services/agent/agentRestClient'

export type AgentResponseStep =
| {
kind: 'agent_event'
event: AgentWsEvent
}
| {
kind: 'graph_operations'
operations: readonly GraphOperation[]
}

export interface AgentRequestResponseScenario {
request: PostMessageInput
responses: readonly AgentResponseStep[]
}
Loading