85 lines
3.7 KiB
JavaScript
85 lines
3.7 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { resumeMcpSteps } from './mcp-step-journal.mjs'
|
|
|
|
const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i
|
|
|
|
function isProtocolStorageBusy(error) {
|
|
const text = error?.data?.text
|
|
if (error?.name !== 'SdkHttpError' || error.status !== 503 ||
|
|
typeof text !== 'string' || Buffer.byteLength(text) > 1024) return false
|
|
try {
|
|
const body = JSON.parse(text)
|
|
return body?.code === 'PROTOCOL_STORAGE_BUSY'
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/** The caller supplies an already registered synthetic test server and owner review. */
|
|
export function resumeServerPoll({ journal, fixture, serverId, connectionId, call, findPlan, findAcceptedOperation, assertApproved }) {
|
|
assert(Number.isSafeInteger(serverId) && serverId > 0)
|
|
assert.match(connectionId, uuid)
|
|
const find = async args => {
|
|
const planId = await findPlan?.({ ...args, connectionId, serverId })
|
|
return planId ? { state: 'verified', evidence: planId } : { state: 'unknown' }
|
|
}
|
|
return resumeMcpSteps({ journal, fixture: `${fixture}:server-${serverId}:connection-${connectionId}`, steps: [
|
|
{ id: 'mm_server_poll_plan', kind: 'plan', reconcile: find, async run({ accept }) {
|
|
const result = await call('mm_server_poll_plan', { serverId })
|
|
accept(result.planId)
|
|
return result.planId
|
|
} },
|
|
{ id: 'manager.plan-approved', kind: 'read', async run({ evidence }) {
|
|
const planId = evidence.get('mm_server_poll_plan')
|
|
await assertApproved(planId)
|
|
return planId
|
|
} },
|
|
{ id: 'mm_change_apply', kind: 'apply',
|
|
async reconcile(args) {
|
|
const id = args.accepted ?? await findAcceptedOperation?.({
|
|
...args, connectionId, planId: args.evidence.get('mm_server_poll_plan'),
|
|
})
|
|
return id ? { state: 'verified', evidence: id } : { state: 'unknown' }
|
|
},
|
|
async run({ evidence, idempotencyKey, accept }) {
|
|
const planId = evidence.get('mm_server_poll_plan')
|
|
const result = await call('mm_change_apply', { planId, idempotencyKey })
|
|
accept(result.id)
|
|
return result.id
|
|
} },
|
|
{ id: 'mm_operation_get', kind: 'read', async run({ evidence }) {
|
|
const operationId = evidence.get('mm_change_apply')
|
|
let result
|
|
try {
|
|
result = await call('mm_operation_get', { operationId })
|
|
} catch (error) {
|
|
if (!isProtocolStorageBusy(error)) throw error
|
|
throw Object.assign(new Error('MCP протокольное хранилище занято', { cause: error }), {
|
|
code: 'PROTOCOL_STORAGE_BUSY',
|
|
})
|
|
}
|
|
assert.equal(result.id, operationId)
|
|
assert.equal(result.plan_id, evidence.get('mm_server_poll_plan'))
|
|
assert.equal(result.connection_id, connectionId)
|
|
if (result.state !== 'applied' || result.verification !== 'passed') {
|
|
const code = ['pending', 'running'].includes(result.state) ? 'POLL_PENDING' : 'POLL_UNVERIFIED'
|
|
throw Object.assign(new Error('Poll terminal result is unverified'), { code })
|
|
}
|
|
assert.equal(result.steps.length, 1)
|
|
assert.equal(result.steps[0].state, 'verified')
|
|
const receipt = result.steps[0].result
|
|
assert.equal(receipt.operationId, operationId)
|
|
assert.match(receipt.receiptId, uuid)
|
|
assert.equal(receipt.nativeReads, 4)
|
|
assert.equal(receipt.complete, true)
|
|
assert.equal(receipt.maintenanceComplete, true)
|
|
assert.equal(receipt.configurationVerified, true)
|
|
assert.equal(receipt.automaticReplay, false)
|
|
assert(Number.isSafeInteger(receipt.completedPublications) && receipt.completedPublications >= 2)
|
|
assert.equal(receipt.confirmedPublications, receipt.completedPublications)
|
|
assert.equal(receipt.snapshot.serverId, serverId)
|
|
return operationId
|
|
} },
|
|
] })
|
|
}
|