|
| 1 | +import { describe, test, expect } from "vitest"; |
| 2 | +import { Context7 } from "./client"; |
| 3 | + |
| 4 | +describe("Context7 Client integration", () => { |
| 5 | + const apiKey = process.env.CONTEXT7_API_KEY!; |
| 6 | + |
| 7 | + describe("searchLibrary", () => { |
| 8 | + const client = new Context7({ apiKey }); |
| 9 | + |
| 10 | + test("should search for libraries and return array directly", async () => { |
| 11 | + const result = await client.searchLibrary("I need to build a UI", "react"); |
| 12 | + |
| 13 | + expect(result).toBeDefined(); |
| 14 | + expect(Array.isArray(result)).toBe(true); |
| 15 | + expect(result.length).toBeGreaterThan(0); |
| 16 | + }); |
| 17 | + |
| 18 | + test("should return Library objects with all fields", async () => { |
| 19 | + const result = await client.searchLibrary("I want to use TypeScript", "typescript"); |
| 20 | + |
| 21 | + expect(result.length).toBeGreaterThan(0); |
| 22 | + const library = result[0]; |
| 23 | + |
| 24 | + expect(library).toHaveProperty("id"); |
| 25 | + expect(library).toHaveProperty("name"); |
| 26 | + expect(library).toHaveProperty("description"); |
| 27 | + expect(library).toHaveProperty("totalSnippets"); |
| 28 | + expect(library).toHaveProperty("trustScore"); |
| 29 | + expect(library).toHaveProperty("benchmarkScore"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("should search with different queries", async () => { |
| 33 | + const queries = ["vue", "express", "next"]; |
| 34 | + |
| 35 | + for (const query of queries) { |
| 36 | + const result = await client.searchLibrary(`I want to use ${query}`, query); |
| 37 | + expect(result.length).toBeGreaterThan(0); |
| 38 | + } |
| 39 | + }, 15000); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("getContext - JSON format (default)", () => { |
| 43 | + const client = new Context7({ apiKey }); |
| 44 | + |
| 45 | + test("should get context as Documentation array (default)", async () => { |
| 46 | + const result = await client.getContext("How to use hooks", "/react/react"); |
| 47 | + |
| 48 | + expect(result).toBeDefined(); |
| 49 | + expect(Array.isArray(result)).toBe(true); |
| 50 | + expect(result.length).toBeGreaterThan(0); |
| 51 | + }); |
| 52 | + |
| 53 | + test("should get context with explicit json type", async () => { |
| 54 | + const result = await client.getContext("How to use hooks", "/react/react", { |
| 55 | + type: "json", |
| 56 | + }); |
| 57 | + |
| 58 | + expect(result).toBeDefined(); |
| 59 | + expect(Array.isArray(result)).toBe(true); |
| 60 | + expect(result.length).toBeGreaterThan(0); |
| 61 | + }); |
| 62 | + |
| 63 | + test("should have correct Documentation structure", async () => { |
| 64 | + const result = await client.getContext("How to use hooks", "/react/react", { |
| 65 | + type: "json", |
| 66 | + }); |
| 67 | + |
| 68 | + expect(result.length).toBeGreaterThan(0); |
| 69 | + const doc = result[0]; |
| 70 | + expect(doc).toHaveProperty("title"); |
| 71 | + expect(doc).toHaveProperty("content"); |
| 72 | + expect(doc).toHaveProperty("source"); |
| 73 | + expect(typeof doc.title).toBe("string"); |
| 74 | + expect(typeof doc.content).toBe("string"); |
| 75 | + expect(typeof doc.source).toBe("string"); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("getContext - text format", () => { |
| 80 | + const client = new Context7({ apiKey }); |
| 81 | + |
| 82 | + test("should get context as text string with type: txt", async () => { |
| 83 | + const result = await client.getContext("How to use hooks", "/react/react", { |
| 84 | + type: "txt", |
| 85 | + }); |
| 86 | + |
| 87 | + expect(result).toBeDefined(); |
| 88 | + expect(typeof result).toBe("string"); |
| 89 | + expect(result.length).toBeGreaterThan(0); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("getContext - different libraries", () => { |
| 94 | + const client = new Context7({ apiKey }); |
| 95 | + |
| 96 | + test("should get context for Vue", async () => { |
| 97 | + const result = await client.getContext("How to create components", "/vuejs/core"); |
| 98 | + |
| 99 | + expect(result).toBeDefined(); |
| 100 | + expect(Array.isArray(result)).toBe(true); |
| 101 | + expect(result.length).toBeGreaterThan(0); |
| 102 | + }); |
| 103 | + |
| 104 | + test("should get context for Express", async () => { |
| 105 | + const result = await client.getContext("How to create routes", "/expressjs/express"); |
| 106 | + |
| 107 | + expect(result).toBeDefined(); |
| 108 | + expect(Array.isArray(result)).toBe(true); |
| 109 | + expect(result.length).toBeGreaterThan(0); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("live error handling", () => { |
| 114 | + const client = new Context7({ apiKey }); |
| 115 | + |
| 116 | + test("should handle invalid library ID gracefully", async () => { |
| 117 | + await expect(client.getContext("test query", "/nonexistent/library")).rejects.toThrow(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments