test: harden plugin and UI isolation checks

This commit is contained in:
Peter Steinberger
2026-05-04 11:45:10 +01:00
parent 24ec2aebe8
commit a9f1882047
6 changed files with 67 additions and 56 deletions

View File

@@ -550,10 +550,14 @@ export function setTabFromRoute(host: SettingsHost, next: Tab) {
}
function updateBrowserHistory(url: URL, replace: boolean) {
if (replace) {
return window.history.replaceState({}, "", url.toString());
const history = typeof window === "undefined" ? undefined : window.history;
if (!history) {
return;
}
return window.history.pushState({}, "", url.toString());
if (replace) {
return history.replaceState({}, "", url.toString());
}
return history.pushState({}, "", url.toString());
}
function applyTabSelection(
@@ -592,12 +596,14 @@ function applyTabSelection(
}
export function syncUrlWithTab(host: SettingsHost, tab: Tab, replace: boolean) {
if (typeof window === "undefined") {
const href = typeof window === "undefined" ? undefined : window.location?.href;
const pathname = typeof window === "undefined" ? undefined : window.location?.pathname;
if (!href || !pathname) {
return;
}
const targetPath = normalizePath(pathForTab(tab, host.basePath));
const currentPath = normalizePath(window.location.pathname);
const url = new URL(window.location.href);
const currentPath = normalizePath(pathname);
const url = new URL(href);
if (tab === "chat" && host.sessionKey) {
url.searchParams.set("session", host.sessionKey);

View File

@@ -2,33 +2,14 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const { realtimeTalkCtor, startMock, stopMock } = vi.hoisted(() => ({
realtimeTalkCtor: vi.fn(),
startMock: vi.fn(),
stopMock: vi.fn(),
}));
vi.mock("./chat/realtime-talk.ts", () => ({
RealtimeTalkSession: realtimeTalkCtor,
}));
describe("OpenClawApp Talk controls", () => {
beforeEach(() => {
realtimeTalkCtor.mockReset();
startMock.mockReset();
stopMock.mockReset();
realtimeTalkCtor.mockImplementation(
function MockRealtimeTalkSession(this: { start: typeof startMock; stop: typeof stopMock }) {
this.start = startMock;
this.stop = stopMock;
},
);
startMock.mockResolvedValue(undefined);
vi.restoreAllMocks();
});
it("retries Talk immediately when the previous session is already in error state", async () => {
const { OpenClawApp } = await import("./app.ts");
const app = new OpenClawApp() as unknown as {
await import("./app.ts");
const app = document.createElement("openclaw-app") as unknown as {
client: unknown;
connected: boolean;
realtimeTalkActive: boolean;
@@ -38,7 +19,8 @@ describe("OpenClawApp Talk controls", () => {
toggleRealtimeTalk(): Promise<void>;
};
const staleStop = vi.fn();
app.client = { request: vi.fn() } as never;
const request = vi.fn().mockRejectedValue(new Error("session unavailable"));
app.client = { request } as never;
app.connected = true;
app.sessionKey = "main";
app.realtimeTalkActive = true;
@@ -48,10 +30,9 @@ describe("OpenClawApp Talk controls", () => {
await app.toggleRealtimeTalk();
expect(staleStop).toHaveBeenCalledOnce();
expect(realtimeTalkCtor).toHaveBeenCalledOnce();
expect(startMock).toHaveBeenCalledOnce();
expect(stopMock).not.toHaveBeenCalled();
expect(app.realtimeTalkStatus).toBe("connecting");
expect(app.realtimeTalkSession).not.toBeNull();
expect(request).toHaveBeenCalledOnce();
expect(request).toHaveBeenCalledWith("talk.realtime.session", { sessionKey: "main" });
expect(app.realtimeTalkStatus).toBe("error");
expect(app.realtimeTalkSession).toBeNull();
});
});