mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 06:04:40 +00:00
fix(coding-agent): centralize package source normalization and local path parsing fixes #1304
This commit is contained in:
parent
8792ee2a66
commit
6b0f1fefdb
5 changed files with 178 additions and 121 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { mkdirSync, readFileSync, rmSync } from "node:fs";
|
||||
import { mkdirSync, readFileSync, realpathSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
|
|
@ -38,6 +38,20 @@ describe("package commands", () => {
|
|||
rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("should persist global relative local package paths relative to settings.json", async () => {
|
||||
const relativePkgDir = join(projectDir, "packages", "local-package");
|
||||
mkdirSync(relativePkgDir, { recursive: true });
|
||||
|
||||
await main(["install", "./packages/local-package"]);
|
||||
|
||||
const settingsPath = join(agentDir, "settings.json");
|
||||
const settings = JSON.parse(readFileSync(settingsPath, "utf-8")) as { packages?: string[] };
|
||||
expect(settings.packages?.length).toBe(1);
|
||||
const stored = settings.packages?.[0] ?? "";
|
||||
const resolvedFromSettings = realpathSync(join(agentDir, stored));
|
||||
expect(resolvedFromSettings).toBe(realpathSync(relativePkgDir));
|
||||
});
|
||||
|
||||
it("should remove local packages using a path with a trailing slash", async () => {
|
||||
await main(["install", `${packageDir}/`]);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { join, relative } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { DefaultPackageManager, type ProgressEvent, type ResolvedResource } from "../src/core/package-manager.js";
|
||||
import { SettingsManager } from "../src/core/settings-manager.js";
|
||||
|
|
@ -280,6 +280,67 @@ Content`,
|
|||
// Should have attempted clone, not thrown unsupported error
|
||||
expect(events.some((e) => e.type === "start" && e.action === "install")).toBe(true);
|
||||
});
|
||||
|
||||
it("should parse package source types from docs examples", () => {
|
||||
expect((packageManager as any).parseSource("npm:@scope/pkg@1.2.3").type).toBe("npm");
|
||||
expect((packageManager as any).parseSource("npm:pkg").type).toBe("npm");
|
||||
|
||||
expect((packageManager as any).parseSource("git:github.com/user/repo@v1").type).toBe("git");
|
||||
expect((packageManager as any).parseSource("https://github.com/user/repo@v1").type).toBe("git");
|
||||
expect((packageManager as any).parseSource("git@github.com:user/repo@v1").type).toBe("git");
|
||||
expect((packageManager as any).parseSource("ssh://git@github.com/user/repo@v1").type).toBe("git");
|
||||
|
||||
expect((packageManager as any).parseSource("/absolute/path/to/package").type).toBe("local");
|
||||
expect((packageManager as any).parseSource("./relative/path/to/package").type).toBe("local");
|
||||
expect((packageManager as any).parseSource("../relative/path/to/package").type).toBe("local");
|
||||
});
|
||||
|
||||
it("should never parse dot-relative paths as git", () => {
|
||||
const dotSlash = (packageManager as any).parseSource("./packages/agent-timers");
|
||||
expect(dotSlash.type).toBe("local");
|
||||
expect(dotSlash.path).toBe("./packages/agent-timers");
|
||||
|
||||
const dotDotSlash = (packageManager as any).parseSource("../packages/agent-timers");
|
||||
expect(dotDotSlash.type).toBe("local");
|
||||
expect(dotDotSlash.path).toBe("../packages/agent-timers");
|
||||
});
|
||||
});
|
||||
|
||||
describe("settings source normalization", () => {
|
||||
it("should store global local packages relative to agent settings base", () => {
|
||||
const pkgDir = join(tempDir, "packages", "local-global-pkg");
|
||||
mkdirSync(join(pkgDir, "extensions"), { recursive: true });
|
||||
writeFileSync(join(pkgDir, "extensions", "index.ts"), "export default function() {}");
|
||||
|
||||
const added = packageManager.addSourceToSettings("./packages/local-global-pkg");
|
||||
expect(added).toBe(true);
|
||||
|
||||
const settings = settingsManager.getGlobalSettings();
|
||||
expect(settings.packages?.[0]).toBe(relative(agentDir, pkgDir) || ".");
|
||||
});
|
||||
|
||||
it("should store project local packages relative to .pi settings base", () => {
|
||||
const projectPkgDir = join(tempDir, "project-local-pkg");
|
||||
mkdirSync(join(projectPkgDir, "extensions"), { recursive: true });
|
||||
writeFileSync(join(projectPkgDir, "extensions", "index.ts"), "export default function() {}");
|
||||
|
||||
const added = packageManager.addSourceToSettings("./project-local-pkg", { local: true });
|
||||
expect(added).toBe(true);
|
||||
|
||||
const settings = settingsManager.getProjectSettings();
|
||||
expect(settings.packages?.[0]).toBe(relative(join(tempDir, ".pi"), projectPkgDir) || ".");
|
||||
});
|
||||
|
||||
it("should remove local package entries using equivalent path forms", () => {
|
||||
const pkgDir = join(tempDir, "remove-local-pkg");
|
||||
mkdirSync(join(pkgDir, "extensions"), { recursive: true });
|
||||
writeFileSync(join(pkgDir, "extensions", "index.ts"), "export default function() {}");
|
||||
|
||||
packageManager.addSourceToSettings("./remove-local-pkg");
|
||||
const removed = packageManager.removeSourceFromSettings(`${pkgDir}/`);
|
||||
expect(removed).toBe(true);
|
||||
expect(settingsManager.getGlobalSettings().packages ?? []).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("HTTPS git URL parsing (old behavior)", () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue