import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { countTokens, Devdocs } from "../src/engine.ts"; import type { Options } from "../src/types.ts"; const root = await mkdtemp(join(tmpdir(), "devdocs-live-")); const cases = [ { source: "local", target: "node", query: "inspect" }, { source: "devdocs", target: "git", query: "rebase" }, { source: "upstream", target: "https://www.nushell.sh/book/pipelines.html", query: "pipeline", }, { source: "context7", target: "/react/react", query: "useState" }, ]; try { for (const c of cases) { const options: Options = { databasePath: join(root, c.source + ".db"), enabledSources: [c.source], sources: [], offline: false, cacheBytes: 16 * 1024 * 1024, ttlMs: 60000, }; const engine = new Devdocs(options); let ref: string | undefined; try { const found = await engine.search({ target: c.target, query: c.query, cwd: root, signal: AbortSignal.timeout(30000), }); if (found.status !== "ok") throw new Error(JSON.stringify(found)); ref = found.results[0]?.ref; if (!ref) throw new Error("empty results"); const read = await engine.read([ref], 2000, root); if ( !read.sections[0]?.content || countTokens(JSON.stringify(read)) > 2000 ) throw new Error("invalid bounded read"); process.stdout.write( JSON.stringify({ source: c.source, status: "ok", resolvedVersion: read.sections[0]?.version, origin: read.sections[0]?.origin, readCodePoints: countTokens(JSON.stringify(read)), }) + "\n", ); } catch (error) { process.stdout.write( JSON.stringify({ source: c.source, status: "failed", reason: error instanceof Error ? error.message : "failed", }) + "\n", ); process.exitCode = 1; } finally { await engine.close(); } if (ref) { const offline = new Devdocs({ ...options, offline: true }); try { const found = await offline.search({ target: c.target, query: c.query, cwd: root, }); if (found.status !== "ok") throw new Error("offline search unavailable"); await offline.read([ref], 2000, root); process.stdout.write( JSON.stringify({ source: c.source, offline: "ok" }) + "\n", ); } finally { await offline.close(); } } } } finally { await rm(root, { recursive: true, force: true }); }