|
| 1 | +import type { NodeContext } from "@dafthunk/runtime"; |
| 2 | +import type { Node } from "@dafthunk/types"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { ExtractTavilyNode } from "./extract-tavily-node"; |
| 5 | + |
| 6 | +global.fetch = vi.fn(); |
| 7 | + |
| 8 | +describe("ExtractTavilyNode", () => { |
| 9 | + beforeEach(() => vi.clearAllMocks()); |
| 10 | + |
| 11 | + const createContext = ( |
| 12 | + inputs: Record<string, unknown>, |
| 13 | + env: Record<string, string> = {} |
| 14 | + ): NodeContext => |
| 15 | + ({ |
| 16 | + nodeId: "extract-tavily", |
| 17 | + inputs, |
| 18 | + organizationId: "test-org", |
| 19 | + env, |
| 20 | + }) as unknown as NodeContext; |
| 21 | + |
| 22 | + const createNode = () => |
| 23 | + new ExtractTavilyNode({ |
| 24 | + nodeId: "extract-tavily", |
| 25 | + } as unknown as Node); |
| 26 | + |
| 27 | + it("should return error for missing urls", async () => { |
| 28 | + const result = await createNode().execute( |
| 29 | + createContext({}, { TAVILY_API_KEY: "tvly-test" }) |
| 30 | + ); |
| 31 | + expect(result.status).toBe("error"); |
| 32 | + expect(result.error).toContain("Missing required input: urls"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should return error for invalid urls type", async () => { |
| 36 | + const result = await createNode().execute( |
| 37 | + createContext({ urls: 123 }, { TAVILY_API_KEY: "tvly-test" }) |
| 38 | + ); |
| 39 | + expect(result.status).toBe("error"); |
| 40 | + expect(result.error).toContain("Invalid input type for urls"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should return error for missing API key", async () => { |
| 44 | + const result = await createNode().execute( |
| 45 | + createContext({ urls: ["https://example.com"] }) |
| 46 | + ); |
| 47 | + expect(result.status).toBe("error"); |
| 48 | + expect(result.error).toContain("TAVILY_API_KEY"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should extract content from URLs", async () => { |
| 52 | + (global.fetch as ReturnType<typeof vi.fn>).mockResolvedValue({ |
| 53 | + ok: true, |
| 54 | + json: async () => ({ |
| 55 | + results: [ |
| 56 | + { |
| 57 | + url: "https://example.com", |
| 58 | + raw_content: "# Example\nPage content here.", |
| 59 | + }, |
| 60 | + ], |
| 61 | + failed_results: [], |
| 62 | + response_time: 1.2, |
| 63 | + }), |
| 64 | + }); |
| 65 | + |
| 66 | + const result = await createNode().execute( |
| 67 | + createContext( |
| 68 | + { urls: ["https://example.com"] }, |
| 69 | + { TAVILY_API_KEY: "tvly-test" } |
| 70 | + ) |
| 71 | + ); |
| 72 | + |
| 73 | + expect(result.status).toBe("completed"); |
| 74 | + expect(result.outputs?.results).toEqual([ |
| 75 | + { |
| 76 | + url: "https://example.com", |
| 77 | + content: "# Example\nPage content here.", |
| 78 | + }, |
| 79 | + ]); |
| 80 | + expect(result.outputs?.count).toBe(1); |
| 81 | + expect(result.outputs?.failedResults).toEqual([]); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should accept a single URL string", async () => { |
| 85 | + (global.fetch as ReturnType<typeof vi.fn>).mockResolvedValue({ |
| 86 | + ok: true, |
| 87 | + json: async () => ({ |
| 88 | + results: [{ url: "https://example.com", raw_content: "Content" }], |
| 89 | + failed_results: [], |
| 90 | + response_time: 0.5, |
| 91 | + }), |
| 92 | + }); |
| 93 | + |
| 94 | + await createNode().execute( |
| 95 | + createContext( |
| 96 | + { urls: "https://example.com" }, |
| 97 | + { TAVILY_API_KEY: "tvly-test" } |
| 98 | + ) |
| 99 | + ); |
| 100 | + |
| 101 | + const fetchCall = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 102 | + const body = JSON.parse(fetchCall[1].body); |
| 103 | + expect(body.urls).toEqual(["https://example.com"]); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should report failed extractions", async () => { |
| 107 | + (global.fetch as ReturnType<typeof vi.fn>).mockResolvedValue({ |
| 108 | + ok: true, |
| 109 | + json: async () => ({ |
| 110 | + results: [], |
| 111 | + failed_results: [ |
| 112 | + { |
| 113 | + url: "https://blocked.com", |
| 114 | + error: "Access denied", |
| 115 | + }, |
| 116 | + ], |
| 117 | + response_time: 0.8, |
| 118 | + }), |
| 119 | + }); |
| 120 | + |
| 121 | + const result = await createNode().execute( |
| 122 | + createContext( |
| 123 | + { urls: ["https://blocked.com"] }, |
| 124 | + { TAVILY_API_KEY: "tvly-test" } |
| 125 | + ) |
| 126 | + ); |
| 127 | + |
| 128 | + expect(result.status).toBe("completed"); |
| 129 | + expect(result.outputs?.results).toEqual([]); |
| 130 | + expect(result.outputs?.failedResults).toEqual([ |
| 131 | + { url: "https://blocked.com", error: "Access denied" }, |
| 132 | + ]); |
| 133 | + expect(result.outputs?.count).toBe(0); |
| 134 | + }); |
| 135 | + |
| 136 | + it("should handle API errors", async () => { |
| 137 | + (global.fetch as ReturnType<typeof vi.fn>).mockResolvedValue({ |
| 138 | + ok: false, |
| 139 | + status: 401, |
| 140 | + }); |
| 141 | + |
| 142 | + const result = await createNode().execute( |
| 143 | + createContext( |
| 144 | + { urls: ["https://example.com"] }, |
| 145 | + { TAVILY_API_KEY: "tvly-bad-key" } |
| 146 | + ) |
| 147 | + ); |
| 148 | + |
| 149 | + expect(result.status).toBe("error"); |
| 150 | + expect(result.error).toContain("401"); |
| 151 | + }); |
| 152 | +}); |
0 commit comments