Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

feat(updater): download releases before restart

Yuting Zhong1fc1547d7600
diff --git a/notfair/src/components/sidebar-version.test.tsx b/notfair/src/components/sidebar-version.test.tsx
new file mode 100644
index 00000000..c5184768
--- /dev/null
+++ b/notfair/src/components/sidebar-version.test.tsx
@@ -0,0 +1,115 @@
+// @vitest-environment jsdom
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+
+import { SidebarVersion } from "@/components/sidebar-version";
+
+const toastSuccess = vi.hoisted(() => vi.fn());
+const toastError = vi.hoisted(() => vi.fn());
+vi.mock("sonner", () => ({
+ toast: { success: toastSuccess, error: toastError },
+}));
+
+function jsonResponse(body: unknown, ok = true) {
+ return { ok, json: async () => body } as Response;
+}
+
+describe("SidebarVersion", () => {
+ const fetchMock = vi.fn();
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.stubGlobal("fetch", fetchMock);
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it("automatically installs an available update before asking to apply it", async () => {
+ fetchMock
+ .mockResolvedValueOnce(
+ jsonResponse({ current: "0.9.13", latest: "0.9.14", has_update: true }),
+ )
+ .mockResolvedValueOnce(jsonResponse({ ok: true, can_restart: true }));
+
+ render(<SidebarVersion />);
+
+ await waitFor(() => {
+ expect(fetchMock).toHaveBeenCalledWith("/api/upgrade", { method: "POST" });
+ });
+ expect(
+ await screen.findByRole("button", { name: /update to v0\.9\.14/i }),
+ ).toBeEnabled();
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(screen.queryByRole("button", { name: /restart now/i })).toBeNull();
+ });
+
+ it("restarts immediately when the user applies the installed update", async () => {
+ fetchMock
+ .mockResolvedValueOnce(
+ jsonResponse({ current: "0.9.13", latest: "0.9.14", has_update: true }),
+ )
+ .mockResolvedValueOnce(jsonResponse({ ok: true, can_restart: true }))
+ // Keep the restart request pending so the component remains in its
+ // visible restarting state without beginning the version poll.
+ .mockReturnValueOnce(new Promise<Response>(() => {}));
+
+ render(<SidebarVersion />);
+ fireEvent.click(
+ await screen.findByRole("button", { name: /update to v0\.9\.14/i }),
+ );
+
+ await waitFor(() => {
+ expect(fetchMock).toHaveBeenCalledWith("/api/restart", { method: "POST" });
+ });
+ expect(screen.queryByRole("button", { name: /restart now/i })).toBeNull();
+ expect(
+ screen.getByRole("button", { name: /restarting/i }),
+ ).toBeDisabled();
+ });
+
+ it("keeps the manual restart fallback for foreground and dev servers", async () => {
+ fetchMock
+ .mockResolvedValueOnce(
+ jsonResponse({ current: "0.9.13", latest: "0.9.14", has_update: true }),
+ )
+ .mockResolvedValueOnce(
+ jsonResponse({
+ ok: true,
+ can_restart: false,
+ note: "Restart NotFair from your terminal.",
+ }),
+ );
+
+ render(<SidebarVersion />);
+
+ expect(await screen.findByText("Restart to apply")).toBeInTheDocument();
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(toastSuccess).toHaveBeenCalledWith(
+ "Restart NotFair from your terminal.",
+ { duration: 15_000 },
+ );
+ });
+
+ it("offers a retry without repeatedly downloading after a failure", async () => {
+ fetchMock
+ .mockResolvedValueOnce(
+ jsonResponse({ current: "0.9.13", latest: "0.9.14", has_update: true }),
+ )
+ .mockResolvedValueOnce(
+ jsonResponse({ ok: false, error: "registry unavailable" }, false),
+ )
+ .mockResolvedValueOnce(jsonResponse({ ok: true, can_restart: true }));
+
+ render(<SidebarVersion />);
+ const retry = await screen.findByRole("button", { name: /retry update/i });
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+
+ fireEvent.click(retry);
+ expect(
+ await screen.findByRole("button", { name: /update to v0\.9\.14/i }),
+ ).toBeEnabled();
+ expect(fetchMock).toHaveBeenCalledTimes(3);
+ });
+});
diff --git a/notfair/src/components/sidebar-version.tsx b/notfair/src/components/sidebar-version.tsx
index 287134ec..8732d88d 100644
--- a/notfair/src/components/sidebar-version.tsx
+++ b/notfair/src/components/sidebar-version.tsx
@@ -1,7 +1,7 @@
"use client";
-import { useEffect, useState } from "react";
-import { Loader2, RefreshCw, Sparkles } from "lucide-react";
+import { useEffect, useRef, useState } from "react";
+import { Loader2, Sparkles } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
@@ -12,18 +12,25 @@ type VersionInfo = {
has_update: boolean;
};
-type Phase = "idle" | "upgrading" | "restartable" | "restarting" | "manual";
+type Phase =
+ | "idle"
+ | "downloading"
+ | "ready"
+ | "restarting"
+ | "manual"
+ | "failed";
/**
* Sidebar footer: current version + the update flow. When npm reports a
- * newer version an Update button appears; clicking it runs
- * `npm i -g notfair@latest` via /api/upgrade. Background/launchd servers
- * then offer one-click Restart (/api/restart) and the page reloads on the
- * new version; foreground/dev runs are told to restart from the terminal.
+ * newer version, it installs automatically through /api/upgrade. Once ready,
+ * the Update button restarts background/launchd servers through /api/restart
+ * and reloads on the new version. Foreground/dev runs are told to restart
+ * from the terminal because the app does not own those processes.
*/
export function SidebarVersion() {
const [info, setInfo] = useState<VersionInfo | null>(null);
const [phase, setPhase] = useState<Phase>("idle");
+ const autoDownloadVersion = useRef<string | null>(null);
useEffect(() => {
let cancelled = false;
@@ -40,9 +47,16 @@ export function SidebarVersion() {
};
}, []);
- async function upgrade() {
- if (!info?.has_update || phase !== "idle") return;
- setPhase("upgrading");
+ useEffect(() => {
+ if (!info?.has_update || !info.latest) return;
+ if (autoDownloadVersion.current === info.latest) return;
+ autoDownloadVersion.current = info.latest;
+ void downloadUpdate(info);
+ }, [info]);
+
+ async function downloadUpdate(version: VersionInfo) {
+ if (!version.has_update) return;
+ setPhase("downloading");
try {
const res = await fetch("/api/upgrade", { method: "POST" });
const body = (await res.json()) as
@@ -58,12 +72,12 @@ export function SidebarVersion() {
} else {
toast.error(msg, { duration: 8_000 });
}
- setPhase("idle");
+ setPhase("failed");
return;
}
if (body.can_restart) {
- setPhase("restartable");
- toast.success(`v${info.latest} installed — restart to apply.`, {
+ setPhase("ready");
+ toast.success(`v${version.latest} installed — click Update to apply.`, {
duration: 10_000,
});
} else {
@@ -75,12 +89,12 @@ export function SidebarVersion() {
}
} catch (err) {
toast.error(err instanceof Error ? err.message : String(err));
- setPhase("idle");
+ setPhase("failed");
}
}
async function restart() {
- if (phase !== "restartable") return;
+ if (phase !== "ready") return;
setPhase("restarting");
try {
const res = await fetch("/api/restart", { method: "POST" });
@@ -137,35 +151,39 @@ export function SidebarVersion() {
NotFair v{info.current}
</span>
- {info.has_update && (phase === "idle" || phase === "upgrading") && (
+ {info.has_update && (phase === "idle" || phase === "downloading") && (
<Button
size="sm"
variant="outline"
- disabled={phase === "upgrading"}
- onClick={upgrade}
- title={`Update available: v${info.latest}`}
+ disabled
+ title={`Downloading update v${info.latest}`}
className="h-6 gap-1 px-2 text-[10.5px] font-medium"
>
- {phase === "upgrading" ? (
- <>
- <Loader2 className="h-3 w-3 animate-spin" />
- Updating…
- </>
- ) : (
- <>
- <Sparkles className="h-3 w-3" />
- v{info.latest} available
- </>
- )}
+ <Loader2 className="h-3 w-3 animate-spin" />
+ Downloading…
</Button>
)}
- {(phase === "restartable" || phase === "restarting") && (
+ {phase === "failed" && (
+ <Button
+ size="sm"
+ variant="outline"
+ onClick={() => void downloadUpdate(info)}
+ title={`Retry update v${info.latest}`}
+ className="h-6 gap-1 px-2 text-[10.5px] font-medium"
+ >
+ <Sparkles className="h-3 w-3" />
+ Retry update
+ </Button>
+ )}
+
+ {(phase === "ready" || phase === "restarting") && (
<Button
size="sm"
variant="outline"
disabled={phase === "restarting"}
onClick={restart}
+ title={`Restart and update to v${info.latest}`}
className="h-6 gap-1 px-2 text-[10.5px] font-medium"
>
{phase === "restarting" ? (
@@ -175,8 +193,8 @@ export function SidebarVersion() {
</>
) : (
<>
- <RefreshCw className="h-3 w-3" />
- Restart now
+ <Sparkles className="h-3 w-3" />
+ Update to v{info.latest}
</>
)}
</Button>