59 lines
2.7 KiB
JavaScript
59 lines
2.7 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { setTimeout as delay } from 'node:timers/promises'
|
|
import { Client, StreamableHTTPClientTransport } from '@modelcontextprotocol/client'
|
|
import { resumeMcpSteps } from './mcp-step-journal.mjs'
|
|
|
|
const [phase, port, planId, connectionId] = process.argv.slice(2)
|
|
assert(process.send && process.env.MCP_FIXTURE_TOKEN && ['dispatch', 'resume'].includes(phase))
|
|
const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i
|
|
assert.match(planId, uuid)
|
|
assert.match(connectionId, uuid)
|
|
const client = new Client({ name: 'owned-routing-observer', version: '1.0.0' })
|
|
async function call(name, args) {
|
|
const reply = await client.callTool({ name, arguments: args })
|
|
assert(!reply.isError, 'MCP fixture call failed: ' + name)
|
|
return reply.structuredContent?.result ?? JSON.parse(reply.content[0].text)
|
|
}
|
|
|
|
try {
|
|
await client.connect(new StreamableHTTPClientTransport(new URL(`http://127.0.0.1:${port}/mcp`), {
|
|
requestInit: { headers: { Authorization: 'Bearer ' + process.env.MCP_FIXTURE_TOKEN } },
|
|
}))
|
|
let operationId
|
|
const result = await resumeMcpSteps({ journal: 'routing-forward.jsonl', fixture: `routing-scoped-forward:${connectionId}:${planId}`, steps: [{
|
|
id: 'forward-apply', kind: 'apply',
|
|
async run({ idempotencyKey, accept }) {
|
|
assert.equal(phase, 'dispatch', 'A resumed observer cannot send apply')
|
|
const receipt = await call('mm_change_apply', { planId, idempotencyKey })
|
|
assert.deepEqual([receipt.plan_id, receipt.connection_id], [planId, connectionId])
|
|
accept(receipt.id)
|
|
// The supervisor terminates this process after the journal fsync.
|
|
process.send(receipt.id)
|
|
await new Promise(resolve => process.once('disconnect', resolve))
|
|
throw new Error('Observer was disconnected by its supervisor')
|
|
},
|
|
async reconcile({ accepted }) {
|
|
assert.equal(phase, 'resume')
|
|
assert.ok(accepted)
|
|
const deadline = Date.now() + 60_000
|
|
while (Date.now() < deadline) {
|
|
const receipt = await call('mm_operation_get', { operationId: accepted })
|
|
assert.deepEqual([receipt.id, receipt.plan_id, receipt.connection_id], [accepted, planId, connectionId])
|
|
if (!['pending', 'running'].includes(receipt.state)) {
|
|
assert.deepEqual([receipt.state, receipt.verification], ['applied', 'passed'])
|
|
operationId = accepted
|
|
return { state: 'verified', evidence: accepted }
|
|
}
|
|
await delay(400)
|
|
}
|
|
return { state: 'unknown' }
|
|
},
|
|
}] })
|
|
assert.deepEqual(result, { status: 'passed' })
|
|
assert.ok(operationId)
|
|
await new Promise((resolve, reject) => process.send(operationId, error => error ? reject(error) : resolve()))
|
|
} finally {
|
|
await client.close()
|
|
if (process.connected) process.disconnect()
|
|
}
|