|
| 1 | +import { test, expect, mock, beforeEach, afterEach } from "bun:test"; |
| 2 | +import { urlToMarkdown } from "./url-to-markdown"; |
| 3 | + |
| 4 | +// Track fetch calls to verify headers and URL selection |
| 5 | +let fetchCalls: { url: string; headers: Record<string, string> }[] = []; |
| 6 | +const originalFetch = globalThis.fetch; |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + fetchCalls = []; |
| 10 | +}); |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + globalThis.fetch = originalFetch; |
| 14 | +}); |
| 15 | + |
| 16 | +/** |
| 17 | + * Create a mock fetch that responds based on the Accept header. |
| 18 | + * When Accept includes text/markdown, returns markdown with the right content-type. |
| 19 | + * Otherwise returns HTML. |
| 20 | + */ |
| 21 | +function mockFetchWithMarkdownSupport(markdown: string) { |
| 22 | + return mock((url: string | URL | Request, init?: RequestInit) => { |
| 23 | + const headers = init?.headers as Record<string, string> | undefined; |
| 24 | + fetchCalls.push({ url: String(url), headers: headers ?? {} }); |
| 25 | + |
| 26 | + const accept = headers?.Accept || headers?.accept || ""; |
| 27 | + if (accept.includes("text/markdown")) { |
| 28 | + return Promise.resolve( |
| 29 | + new Response(markdown, { |
| 30 | + status: 200, |
| 31 | + headers: { |
| 32 | + "content-type": "text/markdown; charset=utf-8", |
| 33 | + "x-markdown-tokens": "42", |
| 34 | + }, |
| 35 | + }), |
| 36 | + ); |
| 37 | + } |
| 38 | + return Promise.resolve( |
| 39 | + new Response("<html><body><p>Hello</p></body></html>", { |
| 40 | + status: 200, |
| 41 | + headers: { "content-type": "text/html; charset=utf-8" }, |
| 42 | + }), |
| 43 | + ); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +/** Mock fetch that only returns HTML (no markdown support). */ |
| 48 | +function mockFetchHtmlOnly(html = "<html><body><p>Fallback</p></body></html>") { |
| 49 | + return mock((url: string | URL | Request, init?: RequestInit) => { |
| 50 | + const headers = init?.headers as Record<string, string> | undefined; |
| 51 | + fetchCalls.push({ url: String(url), headers: headers ?? {} }); |
| 52 | + return Promise.resolve( |
| 53 | + new Response(html, { |
| 54 | + status: 200, |
| 55 | + headers: { "content-type": "text/html; charset=utf-8" }, |
| 56 | + }), |
| 57 | + ); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +test("content negotiation: uses markdown when server supports it", async () => { |
| 62 | + const md = "# Hello\n\nThis is markdown from the server."; |
| 63 | + globalThis.fetch = mockFetchWithMarkdownSupport(md) as typeof fetch; |
| 64 | + |
| 65 | + const result = await urlToMarkdown("https://example.com/page", { useJina: true }); |
| 66 | + |
| 67 | + expect(result.source).toBe("content-negotiation"); |
| 68 | + expect(result.markdown).toBe(md); |
| 69 | + // Should only make one fetch (the content negotiation request) |
| 70 | + expect(fetchCalls).toHaveLength(1); |
| 71 | + expect(fetchCalls[0].headers.Accept).toContain("text/markdown"); |
| 72 | +}); |
| 73 | + |
| 74 | +test("content negotiation: falls through to Jina when server returns HTML", async () => { |
| 75 | + // First call (content negotiation) returns HTML, second (Jina) returns markdown |
| 76 | + let callCount = 0; |
| 77 | + globalThis.fetch = mock((url: string | URL | Request, init?: RequestInit) => { |
| 78 | + const headers = init?.headers as Record<string, string> | undefined; |
| 79 | + fetchCalls.push({ url: String(url), headers: headers ?? {} }); |
| 80 | + callCount++; |
| 81 | + |
| 82 | + if (callCount === 1) { |
| 83 | + // Content negotiation attempt — server doesn't support it |
| 84 | + return Promise.resolve( |
| 85 | + new Response("<html><body>Hi</body></html>", { |
| 86 | + status: 200, |
| 87 | + headers: { "content-type": "text/html; charset=utf-8" }, |
| 88 | + }), |
| 89 | + ); |
| 90 | + } |
| 91 | + // Jina Reader call |
| 92 | + return Promise.resolve( |
| 93 | + new Response("# From Jina", { |
| 94 | + status: 200, |
| 95 | + headers: { "content-type": "text/plain" }, |
| 96 | + }), |
| 97 | + ); |
| 98 | + }) as typeof fetch; |
| 99 | + |
| 100 | + const result = await urlToMarkdown("https://example.com/page", { useJina: true }); |
| 101 | + |
| 102 | + expect(result.source).toBe("jina"); |
| 103 | + expect(result.markdown).toBe("# From Jina"); |
| 104 | + // Content negotiation fetch + Jina fetch |
| 105 | + expect(fetchCalls.length).toBeGreaterThanOrEqual(2); |
| 106 | + expect(fetchCalls[1].url).toContain("r.jina.ai"); |
| 107 | +}); |
| 108 | + |
| 109 | +test("content negotiation: skipped for local URLs", async () => { |
| 110 | + let callCount = 0; |
| 111 | + globalThis.fetch = mock((_url: string | URL | Request, init?: RequestInit) => { |
| 112 | + const headers = init?.headers as Record<string, string> | undefined; |
| 113 | + fetchCalls.push({ url: String(_url), headers: headers ?? {} }); |
| 114 | + callCount++; |
| 115 | + return Promise.resolve( |
| 116 | + new Response("<html><body>Local</body></html>", { |
| 117 | + status: 200, |
| 118 | + headers: { "content-type": "text/html; charset=utf-8" }, |
| 119 | + }), |
| 120 | + ); |
| 121 | + }) as typeof fetch; |
| 122 | + |
| 123 | + const result = await urlToMarkdown("http://localhost:3000/readme", { useJina: false }); |
| 124 | + |
| 125 | + expect(result.source).toBe("fetch+turndown"); |
| 126 | + // No content negotiation request should have been made |
| 127 | + // (first call should be the Turndown fetch, not a markdown request) |
| 128 | + for (const call of fetchCalls) { |
| 129 | + if (call.headers.Accept) { |
| 130 | + expect(call.headers.Accept).not.toContain("text/markdown"); |
| 131 | + } |
| 132 | + } |
| 133 | +}); |
| 134 | + |
| 135 | +test("content negotiation: handles server error gracefully", async () => { |
| 136 | + let callCount = 0; |
| 137 | + globalThis.fetch = mock((_url: string | URL | Request, init?: RequestInit) => { |
| 138 | + const headers = init?.headers as Record<string, string> | undefined; |
| 139 | + fetchCalls.push({ url: String(_url), headers: headers ?? {} }); |
| 140 | + callCount++; |
| 141 | + |
| 142 | + if (callCount === 1) { |
| 143 | + // Content negotiation — server error |
| 144 | + return Promise.resolve(new Response(null, { status: 500 })); |
| 145 | + } |
| 146 | + // Jina fallback |
| 147 | + return Promise.resolve( |
| 148 | + new Response("# Jina fallback", { |
| 149 | + status: 200, |
| 150 | + headers: { "content-type": "text/plain" }, |
| 151 | + }), |
| 152 | + ); |
| 153 | + }) as typeof fetch; |
| 154 | + |
| 155 | + const result = await urlToMarkdown("https://example.com/page", { useJina: true }); |
| 156 | + |
| 157 | + // Should fall through to Jina |
| 158 | + expect(result.source).toBe("jina"); |
| 159 | +}); |
| 160 | + |
| 161 | +test("raw .md URL: still takes priority over content negotiation", async () => { |
| 162 | + globalThis.fetch = mock((_url: string | URL | Request, init?: RequestInit) => { |
| 163 | + const headers = init?.headers as Record<string, string> | undefined; |
| 164 | + fetchCalls.push({ url: String(_url), headers: headers ?? {} }); |
| 165 | + return Promise.resolve( |
| 166 | + new Response("# Raw markdown file", { |
| 167 | + status: 200, |
| 168 | + headers: { "content-type": "text/plain; charset=utf-8" }, |
| 169 | + }), |
| 170 | + ); |
| 171 | + }) as typeof fetch; |
| 172 | + |
| 173 | + const result = await urlToMarkdown("https://example.com/README.md", { useJina: true }); |
| 174 | + |
| 175 | + expect(result.source).toBe("fetch-raw"); |
| 176 | + expect(result.markdown).toBe("# Raw markdown file"); |
| 177 | +}); |
0 commit comments