first commit

This commit is contained in:
Cheney
2026-07-08 18:01:27 +08:00
commit 22ae3ac755
28 changed files with 9247 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
import { describe, it, expect, beforeEach } from "vitest";
import { collectFormFields, applyMapping } from "../src/autofill.js";
// 针对表单抓取与回填的纯逻辑测试。
describe("autofill", () => {
beforeEach(() => {
document.body.innerHTML = `
<form>
<label for="title">抬头</label><input id="title" name="title" type="text" />
<label>税号 <input name="taxno" placeholder="请输入税号" /></label>
<input type="hidden" name="token" />
<select id="type"><option>普票</option></select>
<button type="submit">提交</button>
</form>`;
});
// 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"]);
});
});