fix(coding-agent): install checked pi update version

This commit is contained in:
Armin Ronacher
2026-06-21 14:46:07 +02:00
parent d93b92baca
commit bc0db64350
5 changed files with 105 additions and 42 deletions
@@ -374,7 +374,7 @@ describe("package commands", () => {
}
});
it("uses global npmCommand and current package name for forced self updates without checking the api", async () => {
it("uses the update check version for forced self updates even when current", async () => {
const globalPrefix = join(tempDir, "global-prefix");
const projectPrefix = join(tempDir, "project-prefix");
const selfPackageDir = join(globalPrefix, "lib", "node_modules", "@earendil-works", "pi-coding-agent");
@@ -402,7 +402,7 @@ else fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(args));
value: join(selfPackageDir, "dist", "cli.js"),
configurable: true,
});
const fetchMock = vi.fn();
const fetchMock = vi.fn(async () => Response.json({ version: VERSION }));
vi.stubGlobal("fetch", fetchMock);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
@@ -413,11 +413,14 @@ else fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(args));
expect(process.exitCode).toBeUndefined();
expect(errorSpy).not.toHaveBeenCalled();
expect(fetchMock).not.toHaveBeenCalled();
expect(fetchMock).toHaveBeenCalledOnce();
const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n");
const recordedArgs = JSON.parse(readFileSync(recordPath, "utf-8")) as string[];
expect(recordedArgs).toContain(globalPrefix);
expect(recordedArgs).toContain(PACKAGE_NAME);
expect(recordedArgs).toContain(`${PACKAGE_NAME}@${VERSION}`);
expect(recordedArgs).not.toContain(PACKAGE_NAME);
expect(recordedArgs).not.toContain(projectPrefix);
expect(stdout).toContain(`Updated pi from ${VERSION} to ${VERSION}`);
} finally {
logSpy.mockRestore();
errorSpy.mockRestore();
@@ -446,7 +449,8 @@ else fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(args));
value: join(selfPackageDir, "dist", "cli.js"),
configurable: true,
});
const fetchMock = vi.fn(async () => Response.json({ version: getNewerPatchVersion() }));
const targetVersion = getNewerPatchVersion();
const fetchMock = vi.fn(async () => Response.json({ version: targetVersion }));
vi.stubGlobal("fetch", fetchMock);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
@@ -458,8 +462,11 @@ else fs.writeFileSync(${JSON.stringify(recordPath)},JSON.stringify(args));
expect(process.exitCode).toBeUndefined();
expect(errorSpy).not.toHaveBeenCalled();
expect(fetchMock).toHaveBeenCalledOnce();
const stdout = logSpy.mock.calls.map(([message]) => String(message)).join("\n");
const recordedArgs = JSON.parse(readFileSync(recordPath, "utf-8")) as string[];
expect(recordedArgs).toContain(PACKAGE_NAME);
expect(recordedArgs).toContain(`${PACKAGE_NAME}@${targetVersion}`);
expect(recordedArgs).not.toContain(PACKAGE_NAME);
expect(stdout).toContain(`Updated pi from ${VERSION} to ${targetVersion}`);
} finally {
logSpy.mockRestore();
errorSpy.mockRestore();
@@ -509,7 +516,7 @@ else {
const recordedCalls = JSON.parse(readFileSync(recordPath, "utf-8")) as string[][];
expect(recordedCalls).toEqual([
expect.arrayContaining(["uninstall", "-g", PACKAGE_NAME]),
expect.arrayContaining(["install", "-g", activePackageName]),
expect.arrayContaining(["install", "-g", `${activePackageName}@0.73.0`]),
]);
} finally {
logSpy.mockRestore();
@@ -565,7 +572,7 @@ if(args.includes("install")) process.exit(23);
const recordedCalls = JSON.parse(readFileSync(recordPath, "utf-8")) as string[][];
expect(recordedCalls).toEqual([
expect.arrayContaining(["uninstall", "-g", PACKAGE_NAME]),
expect.arrayContaining(["install", "-g", activePackageName]),
expect.arrayContaining(["install", "-g", `${activePackageName}@0.73.0`]),
]);
} finally {
logSpy.mockRestore();