Files
openclaw/ui/src/app/update-confirmation.runtime.test.ts
Vyctor H. Brzezowski e7ba2e1f6b fix(ui): give the Gateway update a visible, truthful outcome (#121686)
* fix(ui): give the Gateway update a visible, truthful outcome

Confirming an update in the Control UI could end in nothing the operator
could see. The sidebar card only knew `updateRunning`, which clears as soon
as `update.run` answers, so a dev-channel handoff — git pull, install, full
build, restart — spent minutes back on the stale "N commits behind" call to
action. A verified install then published a null banner, so success was
never announced at all, and the reload that a verified install triggers
would have destroyed any in-memory notice anyway.

The lifecycle was already recorded; nothing rendered it. Feed the sidebar
the reconciliation fact the settings page already uses, narrate the wait in
the shell callout that survives the restart, and record the verified result
so it outlives the reload it causes.

Failures now name their cause. The updater already stores the failing step
and its captured output in the restart sentinel and `update.status` returns
it whole, so a disk-full install reads "The update failed at install:
ENOSPC: no space left on device" instead of a reason slug plus "see the
gateway logs". Missing handoff-reason guidance is filled in, and the
duplicate post-restart banner resolver is folded into the shared one.

Also fixes a callout clipped by the fixed shell chrome controls, and the
"Available 246 commits behind" double framing in the confirmation.

* fix(ui): report the Gateway update in the dialog that starts it

Follow-up to the first pass: the shell-top callout was the wrong surface for
this. The dialog the operator clicked is now the one that reports the whole
update, and the ambient surfaces are the fallback for anyone who leaves it.

Confirming no longer closes the dialog. It becomes stateful — the danger
button turns into a disabled "Updating…" with a spinner, and the body
narrates the phases in place: installing, then "The Gateway is restarting.
This page disconnects and reconnects on its own." The dialog is mounted on
document.body, outside the shell, so losing the connection cannot unmount
the only surface still reporting. A request the Gateway never accepts stops
spinning after a bounded grace and says so.

A failure lands in the dialog with its recorded cause and next step. Success
still arrives after the reload a verified install triggers, so it stays a
toast — no dialog exists by then.

The shell-top status callout is gone. The same outcome now renders beside
the sidebar update card that started the update, and stays hidden while the
dialog owns the report so the two surfaces never duplicate each other.

Splits the wire-payload readers out of update-overlay-helpers into
update-schedule-dto and moves update.run answer classification next to them,
keeping both owners under the file-size budget.

* fix(ui): keep the update dialog open through the install in E2E and use a defined warn token

* refactor(ui): wire the update progress stream into the floating card and drop unused video capture

* fix(ui): report the current update, not the previous failure

The dialog subscribed to progress before starting the update. Producers
replay their current snapshot synchronously on subscribe, so a retry
still carrying the previous attempt's failure banner painted that old
error as this update's outcome.

Start the request first, and treat a failure present on the subscribe
emit as the previous attempt's: an accepted run clears the banner before
its first await, so a failure that survives means the request was
refused, which the accept timer already reports truthfully.

* fix(ui): keep update helpers internal

* chore(perf): raise startup-JS budget to 350 KiB

Approved by maintainer Vyctor on 2026-08-11.

Trigger: after #121734, main measured 326650 B—6 B below the prior 319 KiB hard ceiling. Current main subsequently moved the ceiling to 320 KiB; this records the authorized final 350 KiB decision for #121686.

* fix(ui): announce update success without session storage

Show the verified install identity directly when the current document does not reload, so denied session storage cannot suppress the operator-visible outcome.

* test(ui): isolate update toast in overlay tests

The Node-only overlay suite now reaches the direct non-reload success toast. Mock the DOM-owned toast boundary so reconciliation assertions can complete without a browser document.
2026-08-11 13:49:12 -03:00

285 lines
9.8 KiB
TypeScript

/* @vitest-environment jsdom */
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import type { UpdateAvailable, UpdateScheduleState } from "../api/types.ts";
import { getRenderedModalDialog, installDialogPolyfill } from "../test-helpers/modal-dialog.ts";
import { confirmAndStartUpdateRuntime } from "./update-confirmation.runtime.ts";
import type { UpdateProgress } from "./update-confirmation.ts";
/** Drives the dialog the way the shell does: one live lifecycle stream. */
function createProgressStream() {
let emit: ((progress: UpdateProgress) => void) | null = null;
let stopped = false;
return {
get stopped() {
return stopped;
},
watchUpdateProgress: (listener: (progress: UpdateProgress) => void) => {
emit = listener;
listener({ busy: false, connected: true, failure: null });
return () => {
stopped = true;
};
},
async push(progress: UpdateProgress) {
emit?.(progress);
await Promise.resolve();
},
};
}
const UPDATE_AVAILABLE: UpdateAvailable = {
channel: "stable",
currentVersion: "1.0.0",
latestVersion: "2.0.0",
};
let restoreDialogPolyfill: () => void;
let originalWebkit: PropertyDescriptor | undefined;
function findButton(label: string): HTMLButtonElement {
const button = [...document.body.querySelectorAll("button")].find(
(candidate) => candidate.textContent?.trim() === label,
);
if (!(button instanceof HTMLButtonElement)) {
throw new Error(`Expected ${label} button`);
}
return button;
}
function installNativeBridge(): ReturnType<typeof vi.fn> {
const postMessage = vi.fn();
Object.defineProperty(window, "webkit", {
configurable: true,
value: { messageHandlers: { openclawUpdate: { postMessage } } },
});
return postMessage;
}
function startUpdate(
overrides: {
startGatewayUpdate?: () => void;
updateAvailable?: UpdateAvailable | null;
updateSchedule?: UpdateScheduleState | null;
viaNativeApp?: boolean;
watchUpdateProgress?: (listener: (progress: UpdateProgress) => void) => () => void;
} = {},
) {
const startGatewayUpdate = vi.fn();
const settled = confirmAndStartUpdateRuntime({
...(overrides.watchUpdateProgress
? { watchUpdateProgress: overrides.watchUpdateProgress }
: {}),
startGatewayUpdate: overrides.startGatewayUpdate ?? startGatewayUpdate,
updateAvailable:
overrides.updateAvailable === undefined ? UPDATE_AVAILABLE : overrides.updateAvailable,
updateSchedule: overrides.updateSchedule ?? null,
viaNativeApp: overrides.viaNativeApp ?? false,
});
return { settled, startGatewayUpdate };
}
beforeEach(() => {
restoreDialogPolyfill = installDialogPolyfill();
originalWebkit = Object.getOwnPropertyDescriptor(window, "webkit");
});
afterEach(() => {
document.body.replaceChildren();
restoreDialogPolyfill();
if (originalWebkit) {
Object.defineProperty(window, "webkit", originalWebkit);
} else {
Reflect.deleteProperty(window, "webkit");
}
});
it("hands a confirmed update to the Mac app instead of the Gateway", async () => {
const postMessage = installNativeBridge();
const { settled, startGatewayUpdate } = startUpdate({ viaNativeApp: true });
const { dialog } = await getRenderedModalDialog(document.body);
expect(dialog.getAttribute("aria-label")).toBe("Update Mac app + Gateway");
findButton("Update Mac app and restart").click();
await settled;
expect(postMessage).toHaveBeenCalledExactlyOnceWith({ type: "start-update" });
expect(startGatewayUpdate).not.toHaveBeenCalled();
});
it("falls back to the Gateway when the Mac bridge disappears during confirmation", async () => {
installNativeBridge();
const { settled, startGatewayUpdate } = startUpdate({ viaNativeApp: true });
await getRenderedModalDialog(document.body);
Reflect.deleteProperty(window, "webkit");
findButton("Update Mac app and restart").click();
await settled;
expect(startGatewayUpdate).toHaveBeenCalledOnce();
});
it("shows the git target when no package version is available", async () => {
const { settled } = startUpdate({
updateAvailable: null,
updateSchedule: {
target: { commitsBehind: 3, kind: "git" },
} as unknown as UpdateScheduleState,
});
const { modal } = await getRenderedModalDialog(document.body);
expect(modal.textContent).toContain("3 commits behind");
findButton("Cancel").click();
await settled;
});
it("states a git distance once instead of labelling it as an available version", async () => {
const { settled } = startUpdate({
updateAvailable: { channel: "dev", currentVersion: "2026.8.1", latestVersion: "2026.8.1" },
updateSchedule: {
target: { commitsBehind: 246, kind: "git" },
} as unknown as UpdateScheduleState,
});
const { modal } = await getRenderedModalDialog(document.body);
expect(modal.textContent).toContain("Installed v2026.8.1 · 246 commits behind");
expect(modal.textContent).not.toContain("Available 246");
findButton("Cancel").click();
await settled;
});
it("keeps a repeated request from stacking a second confirmation or update", async () => {
const first = startUpdate();
const second = startUpdate();
await getRenderedModalDialog(document.body);
await second.settled;
expect(document.body.querySelectorAll("openclaw-modal-dialog")).toHaveLength(1);
expect(second.startGatewayUpdate).not.toHaveBeenCalled();
findButton("Update and restart").click();
await first.settled;
expect(first.startGatewayUpdate).toHaveBeenCalledOnce();
});
it("keeps the dialog open and narrates the install, the restart, and the failure", async () => {
const stream = createProgressStream();
const { settled, startGatewayUpdate } = startUpdate({
watchUpdateProgress: stream.watchUpdateProgress,
});
const { modal } = await getRenderedModalDialog(document.body);
findButton("Update and restart").click();
await Promise.resolve();
expect(startGatewayUpdate).toHaveBeenCalledOnce();
const updating = findButton("Updating…");
expect(updating.disabled).toBe(true);
expect(modal.textContent).toContain("Installing the update on the Gateway");
// The Gateway goes away mid-install; the dialog is mounted outside the shell
// precisely so it can keep reporting through the disconnect.
await stream.push({ busy: true, connected: false, failure: null });
expect(modal.textContent).toContain("The Gateway is restarting");
expect(document.body.querySelector("openclaw-modal-dialog")).not.toBeNull();
await stream.push({
busy: false,
connected: true,
failure: "The update failed at install: ENOSPC: no space left on device, write.",
});
expect(modal.textContent).toContain("ENOSPC: no space left on device");
findButton("Close").click();
await settled;
expect(stream.stopped).toBe(true);
});
it("closes itself once a watched update finishes without a failure", async () => {
const stream = createProgressStream();
const { settled } = startUpdate({ watchUpdateProgress: stream.watchUpdateProgress });
await getRenderedModalDialog(document.body);
findButton("Update and restart").click();
await Promise.resolve();
await stream.push({ busy: true, connected: true, failure: null });
await stream.push({ busy: false, connected: true, failure: null });
await settled;
expect(document.body.querySelector("openclaw-modal-dialog")).toBeNull();
});
/**
* Retry after a failure: the shell keeps the previous attempt's banner until an
* accepted run clears it, and producers replay the current snapshot as their
* subscribe-time emit. `accepted: false` models `overlays.runUpdate` refusing
* the request (disconnected, already running, no admin), which leaves the
* banner in place.
*/
function createRetryStream(options: { accepted: boolean }) {
let progress: UpdateProgress = {
busy: false,
connected: true,
failure: "The update failed at install: ENOSPC: no space left on device, write.",
};
let emit: ((next: UpdateProgress) => void) | null = null;
return {
startGatewayUpdate: () => {
if (!options.accepted) {
return;
}
progress = { busy: true, connected: true, failure: null };
emit?.(progress);
},
watchUpdateProgress: (listener: (next: UpdateProgress) => void) => {
emit = listener;
listener(progress);
return () => {};
},
};
}
it("reports a refused retry as unanswered rather than as the old failure", async () => {
vi.useFakeTimers({ shouldAdvanceTime: true });
try {
const stream = createRetryStream({ accepted: false });
const { settled } = startUpdate({
startGatewayUpdate: stream.startGatewayUpdate,
watchUpdateProgress: stream.watchUpdateProgress,
});
const { modal } = await getRenderedModalDialog(document.body);
findButton("Update and restart").click();
await Promise.resolve();
// The refused request must not inherit the previous error as its outcome.
expect(modal.textContent).not.toContain("ENOSPC");
await vi.advanceTimersByTimeAsync(5_000);
expect(modal.textContent).toContain("The update request went unanswered");
findButton("Close").click();
await settled;
} finally {
vi.useRealTimers();
}
});
it("reports a request the Gateway never accepted instead of spinning forever", async () => {
// Auto-advancing keeps the modal's own animation frames running while the
// grace deadline is fast-forwarded.
vi.useFakeTimers({ shouldAdvanceTime: true });
try {
const stream = createProgressStream();
const { settled } = startUpdate({ watchUpdateProgress: stream.watchUpdateProgress });
const { modal } = await getRenderedModalDialog(document.body);
findButton("Update and restart").click();
await vi.advanceTimersByTimeAsync(5_000);
expect(modal.textContent).toContain("The update request went unanswered");
findButton("Close").click();
await settled;
} finally {
vi.useRealTimers();
}
});