-
Notifications
You must be signed in to change notification settings - Fork 20
test(cli): add reusable Vitest e2e helpers #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yada
wants to merge
2
commits into
reshaprio:main
Choose a base branch
from
yada:cli-e2e-helpers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright The Reshapr Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { runCli, runCliExpectSuccess } from './cli-runner.js'; | ||
|
|
||
| interface ServiceRef { | ||
| id: string; | ||
| name: string; | ||
| version: string; | ||
| } | ||
|
|
||
| interface SecretRef { | ||
| id: string; | ||
| name: string; | ||
| } | ||
|
|
||
| async function listServices(): Promise<ServiceRef[]> { | ||
| const result = await runCli('service', 'list', '-o', 'json'); | ||
| if (result.exitCode !== 0 || !result.stdout.trim()) { | ||
| return []; | ||
| } | ||
| return JSON.parse(result.stdout) as ServiceRef[]; | ||
| } | ||
|
|
||
| async function listSecrets(): Promise<SecretRef[]> { | ||
| const result = await runCli('secret', 'list', '-o', 'json'); | ||
| if (result.exitCode !== 0 || !result.stdout.trim()) { | ||
| return []; | ||
| } | ||
| return JSON.parse(result.stdout) as SecretRef[]; | ||
| } | ||
|
|
||
| export async function deleteServiceIfPresent(serviceId: string | undefined): Promise<void> { | ||
| if (!serviceId) { | ||
| return; | ||
| } | ||
| await runCli('service', 'delete', serviceId, '-f'); | ||
| } | ||
|
|
||
| export async function deleteSecretIfPresent(secretId: string | undefined): Promise<void> { | ||
| if (!secretId) { | ||
| return; | ||
| } | ||
| await runCli('secret', 'delete', secretId); | ||
| } | ||
|
|
||
| export async function deleteServicesByNameVersion(name: string, version: string): Promise<void> { | ||
| const services = await listServices(); | ||
| for (const service of services) { | ||
| if (service.name === name && service.version === version) { | ||
| await runCliExpectSuccess('service', 'delete', service.id, '-f'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function deleteSecretsByName(name: string): Promise<void> { | ||
| const secrets = await listSecrets(); | ||
| for (const secret of secrets) { | ||
| if (secret.name === name) { | ||
| await runCliExpectSuccess('secret', 'delete', secret.id); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Copyright The Reshapr Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { GATEWAY_URL } from './setup.js'; | ||
|
|
||
| export interface McpTool { | ||
| name: string; | ||
| } | ||
|
|
||
| export interface McpResponse { | ||
| jsonrpc: string; | ||
| id: number; | ||
| result?: any; | ||
| error?: { | ||
| code: number; | ||
| message: string; | ||
| data?: any; | ||
| }; | ||
| } | ||
|
|
||
| interface ToolExpectation { | ||
| exact?: number; | ||
| minimum?: number; | ||
| include?: string[]; | ||
| exclude?: string[]; | ||
| } | ||
|
|
||
| const MCP_PROTOCOL_VERSION = '2025-06-18'; | ||
| const E2E_ORG = 'e2eorg'; | ||
|
|
||
| function sleep(ms: number): Promise<void> { | ||
| return new Promise(resolve => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| export function mcpServiceName(name: string): string { | ||
| return name.replaceAll(' ', '+'); | ||
| } | ||
|
|
||
| export function mcpUrl(serviceName: string, serviceVersion: string): string { | ||
| return `${GATEWAY_URL}/mcp/${E2E_ORG}/${mcpServiceName(serviceName)}/${serviceVersion}`; | ||
| } | ||
|
|
||
| export async function mcpRequest(url: string, method: string, params: any = {}, sessionId?: string): Promise<McpResponse> { | ||
| const headers: Record<string, string> = { | ||
| 'Content-Type': 'application/json', | ||
| }; | ||
| if (sessionId) { | ||
| headers['MCP-Session-Id'] = sessionId; | ||
| headers['MCP-Protocol-Version'] = MCP_PROTOCOL_VERSION; | ||
| } | ||
|
|
||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: Date.now(), | ||
| method, | ||
| params, | ||
| }), | ||
| }); | ||
|
|
||
| const body = await response.text(); | ||
| if (!response.ok) { | ||
| throw new Error(`MCP ${method} failed with HTTP ${response.status}: ${body}`); | ||
| } | ||
| return JSON.parse(body) as McpResponse; | ||
| } | ||
|
|
||
| export async function initializeMcp(url: string, capabilities: Record<string, any> = {}): Promise<{ body: McpResponse; sessionId?: string }> { | ||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: Date.now(), | ||
| method: 'initialize', | ||
| params: { | ||
| protocolVersion: MCP_PROTOCOL_VERSION, | ||
| capabilities, | ||
| clientInfo: { name: 'reshapr-cli-e2e', version: '0.0.1' }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| const bodyText = await response.text(); | ||
| if (!response.ok) { | ||
| throw new Error(`MCP initialize failed with HTTP ${response.status}: ${bodyText}`); | ||
| } | ||
|
|
||
| return { | ||
| body: JSON.parse(bodyText) as McpResponse, | ||
| sessionId: response.headers.get('mcp-session-id') ?? undefined, | ||
| }; | ||
| } | ||
|
|
||
| export async function listMcpTools(url: string): Promise<McpTool[]> { | ||
| const body = await mcpRequest(url, 'tools/list', {}); | ||
| if (body.error) { | ||
| throw new Error(`MCP tools/list returned error: ${JSON.stringify(body.error)}`); | ||
| } | ||
| const tools = body.result?.tools; | ||
| if (!Array.isArray(tools)) { | ||
| throw new Error(`MCP tools/list did not return result.tools[]: ${JSON.stringify(body)}`); | ||
| } | ||
| return tools as McpTool[]; | ||
| } | ||
|
|
||
| export async function waitForMcpTools(url: string, expectation: ToolExpectation): Promise<McpTool[]> { | ||
| const deadline = Date.now() + 60_000; | ||
| let lastError: unknown; | ||
|
|
||
| while (Date.now() < deadline) { | ||
| try { | ||
| const tools = await listMcpTools(url); | ||
| assertToolExpectation(tools, expectation); | ||
| return tools; | ||
| } catch (error) { | ||
| lastError = error; | ||
| await sleep(2_000); | ||
| } | ||
| } | ||
|
|
||
| throw lastError instanceof Error ? lastError : new Error('MCP tools did not satisfy expectation'); | ||
| } | ||
|
|
||
| function assertToolExpectation(tools: McpTool[], expectation: ToolExpectation): void { | ||
| const names = new Set(tools.map(tool => tool.name)); | ||
| if (expectation.exact !== undefined && tools.length !== expectation.exact) { | ||
| throw new Error(`Expected exactly ${expectation.exact} MCP tools, got ${tools.length}`); | ||
| } | ||
| if (expectation.minimum !== undefined && tools.length < expectation.minimum) { | ||
| throw new Error(`Expected at least ${expectation.minimum} MCP tools, got ${tools.length}`); | ||
| } | ||
| for (const name of expectation.include ?? []) { | ||
| if (!names.has(name)) { | ||
| throw new Error(`Expected MCP tool '${name}' to be present`); | ||
| } | ||
| } | ||
| for (const name of expectation.exclude ?? []) { | ||
| if (names.has(name)) { | ||
| throw new Error(`Expected MCP tool '${name}' to be absent`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function callMcpTool(url: string, name: string, args: Record<string, any>, sessionId?: string): Promise<McpResponse> { | ||
| return mcpRequest(url, 'tools/call', { name, arguments: args }, sessionId); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,4 +123,3 @@ export async function stopInfrastructure(): Promise<void> { | |
| }); | ||
| console.log('✅ Infrastructure stopped.'); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it an helper to run the CLI command with
-o jsonflag? If yes, then we should add it explicitly and not set expectations on the caller to have added-o jsonto the methodargs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 The helper name implies that it runs a JSON-producing CLI command, so callers should not have to remember to pass
-o json. I’ll update it sorunCliJson()appends-o jsonitself and only accepts the logical CLI command arguments.