You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { describe, expect, it } from "vitest";
import { RpaError } from "@/modules/rpa/errors";
import { RPAContext, resolveRPAContext } from "@/workers/rpa/kernel/context";
import {
assertRPAContext,
ensureArray,
isRPAContext,
snapshotContextContract,
} from "@/workers/rpa/kernel/context-validator";
function mockPage(): object {
return {
locator: () => ({
filter: () => ({ nth: () => ({}) }),
first: () => ({}),
}),
waitForTimeout: async () => undefined,
url: () => "https://example.com",
};
}
describe("context-validator", () => {
it("ensureArrayundefined → []", () => {
expect(ensureArray(undefined)).toEqual([]);
expect(ensureArray(null)).toEqual([]);
expect(ensureArray([1])).toEqual([1]);
});
it("resolveRPAContext已是 context 时不再包一层", () => {
const page = mockPage();
const ctx = new RPAContext(page as never);
const resolved = resolveRPAContext(ctx);
expect(resolved).toBe(ctx);
expect(isRPAContext(resolved)).toBe(true);
});
it("fromSession 误传 context 不再导致 page.locator 断裂", () => {
const page = mockPage();
const ctx = new RPAContext(page as never);
const wrapped = RPAContext.fromSession(ctx);
expect(wrapped).toBe(ctx);
expect(snapshotContextContract(wrapped).hasPage).toBe(true);
});
it("assertRPAContext缺 state 时 fail-fast", () => {
const page = mockPage();
const broken = {
page,
selectors: {},
exec: {},
runtime: {},
getWidgetScrollAt: () => 0,
setWidgetScrollAt: () => undefined,
};
expect(() => assertRPAContext(broken)).toThrow(RpaError);
});
});