Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/ephemlets/__serial__/package.test.ts

Raw
import { cp, mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import type { ExtensionUIContext } from "@earendil-works/pi-coding-agent";
import { expect, it } from "vitest";
import { createPiSession } from "../../../test/fixture/create-pi-session.ts";

it.skipIf(process.env["PI_EPHEMLETS_PACKAGE_TEST"] !== "1")(
	"relocated package loads and persists Markdown reports",
	async () => {
		const cwd = await mkdtemp(join(tmpdir(), "ephemlets-package-"));
		let fixture: Awaited<ReturnType<typeof createPiSession>> | undefined;
		try {
			const target = join(cwd, "relocated");
			await cp(
				fileURLToPath(new URL("../../../dist/ephemlets", import.meta.url)),
				target,
				{ recursive: true },
			);
			const pkg = JSON.parse(
				await readFile(join(target, "package.json"), "utf8"),
			) as {
				pi: { extensions: string[] };
			};
			const entry = pkg.pi.extensions[0];
			if (!entry) throw new Error("Ephemlets Pi extension entry missing");
			fixture = await createPiSession({
				cwd,
				extensionPaths: [join(target, entry)],
			});
			expect(fixture.errors).toEqual([]);
			await fixture.session.bindExtensions({
				mode: "tui",
				uiContext: {
					...fixture.session.extensionRunner.getUIContext(),
					custom: async () => undefined,
				} as ExtensionUIContext,
			});
			const owner = fixture;
			const tool = owner.extensions[0]?.tools.get("ephemlet")?.definition;
			if (!tool) throw new Error("Ephemlet tool missing");
			const call = (params: Record<string, unknown>) =>
				tool.execute(
					"relocated",
					params,
					undefined,
					undefined,
					owner.session.extensionRunner.createToolContext(
						"relocated",
						undefined,
					),
				);
			const put = await call({
				action: "put",
				title: "Report",
				source: "# Relocated",
			});
			if (put.content[0]?.type !== "text") throw new Error("No put result");
			const { id } = JSON.parse(put.content[0].text) as { id: string };
			const got = await call({ action: "get", id, include: ["source"] });
			if (got.content[0]?.type !== "text") throw new Error("No get result");
			expect(JSON.parse(got.content[0].text)).toMatchObject({
				title: "Report",
				purpose: "Report",
				source: "# Relocated",
			});
			expect(fixture.providerState.callCount).toBe(0);
		} finally {
			await fixture?.dispose();
			await rm(cwd, { recursive: true, force: true });
		}
	},
	30_000,
);