Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/devdocs/__serial__/package.ts

Raw
import assert from "node:assert/strict";
import { cp, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import {
	createPiSession,
	type PiSession,
} from "../../../test/fixture/create-pi-session.ts";

const root = await mkdtemp(join(tmpdir(), "devdocs-package-"));
let box: PiSession | undefined;
try {
	const packagePath = join(root, "package");
	await cp(
		fileURLToPath(new URL("../../../dist/devdocs", import.meta.url)),
		packagePath,
		{ recursive: true },
	);
	const manual = join(root, "manual.md");
	await writeFile(
		manual,
		"# Transactions\n\nAtomic package fixture commits safely.",
	);
	const cwd = join(root, "project");
	await mkdir(cwd);
	box = await createPiSession({
		cwd,
		extensionPaths: [join(packagePath, "index.ts")],
		env: { PI_DEVDOCS_DEBUG: "0", PI_DEVDOCS_DATABASE_PATH: undefined },
		userSettings: {
			devdocs: {
				databasePath: join(root, "cache.db"),
				offline: true,
				enabledSources: ["fixture"],
				sources: [
					{
						id: "fixture",
						target: "package-fixture",
						path: manual,
						version: "1.0.0",
					},
				],
			},
		},
	});
	assert.deepEqual(box.errors, []);
	const tool = box.extensions[0]?.tools.get("devdocs")?.definition;
	assert.ok(tool);
	const ctx = box.session.extensionRunner.createToolContext(
		"fixture",
		undefined,
	);
	const found = await tool.execute(
		"search",
		{
			action: "search",
			target: "package-fixture",
			query: "atomic",
			version: "1.0.0",
		},
		undefined,
		undefined,
		ctx,
	);
	assert.ok(
		found.details &&
			typeof found.details === "object" &&
			!Array.isArray(found.details) &&
			"results" in found.details,
	);
	const refs = (found.details.results as { ref: string }[]).map((s) => s.ref);
	const read = await tool.execute(
		"read",
		{ action: "read", refs, budget: 2000 },
		undefined,
		undefined,
		ctx,
	);
	assert.match(JSON.stringify(read.details), /Atomic package fixture/);
	assert.ok(JSON.stringify(read.details).length <= 2000);
	console.log(
		"OK relocated devdocs package search/read + native SQLite worker",
	);
} finally {
	await box?.dispose();
	await rm(root, { recursive: true, force: true });
}