diff --git a/core/indexing/docs/DocsCrawler.test.ts b/core/indexing/docs/DocsCrawler.test.ts deleted file mode 100644 index cd40d9fe4..000000000 --- a/core/indexing/docs/DocsCrawler.test.ts +++ /dev/null @@ -1,146 +0,0 @@ -import os from "os"; - -// import { pathToFileUrl } from "path"; -import { ContinueConfig } from "../.."; -import { testConfigHandler } from "../../test/fixtures"; -import FileSystemIde from "../../util/filesystem"; - -import DocsCrawler, { - ChromiumInstaller, - DocsCrawlerType, - type PageData, -} from "./DocsCrawler"; - -// Temporary workaround until we have better caching of Chromium -// download between test runs -const TIMEOUT_MS = 1_000_000_000; - -// Overwrite the Chromium download path to `os.tmpdir()` -// so that we don't delete the Chromium install between tests -ChromiumInstaller.PCR_CONFIG = { downloadPath: os.tmpdir() }; - -// Skipped until we have a better way to cache Chromium installs -// between tests and in CI -describe("DocsCrawler", () => { - const NUM_PAGES_TO_CRAWL = 5; - - let config: ContinueConfig; - let mockIde: FileSystemIde; - let docsCrawler: DocsCrawler; - - beforeAll(async () => { - const result = await testConfigHandler.loadConfig(); - if (!result.config) { - throw new Error("Failed to load config"); - } - config = result.config; - mockIde = new FileSystemIde(process.cwd()); - // docsCrawler = new DocsCrawler(mockIde, config, 5, 5, false); - // chromiumInstaller = new ChromiumInstaller(mockIde, config); - - // Make sure we have Chromium pulled down before beginning tests - // await chromiumInstaller.install(); - }, TIMEOUT_MS); - - // async function crawlTest( - // url: string, - // numPagesToCrawl: number = NUM_PAGES_TO_CRAWL, - // expectedCrawlerUsed: DocsCrawlerType, - // ) { - // const crawlResults: PageData[] = []; - // it( - // `Crawl ${url}`, - // async () => { - // const generator = docsCrawler.crawl(new URL(url)); - // let done = false; - // while (!done) { - // const result = await generator.next(); - // if (result.done) { - // done = true; - // expect(result.value).toEqual(expectedCrawlerUsed); - // } else { - // crawlResults.push(result.value); - // } - // } - - // expect(crawlResults.length).toBeGreaterThanOrEqual(numPagesToCrawl); - // }, - // TIMEOUT_MS, - // ); - // return crawlResults; - // } - - // describe("GitHub Crawler", async () => { - // docsCrawler = new DocsCrawler(mockIde, config, 5, 5, false); - - // const repoUrl = - // "https://github.com/Patrick-Erichsen/test-github-repo-for-crawling"; - - // const crawlResults = await crawlTest(repoUrl, 1, "github"); - // // test("finds expected markdown files", async () => { - // expect(crawlResults.some((page) => page.path.endsWith("README.md"))).toBe( - // true, - // ); - - // expect( - // crawlResults.some((page) => page.path.endsWith("docs-depth-1.md")), - // ).toBe(true); - // // }); - - // // test("html includes the correct content", async () => { - // const doc = crawlResults.find((page) => - // page.path.endsWith("docs-depth-1.md"), - // ); - - // expect(doc?.content.includes("Test body")).toBe(true); - // expect(doc?.content.includes("Test H2 Header")).toBe(true); - // // }); - - // // test("ignores non-markdown files, e.g. `test.js` at the root", async () => { - // expect(crawlResults.some((page) => page.path.endsWith("test.js"))).toBe( - // false, - // ); - // // }); - // }); - - // describe("Chromium Crawler", () => { - // let shouldUseChromiumSpy: any; - - // beforeAll(() => { - // shouldUseChromiumSpy = jest - // .spyOn(docsCrawler as any, "shouldUseChromium") - // .mockReturnValue(true); - // }); - - // afterAll(() => { - // shouldUseChromiumSpy.mockRestore(); - // }); - - // describe("Pre-indexed Docs", () => { - // crawlTest(Object.keys(preIndexedDocs)); - // }); - - // describe("Running list of common sites", () => { - // crawlTest([ - // "https://docs.nestjs.com/", - // "https://go.dev/doc/", - // "https://clickhouse.com/docs", - // "https://www.tensorflow.org/api_docs", - // // "https://www.rust-lang.org/learn", TODO: This is failing - // "https://docs.anthropic.com/en/docs", - // ]); - // }); - // }); - - // describe("Default Crawler", () => { - // docsCrawler = new DocsCrawler(mockIde, config, 5, 5, true); - - // crawlTest("https://amplified.dev/", 1, "default"); - // }); - - describe("Cheerio Crawler", () => { - docsCrawler = new DocsCrawler(mockIde, config, 5, 5, true); - - // crawlTest("https://amplified.dev/", 1, "cheerio"); - }); -}); diff --git a/core/indexing/docs/DocsService.ts b/core/indexing/docs/DocsService.ts index ee0b17f5d..c8507f256 100644 --- a/core/indexing/docs/DocsService.ts +++ b/core/indexing/docs/DocsService.ts @@ -30,7 +30,7 @@ import { htmlPageToArticleWithChunks, markdownPageToArticleWithChunks, } from "./article"; -import DocsCrawler, { DocsCrawlerType, PageData } from "./DocsCrawler"; +import DocsCrawler, { DocsCrawlerType, PageData } from "./crawlers/DocsCrawler"; import { ConfigResult } from "@continuedev/config-yaml"; import { runLanceMigrations, runSqliteMigrations } from "./migrations"; import { diff --git a/core/indexing/docs/article.ts b/core/indexing/docs/article.ts index 53e2a4e68..428bae967 100644 --- a/core/indexing/docs/article.ts +++ b/core/indexing/docs/article.ts @@ -4,7 +4,7 @@ import { JSDOM } from "jsdom"; import { Chunk } from "../../"; import { cleanFragment, cleanHeader, markdownChunker } from "../chunk/markdown"; -import { type PageData } from "./DocsCrawler"; +import { type PageData } from "./crawlers/DocsCrawler"; export type ArticleComponent = { title: string; diff --git a/core/indexing/docs/crawlers/CheerioCrawler.ts b/core/indexing/docs/crawlers/CheerioCrawler.ts index 5e2d8a20a..dd5fb1918 100644 --- a/core/indexing/docs/crawlers/CheerioCrawler.ts +++ b/core/indexing/docs/crawlers/CheerioCrawler.ts @@ -2,7 +2,7 @@ import { URL } from "node:url"; import * as cheerio from "cheerio"; -import { PageData } from "../DocsCrawler"; +import { PageData } from "./DocsCrawler"; export default class CheerioCrawler { private readonly IGNORE_PATHS_ENDING_IN = [ diff --git a/core/indexing/docs/crawlers/ChromiumCrawler.ts b/core/indexing/docs/crawlers/ChromiumCrawler.ts index 20cddf4a3..ef9b528ce 100644 --- a/core/indexing/docs/crawlers/ChromiumCrawler.ts +++ b/core/indexing/docs/crawlers/ChromiumCrawler.ts @@ -13,7 +13,7 @@ import { getChromiumPath, getContinueUtilsPath, } from "../../../util/paths"; -import { PageData } from "../DocsCrawler"; +import { PageData } from "./DocsCrawler"; export class ChromiumCrawler { private readonly LINK_GROUP_SIZE = 2; diff --git a/core/indexing/docs/crawlers/DefaultCrawler.ts b/core/indexing/docs/crawlers/DefaultCrawler.ts index b6caabf42..0f0d9e593 100644 --- a/core/indexing/docs/crawlers/DefaultCrawler.ts +++ b/core/indexing/docs/crawlers/DefaultCrawler.ts @@ -2,7 +2,7 @@ import { URL } from "node:url"; import { getHeaders } from "../../../continueServer/stubs/headers"; import { TRIAL_PROXY_URL } from "../../../control-plane/client"; -import { PageData } from "../DocsCrawler"; +import { PageData } from "./DocsCrawler"; export class DefaultCrawler { constructor( diff --git a/core/indexing/docs/crawlers/DocsCrawler.test.ts b/core/indexing/docs/crawlers/DocsCrawler.test.ts new file mode 100644 index 000000000..d3a246cf9 --- /dev/null +++ b/core/indexing/docs/crawlers/DocsCrawler.test.ts @@ -0,0 +1,177 @@ +import os from "os"; + +import { ContinueConfig } from "../../.."; +import { testConfigHandler } from "../../../test/fixtures"; +import FileSystemIde from "../../../util/filesystem"; + +import DocsCrawler, { + ChromiumInstaller, + DocsCrawlerType, + type PageData, +} from "./DocsCrawler"; +import { commonDocsSites } from "./crawl-test-cases"; + +// Temporary workaround until we have better caching of Chromium +// download between test runs +const TIMEOUT_MS = 1_000_000_000; + +// Overwrite the Chromium download path to `os.tmpdir()` +// so that we don't delete the Chromium install between tests +ChromiumInstaller.PCR_CONFIG = { downloadPath: os.tmpdir() }; + +describe("DocsCrawler", () => { + let config: ContinueConfig; + let mockIde: FileSystemIde; + let chromiumInstaller: ChromiumInstaller; + let docsCrawler: DocsCrawler; + + beforeAll(async () => { + const result = await testConfigHandler.loadConfig(); + config = result.config!; + mockIde = new FileSystemIde(process.cwd()); + chromiumInstaller = new ChromiumInstaller(mockIde, config); + docsCrawler = new DocsCrawler(mockIde, config); + }, TIMEOUT_MS); + + async function runCrawl(url: string) { + const pages: PageData[] = []; + const generator = docsCrawler.crawl(new URL(url)); + let done = false; + let crawler: DocsCrawlerType = "default"; + while (!done) { + const result = await generator.next(); + if (result.done) { + done = true; + crawler = result.value; + } else { + pages.push(result.value); + } + } + return { + pages, + crawler, + }; + } + + describe("GitHub Crawler", () => { + const repoUrl = + "https://github.com/Patrick-Erichsen/test-github-repo-for-crawling"; + + let crawlResults: PageData[]; + let crawlerUsed: DocsCrawlerType; + + beforeAll(async () => { + docsCrawler = new DocsCrawler(mockIde, config, 5, 5, false); + const { pages, crawler } = await runCrawl(repoUrl); + crawlerUsed = crawler; + crawlResults = pages; + }, TIMEOUT_MS); + + test( + "finds expected markdown files", + async () => { + expect( + crawlResults.some((page) => page.path.endsWith("README.md")), + ).toBe(true); + + expect( + crawlResults.some((page) => page.path.endsWith("docs-depth-1.md")), + ).toBe(true); + }, + TIMEOUT_MS, + ); + + test( + "html includes the correct content", + async () => { + const doc = crawlResults.find((page) => + page.path.endsWith("docs-depth-1.md"), + ); + + expect(doc?.content.includes("Test body")).toBe(true); + expect(doc?.content.includes("Test H2 Header")).toBe(true); + }, + TIMEOUT_MS, + ); + + test( + "ignores non-markdown files, e.g. `test.js` at the root", + async () => { + expect(crawlResults.some((page) => page.path.endsWith("test.js"))).toBe( + false, + ); + }, + TIMEOUT_MS, + ); + }); + + // Skipped until we have a better way to cache Chromium installs + // between tests and in CI + // Run this periodically to make sure it's still working + describe.skip("Chromium Crawler", () => { + let shouldUseChromiumSpy: any; + + beforeAll(async () => { + docsCrawler = new DocsCrawler(mockIde, config, 5, 5, true); + await chromiumInstaller.install(); + shouldUseChromiumSpy = jest + .spyOn(docsCrawler as any, "shouldUseChromium") + .mockReturnValue(true); + }, TIMEOUT_MS); + + afterAll(() => { + shouldUseChromiumSpy.mockRestore(); + }, TIMEOUT_MS); + + commonDocsSites.forEach((url) => { + test( + `Chromium crawler common site: ${url}`, + async () => { + const { pages, crawler } = await runCrawl(url); + expect(pages.length).toBeGreaterThanOrEqual(1); + expect(crawler).toEqual("chromium"); + }, + TIMEOUT_MS, + ); + }); + }); + + describe("Default Crawler", () => { + beforeAll(() => { + docsCrawler = new DocsCrawler(mockIde, config, 2, 2, false); + }); + + test( + "works on static site", + async () => { + const { pages, crawler } = await runCrawl("https://amplified.dev/"); + expect(pages.length).toBeGreaterThanOrEqual(1); + expect(crawler).toEqual("default"); + }, + TIMEOUT_MS, + ); + + commonDocsSites.forEach((url) => { + test(`Default crawler common site: ${url}`, async () => { + const { pages, crawler } = await runCrawl(url); + expect(pages.length).toBeGreaterThanOrEqual(1); + expect(crawler).toEqual("default"); + }); + }); + }); + + describe("Cheerio Crawler", () => { + beforeAll(() => { + docsCrawler = new DocsCrawler(mockIde, config, 2, 2, true); + }); + test( + "works on static site", + async () => { + const { pages, crawler } = await runCrawl("https://amplified.dev/"); + expect(pages.length).toBeGreaterThanOrEqual(1); + expect(crawler).toEqual("cheerio"); + }, + TIMEOUT_MS, + ); + }); +}); diff --git a/core/indexing/docs/DocsCrawler.ts b/core/indexing/docs/crawlers/DocsCrawler.ts similarity index 91% rename from core/indexing/docs/DocsCrawler.ts rename to core/indexing/docs/crawlers/DocsCrawler.ts index f7e13804a..bea95c3f4 100644 --- a/core/indexing/docs/DocsCrawler.ts +++ b/core/indexing/docs/crawlers/DocsCrawler.ts @@ -1,11 +1,11 @@ import { URL } from "node:url"; -import { ContinueConfig, IDE } from "../.."; +import { ContinueConfig, IDE } from "../../.."; -import CheerioCrawler from "./crawlers/CheerioCrawler"; -import { ChromiumCrawler, ChromiumInstaller } from "./crawlers/ChromiumCrawler"; -import { DefaultCrawler } from "./crawlers/DefaultCrawler"; -import GitHubCrawler from "./crawlers/GitHubCrawler"; +import CheerioCrawler from "./CheerioCrawler"; +import { ChromiumCrawler, ChromiumInstaller } from "./ChromiumCrawler"; +import { DefaultCrawler } from "./DefaultCrawler"; +import GitHubCrawler from "./GitHubCrawler"; export type PageData = { url: string; diff --git a/core/indexing/docs/crawlers/GitHubCrawler.ts b/core/indexing/docs/crawlers/GitHubCrawler.ts index 22864f6ac..a9867c435 100644 --- a/core/indexing/docs/crawlers/GitHubCrawler.ts +++ b/core/indexing/docs/crawlers/GitHubCrawler.ts @@ -2,7 +2,7 @@ import { URL } from "node:url"; import { Octokit } from "@octokit/rest"; -import { PageData } from "../DocsCrawler"; +import { PageData } from "./DocsCrawler"; class GitHubCrawler { private readonly markdownRegex = new RegExp(/\.(md|mdx)$/); diff --git a/core/indexing/docs/crawlers/github-repos.ts b/core/indexing/docs/crawlers/crawl-test-cases.ts similarity index 87% rename from core/indexing/docs/crawlers/github-repos.ts rename to core/indexing/docs/crawlers/crawl-test-cases.ts index 672145bf7..3784c7daa 100644 --- a/core/indexing/docs/crawlers/github-repos.ts +++ b/core/indexing/docs/crawlers/crawl-test-cases.ts @@ -1,4 +1,4 @@ -export const githubTestRepos: string[] = [ +export const commonDocsRepos: string[] = [ // "https://github.com/freeCodeCamp/freeCodeCamp", "https://github.com/996icu/996.ICU", // "https://github.com/vuejs/vue", @@ -50,3 +50,15 @@ export const githubTestRepos: string[] = [ // "https://github.com/RocketChat/Rocket.Chat", // "https://github.com/open-source-labs/reactime", ]; + +export const commonDocsSites = [ + "https://docs.nestjs.com/", + "https://go.dev/doc/", + "https://clickhouse.com/docs", +]; + +export const failingDocsSites = [ + "https://www.tensorflow.org/api_docs", + "https://docs.anthropic.com/en/docs", + "https://www.rust-lang.org/learn", +];