|
| 1 | +import { describe, it, expect, vi, beforeEach, type Mock } from "vitest"; |
| 2 | +import type { DocumentManagementService } from "../store"; |
| 3 | +import { FindVersionTool, type FindVersionToolOptions } from "./FindVersionTool"; |
| 4 | +import { VersionNotFoundError } from "./errors"; |
| 5 | +import { logger } from "../utils/logger"; |
| 6 | + |
| 7 | +// Mock dependencies |
| 8 | +vi.mock("../store"); // Mock the entire store module if DocumentManagementService is complex |
| 9 | +vi.mock("../utils/logger"); |
| 10 | + |
| 11 | +describe("FindVersionTool", () => { |
| 12 | + let mockDocService: Partial<DocumentManagementService>; |
| 13 | + let findVersionTool: FindVersionTool; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + // Reset mocks before each test |
| 17 | + vi.resetAllMocks(); |
| 18 | + |
| 19 | + // Setup mock DocumentManagementService |
| 20 | + mockDocService = { |
| 21 | + findBestVersion: vi.fn(), |
| 22 | + }; |
| 23 | + |
| 24 | + // Create instance of the tool with the mock service |
| 25 | + findVersionTool = new FindVersionTool( |
| 26 | + mockDocService as DocumentManagementService, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should return message indicating best match when found", async () => { |
| 31 | + const options: FindVersionToolOptions = { library: "react", targetVersion: "18.2.0" }; |
| 32 | + const mockResult = { bestMatch: "18.2.0", hasUnversioned: false }; |
| 33 | + (mockDocService.findBestVersion as Mock).mockResolvedValue(mockResult); |
| 34 | + |
| 35 | + const result = await findVersionTool.execute(options); |
| 36 | + |
| 37 | + expect(mockDocService.findBestVersion).toHaveBeenCalledWith("react", "18.2.0"); |
| 38 | + expect(result).toContain("Best match: 18.2.0"); |
| 39 | + expect(result).not.toContain("Unversioned docs"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should return message indicating best match and unversioned docs when both exist", async () => { |
| 43 | + const options: FindVersionToolOptions = { library: "react", targetVersion: "18.x" }; |
| 44 | + const mockResult = { bestMatch: "18.3.1", hasUnversioned: true }; |
| 45 | + (mockDocService.findBestVersion as Mock).mockResolvedValue(mockResult); |
| 46 | + |
| 47 | + const result = await findVersionTool.execute(options); |
| 48 | + |
| 49 | + expect(mockDocService.findBestVersion).toHaveBeenCalledWith("react", "18.x"); |
| 50 | + expect(result).toContain("Best match: 18.3.1"); |
| 51 | + expect(result).toContain("Unversioned docs also available"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should return message indicating only unversioned docs when no version matches", async () => { |
| 55 | + const options: FindVersionToolOptions = { library: "vue", targetVersion: "4.0.0" }; |
| 56 | + const mockResult = { bestMatch: null, hasUnversioned: true }; |
| 57 | + (mockDocService.findBestVersion as Mock).mockResolvedValue(mockResult); |
| 58 | + |
| 59 | + const result = await findVersionTool.execute(options); |
| 60 | + |
| 61 | + expect(mockDocService.findBestVersion).toHaveBeenCalledWith("vue", "4.0.0"); |
| 62 | + expect(result).toContain("No matching version found"); |
| 63 | + expect(result).toContain("but unversioned docs exist"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should return message indicating no match when VersionNotFoundError is thrown", async () => { |
| 67 | + const options: FindVersionToolOptions = { library: "angular", targetVersion: "1.0.0" }; |
| 68 | + const available = [{ version: "15.0.0", indexed: true }, { version: "16.1.0", indexed: false }]; |
| 69 | + const error = new VersionNotFoundError("angular", "1.0.0", available); |
| 70 | + (mockDocService.findBestVersion as Mock).mockRejectedValue(error); |
| 71 | + |
| 72 | + const result = await findVersionTool.execute(options); |
| 73 | + |
| 74 | + expect(mockDocService.findBestVersion).toHaveBeenCalledWith("angular", "1.0.0"); |
| 75 | + expect(result).toContain("No matching version or unversioned documents found"); |
| 76 | + expect(result).toContain("Available:"); // Check it mentions availability without exact format |
| 77 | + expect(result).toContain("15.0.0"); |
| 78 | + expect(result).toContain("16.1.0"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should return message indicating no match when VersionNotFoundError is thrown with no available versions", async () => { |
| 82 | + const options: FindVersionToolOptions = { library: "unknown-lib" }; |
| 83 | + const error = new VersionNotFoundError("unknown-lib", "latest", []); // Assuming default is 'latest' if targetVersion omitted |
| 84 | + (mockDocService.findBestVersion as Mock).mockRejectedValue(error); |
| 85 | + |
| 86 | + const result = await findVersionTool.execute(options); |
| 87 | + |
| 88 | + expect(mockDocService.findBestVersion).toHaveBeenCalledWith("unknown-lib", undefined); // targetVersion is undefined |
| 89 | + expect(result).toContain("No matching version or unversioned documents found"); |
| 90 | + expect(result).toContain("Available: None"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should re-throw unexpected errors from docService", async () => { |
| 94 | + const options: FindVersionToolOptions = { library: "react" }; |
| 95 | + const unexpectedError = new Error("Database connection failed"); |
| 96 | + (mockDocService.findBestVersion as Mock).mockRejectedValue(unexpectedError); |
| 97 | + |
| 98 | + await expect(findVersionTool.execute(options)).rejects.toThrow( |
| 99 | + "Database connection failed", |
| 100 | + ); |
| 101 | + expect(logger.error).toHaveBeenCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should handle missing targetVersion correctly", async () => { |
| 105 | + const options: FindVersionToolOptions = { library: "react" }; // No targetVersion |
| 106 | + const mockResult = { bestMatch: "18.3.1", hasUnversioned: false }; |
| 107 | + (mockDocService.findBestVersion as Mock).mockResolvedValue(mockResult); |
| 108 | + |
| 109 | + const result = await findVersionTool.execute(options); |
| 110 | + |
| 111 | + // Check that findBestVersion was called with undefined for targetVersion |
| 112 | + expect(mockDocService.findBestVersion).toHaveBeenCalledWith("react", undefined); |
| 113 | + expect(result).toContain("Best match: 18.3.1"); |
| 114 | + }); |
| 115 | +}); |
0 commit comments