feat: Update check against pi.dev (#3877)

This commit is contained in:
Armin Ronacher
2026-04-28 12:48:27 +02:00
committed by GitHub
parent 4166cfa921
commit c745efc0d0
9 changed files with 216 additions and 28 deletions

View File

@@ -0,0 +1,12 @@
import { describe, expect, it } from "vitest";
import { getPiUserAgent } from "../src/utils/pi-user-agent.js";
describe("getPiUserAgent", () => {
it("formats the user agent expected by pi.dev", () => {
const runtime = process.versions.bun ? `bun/${process.versions.bun}` : `node/${process.version}`;
const userAgent = getPiUserAgent("1.2.3");
expect(userAgent).toBe(`pi/1.2.3 (${process.platform}; ${runtime}; ${process.arch})`);
expect(userAgent).toMatch(/^pi\/[^\s()]+ \([^;()]+;\s*[^;()]+;\s*[^()]+\)$/);
});
});

View File

@@ -0,0 +1,67 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import {
checkForNewPiVersion,
comparePackageVersions,
getLatestPiVersion,
isNewerPackageVersion,
} from "../src/utils/version-check.js";
const originalSkipVersionCheck = process.env.PI_SKIP_VERSION_CHECK;
const originalOffline = process.env.PI_OFFLINE;
afterEach(() => {
vi.unstubAllGlobals();
if (originalSkipVersionCheck === undefined) {
delete process.env.PI_SKIP_VERSION_CHECK;
} else {
process.env.PI_SKIP_VERSION_CHECK = originalSkipVersionCheck;
}
if (originalOffline === undefined) {
delete process.env.PI_OFFLINE;
} else {
process.env.PI_OFFLINE = originalOffline;
}
});
describe("version checks", () => {
it("compares package versions", () => {
expect(comparePackageVersions("0.70.6", "0.70.5")).toBeGreaterThan(0);
expect(comparePackageVersions("0.70.5", "0.70.5")).toBe(0);
expect(comparePackageVersions("0.70.4", "0.70.5")).toBeLessThan(0);
expect(isNewerPackageVersion("0.70.5", "0.70.5")).toBe(false);
expect(isNewerPackageVersion("0.70.6", "0.70.5")).toBe(true);
});
it("returns only newer versions", async () => {
const fetchMock = vi.fn(async () => Response.json({ version: "1.2.3" }));
vi.stubGlobal("fetch", fetchMock);
await expect(checkForNewPiVersion("1.2.3")).resolves.toBeUndefined();
await expect(checkForNewPiVersion("1.2.2")).resolves.toBe("1.2.3");
});
it("uses the pi.dev version check api with a pi user agent", async () => {
const fetchMock = vi.fn(async () => Response.json({ version: "1.2.4" }));
vi.stubGlobal("fetch", fetchMock);
await expect(getLatestPiVersion("1.2.3")).resolves.toBe("1.2.4");
expect(fetchMock).toHaveBeenCalledWith(
"https://pi.dev/api/latest-version",
expect.objectContaining({
headers: expect.objectContaining({
"User-Agent": expect.stringMatching(/^pi\/1\.2\.3 /),
accept: "application/json",
}),
}),
);
});
it("skips api calls when version checks are disabled", async () => {
process.env.PI_SKIP_VERSION_CHECK = "1";
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
await expect(getLatestPiVersion("1.2.3")).resolves.toBeUndefined();
expect(fetchMock).not.toHaveBeenCalled();
});
});