gentle-pi 4.7.0 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (365) hide show
  1. package/README.md +55 -44
  2. package/assets/agents/gentle-ai-explore.md +3 -3
  3. package/assets/agents/gentle-ai-verify.md +20 -6
  4. package/assets/agents/gentle-ai-worker.md +6 -6
  5. package/assets/agents/jd-judge-a.md +1 -3
  6. package/assets/agents/jd-judge-b.md +1 -3
  7. package/assets/agents/review-readability.md +1 -5
  8. package/assets/agents/review-reliability.md +1 -5
  9. package/assets/agents/review-resilience.md +1 -5
  10. package/assets/agents/review-risk.md +1 -5
  11. package/assets/design-canvas/canvas-api.js +45 -0
  12. package/assets/design-canvas/canvas-boards.js +456 -0
  13. package/assets/design-canvas/canvas-boot.js +217 -0
  14. package/assets/design-canvas/canvas-checks.js +491 -0
  15. package/assets/design-canvas/canvas-comments.js +315 -0
  16. package/assets/design-canvas/canvas-compare.js +159 -0
  17. package/assets/design-canvas/canvas-events.js +75 -0
  18. package/assets/design-canvas/canvas-export.js +393 -0
  19. package/assets/design-canvas/canvas-focus.js +203 -0
  20. package/assets/design-canvas/canvas-handoffs.js +150 -0
  21. package/assets/design-canvas/canvas-i18n-es.js +392 -0
  22. package/assets/design-canvas/canvas-i18n.js +108 -0
  23. package/assets/design-canvas/canvas-layout.js +526 -0
  24. package/assets/design-canvas/canvas-menu.js +213 -0
  25. package/assets/design-canvas/canvas-messages.js +86 -0
  26. package/assets/design-canvas/canvas-minimap.js +163 -0
  27. package/assets/design-canvas/canvas-parts.js +401 -0
  28. package/assets/design-canvas/canvas-popover.js +234 -0
  29. package/assets/design-canvas/canvas-rail.js +348 -0
  30. package/assets/design-canvas/canvas-sizes.js +524 -0
  31. package/assets/design-canvas/canvas-store.js +164 -0
  32. package/assets/design-canvas/canvas-tokens.js +674 -0
  33. package/assets/design-canvas/canvas-ui.js +24 -0
  34. package/assets/design-canvas/canvas-view.js +184 -0
  35. package/assets/design-canvas/canvas.css +746 -15
  36. package/assets/design-canvas/canvas.js +2 -1506
  37. package/assets/design-canvas/index.html +117 -36
  38. package/assets/design-canvas/inspector.js +546 -16
  39. package/assets/install-wizard/wizard.js +5 -1
  40. package/assets/migrations/agent-body-history.json +24 -0
  41. package/assets/orchestrator-delegation-reference.md +8 -14
  42. package/assets/orchestrator-delegation.md +4 -4
  43. package/assets/orchestrator-full-delegation-card.md +4 -5
  44. package/assets/orchestrator-full-delegation.md +8 -8
  45. package/assets/orchestrator-memory.md +2 -2
  46. package/assets/orchestrator-prompts.md +1 -1
  47. package/assets/orchestrator-quota-recovery.md +5 -0
  48. package/assets/orchestrator-skills.md +13 -12
  49. package/assets/orchestrator-tracking.md +10 -14
  50. package/assets/orchestrator-verification.md +10 -8
  51. package/assets/orchestrator-writer.md +4 -4
  52. package/assets/orchestrator.md +18 -22
  53. package/bin/gentle-shell.mjs +128 -9
  54. package/contracts/review-provider-contract-mirror/provider-contract.lock.json +8 -9
  55. package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/manifest.json +3 -3
  56. package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/orchestration/pi.md +1 -0
  57. package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/routing/pi.md +6 -6
  58. package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/generated/provider-capabilities.baseline.json +3 -3
  59. package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/generated/provider-roles.baseline.json +1 -1
  60. package/docs/assets/brand/sponsor-banner.svg +60 -0
  61. package/docs/bundled-install.md +14 -6
  62. package/docs/delegated-verification.md +18 -2
  63. package/docs/desktop-rpc.md +1253 -0
  64. package/docs/gentle-agents-activity.md +26 -0
  65. package/docs/gentle-shell.md +39 -16
  66. package/docs/install-wizard.md +30 -21
  67. package/docs/odd-preparation-continuity.md +24 -0
  68. package/docs/permission-modes.md +9 -1
  69. package/docs/prompt-budget.md +12 -0
  70. package/docs/rdd-odd-friction-triage-2026-11-07.md +57 -0
  71. package/docs/readme-reference.md +76 -24
  72. package/docs/review-coordinator.md +29 -9
  73. package/docs/review-delivery-preflight.md +22 -0
  74. package/docs/work-continuity.md +11 -1
  75. package/extensions/ask-user-question.ts +2 -2
  76. package/extensions/child-context.ts +21 -7
  77. package/extensions/gentle-agents.ts +234 -33
  78. package/extensions/gentle-ai.ts +576 -135
  79. package/extensions/gentle-design.ts +69 -3
  80. package/extensions/gentle-desktop-rpc.ts +143 -0
  81. package/extensions/gentle-guide.ts +3 -0
  82. package/extensions/gentle-shell.ts +105 -52
  83. package/extensions/gentle-todo.ts +48 -25
  84. package/extensions/skill-registry.ts +248 -223
  85. package/lib/agent-assets.ts +30 -6
  86. package/lib/agent-tool-overlay.ts +117 -0
  87. package/lib/agents-config.ts +47 -6
  88. package/lib/agents-message-delivery.ts +17 -0
  89. package/lib/agents-messaging.ts +8 -2
  90. package/lib/agents-protocol.ts +15 -0
  91. package/lib/agents-report-cap.ts +115 -0
  92. package/lib/agents-runner.ts +91 -6
  93. package/lib/agents-session-transport.ts +17 -1
  94. package/lib/background-jobs-tools.ts +9 -4
  95. package/lib/bounded-writer-admission.ts +184 -31
  96. package/lib/child-context-files.ts +21 -6
  97. package/lib/codemode-renderer.ts +1 -0
  98. package/lib/delivery-conflict-markers.ts +349 -0
  99. package/lib/design-canvas-assets.ts +36 -0
  100. package/lib/design-canvas-server.ts +241 -23
  101. package/lib/design-comments.ts +94 -4
  102. package/lib/design-handoffs.ts +2 -1
  103. package/lib/design-layout.ts +125 -0
  104. package/lib/design-parts.ts +281 -0
  105. package/lib/design-system-tokens.ts +111 -0
  106. package/lib/desktop-agents-service.ts +294 -0
  107. package/lib/desktop-design-service.ts +233 -0
  108. package/lib/desktop-jobs-service.ts +263 -0
  109. package/lib/desktop-rpc-agents.ts +294 -0
  110. package/lib/desktop-rpc-config.ts +731 -0
  111. package/lib/desktop-rpc-design.ts +138 -0
  112. package/lib/desktop-rpc-jobs.ts +181 -0
  113. package/lib/desktop-rpc-permissions.ts +177 -0
  114. package/lib/desktop-rpc-profiles.ts +196 -0
  115. package/lib/desktop-rpc-usage.ts +229 -0
  116. package/lib/desktop-rpc.ts +225 -0
  117. package/lib/desktop-usage-service.ts +310 -0
  118. package/lib/destructive-command-guard.ts +188 -13
  119. package/lib/gentle-ai-renderer.ts +13 -1
  120. package/lib/gentle-design.ts +201 -14
  121. package/lib/gentle-shell-bundled.ts +100 -9
  122. package/lib/hidden-tools.ts +35 -0
  123. package/lib/inprocess-reviewer.ts +96 -12
  124. package/lib/model-fallback-policy.ts +9 -0
  125. package/lib/model-routing-authority.ts +9 -0
  126. package/lib/nan-provider.ts +42 -28
  127. package/lib/native-review-cli.ts +79 -4
  128. package/lib/orchestrator-discovery.ts +39 -2
  129. package/lib/reply-language-anchor.ts +27 -2
  130. package/lib/request-context-placement.ts +53 -0
  131. package/lib/review-candidate-view.ts +11 -1
  132. package/lib/review-continuation-registry.ts +17 -0
  133. package/lib/review-coordinator.ts +252 -8
  134. package/lib/review-delivery-preflight.ts +85 -0
  135. package/lib/review-host-relay.ts +5 -1
  136. package/lib/review-integration-v2.ts +56 -3
  137. package/lib/reviewer-stream.ts +87 -0
  138. package/lib/shell-prompt.ts +16 -3
  139. package/lib/shell-usage.ts +10 -0
  140. package/lib/vim-editor-adapter.ts +9 -2
  141. package/lib/writer-batch-admission.ts +55 -0
  142. package/lib/yolo-session-policy.ts +6 -1
  143. package/package.json +2 -2
  144. package/prompts/skill-creation.md +1 -10
  145. package/runtime/design-canvas-assets.mjs +37 -0
  146. package/runtime/gentle-shell-bundled.mjs +100 -9
  147. package/runtime/native-review-cli.mjs +79 -4
  148. package/runtime/review-integration-v2.mjs +56 -3
  149. package/scripts/bootstrap.cmd +2 -2
  150. package/scripts/bootstrap.sh +41 -4
  151. package/scripts/build-full-delegation-card.mjs +4 -0
  152. package/scripts/build-runtime-modules.mjs +1 -0
  153. package/scripts/gentle-ai-installer.mjs +10 -10
  154. package/scripts/gentle-ai-proxy.mjs +1 -1
  155. package/scripts/installer-downloads.mjs +34 -8
  156. package/scripts/installer-preflight.mjs +28 -17
  157. package/scripts/installer-probes.mjs +25 -5
  158. package/scripts/installer-server.mjs +38 -9
  159. package/scripts/installer-windows.mjs +2 -2
  160. package/scripts/pi-update.mjs +166 -27
  161. package/scripts/prepare-verifier.mjs +107 -0
  162. package/scripts/update-agent-body-history.mjs +55 -8
  163. package/scripts/verify-package-files.mjs +40 -19
  164. package/skills/branch-pr/SKILL.md +16 -16
  165. package/skills/comment-writer/SKILL.md +1 -1
  166. package/skills/design/SKILL.md +6 -6
  167. package/skills/gentle-ai/SKILL.md +5 -5
  168. package/skills/judgment-day/SKILL.md +2 -2
  169. package/skills/judgment-day/references/prompts-and-formats.md +1 -1
  170. package/skills/rdd-defect-workflow/SKILL.md +6 -6
  171. package/skills/skill-improver/SKILL.md +2 -2
  172. package/skills/skill-registry/SKILL.md +11 -13
  173. package/skills/tui-design/SKILL.md +1 -1
  174. package/skills/work-unit-commits/SKILL.md +5 -5
  175. package/tests/agent-prompt-diet-contract.test.ts +101 -0
  176. package/tests/agent-tool-overlay-acceptance.test.ts +158 -0
  177. package/tests/agent-tool-overlay.test.ts +89 -0
  178. package/tests/agents-config.test.ts +10 -1
  179. package/tests/agents-message-delivery.test.ts +36 -0
  180. package/tests/agents-queries.test.ts +144 -4
  181. package/tests/agents-report-cap.test.ts +126 -0
  182. package/tests/agents-runner.test.ts +37 -1
  183. package/tests/agents-session-transport.test.ts +34 -0
  184. package/tests/always-on-prompt-verdicts-contract.test.ts +188 -0
  185. package/tests/append-system-prompt-route.test.ts +10 -2
  186. package/tests/approved-text-leftovers-contract.test.ts +136 -0
  187. package/tests/artifact-language.test.ts +1 -1
  188. package/tests/ask-user-question.test.ts +2 -1
  189. package/tests/audit-leftovers-contract.test.ts +51 -0
  190. package/tests/bounded-stop-contract.test.ts +9 -1
  191. package/tests/branch-pr-skill.test.ts +10 -1
  192. package/tests/bundled-install.test.ts +1 -1
  193. package/tests/child-context-files.test.ts +64 -8
  194. package/tests/child-skills-catalog.test.ts +56 -0
  195. package/tests/codemode-rendering.test.ts +4 -1
  196. package/tests/delegate-for-reason-contract.test.ts +30 -8
  197. package/tests/delivery-conflict-markers.test.ts +348 -0
  198. package/tests/design-canvas-all-sizes.test.ts +631 -0
  199. package/tests/design-canvas-allowlist.test.ts +32 -0
  200. package/tests/design-canvas-assets.test.ts +11 -878
  201. package/tests/design-canvas-boards.test.ts +77 -0
  202. package/tests/design-canvas-checks.test.ts +655 -0
  203. package/tests/design-canvas-compare.test.ts +79 -0
  204. package/tests/design-canvas-events.test.ts +59 -0
  205. package/tests/design-canvas-export.test.ts +1168 -0
  206. package/tests/design-canvas-focus.test.ts +113 -0
  207. package/tests/design-canvas-handoffs.test.ts +59 -0
  208. package/tests/design-canvas-i18n-review.test.ts +513 -0
  209. package/tests/design-canvas-i18n-views.test.ts +259 -0
  210. package/tests/design-canvas-i18n.test.ts +526 -0
  211. package/tests/design-canvas-inspector.test.ts +937 -0
  212. package/tests/design-canvas-layout.test.ts +1222 -0
  213. package/tests/design-canvas-minimap.test.ts +413 -0
  214. package/tests/design-canvas-modules.test.ts +90 -0
  215. package/tests/design-canvas-parts.test.ts +721 -0
  216. package/tests/design-canvas-rail.test.ts +799 -0
  217. package/tests/design-canvas-server.test.ts +855 -17
  218. package/tests/design-canvas-sizes.test.ts +548 -0
  219. package/tests/design-canvas-store.test.ts +180 -0
  220. package/tests/design-canvas-tokens.test.ts +965 -0
  221. package/tests/design-canvas-view.test.ts +299 -0
  222. package/tests/design-comments.test.ts +201 -1
  223. package/tests/design-handoffs.test.ts +20 -0
  224. package/tests/design-layout.test.ts +100 -0
  225. package/tests/design-parts.test.ts +260 -0
  226. package/tests/design-system-tokens.test.ts +215 -0
  227. package/tests/desktop-rpc-agents.test.ts +893 -0
  228. package/tests/desktop-rpc-config.test.ts +1452 -0
  229. package/tests/desktop-rpc-design.test.ts +662 -0
  230. package/tests/desktop-rpc-jobs.test.ts +796 -0
  231. package/tests/desktop-rpc-permissions.test.ts +311 -0
  232. package/tests/desktop-rpc-profiles.test.ts +140 -0
  233. package/tests/desktop-rpc-safety.test.ts +119 -0
  234. package/tests/desktop-rpc-usage.test.ts +697 -0
  235. package/tests/desktop-rpc.test.ts +252 -0
  236. package/tests/devbinary/pi-host-relay.devtest.ts +149 -80
  237. package/tests/devbinary/review-session-consent.devtest.ts +84 -0
  238. package/tests/engram-safety.test.ts +111 -0
  239. package/tests/fixtures/devbinary/status-v9-escalated-non-blocking.captured.json +92 -0
  240. package/tests/fixtures/devbinary/status-v9-escalated-non-blocking.provenance.md +3 -0
  241. package/tests/fixtures/primary-session-prompt/gentleman.golden.md +20 -29
  242. package/tests/fixtures/refusal-identity.probe.mjs +139 -0
  243. package/tests/full-delegation-closure.test.ts +29 -0
  244. package/tests/gentle-agents.test.ts +329 -4
  245. package/tests/gentle-ai-binary.test.ts +1 -1
  246. package/tests/gentle-ai-child-guards.test.ts +1 -0
  247. package/tests/gentle-ai-installer.test.ts +52 -51
  248. package/tests/gentle-ai-renderer.test.ts +36 -2
  249. package/tests/gentle-ai.test.ts +121 -6
  250. package/tests/gentle-design.test.ts +365 -1
  251. package/tests/gentle-shell-bundled.test.ts +91 -5
  252. package/tests/gentle-shell-launcher.test.ts +1 -1
  253. package/tests/gentle-shell.test.ts +118 -87
  254. package/tests/gentle-todo.test.ts +32 -9
  255. package/tests/hidden-tools.test.ts +188 -0
  256. package/tests/independent-recovery-delivery.test.ts +19 -5
  257. package/tests/inprocess-reviewer.test.ts +73 -0
  258. package/tests/install-wizard.test.ts +27 -1
  259. package/tests/installer-downloads.test.ts +30 -1
  260. package/tests/installer-posix-bootstrap.test.ts +114 -13
  261. package/tests/installer-preflight.test.ts +49 -1
  262. package/tests/installer-probes.test.ts +146 -0
  263. package/tests/installer-server.test.ts +203 -4
  264. package/tests/installer-windows-bootstrap.test.ts +19 -1
  265. package/tests/judgment-day-severity-admission.test.ts +46 -0
  266. package/tests/lazy-module-contradictions-contract.test.ts +158 -0
  267. package/tests/maintainer/desktop-rpc.maintest.ts +632 -0
  268. package/tests/maintainer/pi-update-source.maintest.ts +48 -18
  269. package/tests/model-fallback-policy.test.ts +26 -7
  270. package/tests/nan-provider-runtime.test.ts +17 -3
  271. package/tests/nan-provider.test.ts +111 -56
  272. package/tests/native-review-capability-contract.test.ts +11 -1
  273. package/tests/native-review-cli-nonblocking-findings.test.ts +165 -0
  274. package/tests/native-review-parity.test.ts +24 -5
  275. package/tests/native-review-refusal-identity.test.ts +25 -0
  276. package/tests/no-self-pause-contract.test.ts +11 -3
  277. package/tests/odd-integration.test.ts +11 -3
  278. package/tests/odd-routing-contract.test.ts +80 -28
  279. package/tests/orchestrator-budget.test.ts +30 -5
  280. package/tests/orchestrator-consultation-sdk.test.ts +32 -6
  281. package/tests/orchestrator-delegation-mode.test.ts +2 -1
  282. package/tests/orchestrator-discovery.test.ts +28 -0
  283. package/tests/orchestrator-prompt-budget.test.ts +11 -2
  284. package/tests/orchestrator-prompt-diet-contract.test.ts +221 -0
  285. package/tests/orchestrator-rdd-ownership.test.ts +2 -1
  286. package/tests/orchestrator-work-search.test.ts +4 -2
  287. package/tests/package-files.test.ts +25 -1
  288. package/tests/package-manifest.test.ts +11 -10
  289. package/tests/parallel-reason-contract.test.ts +6 -3
  290. package/tests/permission-allowlist.test.ts +200 -30
  291. package/tests/persona-neutral-voseo.test.ts +4 -4
  292. package/tests/persona-reply-language.test.ts +1 -1
  293. package/tests/persona-single-channel.test.ts +13 -5
  294. package/tests/pi-update-launcher.test.ts +17 -7
  295. package/tests/pi-update-source.test.ts +113 -6
  296. package/tests/pi-update.test.ts +294 -19
  297. package/tests/pnpm-workspace-policy.test.ts +47 -0
  298. package/tests/primary-context-files.test.ts +233 -0
  299. package/tests/primary-context-three-files.test.ts +174 -0
  300. package/tests/provider-defect-handoff.test.ts +9 -2
  301. package/tests/rdd-aware-verification-contract.test.ts +1 -1
  302. package/tests/rdd-status-line.test.ts +94 -1
  303. package/tests/reply-language-anchor-placement.test.ts +184 -0
  304. package/tests/reply-language-anchor.test.ts +17 -1
  305. package/tests/request-context-append-only.test.ts +218 -0
  306. package/tests/request-context-placement.test.ts +148 -0
  307. package/tests/review-agent-end-preflight.test.ts +79 -1
  308. package/tests/review-candidate-view.test.ts +65 -0
  309. package/tests/review-continuation-registry.test.ts +31 -0
  310. package/tests/review-contract-prompt.test.ts +1 -1
  311. package/tests/review-controller.test.ts +2 -1
  312. package/tests/review-coordinator-correction-gate.test.ts +105 -0
  313. package/tests/review-coordinator-findings.test.ts +206 -0
  314. package/tests/review-coordinator-nonblocking-scope.test.ts +169 -0
  315. package/tests/review-coordinator.test.ts +271 -4
  316. package/tests/review-delivery-preflight.test.ts +462 -0
  317. package/tests/review-due-compaction.test.ts +31 -0
  318. package/tests/review-due-public-boundary.test.ts +26 -0
  319. package/tests/review-integration-nonblocking-findings.test.ts +108 -0
  320. package/tests/review-json-arguments.test.ts +41 -2
  321. package/tests/review-ledger-contract.test.ts +19 -40
  322. package/tests/review-loadout-rdd-off-sdk.test.ts +30 -1
  323. package/tests/review-one-call-findings.test.ts +131 -0
  324. package/tests/review-one-call-followups.test.ts +308 -0
  325. package/tests/review-one-call-reviewer-requests.test.ts +207 -0
  326. package/tests/review-one-call.test.ts +452 -0
  327. package/tests/review-relay-transport-agent.test.ts +28 -1
  328. package/tests/review-reviewer-evidence.test.ts +41 -1
  329. package/tests/review-risk-assessment.test.ts +52 -5
  330. package/tests/review-run-statement-guidance-independent.test.ts +54 -0
  331. package/tests/review-session-standing-permission-controller.test.ts +32 -2
  332. package/tests/review-tool-prose-budget.test.ts +1 -0
  333. package/tests/reviewer-stream.test.ts +149 -0
  334. package/tests/session-profile-extension.test.ts +2 -2
  335. package/tests/shell-prompt.test.ts +17 -0
  336. package/tests/shell-usage.test.ts +59 -10
  337. package/tests/skill-contradictions-contract.test.ts +122 -0
  338. package/tests/skill-registry-watcher.test.ts +216 -2
  339. package/tests/skill-registry.test.ts +929 -306
  340. package/tests/support/design-canvas-dom.ts +369 -0
  341. package/tests/support/review-coordinator-runtime.ts +212 -17
  342. package/tests/task-size-routing-contract.test.ts +42 -11
  343. package/tests/test-strength-contract.test.ts +2 -1
  344. package/tests/todo-request-append-only.test.ts +214 -0
  345. package/tests/tool-batching-contract.test.ts +3 -1
  346. package/tests/update-agent-body-history.test.ts +138 -0
  347. package/tests/verification-tiers-contract.test.ts +229 -0
  348. package/tests/verifier-preparation.test.ts +229 -0
  349. package/tests/verify-agent-contract.test.ts +20 -2
  350. package/tests/verify-first-launch-contract.test.ts +87 -11
  351. package/tests/vim-editor-adapter.test.ts +2 -2
  352. package/tests/work-continuity-contract.test.ts +54 -11
  353. package/tests/writer-admission-messages.test.ts +232 -0
  354. package/tests/writer-batch-admission.test.ts +66 -0
  355. package/tests/writer-edit-surface-scope.test.ts +52 -20
  356. package/tests/writer-feature-document-admission.test.ts +184 -0
  357. package/tests/yolo-mode.test.ts +39 -0
  358. package/skills/_shared/review-ledger-contract.md +0 -106
  359. /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/README.md +0 -0
  360. /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/schemas/lens.schema.json +0 -0
  361. /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/schemas/refuter.schema.json +0 -0
  362. /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/schemas/targeted-validator.schema.json +0 -0
  363. /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/vectors/lens.json +0 -0
  364. /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/vectors/refuter.json +0 -0
  365. /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/vectors/targeted-validator.json +0 -0
@@ -0,0 +1,937 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import vm from "node:vm";
4
+ import { findAll, loadCanvas, read, structuredCopy, withClass, type Listener } from "./support/design-canvas-dom.ts";
5
+
6
+ // inspector.js is a classic script injected into each artboard, so it still runs
7
+ // in a vm context with a minimal fake DOM.
8
+
9
+ function loadInspector({ fonts = false } = {}) {
10
+ const posted: any[] = [];
11
+ const listeners = new Map<string, Listener[]>();
12
+ // The document's capture-phase listeners: an image's load does not bubble, so only these see it.
13
+ const captures = new Map<string, Listener[]>();
14
+ // The MutationObserver callbacks the page registers, for the test to call with records.
15
+ const observers: Array<(records: unknown[]) => void> = [];
16
+ // document.fonts (a FontFaceSet) when the test asks for one; older engines and the other tests have none.
17
+ const fontListeners = new Map<string, Listener[]>();
18
+ class Element {}
19
+ const style = () => ({ cursor: "" });
20
+ const makeNode = () => Object.assign(new Element(), {
21
+ style: style(),
22
+ isConnected: false,
23
+ setAttribute() {},
24
+ hasAttribute: () => false,
25
+ getBoundingClientRect: () => ({ left: 0, top: 0, width: 10, height: 10 }),
26
+ });
27
+ // The ids of the elements scrolled into view (a highlight).
28
+ const scrolled: string[] = [];
29
+ // `computed`: the element's computed style, as getComputedStyle reports it (the token inspector measures it).
30
+ const created: any[] = [];
31
+ const button: Record<string, any> = Object.assign(new Element(), {
32
+ id: "cta",
33
+ isConnected: true,
34
+ scrollIntoView() { scrolled.push("cta"); },
35
+ localName: "button",
36
+ className: "buy primary",
37
+ innerText: "Buy now",
38
+ childNodes: [{ nodeType: 3, textContent: " Buy now " }, { nodeType: 1, textContent: "icon" }],
39
+ parentElement: null,
40
+ hasAttribute: () => false,
41
+ getAttribute: () => null,
42
+ getBoundingClientRect: () => ({ left: 4, top: 8, width: 60, height: 20 }),
43
+ computed: {},
44
+ });
45
+ const scrolling = { scrollTop: 0, scrollLeft: 0, clientHeight: 100, scrollHeight: 100, clientWidth: 100, scrollWidth: 100, parentElement: null };
46
+ // The page's width as the frame lays it out (clientWidth) and as wide as its content is (scrollWidth).
47
+ const documentElement = Object.assign(makeNode(), { append() {}, clientWidth: 100, scrollWidth: 100 });
48
+ const document = {
49
+ documentElement,
50
+ body: makeNode(),
51
+ scrollingElement: scrolling,
52
+ createElement: () => {
53
+ const node = makeNode();
54
+ created.push(node);
55
+ return node;
56
+ },
57
+ elementFromPoint: () => button,
58
+ querySelectorAll: (selector: string) => (selector === "#cta" ? [button] : []),
59
+ querySelector: (selector: string) => (selector === "#cta" ? button : null),
60
+ addEventListener(type: string, listener: Listener, options?: boolean | { capture?: boolean }) {
61
+ const capture = options === true || (typeof options === "object" && options.capture === true);
62
+ if (capture) captures.set(type, [...(captures.get(type) ?? []), listener]);
63
+ },
64
+ ...(fonts ? { fonts: { addEventListener(type: string, listener: Listener) { fontListeners.set(type, [...(fontListeners.get(type) ?? []), listener]); } } } : {}),
65
+ };
66
+ const parent = { postMessage: (message: any) => posted.push(structuredCopy(message)) };
67
+ // Animation frames wait until the test runs them, and so do timers (id: index + 1; `done` once run or cleared).
68
+ const frames: Array<() => void> = [];
69
+ const timeouts: Array<{ callback: () => void; ms: number; done: boolean }> = [];
70
+ const context: Record<string, any> = {
71
+ setTimeout: (callback: () => void, ms = 0) => timeouts.push({ callback, ms, done: false }),
72
+ clearTimeout: (id: number) => { if (timeouts[id - 1]) timeouts[id - 1].done = true; },
73
+ innerWidth: 100,
74
+ document,
75
+ Element,
76
+ CSS: { escape: (value: string) => value },
77
+ getComputedStyle: (node: any) => ({ overflowX: "hidden", overflowY: "hidden", ...node?.computed }),
78
+ requestAnimationFrame: (callback: () => void) => { frames.push(callback); return frames.length; },
79
+ MutationObserver: class { constructor(callback: (records: unknown[]) => void) { observers.push(callback); } observe() {} },
80
+ parent,
81
+ addEventListener(type: string, listener: Listener) {
82
+ listeners.set(type, [...(listeners.get(type) ?? []), listener]);
83
+ },
84
+ };
85
+ context.window = context;
86
+ vm.runInNewContext(read("inspector.js"), context);
87
+ const fire = (type: string, init: Record<string, unknown>) => {
88
+ const event: any = { defaultPrevented: false, propagationStopped: false, ...init, preventDefault() { event.defaultPrevented = true; }, stopPropagation() { event.propagationStopped = true; }, stopImmediatePropagation() { event.propagationStopped = true; } };
89
+ for (const listener of listeners.get(type) ?? []) listener(event);
90
+ return event;
91
+ };
92
+ const fromParent = (data: unknown) => fire("message", { source: parent, data });
93
+ /** An event that does not bubble (an image's load) reaching the document on its way down. */
94
+ const captureOnDocument = (type: string, target: unknown) => {
95
+ for (const listener of captures.get(type) ?? []) listener({ type, target });
96
+ };
97
+ /** The page changed: every MutationObserver gets these records. */
98
+ const mutate = (records: unknown[]) => {
99
+ for (const callback of observers) callback(records);
100
+ };
101
+ /** The page's font set finished loading fonts (loadingdone). */
102
+ const fontsLoaded = () => {
103
+ for (const listener of fontListeners.get("loadingdone") ?? []) listener({ type: "loadingdone" });
104
+ };
105
+ const runFrames = () => { while (frames.length > 0) frames.shift()!(); };
106
+ const pendingTimeouts = () => timeouts.filter((timer) => !timer.done).map((timer) => timer.ms);
107
+ const runTimeouts = () => {
108
+ for (const timer of timeouts.slice()) {
109
+ if (timer.done) continue;
110
+ timer.done = true;
111
+ timer.callback();
112
+ }
113
+ };
114
+ // The inspector makes its hover outline first, then the selected one.
115
+ const [hoverBox, selectedBox] = created;
116
+ return { posted, fire, fromParent, context, document, documentElement, runFrames, scrolled, button, Element, pendingTimeouts, runTimeouts, captureOnDocument, mutate, fontsLoaded, makeNode, hoverBox, selectedBox };
117
+ }
118
+
119
+ test("the inspector reports whether its page is wider than the frame, once per change", () => {
120
+ const inspector = loadInspector();
121
+ const root = inspector.documentElement as unknown as Record<string, unknown>;
122
+ inspector.context.innerWidth = 390;
123
+ root.clientWidth = 375;
124
+ root.scrollWidth = 520;
125
+ inspector.fire("load", {});
126
+ assert.deepEqual(inspector.posted, [], "reports wait for the next frame");
127
+ inspector.runFrames();
128
+ assert.deepEqual(inspector.posted, [{ type: "gd:overflow", overflow: true, width: 390, scrollWidth: 520 }]);
129
+ inspector.fire("resize", {});
130
+ inspector.runFrames();
131
+ assert.equal(inspector.posted.length, 1, "an unchanged report is not repeated");
132
+ root.scrollWidth = 375;
133
+ inspector.fire("resize", {});
134
+ inspector.fire("resize", {});
135
+ inspector.runFrames();
136
+ assert.deepEqual(inspector.posted.slice(1), [{ type: "gd:overflow", overflow: false, width: 390, scrollWidth: 375 }], "two resizes in one frame report once");
137
+ inspector.context.innerWidth = 820;
138
+ root.clientWidth = 805;
139
+ root.scrollWidth = 805;
140
+ inspector.fire("resize", {});
141
+ inspector.runFrames();
142
+ assert.deepEqual(inspector.posted.at(-1), { type: "gd:overflow", overflow: false, width: 820, scrollWidth: 805 }, "a new frame width is reported even when the page still fits");
143
+ });
144
+
145
+ test("the inspector checks the page's width again when the MutationObserver sees a page change, not its own boxes", () => {
146
+ const inspector = loadInspector();
147
+ const root = inspector.documentElement as unknown as Record<string, unknown>;
148
+ inspector.fire("load", {});
149
+ inspector.runFrames();
150
+ assert.deepEqual(inspector.posted, [{ type: "gd:overflow", overflow: false, width: 100, scrollWidth: 100 }]);
151
+ root.scrollWidth = 260;
152
+ const ownBox = Object.assign(inspector.makeNode(), { hasAttribute: () => true });
153
+ inspector.mutate([{ type: "attributes", target: ownBox }]);
154
+ inspector.runFrames();
155
+ assert.equal(inspector.posted.length, 1, "the inspector's own boxes moving is no page change");
156
+ inspector.mutate([{ type: "childList", target: inspector.makeNode() }]);
157
+ inspector.runFrames();
158
+ assert.deepEqual(inspector.posted.slice(1), [{ type: "gd:overflow", overflow: true, width: 100, scrollWidth: 260 }]);
159
+ });
160
+
161
+ test("the inspector checks the page's width again when an image loads late, though load does not bubble", () => {
162
+ const inspector = loadInspector();
163
+ const root = inspector.documentElement as unknown as Record<string, unknown>;
164
+ inspector.fire("load", {});
165
+ inspector.runFrames();
166
+ assert.deepEqual(inspector.posted, [{ type: "gd:overflow", overflow: false, width: 100, scrollWidth: 100 }]);
167
+ // A lazy image without a fixed size loads after the window's load and widens the page: no mutation, no resize.
168
+ root.scrollWidth = 340;
169
+ inspector.captureOnDocument("load", inspector.makeNode());
170
+ inspector.runFrames();
171
+ assert.deepEqual(inspector.posted.slice(1), [{ type: "gd:overflow", overflow: true, width: 100, scrollWidth: 340 }]);
172
+ });
173
+
174
+ test("the inspector checks the page's width again when a web font loads late", () => {
175
+ const inspector = loadInspector({ fonts: true });
176
+ const root = inspector.documentElement as unknown as Record<string, unknown>;
177
+ inspector.fire("load", {});
178
+ inspector.runFrames();
179
+ assert.deepEqual(inspector.posted, [{ type: "gd:overflow", overflow: false, width: 100, scrollWidth: 100 }]);
180
+ // A web font that swaps in after the window's load widens the page: no mutation, no resize, no load event.
181
+ root.scrollWidth = 340;
182
+ inspector.fontsLoaded();
183
+ inspector.runFrames();
184
+ assert.deepEqual(inspector.posted.slice(1), [{ type: "gd:overflow", overflow: true, width: 100, scrollWidth: 340 }]);
185
+ });
186
+
187
+ test("gd:size: the inspector reports its overflow again even when unchanged, and tags its picks with that size's generation", () => {
188
+ const inspector = loadInspector();
189
+ inspector.fire("load", {});
190
+ inspector.runFrames();
191
+ assert.deepEqual(inspector.posted, [{ type: "gd:overflow", overflow: false, width: 100, scrollWidth: 100 }]);
192
+ // The canvas dropped what the page reported (a load, a new size): the same answer is said again.
193
+ inspector.fromParent({ type: "gd:size", generation: 3 });
194
+ inspector.runFrames();
195
+ assert.deepEqual(inspector.posted.slice(1), [{ type: "gd:overflow", overflow: false, width: 100, scrollWidth: 100 }]);
196
+ inspector.fromParent({ type: "gd:mode", enabled: true });
197
+ inspector.fromParent({ type: "gd:pick-at", x: 10, y: 12 });
198
+ assert.deepEqual(inspector.posted.at(-1), { type: "gd:pick", selector: "#cta", excerpt: "Buy now", rect: { x: 4, y: 8, width: 60, height: 20 }, generation: 3 });
199
+ inspector.fromParent({ type: "gd:size", generation: "4" });
200
+ inspector.fromParent({ type: "gd:size", generation: -1 });
201
+ inspector.fromParent({ type: "gd:pick-at", x: 10, y: 12 });
202
+ assert.deepEqual(inspector.posted.at(-1), { type: "gd:pick", selector: "#cta", excerpt: "Buy now", rect: { x: 4, y: 8, width: 60, height: 20 }, generation: 3 }, "a malformed generation is ignored");
203
+ });
204
+
205
+ /**
206
+ * The canvas and a size frame's real inspector, wired together: what the canvas posts to the frame reaches the
207
+ * inspector at once, and deliver() hands the canvas what the inspector posted so far.
208
+ */
209
+ async function allSizesWithInspector() {
210
+ const canvas = await loadCanvas(300, { "/t/api/designs/alpha": { slug: "alpha", title: "Alpha", brief: "", artboards: [
211
+ { file: "01-a.html", title: "First", tradeoff: "", width: 1200, height: 800 },
212
+ ] } });
213
+ const $ = (id: string) => canvas.document.getElementById(id);
214
+ for (const id of ["gd-popover", "gd-menu", "gd-device-menu"]) $(id).hidden = true;
215
+ canvas.boards[0].shield!.dispatch("keydown", { key: "Enter" });
216
+ $("gd-device").dispatch("click");
217
+ findAll($("gd-device-menu"), (element) => element.dataset.size === "all")[0].dispatch("click");
218
+ const phoneSection = withClass(canvas.viewport, "gd-size-frame")[0];
219
+ const phone = findAll(phoneSection, (element) => element.localName === "iframe")[0];
220
+ const chipHidden = () => withClass(phoneSection, "gd-overflow-flag")[0].hidden;
221
+ // The page the Phone frame shows now: a navigation replaces it, and the old one hears nothing more.
222
+ let current: ReturnType<typeof loadInspector> | null = null;
223
+ const window = phone.contentWindow!;
224
+ const post = window.postMessage;
225
+ window.postMessage = (message: unknown) => {
226
+ post(message);
227
+ current?.fromParent(structuredCopy(message));
228
+ };
229
+ /** A page loading in the Phone frame: a fresh inspector, 390 px wide, whose content is `scrollWidth` wide. */
230
+ const page = (scrollWidth: number) => {
231
+ const inspector = loadInspector();
232
+ inspector.context.innerWidth = 390;
233
+ Object.assign(inspector.documentElement as unknown as Record<string, unknown>, { clientWidth: 390, scrollWidth });
234
+ current = inspector;
235
+ const deliver = () => {
236
+ inspector.runFrames();
237
+ for (const message of inspector.posted.splice(0)) canvas.message(window, message);
238
+ };
239
+ return { inspector, deliver };
240
+ };
241
+ return { phone, chipHidden, page };
242
+ }
243
+
244
+ test("S26: the Overflows chip survives the frame's load event that comes after an early overflow report", async () => {
245
+ const { phone, chipHidden, page } = await allSizesWithInspector();
246
+ const { inspector, deliver } = page(520);
247
+ // An image loads before the page does and the page reports that it overflows.
248
+ inspector.captureOnDocument("load", inspector.makeNode());
249
+ deliver();
250
+ assert.equal(chipHidden(), false, "the early report shows the chip");
251
+ // The page's own load queues a check; the canvas sees the frame's load before that check runs.
252
+ inspector.fire("load", {});
253
+ phone.dispatch("load");
254
+ deliver();
255
+ assert.equal(chipHidden(), false, "the page still overflows, so the chip is shown again after the load");
256
+ });
257
+
258
+ test("S26: a frame that navigates to another page shows that page's answer, not the previous one's", async () => {
259
+ const { phone, chipHidden, page } = await allSizesWithInspector();
260
+ const first = page(520);
261
+ first.inspector.fire("load", {});
262
+ phone.dispatch("load");
263
+ first.deliver();
264
+ assert.equal(chipHidden(), false);
265
+ // The next page fits: its own reports decide, whether they come before or after the frame's load.
266
+ const next = page(390);
267
+ next.inspector.captureOnDocument("load", next.inspector.makeNode());
268
+ next.deliver();
269
+ assert.equal(chipHidden(), true, "the new page's early report hides the chip");
270
+ next.inspector.fire("load", {});
271
+ phone.dispatch("load");
272
+ next.deliver();
273
+ assert.equal(chipHidden(), true);
274
+ const back = page(520);
275
+ back.inspector.fire("load", {});
276
+ phone.dispatch("load");
277
+ back.deliver();
278
+ assert.equal(chipHidden(), false, "a page that overflows again shows it");
279
+ });
280
+
281
+ test("the inspector posts gd:exit-focus on Shift+Esc and keeps plain Esc for the design", () => {
282
+ const inspector = loadInspector();
283
+ const plain = inspector.fire("keydown", { key: "Escape", shiftKey: false });
284
+ assert.deepEqual(inspector.posted, []);
285
+ assert.equal(plain.defaultPrevented, false);
286
+ const chord = inspector.fire("keydown", { key: "Escape", shiftKey: true });
287
+ assert.deepEqual(inspector.posted, [{ type: "gd:exit-focus" }]);
288
+ assert.equal(chord.defaultPrevented, true);
289
+ inspector.fire("keydown", { key: "Enter", shiftKey: true });
290
+ assert.equal(inspector.posted.length, 1);
291
+ });
292
+
293
+ test("the inspector picks the element under a routed point only in comment mode", () => {
294
+ const inspector = loadInspector();
295
+ inspector.fromParent({ type: "gd:pick-at", x: 10, y: 12 });
296
+ assert.deepEqual(inspector.posted, [], "no pick outside comment mode");
297
+ inspector.fromParent({ type: "gd:mode", enabled: true });
298
+ inspector.fromParent({ type: "gd:pick-at", x: 10, y: 12 });
299
+ assert.deepEqual(inspector.posted, [{ type: "gd:pick", selector: "#cta", excerpt: "Buy now", rect: { x: 4, y: 8, width: 60, height: 20 }, generation: 0 }], "a page that heard no size yet tags generation 0");
300
+ inspector.fromParent({ type: "gd:pick-at", x: "10", y: 12 });
301
+ assert.equal(inspector.posted.length, 1, "non-numeric points are ignored");
302
+ });
303
+
304
+ test("gd:highlight finds the element by a part's selector or by a comment pin's id", () => {
305
+ const inspector = loadInspector();
306
+ inspector.fromParent({ type: "gd:highlight", selector: "#cta" });
307
+ assert.deepEqual(inspector.scrolled, ["cta"], "a part's selector");
308
+ inspector.fromParent({ type: "gd:highlight", selector: "#missing" });
309
+ inspector.fromParent({ type: "gd:highlight", selector: `#${"x".repeat(500)}` });
310
+ inspector.fromParent({ type: "gd:highlight", selector: 3 });
311
+ assert.deepEqual(inspector.scrolled, ["cta"], "no match, a selector over 500 characters or not text: nothing");
312
+ inspector.fromParent({ type: "gd:highlight", id: "c1" });
313
+ assert.deepEqual(inspector.scrolled, ["cta"], "an id with no pin finds nothing");
314
+ inspector.fromParent({ type: "gd:pins", pins: [{ id: "c1", selector: "#cta" }] });
315
+ inspector.fromParent({ type: "gd:highlight", id: "c1" });
316
+ assert.deepEqual(inspector.scrolled, ["cta", "cta"], "a comment pin's id still works");
317
+ assert.deepEqual(inspector.posted, [], "a highlight posts nothing");
318
+ });
319
+
320
+ test("gd:highlight with locate says where the element is once in view, and again after scrolls (once per frame) until gd:clear or another highlight", () => {
321
+ const inspector = loadInspector();
322
+ const located = () => inspector.posted.filter((message) => message.type === "gd:located");
323
+ inspector.fromParent({ type: "gd:highlight", selector: "#cta", locate: true });
324
+ assert.deepEqual([inspector.scrolled, inspector.posted], [["cta"], [{ type: "gd:located", selector: "#cta", rect: { x: 4, y: 8, width: 60, height: 20 } }]], "after it scrolls into view");
325
+ inspector.button.getBoundingClientRect = () => ({ left: 4, top: 120, width: 60, height: 20 });
326
+ inspector.fire("scroll", {});
327
+ inspector.fire("scroll", {});
328
+ assert.equal(located().length, 1, "reports wait for the next frame");
329
+ inspector.runFrames();
330
+ assert.deepEqual(located().slice(1), [{ type: "gd:located", selector: "#cta", rect: { x: 4, y: 120, width: 60, height: 20 } }], "two scrolls, one report");
331
+ inspector.fire("scroll", {});
332
+ inspector.fromParent({ type: "gd:clear" });
333
+ inspector.runFrames();
334
+ inspector.fire("scroll", {});
335
+ inspector.runFrames();
336
+ assert.equal(located().length, 2, "nothing after gd:clear, not even a scroll queued before it");
337
+ inspector.fromParent({ type: "gd:highlight", selector: "#cta", locate: true });
338
+ inspector.fromParent({ type: "gd:highlight", selector: "#cta" });
339
+ inspector.fire("scroll", {});
340
+ inspector.runFrames();
341
+ assert.equal(located().length, 3, "a highlight without locate (a part, a comment) ends it");
342
+ inspector.fromParent({ type: "gd:highlight", selector: "#missing", locate: true });
343
+ inspector.fire("scroll", {});
344
+ inspector.runFrames();
345
+ assert.equal(located().length, 3, "no element, no report");
346
+ });
347
+
348
+ test("gd:mode off hides the hover and selected outlines, which a PNG export relies on, and hovers stay hidden until it is on again", () => {
349
+ const inspector = loadInspector();
350
+ inspector.fromParent({ type: "gd:mode", enabled: true });
351
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
352
+ inspector.fromParent({ type: "gd:pick-at", x: 10, y: 12 });
353
+ assert.deepEqual([inspector.hoverBox.style.display, inspector.selectedBox.style.display], ["block", "block"]);
354
+ inspector.fromParent({ type: "gd:mode", enabled: false });
355
+ assert.deepEqual([inspector.hoverBox.style.display, inspector.selectedBox.style.display], ["none", "none"]);
356
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
357
+ assert.equal(inspector.hoverBox.style.display, "none", "no hover outline while comment mode is off");
358
+ inspector.fromParent({ type: "gd:mode", enabled: true });
359
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
360
+ assert.equal(inspector.hoverBox.style.display, "block", "hover works again once it is back on");
361
+ });
362
+
363
+ test("in focus mode the inspector leaves the wheel to the artboard and never pans the canvas", () => {
364
+ const inspector = loadInspector();
365
+ const wheel = { deltaX: 0, deltaY: 50, deltaMode: 0, shiftKey: false, clientX: 1, clientY: 1, target: null };
366
+ const unfocused = inspector.fire("wheel", { ...wheel, ctrlKey: false, metaKey: false });
367
+ assert.equal(unfocused.defaultPrevented, true);
368
+ assert.equal(inspector.posted.at(-1).type, "gd:wheel");
369
+ inspector.posted.length = 0;
370
+ inspector.fromParent({ type: "gd:focus", enabled: true });
371
+ const plain = inspector.fire("wheel", { ...wheel, ctrlKey: false, metaKey: false });
372
+ assert.equal(plain.defaultPrevented, false, "the artboard scrolls natively");
373
+ const zoom = inspector.fire("wheel", { ...wheel, ctrlKey: true, metaKey: false });
374
+ assert.equal(zoom.defaultPrevented, true, "Ctrl/Cmd+wheel never zooms the browser page");
375
+ assert.deepEqual(inspector.posted, []);
376
+ });
377
+
378
+ // ---- the token inspector's measure (gd:inspect)
379
+
380
+ /** The computed style of a 14 px bold button with padding and rounded corners, as a browser reports it. */
381
+ const BUTTON_STYLE = {
382
+ color: "rgb(17, 17, 17)", backgroundColor: "rgb(255, 212, 121)", backgroundImage: "none",
383
+ fontFamily: "Inter, sans-serif", fontSize: "14px", fontWeight: "bold", lineHeight: "20px",
384
+ marginTop: "0px", marginRight: "4px", marginBottom: "0px", marginLeft: "4px",
385
+ paddingTop: "8px", paddingRight: "16px", paddingBottom: "8px", paddingLeft: "16px",
386
+ borderTopLeftRadius: "6px", borderTopRightRadius: "6px", borderBottomRightRadius: "50%", borderBottomLeftRadius: "6px",
387
+ };
388
+ const BUTTON_MEASURE = {
389
+ selector: "#cta", label: "button#cta", text: "Buy now", hasText: true,
390
+ rect: { x: 4, y: 8, width: 60, height: 20 },
391
+ color: { r: 17, g: 17, b: 17, a: 1 }, background: { r: 255, g: 212, b: 121 },
392
+ fontFamily: "Inter, sans-serif", fontSize: 14, fontWeight: 700, lineHeight: 20,
393
+ margin: [0, 4, 0, 4], padding: [8, 16, 8, 16], radius: [6, 6, 10, 6], rootFontSize: 16,
394
+ };
395
+ const inspects = (inspector: ReturnType<typeof loadInspector>) => inspector.posted.filter((message) => message.type === "gd:inspect");
396
+
397
+ test("in Comment mode with Technical details on the inspector measures the hovered element once per element, and says when none is hovered", () => {
398
+ const inspector = loadInspector();
399
+ inspector.button.computed = BUTTON_STYLE;
400
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
401
+ inspector.fire("pointerover", { target: inspector.button });
402
+ assert.deepEqual(inspector.posted, [], "nothing outside Comment mode");
403
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
404
+ assert.deepEqual(inspector.posted, [], "Comment mode with nothing hovered says nothing");
405
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
406
+ assert.deepEqual(inspector.posted, [{ type: "gd:inspect", element: BUTTON_MEASURE, generation: 0 }]);
407
+ inspector.fromParent({ type: "gd:hover-at", x: 11, y: 12 });
408
+ inspector.fire("pointerover", { target: inspector.button });
409
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
410
+ assert.equal(inspector.posted.length, 1, "the same element is reported once");
411
+ inspector.fire("scroll", {});
412
+ inspector.runFrames();
413
+ assert.equal(inspects(inspector).length, 2, "a scroll moves the element: reported again");
414
+ inspector.fire("pointerout", { relatedTarget: inspector.button });
415
+ assert.equal(inspects(inspector).length, 2, "moving to another element of the page is not leaving it");
416
+ inspector.fire("pointerout", { relatedTarget: null });
417
+ assert.deepEqual(inspector.posted.at(-1), { type: "gd:inspect", element: null, generation: 0 }, "the pointer left the page");
418
+ inspector.fire("scroll", {});
419
+ inspector.runFrames();
420
+ assert.equal(inspects(inspector).length, 3, "nothing hovered, nothing to report on scroll");
421
+ inspector.fire("pointerover", { target: inspector.button });
422
+ assert.deepEqual(inspector.posted.at(-1), { type: "gd:inspect", element: BUTTON_MEASURE, generation: 0 });
423
+ inspector.fromParent({ type: "gd:mode", enabled: false, inspect: true });
424
+ assert.deepEqual(inspector.posted.at(-1), { type: "gd:inspect", element: null, generation: 0 }, "leaving Comment mode clears it");
425
+ assert.equal(inspects(inspector).length, 5);
426
+ });
427
+
428
+ test("S1: each measurement is tagged with the size generation the page heard last, so the canvas can drop one made at an old size", () => {
429
+ const inspector = loadInspector();
430
+ inspector.button.computed = BUTTON_STYLE;
431
+ inspector.fromParent({ type: "gd:size", generation: 3 });
432
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
433
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
434
+ assert.deepEqual(inspects(inspector), [{ type: "gd:inspect", element: BUTTON_MEASURE, generation: 3 }]);
435
+ inspector.fromParent({ type: "gd:size", generation: 4 });
436
+ inspector.fire("scroll", {});
437
+ inspector.runFrames();
438
+ assert.deepEqual(inspects(inspector).at(-1), { type: "gd:inspect", element: BUTTON_MEASURE, generation: 4 });
439
+ });
440
+
441
+ test("the measured background composites translucent layers up to the first opaque one, and is unknown over an image or gradient", () => {
442
+ const inspector = loadInspector();
443
+ const layer = (computed: Record<string, string>, parentElement: unknown = null) => ({ computed, parentElement });
444
+ const page = layer({ backgroundColor: "rgb(0, 0, 255)", backgroundImage: "none" });
445
+ const tint = layer({ backgroundColor: "rgba(255, 0, 0, 0.5)", backgroundImage: "none" }, page);
446
+ inspector.button.computed = { ...BUTTON_STYLE, backgroundColor: "rgba(0, 0, 0, 0)", color: "rgb(10 20 30 / 50%)", lineHeight: "normal", fontWeight: "500" };
447
+ inspector.button.parentElement = tint;
448
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
449
+ inspector.fromParent({ type: "gd:hover-at", x: 1, y: 1 });
450
+ const element = inspector.posted.at(-1).element;
451
+ assert.deepEqual([element.background, element.color, element.lineHeight, element.fontWeight], [{ r: 128, g: 0, b: 128 }, { r: 10, g: 20, b: 30, a: 0.5 }, null, 500]);
452
+
453
+ // No opaque layer at all: the white page is behind it.
454
+ page.computed = { backgroundColor: "transparent", backgroundImage: "none" };
455
+ inspector.fire("scroll", {});
456
+ inspector.runFrames();
457
+ assert.deepEqual(inspector.posted.at(-1).element.background, { r: 255, g: 128, b: 128 });
458
+ page.computed = { backgroundColor: "rgb(0, 0, 255)", backgroundImage: "linear-gradient(red, blue)" };
459
+ inspector.fire("scroll", {});
460
+ inspector.runFrames();
461
+ assert.equal(inspector.posted.at(-1).element.background, null, "a gradient behind: unknown");
462
+ page.computed = { backgroundColor: "color(display-p3 1 0 0)", backgroundImage: "none" };
463
+ inspector.fire("scroll", {});
464
+ inspector.runFrames();
465
+ assert.equal(inspector.posted.at(-1).element.background, null, "a color it cannot read: unknown");
466
+ });
467
+
468
+ test("with Technical details off the inspector outlines the element but never measures it; turning it on reports the hovered one", () => {
469
+ const inspector = loadInspector();
470
+ inspector.button.computed = BUTTON_STYLE;
471
+ const computed = inspector.context.getComputedStyle;
472
+ let styles = 0;
473
+ inspector.context.getComputedStyle = (node: unknown) => { styles += 1; return computed(node); };
474
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: false });
475
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
476
+ inspector.fire("scroll", {});
477
+ inspector.fire("pointerout", { relatedTarget: null });
478
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
479
+ inspector.runFrames();
480
+ assert.deepEqual([inspects(inspector).length, styles], [0, 0], "nothing measured, nothing posted");
481
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
482
+ assert.deepEqual(inspects(inspector), [{ type: "gd:inspect", element: BUTTON_MEASURE, generation: 0 }], "on: the element already hovered, at once");
483
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
484
+ assert.equal(inspects(inspector).length, 1, "still on: not again");
485
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: false });
486
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
487
+ assert.equal(inspects(inspector).length, 2, "off and on again: reported again, so the panel can come back");
488
+ });
489
+
490
+ test("scrolls in one animation frame re-measure the hovered element once", () => {
491
+ const inspector = loadInspector();
492
+ inspector.button.computed = BUTTON_STYLE;
493
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
494
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
495
+ assert.equal(inspects(inspector).length, 1);
496
+ inspector.fire("scroll", {});
497
+ inspector.fire("scroll", {});
498
+ assert.equal(inspects(inspector).length, 1, "reports wait for the next frame");
499
+ inspector.runFrames();
500
+ assert.equal(inspects(inspector).length, 2, "two scrolls, one report");
501
+ inspector.fire("scroll", {});
502
+ inspector.fire("pointerout", { relatedTarget: null });
503
+ inspector.runFrames();
504
+ assert.deepEqual(inspects(inspector).slice(2), [{ type: "gd:inspect", element: null, generation: 0 }], "the pointer left before the frame: nothing to re-measure");
505
+ });
506
+
507
+ // ---- accessibility checks (gd:checks)
508
+
509
+ type PageSpec = { tag: string; attrs?: Record<string, string>; style?: Record<string, string>; size?: [number, number]; at?: [number, number]; text?: string; kids?: PageSpec[] };
510
+
511
+ /** What getComputedStyle reports for an element with no style of its own. */
512
+ const BASE_STYLE = {
513
+ display: "block", visibility: "visible", opacity: "1", color: "rgb(0, 0, 0)", backgroundColor: "rgba(0, 0, 0, 0)", backgroundImage: "none",
514
+ fontSize: "16px", fontWeight: "400", overflow: "visible", overflowX: "visible", overflowY: "visible", clip: "auto", clipPath: "none",
515
+ };
516
+
517
+ /** A small page for the inspector: elements in document order, with computed styles and boxes. */
518
+ function checkPage(specs: PageSpec[]) {
519
+ const inspector = loadInspector();
520
+ const Element = inspector.Element as new () => any;
521
+ const all: any[] = [];
522
+ const build = (spec: PageSpec, parent: any): any => {
523
+ const node = new Element();
524
+ all.push(node);
525
+ const attrs = spec.attrs ?? {};
526
+ Object.assign(node, {
527
+ localName: spec.tag, parentElement: parent, id: attrs.id ?? "", className: attrs.class ?? "", styleSpec: spec.style ?? {},
528
+ size: spec.size ?? [100, 24], childNodes: [], children: [], isConnected: true,
529
+ getAttribute: (name: string) => attrs[name] ?? null,
530
+ hasAttribute: (name: string) => name in attrs,
531
+ scrollIntoView() {},
532
+ });
533
+ node.index = all.length;
534
+ Object.defineProperty(node, "textContent", { get: () => node.childNodes.map((child: any) => child.textContent).join("") });
535
+ // `at` places the box (left, top); without it the elements stack down the page.
536
+ node.getBoundingClientRect = () => ({ left: spec.at?.[0] ?? 8, top: spec.at?.[1] ?? 8 + node.index * 30, width: node.size[0], height: node.size[1] });
537
+ // Like the browser: no box at all under display: none (its own or an ancestor's); a box, even 0 × 0, otherwise.
538
+ node.getClientRects = () => {
539
+ for (let current = node; current; current = current.parentElement) if (current.styleSpec.display === "none") return [];
540
+ return [node.getBoundingClientRect()];
541
+ };
542
+ if (spec.text) node.childNodes.push({ nodeType: 3, textContent: spec.text });
543
+ for (const kid of spec.kids ?? []) {
544
+ const child = build(kid, node);
545
+ child.nodeType = 1;
546
+ node.children.push(child);
547
+ node.childNodes.push(child);
548
+ }
549
+ return node;
550
+ };
551
+ const body = build({ tag: "body", kids: specs }, null);
552
+ const doc = inspector.document as any;
553
+ doc.body = body;
554
+ doc.querySelectorAll = (selector: string) => (selector === "*" ? all : selector.startsWith("#") ? all.filter((node) => node.id === selector.slice(1)) : []);
555
+ doc.querySelector = (selector: string) => (selector.startsWith("#") ? all.find((node) => node.id === selector.slice(1)) ?? null : null);
556
+ doc.getElementById = (id: string) => all.find((node) => node.id === id) ?? null;
557
+ Object.assign(inspector.documentElement, { scrollWidth: 1200, clientWidth: 1200 });
558
+ let styles = 0;
559
+ inspector.context.getComputedStyle = (element: any) => {
560
+ styles += 1;
561
+ return { ...BASE_STYLE, ...(element?.styleSpec ?? {}) };
562
+ };
563
+ const reports = () => inspector.posted.filter((message) => message.type === "gd:checks");
564
+ /** The canvas asks this frame to check itself (its artboard's own frame), and the page settles. */
565
+ const run = () => {
566
+ inspector.fromParent({ type: "gd:run-checks" });
567
+ inspector.runTimeouts();
568
+ return reports().at(-1) as { checks: Array<{ id: string; label: string; count: number; advice: number }>; issues: Array<Record<string, any>> };
569
+ };
570
+ return { inspector, all, run, reports, styles: () => styles };
571
+ }
572
+
573
+ const issuesOf = (report: { issues: Array<Record<string, any>> }, check: string, severity?: string) => report.issues.filter((issue) => issue.check === check && (severity === undefined || issue.severity === severity));
574
+ const gray = (value: number) => ({ color: `rgb(${value}, ${value}, ${value})` });
575
+
576
+ test("S4: each check result is tagged with the size generation the page heard last, and a new size sends even an unchanged result again", () => {
577
+ const page = checkPage([{ tag: "p", attrs: { id: "faint" }, text: "World", style: gray(200) }]);
578
+ page.inspector.fromParent({ type: "gd:size", generation: 2 });
579
+ const first = page.run() as unknown as Record<string, unknown>;
580
+ assert.equal(first.generation, 2);
581
+ page.inspector.fromParent({ type: "gd:size", generation: 3 });
582
+ page.inspector.fire("resize", {});
583
+ page.inspector.runTimeouts();
584
+ assert.equal(page.reports().length, 2, "the same findings at a new size are reported again");
585
+ assert.equal((page.reports().at(-1) as Record<string, unknown>).generation, 3);
586
+ });
587
+
588
+ test("checks run only in a frame the canvas asks (the artboard's own), once the page settles after load and after a resize, and post a changed result only", () => {
589
+ const page = checkPage([{ tag: "h1", text: "Hello" }, { tag: "p", attrs: { id: "faint" }, text: "World", style: gray(200) }, { tag: "button", text: "Go", size: [120, 48] }]);
590
+ page.inspector.fire("load", {});
591
+ page.inspector.fire("resize", {});
592
+ page.inspector.runTimeouts();
593
+ page.inspector.runFrames();
594
+ assert.deepEqual([page.reports().length, page.styles(), page.inspector.pendingTimeouts().length], [0, 0, 0], "not asked (a size frame, the system board): nothing scanned, nothing posted");
595
+ page.inspector.fire("message", { source: {}, data: { type: "gd:run-checks" } });
596
+ assert.deepEqual(page.inspector.pendingTimeouts(), [], "only the canvas can ask");
597
+ page.inspector.fromParent({ type: "gd:run-checks" });
598
+ assert.deepEqual(page.inspector.pendingTimeouts(), [250], "after a short wait, so late images, fonts and scripts are in");
599
+ assert.equal(page.reports().length, 0);
600
+ page.inspector.runTimeouts();
601
+ assert.equal(page.reports().length, 1);
602
+ const first = page.reports()[0];
603
+ assert.deepEqual(first.checks.map((check: { id: string; label: string; count: number; advice: number }) => [check.id, check.label, check.count, check.advice]), [
604
+ ["contrast", "Text contrast", 1, 0], ["target", "Target size", 0, 0], ["alt", "Images have alt text", 0, 0], ["label", "Form fields have labels", 0, 0], ["heading", "Headings in order", 0, 0],
605
+ ]);
606
+ page.inspector.fire("resize", {});
607
+ page.inspector.fire("resize", {});
608
+ assert.deepEqual(page.inspector.pendingTimeouts(), [400], "two resizes in a row: one run, once the size settles");
609
+ page.inspector.runTimeouts();
610
+ assert.equal(page.reports().length, 1, "an unchanged result is not sent again");
611
+ page.all.find((node) => node.localName === "button").size = [10, 10];
612
+ page.inspector.fire("resize", {});
613
+ page.inspector.runTimeouts();
614
+ assert.equal(page.reports().length, 2, "a resize that changes the result sends it");
615
+ assert.equal(page.reports()[1].issues.find((issue: { check: string }) => issue.check === "target").selector, "body > button");
616
+ });
617
+
618
+ test("the contrast check flags text under AA (4.5 : 1, 3 : 1 for large text) on its real background", () => {
619
+ const page = checkPage([
620
+ { tag: "p", attrs: { id: "low" }, text: "or pay with card", style: gray(119) },
621
+ { tag: "p", attrs: { id: "ok" }, text: "fine", style: gray(118) },
622
+ { tag: "p", attrs: { id: "large-ok" }, text: "Big", style: { ...gray(119), fontSize: "24px" } },
623
+ { tag: "p", attrs: { id: "large-low" }, text: "Big", style: { ...gray(170), fontSize: "24px" } },
624
+ { tag: "p", attrs: { id: "bold-ok" }, text: "Bold", style: { ...gray(119), fontSize: "19px", fontWeight: "700" } },
625
+ { tag: "p", attrs: { id: "bold-low" }, text: "Not bold", style: { ...gray(119), fontSize: "19px", fontWeight: "400" } },
626
+ { tag: "p", attrs: { id: "alpha" }, text: "Faint", style: { color: "rgba(0, 0, 0, 0.2)" } },
627
+ { tag: "p", attrs: { id: "dark" }, text: "Light on dark", style: { color: "rgb(246, 239, 243)", backgroundColor: "rgb(26, 18, 24)" } },
628
+ { tag: "div", style: { backgroundColor: "rgb(0, 0, 0)" }, kids: [{ tag: "p", attrs: { id: "inherits" }, text: "Dark text on a dark parent", style: gray(40) }] },
629
+ { tag: "div", style: { backgroundImage: "linear-gradient(red, blue)" }, kids: [{ tag: "p", attrs: { id: "on-image" }, text: "Unknown", style: gray(200) }] },
630
+ { tag: "p", attrs: { id: "hidden" }, text: "Hidden", style: { ...gray(200), display: "none" } },
631
+ { tag: "p", attrs: { id: "invisible" }, text: "Hidden", style: { ...gray(200), visibility: "hidden" } },
632
+ { tag: "p", attrs: { id: "empty" }, text: "Zero", style: gray(200), size: [0, 0] },
633
+ { tag: "button", attrs: { id: "off", disabled: "" }, text: "Disabled", style: gray(200), size: [100, 40] },
634
+ { tag: "p", attrs: { id: "no-text" }, text: " ", style: gray(200) },
635
+ { tag: "script", text: "var x", style: gray(255) },
636
+ ]);
637
+ const report = page.run();
638
+ assert.deepEqual(issuesOf(report, "contrast").map((issue) => issue.selector), ["#low", "#large-low", "#bold-low", "#alpha", "#inherits"]);
639
+ const low = issuesOf(report, "contrast")[0];
640
+ assert.deepEqual(low, {
641
+ check: "contrast", severity: "fail", title: "Low contrast", selector: "#low", label: "p#low", excerpt: "or pay with card", rect: { x: 8, y: 68, width: 100, height: 24 },
642
+ detail: '"or pay with card" is 4.47 : 1 (#777777 on #ffffff); WCAG AA needs 4.5 : 1.',
643
+ key: "contrast", vars: { text: "or pay with card", ratio: "4.47", foreground: "#777777", background: "#ffffff", needed: 4.5 },
644
+ });
645
+ assert.match(issuesOf(report, "contrast")[1].detail, /WCAG AA needs 3 : 1 for large text\.$/);
646
+ assert.match(issuesOf(report, "contrast")[3].detail, /\(#cccccc on #ffffff\)/, "translucent text is judged as composited over its background");
647
+ assert.deepEqual(report.checks.find((check) => check.id === "contrast"), { id: "contrast", label: "Text contrast", count: 5, advice: 0 });
648
+ });
649
+
650
+ test("the contrast check fades the text by its own alpha and by the opacity of the element and its ancestors", () => {
651
+ const page = checkPage([
652
+ { tag: "p", attrs: { id: "half" }, text: "Half", style: { opacity: "0.5" } },
653
+ { tag: "div", style: { opacity: "0.5" }, kids: [{ tag: "p", attrs: { id: "in-faded" }, text: "Inside" }] },
654
+ { tag: "div", style: { opacity: "0.9" }, kids: [{ tag: "p", attrs: { id: "slight" }, text: "Slight" }] },
655
+ { tag: "div", style: { opacity: "0" }, kids: [{ tag: "p", attrs: { id: "invisible" }, text: "Gone" }] },
656
+ { tag: "p", attrs: { id: "plain" }, text: "Plain" },
657
+ ]);
658
+ const report = page.run();
659
+ assert.deepEqual(issuesOf(report, "contrast").map((issue) => issue.selector), ["#half", "#in-faded"]);
660
+ assert.equal(issuesOf(report, "contrast")[0].detail, '"Half" is 3.94 : 1 (#808080 on #ffffff); WCAG AA needs 4.5 : 1.', "black on white at opacity 0.5 is about 3.95 : 1, not 21 : 1");
661
+ assert.equal(issuesOf(report, "contrast")[1].detail, '"Inside" is 3.94 : 1 (#808080 on #ffffff); WCAG AA needs 4.5 : 1.', "an ancestor's opacity counts too");
662
+ });
663
+
664
+ test("S6: an ancestor's opacity fades the text and its opaque background together over what lies behind that ancestor", () => {
665
+ const page = checkPage([
666
+ { tag: "div", style: { opacity: "0.5" }, kids: [{ tag: "p", attrs: { id: "opaque-child" }, text: "On its own white", style: { color: "rgb(0, 0, 0)", backgroundColor: "rgb(255, 255, 255)" } }] },
667
+ { tag: "div", style: { opacity: "0.5" }, kids: [{ tag: "p", attrs: { id: "in-faded" }, text: "Inside" }] },
668
+ { tag: "p", attrs: { id: "opaque-unfaded" }, text: "Not faded", style: { color: "rgb(0, 0, 0)", backgroundColor: "rgb(255, 255, 255)" } },
669
+ ]);
670
+ const report = page.run();
671
+ assert.deepEqual(issuesOf(report, "contrast").map((issue) => issue.selector), ["#opaque-child", "#in-faded"], "no finding for black on an opaque white with no faded ancestor");
672
+ assert.equal(issuesOf(report, "contrast")[0].detail, '"On its own white" is 3.94 : 1 (#808080 on #ffffff); WCAG AA needs 4.5 : 1.', "black on white faded to 0.5 over the white page: #808080 on #ffffff, not 21 : 1");
673
+ assert.equal(issuesOf(report, "contrast")[1].detail, '"Inside" is 3.94 : 1 (#808080 on #ffffff); WCAG AA needs 4.5 : 1.', "the transparent-child control is unchanged");
674
+ assert.equal(report.checks.find((check) => check.id === "contrast")?.count, 2, "only the two faded paragraphs fail");
675
+ });
676
+
677
+ test("S6: nested, zero and dark-backdrop opacities fade the group as drawn; a gradient behind a faded group stays unknown; with no opacity the colors are as before", () => {
678
+ const white = { color: "rgb(0, 0, 0)", backgroundColor: "rgb(255, 255, 255)" };
679
+ const page = checkPage([
680
+ { tag: "div", style: { opacity: "0.5" }, kids: [{ tag: "div", style: { opacity: "0.5" }, kids: [{ tag: "p", attrs: { id: "nested" }, text: "x", style: white }] }] },
681
+ { tag: "div", style: { backgroundColor: "rgb(0, 0, 0)" }, kids: [{ tag: "div", style: { opacity: "0.5" }, kids: [{ tag: "p", attrs: { id: "on-black" }, text: "x", style: white }] }] },
682
+ { tag: "div", style: { backgroundImage: "linear-gradient(red, blue)" }, kids: [{ tag: "div", style: { opacity: "0.5" }, kids: [{ tag: "p", attrs: { id: "on-gradient" }, text: "x", style: white }] }] },
683
+ { tag: "div", style: { opacity: "0" }, kids: [{ tag: "p", attrs: { id: "unseen" }, text: "x", style: { color: "rgb(200, 200, 200)", backgroundColor: "rgb(255, 255, 255)" } }] },
684
+ { tag: "p", attrs: { id: "translucent" }, text: "x", style: { color: "rgba(0, 0, 0, 0.3)", backgroundColor: "rgba(255, 0, 0, 0.2)" } },
685
+ ]);
686
+ const found = issuesOf(page.run(), "contrast");
687
+ assert.deepEqual(found.map((issue) => issue.selector), ["#nested", "#translucent"], "black on white faded to 0.5 over black passes (about 5.3 : 1), a gradient behind is unknown, opacity 0 is not drawn");
688
+ assert.equal(found[0].detail, '"x" is 1.83 : 1 (#bfbfbf on #ffffff); WCAG AA needs 4.5 : 1.', "0.5 × 0.5: the group at 0.25");
689
+ assert.equal(found[1].detail, '"x" is 2.03 : 1 (#b38f8f on #ffcccc); WCAG AA needs 4.5 : 1.', "translucent text on a translucent background, no opacity: the same colors as before");
690
+ });
691
+
692
+ test("a target under 24 × 24 px is advice, never a problem: the spacing exception cannot be measured", () => {
693
+ const report = checkPage([{ tag: "button", attrs: { id: "tiny" }, text: "Go", size: [20, 20] }]).run();
694
+ assert.deepEqual(issuesOf(report, "target").map((issue) => [issue.selector, issue.severity, issue.title, issue.detail]), [
695
+ ["#tiny", "advice", "Small touch target", '"Go" is 20 × 20 px; WCAG 2.2 asks for 24 × 24 px, or enough space around it that a 24 px circle on it touches no other target.'],
696
+ ]);
697
+ assert.deepEqual(report.checks.find((check) => check.id === "target"), { id: "target", label: "Target size", count: 0, advice: 1 });
698
+ });
699
+
700
+ test("the target check advises on interactive elements under 24 × 24 px and under 44 px", () => {
701
+ const page = checkPage([
702
+ { tag: "button", attrs: { id: "tiny" }, text: "Go", size: [20, 20] },
703
+ { tag: "button", attrs: { id: "narrow" }, text: "Go", size: [100, 23] },
704
+ { tag: "a", attrs: { id: "advice", href: "/pay" }, text: "Pay", size: [100, 28] },
705
+ { tag: "button", attrs: { id: "edge" }, text: "Edge", size: [24, 24] },
706
+ { tag: "button", attrs: { id: "big" }, text: "Big", size: [120, 48] },
707
+ { tag: "a", attrs: { id: "no-href" }, text: "Not a link", size: [10, 10] },
708
+ { tag: "a", attrs: { id: "inline", href: "/x" }, text: "inline link", size: [60, 14], style: { display: "inline" } },
709
+ { tag: "button", attrs: { id: "disabled", disabled: "" }, text: "Off", size: [10, 10] },
710
+ { tag: "input", attrs: { id: "hidden", type: "hidden" }, size: [0, 0] },
711
+ { tag: "div", attrs: { id: "tab0", tabindex: "0" }, size: [20, 20] },
712
+ { tag: "div", attrs: { id: "tab-1", tabindex: "-1" }, size: [10, 10] },
713
+ { tag: "div", attrs: { id: "role-button", role: "button", "aria-label": "Close" }, size: [12, 12] },
714
+ { tag: "select", attrs: { id: "pick", "aria-label": "Size" }, size: [80, 22] },
715
+ { tag: "button", attrs: { id: "gone" }, text: "Hidden", size: [0, 0] },
716
+ ]);
717
+ const report = page.run();
718
+ assert.deepEqual(issuesOf(report, "target", "fail").map((issue) => issue.selector), []);
719
+ assert.deepEqual(issuesOf(report, "target", "advice").map((issue) => [issue.selector, issue.title]), [
720
+ ["#tiny", "Small touch target"], ["#narrow", "Small touch target"], ["#advice", "Touch target under 44 px"], ["#edge", "Touch target under 44 px"],
721
+ ["#tab0", "Small touch target"], ["#role-button", "Small touch target"], ["#pick", "Small touch target"],
722
+ ]);
723
+ assert.deepEqual([issuesOf(report, "target")[0].title, issuesOf(report, "target")[0].detail], ["Small touch target", '"Go" is 20 × 20 px; WCAG 2.2 asks for 24 × 24 px, or enough space around it that a 24 px circle on it touches no other target.']);
724
+ assert.deepEqual([issuesOf(report, "target", "advice")[2].title, issuesOf(report, "target", "advice")[2].detail], ["Touch target under 44 px", '"Pay" is 100 × 28 px; 44 × 44 px is recommended for touch.']);
725
+ assert.match(issuesOf(report, "target").find((issue) => issue.selector === "#role-button")!.detail, /^"Close" is 12 × 12 px/, "named by its aria-label");
726
+ assert.deepEqual(report.checks.find((check) => check.id === "target"), { id: "target", label: "Target size", count: 0, advice: 7 });
727
+ });
728
+
729
+ test("the alt check flags images without an alt attribute; alt=\"\", presentation and aria-hidden are fine", () => {
730
+ const page = checkPage([
731
+ { tag: "img", attrs: { id: "bad", src: "/img/hero-photo.png?x=1" } },
732
+ { tag: "img", attrs: { id: "empty", src: "a.png", alt: "" } },
733
+ { tag: "img", attrs: { id: "named", src: "b.png", alt: "A cup" } },
734
+ { tag: "img", attrs: { id: "role", src: "c.png", role: "presentation" } },
735
+ { tag: "img", attrs: { id: "none", src: "d.png", role: "none" } },
736
+ { tag: "img", attrs: { id: "aria", src: "e.png", "aria-hidden": "true" } },
737
+ { tag: "img", attrs: { id: "nosrc" } },
738
+ { tag: "input", attrs: { id: "btn", type: "image", src: "f.png", "aria-label": "Go" }, size: [48, 48] },
739
+ { tag: "input", attrs: { id: "btn-ok", type: "image", src: "f.png", alt: "Go" }, size: [48, 48] },
740
+ ]);
741
+ const report = page.run();
742
+ assert.deepEqual(issuesOf(report, "alt").map((issue) => issue.selector), ["#bad", "#nosrc", "#btn"]);
743
+ assert.deepEqual([issuesOf(report, "alt")[0].title, issuesOf(report, "alt")[0].detail], ["Image without alt text", 'hero-photo.png has no alt attribute. Describe it, or use alt="" if it is decorative.']);
744
+ assert.equal(issuesOf(report, "alt")[1].detail, 'The image has no alt attribute. Describe it, or use alt="" if it is decorative.');
745
+ assert.equal(report.checks.find((check) => check.id === "alt")?.count, 3);
746
+ });
747
+
748
+ test("the alt check skips images that are not rendered (inside a display: none parent); a rendered image not loaded yet still needs alt", () => {
749
+ const page = checkPage([
750
+ { tag: "div", style: { display: "none" }, kids: [{ tag: "img", attrs: { id: "in-hidden", src: "menu.png" } }] },
751
+ { tag: "img", attrs: { id: "own-hidden", src: "tab.png" }, style: { display: "none" } },
752
+ { tag: "img", attrs: { id: "loading", src: "late.png" }, size: [0, 0] },
753
+ ]);
754
+ const report = page.run();
755
+ assert.deepEqual(issuesOf(report, "alt").map((issue) => issue.selector), ["#loading"]);
756
+ assert.equal(report.checks.find((check) => check.id === "alt")?.count, 1);
757
+ });
758
+
759
+ test("the label check flags form fields without an accessible label, and accepts each way of labeling", () => {
760
+ const page = checkPage([
761
+ { tag: "input", attrs: { id: "aria", type: "text", "aria-label": "Email" } },
762
+ { tag: "label", attrs: { for: "for" }, text: "Street" },
763
+ { tag: "input", attrs: { id: "for", type: "text" } },
764
+ { tag: "label", text: "City", kids: [{ tag: "input", attrs: { id: "wrapped", type: "text" } }] },
765
+ { tag: "span", attrs: { id: "name" }, text: "Postal code" },
766
+ { tag: "input", attrs: { id: "by", type: "text", "aria-labelledby": "missing name" } },
767
+ { tag: "input", attrs: { id: "title", type: "text", title: "Phone" } },
768
+ { tag: "input", attrs: { id: "placeholder", type: "text", placeholder: "Search" } },
769
+ { tag: "select", attrs: { id: "select" } },
770
+ { tag: "textarea", attrs: { id: "area" } },
771
+ { tag: "input", attrs: { id: "blank", type: "text", "aria-label": " " } },
772
+ { tag: "label", attrs: { for: "empty-label" }, text: " " },
773
+ { tag: "input", attrs: { id: "empty-label", type: "text" } },
774
+ { tag: "input", attrs: { id: "dangling", type: "text", "aria-labelledby": "nowhere" } },
775
+ { tag: "input", attrs: { id: "submit", type: "submit", value: "Pay" } },
776
+ { tag: "input", attrs: { id: "button", type: "button" } },
777
+ { tag: "input", attrs: { id: "reset", type: "reset" } },
778
+ { tag: "input", attrs: { id: "hidden", type: "hidden" } },
779
+ { tag: "input", attrs: { id: "invisible", type: "text" }, style: { display: "none" } },
780
+ { tag: "input", attrs: { id: "no-type" } },
781
+ { tag: "input", attrs: { id: "email", type: "email" } },
782
+ ]);
783
+ const report = page.run();
784
+ assert.deepEqual(issuesOf(report, "label").map((issue) => issue.selector), ["#placeholder", "#select", "#area", "#blank", "#empty-label", "#dangling", "#no-type", "#email"]);
785
+ assert.deepEqual([issuesOf(report, "label")[0].title, issuesOf(report, "label")[0].detail], ["Form field without a label", 'The text field "Search" has no label. Add a <label for>, aria-label or aria-labelledby; a placeholder is not a label.']);
786
+ assert.equal(issuesOf(report, "label")[1].detail, "The select has no label. Add a <label for>, aria-label or aria-labelledby; a placeholder is not a label.");
787
+ assert.equal(issuesOf(report, "label")[2].detail, "The text area has no label. Add a <label for>, aria-label or aria-labelledby; a placeholder is not a label.");
788
+ assert.equal(issuesOf(report, "label")[7].detail, "The email field has no label. Add a <label for>, aria-label or aria-labelledby; a placeholder is not a label.");
789
+ assert.equal(report.checks.find((check) => check.id === "label")?.count, 8);
790
+ });
791
+
792
+ test("the heading check flags levels that skip, in document order", () => {
793
+ const page = checkPage([
794
+ { tag: "h1", text: "Title" }, { tag: "h2", text: "A" },
795
+ { tag: "h4", attrs: { id: "skip1" }, text: "Detail" },
796
+ { tag: "h2", text: "B" }, { tag: "h3", text: "B1" }, { tag: "h3", text: "B2" },
797
+ { tag: "h6", attrs: { id: "skip2" }, text: "Fine print" },
798
+ { tag: "h1", text: "Again" }, { tag: "h2", text: "Ok" },
799
+ { tag: "div", attrs: { id: "aria", role: "heading", "aria-level": "4" }, text: "ARIA heading" },
800
+ { tag: "h5", attrs: { id: "hidden" }, text: "Hidden", style: { display: "none" } },
801
+ ]);
802
+ const report = page.run();
803
+ assert.deepEqual(issuesOf(report, "heading").map((issue) => issue.selector), ["#skip1", "#skip2", "#aria"]);
804
+ assert.deepEqual([issuesOf(report, "heading")[0].title, issuesOf(report, "heading")[0].detail], ["Heading level skipped", 'h4 "Detail" follows h2: h3 is missing.']);
805
+ assert.equal(issuesOf(report, "heading")[1].detail, 'h6 "Fine print" follows h3: h4, h5 are missing.');
806
+ const clean = checkPage([{ tag: "h2", text: "Starts at two" }, { tag: "h3", text: "x" }, { tag: "h2", text: "y" }]);
807
+ assert.deepEqual(clean.run().issues, [], "the first heading may be any level; going back up is fine");
808
+ });
809
+
810
+ /** canvas-checks.js's English TEXT block, evaluated from its source: the canvas's own English for each finding key. */
811
+ function canvasFindings(): Record<string, { title: string; detail: (vars: Record<string, unknown>) => string }> {
812
+ const source = read("canvas-checks.js");
813
+ const start = source.indexOf("const TEXT = Object.freeze({");
814
+ return new Function(`return ${source.slice(start + "const TEXT = ".length, source.indexOf("\n});", start) + 3)}`)().findings;
815
+ }
816
+
817
+ test("each finding carries its key and plain vars, from which the canvas's English gives back the inspector's title and detail", () => {
818
+ const page = checkPage([
819
+ { tag: "p", attrs: { id: "low" }, text: "or pay with card", style: gray(119) },
820
+ { tag: "p", attrs: { id: "large-low" }, text: "Big", style: { ...gray(170), fontSize: "24px" } },
821
+ { tag: "button", attrs: { id: "tiny" }, text: "Go", size: [20, 20] },
822
+ { tag: "div", attrs: { id: "tab0", tabindex: "0" }, size: [20, 20] },
823
+ { tag: "a", attrs: { id: "advice", href: "/pay" }, text: "Pay", size: [100, 28] },
824
+ { tag: "img", attrs: { id: "bad", src: "/img/hero-photo.png?x=1" } },
825
+ { tag: "img", attrs: { id: "nosrc" } },
826
+ { tag: "input", attrs: { id: "placeholder", type: "text", placeholder: "Search" } },
827
+ { tag: "select", attrs: { id: "select" } },
828
+ { tag: "textarea", attrs: { id: "area" } },
829
+ { tag: "h1", text: "Title" }, { tag: "h2", text: "A" }, { tag: "h4", attrs: { id: "skip1" }, text: "Detail" },
830
+ { tag: "h3", text: "B1" }, { tag: "h6", attrs: { id: "skip2" }, text: "Fine print" },
831
+ ]);
832
+ const findings = canvasFindings();
833
+ const issues = page.run().issues;
834
+ assert.deepEqual([...new Set(issues.map((issue) => issue.key))].sort(), ["alt", "contrast", "contrastLarge", "heading", "label", "targetAdvised", "targetSmall"]);
835
+ assert.deepEqual(Object.keys(findings).sort(), ["alt", "contrast", "contrastLarge", "heading", "label", "targetAdvised", "targetSmall"]);
836
+ for (const issue of issues) {
837
+ assert.ok(Object.values(issue.vars).every((value) => typeof value === "string" || Number.isFinite(value)), `${issue.selector}: plain vars`);
838
+ assert.deepEqual([findings[issue.key].title, findings[issue.key].detail(issue.vars)], [issue.title, issue.detail], issue.selector);
839
+ }
840
+ assert.deepEqual(issues.filter((issue) => issue.check !== "contrast").map((issue) => [issue.selector, issue.key, JSON.stringify(issue.vars)]), [
841
+ ["#tiny", "targetSmall", '{"name":"Go","element":"button#tiny","width":20,"height":20,"min":24}'],
842
+ ["#tab0", "targetSmall", '{"name":"","element":"div#tab0","width":20,"height":20,"min":24}'],
843
+ ["#advice", "targetAdvised", '{"name":"Pay","element":"a#advice","width":100,"height":28,"advised":44}'],
844
+ ["#placeholder", "targetAdvised", '{"name":"","element":"input#placeholder","width":100,"height":24,"advised":44}'],
845
+ ["#select", "targetAdvised", '{"name":"","element":"select#select","width":100,"height":24,"advised":44}'],
846
+ ["#area", "targetAdvised", '{"name":"","element":"textarea#area","width":100,"height":24,"advised":44}'],
847
+ ["#bad", "alt", '{"source":"hero-photo.png"}'],
848
+ ["#nosrc", "alt", '{"source":""}'],
849
+ ["#placeholder", "label", '{"field":"input","type":"text","hint":"Search"}'],
850
+ ["#select", "label", '{"field":"select","type":"","hint":""}'],
851
+ ["#area", "label", '{"field":"textarea","type":"","hint":""}'],
852
+ ["#skip1", "heading", '{"level":4,"previous":2,"text":"Detail","missing":"h3","count":1}'],
853
+ ["#skip2", "heading", '{"level":6,"previous":3,"text":"Fine print","missing":"h4, h5","count":2}'],
854
+ ]);
855
+ });
856
+
857
+ test("each check lists at most 20 problems and 10 advice items, with the full counts", () => {
858
+ const page = checkPage(Array.from({ length: 45 }, (_, index) => ({ tag: "p", attrs: { id: `p${index}` }, text: `text ${index}`, style: gray(200) })));
859
+ const report = page.run();
860
+ assert.deepEqual([issuesOf(report, "contrast").length, report.checks.find((check) => check.id === "contrast")!.count], [20, 45]);
861
+ assert.deepEqual(issuesOf(report, "contrast").map((issue) => issue.selector).slice(0, 2), ["#p0", "#p1"], "the first ones in document order");
862
+ const advice = checkPage(Array.from({ length: 30 }, (_, index) => ({ tag: "button", attrs: { id: `b${index}` }, text: "x", size: [30, 30] as [number, number] }))).run();
863
+ assert.deepEqual([issuesOf(advice, "target", "advice").length, advice.checks.find((check) => check.id === "target")!.advice], [10, 30]);
864
+ });
865
+
866
+ test("a finding whose element is out of sight says hidden: true; below the fold is not out of sight", () => {
867
+ const page = checkPage([
868
+ { tag: "a", attrs: { id: "skip", href: "#main" }, text: "Skip to content", size: [120, 20], at: [-500, 8] },
869
+ { tag: "button", attrs: { id: "clipped" }, text: "x", size: [1, 1] },
870
+ { tag: "button", attrs: { id: "rect" }, text: "y", size: [10, 10], style: { clip: "rect(0px, 0px, 0px, 0px)" } },
871
+ { tag: "div", attrs: { id: "box" }, size: [100, 100], at: [0, 0], style: { overflow: "hidden", overflowX: "hidden", overflowY: "hidden" }, kids: [{ tag: "button", attrs: { id: "cut" }, text: "z", size: [10, 10], at: [300, 0] }] },
872
+ { tag: "button", attrs: { id: "fold" }, text: "w", size: [10, 10], at: [8, 5000] },
873
+ { tag: "button", attrs: { id: "right" }, text: "v", size: [10, 10], at: [1300, 8] },
874
+ ]);
875
+ const report = page.run();
876
+ const hidden = Object.fromEntries(issuesOf(report, "target").map((issue) => [issue.selector, issue.hidden === true]));
877
+ assert.deepEqual(hidden, { "#skip": true, "#clipped": true, "#rect": true, "#cut": true, "#fold": false, "#right": true });
878
+ assert.equal(Object.hasOwn(issuesOf(report, "target").find((issue) => issue.selector === "#fold")!, "hidden"), false, "a visible finding has no hidden field");
879
+ });
880
+
881
+ /** Comment mode with Technical details on and the button hovered and measured; then the button grows without a scroll. */
882
+ function measuredThenResized({ fonts = false } = {}) {
883
+ const inspector = loadInspector({ fonts });
884
+ inspector.button.computed = BUTTON_STYLE;
885
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
886
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
887
+ assert.deepEqual(inspects(inspector), [{ type: "gd:inspect", element: BUTTON_MEASURE, generation: 0 }]);
888
+ inspector.button.getBoundingClientRect = () => ({ left: 4, top: 8, width: 120, height: 48 });
889
+ inspector.button.computed = { ...BUTTON_STYLE, fontSize: "18px" };
890
+ return inspector;
891
+ }
892
+ const RESIZED = { type: "gd:inspect", element: { ...BUTTON_MEASURE, rect: { x: 4, y: 8, width: 120, height: 48 }, fontSize: 18, radius: [6, 6, 24, 6] }, generation: 0 };
893
+
894
+ test("a late load on the page re-measures the hovered element once per frame", () => {
895
+ const inspector = measuredThenResized();
896
+ inspector.captureOnDocument("load", inspector.makeNode());
897
+ inspector.captureOnDocument("load", inspector.makeNode());
898
+ assert.equal(inspects(inspector).length, 1, "reports wait for the next frame");
899
+ inspector.runFrames();
900
+ assert.deepEqual(inspects(inspector).slice(1), [RESIZED], "two loads in one frame, one report with the new size");
901
+ });
902
+
903
+ test("a late web font re-measures the hovered element once per frame", () => {
904
+ const inspector = measuredThenResized({ fonts: true });
905
+ inspector.fontsLoaded();
906
+ inspector.fontsLoaded();
907
+ assert.equal(inspects(inspector).length, 1, "reports wait for the next frame");
908
+ inspector.runFrames();
909
+ assert.deepEqual(inspects(inspector).slice(1), [RESIZED], "two font loads in one frame, one report with the new size");
910
+ });
911
+
912
+ test("a page change re-measures the hovered element once per frame; the inspector's own boxes do not", () => {
913
+ const inspector = measuredThenResized();
914
+ const ownBox = Object.assign(inspector.makeNode(), { hasAttribute: () => true });
915
+ inspector.mutate([{ type: "attributes", target: ownBox }]);
916
+ inspector.runFrames();
917
+ assert.equal(inspects(inspector).length, 1, "the inspector's own boxes moving is no page change");
918
+ inspector.mutate([{ type: "childList", target: inspector.makeNode() }]);
919
+ inspector.mutate([{ type: "attributes", target: inspector.makeNode() }]);
920
+ assert.equal(inspects(inspector).length, 1, "reports wait for the next frame");
921
+ inspector.runFrames();
922
+ assert.deepEqual(inspects(inspector).slice(1), [RESIZED], "two changes in one frame, one report with the new size");
923
+ });
924
+
925
+ test("the measure reports the page's root font size, so rem tokens are compared in this page's px; 16 when unreadable", () => {
926
+ const inspector = loadInspector();
927
+ inspector.button.computed = BUTTON_STYLE;
928
+ const root = inspector.documentElement as unknown as Record<string, unknown>;
929
+ root.computed = { fontSize: "10px" };
930
+ inspector.fromParent({ type: "gd:mode", enabled: true, inspect: true });
931
+ inspector.fromParent({ type: "gd:hover-at", x: 10, y: 12 });
932
+ assert.equal(inspector.posted.at(-1).element.rootFontSize, 10);
933
+ root.computed = { fontSize: "" };
934
+ inspector.fire("scroll", {});
935
+ inspector.runFrames();
936
+ assert.equal(inspector.posted.at(-1).element.rootFontSize, 16, "no readable root font size: the browser default");
937
+ });