import assert from 'node:assert/strict' import path from 'node:path' import { readFile } from 'node:fs/promises' const root = process.cwd() const storePath = path.join(root, 'app/lib/mcpServerStore.ts') const brokerStorePath = path.join(root, 'app/lib/mcpBrokerStore.ts') const brokerClientPath = path.join(root, 'app/lib/mcpBrokerClient.ts') const manifestPath = path.join(root, 'app/lib/sandboxMcpManifest.ts') const privilegedFilesPath = path.join(root, 'app/lib/sandboxPrivilegedFiles.ts') const routePath = path.join(root, 'app/api/mcp/route.ts') const uploadRoutePath = path.join(root, 'app/api/mcp/upload/route.ts') const preflightRoutePath = path.join(root, 'app/api/mcp/preflight/route.ts') const healthRoutePath = path.join(root, 'app/api/mcp/health/route.ts') const registryRoutePath = path.join(root, 'app/api/mcp/registry/route.ts') const registriesRoutePath = path.join(root, 'app/api/mcp/registries/route.ts') const registriesAssistRoutePath = path.join(root, 'app/api/mcp/registries/assist/route.ts') const installAssistRoutePath = path.join(root, 'app/api/mcp/install-assist/route.ts') const registryStorePath = path.join(root, 'app/lib/mcpRegistryStore.ts') const brokerCapabilitiesRoutePath = path.join(root, 'app/api/mcp/broker/capabilities/route.ts') const brokerCallRoutePath = path.join(root, 'app/api/mcp/broker/call/route.ts') const sandboxMcpRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/mcp/route.ts') const brokerUrlPath = path.join(root, 'app/lib/mcpBrokerUrl.ts') const preflightLibPath = path.join(root, 'app/lib/mcpPreflight.ts') const preflightRepairLibPath = path.join(root, 'app/lib/mcpPreflightRepair.ts') const middlewarePath = path.join(root, 'middleware.ts') const panelPath = path.join(root, 'app/components/McpConfigurationPanel.tsx') const helpPath = path.join(root, 'app/components/HelpPanel.tsx') const sidebarPath = path.join(root, 'app/components/Sidebar.tsx') const sandboxListPath = path.join(root, 'app/components/SandboxList.tsx') const pagePath = path.join(root, 'app/page.tsx') const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privilegedFilesSource, routeSource, uploadRouteSource, preflightRouteSource, healthRouteSource, registryRouteSource, registriesRouteSource, registriesAssistRouteSource, installAssistRouteSource, registryStoreSource, brokerCapabilitiesRouteSource, brokerCallRouteSource, sandboxMcpRouteSource, brokerUrlSource, preflightLibSource, preflightRepairLibSource, middlewareSource, panelSource, helpSource, sidebarSource, sandboxListSource, pageSource] = await Promise.all([ readFile(storePath, 'utf8'), readFile(brokerStorePath, 'utf8'), readFile(brokerClientPath, 'utf8'), readFile(manifestPath, 'utf8'), readFile(privilegedFilesPath, 'utf8'), readFile(routePath, 'utf8'), readFile(uploadRoutePath, 'utf8'), readFile(preflightRoutePath, 'utf8'), readFile(healthRoutePath, 'utf8'), readFile(registryRoutePath, 'utf8'), readFile(registriesRoutePath, 'utf8'), readFile(registriesAssistRoutePath, 'utf8'), readFile(installAssistRoutePath, 'utf8'), readFile(registryStorePath, 'utf8'), readFile(brokerCapabilitiesRoutePath, 'utf8'), readFile(brokerCallRoutePath, 'utf8'), readFile(sandboxMcpRoutePath, 'utf8'), readFile(brokerUrlPath, 'utf8'), readFile(preflightLibPath, 'utf8'), readFile(preflightRepairLibPath, 'utf8'), readFile(middlewarePath, 'utf8'), readFile(panelPath, 'utf8'), readFile(helpPath, 'utf8'), readFile(sidebarPath, 'utf8'), readFile(sandboxListPath, 'utf8'), readFile(pagePath, 'utf8'), ]) assert.match(storeSource, /export const MCP_SERVER_CATALOG/, 'MCP store must expose an install catalog') assert.match(storeSource, /blendermcp\.org/, 'MCP catalog must include the requested BlenderMCP site') assert.match(storeSource, /args:\s*\["blender-mcp"\]/, 'MCP catalog must install BlenderMCP via uvx blender-mcp') assert.match(storeSource, /BASELINE_MCP_SERVER_IDS = \["memory"\]/, 'MCP dashboard should install Memory as the baseline test server') assert.match(storeSource, /ensureBaselineMcpServers/, 'MCP store must seed baseline servers into dashboard state') assert.match(storeSource, /mcp-servers\.json/, 'MCP installs must persist to dashboard state') assert.match(storeSource, /accessMode/, 'MCP installs must persist sandbox access mode') assert.match(storeSource, /allowedSandboxIds/, 'MCP installs must persist allowed sandbox lists') assert.match(storeSource, /existing\?\.accessMode \|\| "disabled"/, 'MCP server access should default to disabled for sandboxes') assert.match(storeSource, /export function buildMcpClientConfig/, 'MCP store must export client config JSON') assert.match(storeSource, /mcpServers/, 'MCP client config must use the common mcpServers shape') assert.match(brokerStoreSource, /mcp-broker-sessions\.json/, 'MCP broker sessions must persist server-side') assert.match(brokerStoreSource, /verifySandboxMcpBrokerToken/, 'MCP broker must verify sandbox tokens server-side') assert.match(brokerStoreSource, /listAllowedBrokerServers/, 'MCP broker must filter servers by access policy') assert.match(brokerClientSource, /@modelcontextprotocol\/sdk\/client\/index\.js/, 'MCP broker must use the official SDK client') assert.match(brokerClientSource, /StdioClientTransport/, 'MCP broker must support stdio servers') assert.match(brokerClientSource, /StreamableHTTPClientTransport/, 'MCP broker must support streamable HTTP servers') assert.match(brokerClientSource, /callBrokerServerTool/, 'MCP broker must forward allowed tool calls') assert.match(manifestSource, /openshell_control_mcp\.md/, 'MCP sandbox manifest must use the requested filename') assert.match(manifestSource, /syncSandboxMcpManifest/, 'MCP sandbox manifest must be syncable into a sandbox') assert.match(manifestSource, /writeSandboxFilePrivileged/, 'MCP sandbox manifest must use a privileged write path for root-owned /sandbox mounts') assert.match(privilegedFilesSource, /kubectl/, 'privileged sandbox file writes must use the OpenShell cluster control path') assert.doesNotMatch(manifestSource, /allowedServers|renderServer|commandLine/, 'MCP sandbox handoff must not disclose server inventory or launch specs') assert.match(manifestSource, /control plane enforces/, 'MCP sandbox handoff must explain broker-side enforcement') assert.match(brokerCapabilitiesRouteSource, /listAllowedBrokerServers/, 'MCP broker capabilities must only list allowed servers') assert.match(brokerCapabilitiesRouteSource, /listBrokerServerTools/, 'MCP broker capabilities must inspect allowed server tools') assert.match(brokerCallRouteSource, /Requested MCP capability is unavailable/, 'MCP broker denied calls must not disclose blocked server names') assert.match(brokerCallRouteSource, /callBrokerServerTool/, 'MCP broker call route must forward allowed calls') assert.match(sandboxMcpRouteSource, /export async function POST/, 'Sandbox MCP route must write the broker handoff') assert.match(sandboxMcpRouteSource, /action === "revoke"/, 'Sandbox MCP route must revoke broker handoff when MCP access is removed') assert.match(sandboxMcpRouteSource, /syncBrokerNetworkAccess/, 'Sandbox MCP route must synchronize broker network access when MCP is enabled') assert.match(sandboxMcpRouteSource, /revokeBrokerNetworkAccess/, 'Sandbox MCP route must remove broker network access when MCP is revoked') assert.match(sandboxMcpRouteSource, /export async function GET/, 'Sandbox MCP route must preview the broker handoff') assert.match(sandboxMcpRouteSource, /brokerBaseUrlForSandbox/, 'Sandbox MCP broker handoff must build a sandbox-routable broker URL') assert.match(brokerUrlSource, /discoverOpenShellDockerGateway/, 'Sandbox MCP broker handoff must discover the active OpenShell network gateway') assert.match(brokerUrlSource, /LOCAL_HOSTNAMES/, 'Sandbox MCP broker handoff must rewrite local-only browser origins') assert.match(brokerUrlSource, /discoverSandboxProxyOrigin/, 'Sandbox MCP broker handoff must discover each sandbox proxy endpoint') assert.match(brokerUrlSource, /HTTP_PROXY/, 'Sandbox MCP broker handoff must derive the proxy endpoint from sandbox environment') assert.match(brokerUrlSource, /FALLBACK_SANDBOX_PROXY_ORIGIN/, 'Sandbox MCP broker handoff may keep a fallback for older OpenShell layouts') assert.match(middlewareSource, /\/api\/mcp\/broker/, 'MCP broker endpoints must bypass dashboard cookie auth and rely on broker token auth') assert.match(routeSource, /export async function GET/, 'MCP API must list server configuration') assert.match(routeSource, /export async function POST/, 'MCP API must install and update server configuration') assert.match(routeSource, /installMcpServer/, 'MCP API must support installing servers') assert.match(routeSource, /uninstallMcpServer/, 'MCP API must support removing servers') assert.match(routeSource, /updateMcpServerAccess/, 'MCP API must support access policy updates') assert.match(uploadRouteSource, /request\.formData\(\)/, 'MCP upload API must accept multipart server bundles') assert.match(uploadRouteSource, /writeDirectoryUpload/, 'MCP upload API must support directory uploads') assert.match(uploadRouteSource, /writeArchiveUpload/, 'MCP upload API must support archive uploads') assert.match(uploadRouteSource, /bootstrapUploadedServer/, 'MCP upload API must bootstrap uploaded server dependencies') assert.match(uploadRouteSource, /requirements\.txt/, 'MCP upload API must install Python requirements when present') assert.match(uploadRouteSource, /pyproject\.toml/, 'MCP upload API must install Python projects when present') assert.match(uploadRouteSource, /package\.json/, 'MCP upload API must install Node dependencies when present') assert.match(uploadRouteSource, /\.venv/, 'MCP upload API must isolate Python uploads in a per-server virtualenv') assert.match(uploadRouteSource, /entryMode/, 'MCP upload API must support non-file launch modes') assert.match(uploadRouteSource, /python-module/, 'MCP upload API must support Python module launch') assert.match(uploadRouteSource, /console-script/, 'MCP upload API must support installed console script launch') assert.match(uploadRouteSource, /resolveProjectRoot/, 'MCP upload API must find project manifests inside uploaded directories') assert.match(uploadRouteSource, /preflightMcpServer/, 'MCP upload API must preflight uploaded server bundles') assert.match(uploadRouteSource, /repairUploadedMcpServerWithLlm/, 'MCP upload API must attempt bounded LLM repair when uploaded server preflight fails') assert.match(uploadRouteSource, /form\.get\("sandboxId"\)/, 'MCP upload API must accept a sandbox id so repair can mirror that sandbox LLM route') assert.match(uploadRouteSource, /preflight-failed/, 'MCP upload API must mark failed preflight installs') assert.match(uploadRouteSource, /installMcpServer/, 'MCP upload API must install uploaded server bundles') assert.match(preflightRouteSource, /export async function POST/, 'MCP preflight API must expose a POST check endpoint') assert.match(preflightRouteSource, /preflightMcpServer/, 'MCP preflight API must call the shared preflight helper') assert.match(preflightLibSource, /listBrokerServerTools/, 'MCP preflight must validate servers through SDK tool discovery') assert.match(preflightLibSource, /attempted relative import/, 'MCP preflight assistant must recognize Python package launch failures') assert.match(preflightLibSource, /FastMCP/, 'MCP preflight assistant must recognize MCP SDK compatibility failures') assert.match(preflightRepairLibSource, /getSandboxInferenceConfig/, 'MCP preflight repair must reuse the selected sandbox inference profile when available') assert.match(preflightRepairLibSource, /chat\/completions/, 'MCP preflight repair must call an OpenAI-compatible chat endpoint') assert.match(preflightRepairLibSource, /response_format: \{ type: "json_object" \}/, 'MCP preflight repair must ask the model for structured JSON') assert.match(preflightRepairLibSource, /repair path must stay inside the uploaded server/, 'MCP preflight repair must confine edits to the uploaded server') assert.match(preflightRepairLibSource, /provided context/, 'MCP preflight repair must only edit files shown to the model') assert.match(healthRouteSource, /listBrokerServerTools/, 'MCP health API must inspect server tools through the broker client') assert.match(healthRouteSource, /enabledServers/, 'MCP health API must check enabled MCP servers') assert.match(registryRouteSource, /registry\.modelcontextprotocol\.io/, 'MCP registry search should default to the official registry') assert.match(registryRouteSource, /\/v0\/servers/, 'MCP registry search should call the registry server list endpoint') assert.match(registryRouteSource, /normalizeRegistryEntry/, 'MCP registry search should normalize results into installable entries') assert.match(registryRouteSource, /packageInstall/, 'MCP registry search should support package-backed stdio installs') assert.match(registryRouteSource, /remoteInstall/, 'MCP registry search should support remote HTTP installs') assert.match(registryRouteSource, /searchParams\.get\("baseUrl"\)/, 'MCP registry search must accept a selected registry base URL') assert.match(registryStoreSource, /mcp-registries\.json/, 'MCP registries must persist to dashboard state') assert.match(registryStoreSource, /DEFAULT_MCP_REGISTRY/, 'MCP registries should start with the official registry before user changes') assert.match(registriesRouteSource, /listMcpRegistries/, 'MCP registries API must list registries') assert.match(registriesRouteSource, /saveMcpRegistry/, 'MCP registries API must save registries') assert.match(registriesRouteSource, /deleteMcpRegistry/, 'MCP registries API must delete registries') assert.match(registriesAssistRouteSource, /chat\/completions/, 'MCP registry assistant must use the running OpenAI-compatible LLM endpoint') assert.match(registriesAssistRouteSource, /response_format: \{ type: "json_object" \}/, 'MCP registry assistant must request structured JSON') assert.match(installAssistRouteSource, /chat\/completions/, 'MCP install wizard assistant must use the running OpenAI-compatible LLM endpoint') assert.match(installAssistRouteSource, /uploadRuntime/, 'MCP install wizard assistant must draft upload metadata') assert.match(installAssistRouteSource, /response_format: \{ type: "json_object" \}/, 'MCP install wizard assistant must request structured JSON') assert.match(panelSource, /Install Server Command/, 'MCP page must expose a server command install action') assert.match(panelSource, /Server Install Wizard/, 'MCP page must present custom installation as a wizard') assert.match(panelSource, /Upload and Preflight/, 'MCP custom server accordion must support uploaded server bundles') assert.match(panelSource, /Choose Directory/, 'MCP custom server upload must accept directories') assert.match(panelSource, /Choose Archive/, 'MCP custom server upload must accept archives') assert.match(panelSource, /\/api\/mcp\/upload/, 'MCP custom server upload must call the upload API') assert.match(panelSource, /dependencyInstall/, 'MCP custom server upload must surface dependency bootstrap results') assert.match(panelSource, /uploadEntrypoint/, 'MCP custom server upload must collect an entrypoint') assert.match(panelSource, /uploadEntryMode/, 'MCP custom server upload must collect a launch mode') assert.match(panelSource, /Python module/, 'MCP custom server upload must expose Python module launch mode') assert.match(panelSource, /Console script/, 'MCP custom server upload must expose console script launch mode') assert.match(panelSource, /uploadRepair/, 'MCP custom server upload must expose LLM repair controls') assert.match(panelSource, /repairSandboxId/, 'MCP custom server upload must let operators pick a sandbox LLM route for repair') assert.match(panelSource, /LLM repair/, 'MCP custom server upload must summarize LLM-assisted repair results') assert.match(panelSource, /FieldHint/, 'MCP server install wizard fields must include tooltips') assert.match(panelSource, /wizardStep/, 'MCP server install wizard must expose install workflow steps') assert.match(panelSource, /assistInstall/, 'MCP server install wizard must let the LLM draft install fields') assert.match(panelSource, /Assist Install/, 'MCP server install wizard must expose an assist install action') assert.match(panelSource, /Back/, 'MCP server install wizard must expose back navigation') assert.match(panelSource, /Next/, 'MCP server install wizard must expose next navigation') assert.match(panelSource, /Upload and Preflight/, 'MCP server install wizard must expose upload preflight as the upload action') assert.match(panelSource, /preflightServer/, 'MCP page must let operators preflight installed servers') assert.match(panelSource, /Preflight/, 'MCP page must show preflight controls and results') assert.match(panelSource, /serverPayloadFromJson/, 'MCP page must parse edited server JSON') assert.match(panelSource, /startEditingServer/, 'MCP installed server list must expose edit state') assert.match(panelSource, /Server JSON/, 'MCP installed server editor must render an inline JSON file editor') assert.match(panelSource, /MCP Repo Search and Preconfigured Servers/, 'MCP page must combine repo search and preconfigured servers') assert.match(panelSource, /Available Registries/, 'MCP page must display available registries') assert.match(panelSource, /Add an MCP Registry/, 'MCP page must show an add-registry empty state') assert.match(panelSource, /Draft With LLM/, 'MCP page must let the running LLM draft registry fields') assert.match(panelSource, /Delete Selected Registry/, 'MCP page must allow deleting registries') assert.match(panelSource, /selectedRegistryId/, 'MCP page must track and highlight the selected registry') assert.match(panelSource, /\/api\/mcp\/registry/, 'MCP page must query the registry search API') assert.match(panelSource, /\/api\/mcp\/registries/, 'MCP page must manage registry sources through the registry API') assert.match(panelSource, /const REGISTRY_PAGE_SIZE = 4/, 'MCP registry search should page results four at a time') assert.match(panelSource, /setRegistryResults\(\[\]\)/, 'MCP registry search should clear stale results before a new search') assert.match(panelSource, /pagedRegistryResults/, 'MCP registry search should render paged results instead of the full result set') assert.match(panelSource, /Preconfigured Servers/, 'MCP page should label catalog presets as preconfigured servers') assert.match(panelSource, /aria-expanded=\{catalogOpen\}/, 'MCP preconfigured servers should render as an accordion') assert.match(panelSource, /aria-expanded=\{customOpen\}/, 'MCP custom server form should render as an accordion') assert.match(panelSource, /MCP Security/, 'MCP page must expose sandbox access controls') assert.match(panelSource, /openSecurityServers/, 'MCP security must render installed servers as sub accordions') assert.match(panelSource, /aria-expanded=\{securityOpen\}/, 'MCP security must render as an accordion') assert.match(panelSource, /Allow All/, 'MCP security must support allow-all access') assert.match(panelSource, /Allow Only/, 'MCP security must support allow-list access') assert.match(panelSource, /syncSandboxBrokerConfig/, 'MCP configuration grants must sync the sandbox broker config automatically') assert.match(panelSource, /\/api\/sandbox\/\$\{encodeURIComponent\(sandbox\.id\)\}\/mcp/, 'MCP configuration grants must call the sandbox MCP sync endpoint') assert.match(panelSource, /allowed \|\| hasOtherMcpAccess \? "sync" : "revoke"/, 'MCP configuration revokes must preserve broker config when other MCP access remains') assert.match(panelSource, /syncAccessModeChange/, 'MCP availability changes must synchronize sandbox broker policy') assert.match(panelSource, /Setup Guide/, 'MCP catalog cards should link out to app setup guides') assert.match(panelSource, /Client JSON/, 'MCP page must surface generated client JSON') assert.match(helpSource, /MCP Server Health/, 'Help must expose MCP server health checks') assert.match(helpSource, /\/api\/mcp\/health/, 'Help MCP health check must call the health API') assert.match(helpSource, /Refresh MCP Health/, 'Help MCP health check must be refreshable') assert.match(sandboxListSource, /Allowed MCP Server Access/, 'Sandbox page must expose allowed MCP server access accordion') assert.match(sandboxListSource, /Sync Broker Config/, 'Sandbox MCP access controls must sync broker config') assert.match(sandboxListSource, /hasOtherMcpAccess/, 'Sandbox MCP access controls must revoke broker config when no MCP servers remain') assert.match(sandboxListSource, /\/sandbox\/openshell_control_mcp\.md/, 'Sandbox MCP access controls must name the manifest path') assert.match(sandboxListSource, /sandboxCanAccessMcpServer/, 'Sandbox page must derive MCP access per sandbox') assert.match(sandboxListSource, /MCP access allowed/, 'Sandbox cards must show a lit MCP access indicator') assert.match(sandboxListSource, /Revoke/, 'Sandbox MCP access controls must include revoke') assert.match(sidebarSource, /onMcpClick/, 'Sidebar must include MCP navigation callback') assert.match(sidebarSource, /activeView === 'mcp'/, 'Sidebar must mark MCP view active') assert.match(pageSource, /McpConfigurationPanel/, 'Dashboard must render MCP configuration panel') assert.match(pageSource, /setActiveView\('mcp'\)/, 'Dashboard must switch to MCP view') console.log('mcp-configuration-page-check: PASS MCP dashboard assertions')