| 1 | // Run: tsx src/__tests__/markdown-image-proxy.test.ts |
| 2 | |
| 3 | import { readFileSync } from "node:fs"; |
| 4 | import { dirname, resolve } from "node:path"; |
| 5 | import { fileURLToPath } from "node:url"; |
| 6 | import { markdownImageSource, REMOTE_MARKDOWN_IMAGE_PATH } from "../lib/markdownImage"; |
| 7 | |
| 8 | let passed = 0; |
| 9 | let failed = 0; |
| 10 | |
| 11 | function eq(actual: unknown, expected: unknown, label: string) { |
| 12 | if (actual === expected) { |
| 13 | process.stdout.write(` PASS ${label}\n`); |
| 14 | passed += 1; |
| 15 | } else { |
| 16 | process.stdout.write(` FAIL ${label}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}\n`); |
| 17 | failed += 1; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | console.log("\nmarkdown image proxy"); |
| 22 | |
| 23 | eq( |
| 24 | markdownImageSource("https://cdn.example.com/a b.png?size=2", true), |
| 25 | `${REMOTE_MARKDOWN_IMAGE_PATH}?url=${encodeURIComponent("https://cdn.example.com/a b.png?size=2")}`, |
| 26 | "native shell routes HTTPS images through the backend endpoint", |
| 27 | ); |
| 28 | eq( |
| 29 | markdownImageSource("//cdn.example.com/pixel.png", true), |
| 30 | `${REMOTE_MARKDOWN_IMAGE_PATH}?url=${encodeURIComponent("https://cdn.example.com/pixel.png")}`, |
| 31 | "native shell routes protocol-relative images through the backend endpoint", |
| 32 | ); |
| 33 | eq(markdownImageSource("data:image/png;base64,AAAA", true), "data:image/png;base64,AAAA", "data images stay local"); |
| 34 | eq(markdownImageSource("/__reasonix_workspace_media/token/a.png", true), "/__reasonix_workspace_media/token/a.png", "workspace images stay local"); |
| 35 | eq(markdownImageSource("https://cdn.example.com/a.png", false), "https://cdn.example.com/a.png", "browser development keeps direct image URLs"); |
| 36 | |
| 37 | const testDir = dirname(fileURLToPath(import.meta.url)); |
| 38 | const rendererSource = readFileSync(resolve(testDir, "../components/MarkdownRenderer.tsx"), "utf8"); |
| 39 | eq(rendererSource.includes("img: ({ src, alt, title })"), true, "Markdown renderer owns the image element"); |
| 40 | eq(rendererSource.includes("markdownImageSource(src)"), true, "Markdown renderer applies backend routing"); |
| 41 | |
| 42 | if (failed > 0) { |
| 43 | process.stderr.write(`\n${failed} markdown image proxy assertion(s) failed\n`); |
| 44 | process.exit(1); |
| 45 | } |
| 46 | process.stdout.write(`\n${passed} markdown image proxy assertions passed\n`); |
| 47 |