// Story 2.15 browser acceptance: OpenAPI branch in the guided onboarding. // // STATUS: ACTIVE. All leaves are activated and assert the behavior promised by the // approved spec `_bmad-output/implementation-artifacts/spec-2-15-openapi-guided-onboarding.md`. // // Backend authority (immutable ImportPlan apply/replay, onboarding selection and // event rollups, migration/schema guards) is covered by the Rust integration // suites mapped in // `_bmad-output/test-artifacts/atdd-checklist-2-15-openapi-guided-onboarding.md`. // This file only covers user-observable browser behavior. const { test, expect } = require('@playwright/test'); const { browserJson, getCurrentWorkspace, login, localized, uniqueName } = require('./helpers'); const SECRET_CANARY = 'crk_onboarding_raw_secret_canary'; const REPRESENTATIVE_OPENAPI = [ 'openapi: 3.0.3', "info: { title: 'Onboarding representative document', version: '1' }", 'servers: [{ url: http://127.0.0.1:3310 }]', 'paths:', ' /rates:', ' get:', ' operationId: listRates', ' responses:', " '200':", ' description: JSON exchange rates', ' content:', ' application/json:', ' schema:', ' type: object', ' properties:', ' amount: { type: number }', ' base: { type: string }', ' date: { type: string }', ' rates:', ' type: object', ' properties:', ' EUR: { type: number }', ' /health:', ' get:', ' operationId: getHealth', ' responses:', " '200':", ' description: JSON service health', ' content:', ' application/json:', ' schema:', ' type: object', ' properties:', ' status: { type: string }', '', ].join('\n'); function representativeOpenApi(suffix) { const safeSuffix = suffix.replace(/[^a-zA-Z0-9_]/g, '_'); return REPRESENTATIVE_OPENAPI .replace('operationId: listRates', `operationId: listRates_${safeSuffix}`) .replace('operationId: getHealth', `operationId: getHealth_${safeSuffix}`); } function importContext(report, overrides = {}) { return Object.assign({ application_idempotency_key: report.idempotency_key, plan_digest: report.plan_digest, report, choices: report.outcomes.map((outcome) => ({ key: outcome.key, name: outcome.name, outcome: outcome.outcome, operation_id: outcome.operation_id, version: outcome.version, reason: outcome.reason, selectable: ['created', 'versioned', 'renamed'].includes(outcome.outcome), })), selected_operation_id: null, selected_operation_version: null, drift: null, }, overrides); } function registeredResponse(snapshot, context) { return Object.assign({}, snapshot, { registered: true, revision: snapshot.revision + 1, import_context: context, }); } function selectedResponse(snapshot, context, operationId, version) { return Object.assign({}, snapshot, { selected: true, revision: snapshot.revision + 1, operation_id: operationId, operation_version: version, import_context: Object.assign({}, context, { selected_operation_id: operationId, selected_operation_version: version, }), }); } function onboardingSnapshot(workspaceId, overrides = {}) { return { schema_version: 1, workspace_id: workspaceId, revision: 7, status: 'in_progress', completed: false, eligible_since: '2026-10-03T08:00:00Z', steps: [ { id: 'operation', completed: false, status: 'current', action_code: 'create_operation', reason_code: 'authoritative_evidence_missing' }, { id: 'test', completed: false, status: 'pending', action_code: 'test_operation', reason_code: 'prerequisite_incomplete' }, { id: 'publish_operation', completed: false, status: 'pending', action_code: 'publish_operation', reason_code: 'prerequisite_incomplete' }, { id: 'agent', completed: false, status: 'pending', action_code: 'publish_agent', reason_code: 'prerequisite_incomplete' }, { id: 'key', completed: false, status: 'pending', action_code: 'create_mcp_key', reason_code: 'prerequisite_incomplete' }, { id: 'mcp_connection', completed: false, status: 'pending', action_code: 'connect_mcp_client', reason_code: 'prerequisite_incomplete' }, { id: 'first_call', completed: false, status: 'pending', action_code: 'call_tool', reason_code: 'prerequisite_incomplete' }, ], operation_id: null, operation_version: null, agent_id: null, catalog_revision: null, platform_api_key_id: null, mcp_endpoint: 'http://127.0.0.1:3302/mcp', first_call: null, ...overrides, }; } function candidate(name, overrides = {}) { const draft = { name, display_name: name, category: 'default', target: { method: 'GET', path: '/' + name, server_url: 'https://api.example.test' }, input_schema: { type: 'object', properties: {} }, output_schema: { type: 'object', properties: {} }, input_mapping: [], output_mapping: [], tool_description: {}, wizard_state: null, }; return Object.assign( { key: 'GET /' + name, method: 'GET', path: '/' + name, status: 'actionable', operation_id: null, suggested_name: name, suggested_display_name: name, description: 'ATDD candidate', category: 'default', input_fields: 0, output_fields: 1, server_urls: ['https://api.example.test'], source_location: { pointer: '/paths/~1' + name + '/get' }, findings: [], security_requirements: { source: 'none', alternatives: [] }, security_schemes: {}, draft, }, overrides, ); } function previewResponse(operations, extra = {}) { const base = { job_id: 'imp_e2e_onboarding_215', expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), preview: { source: { format: 'openapi', version: '3.0.3', title: 'Onboarding ATDD', servers: ['https://api.example.test'] }, groups: [{ key: 'default', title: 'Default', operations }], findings: [], conflicts: [], }, security_profile_choices: [], }; return Object.assign(base, extra, { preview: Object.assign(base.preview, extra.preview || {}), }); } function planResponse(candidates, overrides = {}) { return Object.assign( { plan_reference: 'plan_e2e_onboarding_215', plan_digest: 'a'.repeat(64), expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), selection_mode: 'full', counts: { selected: candidates.length, excluded: 0, actionable: candidates.length, blocked: 0 }, candidates, }, overrides, ); } function applyReport(outcomes, overrides = {}) { return Object.assign( { schema_version: 'import-plan-apply-report-v1', status: 'applied', plan_id: 'plan_e2e_onboarding_215', plan_digest: 'a'.repeat(64), job_id: 'imp_e2e_onboarding_215', idempotency_key: 'onboarding:apply:1', source_id: 'src_e2e_onboarding', source_digest: 'b'.repeat(64), dependencies: [], counts: { created: outcomes.length, versioned: 0, renamed: 0, skipped: 0, excluded: 0, rejected: 0 }, outcomes, summary: 'Plan applied', recovery: 'none', applied_at: '2026-10-03T08:05:00Z', }, overrides, ); } async function routeOnboarding(page, snapshot) { await page.route( /\/api\/admin\/workspaces\/[^/]+\/onboarding(?:\/events|\/reset-selection)?(?:\?.*)?$/, async (route) => { if (route.request().method() === 'GET') { await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(snapshot) }); return; } await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(Object.assign({ accepted: true }, snapshot)), }); }, ); } async function routePreview(page, body, status = 200) { await page.route(/\/imports\/openapi\/preview(?:\?.*)?$/, (route) => route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(body), })); } async function routePlanCreate(page, body, status = 200) { await page.route(/\/imports\/openapi\/[^/]+\/plans(?:\?.*)?$/, (route) => route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(body), })); } async function routeApply(page, body, status = 200) { await page.route(/\/imports\/openapi\/[^/]+\/apply(?:\?.*)?$/, async (route) => { const requestBody = route.request().postDataJSON(); const responseBody = typeof body === 'function' ? body(requestBody) : body; await route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(responseBody) }); }); } async function openOnboarding(page) { await login(page); const workspace = await getCurrentWorkspace(page); await routeOnboarding(page, onboardingSnapshot(workspace.id)); await page.goto('/?onboarding=1'); const checklist = page.getByTestId('onboarding-checklist'); if (!(await checklist.isVisible())) { await page.getByTestId('onboarding-trigger').click(); } await expect(checklist).toBeVisible(); return workspace; } async function chooseOpenApiPath(page) { await page.getByTestId('onboarding-openapi-path').click(); await expect(page.locator('#openapi-import-modal')).toBeVisible(); } async function uploadPreview(page, document = REPRESENTATIVE_OPENAPI) { await page.locator('#openapi-import-file').setInputFiles({ name: 'openapi.yaml', mimeType: 'application/yaml', buffer: Buffer.from(document), }); await page.locator('#openapi-import-preview').click(); } function parseMcpResponse(response) { return response.text().then((text) => { const dataLine = text.split(/\r?\n/).find((line) => line.startsWith('data:')); return JSON.parse(dataLine ? dataLine.slice(5).trim() : text); }); } async function mcpPost(request, endpoint, key, payload, sessionId) { const headers = { Authorization: `Bearer ${key}`, Accept: 'application/json, text/event-stream', 'Content-Type': 'application/json', }; if (sessionId) { headers['MCP-Session-Id'] = sessionId; headers['MCP-Protocol-Version'] = '2025-11-25'; } return request.post(endpoint, { headers, data: payload }); } test.describe('Story 2.15 OpenAPI guided onboarding acceptance', () => { test('[P1] AC-1 a protected non-Operations entry safely routes to canonical OpenAPI and preserves same-origin return context', async ({ page }) => { await login(page); const workspace = await getCurrentWorkspace(page); await routeOnboarding(page, onboardingSnapshot(workspace.id)); await page.goto('/agents?onboarding=1'); const trigger = page.getByTestId('onboarding-trigger'); if (!await trigger.isVisible()) await expect(trigger).toBeVisible(); if (!await page.getByTestId('onboarding-checklist').isVisible()) await trigger.click(); await expect(page.getByTestId('onboarding-openapi-path')).toBeVisible(); expect(await page.evaluate(() => Boolean(window.CrankOpenApiImport))).toBe(false); // Without the Operations importer bundle, the visible onboarding entry // routes to Operations with a bounded, same-origin return hint. const operationsNavigation = page.waitForRequest((request) => { const url = new URL(request.url()); return request.isNavigationRequest() && url.pathname === '/' && url.searchParams.get('onboarding_openapi') === '1'; }); await page.getByTestId('onboarding-openapi-path').click(); const navigation = await operationsNavigation; expect(new URL(navigation.url()).origin).toBe(new URL(process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:3300').origin); expect(new URL(page.url()).pathname).toBe('/'); await expect(page.locator('#openapi-import-modal')).toBeVisible(); await expect(page.locator('#openapi-import-file')).toBeAttached(); const returnHint = new URL(page.url()).searchParams.get('return'); expect(returnHint).toBe('/agents?onboarding=1'); expect(returnHint.length).toBeLessThanOrEqual(256); const returnContext = await page.getByTestId('onboarding-return-context').textContent(); expect(returnContext).toBe('/agents?onboarding=1'); expect(returnContext).not.toContain('://'); expect(new URL(page.url()).origin).toBe(new URL(process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:3300').origin); }); test('[P1] AC-2 guided preview shows methods, findings, the exact proposed Operation and apply consequences without hiding blockers', async ({ page }) => { await openOnboarding(page); const actionable = candidate('onboarding-rates'); const blocked = candidate('onboarding-blocked', { status: 'blocked', findings: [{ code: 'unsupported_xml_body', severity: 'Error', message: 'XML bodies are unsupported', operation_key: 'GET /onboarding-blocked', source_location: { pointer: '/paths/~1onboarding-blocked/post' } }], }); await routePreview(page, previewResponse([actionable, blocked], { preview: { findings: [{ code: 'unsupported_xml_body', severity: 'Error', message: 'XML bodies are unsupported', operation_key: blocked.key, source_location: { pointer: '/paths/~1onboarding-blocked/post' } }] }, })); await chooseOpenApiPath(page); await uploadPreview(page); // Criterion-defining assertion: the exact proposed Operation is visible for the selected candidate. await expect(page.locator('#openapi-import-groups')).toContainText(actionable.suggested_name); await expect(page.locator('#openapi-import-groups')).toContainText('GET'); await expect(page.locator('[data-testid="openapi-import-finding-code"]')).toContainText('unsupported_xml_body'); // A blocker cannot be hidden to continue: the blocker summary is shown and // creation stays unavailable until the finding is resolved. await expect(page.locator('[data-testid="openapi-import-blockers"]')).toBeVisible(); await expect(page.locator('#openapi-import-create')).toBeDisabled(); }); test('[P1] AC-3 security mapping requires only a canonical Auth Profile reference and never stores a raw credential', async ({ page }) => { await openOnboarding(page); const workspace = await getCurrentWorkspace(page); const profileName = uniqueName('onboarding_auth_profile'); const secretValue = SECRET_CANARY + '_' + uniqueName('value'); const secret = await browserJson( page, 'POST', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/secrets`, { name: uniqueName('onboarding_secret'), kind: 'token', value: secretValue }, ); const profile = await browserJson( page, 'POST', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/auth-profiles`, { name: profileName, kind: 'bearer', config: { bearer: { header_name: 'Authorization', secret_id: secret.id } }, }, ); await chooseOpenApiPath(page); await page.locator('#openapi-import-file').setInputFiles({ name: 'onboarding-security.yaml', mimeType: 'application/yaml', buffer: Buffer.from([ 'openapi: 3.0.3', `info: { title: ${profileName}, version: "1" }`, 'servers: [{ url: "http://127.0.0.1:3310" }]', 'paths:', ' /rates:', ' get:', ' operationId: onboarding_secured_rates', ' security:', ' - bearerAuth: []', ' responses:', " '200': { description: OK }", 'components:', ' securitySchemes:', ' bearerAuth: { type: http, scheme: bearer }', ].join('\n')), }); const previewResponsePromise = page.waitForResponse((response) => response.request().method() === 'POST' && response.url().includes('/imports/openapi/preview')); await page.locator('#openapi-import-preview').click(); const previewResponseReal = await previewResponsePromise; expect(previewResponseReal.status()).toBe(200); const previewArtifact = await previewResponseReal.json(); expect(JSON.stringify(previewArtifact)).not.toContain(secretValue); await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); // Criterion-defining assertion: the canonical Auth Profile picker is shown. const alternative = page.getByLabel(localized('Security alternative', 'Вариант безопасности')); const bearerOption = alternative.locator('option', { hasText: 'bearerAuth' }); await bearerOption.waitFor({ state: 'attached' }); const bearerIndex = await bearerOption.getAttribute('value'); expect(bearerIndex).toMatch(/^\d+$/); await alternative.selectOption(bearerIndex); const profilePicker = page.locator('#openapi-import-groups [data-openapi-security-profile]').first(); await expect(profilePicker).toBeVisible(); await profilePicker.selectOption(profile.id); const planRequest = page.waitForRequest((request) => request.method() === 'POST' && /\/imports\/openapi\/[^/]+\/plans$/.test(request.url())); await page.locator('#openapi-import-create').click(); const sentPlanRequest = await planRequest; const planPayload = sentPlanRequest.postDataJSON(); expect(planPayload.decisions[0]).toMatchObject({ auth_profile_id: profile.id }); expect(planPayload.decisions[0].security_alternative_index).toEqual(expect.any(Number)); const planResponseReal = await sentPlanRequest.response(); expect(planResponseReal.ok(), await planResponseReal.text()).toBe(true); const planArtifact = await planResponseReal.json(); expect(JSON.stringify(planPayload)).not.toContain(secretValue); // The canonical artifact, onboarding projection and browser state carry the // typed reference only. Secret material remains server-side. const onboarding = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); const browserState = await page.evaluate(() => JSON.stringify({ local: Object.values(localStorage), session: Object.values(sessionStorage), url: location.href, body: document.body.innerText, })); for (const value of [JSON.stringify(previewArtifact), JSON.stringify(planArtifact), JSON.stringify(onboarding), browserState, JSON.stringify(planPayload)]) { expect(value).not.toContain(secretValue); } expect(JSON.stringify(planPayload)).toContain(profile.id); expect(JSON.stringify(planArtifact)).toContain(profile.id); await expect(page.locator('body')).not.toContainText(secretValue); }); test('[P1] AC-4 after atomic apply of multiple Drafts the onboarding explicitly selects one actionable Draft and never auto-publishes', async ({ page }) => { await openOnboarding(page); const first = candidate('onboarding-rates'); const second = candidate('onboarding-currencies'); await routePreview(page, previewResponse([first, second])); await routePlanCreate(page, planResponse([ { key: first.key, name: first.suggested_name, state: 'actionable', action: 'create' }, { key: second.key, name: second.suggested_name, state: 'actionable', action: 'create' }, ])); const outcomes = [ { key: first.key, name: first.suggested_name, outcome: 'created', operation_id: 'op_onboarding_rates', version: 1, reason: null }, { key: second.key, name: second.suggested_name, outcome: 'created', operation_id: 'op_onboarding_currencies', version: 1, reason: null }, ]; let report; const initial = onboardingSnapshot((await getCurrentWorkspace(page)).id); let context; const registrationRequests = []; const selectionRequests = []; let latestOnboarding = initial; await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding(?:\?.*)?$/, async (route) => { if (route.request().method() !== 'GET') return route.fallback(); await route.fulfill({ contentType: 'application/json', body: JSON.stringify(latestOnboarding) }); }); await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/import-context$/, async (route) => { registrationRequests.push(route.request().postDataJSON()); context = importContext(report); const response = registeredResponse(initial, context); latestOnboarding = response; await route.fulfill({ contentType: 'application/json', body: JSON.stringify(response) }); }); await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/select-draft$/, async (route) => { const payload = route.request().postDataJSON(); selectionRequests.push(payload); const response = selectedResponse(latestOnboarding, context, payload.operation_id, payload.operation_version); latestOnboarding = response; await route.fulfill({ contentType: 'application/json', body: JSON.stringify(response) }); }); await routeApply(page, (payload) => { report = applyReport(outcomes, { idempotency_key: payload.idempotency_key, plan_digest: payload.plan_digest, }); return report; }); const publishRequests = []; page.on('request', (request) => { if (/\/publish(?:es)?(?:\?|$)/.test(new URL(request.url()).pathname)) publishRequests.push(request.url()); }); await chooseOpenApiPath(page); await uploadPreview(page); await page.locator('#openapi-import-create').click(); await page.locator('[data-testid="openapi-import-plan-apply"]').click(); // Criterion-defining assertion: an explicit Draft chooser lists the created Drafts. const chooser = page.getByTestId('onboarding-import-draft-choice'); await expect(chooser).toBeVisible(); await expect(chooser).toContainText(first.suggested_name); await expect(chooser).toContainText(second.suggested_name); // The full import report remains available and nothing is auto-published. await expect(page.locator('#openapi-import-result')).toBeVisible(); expect(publishRequests).toEqual([]); await expect.poll(() => registrationRequests.length).toBe(1); expect(registrationRequests[0]).toEqual({ application_idempotency_key: report.idempotency_key, plan_digest: report.plan_digest, expected_revision: initial.revision, }); expect(context.report).toEqual(report); const chosen = page.waitForRequest((request) => request.method() === 'POST' && /\/onboarding\/select-draft$/.test(request.url())); const firstChoice = chooser.locator('.onboarding-import-draft-option-row') .filter({ hasText: first.suggested_name }).getByRole('button'); await firstChoice.click(); const selectedRequest = await chosen; expect(selectedRequest.postDataJSON()).toEqual({ application_idempotency_key: report.idempotency_key, plan_digest: report.plan_digest, operation_id: 'op_onboarding_rates', operation_version: 1, expected_revision: initial.revision + 1, }); }); test('[P1] AC-5 refresh or source correction restores the checklist from authoritative state without duplicating a version and requires a new preview for a changed digest', async ({ page }) => { await login(page); const workspace = await getCurrentWorkspace(page); const pendingReport = applyReport([ { key: 'GET /onboarding-rates', name: 'onboarding_rates', outcome: 'created', operation_id: 'op_imported_rates', version: 1, reason: null }, { key: 'GET /onboarding-blocked', name: 'onboarding_blocked', outcome: 'rejected', operation_id: null, version: null, reason: 'unsupported_xml_body' }, ], { idempotency_key: 'ws_default:apply:openapi_onboarding:v1', counts: { created: 1, versioned: 0, renamed: 0, skipped: 0, excluded: 0, rejected: 1 }, }); const imported = onboardingSnapshot(workspace.id, { revision: 9, operation_id: null, operation_version: null, import_context: importContext(pendingReport), steps: [ { id: 'operation', completed: false, status: 'current', action_code: 'create_operation', reason_code: 'authoritative_evidence_missing' }, { id: 'test', completed: false, status: 'pending', action_code: 'test_operation', reason_code: 'prerequisite_incomplete' }, { id: 'publish_operation', completed: false, status: 'pending', action_code: 'publish_operation', reason_code: 'prerequisite_incomplete' }, { id: 'agent', completed: false, status: 'pending', action_code: 'publish_agent', reason_code: 'prerequisite_incomplete' }, { id: 'key', completed: false, status: 'pending', action_code: 'create_mcp_key', reason_code: 'prerequisite_incomplete' }, { id: 'mcp_connection', completed: false, status: 'pending', action_code: 'connect_mcp_client', reason_code: 'prerequisite_incomplete' }, { id: 'first_call', completed: false, status: 'pending', action_code: 'call_tool', reason_code: 'prerequisite_incomplete' }, ], }); await routeOnboarding(page, imported); await page.goto('/?onboarding=1'); const checklist = page.getByTestId('onboarding-checklist'); if (!(await checklist.isVisible())) await page.getByTestId('onboarding-trigger').click(); await expect(checklist.locator('[aria-current="step"]')).toContainText(localized('Create an Operation', 'Создать операцию')); await expect(page.getByTestId('onboarding-import-context')).toContainText('onboarding_rates'); // A full page refresh must recover the pending report and every outcome // from the authoritative onboarding projection before explicit selection. await page.reload(); const resumed = page.getByTestId('onboarding-checklist'); if (!(await resumed.isVisible())) await page.getByTestId('onboarding-trigger').click(); await expect(resumed.locator('[aria-current="step"]')).toContainText(localized('Create an Operation', 'Создать операцию')); await page.getByTestId('onboarding-openapi-path').click(); await expect(page.locator('#openapi-import-modal')).toBeVisible(); await expect(page.locator('#openapi-import-result')).toBeVisible(); await expect(page.locator('#openapi-import-result .openapi-import-plan-outcome[data-outcome="created"]')).toContainText('onboarding_rates'); await expect(page.locator('#openapi-import-result .openapi-import-plan-outcome[data-outcome="rejected"]')).toContainText('onboarding_blocked'); const restoredChooser = page.getByTestId('onboarding-import-draft-choice'); await expect(restoredChooser).toContainText('onboarding_rates'); await expect(restoredChooser).toContainText(localized( 'This imported item needs review in the wizard.', 'Этот импортированный элемент нужно проверить в мастере.', )); // A changed source digest yields visible 409 recovery and disables Apply // until a fresh preview/confirmation is created. const resumeCandidate = candidate('onboarding-rates'); await page.locator('[data-openapi-close]').last().click(); await page.getByTestId('onboarding-openapi-path').click(); await routePreview(page, previewResponse([resumeCandidate])); await uploadPreview(page); await routePlanCreate(page, planResponse([ { key: resumeCandidate.key, name: resumeCandidate.suggested_name, state: 'actionable', action: 'create' }, ], { plan_reference: 'plan_e2e_onboarding_215_fresh', plan_digest: 'c'.repeat(64) })); await page.locator('#openapi-import-create').click(); await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toContainText(resumeCandidate.suggested_name); await page.route(/\/imports\/openapi\/plans\/[^/]+(?:\?.*)?$/, (route) => route.fulfill({ status: 409, contentType: 'application/json', body: JSON.stringify({ error: { code: 'import_plan_stale', message: 'Import source changed', context: { recovery: 'preview', reason: 'source_digest_changed' } } }), })); await page.locator('[data-testid="openapi-import-plan-reopen"]').click(); await expect(page.locator('[data-testid="openapi-import-plan-stale"]')).toBeVisible(); await expect(page.locator('[data-testid="openapi-import-plan-apply"]')).toBeDisabled(); await expect(page.locator('#openapi-import-status')).not.toHaveText(''); await page.locator('[data-openapi-close]').last().click(); await page.getByTestId('onboarding-openapi-path').click(); await routePreview(page, previewResponse([resumeCandidate], { job_id: 'imp_e2e_onboarding_215_recovered' })); await uploadPreview(page); await routePlanCreate(page, planResponse([ { key: resumeCandidate.key, name: resumeCandidate.suggested_name, state: 'actionable', action: 'create' }, ], { plan_reference: 'plan_e2e_onboarding_215_recovered', plan_digest: 'd'.repeat(64) })); await page.locator('#openapi-import-create').click(); await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toContainText(resumeCandidate.suggested_name); await page.locator('[data-openapi-close]').last().click(); await page.getByTestId('onboarding-openapi-path').click(); const selectionRequests = []; await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/select-draft$/, async (route) => { const payload = route.request().postDataJSON(); selectionRequests.push(payload); if (selectionRequests.length === 1) { await route.fulfill({ status: 409, contentType: 'application/json', body: JSON.stringify({ error: { code: 'onboarding_stale_revision', message: 'Reload required', context: { recovery: 'reload' } } }), }); return; } await route.fulfill({ contentType: 'application/json', body: JSON.stringify(selectedResponse(imported, importContext(pendingReport), payload.operation_id, payload.operation_version)) }); }); const continueButton = page.getByTestId('onboarding-import-draft-choice').getByRole('button').first(); await continueButton.click(); await expect(page.locator('#openapi-import-status')).toContainText(/reload|latest report|state changed|обнов|измен/i); await expect(page.locator('#openapi-import-modal')).toBeVisible(); await expect(continueButton).toBeVisible(); await continueButton.click(); await page.waitForURL(/\/wizard\/\?mode=edit/); expect(selectionRequests).toHaveLength(2); for (const payload of selectionRequests) { expect(payload).toEqual({ application_idempotency_key: pendingReport.idempotency_key, plan_digest: pendingReport.plan_digest, operation_id: 'op_imported_rates', operation_version: 1, expected_revision: imported.revision, }); } }); test('[P1] AC-6 presentation event requests carry only the bounded client contract and make no external calls', async ({ page }) => { const requests = []; page.on('request', (request) => requests.push({ url: request.url(), postData: request.postData() })); const workspace = await openOnboarding(page); const actionable = candidate('onboarding-rates'); await routePreview(page, previewResponse([actionable])); await routePlanCreate(page, planResponse([{ key: actionable.key, name: actionable.suggested_name, state: 'actionable', action: 'create' }])); let report; await routeApply(page, (payload) => { report = applyReport([ { key: actionable.key, name: actionable.suggested_name, outcome: 'created', operation_id: 'op_onboarding_rates', version: 1, reason: null }, ], { idempotency_key: payload.idempotency_key, plan_digest: payload.plan_digest }); return report; }); await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/import-context$/, async (route) => { await route.fulfill({ contentType: 'application/json', body: JSON.stringify(registeredResponse(onboardingSnapshot(workspace.id), importContext(report))) }); }); await chooseOpenApiPath(page); await uploadPreview(page); await page.locator('#openapi-import-create').click(); await page.locator('[data-testid="openapi-import-plan-apply"]').click(); // Presentation requests are client-owned UI signals with a closed payload. const eventPosts = requests.filter((item) => /\/onboarding\/events(?:\?|$)/.test(item.url)); expect(eventPosts.length).toBeGreaterThan(0); eventPosts.forEach((item) => { const payload = JSON.parse(item.postData || '{}'); expect(Object.keys(payload).sort()).toEqual(['event', 'expected_revision', 'idempotency_key']); expect(payload.event).toMatch(/^(started|resumed|dismissed|abandoned)$/); expect(payload.idempotency_key).toMatch(/^ui:/); expect(typeof payload.expected_revision).toBe('number'); expect(JSON.stringify(payload)).not.toContain('openapi: 3.0'); expect(JSON.stringify(payload)).not.toContain(SECRET_CANARY); expect(Object.keys(payload)).not.toContain('user_id'); }); // ...and keep all telemetry local unless the operator opts in (no external egress). const external = requests.filter((item) => !/^https?:\/\/(?:127\.0\.0\.1|localhost)(?::\d+)?\//.test(item.url)); expect(external).toEqual([]); // Server-owned properties for all three funnel milestones, including the // exact keys and raw-data exclusion, are asserted against stored PG rows by // `crates/crank-registry/tests/integration/onboarding_import_selection.rs`. }); for (const locale of ['ru', 'en']) { test(`[P1] AC-7 ${locale} OpenAPI import reaches an authoritative public MCP call after selection reset`, async ({ page, request }) => { test.setTimeout(180_000); await login(page); const workspace = await getCurrentWorkspace(page); // The shared E2E admin workspace may have terminal evidence from an earlier // suite. Reset only its canonical onboarding selection; every milestone // below is recreated through the real import/UI/public endpoint journey. let reset; for (let attempt = 0; attempt < 5; attempt += 1) { const prior = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); reset = await browserJson( page, 'POST', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding/reset-selection`, { expected_revision: prior.revision }, [200, 409], ); if (!reset.error) break; expect(reset.error.code).toBe('onboarding_stale_revision'); } expect(reset.error).toBeUndefined(); const resetProjection = reset.onboarding || reset; expect(resetProjection.status).not.toBe('complete'); expect(resetProjection.import_context).toBeNull(); expect(resetProjection.first_call).toBeNull(); const suffix = uniqueName('openapi_onboarding').replace(/_/g, '-'); const agentSlug = `onboarding-${suffix}`.toLowerCase().replace(/[^a-z0-9-]/g, '-'); await page.goto('/?onboarding_openapi=1&return=%2F'); await expect(page.locator('#openapi-import-modal')).toBeVisible(); await page.evaluate((language) => window.setLang(language), locale); await expect(page.locator('#openapi-import-modal')).toContainText(locale === 'ru' ? 'Импорт OpenAPI' : 'Import OpenAPI'); await uploadPreview(page, representativeOpenApi(suffix)); // Import preview, atomic apply of multiple candidates, and full report are // all exercised against the real import API. await expect(page.locator('#openapi-import-groups')).toContainText(/list.?rates/i); await page.locator('#openapi-import-create').click(); await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); const importContextRequest = page.waitForRequest((r) => r.method() === 'POST' && /\/onboarding\/import-context$/.test(r.url())); await page.locator('[data-testid="openapi-import-plan-apply"]').click(); await expect(page.locator('#openapi-import-result')).toBeVisible(); const registration = await importContextRequest; expect(registration.postDataJSON()).toMatchObject({ application_idempotency_key: expect.any(String), plan_digest: expect.stringMatching(/^[a-f0-9]{64}$/), expected_revision: expect.any(Number), }); expect(await registration.response().then((response) => response.ok())).toBe(true); // Reload while the import context is still pending, then recover the full // immutable report and every selectable/non-selectable outcome from GET. await page.reload(); const checklistAfterReload = page.getByTestId('onboarding-checklist'); if (!await checklistAfterReload.isVisible()) await page.getByTestId('onboarding-trigger').click(); await expect(page.getByTestId('onboarding-import-context')).toContainText(/list.?rates/i); await page.getByTestId('onboarding-openapi-path').click(); await expect(page.locator('#openapi-import-modal')).toBeVisible(); await expect(page.locator('#openapi-import-result')).toBeVisible(); await expect(page.locator('#openapi-import-result .openapi-import-plan-outcomes')).toContainText(/list.?rates/i); const pendingContext = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); expect(pendingContext.import_context.report.outcomes.length).toBeGreaterThan(1); expect(pendingContext.import_context.selected_operation_id).toBeNull(); expect(pendingContext.import_context.selected_operation_version).toBeNull(); expect(pendingContext.import_context.choices).toHaveLength(2); expect(pendingContext.import_context.choices.every((choice) => choice.selectable)).toBe(true); expect(pendingContext.import_context.report.outcomes.map((outcome) => outcome.outcome).sort()).toEqual(['created', 'created']); const selectedChoice = pendingContext.import_context.choices.find((choice) => /list.?rates/i.test(choice.name)); expect(selectedChoice).toBeTruthy(); const chooser = page.getByTestId('onboarding-import-draft-choice'); await expect(chooser).toBeVisible(); const draftChoice = chooser.locator('.onboarding-import-draft-choice-list .onboarding-import-draft-option-row') .filter({ hasText: /list.?rates/i }).getByRole('button'); await expect(draftChoice).toBeVisible(); const selectDraftRequest = page.waitForRequest((r) => r.method() === 'POST' && /\/onboarding\/select-draft$/.test(r.url())); const selectedVersionResponse = page.waitForResponse((r) => r.request().method() === 'GET' && r.url().includes(`/operations/${selectedChoice.operation_id}/versions/${selectedChoice.version}`)); await draftChoice.click(); const selection = await selectDraftRequest; const loadedVersion = await selectedVersionResponse; expect(loadedVersion.ok()).toBe(true); const importedVersion = await loadedVersion.json(); expect(importedVersion.snapshot.target).toMatchObject({ kind: 'rest', method: 'GET', path_template: '/rates', base_url: 'http://127.0.0.1:3310' }); expect(selection.postDataJSON()).toMatchObject({ application_idempotency_key: registration.postDataJSON().application_idempotency_key, plan_digest: registration.postDataJSON().plan_digest, operation_id: expect.any(String), operation_version: expect.any(Number), expected_revision: expect.any(Number), }); await page.waitForFunction(() => window.CrankWizardReady === true && window.currentStep === 1); await expect(page.locator('[data-testid="wizard-protocol-rest"]')).toBeVisible(); const selectedOperationId = selection.postDataJSON().operation_id; const selectedVersion = selection.postDataJSON().operation_version; await expect(page).toHaveURL(new RegExp(`operationId=${selectedOperationId}`)); expect(new URL(page.url()).searchParams.get('operationVersion')).toBe(String(selectedVersion)); await expect(page.locator('#tool-name')).toHaveValue(/list.?rates/i); const operationWrites = []; page.on('request', (r) => { const path = new URL(r.url()).pathname; const createsOperation = r.method() === 'POST' && /\/api\/admin\/workspaces\/[^/]+\/operations$/.test(path); const updatesOperation = r.method() === 'PATCH' && /\/api\/admin\/workspaces\/[^/]+\/operations\/[^/]+$/.test(path); const createsVersion = r.method() === 'POST' && /\/api\/admin\/workspaces\/[^/]+\/operations\/[^/]+\/versions$/.test(path); if (createsOperation || updatesOperation || createsVersion) operationWrites.push({ method: r.method(), url: r.url() }); }); // The selected imported Draft is edited in normal wizard edit mode. A // no-op lifecycle save must retain its exact version without creating a // duplicate, then test and publish that same version. const testedVersions = []; const publishedVersions = []; page.on('request', (r) => { if (r.method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/test-runs`)) { testedVersions.push(r.postDataJSON().version); } if (r.method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/publish`)) { publishedVersions.push(r.postDataJSON().version); } }); const testResponse = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/test-runs`)); await page.evaluate(() => window.CrankWizardShell.doGoToStep(5)); await page.locator('#wizard-run-test').click(); const tested = await testResponse; expect(tested.ok(), await tested.text()).toBe(true); const testResult = await tested.json(); expect(testResult.ok, JSON.stringify(testResult)).toBe(true); await expect.poll(() => testedVersions).toEqual([selectedVersion]); await expect(page.locator('#wizard-test-request-id')).not.toHaveText(''); page.once('dialog', (dialog) => dialog.accept()); const qualityResponse = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes('/operations/analyze-quality')); const publishResponse = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/publish`)); await page.locator('#wizard-publish-operation').click(); const quality = await qualityResponse; expect(quality.ok(), await quality.text()).toBe(true); expect((await publishResponse).ok()).toBe(true); await expect.poll(() => publishedVersions).toEqual([selectedVersion]); expect(operationWrites).toEqual([]); const agentPage = `/agents?onboarding=1&action=create&operationId=${encodeURIComponent(selectedOperationId)}&operationVersion=${selectedVersion}`; await page.goto(agentPage); await expect(page.locator('.drawer')).toHaveClass(/open/); const identityInputs = page.locator('.drawer-section').first().locator('input.form-input'); await identityInputs.nth(0).fill('OpenAPI onboarding Agent'); await identityInputs.nth(1).fill(agentSlug); await page.locator('.drawer-section').first().locator('textarea').fill('Agent created from an explicitly selected OpenAPI Draft.'); await expect(page.locator('.ops-picker-item.selected')).toContainText(/list.?rates/i); const createAgentResponse = page.waitForResponse((r) => r.request().method() === 'POST' && /\/api\/admin\/workspaces\/[^/]+\/agents$/.test(r.url())); const bindAgentResponse = page.waitForResponse((r) => r.request().method() === 'POST' && /\/agents\/[^/]+\/bindings$/.test(r.url())); const publishAgentResponse = page.waitForResponse((r) => r.request().method() === 'POST' && /\/agents\/[^/]+\/publish$/.test(r.url())); await page.locator('.drawer-footer .btn-primary-sm').click(); const agentResponse = await createAgentResponse; expect(agentResponse.ok(), await agentResponse.text()).toBe(true); expect((await bindAgentResponse).ok()).toBe(true); expect((await publishAgentResponse).ok()).toBe(true); const agentId = (await agentResponse.json()).agent_id; await page.goto(`/api-keys?onboarding=1&action=create&agentId=${encodeURIComponent(agentId)}`); await expect(page.locator('#agent-select')).toHaveValue(agentId); await expect(page.locator('#modal-create')).toHaveClass(/open/); await page.locator('#new-key-name').fill(uniqueName('openapi_onboarding_key')); const keyResponsePromise = page.waitForResponse((r) => r.request().method() === 'POST' && /\/platform-api-keys$/.test(r.url())); await page.locator('#modal-confirm-btn').click(); const keyResponse = await keyResponsePromise; expect(keyResponse.ok(), await keyResponse.text()).toBe(true); const keyBody = await keyResponse.json(); const rawKey = await page.locator('#reveal-key-value').textContent(); const keyId = keyBody.api_key.id || keyBody.api_key.api_key.id; const endpoint = await page.locator('#onboarding-connection-clients > code').textContent(); expect(rawKey).toMatch(/^crk_/); await expect(page.locator('body')).not.toContainText(SECRET_CANARY); // An unauthorized public call must not advance the authoritative checklist. const denied = await mcpPost(request, endpoint, SECRET_CANARY, { jsonrpc: '2.0', id: 90, method: 'tools/call', params: { name: 'unavailable', arguments: {} }, }); expect([401, 403]).toContain(denied.status()); const beforeSuccessfulCall = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); expect(beforeSuccessfulCall.status).not.toBe('complete'); expect(beforeSuccessfulCall.first_call).toBeNull(); const initialize = await mcpPost(request, endpoint, rawKey, { jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2025-11-25', capabilities: {}, clientInfo: { name: 'crank-openapi-onboarding-playwright', version: '1.0.0' }, }, }); const initializeBody = await parseMcpResponse(initialize); expect(initialize.ok(), JSON.stringify(initializeBody)).toBe(true); const sessionId = initialize.headers()['mcp-session-id']; expect(sessionId).toBeTruthy(); const initialized = await mcpPost(request, endpoint, rawKey, { jsonrpc: '2.0', method: 'notifications/initialized', params: {}, }, sessionId); expect([200, 202]).toContain(initialized.status()); const listed = await mcpPost(request, endpoint, rawKey, { jsonrpc: '2.0', id: 2, method: 'tools/list', params: {}, }, sessionId); expect(listed.ok()).toBe(true); const listedBody = await parseMcpResponse(listed); const selectedTool = listedBody.result.tools.find((tool) => /list.?rates/i.test(tool.name)); expect(selectedTool).toBeTruthy(); const toolName = selectedTool.name; const called = await mcpPost(request, endpoint, rawKey, { jsonrpc: '2.0', id: 3, method: 'tools/call', params: { name: toolName, arguments: {} }, }, sessionId); expect(called.ok()).toBe(true); const calledBody = await parseMcpResponse(called); expect(calledBody.error).toBeUndefined(); expect(calledBody.result.isError).not.toBe(true); const progress = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); expect(progress.status).toBe('complete'); expect(progress.operation_id).toBe(selectedOperationId); expect(progress.operation_version).toBe(selectedVersion); expect(progress.agent_id).toBe(agentId); expect(progress.platform_api_key_id).toBe(keyId); expect(progress.first_call.agent_id).toBe(agentId); expect(progress.first_call.key_id).toBe(keyId); expect(progress.first_call.operation_id).toBe(selectedOperationId); expect(progress.first_call.operation_version).toBe(selectedVersion); expect(progress.first_call.tool_name).toBe(toolName); expect(progress.first_call.request_id).toBeTruthy(); expect(progress.first_call.trace_id).toMatch(/^[0-9a-f]{32}$/); await page.goto('/'); await expect(page.getByTestId('onboarding-trigger')).toContainText(/7\s*\/\s*7/); await page.getByTestId('onboarding-trigger').click(); await expect(page.getByTestId('onboarding-completion')).toContainText(toolName); await expect(page.getByTestId('onboarding-completion')).toContainText(progress.first_call.request_id); await page.evaluate((language) => window.setLang(language), locale); await expect(page.getByTestId('onboarding-completion')).toBeVisible(); await page.reload(); await expect(page.getByTestId('onboarding-trigger')).toContainText(/7\s*\/\s*7/); }); } });