| 1 | import os from "os"; |
| 2 | |
| 3 | import { ContinueConfig } from "../../.."; |
| 4 | import { testConfigHandler } from "../../../test/fixtures"; |
| 5 | import FileSystemIde from "../../../util/filesystem"; |
| 6 | |
| 7 | import DocsCrawler, { |
| 8 | ChromiumInstaller, |
| 9 | DocsCrawlerType, |
| 10 | type PageData, |
| 11 | } from "./DocsCrawler"; |
| 12 | import { commonDocsSites } from "./crawl-test-cases"; |
| 13 | |
| 14 | // Temporary workaround until we have better caching of Chromium |
| 15 | // download between test runs |
| 16 | const TIMEOUT_MS = 1_000_000_000; |
| 17 | |
| 18 | // Overwrite the Chromium download path to `os.tmpdir()` |
| 19 | // so that we don't delete the Chromium install between tests |
| 20 | ChromiumInstaller.PCR_CONFIG = { downloadPath: os.tmpdir() }; |
| 21 | |
| 22 | describe("DocsCrawler", () => { |
| 23 | let config: ContinueConfig; |
| 24 | let mockIde: FileSystemIde; |
| 25 | let chromiumInstaller: ChromiumInstaller; |
| 26 | let docsCrawler: DocsCrawler; |
| 27 | |
| 28 | beforeAll(async () => { |
| 29 | const result = await testConfigHandler.loadConfig(); |
| 30 | config = result.config!; |
| 31 | mockIde = new FileSystemIde(process.cwd()); |
| 32 | chromiumInstaller = new ChromiumInstaller(mockIde, config); |
| 33 | docsCrawler = new DocsCrawler(mockIde, config); |
| 34 | }, TIMEOUT_MS); |
| 35 | |
| 36 | async function runCrawl(url: string) { |
| 37 | const pages: PageData[] = []; |
| 38 | const generator = docsCrawler.crawl(new URL(url)); |
| 39 | let done = false; |
| 40 | let crawler: DocsCrawlerType = "default"; |
| 41 | while (!done) { |
| 42 | const result = await generator.next(); |
| 43 | if (result.done) { |
| 44 | done = true; |
| 45 | crawler = result.value; |
| 46 | } else { |
| 47 | pages.push(result.value); |
| 48 | } |
| 49 | } |
| 50 | return { |
| 51 | pages, |
| 52 | crawler, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | describe.skip("GitHub Crawler", () => { |
| 57 | const repoUrl = |
| 58 | "https://github.com/Patrick-Erichsen/test-github-repo-for-crawling"; |
| 59 | |
| 60 | let crawlResults: PageData[]; |
| 61 | let crawlerUsed: DocsCrawlerType; |
| 62 | |
| 63 | beforeAll(async () => { |
| 64 | docsCrawler = new DocsCrawler(mockIde, config, 5, 5, false); |
| 65 | const { pages, crawler } = await runCrawl(repoUrl); |
| 66 | crawlerUsed = crawler; |
| 67 | crawlResults = pages; |
| 68 | }, TIMEOUT_MS); |
| 69 | |
| 70 | test( |
| 71 | "finds expected markdown files", |
| 72 | async () => { |
| 73 | expect( |
| 74 | crawlResults.some((page) => page.path.endsWith("README.md")), |
| 75 | ).toBe(true); |
| 76 | |
| 77 | expect( |
| 78 | crawlResults.some((page) => page.path.endsWith("docs-depth-1.md")), |
| 79 | ).toBe(true); |
| 80 | }, |
| 81 | TIMEOUT_MS, |
| 82 | ); |
| 83 | |
| 84 | test( |
| 85 | "html includes the correct content", |
| 86 | async () => { |
| 87 | const doc = crawlResults.find((page) => |
| 88 | page.path.endsWith("docs-depth-1.md"), |
| 89 | ); |
| 90 | |
| 91 | expect(doc?.content.includes("Test body")).toBe(true); |
| 92 | expect(doc?.content.includes("Test H2 Header")).toBe(true); |
| 93 | }, |
| 94 | TIMEOUT_MS, |
| 95 | ); |
| 96 | |
| 97 | test( |
| 98 | "ignores non-markdown files, e.g. `test.js` at the root", |
| 99 | async () => { |
| 100 | expect(crawlResults.some((page) => page.path.endsWith("test.js"))).toBe( |
| 101 | false, |
| 102 | ); |
| 103 | }, |
| 104 | TIMEOUT_MS, |
| 105 | ); |
| 106 | }); |
| 107 | |
| 108 | // Skipped until we have a better way to cache Chromium installs |
| 109 | // between tests and in CI |
| 110 | // Run this periodically to make sure it's still working |
| 111 | describe.skip("Chromium Crawler", () => { |
| 112 | let shouldUseChromiumSpy: any; |
| 113 | |
| 114 | beforeAll(async () => { |
| 115 | docsCrawler = new DocsCrawler(mockIde, config, 5, 5, true); |
| 116 | await chromiumInstaller.install(); |
| 117 | shouldUseChromiumSpy = jest |
| 118 | .spyOn(docsCrawler as any, "shouldUseChromium") |
| 119 | .mockReturnValue(true); |
| 120 | }, TIMEOUT_MS); |
| 121 | |
| 122 | afterAll(() => { |
| 123 | shouldUseChromiumSpy.mockRestore(); |
| 124 | }, TIMEOUT_MS); |
| 125 | |
| 126 | commonDocsSites.forEach((url) => { |
| 127 | test( |
| 128 | `Chromium crawler common site: ${url}`, |
| 129 | async () => { |
| 130 | const { pages, crawler } = await runCrawl(url); |
| 131 | expect(pages.length).toBeGreaterThanOrEqual(1); |
| 132 | expect(crawler).toEqual("chromium"); |
| 133 | }, |
| 134 | TIMEOUT_MS, |
| 135 | ); |
| 136 | }); |
| 137 | }); |
| 138 | |
| 139 | describe("Default Crawler", () => { |
| 140 | beforeAll(() => { |
| 141 | docsCrawler = new DocsCrawler(mockIde, config, 2, 2, false); |
| 142 | }); |
| 143 | |
| 144 | test( |
| 145 | "works on static site", |
| 146 | async () => { |
| 147 | const { pages, crawler } = await runCrawl("https://amplified.dev/"); |
| 148 | expect(pages.length).toBeGreaterThanOrEqual(1); |
| 149 | expect(crawler).toEqual("default"); |
| 150 | }, |
| 151 | TIMEOUT_MS, |
| 152 | ); |
| 153 | |
| 154 | commonDocsSites.forEach((url) => { |
| 155 | test( |
| 156 | `Default crawler common site: ${url}`, |
| 157 | async () => { |
| 158 | // Retry logic to handle flaky network requests to TRIAL_PROXY_URL |
| 159 | let lastError; |
| 160 | const maxRetries = 3; |
| 161 | |
| 162 | for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 163 | try { |
| 164 | const { pages, crawler } = await runCrawl(url); |
| 165 | expect(pages.length).toBeGreaterThanOrEqual(1); |
| 166 | expect(crawler).toEqual("default"); |
| 167 | return; // Test passed, exit retry loop |
| 168 | } catch (error) { |
| 169 | lastError = error; |
| 170 | if (attempt === maxRetries) { |
| 171 | throw error; // Final attempt failed, throw the error |
| 172 | } |
| 173 | // Wait before retrying (exponential backoff) |
| 174 | await new Promise((resolve) => |
| 175 | setTimeout(resolve, 1000 * attempt), |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 | }, |
| 180 | TIMEOUT_MS, |
| 181 | ); |
| 182 | }); |
| 183 | }); |
| 184 | |
| 185 | describe("Cheerio Crawler", () => { |
| 186 | beforeAll(() => { |
| 187 | docsCrawler = new DocsCrawler(mockIde, config, 2, 2, true); |
| 188 | }); |
| 189 | test( |
| 190 | "works on static site", |
| 191 | async () => { |
| 192 | const { pages, crawler } = await runCrawl("https://amplified.dev/"); |
| 193 | expect(pages.length).toBeGreaterThanOrEqual(1); |
| 194 | expect(crawler).toEqual("cheerio"); |
| 195 | }, |
| 196 | TIMEOUT_MS, |
| 197 | ); |
| 198 | }); |
| 199 | }); |