import { beforeEach, describe, expect, it } from "vitest"; import { describeEventTarget, describeTarget, isMeaningfulClickTarget } from "../describe-target"; describe("describeTarget", () => { beforeEach(() => { document.body.innerHTML = ""; }); it("prefers aria-label and button role", () => { document.body.innerHTML = ``; const el = document.querySelector("button")!; expect(describeTarget(el)).toEqual({ tag: "button", role: "button", name: "发布", }); }); it("uses label text and name_attr for inputs", () => { document.body.innerHTML = ` `; const el = document.querySelector("input")!; expect(describeTarget(el)).toEqual({ tag: "input", role: "textbox", name: "服务名称", name_attr: "serviceName", }); }); it("never invents CSS selectors or @eN", () => { document.body.innerHTML = `详情`; const desc = describeTarget(document.querySelector("a")!); expect(JSON.stringify(desc)).not.toMatch(/@e\d+|nth-of-type|#/); expect(desc).toEqual({ tag: "a", role: "link", name: "详情" }); }); it("prefers heading text inside SERP-style links", () => { document.body.innerHTML = `

Tencent 腾讯

https://www.tencent.com
`; expect(describeTarget(document.querySelector("a")!).name).toBe("Tencent 腾讯"); }); }); describe("LLM textbook click targets", () => { beforeEach(() => { document.body.innerHTML = ""; }); it("rejects tag-only targets the LLM cannot find", () => { expect(isMeaningfulClickTarget({ tag: "div" })).toBe(false); }); it("treats a short visible name as the teaching signal", () => { expect(isMeaningfulClickTarget({ tag: "div", name: "发布" })).toBe(true); }); it("ignores anonymous layout chrome and lifts child clicks to the named button", () => { document.body.innerHTML = `
background
`; expect(describeEventTarget(document.querySelector("#noise"))).toBeNull(); expect(describeEventTarget(document.querySelector("#icon"))).toEqual({ tag: "button", role: "button", name: "发布", }); }); it("does not attach layout sibling text as nearby_label on buttons", () => { document.body.innerHTML = `
background
`; expect(describeTarget(document.querySelector("#pub")!)).toEqual({ tag: "button", role: "button", name: "发布", }); }); it("keeps nearby_label for form fields from a preceding label-like sibling", () => { document.body.innerHTML = `
服务名称
`; expect(describeTarget(document.querySelector("#svc")!)).toEqual( expect.objectContaining({ tag: "input", nearby_label: "服务名称", }), ); }); it("does not treat plain text blocks as clicks", () => { document.body.innerHTML = `

说明文字

`; expect(describeEventTarget(document.querySelector("#copy"))).toBeNull(); }); it("records custom button-like controls by their visible label", () => { document.body.innerHTML = `
发布
`; expect(describeEventTarget(document.querySelector("#pub"))).toEqual( expect.objectContaining({ tag: "div", name: "发布" }), ); }); });