co-mono/packages/coding-agent/test/package-command-paths.test.ts

53 lines
1.8 KiB
TypeScript

import { mkdirSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { ENV_AGENT_DIR } from "../src/config.js";
import { main } from "../src/main.js";
describe("package commands", () => {
let tempDir: string;
let agentDir: string;
let projectDir: string;
let packageDir: string;
let originalCwd: string;
let originalAgentDir: string | undefined;
beforeEach(() => {
tempDir = join(tmpdir(), `pi-package-commands-${Date.now()}-${Math.random().toString(36).slice(2)}`);
agentDir = join(tempDir, "agent");
projectDir = join(tempDir, "project");
packageDir = join(tempDir, "local-package");
mkdirSync(agentDir, { recursive: true });
mkdirSync(projectDir, { recursive: true });
mkdirSync(packageDir, { recursive: true });
originalCwd = process.cwd();
originalAgentDir = process.env[ENV_AGENT_DIR];
process.env[ENV_AGENT_DIR] = agentDir;
process.chdir(projectDir);
});
afterEach(() => {
process.chdir(originalCwd);
if (originalAgentDir === undefined) {
delete process.env[ENV_AGENT_DIR];
} else {
process.env[ENV_AGENT_DIR] = originalAgentDir;
}
rmSync(tempDir, { recursive: true, force: true });
});
it("should remove local packages using a path with a trailing slash", async () => {
await main(["install", `${packageDir}/`]);
const settingsPath = join(agentDir, "settings.json");
const installedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] };
expect(installedSettings.packages?.length).toBe(1);
await main(["remove", `${packageDir}/`]);
const removedSettings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] };
expect(removedSettings.packages ?? []).toHaveLength(0);
});
});