mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 09:40:36 +00:00
refactor(test): share pw-tools-core test setup
This commit is contained in:
@@ -1,50 +1,13 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
installPwToolsCoreTestHooks,
|
||||
setPwToolsCoreCurrentPage,
|
||||
} from "./pw-tools-core.test-harness.js";
|
||||
|
||||
let currentPage: Record<string, unknown> | null = null;
|
||||
let currentRefLocator: Record<string, unknown> | null = null;
|
||||
let pageState: {
|
||||
console: unknown[];
|
||||
armIdUpload: number;
|
||||
armIdDialog: number;
|
||||
armIdDownload: number;
|
||||
};
|
||||
|
||||
const sessionMocks = vi.hoisted(() => ({
|
||||
getPageForTargetId: vi.fn(async () => {
|
||||
if (!currentPage) {
|
||||
throw new Error("missing page");
|
||||
}
|
||||
return currentPage;
|
||||
}),
|
||||
ensurePageState: vi.fn(() => pageState),
|
||||
restoreRoleRefsForTarget: vi.fn(() => {}),
|
||||
refLocator: vi.fn(() => {
|
||||
if (!currentRefLocator) {
|
||||
throw new Error("missing locator");
|
||||
}
|
||||
return currentRefLocator;
|
||||
}),
|
||||
rememberRoleRefsForTarget: vi.fn(() => {}),
|
||||
}));
|
||||
|
||||
vi.mock("./pw-session.js", () => sessionMocks);
|
||||
installPwToolsCoreTestHooks();
|
||||
const mod = await import("./pw-tools-core.js");
|
||||
|
||||
describe("pw-tools-core", () => {
|
||||
beforeEach(() => {
|
||||
currentPage = null;
|
||||
currentRefLocator = null;
|
||||
pageState = {
|
||||
console: [],
|
||||
armIdUpload: 0,
|
||||
armIdDialog: 0,
|
||||
armIdDownload: 0,
|
||||
};
|
||||
for (const fn of Object.values(sessionMocks)) {
|
||||
fn.mockClear();
|
||||
}
|
||||
});
|
||||
|
||||
it("last file-chooser arm wins", async () => {
|
||||
let resolve1: ((value: unknown) => void) | null = null;
|
||||
let resolve2: ((value: unknown) => void) | null = null;
|
||||
@@ -67,10 +30,10 @@ describe("pw-tools-core", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
currentPage = {
|
||||
setPwToolsCoreCurrentPage({
|
||||
waitForEvent,
|
||||
keyboard: { press: vi.fn(async () => {}) },
|
||||
};
|
||||
});
|
||||
|
||||
await mod.armFileUploadViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
@@ -93,9 +56,9 @@ describe("pw-tools-core", () => {
|
||||
const dismiss = vi.fn(async () => {});
|
||||
const dialog = { accept, dismiss };
|
||||
const waitForEvent = vi.fn(async () => dialog);
|
||||
currentPage = {
|
||||
setPwToolsCoreCurrentPage({
|
||||
waitForEvent,
|
||||
};
|
||||
});
|
||||
|
||||
await mod.armDialogViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
@@ -129,7 +92,7 @@ describe("pw-tools-core", () => {
|
||||
const waitForFunction = vi.fn(async () => {});
|
||||
const waitForTimeout = vi.fn(async () => {});
|
||||
|
||||
currentPage = {
|
||||
const page = {
|
||||
locator: vi.fn(() => ({
|
||||
first: () => ({ waitFor: waitForSelector }),
|
||||
})),
|
||||
@@ -139,6 +102,7 @@ describe("pw-tools-core", () => {
|
||||
waitForTimeout,
|
||||
getByText: vi.fn(() => ({ first: () => ({ waitFor: vi.fn() }) })),
|
||||
};
|
||||
setPwToolsCoreCurrentPage(page);
|
||||
|
||||
await mod.waitForViaPlaywright({
|
||||
cdpUrl: "http://127.0.0.1:18792",
|
||||
@@ -151,7 +115,7 @@ describe("pw-tools-core", () => {
|
||||
});
|
||||
|
||||
expect(waitForTimeout).toHaveBeenCalledWith(50);
|
||||
expect(currentPage.locator as ReturnType<typeof vi.fn>).toHaveBeenCalledWith("#main");
|
||||
expect(page.locator as ReturnType<typeof vi.fn>).toHaveBeenCalledWith("#main");
|
||||
expect(waitForSelector).toHaveBeenCalledWith({
|
||||
state: "visible",
|
||||
timeout: 1234,
|
||||
|
||||
Reference in New Issue
Block a user