// tests/content-fill.test.js // 用 jsdom(真实 DOM)测试 content.js 的登录表单探测与填充引擎: // 字段猜测、隐藏元素跳过、Shadow DOM 穿透、HTML 片段定位、 // 自动提交/验证码拦截、失败次数限制、多环境浮窗、SPA 重试、手动填充消息。 // 运行:node --test tests/content-fill.test.js // // jsdom 不实现布局(offsetWidth/offsetParent 恒为 0/null),因此这里统一把元素视为"可见", // 需要模拟隐藏时用 hide() 单独覆盖 —— 这样能真实覆盖 content.js 里的可见性判断分支。 const { describe, it } = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const { JSDOM } = require("jsdom"); const UTILS_CODE = fs.readFileSync(path.join(__dirname, "..", "dist", "utils.js"), "utf8"); const CONTENT_CODE = fs.readFileSync(path.join(__dirname, "..", "dist", "content.js"), "utf8"); const tick = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); // autoSubmit 的最小延迟是 300ms(content.js 里 Math.max(300, delayMs)) const AFTER_SUBMIT = 400; function createChromeMock({ configs = [], attempts = {}, activeEnvId = null } = {}) { const state = { configs, activeEnvId, attempts: new Map(Object.entries(attempts)), calls: [], messageListener: null, // 每个帧注入 content.js 都会注册一个监听器;allFrames 场景下用最后一个(最内层帧) messageListeners: [], }; const api = { runtime: { id: "mock-extension-id", onMessage: { addListener: (fn) => { state.messageListener = fn; state.messageListeners.push(fn); }, }, sendMessage: (message) => { state.calls.push(message.action); switch (message.action) { case "loadAllConfigs": return Promise.resolve({ configs: state.configs, activeEnvId: state.activeEnvId }); case "getVaultStatus": return Promise.resolve({ hasVault: false, unlocked: true, pendingImport: false }); case "getDomainAttempts": { const key = `${message.envId}::${message.domain}`; return Promise.resolve({ attempts: state.attempts.get(key) || { count: 0, lastAttempt: 0 } }); } case "incrementDomainAttempts": { const key = `${message.envId}::${message.domain}`; const cur = state.attempts.get(key) || { count: 0, lastAttempt: 0 }; cur.count += 1; cur.lastAttempt = Date.now(); state.attempts.set(key, cur); return Promise.resolve({ attempts: cur }); } case "resetDomainAttempts": return Promise.resolve({ success: true }); default: return Promise.resolve({}); } }, }, i18n: { getMessage: (key) => key }, storage: { local: { get: async () => ({}), set: async () => {}, remove: async () => {} }, session: { get: async () => ({}), set: async () => {}, remove: async () => {} }, }, }; return { api, state }; } /** * jsdom 没有布局引擎(offsetParent 恒为 null、offsetWidth 恒为 0), * 而 content.js 会用它们判断元素是否可见。这里按 realm 逐个打补丁: * 主窗口与每个 iframe 都各有自己的 HTMLElement.prototype,必须分别处理。 */ function patchLayout(win) { Object.defineProperty(win.HTMLElement.prototype, "offsetWidth", { get() { return 100; }, configurable: true, }); Object.defineProperty(win.HTMLElement.prototype, "offsetHeight", { get() { return 20; }, configurable: true, }); Object.defineProperty(win.HTMLElement.prototype, "offsetParent", { get() { return this.parentElement || this.ownerDocument.body; }, configurable: true, }); // jsdom 未实现 CSS.escape(真实浏览器有) if (!win.CSS) win.CSS = {}; if (!win.CSS.escape) win.CSS.escape = (s) => String(s).replace(/[^a-zA-Z0-9_\u00a0-\uffff-]/g, (c) => "\\" + c); } /** * 在 jsdom 页面里执行 utils.js + content.js(与浏览器一致:按普通脚本顺序执行)。 * setup 回调在注入脚本之前执行,用于准备"脚本加载前就必须存在"的结构(例如 Shadow Root)。 */ function loadContentPage({ html = "", url = "https://example.com/login", configs = [], attempts = {}, activeEnvId = null, timing, setup, } = {}) { // pretendToBeVisual: 让 jsdom 提供 requestAnimationFrame(content.js 的填充完成 toast 依赖它), // 否则 showFillToast() 会抛 ReferenceError。 const dom = new JSDOM(`
${html}`, { url, runScripts: "dangerously", pretendToBeVisual: true, }); const win = dom.window; patchLayout(win); // 缩短时间常量(30 秒超时 / 4 秒结果观察),否则这些用例要真等半分钟 if (timing) win.__autoLoginTiming = timing; const chrome = createChromeMock({ configs, attempts, activeEnvId }); win.chrome = chrome.api; if (setup) setup(win, win.document); const inject = (code) => { const el = win.document.createElement("script"); el.textContent = code; win.document.head.appendChild(el); }; inject(UTILS_CODE); inject(CONTENT_CODE); return { dom, win, doc: win.document, chrome }; } /** 模拟元素不可见(隐藏域 / display:none) */ function hide(el) { Object.defineProperty(el, "offsetWidth", { get: () => 0, configurable: true }); Object.defineProperty(el, "offsetParent", { get: () => null, configurable: true }); return el; } function makeConfig(overrides = {}) { return { id: "cfg1", envId: "env1", envName: "测试", domain: "example.com", alias: null, username: "auto-user", password: "auto-pass", autoSubmit: false, usernameSelector: null, passwordSelector: null, submitSelector: null, enabled: true, delayMs: null, ...overrides, }; } describe("content - 字段探测", () => { it("标准登录表单:用户名 / 密码 / 提交按钮都能找到", () => { const ctx = loadContentPage({ html: ``, }); assert.equal(ctx.win.findPasswordField({}).getAttribute("name"), "pass"); assert.equal(ctx.win.findUsernameField({}).getAttribute("name"), "username"); assert.equal( ctx.win.findSubmitButton({}, ctx.doc.querySelector("input[type=password]")).textContent, "登录", ); }); it("显式选择器优先于自动探测", () => { const ctx = loadContentPage({ html: ``, }); const config = { passwordSelector: "#right", usernameSelector: "#u2" }; assert.equal(ctx.win.findPasswordField(config).id, "right"); assert.equal(ctx.win.findUsernameField(config).id, "u2"); }); it("用户名按 name / placeholder 关键词猜测", () => { const ctx = loadContentPage({ html: ``, }); assert.equal(ctx.win.findUsernameField({}).id, "account"); }); it("隐藏的密码框被跳过,选可见的那个", () => { const ctx = loadContentPage({ html: ``, }); hide(ctx.doc.getElementById("ghost")); assert.equal(ctx.win.findPasswordField({}).id, "real"); }); it("页面上没有密码框时返回 null,fillForm 返回 false", () => { const ctx = loadContentPage({ html: `` }); assert.equal(ctx.win.findPasswordField({}), null); assert.equal(ctx.win.fillForm({ username: "u", password: "p", autoSubmit: false }, false), false); }); it("提交按钮按文案识别(非 type=submit)", () => { const ctx = loadContentPage({ html: ``, }); assert.equal(ctx.win.findSubmitButton({}, ctx.doc.querySelector("input[type=password]")).id, "btn"); }); it("能穿透 Shadow DOM 找到登录框", () => { const ctx = loadContentPage({ html: `` }); const shadow = ctx.doc.getElementById("host").attachShadow({ mode: "open" }); shadow.innerHTML = ``; assert.equal(ctx.win.findPasswordField({}).getAttribute("name"), "pass"); assert.equal(ctx.win.findUsernameField({}).getAttribute("name"), "user"); }); it("resolveElement 支持选择器与整段 HTML 片段两种写法", () => { const ctx = loadContentPage({ html: ``, }); assert.equal(ctx.win.resolveElement("input[name=pass]").id, "pwd"); // 从 HTML 片段反推选择器(用户直接粘贴元素代码的场景) assert.equal(ctx.win.resolveElement('').id, "pwd"); }); }); describe("content - 填充与提交", () => { it("填充用户名和密码,并对两个字段派发 input/change 事件", () => { const ctx = loadContentPage({ html: ``, }); const events = []; for (const id of ["u", "p"]) { const el = ctx.doc.getElementById(id); el.addEventListener("input", () => events.push(`${id}:input`)); el.addEventListener("change", () => events.push(`${id}:change`)); } assert.equal(ctx.win.fillForm({ username: "alice", password: "secret", autoSubmit: false }, false), true); assert.equal(ctx.doc.getElementById("u").value, "alice"); assert.equal(ctx.doc.getElementById("p").value, "secret"); assert.deepEqual(events, ["u:input", "u:change", "p:input", "p:change"]); }); it("autoSubmit 为真时延迟点击提交按钮", async () => { const ctx = loadContentPage({ html: ``, }); const btn = ctx.doc.getElementById("btn"); let clicks = 0; btn.addEventListener("click", () => clicks++); ctx.win.fillForm({ username: "u", password: "p", autoSubmit: true }, true); assert.equal(clicks, 0, "不应立即点击"); await tick(AFTER_SUBMIT); assert.equal(clicks, 1); }); it("检测到验证码时不自动提交,并弹出提示", async () => { const ctx = loadContentPage({ html: ``, }); const btn = ctx.doc.getElementById("btn"); let clicks = 0; btn.addEventListener("click", () => clicks++); ctx.win.fillForm({ username: "u", password: "p", autoSubmit: true }, true); await tick(AFTER_SUBMIT); assert.equal(clicks, 0, "有验证码时不能自动提交"); assert.ok(ctx.doc.body.innerHTML.includes("captchaToastTitle"), "应显示验证码提示浮窗"); }); it("没有提交按钮但在 form 内时回退到 requestSubmit", async () => { const ctx = loadContentPage({ html: ``, }); let submitted = 0; const form = ctx.doc.querySelector("form"); Object.defineProperty(form, "requestSubmit", { value: () => { submitted++; }, configurable: true, }); ctx.win.fillForm({ username: "u", password: "p", autoSubmit: true }, true); await tick(AFTER_SUBMIT); assert.equal(submitted, 1); }); }); describe("content - 自动填充主流程", () => { const LOGIN_HTML = ``; it("域名命中配置时自动填充", async () => { const ctx = loadContentPage({ html: LOGIN_HTML, url: "https://example.com/login", configs: [makeConfig()], }); await tick(60); assert.equal(ctx.doc.getElementById("u").value, "auto-user"); assert.equal(ctx.doc.getElementById("p").value, "auto-pass"); }); it("域名不匹配时什么都不做", async () => { const ctx = loadContentPage({ html: LOGIN_HTML, url: "https://other.com/login", configs: [makeConfig()], }); await tick(60); assert.equal(ctx.doc.getElementById("u").value, ""); assert.equal(ctx.doc.getElementById("p").value, ""); }); it("配置被禁用时不填充", async () => { const ctx = loadContentPage({ html: LOGIN_HTML, configs: [makeConfig({ enabled: false })] }); await tick(60); assert.equal(ctx.doc.getElementById("u").value, ""); }); it("未达失败上限时自动提交并累加计数", async () => { const ctx = loadContentPage({ html: LOGIN_HTML, configs: [makeConfig({ autoSubmit: true })] }); let clicks = 0; ctx.doc.getElementById("btn").addEventListener("click", () => clicks++); await tick(AFTER_SUBMIT); assert.equal(ctx.doc.getElementById("u").value, "auto-user"); assert.equal(clicks, 1); assert.ok(ctx.chrome.state.calls.includes("incrementDomainAttempts")); }); it("达到上限后只填充、不自动提交,并给出可见提示", async () => { const ctx = loadContentPage({ html: LOGIN_HTML, configs: [makeConfig({ autoSubmit: true })], attempts: { "env1::example.com": { count: 3, lastAttempt: Date.now() } }, }); let clicks = 0; ctx.doc.getElementById("btn").addEventListener("click", () => clicks++); await tick(AFTER_SUBMIT); assert.equal(ctx.doc.getElementById("u").value, "auto-user", "仍然填充"); assert.equal(clicks, 0, "不应自动提交"); assert.ok(!ctx.chrome.state.calls.includes("incrementDomainAttempts")); // 用户必须能看懂"为什么只填不提交",否则会以为扩展坏了 assert.ok(ctx.doc.body.innerHTML.includes("attemptToastTitle"), "应显示暂停提示浮窗"); }); it("命中配置但页面上没有登录框时不消耗计数(浏览站内页面不会被误扣)", async () => { const ctx = loadContentPage({ html: `