paseo-plugin-genui 0.0.0-stage → 0.1.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 (375) hide show
  1. package/LICENSE +202 -0
  2. package/OVERVIEW.md +21 -0
  3. package/README.md +77 -2
  4. package/assets/fonts/Excalifont-OFL.txt +109 -0
  5. package/assets/fonts/Excalifont-Regular-latin.woff2 +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +1 -0
  8. package/assets/screenshot-chat.png +0 -0
  9. package/assets/screenshot-gallery.png +0 -0
  10. package/assets/screenshot-playground.png +0 -0
  11. package/client/components/content.tsx +468 -0
  12. package/client/components/data.tsx +967 -0
  13. package/client/components/dev.tsx +998 -0
  14. package/client/components/diagram.tsx +513 -0
  15. package/client/components/inputs.tsx +994 -0
  16. package/client/components/layout.tsx +239 -0
  17. package/client/components/media.tsx +428 -0
  18. package/client/components/project.tsx +641 -0
  19. package/client/components/widgets.tsx +836 -0
  20. package/client/gallery.tsx +449 -0
  21. package/client/live-ui.tsx +525 -0
  22. package/client/loaders.ts +342 -0
  23. package/client/markdown.tsx +172 -0
  24. package/client/node-renderer.tsx +149 -0
  25. package/client/render.tsx +186 -0
  26. package/client/runtime.ts +103 -0
  27. package/client/settings.tsx +57 -0
  28. package/client/web.ts +186 -0
  29. package/gallery/Acceptance-baseline.ui +1 -0
  30. package/gallery/Acceptance-checklist.ui +14 -0
  31. package/gallery/Acceptance-evidence.ui +9 -0
  32. package/gallery/Acceptance.ui +1 -0
  33. package/gallery/Badge.ui +3 -0
  34. package/gallery/Benchmark-context.ui +8 -0
  35. package/gallery/Benchmark-hyperfine.ui +1 -0
  36. package/gallery/Benchmark-runs-list.ui +9 -0
  37. package/gallery/Benchmark-runs.ui +1 -0
  38. package/gallery/Benchmark-vitest.ui +1 -0
  39. package/gallery/Benchmark.ui +9 -0
  40. package/gallery/Board-bb-tasks.ui +1 -0
  41. package/gallery/Board-gh-issues.ui +1 -0
  42. package/gallery/Board-gh-prs.ui +1 -0
  43. package/gallery/Board.ui +12 -0
  44. package/gallery/Button-destructive.ui +4 -0
  45. package/gallery/Button.ui +5 -0
  46. package/gallery/Calendar.ui +15 -0
  47. package/gallery/CalendarHeatmap-short.ui +11 -0
  48. package/gallery/CalendarHeatmap.ui +4 -0
  49. package/gallery/Callout.ui +4 -0
  50. package/gallery/Card.ui +4 -0
  51. package/gallery/Carousel.ui +8 -0
  52. package/gallery/Changes-paths.ui +1 -0
  53. package/gallery/Changes-uncommitted.ui +1 -0
  54. package/gallery/Changes.ui +1 -0
  55. package/gallery/Chart-area.ui +3 -0
  56. package/gallery/Chart-funnel.ui +3 -0
  57. package/gallery/Chart-heatmap.ui +11 -0
  58. package/gallery/Chart-pie.ui +3 -0
  59. package/gallery/Chart-radar.ui +3 -0
  60. package/gallery/Chart-scatter.ui +4 -0
  61. package/gallery/Chart-stacked.ui +5 -0
  62. package/gallery/Chart.ui +3 -0
  63. package/gallery/Checklist.ui +6 -0
  64. package/gallery/Choice.ui +8 -0
  65. package/gallery/Code.ui +3 -0
  66. package/gallery/Commits-limit.ui +1 -0
  67. package/gallery/Commits.ui +1 -0
  68. package/gallery/Compare-conditions.ui +4 -0
  69. package/gallery/Compare-three.ui +7 -0
  70. package/gallery/Compare.ui +4 -0
  71. package/gallery/Coverage-go.ui +1 -0
  72. package/gallery/Coverage-lcov.ui +1 -0
  73. package/gallery/Coverage-python.ui +1 -0
  74. package/gallery/Coverage.ui +1 -0
  75. package/gallery/DateInput.ui +5 -0
  76. package/gallery/Decision-percent.ui +10 -0
  77. package/gallery/Decision.ui +12 -0
  78. package/gallery/Dependencies-audit.ui +1 -0
  79. package/gallery/Dependencies-both.ui +1 -0
  80. package/gallery/Dependencies-list.ui +7 -0
  81. package/gallery/Dependencies-pip.ui +1 -0
  82. package/gallery/Dependencies-pnpm.ui +1 -0
  83. package/gallery/Dependencies.ui +1 -0
  84. package/gallery/Details.ui +4 -0
  85. package/gallery/Diagram-flow.ui +16 -0
  86. package/gallery/Diagram-sequence.ui +15 -0
  87. package/gallery/Diagram.ui +16 -0
  88. package/gallery/Diff.ui +3 -0
  89. package/gallery/Divider.ui +5 -0
  90. package/gallery/EditableTable.ui +9 -0
  91. package/gallery/Entity-list.ui +6 -0
  92. package/gallery/Entity.ui +8 -0
  93. package/gallery/Environments.ui +1 -0
  94. package/gallery/Estimate-points.ui +6 -0
  95. package/gallery/Estimate.ui +8 -0
  96. package/gallery/File.ui +4 -0
  97. package/gallery/Findings-biome.ui +1 -0
  98. package/gallery/Findings-eslint.ui +1 -0
  99. package/gallery/Findings-oxlint.ui +1 -0
  100. package/gallery/Findings-review.ui +1 -0
  101. package/gallery/Findings-ruff.ui +1 -0
  102. package/gallery/Findings-sarif.ui +1 -0
  103. package/gallery/Findings-ship.ui +11 -0
  104. package/gallery/Findings-tsc.ui +1 -0
  105. package/gallery/Findings.ui +7 -0
  106. package/gallery/Flashcards.ui +3 -0
  107. package/gallery/FollowUps.ui +4 -0
  108. package/gallery/Form.ui +8 -0
  109. package/gallery/Gallery-links.ui +8 -0
  110. package/gallery/Gallery-strip.ui +14 -0
  111. package/gallery/Gallery.ui +10 -0
  112. package/gallery/Gauge.ui +7 -0
  113. package/gallery/Grid.ui +8 -0
  114. package/gallery/Hotspots-photo.ui +8 -0
  115. package/gallery/Hotspots.ui +11 -0
  116. package/gallery/Icon.ui +5 -0
  117. package/gallery/Image.ui +4 -0
  118. package/gallery/ImageSearch-aspects.ui +6 -0
  119. package/gallery/ImageSearch.ui +4 -0
  120. package/gallery/Input.ui +5 -0
  121. package/gallery/Json-api.ui +1 -0
  122. package/gallery/Json.ui +8 -0
  123. package/gallery/KeyValue.ui +3 -0
  124. package/gallery/List-dots.ui +8 -0
  125. package/gallery/List.ui +8 -0
  126. package/gallery/ListEditor.ui +5 -0
  127. package/gallery/Logs-jsonl.ui +1 -0
  128. package/gallery/Logs-lines.ui +11 -0
  129. package/gallery/Logs-python.ui +1 -0
  130. package/gallery/Logs.ui +1 -0
  131. package/gallery/Map-city.ui +8 -0
  132. package/gallery/Map.ui +8 -0
  133. package/gallery/Metric.ui +3 -0
  134. package/gallery/OptionCards-multi.ui +10 -0
  135. package/gallery/OptionCards.ui +10 -0
  136. package/gallery/Pipeline-checks.ui +1 -0
  137. package/gallery/Pipeline-passed.ui +1 -0
  138. package/gallery/Pipeline-running.ui +1 -0
  139. package/gallery/Pipeline-stages.ui +13 -0
  140. package/gallery/Pipeline.ui +1 -0
  141. package/gallery/Plan.ui +9 -0
  142. package/gallery/Progress.ui +4 -0
  143. package/gallery/Provenance-deploy.ui +6 -0
  144. package/gallery/Provenance-no-receipt.ui +1 -0
  145. package/gallery/Provenance.ui +1 -0
  146. package/gallery/PullRequest.ui +1 -0
  147. package/gallery/Quiz.ui +6 -0
  148. package/gallery/Radio.ui +5 -0
  149. package/gallery/Rating.ui +5 -0
  150. package/gallery/Roadmap-sprint.ui +8 -0
  151. package/gallery/Roadmap-year.ui +9 -0
  152. package/gallery/Roadmap.ui +12 -0
  153. package/gallery/Row.ui +3 -0
  154. package/gallery/SegmentedBar-status.ui +8 -0
  155. package/gallery/SegmentedBar.ui +8 -0
  156. package/gallery/Select.ui +5 -0
  157. package/gallery/Slider.ui +7 -0
  158. package/gallery/Snippet.ui +3 -0
  159. package/gallery/Sources-chips.ui +4 -0
  160. package/gallery/Sources.ui +8 -0
  161. package/gallery/Sparkline.ui +6 -0
  162. package/gallery/Stack.ui +7 -0
  163. package/gallery/StackTrace-aggregate.ui +1 -0
  164. package/gallery/StackTrace-browser.ui +1 -0
  165. package/gallery/StackTrace-go.ui +1 -0
  166. package/gallery/StackTrace-java.ui +1 -0
  167. package/gallery/StackTrace-python.ui +1 -0
  168. package/gallery/StackTrace-rust.ui +1 -0
  169. package/gallery/StackTrace.ui +1 -0
  170. package/gallery/Status.ui +9 -0
  171. package/gallery/Stepper.ui +5 -0
  172. package/gallery/Steps.ui +8 -0
  173. package/gallery/Swatches-long-names.ui +4 -0
  174. package/gallery/Swatches-picker.ui +5 -0
  175. package/gallery/Swatches.ui +10 -0
  176. package/gallery/Table.ui +7 -0
  177. package/gallery/Tabs.ui +8 -0
  178. package/gallery/Tests-junit.ui +1 -0
  179. package/gallery/Tests-vitest-junit.ui +1 -0
  180. package/gallery/Tests-vitest.ui +1 -0
  181. package/gallery/Tests.ui +9 -0
  182. package/gallery/Text.ui +4 -0
  183. package/gallery/TextArea.ui +5 -0
  184. package/gallery/Threads-ids.ui +1 -0
  185. package/gallery/Threads.ui +1 -0
  186. package/gallery/Timeline.ui +8 -0
  187. package/gallery/Timer.ui +4 -0
  188. package/gallery/Toggle.ui +5 -0
  189. package/gallery/Transcript-text.ui +1 -0
  190. package/gallery/Transcript-untimed.ui +1 -0
  191. package/gallery/Transcript-whisper.ui +1 -0
  192. package/gallery/Transcript.ui +1 -0
  193. package/gallery/Tree.ui +3 -0
  194. package/gallery/Treemap-du.ui +1 -0
  195. package/gallery/Treemap-esbuild.ui +1 -0
  196. package/gallery/Treemap-tree.ui +7 -0
  197. package/gallery/Treemap.ui +18 -0
  198. package/gallery/Walkthrough.ui +9 -0
  199. package/gallery/Waterfall-har.ui +1 -0
  200. package/gallery/Waterfall-trace.ui +1 -0
  201. package/gallery/Waterfall.ui +11 -0
  202. package/gallery/data/acceptance/coil/viewport-lifecycle.json +25 -0
  203. package/gallery/data/acceptance/coil-gate.json +153 -0
  204. package/gallery/data/acceptance/orbit-baseline.json +163 -0
  205. package/gallery/data/acceptance/shots/gate-cell-opened.png +0 -0
  206. package/gallery/data/acceptance/shots/gate-light.png +0 -0
  207. package/gallery/data/acceptance/shots/gate-phone-dark.png +0 -0
  208. package/gallery/data/activity.json +1 -0
  209. package/gallery/data/bb-tasks-orb.json +420 -0
  210. package/gallery/data/biome.json +229 -0
  211. package/gallery/data/compare/status-after.png +0 -0
  212. package/gallery/data/compare/status-before.png +0 -0
  213. package/gallery/data/compare/status-dark-2x.png +0 -0
  214. package/gallery/data/compare/status-rerender.png +0 -0
  215. package/gallery/data/coverage-py.json +1 -0
  216. package/gallery/data/coverage-summary.json +1 -0
  217. package/gallery/data/coverage.out +23 -0
  218. package/gallery/data/deploys.json +1 -0
  219. package/gallery/data/device-runs.json +285 -0
  220. package/gallery/data/du-node-modules.txt +359 -0
  221. package/gallery/data/esbuild-meta.json +292 -0
  222. package/gallery/data/eslint.json +266 -0
  223. package/gallery/data/eslint.sarif +343 -0
  224. package/gallery/data/findings-review.json +76 -0
  225. package/gallery/data/gh-issues-nextjs.json +293 -0
  226. package/gallery/data/gh-pr-checks.json +272 -0
  227. package/gallery/data/gh-prs-nextjs.json +419 -0
  228. package/gallery/data/gh-run-failed.json +1 -0
  229. package/gallery/data/gh-run-passed.json +1 -0
  230. package/gallery/data/gh-run-running.json +1 -0
  231. package/gallery/data/github-release.json +57 -0
  232. package/gallery/data/hyperfine-startup.json +187 -0
  233. package/gallery/data/lcov.info +234 -0
  234. package/gallery/data/logs/media-sync.log +127 -0
  235. package/gallery/data/logs/thumbnailer.jsonl +17 -0
  236. package/gallery/data/logs/vitest-run.log +89 -0
  237. package/gallery/data/npm-audit.json +672 -0
  238. package/gallery/data/npm-outdated.json +179 -0
  239. package/gallery/data/oxlint.json +156 -0
  240. package/gallery/data/pip-outdated.json +68 -0
  241. package/gallery/data/pnpm-outdated.json +58 -0
  242. package/gallery/data/provenance/devicectl-install.json +35 -0
  243. package/gallery/data/provenance/install-timeout.log +4 -0
  244. package/gallery/data/provenance/iphone-install.json +72 -0
  245. package/gallery/data/provenance/iphone-no-receipt.json +57 -0
  246. package/gallery/data/provenance/live-home.jpg +0 -0
  247. package/gallery/data/provenance/live-probe.txt +10 -0
  248. package/gallery/data/provenance/xcodebuild-2811.log +19 -0
  249. package/gallery/data/provenance/xcodebuild-4712.log +19 -0
  250. package/gallery/data/pytest-junit.xml +64 -0
  251. package/gallery/data/python-org-trace.json +35 -0
  252. package/gallery/data/python-org.har +31 -0
  253. package/gallery/data/retry.ts +13 -0
  254. package/gallery/data/ruff.json +261 -0
  255. package/gallery/data/src/durations.py +63 -0
  256. package/gallery/data/src/json-logic.ts +156 -0
  257. package/gallery/data/src/mapMath.ts +135 -0
  258. package/gallery/data/src/semver/bump.go +73 -0
  259. package/gallery/data/traces/browser-error.json +5 -0
  260. package/gallery/data/traces/go/main.go +27 -0
  261. package/gallery/data/traces/go-panic.txt +16 -0
  262. package/gallery/data/traces/java-caused-by.txt +15 -0
  263. package/gallery/data/traces/node-aggregate.txt +30 -0
  264. package/gallery/data/traces/node-cause.txt +21 -0
  265. package/gallery/data/traces/py/deploy.json +5 -0
  266. package/gallery/data/traces/py/deploy.py +17 -0
  267. package/gallery/data/traces/py/settings.py +19 -0
  268. package/gallery/data/traces/python-chain.txt +30 -0
  269. package/gallery/data/traces/rust/main.rs +11 -0
  270. package/gallery/data/traces/rust-panic.txt +16 -0
  271. package/gallery/data/traces/shop/cart.mjs +19 -0
  272. package/gallery/data/traces/shop/pricing.mjs +12 -0
  273. package/gallery/data/traces/shop/status.mjs +9 -0
  274. package/gallery/data/traces/web/app.js +11 -0
  275. package/gallery/data/traces/web/index.html +1 -0
  276. package/gallery/data/traces/web/search.js +10 -0
  277. package/gallery/data/transcript/call-noisy.wav +0 -0
  278. package/gallery/data/transcript/call.json +362 -0
  279. package/gallery/data/transcript/call.wav +0 -0
  280. package/gallery/data/transcript/mlx-whisper-tiny.en-noisy.json +1 -0
  281. package/gallery/data/transcript/whisper-cpp-base.en-noisy.json +658 -0
  282. package/gallery/data/transcript/whisper-cpp-base.en.json +658 -0
  283. package/gallery/data/transcript/whisper-cpp-base.en.srt +16 -0
  284. package/gallery/data/transcript/whisper-cpp-tiny.en-noisy.json +619 -0
  285. package/gallery/data/tsc.txt +4 -0
  286. package/gallery/data/vitest-bench.json +119 -0
  287. package/gallery/data/vitest-junit.xml +133 -0
  288. package/gallery/data/vitest-report.json +612 -0
  289. package/gallery/images/bell.svg +10 -0
  290. package/gallery/images/bike.svg +47 -0
  291. package/gallery/images/footer-planet.webp +0 -0
  292. package/gallery/images/front-light.svg +13 -0
  293. package/gallery/images/golden-hour.webp +0 -0
  294. package/gallery/images/option-flowers.webp +0 -0
  295. package/gallery/images/option-hilltop.webp +0 -0
  296. package/gallery/images/option-meadow.webp +0 -0
  297. package/gallery/images/option-sky.webp +0 -0
  298. package/gallery/images/u-lock.svg +10 -0
  299. package/index.client.tsx +164 -0
  300. package/index.server.ts +216 -0
  301. package/package.json +50 -4
  302. package/paseo-plugin.json +14 -0
  303. package/server/data-source.ts +407 -0
  304. package/server/project.ts +981 -0
  305. package/server/web/address.ts +57 -0
  306. package/server/web/cache.ts +85 -0
  307. package/server/web/commons.ts +126 -0
  308. package/server/web/geocode.ts +40 -0
  309. package/server/web/image.ts +35 -0
  310. package/server/web/lookups.ts +248 -0
  311. package/server/web/net.ts +210 -0
  312. package/shared/core/acceptance.ts +744 -0
  313. package/shared/core/ast.ts +76 -0
  314. package/shared/core/benchmark.ts +743 -0
  315. package/shared/core/catalog.ts +733 -0
  316. package/shared/core/compare.ts +225 -0
  317. package/shared/core/coverage.ts +456 -0
  318. package/shared/core/data.ts +334 -0
  319. package/shared/core/decisions.ts +527 -0
  320. package/shared/core/dependencies.ts +604 -0
  321. package/shared/core/diagnostics.ts +24 -0
  322. package/shared/core/environments.ts +212 -0
  323. package/shared/core/evaluate.ts +737 -0
  324. package/shared/core/fields.ts +66 -0
  325. package/shared/core/findings.ts +1172 -0
  326. package/shared/core/form.ts +69 -0
  327. package/shared/core/index.ts +44 -0
  328. package/shared/core/junit.ts +252 -0
  329. package/shared/core/lexer.ts +275 -0
  330. package/shared/core/logs.ts +496 -0
  331. package/shared/core/markdown.ts +2374 -0
  332. package/shared/core/parser.ts +355 -0
  333. package/shared/core/perf-units.ts +228 -0
  334. package/shared/core/pipeline.ts +679 -0
  335. package/shared/core/project.ts +472 -0
  336. package/shared/core/provenance.ts +725 -0
  337. package/shared/core/stack-trace.ts +718 -0
  338. package/shared/core/streaming.ts +136 -0
  339. package/shared/core/test-results.ts +562 -0
  340. package/shared/core/thread-activity.ts +203 -0
  341. package/shared/core/tracking.ts +887 -0
  342. package/shared/core/transcript.ts +1364 -0
  343. package/shared/core/treemap.ts +446 -0
  344. package/shared/core/types.ts +70 -0
  345. package/shared/core/validate.ts +574 -0
  346. package/shared/core/waterfall.ts +536 -0
  347. package/shared/diagram/flowchart.ts +205 -0
  348. package/shared/diagram/layout.ts +440 -0
  349. package/shared/diagram/markdown.ts +135 -0
  350. package/shared/diagram/sequence.ts +122 -0
  351. package/shared/diagram/zoom.ts +114 -0
  352. package/shared/fallback.ts +120 -0
  353. package/shared/genui.test.ts +122 -0
  354. package/shared/instructions.ts +144 -0
  355. package/shared/logic/calendarMath.ts +130 -0
  356. package/shared/logic/chartMath.ts +289 -0
  357. package/shared/logic/choose-logic.ts +196 -0
  358. package/shared/logic/compare-logic.ts +270 -0
  359. package/shared/logic/content-model.ts +227 -0
  360. package/shared/logic/environments-logic.ts +53 -0
  361. package/shared/logic/explain-logic.ts +315 -0
  362. package/shared/logic/fieldLogic.ts +138 -0
  363. package/shared/logic/gallery-search.ts +71 -0
  364. package/shared/logic/icons.ts +322 -0
  365. package/shared/logic/json-logic.ts +153 -0
  366. package/shared/logic/mapMath.ts +133 -0
  367. package/shared/logic/project-logic.ts +39 -0
  368. package/shared/logic/references-model.ts +201 -0
  369. package/shared/logic/sketch.ts +165 -0
  370. package/shared/logic/supersession.ts +77 -0
  371. package/shared/logic/values.ts +133 -0
  372. package/shared/logic/widget-logic.ts +163 -0
  373. package/shared/rpc.ts +252 -0
  374. package/shared/segment.ts +81 -0
  375. package/shared/settings.ts +12 -0
@@ -0,0 +1,562 @@
1
+ // What `Tests` shows, from the three shapes it takes: a plain list
2
+ // (`[{name, status, message?, file?, line?, ms?}]`, which is also what a
3
+ // JUnit `.xml` Load reads into), a vitest or jest JSON reporter object, and
4
+ // the suites a very large JUnit report becomes. Also the pieces of a failure
5
+ // message the renderer and the markdown fallback share: ANSI colour codes and
6
+ // stack frames. Pure: no React, no DOM.
7
+ import type { Value } from "./types";
8
+
9
+ export type TestStatus = "passed" | "failed" | "skipped";
10
+
11
+ export interface TestCase {
12
+ readonly name: string;
13
+ readonly status: TestStatus;
14
+ /** The reporter's own word where it says more than the status: "error", "todo", "pending", "xfail". */
15
+ readonly detail: string | null;
16
+ /** The describe block, class or module; for JS reports the file. */
17
+ readonly suite: string | null;
18
+ readonly file: string | null;
19
+ readonly line: number | null;
20
+ readonly ms: number | null;
21
+ readonly message: string | null;
22
+ /** Captured stdout/stderr, kept for failures. */
23
+ readonly output: string | null;
24
+ }
25
+
26
+ export interface TestRun {
27
+ readonly tests: readonly TestCase[];
28
+ readonly counts: Readonly<Record<TestStatus, number>>;
29
+ /** The whole run in ms: the report's wall clock where it has one, else the tests' sum; null when nothing is timed. */
30
+ readonly ms: number | null;
31
+ /** List items that were not tests (no name, or no status a test can have). */
32
+ readonly ignored: number;
33
+ }
34
+
35
+ export type TestRunResult = { readonly ok: true; readonly run: TestRun } | { readonly ok: false; readonly error: string };
36
+
37
+ /** A plain data object's fields; absent keys read as undefined. */
38
+ type Fields = Readonly<Record<string, Value | undefined>>;
39
+
40
+ function isList(value: Value | undefined): value is readonly Value[] {
41
+ return Array.isArray(value);
42
+ }
43
+
44
+ /** `value` as a plain data object, or null for scalars, lists and the interpreter's node/action/binding shapes. */
45
+ function fieldsOf(value: Value | undefined): Fields | null {
46
+ if (typeof value !== "object" || value === null || isList(value)) return null;
47
+ if ("kind" in value && (value.kind === "node" || value.kind === "action" || value.kind === "binding")) return null;
48
+ // Only plain data objects are left; TypeScript cannot drop the interpreter's interfaces by a `kind` an index signature also has.
49
+ const fields = value as Fields;
50
+ return fields;
51
+ }
52
+
53
+ function str(value: Value | undefined): string | null {
54
+ if (typeof value === "string") return value.trim() === "" ? null : value;
55
+ if (typeof value === "number" && Number.isFinite(value)) return String(value);
56
+ return null;
57
+ }
58
+
59
+ function num(value: Value | undefined): number | null {
60
+ if (typeof value === "number" && Number.isFinite(value)) return value;
61
+ if (typeof value === "string" && /^\s*-?\d+(\.\d+)?\s*$/.test(value)) return Number(value);
62
+ return null;
63
+ }
64
+
65
+ function positiveInteger(value: Value | undefined): number | null {
66
+ const n = num(value);
67
+ return n !== null && Number.isSafeInteger(n) && n > 0 ? n : null;
68
+ }
69
+
70
+ const STATUS_WORDS: Readonly<Record<string, readonly [TestStatus, string | null]>> = {
71
+ pass: ["passed", null],
72
+ passed: ["passed", null],
73
+ passing: ["passed", null],
74
+ ok: ["passed", null],
75
+ success: ["passed", null],
76
+ succeeded: ["passed", null],
77
+ successful: ["passed", null],
78
+ xpass: ["passed", "xpass"],
79
+ xpassed: ["passed", "xpass"],
80
+ // jest: a `.only` test, which ran and passed.
81
+ focused: ["passed", null],
82
+ fail: ["failed", null],
83
+ failed: ["failed", null],
84
+ failing: ["failed", null],
85
+ failure: ["failed", null],
86
+ error: ["failed", "error"],
87
+ errored: ["failed", "error"],
88
+ broken: ["failed", "broken"],
89
+ crashed: ["failed", "crashed"],
90
+ timedout: ["failed", "timed out"],
91
+ timeout: ["failed", "timed out"],
92
+ skip: ["skipped", null],
93
+ skipped: ["skipped", null],
94
+ pending: ["skipped", "pending"],
95
+ todo: ["skipped", "todo"],
96
+ disabled: ["skipped", "disabled"],
97
+ ignored: ["skipped", "ignored"],
98
+ excluded: ["skipped", "excluded"],
99
+ xfail: ["skipped", "xfail"],
100
+ xfailed: ["skipped", "xfail"],
101
+ };
102
+
103
+ /** A status word as a status, its detail kept when it says more; null when it is no test status. */
104
+ export function testStatus(value: Value | undefined): { readonly status: TestStatus; readonly detail: string | null } | null {
105
+ if (typeof value === "boolean") return { status: value ? "passed" : "failed", detail: null };
106
+ if (typeof value !== "string") return null;
107
+ const word = value.trim().toLowerCase().replace(/[\s_-]+/g, "");
108
+ if (!Object.hasOwn(STATUS_WORDS, word)) return null;
109
+ const [status, detail] = STATUS_WORDS[word];
110
+ return { status, detail };
111
+ }
112
+
113
+ /** `a.ts:12` or `a.ts:12:3` → path and line; anything else is all path. */
114
+ function splitLocation(raw: string): { readonly file: string; readonly line: number | null } {
115
+ const match = /^(.*?):(\d+)(?::\d+)?$/.exec(raw);
116
+ return match === null || match[1] === "" ? { file: raw, line: null } : { file: match[1], line: Number(match[2]) };
117
+ }
118
+
119
+ /** An error value as text: a string, a list of strings, or `{stack|message}`. */
120
+ function errorText(value: Value | undefined): string | null {
121
+ if (typeof value === "string") return str(value);
122
+ if (Array.isArray(value)) {
123
+ const parts = value.map(errorText).filter((part): part is string => part !== null);
124
+ return parts.length === 0 ? null : parts.join("\n\n");
125
+ }
126
+ const error = fieldsOf(value);
127
+ return error === null ? null : (str(error.stack) ?? str(error.message));
128
+ }
129
+
130
+ /** One test from a plain list item; null when it is not one. */
131
+ function listTest(value: Value, group: { suite: string | null; file: string | null }): TestCase | null {
132
+ const item = fieldsOf(value);
133
+ if (item === null) return null;
134
+ const name = str(item.name) ?? str(item.title) ?? str(item.fullName) ?? str(item.test);
135
+ const status = testStatus(item.status ?? item.state ?? item.outcome ?? item.result ?? item.ok ?? item.passed);
136
+ if (name === null || status === null) return null;
137
+ const location = str(item.file) ?? str(item.path) ?? (typeof item.location === "string" ? str(item.location) : null);
138
+ const split = location === null ? null : splitLocation(location);
139
+ const locationLine = positiveInteger(fieldsOf(item.location)?.line);
140
+ return {
141
+ name,
142
+ status: status.status,
143
+ detail: status.detail,
144
+ suite: str(item.suite) ?? str(item.classname) ?? str(item.describe) ?? group.suite,
145
+ file: split?.file ?? group.file,
146
+ line: positiveInteger(item.line) ?? split?.line ?? locationLine,
147
+ ms: num(item.ms) ?? num(item.duration) ?? num(item.durationMs),
148
+ message: errorText(item.message) ?? errorText(item.error) ?? errorText(item.failureMessages) ?? errorText(item.failureMessage),
149
+ output: str(item.output) ?? str(item.stdout),
150
+ };
151
+ }
152
+
153
+ function fromList(items: readonly Value[]): { tests: TestCase[]; ignored: number } {
154
+ const tests: TestCase[] = [];
155
+ let ignored = 0;
156
+ for (const item of items) {
157
+ // A group: `{suite|name|file, tests: [...]}` (what a very large JUnit report becomes).
158
+ const fields = fieldsOf(item);
159
+ if (fields !== null && Array.isArray(fields.tests)) {
160
+ const file = str(fields.file);
161
+ const group = { suite: str(fields.suite) ?? str(fields.name) ?? file, file };
162
+ for (const test of fields.tests) {
163
+ const read = listTest(test, group);
164
+ if (read === null) ignored++;
165
+ else tests.push(read);
166
+ }
167
+ continue;
168
+ }
169
+ const read = listTest(item, { suite: null, file: null });
170
+ if (read === null) ignored++;
171
+ else tests.push(read);
172
+ }
173
+ return { tests, ignored };
174
+ }
175
+
176
+ /** vitest's and jest's JSON reporter: `testResults[]` per file, each with `assertionResults[]`. */
177
+ function fromJestReport(report: Fields): { tests: TestCase[]; ms: number | null } {
178
+ const tests: TestCase[] = [];
179
+ let earliest = num(report.startTime);
180
+ let latest: number | null = null;
181
+ let fileTime = 0;
182
+ for (const entry of Array.isArray(report.testResults) ? report.testResults : []) {
183
+ const result = fieldsOf(entry);
184
+ if (result === null) continue;
185
+ const file = str(result.name) ?? str(result.testFilePath);
186
+ const start = num(result.startTime);
187
+ const end = num(result.endTime);
188
+ if (start !== null && end !== null && end >= start) {
189
+ fileTime += end - start;
190
+ earliest = earliest === null ? start : Math.min(earliest, start);
191
+ latest = latest === null ? end : Math.max(latest, end);
192
+ }
193
+ const assertions = Array.isArray(result.assertionResults) ? result.assertionResults : [];
194
+ let failed = false;
195
+ for (const entry of assertions) {
196
+ const assertion = fieldsOf(entry);
197
+ if (assertion === null) continue;
198
+ const status = testStatus(assertion.status);
199
+ const title = str(assertion.title);
200
+ const ancestors = Array.isArray(assertion.ancestorTitles) ? assertion.ancestorTitles.map(str).filter((t) => t !== null) : [];
201
+ const name = title !== null && ancestors.length > 0 ? [...ancestors, title].join(" › ") : (title ?? str(assertion.fullName));
202
+ if (status === null || name === null) continue;
203
+ failed ||= status.status === "failed";
204
+ tests.push({
205
+ name,
206
+ status: status.status,
207
+ detail: status.detail,
208
+ suite: file,
209
+ file,
210
+ line: positiveInteger(fieldsOf(assertion.location)?.line),
211
+ ms: num(assertion.duration),
212
+ message: errorText(assertion.failureMessages) ?? errorText(assertion.failureDetails),
213
+ output: null,
214
+ });
215
+ }
216
+ // A file that failed to load (syntax error, failing hook) has a message and no failed test to carry it.
217
+ const message = str(result.message) ?? str(result.failureMessage);
218
+ if (!failed && message !== null && (result.status === "failed" || assertions.length === 0)) {
219
+ tests.push({ name: "(the file failed to run)", status: "failed", detail: null, suite: file, file, line: null, ms: null, message, output: null });
220
+ }
221
+ }
222
+ const wall = earliest !== null && latest !== null && latest >= earliest ? latest - earliest : null;
223
+ return { tests, ms: wall ?? (fileTime > 0 ? fileTime : null) };
224
+ }
225
+
226
+ const SHAPE_HINT = "Tests needs a list of {name, status}, a vitest/jest JSON report or a JUnit XML report";
227
+
228
+ /** The run `value` describes, failures located by their stack where the report gives no line. */
229
+ export function readTestRun(value: Value | undefined): TestRunResult {
230
+ let tests: TestCase[];
231
+ let ms: number | null = null;
232
+ let ignored = 0;
233
+ if (Array.isArray(value)) {
234
+ ({ tests, ignored } = fromList(value));
235
+ if (tests.length === 0 && value.length > 0) return { ok: false, error: "Tests items need a name and a status (passed, failed or skipped)" };
236
+ } else {
237
+ const report = fieldsOf(value);
238
+ if (report !== null && Array.isArray(report.testResults)) ({ tests, ms } = fromJestReport(report));
239
+ else if (report !== null && Array.isArray(report.tests)) ({ tests, ignored } = fromList(report.tests));
240
+ else return { ok: false, error: SHAPE_HINT };
241
+ }
242
+ const located = tests.map(locate);
243
+ const counts = { passed: 0, failed: 0, skipped: 0 };
244
+ let sum = 0;
245
+ let timed = false;
246
+ for (const test of located) {
247
+ counts[test.status]++;
248
+ if (test.ms !== null) {
249
+ sum += test.ms;
250
+ timed = true;
251
+ }
252
+ }
253
+ return { ok: true, run: { tests: located, counts, ms: ms ?? (timed ? sum : null), ignored } };
254
+ }
255
+
256
+ /** A failure without a line takes it from its first app frame in the test's file (or any app frame when the test names no file). */
257
+ function locate(test: TestCase): TestCase {
258
+ if (test.status !== "failed" || test.line !== null || test.message === null) return test;
259
+ const frame = firstAppFrame(stripAnsi(test.message).split("\n"));
260
+ if (frame === null || !frame.linkable) return test;
261
+ if (test.file === null) return { ...test, file: frame.path, line: frame.line };
262
+ return samePath(test.file, frame.path) ? { ...test, line: frame.line } : test;
263
+ }
264
+
265
+ function samePath(a: string, b: string): boolean {
266
+ const x = a.replace(/^file:\/\//, "").replace(/^\.\//, "");
267
+ const y = b.replace(/^file:\/\//, "").replace(/^\.\//, "");
268
+ return x === y || x.endsWith(`/${y}`) || y.endsWith(`/${x}`);
269
+ }
270
+
271
+ // ---------------------------------------------------------------------------
272
+ // Grouping
273
+
274
+ export interface TestGroup {
275
+ /** The suite or file the tests share; "Tests" when they name neither. */
276
+ readonly label: string;
277
+ readonly file: string | null;
278
+ readonly tests: readonly TestCase[];
279
+ readonly counts: Readonly<Record<TestStatus, number>>;
280
+ readonly ms: number | null;
281
+ }
282
+
283
+ /** Tests grouped by suite (else file), in order of first appearance. */
284
+ export function groupTests(tests: readonly TestCase[]): TestGroup[] {
285
+ const groups = new Map<string, { label: string; file: string | null; tests: TestCase[] }>();
286
+ for (const test of tests) {
287
+ const label = test.suite ?? test.file ?? "Tests";
288
+ let group = groups.get(label);
289
+ if (group === undefined) {
290
+ group = { label, file: test.file, tests: [] };
291
+ groups.set(label, group);
292
+ } else if (group.file !== test.file) {
293
+ group.file = null;
294
+ }
295
+ group.tests.push(test);
296
+ }
297
+ return [...groups.values()].map((group) => {
298
+ const counts = { passed: 0, failed: 0, skipped: 0 };
299
+ let ms: number | null = null;
300
+ for (const test of group.tests) {
301
+ counts[test.status]++;
302
+ if (test.ms !== null) ms = (ms ?? 0) + test.ms;
303
+ }
304
+ return { ...group, counts, ms };
305
+ });
306
+ }
307
+
308
+ /** "3 failed, 45 passed, 2 skipped": failures first, zero counts left out, "no tests" when empty. */
309
+ export function countsText(counts: Readonly<Record<TestStatus, number>>): string {
310
+ const parts = (["failed", "passed", "skipped"] as const).filter((status) => counts[status] > 0).map((status) => `${counts[status]} ${status}`);
311
+ return parts.length === 0 ? "no tests" : parts.join(", ");
312
+ }
313
+
314
+ /** `0.4 ms`, `12 ms`, `1.24 s`, `14.2 s`, `2m 05s`. */
315
+ export function formatDuration(ms: number): string {
316
+ if (ms < 1) return ms <= 0 ? "0 ms" : `${Math.round(ms * 10) / 10} ms`;
317
+ if (ms < 1000) return `${Math.round(ms)} ms`;
318
+ if (ms < 10_000) return `${(ms / 1000).toFixed(2)} s`;
319
+ if (ms < 60_000) return `${(ms / 1000).toFixed(1)} s`;
320
+ const seconds = Math.round(ms / 1000);
321
+ const hours = Math.floor(seconds / 3600);
322
+ const minutes = Math.floor((seconds % 3600) / 60);
323
+ const rest = String(seconds % 60).padStart(2, "0");
324
+ return hours > 0 ? `${hours}h ${String(minutes).padStart(2, "0")}m` : `${minutes}m ${rest}s`;
325
+ }
326
+
327
+ // ---------------------------------------------------------------------------
328
+ // ANSI colour codes
329
+
330
+ export type AnsiColor = "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "dim";
331
+
332
+ export interface AnsiSegment {
333
+ readonly text: string;
334
+ readonly color: AnsiColor | null;
335
+ readonly bold: boolean;
336
+ /** Reverse video, which jest uses to mark the changed characters of a diff. */
337
+ readonly inverse: boolean;
338
+ }
339
+
340
+ /** CSI sequences (colours, cursor moves, erases), OSC sequences (titles, links) and lone two-byte escapes. */
341
+ const ESCAPE = /(?:\x1b\[|\x9b)([0-?]*)[ -/]*([@-~])|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?|\x1b[@-Z\\-_]?/g;
342
+
343
+ const BASIC: readonly (AnsiColor | null)[] = ["dim", "red", "green", "yellow", "blue", "magenta", "cyan", null];
344
+
345
+ export function stripAnsi(text: string): string {
346
+ return text.includes("\x1b") || text.includes("\x9b") ? text.replace(ESCAPE, "") : text;
347
+ }
348
+
349
+ /** `text` split at its SGR colour codes into styled runs; every other escape is dropped. */
350
+ export function ansiSegments(text: string): AnsiSegment[] {
351
+ const segments: AnsiSegment[] = [];
352
+ let color: AnsiColor | null = null;
353
+ let bold = false;
354
+ let inverse = false;
355
+ let dim = false;
356
+ let last = 0;
357
+ const push = (end: number) => {
358
+ if (end > last) {
359
+ const piece = text.slice(last, end);
360
+ const style = { color: color ?? (dim ? "dim" : null), bold, inverse };
361
+ const previous = segments[segments.length - 1];
362
+ if (previous !== undefined && previous.color === style.color && previous.bold === bold && previous.inverse === inverse) {
363
+ segments[segments.length - 1] = { ...previous, text: previous.text + piece };
364
+ } else {
365
+ segments.push({ text: piece, ...style });
366
+ }
367
+ }
368
+ };
369
+ for (const match of text.matchAll(ESCAPE)) {
370
+ push(match.index);
371
+ last = match.index + match[0].length;
372
+ if (match[2] !== "m") continue;
373
+ const codes = (match[1] ?? "").split(";").map((code) => (code === "" ? 0 : Number(code)));
374
+ for (let at = 0; at < codes.length; at++) {
375
+ const code = codes[at];
376
+ if (code === 0) {
377
+ color = null;
378
+ bold = dim = inverse = false;
379
+ } else if (code === 1) bold = true;
380
+ else if (code === 2) dim = true;
381
+ else if (code === 22) bold = dim = false;
382
+ else if (code === 7) inverse = true;
383
+ else if (code === 27) inverse = false;
384
+ else if (code >= 30 && code <= 37) color = BASIC[code - 30];
385
+ else if (code >= 90 && code <= 97) color = BASIC[code - 90];
386
+ else if (code === 39) color = null;
387
+ else if (code === 38 || code === 48) {
388
+ // 38;5;n and 38;2;r;g;b: the 16 basic indexes keep their colour; the rest read as plain text.
389
+ const mode = codes[at + 1];
390
+ if (code === 38 && mode === 5) {
391
+ const index = codes[at + 2];
392
+ color = index !== undefined && index < 16 ? BASIC[index % 8] : null;
393
+ } else if (code === 38) {
394
+ color = null;
395
+ }
396
+ at += mode === 5 ? 2 : mode === 2 ? 4 : 1;
397
+ }
398
+ }
399
+ }
400
+ push(text.length);
401
+ return segments;
402
+ }
403
+
404
+ /** ansiSegments cut into lines, each line keeping the style that was open across the break. */
405
+ export function ansiLines(text: string): AnsiSegment[][] {
406
+ const lines: AnsiSegment[][] = [[]];
407
+ for (const segment of ansiSegments(text)) {
408
+ const parts = segment.text.split("\n");
409
+ parts.forEach((part, index) => {
410
+ if (index > 0) lines.push([]);
411
+ if (part !== "") lines[lines.length - 1].push({ ...segment, text: part.replace(/\r$/, "") });
412
+ });
413
+ }
414
+ return lines;
415
+ }
416
+
417
+ // ---------------------------------------------------------------------------
418
+ // Stack frames
419
+
420
+ export interface StackFrame {
421
+ /** As the trace wrote it (absolute, relative or `file://`). */
422
+ readonly path: string;
423
+ readonly line: number;
424
+ readonly column: number | null;
425
+ /** Not a dependency, the runtime or the test runner: the code under test or the test itself. */
426
+ readonly app: boolean;
427
+ /** False for frames that name only a file (Java's `Foo.java:12`), which no workspace path matches. */
428
+ readonly linkable: boolean;
429
+ }
430
+
431
+ /** The file extensions a trace or log names source files by (a regex alternation). */
432
+ export const CODE_EXTENSIONS = "[cm]?[jt]sx?|py|go|rs|rb|php|cs|swift|kt|kts|java|scala|ex|exs|erl|c|cc|cpp|cxx|h|hpp|m|mm|dart|lua|vue|svelte|astro";
433
+
434
+ const FRAME_PATTERNS: readonly RegExp[] = [
435
+ // V8 `at fn (path:1:2)` / `at path:1:2`; vitest's ` ❯ path:1:2`
436
+ new RegExp(`^\\s*(?:at|❯)\\s+(?:.*?\\s\\(|.*?\\s)?((?:file:\\/\\/)?[^\\s()]+?\\.(?:${CODE_EXTENSIONS})):(\\d+)(?::(\\d+))?\\)?\\s*$`),
437
+ // Python `File "path", line 12, in fn`
438
+ /^\s*File "([^"]+)", line (\d+)()/,
439
+ // C# `at Ns.Type.Method() in /path/File.cs:line 42`
440
+ /\sin\s(\S.*?\.cs):line (\d+)()/,
441
+ // Rust `--> src/lib.rs:12:5`, `panicked at src/lib.rs:12:5:`
442
+ /(?:-->|panicked at)\s+(\S+?\.rs):(\d+)(?::(\d+))?/,
443
+ // Firefox and Safari `fn@/abs/a.js:1:2` / `fn@file:///abs/a.js:1:2`
444
+ new RegExp(`^\\s*[^\\s@]*@((?:file:\\/\\/)?\\/[^\\s@]*?\\.(?:${CODE_EXTENSIONS})):(\\d+)(?::(\\d+))?\\s*$`),
445
+ // pytest `tests/test_api.py:22: AssertionError`, go ` foo_test.go:12: msg`, go frames, ruby `from app.rb:3:in 'm'`, `path:1:2` lines
446
+ new RegExp(`^\\s*(?:from\\s+)?((?:file:\\/\\/)?[^\\s:"'()]+\\.(?:${CODE_EXTENSIONS})):(\\d+)(?::(\\d+))?(?=$|[:\\s])`),
447
+ ];
448
+
449
+ /**
450
+ * A script served by a dev server or a bundler, in a browser trace: V8's
451
+ * `at fn (http://localhost:5173/src/a.ts?t=1:12:5)`, Firefox and Safari's
452
+ * `fn@http://…/a.js:1:2`, webpack's `webpack-internal:///./src/a.tsx:3:4`.
453
+ * The URL's path is the file's path in the app (Vite's `/@fs/` marks an absolute one).
454
+ */
455
+ const URL_FRAME = new RegExp(
456
+ `(?:^\\s*at\\s+(?:.*?\\s\\()?|^\\s*[^\\s@]*@)(?:https?:\\/\\/[^\\s/()]+|webpack(?:-internal)?:\\/\\/[^\\s/()]*)(\\/[^\\s()?#]*?\\.(?:${CODE_EXTENSIONS}))(?:[?#][^\\s()]*?)?:(\\d+)(?::(\\d+))?\\)?\\s*$`,
457
+ );
458
+
459
+ function servedPath(urlPath: string): string {
460
+ const path = decodeURIComponentSafe(urlPath);
461
+ return path.startsWith("/@fs/") ? path.slice(4) : path.slice(1);
462
+ }
463
+
464
+ /** Java/Kotlin/Scala `at pkg.Cls.method(Cls.java:12)`: only a file name. */
465
+ const JVM_FRAME = /^\s*at\s+([\w$.<>/]+)\(([\w$.-]+\.(?:java|kt|kts|scala|groovy|clj)):(\d+)\)/;
466
+ /** The JDK, the JVM languages' runtimes, test runners and the frameworks and drivers most traces pass through. */
467
+ export const JVM_LIBRARY =
468
+ /^(?:[\w.]+\/)?(?:java|javax|jakarta|jdk|sun|com\.sun|kotlin|kotlinx|scala|org\.junit|junit|org\.testng|org\.opentest4j|org\.apache|org\.gradle|org\.spockframework|io\.kotest|org\.springframework|org\.hibernate|org\.postgresql|com\.mysql|com\.zaxxer|io\.netty|io\.micrometer|reactor|okhttp3|retrofit2|com\.fasterxml|com\.google|org\.eclipse|org\.jboss|io\.undertow|android|androidx|dalvik|com\.android)\./;
469
+
470
+ const LIBRARY_PATH =
471
+ /node_modules\/|^node:|^internal\/|\(internal\/|<anonymous>|<frozen |\/site-packages\/|\/dist-packages\/|\/lib\/python\d|(?:^|\/)_pytest\/|(?:^|\/)pluggy\/|^\/rustc\/|\/rustlib\/|\/\.cargo\/registry\/|\/\.rustup\/|\/go\/src\/|\/libexec\/src\/|\/pkg\/mod\/|^\/usr\/(?:lib|local\/lib|share)\/|\/gems\/|\/lib\/ruby\/|^Swift\//;
472
+
473
+ /** The frame a line of a trace names, or null. */
474
+ export function stackFrame(line: string): StackFrame | null {
475
+ const jvm = JVM_FRAME.exec(line);
476
+ if (jvm !== null) {
477
+ return { path: jvm[2], line: Number(jvm[3]), column: null, app: !JVM_LIBRARY.test(jvm[1]), linkable: false };
478
+ }
479
+ const served = URL_FRAME.exec(line);
480
+ const match = served ?? FRAME_PATTERNS.reduce<RegExpExecArray | null>((found, pattern) => found ?? pattern.exec(line), null);
481
+ if (match === null) return null;
482
+ const lineNumber = Number(match[2]);
483
+ if (!Number.isSafeInteger(lineNumber) || lineNumber < 1) return null;
484
+ const path = served === null ? match[1] : servedPath(match[1]);
485
+ return {
486
+ path,
487
+ line: lineNumber,
488
+ column: match[3] ? Number(match[3]) : null,
489
+ app: !LIBRARY_PATH.test(path),
490
+ linkable: true,
491
+ };
492
+ }
493
+
494
+ /** A trace line that is no part of the code under test: a dependency's or runtime's frame, or V8's `at new Promise (<anonymous>)`. */
495
+ export function libraryFrame(line: string): boolean {
496
+ const frame = stackFrame(line);
497
+ if (frame !== null) return !frame.app;
498
+ return /^\s*at\s.*(?:<anonymous>|\(native\)|\(index \d+\)|\bnode:)|@\[native code\]\s*$/.test(line);
499
+ }
500
+
501
+ /** The first frame in `lines` that is the code under test (not a dependency or the runner), with its line index. */
502
+ export function firstAppFrame(lines: readonly string[]): (StackFrame & { readonly index: number }) | null {
503
+ for (let index = 0; index < lines.length; index++) {
504
+ const frame = stackFrame(lines[index]);
505
+ if (frame?.app) return { ...frame, index };
506
+ }
507
+ return null;
508
+ }
509
+
510
+ // ---------------------------------------------------------------------------
511
+ // Paths
512
+
513
+ /**
514
+ * The workspace-relative path BB can open for a path from a report, or null:
515
+ * `file://` and `./` dropped, absolute paths made relative to `root` (null
516
+ * outside it or without a root), `..` refused.
517
+ */
518
+ export function workspacePath(path: string, root: string | null): string | null {
519
+ let target = path.startsWith("file://") ? decodeURIComponentSafe(path.slice(7)) : path;
520
+ if (target.startsWith("/")) {
521
+ if (root === null) return null;
522
+ const base = root.endsWith("/") ? root : `${root}/`;
523
+ if (!target.startsWith(base)) return null;
524
+ target = target.slice(base.length);
525
+ }
526
+ target = target.replace(/^(?:\.\/)+/, "");
527
+ if (target === "" || target.includes("\\") || target.split("/").some((part) => part === "" || part === "." || part === "..")) return null;
528
+ return target;
529
+ }
530
+
531
+ function decodeURIComponentSafe(text: string): string {
532
+ try {
533
+ return decodeURIComponent(text);
534
+ } catch {
535
+ return text;
536
+ }
537
+ }
538
+
539
+ /** The longest directory (ending in `/`) every absolute path in `paths` starts with; "" when there is none. */
540
+ export function commonDirectory(paths: Iterable<string>): string {
541
+ let common: string | null = null;
542
+ for (const path of paths) {
543
+ if (!path.startsWith("/")) continue;
544
+ const directory = path.slice(0, path.lastIndexOf("/") + 1);
545
+ if (common === null) {
546
+ common = directory;
547
+ continue;
548
+ }
549
+ let length = 0;
550
+ while (length < common.length && length < directory.length && common[length] === directory[length]) length++;
551
+ common = common.slice(0, common.lastIndexOf("/", length - 1) + 1);
552
+ }
553
+ return common ?? "";
554
+ }
555
+
556
+ /** A path as shown: relative to `root` when inside it, else with `common` (see commonDirectory) cut off. */
557
+ export function displayPath(path: string, root: string | null, common: string): string {
558
+ const relative = workspacePath(path, root);
559
+ if (relative !== null) return relative;
560
+ const plain = path.startsWith("file://") ? decodeURIComponentSafe(path.slice(7)) : path;
561
+ return common !== "" && plain.startsWith(common) && plain.length > common.length ? plain.slice(common.length) : plain;
562
+ }