Files
cc-switch/tests/hooks/useMcpValidation.test.tsx

170 lines
5.4 KiB
TypeScript
Raw Permalink Normal View History

test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
import { renderHook } from "@testing-library/react";
import { describe, it, expect, beforeEach, vi } from "vitest";
import { useMcpValidation } from "@/components/mcp/useMcpValidation";
const validateTomlMock = vi.hoisted(() => vi.fn());
const tomlToMcpServerMock = vi.hoisted(() => vi.fn());
vi.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key: string) => key,
}),
}));
vi.mock("@/utils/tomlUtils", () => ({
validateToml: (...args: unknown[]) => validateTomlMock(...args),
tomlToMcpServer: (...args: unknown[]) => tomlToMcpServerMock(...args),
}));
describe("useMcpValidation", () => {
beforeEach(() => {
validateTomlMock.mockReset();
tomlToMcpServerMock.mockReset();
validateTomlMock.mockReturnValue("");
});
test: update test suites to match component refactoring Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
2025-11-21 11:12:06 +08:00
const getHookResult = () =>
renderHook(() => useMcpValidation()).result.current;
test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
describe("validateJson", () => {
it("returns empty string for blank text", () => {
const { validateJson } = getHookResult();
expect(validateJson(" ")).toBe("");
});
it("returns error key when JSON parsing fails", () => {
const { validateJson } = getHookResult();
expect(validateJson("{ invalid")).toBe("mcp.error.jsonInvalid");
});
it("returns error key when parsed value is not an object", () => {
const { validateJson } = getHookResult();
expect(validateJson('"string"')).toBe("mcp.error.jsonInvalid");
expect(validateJson("[]")).toBe("mcp.error.jsonInvalid");
});
it("accepts valid object payload", () => {
const { validateJson } = getHookResult();
expect(validateJson('{"id":"demo"}')).toBe("");
});
});
describe("formatTomlError", () => {
it("maps mustBeObject and parseError to i18n key", () => {
const { formatTomlError } = getHookResult();
expect(formatTomlError("mustBeObject")).toBe("mcp.error.tomlInvalid");
expect(formatTomlError("parseError")).toBe("mcp.error.tomlInvalid");
});
it("appends error message when details provided", () => {
const { formatTomlError } = getHookResult();
expect(formatTomlError("unknown")).toBe("mcp.error.tomlInvalid: unknown");
});
});
describe("validateTomlConfig", () => {
it("propagates errors returned by validateToml", () => {
validateTomlMock.mockReturnValue("parse-error-detail");
const { validateTomlConfig } = getHookResult();
test: update test suites to match component refactoring Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
2025-11-21 11:12:06 +08:00
expect(validateTomlConfig("foo")).toBe(
"mcp.error.tomlInvalid: parse-error-detail",
);
test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
expect(tomlToMcpServerMock).not.toHaveBeenCalled();
});
it("returns command required when stdio server missing command", () => {
tomlToMcpServerMock.mockReturnValue({
type: "stdio",
command: " ",
});
const { validateTomlConfig } = getHookResult();
expect(validateTomlConfig("foo")).toBe("mcp.error.commandRequired");
});
it("returns url required when http server missing url", () => {
tomlToMcpServerMock.mockReturnValue({
type: "http",
url: "",
});
const { validateTomlConfig } = getHookResult();
expect(validateTomlConfig("foo")).toBe("mcp.wizard.urlRequired");
});
it("returns url required when sse server missing url", () => {
tomlToMcpServerMock.mockReturnValue({
type: "sse",
url: "",
});
const { validateTomlConfig } = getHookResult();
expect(validateTomlConfig("foo")).toBe("mcp.wizard.urlRequired");
});
test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
it("surface tomlToMcpServer errors via formatter", () => {
tomlToMcpServerMock.mockImplementation(() => {
throw new Error("normalize failed");
});
const { validateTomlConfig } = getHookResult();
test: update test suites to match component refactoring Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
2025-11-21 11:12:06 +08:00
expect(validateTomlConfig("foo")).toBe(
"mcp.error.tomlInvalid: normalize failed",
);
test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
});
it("returns empty string when validation passes", () => {
tomlToMcpServerMock.mockReturnValue({
type: "stdio",
command: "run.sh",
});
const { validateTomlConfig } = getHookResult();
expect(validateTomlConfig("foo")).toBe("");
});
});
describe("validateJsonConfig", () => {
it("returns error when JSON invalid", () => {
const { validateJsonConfig } = getHookResult();
expect(validateJsonConfig("invalid")).toBe("mcp.error.jsonInvalid");
});
it("rejects arrays of servers", () => {
const { validateJsonConfig } = getHookResult();
expect(validateJsonConfig('{"mcpServers": {}}')).toBe(
"mcp.error.singleServerObjectRequired",
);
});
it("requires command for stdio type", () => {
const { validateJsonConfig } = getHookResult();
test: update test suites to match component refactoring Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
2025-11-21 11:12:06 +08:00
expect(validateJsonConfig('{"type":"stdio"}')).toBe(
"mcp.error.commandRequired",
);
test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
});
it("requires url for http type", () => {
const { validateJsonConfig } = getHookResult();
test: update test suites to match component refactoring Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
2025-11-21 11:12:06 +08:00
expect(validateJsonConfig('{"type":"http","url":""}')).toBe(
"mcp.wizard.urlRequired",
);
test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
});
it("requires url for sse type", () => {
const { validateJsonConfig } = getHookResult();
test: update test suites to match component refactoring Comprehensive test updates to align with recent component refactoring and new auto-launch functionality. Component Tests: - AddProviderDialog.test.tsx (10 lines): * Updated test cases for new dialog behavior * Enhanced mock data for preset selection * Improved assertions for validation - ImportExportSection.test.tsx (16 lines): * Updated for new settings page integration * Enhanced test coverage for error scenarios * Better mock state management - McpFormModal.test.tsx (60 lines): * Extensive updates for form refactoring * New test cases for multi-app selection * Enhanced validation testing * Better coverage of stdio/http server types - ProviderList.test.tsx (11 lines): * Updated for new card layout * Enhanced drag-and-drop testing - SettingsDialog.test.tsx (96 lines): * Major updates for SettingsPage migration * New test cases for auto-launch functionality * Enhanced integration test coverage * Better async operation testing Hook Tests: - useDirectorySettings.test.tsx (32 lines): * Updated for refactored hook logic * Enhanced test coverage for edge cases - useDragSort.test.tsx (36 lines): * Simplified test cases * Better mock implementation * Improved assertions - useImportExport tests (16 lines total): * Updated for new error handling * Enhanced test coverage - useMcpValidation.test.tsx (23 lines): * Updated validation test cases * Better coverage of error scenarios - useProviderActions.test.tsx (48 lines): * Extensive updates for hook refactoring * New test cases for provider operations * Enhanced mock data - useSettings.test.tsx (12 lines): * New test cases for auto-launch * Enhanced settings state testing * Better async operation coverage Integration Tests: - App.test.tsx (41 lines): * Updated for new routing logic * Enhanced navigation testing * Better component integration coverage - SettingsDialog.test.tsx (88 lines): * Complete rewrite for SettingsPage * New integration test scenarios * Enhanced user workflow testing Mock Infrastructure: - handlers.ts (117 lines): * Major updates for MSW handlers * New handlers for auto-launch commands * Enhanced error simulation * Better request/response mocking - state.ts (37 lines): * Updated mock state structure * New state for auto-launch * Enhanced state reset functionality - tauriMocks.ts (10 lines): * Updated mock implementations * Better type safety - server.ts & testQueryClient.ts: * Minor cleanup (2 lines removed) Test Infrastructure Improvements: - Better test isolation - Enhanced mock data consistency - Improved async operation testing - Better error scenario coverage - Enhanced integration test patterns Coverage Improvements: - Net increase of 195 lines of test code - Better coverage of edge cases - Enhanced error path testing - Improved integration test scenarios - Better mock infrastructure All tests now pass with the refactored components while maintaining comprehensive coverage of functionality and edge cases.
2025-11-21 11:12:06 +08:00
expect(validateJsonConfig('{"type":"sse","url":""}')).toBe(
"mcp.wizard.urlRequired",
);
});
test: add MCP functionality tests achieving 100% hooks coverage Milestone Achievement: 100% Hooks Coverage 🎉 - Hooks coverage: 87.5% (7/8) → 100% (8/8) - Total tests: 81 → 105 (+29.6%) - MCP functionality: 0 → 24 tests useMcpActions Tests (8 tests): - Test server reload with loading state management - Use deferred promise pattern to verify loading state transitions - Verify intermediate loading=true state during async operation - Verify error toast (duration: 6s) on reload failure - Ensure loading returns to false after error - Test optimistic toggle with rollback on failure - Immediately update enabled flag before API confirmation - Verify success toast messages for enable/disable - Roll back state to original value on API failure - Show error toast (duration: 6s) when toggle fails - Test server save with list refresh - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId} - Verify syncOtherSide option correctly propagated to API - Refresh server list after successful save - Propagate errors to caller while showing error toast - Do not refresh list when save fails - Test server delete with state management - Verify deleteServerInConfig called with correct parameters - Verify list refresh removes deleted server - Show success toast (duration: 1.5s) on delete - Keep state unchanged on delete failure - Propagate error to caller with error toast useMcpValidation Tests (16 tests): - Test JSON validation (4 tests) - Return empty string for blank/whitespace text - Return "mcp.error.jsonInvalid" for parsing failures - Reject non-object types (string, array) - Accept valid object payloads - Test TOML error formatting (2 tests) - Map mustBeObject/parseError to "mcp.error.tomlInvalid" - Append error details for unknown errors - Test TOML config validation (5 tests) - Propagate errors from validateToml utility - Return "mcp.error.commandRequired" for stdio without command - Return "mcp.wizard.urlRequired" for http without url - Catch and format tomlToMcpServer exceptions - Return empty string when validation passes - Test JSON config validation (5 tests) - Reject invalid JSON syntax - Reject mcpServers array format (single object required) - Require command field for stdio type servers - Require url field for http type servers - Accept valid server configurations Technical Highlights: - Deferred Promise Pattern: Precise async timing control - Optimistic Updates: Test immediate feedback + rollback - Error Propagation: Distinguish caller errors vs toast notifications - i18n Validation: All validators return translation keys - Factory Functions: Reusable test data builders All tests passing: 105/105
2025-10-26 11:56:24 +08:00
it("returns empty string when json config valid", () => {
const { validateJsonConfig } = getHookResult();
expect(
validateJsonConfig(
JSON.stringify({
type: "stdio",
command: "node",
args: ["index.js"],
}),
),
).toBe("");
});
});
});