Files
kandev/apps/web/components/settings/system/updates-card.test.tsx
T
José Almeida 041a19f060 feat: service-managed UI self-update (#1210)
* feat: add service-managed self-update

* chore: add temporary self-update test scripts

* chore: simplify temporary script markdown fences

* chore: run frontend in temporary self-update scripts

* fix: show self-update job progress after apply

* chore: add real npm self-update test script

* fix: isolate npm self-update prefix

* fix: detect npm service installs from bin shim

* chore: reset service before real npm update test

* chore: expand real npm status diagnostics

* fix: preserve service home during self-update

* fix(cli): tee self-update helper output to a log file

* fix(cli): avoid launchd bootstrap race when reloading the macOS service

* fix(backend): run macOS self-update helper once via transient launchd plist

* fix(web): show self-update progress and lock the apply button until the update lands

* fix(cli): reinstall via the kandev wrapper on Homebrew self-update

* chore: remove temp self-update test scripts

* fix(web): drop unused apply path from useUpdates hook

* fix(backend): snapshot install state once in apply preflight

* docs: align self-update docs with transient LaunchAgent flow

* chore(release): run cli bundle from apps workspace root

* fix(backend): guard against concurrent self-update applies

* fix(backend): check scheme and helper paths in self-update apply

* fix(backend): drop duplicate binary name from npx manual command

* fix(cli): allow --help for service self-update without --intent

* fix(cli): harden self-update dir perms and darwin uid resolution

* fix(web): set required POST fields last in applyUpdate

* docs: use JSON body notation for the apply endpoint

* fix(backend): expire self-update guard so a stuck helper cannot wedge apply

* refactor(cli): collapse redundant writeSync branch in self-update log
2026-06-01 17:49:15 +01:00

132 lines
4.1 KiB
TypeScript

import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { UpdatesResponse } from "@/lib/types/system";
import type { SelfUpdateController } from "@/hooks/domains/system/use-self-update";
const mocks = vi.hoisted(() => ({
useUpdates: vi.fn(),
useSelfUpdate: vi.fn(),
}));
vi.mock("@/hooks/domains/system/use-updates", () => ({
useUpdates: mocks.useUpdates,
}));
vi.mock("@/hooks/domains/system/use-self-update", () => ({
useSelfUpdate: mocks.useSelfUpdate,
}));
// The @kandev/ui Spinner source trips the classic JSX runtime under vitest;
// stub it so the card (and progress block) can render in jsdom.
vi.mock("@kandev/ui/spinner", () => ({
Spinner: () => null,
}));
import { UpdatesCard } from "./updates-card";
const APPLY_TESTID = "system-updates-apply";
function updates(overrides: Partial<UpdatesResponse> = {}): UpdatesResponse {
return {
current: "v1.0.0",
latest: "v1.0.1",
latest_url: "https://example/v1.0.1",
latest_checked_at: "2026-05-29T00:00:00.000Z",
update_available: true,
install: {
running_as_service: true,
managed_service: true,
mode: "user",
manager: "systemd",
kind: "npm",
},
apply_supported: true,
...overrides,
};
}
function selfUpdate(overrides: Partial<SelfUpdateController> = {}): SelfUpdateController {
return {
phase: "idle",
targetVersion: null,
errorMessage: null,
isUpdating: false,
start: vi.fn().mockResolvedValue(undefined),
dismiss: vi.fn(),
...overrides,
};
}
describe("UpdatesCard self-update", () => {
beforeEach(() => {
mocks.useUpdates.mockReset();
mocks.useSelfUpdate.mockReset();
mocks.useSelfUpdate.mockReturnValue(selfUpdate());
});
afterEach(() => {
cleanup();
});
it("does not render Apply update when the install is not a managed service", () => {
mocks.useUpdates.mockReturnValue({
updates: updates({
install: { running_as_service: false, managed_service: false },
apply_supported: false,
apply_unsupported_reason: "Kandev is not running as a managed service.",
manual_commands: ["kandev service install"],
}),
check: vi.fn(),
reload: vi.fn(),
});
render(<UpdatesCard />);
expect(screen.queryByTestId(APPLY_TESTID)).toBeNull();
expect(screen.getByTestId("system-updates-manual").textContent).toContain(
"Kandev is not running as a managed service.",
);
});
it("starts the self-update only after confirmation", async () => {
const start = vi.fn().mockResolvedValue(undefined);
mocks.useUpdates.mockReturnValue({ updates: updates(), check: vi.fn(), reload: vi.fn() });
mocks.useSelfUpdate.mockReturnValue(selfUpdate({ start }));
render(<UpdatesCard />);
fireEvent.click(screen.getByTestId(APPLY_TESTID));
fireEvent.click(await screen.findByTestId("system-updates-apply-confirm"));
await waitFor(() => expect(start).toHaveBeenCalledTimes(1));
});
it("hides the Apply button and shows progress while updating", () => {
mocks.useUpdates.mockReturnValue({ updates: updates(), check: vi.fn(), reload: vi.fn() });
mocks.useSelfUpdate.mockReturnValue(
selfUpdate({ phase: "restarting", targetVersion: "v1.0.1", isUpdating: true }),
);
render(<UpdatesCard />);
expect(screen.queryByTestId(APPLY_TESTID)).toBeNull();
const progress = screen.getByTestId("system-updates-progress");
expect(progress.getAttribute("data-phase")).toBe("restarting");
expect(progress.textContent).toContain("Restarting Kandev");
});
it("shows the updated confirmation when done", () => {
mocks.useUpdates.mockReturnValue({ updates: updates(), check: vi.fn(), reload: vi.fn() });
mocks.useSelfUpdate.mockReturnValue(
selfUpdate({ phase: "done", targetVersion: "v1.0.1", isUpdating: false }),
);
render(<UpdatesCard />);
expect(screen.queryByTestId(APPLY_TESTID)).toBeNull();
expect(screen.getByTestId("system-updates-progress").textContent).toContain(
"Updated to v1.0.1",
);
});
});