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,965 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFileSync } from "node:fs";
3
+ import test from "node:test";
4
+ import { fileURLToPath } from "node:url";
5
+ import { findAll, flush, heardGeneration, inspectorPick, loadCanvas, memoryStorage, read, texts, withClass, type FakeElement } from "./support/design-canvas-dom.ts";
6
+
7
+ // The token inspector (canvas-tokens.js): with Technical details on (a checkbox in the rail's Settings,
8
+ // off by default, saved in this browser), Comment mode shows a panel beside the frame of the hovered
9
+ // element (inspector.js gd:inspect) with its computed values, the design/system.md token each one
10
+ // matches or "⚠ no token" with the nearest, the WCAG contrast, Ask the agent to fix and Copy. Covers
11
+ // every reload, stale-identity and exit path the maintainer probes (S7). Every assertion compares
12
+ // strings, numbers or booleans, never fake elements, which node:assert would walk whole on a failure.
13
+
14
+ const TOKENS_URL = "/t/api/system/tokens";
15
+ const TECHNICAL_KEY = "gentle-design:technical";
16
+ const HIDE_MS = 300;
17
+ // While a panel shows, the pointer must rest this long on another element before the panel follows it.
18
+ const DWELL_MS = 250;
19
+
20
+ const TOKEN_SET = { exists: true, tokens: [
21
+ { name: "--bg", value: "#1a1218", section: "Colors", group: "color" },
22
+ { name: "--surface-2", value: "#342230", section: "Colors", group: "color" },
23
+ { name: "--border", value: "#342230", section: "Colors", group: "color" },
24
+ { name: "--text", value: "#f6eff3", section: "Colors", group: "color" },
25
+ { name: "--text-muted", value: "#a78e9b", section: "Colors", group: "color" },
26
+ { name: "--text-faint", value: "#76616b", section: "Colors", group: "color" },
27
+ { name: "--paper", value: "#ffffff", section: "Colors", group: "color" },
28
+ { name: "--font-sans", value: 'Inter, "SF Pro Text", system-ui, sans-serif', section: "Typography", group: "font" },
29
+ { name: "--radius-sm", value: "8px", section: "Radius", group: "radius" },
30
+ { name: "--radius-md", value: "12px", section: "Radius", group: "radius" },
31
+ { name: "--rail-width", value: "60px", section: "Layout", group: "layout" },
32
+ ] };
33
+ const EXTRA_TOKENS = { exists: true, tokens: [...TOKEN_SET.tokens,
34
+ { name: "--space-2", value: "8px", section: "Spacing", group: "spacing" },
35
+ { name: "--space-4", value: "16px", section: "Spacing", group: "spacing" },
36
+ { name: "--text-sm", value: "12px", section: "Typography", group: "font" },
37
+ { name: "--text-md", value: "14px", section: "Typography", group: "font" },
38
+ ] };
39
+
40
+ /** What inspector.js reports for one element (gd:inspect): computed values, lengths in CSS px. */
41
+ const MEASURE = {
42
+ selector: "main > p.or", label: "p.or", text: "or pay with card", hasText: true,
43
+ rect: { x: 30, y: 40, width: 200, height: 20 },
44
+ color: { r: 118, g: 97, b: 107, a: 1 }, background: { r: 255, g: 255, b: 255 },
45
+ fontFamily: 'Inter, "SF Pro Text", system-ui, sans-serif', fontSize: 14, fontWeight: 400, lineHeight: 21,
46
+ margin: [8, 0, 8, 0], padding: [0, 0, 0, 0], radius: [0, 0, 0, 0],
47
+ };
48
+ // Off the system: #96828c is no token and 3.6 : 1 on white; a 10 px radius sits between the radius tokens.
49
+ const OFF = { ...MEASURE, color: { r: 150, g: 130, b: 140, a: 1 }, radius: [10, 10, 10, 10] };
50
+ const MATCHING = { ...MEASURE, color: { r: 246, g: 239, b: 243, a: 1 }, background: { r: 26, g: 18, b: 24 } };
51
+
52
+ const WIDE_ALPHA = { "/t/api/designs/alpha": { slug: "alpha", title: "Alpha", brief: "", artboards: [
53
+ { file: "01-a.html", title: "First", tradeoff: "", width: 1200, height: 800 },
54
+ { file: "02-b.html", title: "Second", tradeoff: "", width: 400, height: 300 },
55
+ ] } };
56
+ const SYSTEM_REPLY = { "/t/api/system": { exists: true, file: "system.html", width: 800, height: 600 } };
57
+
58
+ type Options = { replies?: Record<string, unknown>; tokens?: unknown; stored?: string; broken?: boolean; extraWindow?: Record<string, unknown>; timers?: boolean };
59
+
60
+ /** The canvas with an in-memory localStorage; `timers` keeps the canvas's timeouts until the test runs them. */
61
+ async function loadTokens(options: Options = {}) {
62
+ const storage = memoryStorage(options.stored === undefined ? {} : { [TECHNICAL_KEY]: options.stored }, options.broken);
63
+ // Every timer the canvas set, in order; its id is its index + 1, and `done` once it ran or was cleared.
64
+ const timers: Array<{ callback: () => void; ms: number; done: boolean }> = [];
65
+ const clock = options.timers ? {
66
+ setTimeout: (callback: () => void, ms = 0) => timers.push({ callback, ms, done: false }),
67
+ clearTimeout: (id: number) => { if (timers[id - 1]) timers[id - 1].done = true; },
68
+ } : {};
69
+ const replies = { ...(options.tokens === undefined ? { [TOKENS_URL]: TOKEN_SET } : options.tokens === null ? {} : { [TOKENS_URL]: options.tokens }), ...options.replies };
70
+ const canvas = await loadCanvas(300, replies, {}, { localStorage: storage.localStorage, ...clock, ...options.extraWindow });
71
+ // The fake DOM starts every element visible; index.html starts these hidden.
72
+ for (const id of ["gd-popover", "gd-menu", "gd-device-menu", "gd-inspector"]) canvas.document.getElementById(id).hidden = true;
73
+ /** Runs the pending timers of `ms` (every pending one without it), as if that time passed; others stay pending. */
74
+ const run = (ms?: number) => {
75
+ for (const timer of timers.slice()) {
76
+ if (timer.done || (ms !== undefined && timer.ms !== ms)) continue;
77
+ timer.done = true;
78
+ timer.callback();
79
+ }
80
+ };
81
+ /** The panel's pending hide timers (the 300 ms ones). */
82
+ const runHide = () => run(HIDE_MS);
83
+ /** The panel's pending switch to another element (the 250 ms rest). */
84
+ const runDwell = () => run(DWELL_MS);
85
+ return { ...canvas, storage, run, runHide, runDwell, timers };
86
+ }
87
+ type Canvas = Awaited<ReturnType<typeof loadTokens>>;
88
+
89
+ const $ = (canvas: Canvas, id: string) => canvas.document.getElementById(id);
90
+ const settle = async () => { for (let i = 0; i < 10; i++) await flush(); };
91
+ function deferred() {
92
+ let answer!: (value: unknown) => void;
93
+ const promise = new Promise((resolve) => { answer = resolve; });
94
+ return { promise, answer };
95
+ }
96
+
97
+ const menuEl = (canvas: Canvas) => $(canvas, "gd-menu");
98
+ const settingsItems = (canvas: Canvas) => findAll(menuEl(canvas), (element) => (element.getAttribute("role") ?? "").startsWith("menuitem"));
99
+ const technicalItem = (canvas: Canvas) => settingsItems(canvas).find((item) => texts(item, "gd-menu-label")[0] === "Technical details");
100
+ /** Technical details as Settings shows it: "true" or "false" (aria-checked), the menu closed again. */
101
+ function technicalShown(canvas: Canvas) {
102
+ $(canvas, "gd-rail-settings").dispatch("click");
103
+ const checked = technicalItem(canvas)?.getAttribute("aria-checked") ?? "missing";
104
+ $(canvas, "gd-rail-settings").dispatch("click");
105
+ return checked;
106
+ }
107
+ function toggleTechnical(canvas: Canvas) {
108
+ $(canvas, "gd-rail-settings").dispatch("click");
109
+ technicalItem(canvas)!.dispatch("click");
110
+ }
111
+ const commentMode = (canvas: Canvas) => $(canvas, "gd-comment").dispatch("click");
112
+ /** Technical details on and Comment mode on, the way a person turns them on. */
113
+ async function inspecting(options: Options = {}) {
114
+ const canvas = await loadTokens(options);
115
+ canvas.viewport.rect = { left: 0, top: 50, width: 1000, height: 1400 };
116
+ toggleTechnical(canvas);
117
+ commentMode(canvas);
118
+ return canvas;
119
+ }
120
+ /** A gd:inspect as inspector.js posts it from `frame`'s page now: tagged with the size generation that page heard last. */
121
+ function inspect(canvas: Canvas, frame: FakeElement, element: unknown = MEASURE, generation = heardGeneration(frame)) {
122
+ canvas.message(frame.contentWindow, { type: "gd:inspect", element, generation });
123
+ }
124
+
125
+ const panelOf = (canvas: Canvas) => $(canvas, "gd-inspector");
126
+ const shown = (canvas: Canvas) => !panelOf(canvas).hidden;
127
+ function allText(element: FakeElement): string {
128
+ return [element.textContent, ...element.children.map(allText)].filter(Boolean).join(" ");
129
+ }
130
+ const rows = (canvas: Canvas) => withClass($(canvas, "gd-inspector-list"), "gd-inspector-row");
131
+ const rowOf = (canvas: Canvas, label: string) => rows(canvas).find((row) => texts(row, "gd-inspector-label")[0] === label);
132
+ const rowText = (canvas: Canvas, label: string) => { const row = rowOf(canvas, label); return row ? allText(withClass(row, "gd-inspector-value")[0]) : ""; };
133
+ const flagged = (canvas: Canvas, label: string) => withClass(rowOf(canvas, label)!, "gd-inspector-bad").length;
134
+
135
+ const focusOn = (canvas: Canvas, index = 0) => canvas.boards[index].shield!.dispatch("keydown", { key: "Enter" });
136
+ const chooseSize = (canvas: Canvas, size: string) => {
137
+ if ($(canvas, "gd-device-menu").hidden) $(canvas, "gd-device").dispatch("click");
138
+ findAll($(canvas, "gd-device-menu"), (element) => element.dataset.size === size)[0].dispatch("click");
139
+ };
140
+ const sizeFrames = (canvas: Canvas) => withClass(canvas.viewport, "gd-size-frame").map((root) => findAll(root, (element) => element.localName === "iframe")[0]);
141
+
142
+ // ---- tokens: load and reload (S8)
143
+
144
+ test("the canvas reads the tokens at start and again when design/system.md changes, and only then", async () => {
145
+ const canvas = await loadTokens();
146
+ const reads = () => canvas.fetched.filter((url) => url === TOKENS_URL).length;
147
+ assert.equal(reads(), 1, "once at start");
148
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
149
+ assert.equal(reads(), 2, "again when system.md changes");
150
+ await canvas.serverEvent("change", { slug: null, file: "system.html" });
151
+ await canvas.serverEvent("change", { slug: "alpha", file: "system.md" });
152
+ assert.equal(reads(), 2, "system.html is the board, and only the root system.md holds the tokens");
153
+ });
154
+
155
+ test("a missing, failing or malformed tokens reply never touches the status line", async () => {
156
+ for (const reply of [null, { __http: 500, error: "boom" }, { __throw: "down" }, { exists: true, tokens: "nope" }, "text", { exists: true, tokens: [{ name: 3 }, null, { name: "--a", value: "#fff" }] }]) {
157
+ const canvas = await loadTokens({ tokens: reply });
158
+ assert.equal($(canvas, "gd-status").textContent, "", JSON.stringify(reply));
159
+ assert.equal(canvas.frames().length, 2, "the canvas still shows the design");
160
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
161
+ assert.equal($(canvas, "gd-status").textContent, "", JSON.stringify(reply));
162
+ assert.equal($(canvas, "gd-status").classes.has("gd-error"), false);
163
+ }
164
+ });
165
+
166
+ test("a system.md change while a tokens read is in flight: the newest read wins, and a failed read keeps the last tokens", async () => {
167
+ const canvas = await inspecting();
168
+ const [first] = canvas.boards;
169
+ const ink = (name: string) => ({ exists: true, tokens: [{ name, value: "#010203", section: "Colors", group: "color" }] });
170
+ const late = deferred();
171
+ canvas.replies[TOKENS_URL] = late.promise;
172
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
173
+ canvas.replies[TOKENS_URL] = ink("--ink-new");
174
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
175
+ late.answer(ink("--ink-old"));
176
+ await settle();
177
+ inspect(canvas, first.frame, { ...MEASURE, color: { r: 1, g: 2, b: 3, a: 1 } });
178
+ assert.equal(rowText(canvas, "Color"), "#010203 ✓ --ink-new", "the older reply, landing last, changes nothing");
179
+ for (const reply of [{ __http: 500 }, { __throw: "down" }, { exists: true, tokens: "nope" }]) {
180
+ canvas.replies[TOKENS_URL] = reply;
181
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
182
+ inspect(canvas, first.frame, { ...MEASURE, color: { r: 1, g: 2, b: 3, a: 1 } });
183
+ assert.equal(rowText(canvas, "Color"), "#010203 ✓ --ink-new", `a failed read keeps the tokens: ${JSON.stringify(reply)}`);
184
+ }
185
+ // A system.md that is gone (exists: false) has no tokens.
186
+ canvas.replies[TOKENS_URL] = { exists: false, tokens: [] };
187
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
188
+ inspect(canvas, first.frame, { ...MEASURE, color: { r: 1, g: 2, b: 3, a: 1 } });
189
+ assert.equal(rowText(canvas, "Color"), "#010203");
190
+ assert.equal($(canvas, "gd-inspector-note").hidden, false);
191
+ });
192
+
193
+ test("only well-formed tokens count: a custom property name, a value, at most 500", async () => {
194
+ const many = Array.from({ length: 501 }, (_, index) => ({ name: `--c${index}`, value: index === 500 ? "#010203" : "#ffffff", section: "Colors", group: "color" }));
195
+ const bad = [{ name: "ink", value: "#010203" }, { name: "--ink", value: "" }, { name: "--ink;x", value: "#010203" }, { name: "--ink", value: 3 }];
196
+ for (const tokens of [many, bad]) {
197
+ const canvas = await inspecting({ tokens: { exists: true, tokens } });
198
+ inspect(canvas, canvas.boards[0].frame, { ...MEASURE, color: { r: 1, g: 2, b: 3, a: 1 } });
199
+ assert.ok(!/✓/.test(rowText(canvas, "Color")), JSON.stringify(tokens.slice(-1)));
200
+ }
201
+ });
202
+
203
+ // ---- Technical details in Settings (S8)
204
+
205
+ test("Technical details is a checkbox item in Settings, off by default and remembered in this browser", async () => {
206
+ const canvas = await loadTokens();
207
+ $(canvas, "gd-rail-settings").dispatch("click");
208
+ assert.deepEqual(settingsItems(canvas).map((item) => texts(item, "gd-menu-label")[0]), ["Design system board", "Keyboard shortcuts", "Technical details", "Español", "English", "Match the browser"]);
209
+ const item = technicalItem(canvas)!;
210
+ assert.equal(item.getAttribute("role"), "menuitemcheckbox");
211
+ assert.equal(item.getAttribute("aria-checked"), "false");
212
+ assert.equal(withClass(item, "gd-menu-check").length, 1, "a check mark, shown while it is on");
213
+ assert.equal(findAll(item, (element) => element.localName === "use")[0].getAttribute("href"), "#i-code");
214
+ item.dispatch("click");
215
+ assert.equal(menuEl(canvas).hidden, true, "choosing it closes the menu");
216
+ assert.equal(canvas.document.activeElement?.id, "gd-rail-settings");
217
+ assert.equal(technicalShown(canvas), "true");
218
+ const writes = () => canvas.storage.writes.filter(([key]) => key === TECHNICAL_KEY).map(([, value]) => value);
219
+ assert.deepEqual(writes(), ["on"]);
220
+ toggleTechnical(canvas);
221
+ assert.equal(technicalShown(canvas), "false");
222
+ assert.deepEqual(writes(), ["on", "off"]);
223
+
224
+ for (const [stored, checked] of [["on", "true"], ["off", "false"], ["yes", "false"], ["", "false"]]) {
225
+ assert.equal(technicalShown(await loadTokens({ stored })), checked, `stored "${stored}"`);
226
+ }
227
+ });
228
+
229
+ test("storage that throws starts Technical details off and still lets it be turned on for the page", async () => {
230
+ const canvas = await loadTokens({ broken: true });
231
+ assert.equal(technicalShown(canvas), "false");
232
+ toggleTechnical(canvas);
233
+ assert.equal(technicalShown(canvas), "true");
234
+ commentMode(canvas);
235
+ inspect(canvas, canvas.boards[0].frame);
236
+ assert.equal(shown(canvas), true);
237
+ assert.equal($(canvas, "gd-status").classes.has("gd-error"), false);
238
+ });
239
+
240
+ // ---- when the panel shows (S8)
241
+
242
+ test("the panel shows only with Technical details on, in Comment mode, the comment box closed, from a trusted design frame", async () => {
243
+ const canvas = await loadTokens();
244
+ const [first] = canvas.boards;
245
+ commentMode(canvas);
246
+ inspect(canvas, first.frame);
247
+ assert.equal(shown(canvas), false, "Technical details is off by default");
248
+ toggleTechnical(canvas);
249
+ $(canvas, "gd-tool-navigate").dispatch("click");
250
+ inspect(canvas, first.frame);
251
+ assert.equal(shown(canvas), false, "not in Move mode");
252
+ commentMode(canvas);
253
+ canvas.message({}, { type: "gd:inspect", element: MEASURE });
254
+ assert.equal(shown(canvas), false, "a stranger is not trusted");
255
+ for (const bad of [undefined, 3, "x", {}, { ...MEASURE, rect: null }, { ...MEASURE, margin: [1] }, { ...MEASURE, selector: "" }, { ...MEASURE, selector: "x".repeat(501) }, { ...MEASURE, fontSize: "big" }, { ...MEASURE, color: { r: 999, g: 0, b: 0, a: 1 } }, { ...MEASURE, background: { r: 1, g: 2 } }, { ...MEASURE, lineHeight: "21px" }]) {
256
+ canvas.message(first.frame.contentWindow, { type: "gd:inspect", element: bad });
257
+ assert.equal(shown(canvas), false, JSON.stringify(bad));
258
+ }
259
+ inspect(canvas, first.frame);
260
+ assert.equal(shown(canvas), true);
261
+ // The comment box takes the panel's place, and no report reopens the panel while the box is open.
262
+ canvas.message(first.frame.contentWindow, inspectorPick(first.frame, { type: "gd:pick", selector: "main > p.or", excerpt: "", rect: { x: 1, y: 1, width: 2, height: 2 } }));
263
+ assert.equal($(canvas, "gd-popover").hidden, false);
264
+ assert.equal(shown(canvas), false, "the comment box opening hides the panel");
265
+ inspect(canvas, first.frame);
266
+ assert.equal(shown(canvas), false, "not while the comment box is open");
267
+ $(canvas, "gd-popover-cancel").dispatch("click");
268
+ inspect(canvas, first.frame);
269
+ assert.equal(shown(canvas), true);
270
+ });
271
+
272
+ test("the design system board never shows the panel", async () => {
273
+ const canvas = await inspecting({ replies: SYSTEM_REPLY });
274
+ const [system, first] = canvas.frames();
275
+ inspect(canvas, system);
276
+ assert.equal(shown(canvas), false);
277
+ inspect(canvas, first);
278
+ assert.equal(shown(canvas), true);
279
+ });
280
+
281
+ test("in focus mode only the focused artboard and its All sizes frames show the panel; another artboard's report is ignored", async () => {
282
+ const canvas = await inspecting({ replies: WIDE_ALPHA });
283
+ const [first, second] = canvas.boards;
284
+ focusOn(canvas, 0);
285
+ inspect(canvas, second.frame);
286
+ assert.equal(shown(canvas), false, "another artboard than the focused one");
287
+ inspect(canvas, first.frame);
288
+ assert.equal(shown(canvas), true);
289
+ chooseSize(canvas, "all");
290
+ assert.equal(shown(canvas), false, "a new size hides it: the frame it was beside changed");
291
+ const [phone] = sizeFrames(canvas);
292
+ phone.rect = { left: 100, top: 200, width: 195, height: 422 };
293
+ inspect(canvas, second.frame);
294
+ assert.equal(shown(canvas), false, "still not another artboard's frame");
295
+ inspect(canvas, phone, { ...MEASURE, rect: { x: 40, y: 100, width: 100, height: 40 } });
296
+ assert.equal(shown(canvas), true, "a size frame of the focused artboard");
297
+ // Placed against the scaled 390 px frame (195 px on screen): right of it, level with the element.
298
+ assert.equal(panelOf(canvas).style.left, `${295 + 12}px`);
299
+ assert.equal(panelOf(canvas).style.top, `${200 + 100 * 0.5 - 50}px`);
300
+ });
301
+
302
+ test("the panel sits beside the frame, not over the element: right, else left, else below it inside the viewport", async () => {
303
+ const canvas = await inspecting();
304
+ const [first] = canvas.boards;
305
+ const place = () => [Number.parseFloat(panelOf(canvas).style.left), Number.parseFloat(panelOf(canvas).style.top) + 50];
306
+ first.frame.rect = { left: 100, top: 120, width: 400, height: 300 };
307
+ inspect(canvas, first.frame);
308
+ assert.deepEqual(place(), [512, 160], "right of the frame, level with the element");
309
+ first.frame.rect = { left: 560, top: 120, width: 400, height: 300 };
310
+ inspect(canvas, first.frame);
311
+ assert.deepEqual(place(), [560 - 12 - 280, 160], "no room on the right: left of the frame");
312
+ canvas.viewport.rect = { left: 0, top: 50, width: 420, height: 1400 };
313
+ first.frame.rect = { left: 10, top: 120, width: 400, height: 300 };
314
+ inspect(canvas, first.frame);
315
+ assert.deepEqual(place(), [40, 188], "no room on either side: below the element, inside the viewport");
316
+ });
317
+
318
+ // ---- what the panel says (S8)
319
+
320
+ test("the panel names the element, its values and the token each one matches", async () => {
321
+ const canvas = await inspecting();
322
+ const [first] = canvas.boards;
323
+ inspect(canvas, first.frame, { ...MATCHING, radius: [8, 8, 8, 8] });
324
+ assert.equal($(canvas, "gd-inspector-title").textContent, "p.or");
325
+ assert.equal($(canvas, "gd-inspector-selector").textContent, "main > p.or");
326
+ assert.deepEqual(rows(canvas).map((row) => texts(row, "gd-inspector-label")[0]), ["Color", "Background", "Contrast", "Font", "Size", "Line height", "Spacing", "Radius"]);
327
+ assert.equal(rowText(canvas, "Color"), "#f6eff3 ✓ --text");
328
+ assert.equal(rowText(canvas, "Background"), "#1a1218 ✓ --bg");
329
+ assert.match(rowText(canvas, "Contrast"), /^✓ \d+(\.\d+)? : 1 AA \(4\.5 : 1\)$/);
330
+ assert.equal(rowText(canvas, "Font"), "Inter ✓ --font-sans");
331
+ assert.equal(rowText(canvas, "Size"), "14 px weight 400", "no font size tokens: the value alone");
332
+ assert.equal(rowText(canvas, "Line height"), "21 px");
333
+ assert.equal(rowText(canvas, "Spacing"), "margin 8 0 8 0", "no spacing tokens: the value alone");
334
+ assert.equal(rowText(canvas, "Radius"), "8 px ✓ --radius-sm");
335
+ assert.equal(withClass($(canvas, "gd-inspector-list"), "gd-inspector-bad").length, 0, "nothing flagged");
336
+ assert.deepEqual(withClass($(canvas, "gd-inspector-list"), "gd-inspector-swatch").map((swatch) => swatch.style.background), ["rgb(246, 239, 243)", "rgb(26, 18, 24)"]);
337
+ assert.equal($(canvas, "gd-inspector-note").hidden, true, "there are tokens");
338
+ // Two tokens with one value: both named.
339
+ inspect(canvas, first.frame, { ...MEASURE, color: { r: 52, g: 34, b: 48, a: 1 } });
340
+ assert.equal(rowText(canvas, "Color"), "#342230 ✓ --surface-2 / --border");
341
+ });
342
+
343
+ test("a value off the system shows ⚠ no token and the nearest token: colors by OKLab distance, lengths by numeric distance", async () => {
344
+ const canvas = await inspecting({ tokens: EXTRA_TOKENS });
345
+ const [first] = canvas.boards;
346
+ inspect(canvas, first.frame, { ...MEASURE, color: { r: 119, g: 97, b: 107, a: 1 } });
347
+ assert.equal(rowText(canvas, "Color"), "#77616b ⚠ no token Nearest --text-faint #76616b");
348
+ assert.equal(flagged(canvas, "Color"), 1);
349
+ inspect(canvas, first.frame, { ...MEASURE, background: { r: 40, g: 10, b: 20 } });
350
+ assert.equal(rowText(canvas, "Background"), "#280a14 ⚠ no token Nearest --bg #1a1218", "a dark reddish background is perceptually nearest the dark plum");
351
+ inspect(canvas, first.frame, { ...MEASURE, fontFamily: "Arial, sans-serif", fontSize: 13.4, radius: [10, 10, 10, 10], margin: [8, 0, 12, 0], padding: [0, 16, 0, 16] });
352
+ assert.equal(rowText(canvas, "Font"), "Arial ⚠ no token");
353
+ assert.equal(rowText(canvas, "Size"), "13.4 px ⚠ no token Nearest --text-md 14px weight 400");
354
+ assert.equal(rowText(canvas, "Radius"), "10 px ⚠ no token Nearest --radius-sm 8px");
355
+ assert.equal(rowText(canvas, "Spacing"), "margin 8 0 12 0 padding 0 16 0 16 ⚠ 12 px off scale Nearest --space-2 8px");
356
+ inspect(canvas, first.frame, { ...MEASURE, fontSize: 12, margin: [8, 0, 16, 0], fontFamily: "inter, Arial" });
357
+ assert.equal(rowText(canvas, "Size"), "12 px ✓ --text-sm weight 400");
358
+ assert.equal(rowText(canvas, "Spacing"), "margin 8 0 16 0 ✓ on scale");
359
+ assert.equal(rowText(canvas, "Font"), "inter ✓ --font-sans", "the first family of a token's stack counts, in any case");
360
+ inspect(canvas, first.frame, { ...MEASURE, radius: [8, 8, 0, 12], lineHeight: null });
361
+ assert.equal(rowText(canvas, "Radius"), "8 8 0 12 px", "mixed corners on the scale");
362
+ assert.equal(rowText(canvas, "Line height"), "normal");
363
+ inspect(canvas, first.frame, { ...MEASURE, radius: [8, 8, 0, 9] });
364
+ assert.equal(rowText(canvas, "Radius"), "8 8 0 9 px ⚠ 9 px no token");
365
+ });
366
+
367
+ test("rem tokens are converted with the root font size the frame reports, 16 px when it reports none", async () => {
368
+ const REM_TOKENS = { exists: true, tokens: [...TOKEN_SET.tokens,
369
+ { name: "--space-4", value: "1.6rem", section: "Spacing", group: "spacing" },
370
+ { name: "--text-md", value: "1.4rem", section: "Typography", group: "font" },
371
+ ] };
372
+ const canvas = await inspecting({ tokens: REM_TOKENS });
373
+ const [first] = canvas.boards;
374
+ inspect(canvas, first.frame, { ...MEASURE, rootFontSize: 10, fontSize: 14, margin: [0, 0, 0, 0], padding: [16, 16, 16, 16] });
375
+ assert.equal(rowText(canvas, "Spacing"), "padding 16 16 16 16 ✓ on scale", "1.6rem is 16 px under a 10 px root");
376
+ assert.equal(rowText(canvas, "Size"), "14 px ✓ --text-md weight 400", "1.4rem is 14 px under a 10 px root");
377
+ assert.equal(withClass($(canvas, "gd-inspector-list"), "gd-inspector-bad").length, 0, "nothing flagged");
378
+ inspect(canvas, first.frame, { ...MEASURE, fontSize: 22.4, margin: [0, 0, 0, 0], padding: [25.6, 0, 0, 0] });
379
+ assert.equal(rowText(canvas, "Spacing"), "padding 25.6 0 0 0 ✓ on scale", "no root font size reported: 16 px");
380
+ assert.equal(rowText(canvas, "Size"), "22.4 px ✓ --text-md weight 400");
381
+ inspect(canvas, first.frame, { ...MEASURE, rootFontSize: 16, fontSize: 14, margin: [0, 0, 0, 0], padding: [16, 0, 0, 0] });
382
+ assert.equal(rowText(canvas, "Spacing"), "padding 16 0 0 0 ⚠ 16 px off scale Nearest --space-4 1.6rem", "under a 16 px root 1.6rem is 25.6 px");
383
+ assert.equal(rowText(canvas, "Size"), "14 px ⚠ no token Nearest --text-md 1.4rem weight 400");
384
+ });
385
+
386
+ test("negative margins are checked against the spacing scale by their size", async () => {
387
+ const canvas = await inspecting({ tokens: EXTRA_TOKENS });
388
+ const [first] = canvas.boards;
389
+ inspect(canvas, first.frame, { ...MEASURE, margin: [-12, 0, 0, 0] });
390
+ assert.equal(rowText(canvas, "Spacing"), "margin -12 0 0 0 ⚠ -12 px off scale Nearest --space-2 8px");
391
+ assert.equal(flagged(canvas, "Spacing"), 1);
392
+ inspect(canvas, first.frame, { ...MEASURE, margin: [-8, 0, 16, 0] });
393
+ assert.equal(rowText(canvas, "Spacing"), "margin -8 0 16 0 ✓ on scale", "-8 px is the 8 px token pulled the other way");
394
+ assert.equal(flagged(canvas, "Spacing"), 0);
395
+ });
396
+
397
+ test("contrast is the WCAG ratio, flagged under AA (4.5 : 1, 3 : 1 for large text) and never rounded up", async () => {
398
+ const canvas = await inspecting();
399
+ const [first] = canvas.boards;
400
+ const grey = (value: number, a = 1) => ({ r: value, g: value, b: value, a });
401
+ const contrast = (extra: Record<string, unknown>) => {
402
+ inspect(canvas, first.frame, { ...MEASURE, ...extra });
403
+ return [rowText(canvas, "Contrast"), flagged(canvas, "Contrast")];
404
+ };
405
+ assert.deepEqual(contrast({ color: grey(0x76) }), ["✓ 4.54 : 1 AA (4.5 : 1)", 0]);
406
+ assert.deepEqual(contrast({ color: grey(0x77) }), ["⚠ 4.47 : 1 needs 4.5 : 1", 1], "4.478 shows as 4.47");
407
+ assert.deepEqual(contrast({ color: grey(0x77), fontSize: 24 }), ["✓ 4.47 : 1 AA large text (3 : 1)", 0]);
408
+ assert.deepEqual(contrast({ color: grey(0x77), fontSize: 18.66, fontWeight: 700 }), ["✓ 4.47 : 1 AA large text (3 : 1)", 0]);
409
+ assert.deepEqual(contrast({ color: grey(0x77), fontSize: 18.66, fontWeight: 400 }), ["⚠ 4.47 : 1 needs 4.5 : 1", 1], "18.66 px regular is not large");
410
+ assert.deepEqual(contrast({ color: grey(0xaa), fontSize: 24 }), ["⚠ 2.32 : 1 needs 3 : 1 (large text)", 1]);
411
+ assert.deepEqual(contrast({ color: grey(0) }), ["✓ 21 : 1 AA (4.5 : 1)", 0]);
412
+ assert.deepEqual(contrast({ color: grey(0, 0.5) }), ["⚠ 3.94 : 1 needs 4.5 : 1", 1], "translucent text is composited over its background");
413
+ assert.deepEqual(contrast({ hasText: false }), ["—", 0], "no text, no ratio");
414
+ assert.deepEqual(contrast({ background: null }), ["unknown background", 0]);
415
+ assert.equal(rowText(canvas, "Background"), "image or gradient");
416
+ inspect(canvas, first.frame, { ...MEASURE, color: null });
417
+ assert.equal(rowText(canvas, "Color"), "unreadable");
418
+ });
419
+
420
+ test("without tokens, or without tokens of a kind, the panel shows the values and compares nothing", async () => {
421
+ for (const tokens of [null, { exists: false, tokens: [] }]) {
422
+ const canvas = await inspecting({ tokens });
423
+ inspect(canvas, canvas.boards[0].frame);
424
+ assert.equal(shown(canvas), true);
425
+ assert.equal(rowText(canvas, "Color"), "#76616b");
426
+ assert.ok(!/no token/.test(allText($(canvas, "gd-inspector-list"))));
427
+ assert.equal($(canvas, "gd-inspector-note").hidden, false);
428
+ assert.equal($(canvas, "gd-inspector-note").textContent, "No design/system.md tokens found, so values and contrast are shown without a comparison.");
429
+ }
430
+ });
431
+
432
+ // ---- hiding (S8: the 300 ms timer and every exit path)
433
+
434
+ test("leaving the element hides the panel after 300 ms unless the pointer or the keyboard is in it, or another element came", async () => {
435
+ const canvas = await inspecting({ timers: true });
436
+ const [first, second] = canvas.boards;
437
+ inspect(canvas, first.frame);
438
+ inspect(canvas, first.frame, null);
439
+ assert.equal(shown(canvas), true, "not at once: the pointer may be on its way to the buttons");
440
+ canvas.runHide();
441
+ assert.equal(shown(canvas), false);
442
+
443
+ inspect(canvas, first.frame);
444
+ inspect(canvas, second.frame, null);
445
+ canvas.runHide();
446
+ assert.equal(shown(canvas), true, "another frame leaving its element does not hide this one");
447
+
448
+ panelOf(canvas).dispatch("pointerenter");
449
+ inspect(canvas, first.frame, null);
450
+ canvas.runHide();
451
+ assert.equal(shown(canvas), true, "the pointer is on the panel");
452
+ panelOf(canvas).dispatch("pointerleave");
453
+ canvas.runHide();
454
+ assert.equal(shown(canvas), false, "and leaves when it leaves");
455
+
456
+ inspect(canvas, first.frame);
457
+ const copy = $(canvas, "gd-inspector-copy");
458
+ copy.parentElement = panelOf(canvas);
459
+ copy.focus();
460
+ inspect(canvas, first.frame, null);
461
+ canvas.runHide();
462
+ assert.equal(shown(canvas), true, "keyboard focus inside keeps it");
463
+ canvas.document.body.focus();
464
+ copy.dispatch("focusout", { relatedTarget: canvas.document.body });
465
+ canvas.runHide();
466
+ assert.equal(shown(canvas), false, "and it goes once focus leaves it");
467
+
468
+ inspect(canvas, first.frame);
469
+ inspect(canvas, first.frame, null);
470
+ inspect(canvas, first.frame, { ...MEASURE, selector: "main > h1", label: "h1" });
471
+ canvas.runHide();
472
+ assert.equal(shown(canvas), true, "a new element cancels the pending hide");
473
+ canvas.runDwell();
474
+ assert.equal($(canvas, "gd-inspector-title").textContent, "h1", "and the panel follows it once the pointer rests on it");
475
+ });
476
+
477
+ test("focus leaving the panel hides it after the grace, unless it stays in the panel or the pointer is on it; Esc in the panel closes it", async () => {
478
+ const canvas = await inspecting({ timers: true });
479
+ const [first] = canvas.boards;
480
+ const copy = $(canvas, "gd-inspector-copy");
481
+ const fix = $(canvas, "gd-inspector-fix");
482
+ copy.parentElement = panelOf(canvas);
483
+ fix.parentElement = panelOf(canvas);
484
+ inspect(canvas, first.frame, OFF);
485
+ fix.focus();
486
+ copy.dispatch("focusout", { relatedTarget: fix });
487
+ canvas.runHide();
488
+ assert.equal(shown(canvas), true, "Tab from Copy to Ask the agent to fix stays in the panel");
489
+ panelOf(canvas).dispatch("pointerenter");
490
+ canvas.document.body.focus();
491
+ fix.dispatch("focusout", { relatedTarget: canvas.document.body });
492
+ canvas.runHide();
493
+ assert.equal(shown(canvas), true, "the pointer is on the panel");
494
+ panelOf(canvas).dispatch("pointerleave");
495
+ canvas.runHide();
496
+ assert.equal(shown(canvas), false);
497
+
498
+ inspect(canvas, first.frame, OFF);
499
+ copy.focus();
500
+ canvas.document.activeElement = null;
501
+ copy.dispatch("focusout", { relatedTarget: null });
502
+ canvas.runHide();
503
+ assert.equal(shown(canvas), false, "focus leaving the page");
504
+
505
+ inspect(canvas, first.frame, OFF);
506
+ copy.focus();
507
+ const esc = copy.dispatch("keydown", { key: "Escape" });
508
+ assert.equal(shown(canvas), false, "Esc closes it at once");
509
+ assert.equal(esc.defaultPrevented, true, "and only it: Comment mode stays on");
510
+ assert.equal(canvas.document.activeElement?.id, "gd-viewport", "focus goes back to the canvas");
511
+ assert.equal($(canvas, "gd-status").textContent, "Comment mode: click an element in an artboard");
512
+
513
+ focusOn(canvas, 0);
514
+ inspect(canvas, first.frame, OFF);
515
+ copy.focus();
516
+ copy.dispatch("keydown", { key: "Escape" });
517
+ assert.equal(shown(canvas), false);
518
+ assert.equal(canvas.document.activeElement === first.frame, true, "in focus mode, to the open artboard");
519
+ });
520
+
521
+ // ---- hover intent (L4: the pointer must be able to reach the panel's buttons)
522
+
523
+ test("while the panel shows, quick passes over other elements keep it, so the pointer can reach Ask the agent to fix", async () => {
524
+ const canvas = await inspecting({ timers: true });
525
+ const [first] = canvas.boards;
526
+ first.frame.rect = { left: 100, top: 120, width: 400, height: 300 };
527
+ const place = () => [panelOf(canvas).style.left, panelOf(canvas).style.top];
528
+ const title = () => $(canvas, "gd-inspector-title").textContent;
529
+ inspect(canvas, first.frame, OFF);
530
+ assert.equal(shown(canvas), true, "the first panel shows at once");
531
+ const at = place();
532
+ // On its way to the panel the pointer crosses other elements of the artboard, then leaves the frame.
533
+ inspect(canvas, first.frame, { ...MEASURE, selector: "main > h1", label: "h1", rect: { x: 30, y: 200, width: 200, height: 30 } });
534
+ inspect(canvas, first.frame, { ...MEASURE, selector: "main > button", label: "button", rect: { x: 30, y: 260, width: 120, height: 32 } });
535
+ assert.deepEqual([title(), ...place()], ["p.or", ...at], "quick passes keep the element and the place");
536
+ inspect(canvas, first.frame, null);
537
+ panelOf(canvas).dispatch("pointerenter");
538
+ canvas.run();
539
+ assert.deepEqual([shown(canvas), title(), ...place()], [true, "p.or", ...at], "on the panel nothing changes");
540
+ inspect(canvas, first.frame, { ...MATCHING, selector: "main > h2", label: "h2" });
541
+ canvas.run();
542
+ assert.equal(title(), "p.or", "no report changes it while the pointer is on it");
543
+ $(canvas, "gd-inspector-fix").dispatch("click");
544
+ assert.equal($(canvas, "gd-popover-target").textContent, "01-a.html · main > p.or", "Ask the agent to fix opens the element the panel showed");
545
+ assert.match($(canvas, "gd-popover-text").value, /^Fix the values on main > p\.or /);
546
+ });
547
+
548
+ test("S2: re-measures of the element the pointer rests on do not restart the 250 ms rest; the panel switches with its latest values", async () => {
549
+ const canvas = await inspecting({ timers: true });
550
+ const [first] = canvas.boards;
551
+ first.frame.rect = { left: 100, top: 120, width: 400, height: 300 };
552
+ const title = () => $(canvas, "gd-inspector-title").textContent;
553
+ const h1 = { ...MEASURE, selector: "main > h1", label: "h1", rect: { x: 30, y: 200, width: 200, height: 30 } };
554
+ inspect(canvas, first.frame);
555
+ inspect(canvas, first.frame, h1);
556
+ // Late loads or page changes re-measure h1 about every 100 ms while the pointer stays on it.
557
+ inspect(canvas, first.frame, { ...h1, rect: { ...h1.rect, y: 190 } });
558
+ inspect(canvas, first.frame, { ...h1, rect: { ...h1.rect, y: 180 } });
559
+ assert.equal(title(), "p.or", "before the rest the panel keeps its element");
560
+ // Only the 250 ms that started when the pointer reached h1 pass.
561
+ const rest = canvas.timers.find((timer) => !timer.done && timer.ms === DWELL_MS)!;
562
+ rest.done = true;
563
+ rest.callback();
564
+ assert.deepEqual([title(), panelOf(canvas).style.top], ["h1", "250px"], "250 ms after the pointer reached h1, its latest measurement");
565
+ assert.equal(canvas.timers.filter((timer) => !timer.done && timer.ms === DWELL_MS).length, 0, "no second rest is waiting");
566
+ });
567
+
568
+ test("resting 250 ms on another element switches the panel to it; its own element re-places it at once; leaving everything hides it", async () => {
569
+ const canvas = await inspecting({ timers: true });
570
+ const [first, second] = canvas.boards;
571
+ first.frame.rect = { left: 100, top: 120, width: 400, height: 300 };
572
+ second.frame.rect = { left: 100, top: 520, width: 400, height: 300 };
573
+ const title = () => $(canvas, "gd-inspector-title").textContent;
574
+ const top = () => panelOf(canvas).style.top;
575
+ const h1 = { ...MEASURE, selector: "main > h1", label: "h1", rect: { x: 30, y: 200, width: 200, height: 30 } };
576
+ inspect(canvas, first.frame);
577
+ inspect(canvas, first.frame, h1);
578
+ assert.deepEqual([title(), top()], ["p.or", "110px"]);
579
+ canvas.runDwell();
580
+ assert.deepEqual([title(), top()], ["h1", "270px"], "the pointer rested on it");
581
+ inspect(canvas, first.frame, { ...h1, rect: { ...h1.rect, y: 100 } });
582
+ assert.equal(top(), "170px", "the shown element, scrolled: re-placed at once");
583
+
584
+ inspect(canvas, first.frame);
585
+ inspect(canvas, first.frame, h1);
586
+ canvas.runDwell();
587
+ assert.deepEqual([title(), top()], ["h1", "270px"], "back on the shown element before the rest: no switch");
588
+
589
+ // The pointer goes to another artboard: its frame's element after the rest, the first frame's leaving ignored.
590
+ inspect(canvas, second.frame);
591
+ inspect(canvas, first.frame, null);
592
+ canvas.runDwell();
593
+ canvas.runHide();
594
+ assert.deepEqual([shown(canvas), title(), top()], [true, "p.or", "510px"]);
595
+
596
+ // Leaving the page before the rest: no switch, and the panel goes after the grace.
597
+ inspect(canvas, second.frame, h1);
598
+ inspect(canvas, second.frame, null);
599
+ canvas.runDwell();
600
+ assert.equal(title(), "p.or");
601
+ canvas.runHide();
602
+ assert.equal(shown(canvas), false);
603
+
604
+ // A pending switch on another frame than the panel's, then the pointer leaves that frame too: the panel goes.
605
+ inspect(canvas, first.frame);
606
+ inspect(canvas, first.frame, null);
607
+ inspect(canvas, second.frame, h1);
608
+ inspect(canvas, second.frame, null);
609
+ canvas.run();
610
+ assert.equal(shown(canvas), false, "the pointer is on no element");
611
+
612
+ // A pending switch ends with the panel: Technical details off and on again before the rest.
613
+ inspect(canvas, first.frame);
614
+ inspect(canvas, first.frame, h1);
615
+ toggleTechnical(canvas);
616
+ toggleTechnical(canvas);
617
+ canvas.run();
618
+ assert.equal(shown(canvas), false);
619
+ });
620
+
621
+ // ---- the panel follows what changes under it
622
+
623
+ test("tokens that change while the panel shows re-describe it at once, with no new report", async () => {
624
+ const canvas = await inspecting();
625
+ const [first] = canvas.boards;
626
+ inspect(canvas, first.frame, { ...MEASURE, color: { r: 1, g: 2, b: 3, a: 1 } });
627
+ assert.match(rowText(canvas, "Color"), /^#010203 ⚠ no token/);
628
+ assert.equal($(canvas, "gd-inspector-fix").disabled, false);
629
+ canvas.replies[TOKENS_URL] = { exists: true, tokens: [{ name: "--ink", value: "#010203", section: "Colors", group: "color" }, { name: "--paper", value: "#ffffff", section: "Colors", group: "color" }] };
630
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
631
+ assert.equal(shown(canvas), true);
632
+ assert.equal(rowText(canvas, "Color"), "#010203 ✓ --ink");
633
+ assert.equal($(canvas, "gd-inspector-fix").disabled, true, "nothing is off the new tokens");
634
+ // A hidden panel stays hidden.
635
+ toggleTechnical(canvas);
636
+ toggleTechnical(canvas);
637
+ canvas.replies[TOKENS_URL] = TOKEN_SET;
638
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
639
+ assert.equal(shown(canvas), false);
640
+ });
641
+
642
+ test("a re-center the canvas makes in focus mode (a layout.json change) hides the panel, which would be misplaced", async () => {
643
+ const layoutUrl = "/t/api/designs/alpha/layout";
644
+ const canvas = await inspecting({ replies: { [layoutUrl]: { layout: null } } });
645
+ focusOn(canvas, 0);
646
+ inspect(canvas, canvas.boards[0].frame);
647
+ assert.equal(shown(canvas), true);
648
+ canvas.replies[layoutUrl] = { layout: { arrangement: "column", positions: {} } };
649
+ await canvas.serverEvent("change", { slug: "alpha", file: "layout.json" });
650
+ assert.equal(shown(canvas), false);
651
+ });
652
+
653
+ test("a design/system.md the server could not read says why in the panel", async () => {
654
+ const noTokens = "No design/system.md tokens found, so values and contrast are shown without a comparison.";
655
+ const tooLarge = "design/system.md is over 256 KB, so its tokens were not read: values and contrast are shown without a comparison.";
656
+ const unreadable = "design/system.md could not be read, so values and contrast are shown without a comparison.";
657
+ for (const [problem, note] of [["too-large", tooLarge], ["unreadable", unreadable], ["other", noTokens], [3, noTokens]]) {
658
+ const canvas = await inspecting({ tokens: { exists: true, tokens: [], problem } });
659
+ inspect(canvas, canvas.boards[0].frame);
660
+ assert.deepEqual([$(canvas, "gd-inspector-note").hidden, $(canvas, "gd-inspector-note").textContent], [false, note], String(problem));
661
+ }
662
+ // The panel open while system.md grows too large, then is fixed.
663
+ const canvas = await inspecting();
664
+ inspect(canvas, canvas.boards[0].frame);
665
+ assert.equal($(canvas, "gd-inspector-note").hidden, true);
666
+ canvas.replies[TOKENS_URL] = { exists: true, tokens: [], problem: "too-large" };
667
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
668
+ assert.deepEqual([$(canvas, "gd-inspector-note").hidden, $(canvas, "gd-inspector-note").textContent, rowText(canvas, "Color")], [false, tooLarge, "#76616b"]);
669
+ canvas.replies[TOKENS_URL] = TOKEN_SET;
670
+ await canvas.serverEvent("change", { slug: null, file: "system.md" });
671
+ assert.equal($(canvas, "gd-inspector-note").hidden, true);
672
+ });
673
+
674
+ // ---- measuring only with Technical details on (T1)
675
+
676
+ test("gd:mode tells every frame, size frames included, whether Technical details is on, so a frame measures only then", async () => {
677
+ const canvas = await loadTokens({ replies: WIDE_ALPHA });
678
+ const lastMode = (frame: FakeElement) => JSON.stringify(frame.contentWindow!.posted.filter((message) => message.type === "gd:mode").at(-1) ?? null);
679
+ const modes = (frames: FakeElement[]) => frames.map(lastMode);
680
+ const all = () => [...canvas.frames(), ...sizeFrames(canvas)];
681
+ const mode = (enabled: boolean, inspect: boolean) => JSON.stringify({ type: "gd:mode", enabled, inspect });
682
+ commentMode(canvas);
683
+ assert.deepEqual(modes(all()), [mode(true, false), mode(true, false)]);
684
+ toggleTechnical(canvas);
685
+ assert.deepEqual(modes(all()), [mode(true, true), mode(true, true)], "turning it on tells the frames at once");
686
+ focusOn(canvas, 0);
687
+ chooseSize(canvas, "all");
688
+ for (const frame of sizeFrames(canvas)) frame.dispatch("load");
689
+ assert.deepEqual(modes(sizeFrames(canvas)), [mode(true, true), mode(true, true), mode(true, true)], "a size frame that loads is told too");
690
+ toggleTechnical(canvas);
691
+ assert.deepEqual(modes(all()), all().map(() => mode(true, false)), "off: no frame measures");
692
+ });
693
+
694
+ test("the panel hides when its frame goes or its context ends: every exit path", async () => {
695
+ const exits: Array<[string, (canvas: Canvas) => Promise<void> | void]> = [
696
+ ["Back to canvas", (canvas) => $(canvas, "gd-back").dispatch("click")],
697
+ ["Shift+Esc", (canvas) => { canvas.message(canvas.boards[0].frame.contentWindow, { type: "gd:exit-focus" }); }],
698
+ ["Move (leaving Comment mode)", (canvas) => $(canvas, "gd-tool-navigate").dispatch("click")],
699
+ ["Technical details off", (canvas) => toggleTechnical(canvas)],
700
+ ["another size", (canvas) => chooseSize(canvas, "phone")],
701
+ ["a design switch", (canvas) => canvas.serverEvent("show", { slug: "beta" })],
702
+ ["a design.json reload", (canvas) => canvas.serverEvent("change", { slug: "alpha", file: "design.json" })],
703
+ ];
704
+ for (const [name, exit] of exits) {
705
+ const canvas = await inspecting();
706
+ focusOn(canvas, 0);
707
+ inspect(canvas, canvas.boards[0].frame);
708
+ assert.equal(shown(canvas), true, name);
709
+ await exit(canvas);
710
+ assert.equal(shown(canvas), false, name);
711
+ }
712
+ // In the overview, a pan or a zoom moves the frame away from the panel.
713
+ const canvas = await inspecting();
714
+ inspect(canvas, canvas.boards[0].frame);
715
+ canvas.viewport.dispatch("wheel", { deltaX: 0, deltaY: 40, deltaMode: 0, ctrlKey: false, metaKey: false, shiftKey: false, clientX: 300, clientY: 300 });
716
+ assert.equal(shown(canvas), false, "a pan");
717
+ });
718
+
719
+ test("a design switch: the old design's frames are not trusted for the panel while the new design loads", async () => {
720
+ const beta = deferred();
721
+ const canvas = await inspecting({ replies: { "/t/api/designs/beta": beta.promise } });
722
+ const [first] = canvas.boards;
723
+ inspect(canvas, first.frame);
724
+ assert.equal(shown(canvas), true);
725
+ await canvas.serverEvent("show", { slug: "beta" });
726
+ assert.equal(shown(canvas), false, "the switch hides it");
727
+ inspect(canvas, first.frame);
728
+ assert.equal(shown(canvas), false, "a late report from the old design's frame");
729
+ beta.answer({ slug: "beta", title: "Beta", brief: "", artboards: [{ file: "01-only.html", title: "Only", tradeoff: "", width: 500, height: 400 }] });
730
+ await settle();
731
+ inspect(canvas, canvas.frames()[0]);
732
+ assert.equal(shown(canvas), true, "the new design's frame");
733
+
734
+ // The design list too.
735
+ const other = await inspecting();
736
+ inspect(other, other.boards[0].frame);
737
+ $(other, "gd-design").value = "beta";
738
+ $(other, "gd-design").dispatch("change");
739
+ assert.equal(shown(other), false);
740
+ });
741
+
742
+ test("a design.json reload builds new frames: a report from a replaced frame is ignored", async () => {
743
+ const canvas = await inspecting();
744
+ const [old] = canvas.boards;
745
+ await canvas.serverEvent("change", { slug: "alpha", file: "design.json" });
746
+ inspect(canvas, old.frame);
747
+ assert.equal(shown(canvas), false, "the replaced frame");
748
+ inspect(canvas, canvas.frames()[0]);
749
+ assert.equal(shown(canvas), true, "its replacement");
750
+ });
751
+
752
+ test("an All sizes frame that goes without a size change (a PNG export) takes the panel with it", async () => {
753
+ const track: Record<string, any> = { readyState: "live", getSettings: () => ({ displaySurface: "browser" }), addEventListener() {}, stop() {} };
754
+ const stream = { getTracks: () => [track], getVideoTracks: () => [track] };
755
+ const canvas = await inspecting({ replies: WIDE_ALPHA, extraWindow: { navigator: { mediaDevices: { getDisplayMedia: async () => stream } } } });
756
+ focusOn(canvas, 0);
757
+ chooseSize(canvas, "all");
758
+ const [phone] = sizeFrames(canvas);
759
+ inspect(canvas, phone);
760
+ assert.equal(shown(canvas), true);
761
+ withClass(canvas.boards[0].wrapper, "gd-menu-button")[0].dispatch("click");
762
+ findAll(menuEl(canvas), (element) => texts(element, "gd-menu-label")[0] === "Export as PNG" && element.getAttribute("role") === "menuitem")[0].dispatch("click");
763
+ await settle();
764
+ assert.equal(sizeFrames(canvas).length, 3, "All sizes is back after the export, with new frames");
765
+ assert.equal(shown(canvas), false, "the panel of a removed frame is gone");
766
+ inspect(canvas, phone);
767
+ assert.equal(shown(canvas), false, "and the removed frame is not trusted");
768
+ inspect(canvas, sizeFrames(canvas)[0]);
769
+ assert.equal(shown(canvas), true);
770
+ });
771
+
772
+ test("the pointer moving onto the panel keeps the hovered element's outline in the artboard", async () => {
773
+ const canvas = await inspecting();
774
+ const [first] = canvas.boards;
775
+ first.frame.rect = { left: 100, top: 120, width: 400, height: 300 };
776
+ canvas.viewport.dispatch("pointermove", { pointerId: 1, clientX: 150, clientY: 150, target: first.shield });
777
+ inspect(canvas, first.frame);
778
+ const hovers = () => first.frame.contentWindow!.posted.filter((message) => message.type === "gd:hover-at").length;
779
+ const before = hovers();
780
+ canvas.viewport.dispatch("pointermove", { pointerId: 1, clientX: 600, clientY: 200, target: panelOf(canvas) });
781
+ assert.equal(hovers(), before, "no hover-at reached the artboard");
782
+ assert.equal(shown(canvas), true);
783
+ });
784
+
785
+ // ---- Ask the agent to fix and Copy (S8)
786
+
787
+ test("Ask the agent to fix opens the comment box on that element, prefilled with the flagged values, and sends it as a comment", async () => {
788
+ const canvas = await inspecting();
789
+ const [first] = canvas.boards;
790
+ first.frame.rect = { left: 100, top: 120, width: 400, height: 300 };
791
+ inspect(canvas, first.frame, OFF);
792
+ assert.equal($(canvas, "gd-inspector-fix").disabled, false);
793
+ $(canvas, "gd-inspector-fix").dispatch("click");
794
+ assert.equal($(canvas, "gd-popover").hidden, false);
795
+ assert.equal(shown(canvas), false, "the comment box takes the panel's place");
796
+ assert.equal($(canvas, "gd-popover-target").textContent, "01-a.html · main > p.or");
797
+ const text = $(canvas, "gd-popover-text").value;
798
+ const lines = text.split("\n");
799
+ assert.equal(lines[0], "Fix the values on main > p.or that are off the design system (design/system.md):");
800
+ assert.match(lines[1], /^- Text color #96828c is not a token; nearest is --text-muted \(#a78e9b\)\.$/);
801
+ assert.match(lines[2], /^- Contrast 3\.\d\d:1 is below WCAG AA \(4\.5:1 for normal text\)\.$/);
802
+ assert.equal(lines[3], "- Border radius 10px is not a token; nearest is --radius-sm (8px).");
803
+ assert.equal(lines.length, 4, "values that match a token are not listed");
804
+ assert.equal(canvas.document.activeElement?.id, "gd-popover-text");
805
+ $(canvas, "gd-popover-text").value = `${text}\nAlso check the hover state.`;
806
+ $(canvas, "gd-popover-form").dispatch("submit");
807
+ await settle();
808
+ const post = canvas.posts.at(-1)!;
809
+ assert.equal(post.url, "/t/api/comments");
810
+ const body = post.body as Record<string, unknown>;
811
+ assert.deepEqual([body.slug, body.file, body.selector, body.excerpt, body.mode, "viewport" in body], ["alpha", "01-a.html", "main > p.or", "or pay with card", "send", false]);
812
+ assert.match(String(body.comment), /Also check the hover state\.$/);
813
+ });
814
+
815
+ test("Ask the agent to fix from an All sizes frame records that size; a long list still fits the comment limit", async () => {
816
+ const canvas = await inspecting({ replies: WIDE_ALPHA });
817
+ focusOn(canvas, 0);
818
+ chooseSize(canvas, "all");
819
+ const [phone] = sizeFrames(canvas);
820
+ inspect(canvas, phone, { ...OFF, selector: `main > ${"div > ".repeat(80)}p` });
821
+ $(canvas, "gd-inspector-fix").dispatch("click");
822
+ assert.ok($(canvas, "gd-popover-text").value.length <= 2000);
823
+ $(canvas, "gd-popover-form").dispatch("submit");
824
+ await settle();
825
+ assert.deepEqual((canvas.posts.at(-1)!.body as Record<string, unknown>).viewport, { width: 390, height: 844 });
826
+ });
827
+
828
+ test("S1: a measurement made at Phone whose report arrives after the switch to Tablet shows nothing and files nothing; a report made at Tablet files Tablet's size", async () => {
829
+ const canvas = await inspecting({ replies: WIDE_ALPHA });
830
+ const [first] = canvas.boards;
831
+ focusOn(canvas, 0);
832
+ chooseSize(canvas, "phone");
833
+ const phoneGeneration = heardGeneration(first.frame);
834
+ chooseSize(canvas, "tablet");
835
+ assert.notEqual(heardGeneration(first.frame), phoneGeneration, "the switch tells the page its new size generation");
836
+ inspect(canvas, first.frame, { ...OFF, fontSize: 13 }, phoneGeneration);
837
+ assert.equal(shown(canvas), false, "the Phone-era report opens no panel");
838
+ assert.equal($(canvas, "gd-popover").hidden, true);
839
+ const before = canvas.posts.length;
840
+ inspect(canvas, first.frame, OFF);
841
+ assert.equal(shown(canvas), true, "a report made at the current size still shows");
842
+ $(canvas, "gd-inspector-fix").dispatch("click");
843
+ $(canvas, "gd-popover-form").dispatch("submit");
844
+ await settle();
845
+ assert.equal(canvas.posts.length, before + 1);
846
+ assert.equal(canvas.posts.at(-1)!.url, "/t/api/comments");
847
+ const body = canvas.posts.at(-1)!.body as Record<string, unknown>;
848
+ assert.deepEqual(body.viewport, { width: 820, height: 1180 });
849
+ assert.doesNotMatch(String(body.comment), /13px/);
850
+ });
851
+
852
+ test("with nothing off the system Ask the agent to fix is disabled and says why; Copy still works", async () => {
853
+ const canvas = await inspecting();
854
+ const [first] = canvas.boards;
855
+ inspect(canvas, first.frame, MATCHING);
856
+ const fix = $(canvas, "gd-inspector-fix");
857
+ assert.equal(fix.disabled, true);
858
+ assert.equal(fix.title, "Nothing is off the design system, so there is nothing to fix.");
859
+ assert.equal($(canvas, "gd-inspector-copy").disabled, false);
860
+ fix.dispatch("click");
861
+ assert.equal($(canvas, "gd-popover").hidden, true);
862
+ inspect(canvas, first.frame, OFF);
863
+ assert.equal(fix.disabled, false);
864
+ assert.equal(fix.title, "");
865
+ });
866
+
867
+ test("S3: Copy copies the panel's text: the element, its selector and every row as shown, also with nothing off the system", async () => {
868
+ const written: string[] = [];
869
+ const canvas = await inspecting({ extraWindow: { navigator: { clipboard: { writeText: async (text: string) => { written.push(text); } } } } });
870
+ inspect(canvas, canvas.boards[0].frame, MATCHING);
871
+ const labels = rows(canvas).map((row) => texts(row, "gd-inspector-label")[0]);
872
+ assert.deepEqual(labels, ["Color", "Background", "Contrast", "Font", "Size", "Line height", "Spacing", "Radius"]);
873
+ $(canvas, "gd-inspector-copy").dispatch("click");
874
+ await settle();
875
+ assert.equal(written.length, 1);
876
+ const lines = written[0].split("\n");
877
+ assert.deepEqual(lines, ["p.or", "main > p.or", ...labels.map((label) => `${label}: ${rowText(canvas, label)}`)]);
878
+ assert.match(lines[2], /^Color: #f6eff3 ✓ --text$/);
879
+ assert.doesNotMatch(written[0], /Nothing is off the design system/);
880
+ });
881
+
882
+ test("Copy writes the panel's text to the clipboard, falls back to the copy command, and says when neither works", async () => {
883
+ const written: string[] = [];
884
+ const canvas = await inspecting({ extraWindow: { navigator: { clipboard: { writeText: async (text: string) => { written.push(text); } } } } });
885
+ inspect(canvas, canvas.boards[0].frame, OFF);
886
+ $(canvas, "gd-inspector-copy").dispatch("click");
887
+ await settle();
888
+ assert.equal(written.length, 1);
889
+ assert.deepEqual(written[0].split("\n").slice(0, 3), ["p.or", "main > p.or", `Color: ${rowText(canvas, "Color")}`]);
890
+ assert.match(written[0], /⚠ no token/, "the flagged values are in the rows it copies");
891
+ assert.equal($(canvas, "gd-toast").textContent, "Copied");
892
+ assert.equal($(canvas, "gd-toast").classes.has("gd-error"), false);
893
+ $(canvas, "gd-inspector-fix").dispatch("click");
894
+ assert.match($(canvas, "gd-popover-text").value, /^Fix the values on main > p\.or /, "Ask the agent to fix still prefills the instructions");
895
+
896
+ const commands: Array<[string, string]> = [];
897
+ const refused = await inspecting({ extraWindow: { navigator: { clipboard: { writeText: async () => { throw new Error("denied"); } } } } });
898
+ (refused.document as any).execCommand = (command: string) => { commands.push([command, refused.document.activeElement?.value ?? ""]); return true; };
899
+ inspect(refused, refused.boards[0].frame, OFF);
900
+ refused.document.activeElement = $(refused, "gd-inspector-copy");
901
+ $(refused, "gd-inspector-copy").dispatch("click");
902
+ await settle();
903
+ assert.equal(commands.length, 1);
904
+ assert.equal(commands[0][0], "copy");
905
+ assert.deepEqual(commands[0][1].split("\n").slice(0, 2), ["p.or", "main > p.or"], "the hidden text area holds the text");
906
+ assert.equal($(refused, "gd-toast").textContent, "Copied");
907
+ assert.equal(refused.document.body.children.filter((child) => child.localName === "textarea").length, 0, "the text area is removed");
908
+ assert.equal(refused.document.activeElement?.id, "gd-inspector-copy", "focus goes back");
909
+
910
+ const none = await inspecting();
911
+ inspect(none, none.boards[0].frame, OFF);
912
+ $(none, "gd-inspector-copy").dispatch("click");
913
+ await settle();
914
+ assert.equal($(none, "gd-toast").textContent, "Could not copy. Use Ask the agent to fix and copy the text from the comment box.");
915
+ assert.equal($(none, "gd-toast").classes.has("gd-error"), true);
916
+ });
917
+
918
+ // ---- markup, styles, module rules and docs (S3, S4, S6)
919
+
920
+ test("index.html has the panel in the viewport, hidden, with a heading and real buttons, and the code icon", () => {
921
+ const html = read("index.html");
922
+ const main = html.slice(html.indexOf('<main id="gd-viewport"'), html.indexOf("</main>"));
923
+ assert.match(main, /<section id="gd-inspector" class="gd-inspector" aria-labelledby="gd-inspector-title" hidden>/);
924
+ assert.match(main, /<h2 id="gd-inspector-title" class="gd-inspector-title"><\/h2>/);
925
+ assert.match(main, /<dl id="gd-inspector-list" class="gd-inspector-list"><\/dl>/);
926
+ assert.match(main, /<button type="button" id="gd-inspector-fix"[^>]*>Ask the agent to fix<\/button>/);
927
+ assert.match(main, /<button type="button" id="gd-inspector-copy"[^>]*>Copy<\/button>/);
928
+ assert.match(html, /<symbol id="i-code" viewBox="0 0 24 24">/);
929
+ // Its buttons are keyed for the language (ES.html); the rows come from canvas-tokens.js.
930
+ assert.match(main, /id="gd-inspector-fix"[^>]*data-i18n="inspectorFix"/);
931
+ assert.match(main, /id="gd-inspector-copy"[^>]*data-i18n="inspectorCopy"/);
932
+ });
933
+
934
+ test("canvas.css styles the panel in one section on --gd-* tokens, the warning in words, no dots", () => {
935
+ const css = read("canvas.css");
936
+ assert.equal(css.split("/* Token inspector").length - 1, 1, "one Token inspector section");
937
+ const section = css.slice(css.indexOf("/* Token inspector"), css.indexOf("/*", css.indexOf("/* Token inspector") + 2));
938
+ for (const selector of [".gd-inspector {", ".gd-inspector[hidden]", ".gd-inspector-row {", ".gd-inspector-swatch {", ".gd-inspector-ok {", ".gd-inspector-bad {"]) assert.ok(section.includes(selector), `${selector} is styled`);
939
+ for (const name of section.match(/var\(--[\w-]+/g) ?? []) assert.ok(name.startsWith("var(--gd-"), `${name}: a --gd-* token`);
940
+ assert.ok(!/::before|::after|border-radius: 50%/.test(section), "no dots");
941
+ assert.match(section, /\.gd-inspector-bad \{[^}]*var\(--gd-focus\)/);
942
+ assert.match(section, /\.gd-inspector-ok \{[^}]*var\(--gd-success\)/);
943
+ assert.match(css, /\.gd-menu-item\[aria-checked="true"\] \.gd-menu-check \{ visibility: visible; \}/);
944
+ });
945
+
946
+ test("canvas-tokens.js keeps its English strings in one block, reaches the browser only through ctx and never calls an action from init", () => {
947
+ const source = read("canvas-tokens.js");
948
+ const init = source.indexOf("export function init");
949
+ assert.ok(source.indexOf("const TEXT = Object.freeze({") >= 0 && source.indexOf("const TEXT = Object.freeze({") < init);
950
+ assert.ok(!/(?<![.\w])(navigator|document|window|localStorage|setTimeout)\b/.test(source.slice(init).replace(/\/\/.*$|\/\*[^]*?\*\//gm, "")), "only ctx.win and ctx.doc");
951
+ assert.ok(source.includes('"gentle-design:technical"'));
952
+ assert.ok(!/state\.(tokens|technical)\s*=/.test(source), "store mutators only");
953
+ });
954
+
955
+ test("the docs describe the token inspector with the heuristics note, and Technical details in the Tool rail", () => {
956
+ const docs = readFileSync(fileURLToPath(new URL("../docs/gentle-shell.md", import.meta.url)), "utf8");
957
+ const section = docs.slice(docs.indexOf("### Gentle Design"), docs.indexOf("### Gentle Music"));
958
+ const start = section.indexOf("- **Tokens**");
959
+ assert.ok(start >= 0, "a Tokens entry");
960
+ const entry = section.slice(start, section.indexOf("\n", start));
961
+ assert.ok(entry.includes("These are heuristics: an element with a background image or gradient has no known background, and the match uses computed values, not the CSS that produced them."));
962
+ for (const words of ["**Technical details**", "Comment mode", "⚠ no token", "OKLab", "WCAG", "**Ask the agent to fix**", "**Copy**", "`design/system.md`", "`gentle-design:technical`", "only after the pointer rests on it (250 ms)", "`Esc` in the panel closes it", "keyboard focus leaves the panel", "over 256 KB or could not be read", "measure the element under the pointer only while Technical details is on"]) assert.ok(entry.includes(words), `the Tokens entry mentions ${words}`);
963
+ const rail = section.slice(section.indexOf("- **Tool rail**"), section.indexOf("\n", section.indexOf("- **Tool rail**")));
964
+ assert.ok(rail.includes("**Technical details**"), "the Tool rail entry names Technical details");
965
+ });