search-crates.integration.test.tsā¢4.5 kB
import { describe, expect, test } from "bun:test"
import {
createSearchCratesHandler,
suggestSimilarCrates
} from "../../../src/tools/search-crates.js"
describe("Search Crates Tool", () => {
const searchHandler = createSearchCratesHandler()
test("should search for crates with partial name match", async () => {
const result = await searchHandler({
query: "tinc",
limit: 5
})
// Check if there was an error (network issues, etc)
if (result.isError) {
console.log("Search failed with error:", result.content[0].text)
// Skip the test if network is unavailable
return
}
expect(result.content).toHaveLength(1)
expect(result.content[0].type).toBe("text")
const text = result.content[0].text
expect(text).toContain("tinc")
expect(text).toContain("Downloads:")
expect(text).toMatch(/Found \d+ crates matching "tinc"/)
}, 10000)
test("should handle no results gracefully", async () => {
const result = await searchHandler({
query: "this-crate-definitely-does-not-exist-12345",
limit: 5
})
// Check if there was an error (network issues, etc)
if (result.isError) {
console.log("Search failed with error:", result.content[0].text)
// Skip the test if network is unavailable
return
}
expect(result.content[0].text).toContain(
'No crates found matching "this-crate-definitely-does-not-exist-12345"'
)
}, 10000)
test("should respect limit parameter", async () => {
const result = await searchHandler({
query: "test",
limit: 3
})
// Check if there was an error (network issues, etc)
if (result.isError) {
console.log("Search failed with error:", result.content[0].text)
// Skip the test if network is unavailable
return
}
const text = result.content[0].text
const matches = text.match(/^\d+\./gm)
// Should have at most 3 results
if (matches) {
expect(matches.length).toBeLessThanOrEqual(3)
}
}, 10000)
test("should format results correctly", async () => {
const result = await searchHandler({
query: "tokio",
limit: 1
})
// Skip test if there's a network error
if (result.isError) {
console.log("Search failed with error:", result.content[0].text)
return
}
const text = result.content[0].text
// Check for expected formatting
expect(text).toMatch(/1\. \*\*tokio\*\* v\d+\.\d+\.\d+/)
expect(text).toContain("Downloads:")
expect(text).toContain("recent)")
}, 10000)
})
describe("Suggest Similar Crates", () => {
test("should suggest similar crates for typos", async () => {
try {
const suggestions = await suggestSimilarCrates("cla", 5)
expect(Array.isArray(suggestions)).toBe(true)
// Network might be down, so we just check it returns an array
if (suggestions.length > 0) {
// Should likely include something with "cla" in it (like "clap")
expect(suggestions.some((s) => s.includes("cla"))).toBe(true)
}
} catch (error) {
console.log("Network error during suggestion test:", error)
// Return empty array on network error
expect(true).toBe(true) // Pass the test on network error
}
}, 10000)
test("should return empty array for non-existent crates", async () => {
try {
const suggestions = await suggestSimilarCrates("zzzzz-definitely-not-a-crate-99999", 5)
expect(Array.isArray(suggestions)).toBe(true)
// Might return empty or might return unrelated crates
expect(suggestions.length).toBeGreaterThanOrEqual(0)
} catch (error) {
console.log("Network error during suggestion test:", error)
// Should still return array on error
expect(true).toBe(true)
}
}, 10000)
test("should respect limit parameter", async () => {
try {
const suggestions = await suggestSimilarCrates("async", 3)
expect(Array.isArray(suggestions)).toBe(true)
expect(suggestions.length).toBeLessThanOrEqual(3)
} catch (error) {
console.log("Network error during suggestion test:", error)
expect(true).toBe(true)
}
}, 10000)
test("should handle network errors gracefully", async () => {
// Mock a failed fetch by using an invalid crate name with special characters
// that might cause issues
try {
const suggestions = await suggestSimilarCrates("", 5)
expect(Array.isArray(suggestions)).toBe(true)
// Should return empty array on error
expect(suggestions.length).toBeGreaterThanOrEqual(0)
} catch (error) {
// This is expected behavior - we handle errors gracefully
console.log("Expected error handled:", error)
expect(true).toBe(true)
}
}, 10000)
})