Files
hyperframes/packages/cli/src/utils/updateCheck.stalepin.test.ts
T
Miguel Ángel 693c385508 fix(cli): commands exit right away even when the network or DNS is broken (#4604)
* fix(cli): commands exit without waiting on update checks or telemetry

The update and skills checks run in a detached child that refreshes the
caches for the next run; the parent only reads the caches. finalizeCli no
longer awaits the telemetry flush: the exit handler flushSync() delivers
what is queued. The events beacon, feedback and figma error paths stop
awaiting flush() too.

* fix(cli): retry a failed background check at most once an hour

Before starting the background worker, the CLI stamps a per-check attempt
time and skips a check it attempted within the hour, so a dead network no
longer starts a worker on every command. The stale-pin notice writes onto a
fresh read, detached spawns set windowsHide, and a test ties the worker path
to its build entry.

* fix(cli): a failed-check stamp dated in the future no longer silences update checks
2026-09-28 02:22:12 +00:00

113 lines
4.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
// Drive `latest` through the REAL getUpdateMeta (defined in the module under
// test) via the mocked config store — self-mocking getUpdateMeta on
// "./updateCheck.js" would only override the export binding, not the internal
// call printStalePinNotice makes to it from within the same module.
let store: Record<string, unknown> = {};
let runningVersion = "0.7.55";
vi.mock("../version.js", () => ({
get VERSION() {
return runningVersion;
},
}));
// isDevMode() is true under vitest (module path ends in .ts), which would
// suppress the notice unconditionally — mock ./env.js like updateCheck.test.ts does.
vi.mock("./env.js", () => ({ isDevMode: () => false }));
// What a process-cached readConfig() returns; null means it matches disk.
let cachedRead: Record<string, unknown> | null = null;
vi.mock("../telemetry/config.js", () => ({
readConfig: () => ({ ...(cachedRead ?? store) }),
readConfigFresh: () => ({ ...store }),
writeConfig: (c: Record<string, unknown>) => {
store = { ...c };
return true;
},
}));
import { printStalePinNotice } from "./updateCheck.js";
describe("printStalePinNotice", () => {
let dir: string;
let writes: string[];
const origWrite = process.stderr.write.bind(process.stderr);
beforeEach(() => {
store = { latestVersion: "0.7.55" };
cachedRead = null;
runningVersion = "0.7.55";
writes = [];
dir = mkdtempSync(join(tmpdir(), "hf-pin-"));
process.stderr.write = ((s: unknown) => {
writes.push(String(s));
return true;
}) as typeof process.stderr.write;
delete process.env.CI;
delete process.env.HYPERFRAMES_NO_UPDATE_CHECK;
});
afterEach(() => {
process.stderr.write = origWrite;
rmSync(dir, { recursive: true, force: true });
});
it("keeps what the background check wrote after this process read the settings", () => {
writeFileSync(
join(dir, "package.json"),
JSON.stringify({ scripts: { render: "npx --yes hyperframes@0.7.48 render" } }),
);
cachedRead = { ...store };
store = { ...store, lastUpdateCheck: "2026-09-27T00:00:00.000Z" };
printStalePinNotice(dir);
expect(store).toMatchObject({ lastUpdateCheck: "2026-09-27T00:00:00.000Z" });
expect(store["lastStalePinNoticeAt"]).toEqual(expect.any(Number));
});
it("warns once when the project pins an older version", () => {
writeFileSync(
join(dir, "package.json"),
JSON.stringify({ scripts: { render: "npx --yes hyperframes@0.7.48 render" } }),
);
printStalePinNotice(dir);
printStalePinNotice(dir); // throttled — second call silent
expect(writes.join("")).toContain("0.7.48");
expect(writes.join("")).toContain("upgrade --project");
expect(writes.filter((w) => w.includes("upgrade --project")).length).toBe(1);
});
it("silent when project pin is current", () => {
writeFileSync(
join(dir, "package.json"),
JSON.stringify({ scripts: { render: "npx --yes hyperframes@0.7.55 render" } }),
);
printStalePinNotice(dir);
expect(writes.join("")).toBe("");
});
it("warns on every run when an older CLI than the pin is running", () => {
store = { latestVersion: "0.8.71" };
runningVersion = "0.7.71";
writeFileSync(
join(dir, "package.json"),
JSON.stringify({ scripts: { check: "npx --yes hyperframes@0.8.66 check" } }),
);
printStalePinNotice(dir);
printStalePinNotice(dir);
const mismatch = writes.filter((w) => w.includes("This is hyperframes 0.7.71"));
expect(mismatch.length).toBe(2);
expect(mismatch[0]).toContain("npx hyperframes@0.8.66");
expect(writes.join("")).not.toContain("upgrade --project");
});
it("silent under CI", () => {
process.env.CI = "true";
writeFileSync(
join(dir, "package.json"),
JSON.stringify({ scripts: { render: "npx --yes hyperframes@0.7.48 render" } }),
);
printStalePinNotice(dir);
expect(writes.join("")).toBe("");
});
});