✅ 统一 example/tests 内部结构,去除 innerHTML 直写#1632
Open
cyfung1031 wants to merge 1 commit into
Open
Conversation
按 PR scriptscat#1631 中 cyfung1031 的评审意见(每个 userscript 测试必须自包含、 不引用外部/共享测试库,一眼看完全部逻辑)重新组织 15 个 example/tests 测试脚本:将与具体测试对象无关的基建代码(计数器、test/assert 断言函数、 控制台格式化、结果面板 DOM 生成等)收拢到内层 IIFE 并以对象形式返回, 外层 IIFE 解构接收后只保留原有测试内容,写法与顺序不变;同类概念在各 文件间统一命名(test/assert/assertTrue/printSummary/showResultPanel 等)。 同时把所有 .innerHTML 直接赋值 / insertAdjacentHTML 替换为 createElement/textContent/appendChild 构造,避免 CSP 不兼容。 不引入任何新的共享文件或本地 @require,每个脚本保持独立可运行。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Member
你的观点都站不住脚,这个重构完全不影响阅读,反而是提升;也没有发明新的断言系统,都是和jest、vitest这些通用的统一函数签名,没有自制 比如 UI 面版,甚至可以封装好,将测试的代码展示到页面上,这样反而更加的清晰 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
背景
延续 #1631 的评审结论:@cyfung1031 在该 PR 中明确反对抽出共享测试框架
(
sctest.js)并让每个用户脚本@require它——核心理由是每个example/tests/下的用户脚本测试应当独立、自包含、一眼看完全部逻辑,不应该为了"统一"而强行套用外部框架,也不应该发明一套自制 UI/断言系统。
本 PR 是与 #1631 完全相反的方向:不引入任何共享文件或
@require,只在每个文件内部做结构整理。
本次改动
对
example/tests/下全部 15 个测试脚本做统一的内部结构整理(每个文件独立处理,互不引用):
将与具体测试对象无关的"工具代码"——计数器、
test/assert断言函数、控制台格式化、结果面板 DOM 生成、交互等待辅助函数等——收拢到内层 IIFE,
以对象形式
return;外层 IIFE 通过解构参数接收这些函数,函数体内只保留原有的测试内容(GM API 调用、断言、执行顺序),写法和 UI/UX 基本不变:
同类概念在各文件间统一命名:
test/testAsync/assert/assertTrue/printSummary/showResultPanel(仅在该文件本身确实存在对应概念时才使用,不强行引入用不到的名字;例如
gm_xhr_test.js/gm_download_test.js/gm_xhr_redirect_test.js本身用的是pass/fail/skip风格,予以保留)。将所有
.innerHTML =/insertAdjacentHTML(...)替换为document.createElement/textContent/appendChild/style.cssText构造,视觉效果不变,避免 CSP 不兼容问题(涉及
early_inject_content_test.js、early_inject_page_test.js、gm_download_test.js、gm_value_test.js、gm_xhr_test.js、gm_xhr_redirect_test.js)。实现考虑
sandbox_test.js在 IIFE 外还有必须保持非严格模式的顶层代码(
var testVar1001、mpt等),而带解构参数的函数不能带"use strict"指令序言,因此该文件外层改用单一helpers参数、内部再
const { ... } = helpers解构,其余文件均去掉了外层的"use strict"(严格模式语义保留在内层基础设施 IIFE 中)。gm_value_test.js(✅ 抽离 example/tests 共用测试框架 (sctest) 并全量迁移 14 个测试脚本 #1631 中标注为刻意不套用统一断言框架的交互式多 iframe dashboard 演示)未强行引入
test/assert,只对其原有的DOM 构造类工具函数做了同样的"工具代码后置"处理。
assert(expected, actual, message)(如
gm_xhr_redirect_test.js原assertEq(actual, expected, msg)),已确认所有调用点同步交换顺序,报错文案语义不变。
已知限制
@require;每个脚本依旧可独立复制粘贴运行。整理与断言/结果面板 CSP 加固。
建议审查重点
assertEq→assert、testAsync→test、assertSame→assert、myResolve→resolveNext等)的调用点是否全部同步,尤其是
gm_xhr_test.js(约 277 处)和gm_download_test.js。gm_download_test.js/gm_xhr_test.js/gm_value_test.js中替换innerHTML 的手写标签渲染函数(
renderMarkup/buildLogLine/appendInline等)是否真的走 DOM API 而非字符串拼接后单点赋值innerHTML。sandbox_test.js的helpers单参数写法是否符合预期(因"use strict"限制而与其余文件的解构参数写法略有不同)。验证
node --check对example/tests/下全部 15 个文件均通过(仅语法检查,不执行,因为文件依赖浏览器/GM 全局对象)。
grep -nE '\.innerHTML\s*=|\.insertAdjacentHTML\('对example/tests/*.js无匹配。grep -n '@require'确认仅保留改动前已有的外部 CDN jQuery 引用,未新增任何本地/共享脚本引用。
调用点计数一致、断言参数顺序变化处语义不变、
setTimeout延时数值未变、UserScript 元数据块逐字节不变、结构确为约定的双层 IIFE 形状;未发现任何
行为回归或遗漏调用点。
git commit前置 hook(tsc --noEmit、Prettier、lint)均通过(example/目录本身被排除在 lint/format 范围外,仅命中预期的 ignore 警告)。
example/tests/目前尚未有针对这些脚本的常驻自动化 e2e,故本次验证以静态检查 + 独立差异复核为主,未做浏览器内实跑(这些脚本本身就是手动/
半手动的功能演示与验证脚本)。