-
Notifications
You must be signed in to change notification settings - Fork 5
chore: Simple test setup for tools #21
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
egorpavlikhin
wants to merge
1
commit into
main
Choose a base branch
from
egorp/testing
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,51 @@ | ||
| # Integration Tests | ||
|
|
||
| This directory contains integration tests for MCP server tools that test against a real Octopus Deploy instance. | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Install dependencies: | ||
| ```bash | ||
| npm install | ||
| ``` | ||
|
|
||
| 2. Create a `.env` file in the project root with your Octopus Deploy credentials: | ||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| 3. Edit `.env` and add your actual Octopus Deploy instance URL and API key: | ||
| ``` | ||
| OCTOPUS_SERVER_URL=https://your-octopus-instance.octopus.app | ||
| OCTOPUS_API_KEY=API-XXXXXXXXXXXXXXXXXXXXXXXXXX | ||
| TEST_SPACE_NAME=Default | ||
| ``` | ||
|
|
||
| ## Running Tests | ||
|
|
||
| ```bash | ||
| # Run all tests once | ||
| npm test | ||
|
|
||
| # Run tests in watch mode during development | ||
| npm run test:watch | ||
|
|
||
| # Run tests with coverage report | ||
| npm run test:coverage | ||
|
|
||
| # Run tests with UI (optional) | ||
| npx vitest --ui | ||
| ``` | ||
|
|
||
| ## Test Structure | ||
|
|
||
| - **Integration Tests**: Test against real Octopus Deploy API | ||
| - **Environment Validation**: Ensures required credentials are available | ||
|
|
||
| ## Writing New Tests | ||
|
|
||
| 1. Create a new test file following the pattern: `toolName.integration.test.ts` | ||
| 2. Use the shared test utilities from `testSetup.ts` | ||
| 3. Separate the tool registration with the MCP server with the API call handler | ||
| 4. Follow the existing patterns for success and error scenarios | ||
| 5. Register your tool with the mock server before testing |
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,94 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { testConfig, parseToolResponse } from "./testSetup.js"; | ||
| import { listTenantsHandler } from "../listTenants.js"; | ||
|
|
||
| describe("listTenants Integration Tests", () => { | ||
| describe("Successful scenarios", () => { | ||
| it("should list all tenants in the test space", async () => { | ||
| const response = await listTenantsHandler({ | ||
| spaceName: testConfig.testSpaceName, | ||
| }); | ||
|
|
||
| const data = parseToolResponse(response); | ||
|
|
||
| expect(data).toHaveProperty("totalResults"); | ||
| expect(data).toHaveProperty("itemsPerPage"); | ||
| expect(data).toHaveProperty("numberOfPages"); | ||
| expect(data).toHaveProperty("lastPageNumber"); | ||
| expect(data).toHaveProperty("items"); | ||
| expect(Array.isArray(data.items)).toBe(true); | ||
|
|
||
| // Verify tenant structure if any tenants exist | ||
| if (data.items.length > 0) { | ||
| const tenant = data.items[0]; | ||
| expect(tenant).toHaveProperty("id"); | ||
| expect(tenant).toHaveProperty("name"); | ||
| expect(tenant).toHaveProperty("description"); | ||
| expect(tenant).toHaveProperty("projectEnvironments"); | ||
| expect(tenant).toHaveProperty("tenantTags"); | ||
| expect(tenant).toHaveProperty("spaceId"); | ||
| expect(tenant).toHaveProperty("publicUrl"); | ||
| expect(tenant).toHaveProperty("publicUrlInstruction"); | ||
| expect(typeof tenant.id).toBe("string"); | ||
| expect(typeof tenant.name).toBe("string"); | ||
| expect(typeof tenant.publicUrl).toBe("string"); | ||
| } | ||
| }, testConfig.timeout); | ||
|
|
||
| it("should support pagination with skip and take parameters", async () => { | ||
| const response = await listTenantsHandler({ | ||
| spaceName: testConfig.testSpaceName, | ||
| skip: 0, | ||
| take: 5, | ||
| }); | ||
|
|
||
| const data = parseToolResponse(response); | ||
|
|
||
| expect(data).toHaveProperty("totalResults"); | ||
| expect(data).toHaveProperty("itemsPerPage"); | ||
| expect(data.itemsPerPage).toBe(5); | ||
| expect(data.items.length).toBeLessThanOrEqual(5); | ||
| }, testConfig.timeout); | ||
|
|
||
| it("should support filtering by partial name", async () => { | ||
| const response = await listTenantsHandler({ | ||
| spaceName: testConfig.testSpaceName, | ||
| partialName: "test", | ||
| }); | ||
|
|
||
| const data = parseToolResponse(response); | ||
| expect(data).toHaveProperty("items"); | ||
| expect(Array.isArray(data.items)).toBe(true); | ||
|
|
||
| // If results are found, verify they contain the search term | ||
| data.items.forEach((tenant: any) => { | ||
| if (tenant.name) { | ||
| expect(tenant.name.toLowerCase()).toContain("test"); | ||
| } | ||
| }); | ||
| }, testConfig.timeout); | ||
| }); | ||
|
|
||
| describe("Error scenarios", () => { | ||
| it("should throw error for non-existent space", async () => { | ||
| await expect( | ||
| listTenantsHandler({ | ||
| spaceName: "NonExistentSpace123456", | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }, testConfig.timeout); | ||
|
|
||
| it("should handle empty results gracefully", async () => { | ||
| const response = await listTenantsHandler({ | ||
| spaceName: testConfig.testSpaceName, | ||
| partialName: "ThisTenantNameShouldNotExist123456789", | ||
| }); | ||
|
|
||
| const data = parseToolResponse(response); | ||
| expect(data).toHaveProperty("items"); | ||
| expect(Array.isArray(data.items)).toBe(true); | ||
| expect(data.items.length).toBe(0); | ||
| expect(data.totalResults).toBe(0); | ||
| }, testConfig.timeout); | ||
| }); | ||
| }); | ||
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,64 @@ | ||
| import { beforeAll, expect } from "vitest"; | ||
| import { config } from "dotenv"; | ||
|
|
||
| // Load environment variables from .env files | ||
| config(); | ||
|
|
||
| export const testConfig = { | ||
| octopusServerUrl: process.env.OCTOPUS_SERVER_URL || process.env.CLI_SERVER_URL, | ||
| octopusApiKey: process.env.OCTOPUS_API_KEY || process.env.CLI_API_KEY, | ||
| testSpaceName: process.env.TEST_SPACE_NAME || "Default", | ||
| timeout: 30000, // 30 seconds | ||
| }; | ||
|
|
||
| export function validateTestEnvironment(): void { | ||
| const missing: string[] = []; | ||
|
|
||
| if (!testConfig.octopusServerUrl) { | ||
| missing.push("OCTOPUS_SERVER_URL (or CLI_SERVER_URL)"); | ||
| } | ||
|
|
||
| if (!testConfig.octopusApiKey) { | ||
| missing.push("OCTOPUS_API_KEY (or CLI_API_KEY)"); | ||
| } | ||
|
|
||
| if (missing.length > 0) { | ||
| throw new Error( | ||
| `Missing required environment variables for integration tests: ${missing.join(", ")}. ` + | ||
| "Please set these variables or create a .env file in the project root." | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to create a standardized error test case | ||
| */ | ||
| export function createErrorTestCase( | ||
| description: string, | ||
| setupFn: () => void, | ||
| expectedErrorMessage?: string | ||
| ) { | ||
| return { | ||
| description, | ||
| setup: setupFn, | ||
| expectedErrorMessage, | ||
| }; | ||
| } | ||
|
|
||
| export function assertToolResponse(response: any): void { | ||
| expect(response).toBeDefined(); | ||
| expect(response.content).toBeDefined(); | ||
| expect(Array.isArray(response.content)).toBe(true); | ||
| expect(response.content.length).toBeGreaterThan(0); | ||
| expect(response.content[0].type).toBe("text"); | ||
| expect(response.content[0].text).toBeDefined(); | ||
| } | ||
|
|
||
| export function parseToolResponse(response: any): any { | ||
| assertToolResponse(response); | ||
| return JSON.parse(response.content[0].text); | ||
| } | ||
|
|
||
| beforeAll(() => { | ||
| validateTestEnvironment(); | ||
| }); |
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
Oops, something went wrong.
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.
Not too much value in these tests but they do make it easier to make changes and quickly checks that things are not completely broken.