import { describe, it, expect, beforeEach } from "vitest"; import { collectFormFields, applyMapping } from "../src/autofill.js"; // 针对表单抓取与回填的纯逻辑测试。 describe("autofill", () => { beforeEach(() => { document.body.innerHTML = `
`; }); // 1-1 抓取字段应排除 hidden 与 button,并生成选择器 it("1-1 collectFormFields 抓取可填字段", () => { const fields = collectFormFields(document); expect(fields.length).toBe(3); expect(fields[0]).toMatchObject({ selector: "#title", name: "title", label: "抬头" }); expect(fields[1].placeholder).toBe("请输入税号"); expect(fields.some((f) => f.name === "token")).toBe(false); }); // 1-2 按映射回填并统计命中 it("1-2 applyMapping 回填成功项", () => { const res = applyMapping(document, [ { selector: "#title", value: "某某公司" }, { selector: 'input[name="taxno"]', value: "91310000MA1" }, ]); expect(res.filled).toBe(2); expect(document.querySelector("#title").value).toBe("某某公司"); }); // 1-3 未命中的选择器应记录到 missed it("1-3 applyMapping 记录未命中", () => { const res = applyMapping(document, [{ selector: "#not-exist", value: "x" }]); expect(res.filled).toBe(0); expect(res.missed).toEqual(["#not-exist"]); }); });