@rangojs/router 0.0.0-experimental.32 → 0.0.0-experimental.3232cd17

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 (376) hide show
  1. package/AGENTS.md +4 -0
  2. package/README.md +198 -44
  3. package/dist/bin/rango.js +287 -105
  4. package/dist/testing/vitest.js +82 -0
  5. package/dist/vite/index.js +3248 -1117
  6. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  7. package/package.json +73 -21
  8. package/skills/api-client/SKILL.md +211 -0
  9. package/skills/breadcrumbs/SKILL.md +107 -1
  10. package/skills/bundle-analysis/SKILL.md +159 -0
  11. package/skills/cache-guide/SKILL.md +245 -21
  12. package/skills/caching/SKILL.md +302 -6
  13. package/skills/composability/SKILL.md +27 -2
  14. package/skills/css/SKILL.md +76 -0
  15. package/skills/document-cache/SKILL.md +78 -55
  16. package/skills/handler-use/SKILL.md +364 -0
  17. package/skills/hooks/SKILL.md +270 -30
  18. package/skills/host-router/SKILL.md +82 -22
  19. package/skills/i18n/SKILL.md +276 -0
  20. package/skills/intercept/SKILL.md +49 -5
  21. package/skills/layout/SKILL.md +35 -9
  22. package/skills/links/SKILL.md +249 -17
  23. package/skills/loader/SKILL.md +294 -30
  24. package/skills/middleware/SKILL.md +52 -13
  25. package/skills/migrate-nextjs/SKILL.md +584 -0
  26. package/skills/migrate-react-router/SKILL.md +769 -0
  27. package/skills/mime-routes/SKILL.md +27 -0
  28. package/skills/observability/SKILL.md +137 -0
  29. package/skills/parallel/SKILL.md +203 -7
  30. package/skills/prerender/SKILL.md +123 -100
  31. package/skills/rango/SKILL.md +250 -22
  32. package/skills/react-compiler/SKILL.md +168 -0
  33. package/skills/response-routes/SKILL.md +122 -47
  34. package/skills/route/SKILL.md +97 -5
  35. package/skills/router-setup/SKILL.md +90 -5
  36. package/skills/server-actions/SKILL.md +775 -0
  37. package/skills/streams-and-websockets/SKILL.md +283 -0
  38. package/skills/tailwind/SKILL.md +27 -3
  39. package/skills/testing/SKILL.md +129 -0
  40. package/skills/testing/bindings.md +89 -0
  41. package/skills/testing/cache-prerender.md +124 -0
  42. package/skills/testing/client-components.md +122 -0
  43. package/skills/testing/e2e-parity.md +125 -0
  44. package/skills/testing/flight.md +92 -0
  45. package/skills/testing/handles.md +129 -0
  46. package/skills/testing/loader.md +128 -0
  47. package/skills/testing/middleware.md +99 -0
  48. package/skills/testing/render-handler.md +121 -0
  49. package/skills/testing/response-routes.md +95 -0
  50. package/skills/testing/reverse-and-types.md +84 -0
  51. package/skills/testing/server-actions.md +107 -0
  52. package/skills/testing/server-tree.md +128 -0
  53. package/skills/testing/setup.md +120 -0
  54. package/skills/typesafety/SKILL.md +329 -27
  55. package/skills/use-cache/SKILL.md +36 -5
  56. package/skills/view-transitions/SKILL.md +294 -0
  57. package/src/__augment-tests__/augment.ts +81 -0
  58. package/src/__augment-tests__/augmented.check.ts +116 -0
  59. package/src/__internal.ts +67 -40
  60. package/src/browser/action-coordinator.ts +53 -36
  61. package/src/browser/action-fence.ts +47 -0
  62. package/src/browser/app-shell.ts +39 -0
  63. package/src/browser/app-version.ts +14 -0
  64. package/src/browser/cookie-name.ts +140 -0
  65. package/src/browser/event-controller.ts +86 -147
  66. package/src/browser/history-state.ts +21 -0
  67. package/src/browser/index.ts +3 -3
  68. package/src/browser/invalidate-client-cache.ts +52 -0
  69. package/src/browser/link-interceptor.ts +4 -0
  70. package/src/browser/navigation-bridge.ts +148 -19
  71. package/src/browser/navigation-client.ts +187 -67
  72. package/src/browser/navigation-store-handle.ts +38 -0
  73. package/src/browser/navigation-store.ts +76 -67
  74. package/src/browser/navigation-transaction.ts +18 -66
  75. package/src/browser/partial-update.ts +123 -94
  76. package/src/browser/prefetch/cache.ts +214 -36
  77. package/src/browser/prefetch/fetch.ts +260 -38
  78. package/src/browser/prefetch/policy.ts +6 -0
  79. package/src/browser/prefetch/queue.ts +126 -20
  80. package/src/browser/prefetch/resource-ready.ts +77 -0
  81. package/src/browser/rango-state.ts +158 -76
  82. package/src/browser/react/Link.tsx +93 -11
  83. package/src/browser/react/NavigationProvider.tsx +115 -34
  84. package/src/browser/react/ScrollRestoration.tsx +10 -6
  85. package/src/browser/react/context.ts +7 -2
  86. package/src/browser/react/filter-segment-order.ts +49 -7
  87. package/src/browser/react/index.ts +0 -48
  88. package/src/browser/react/location-state-shared.ts +166 -8
  89. package/src/browser/react/location-state.ts +39 -14
  90. package/src/browser/react/use-action.ts +6 -15
  91. package/src/browser/react/use-handle.ts +23 -69
  92. package/src/browser/react/use-link-status.ts +0 -4
  93. package/src/browser/react/use-navigation.ts +22 -5
  94. package/src/browser/react/use-params.ts +20 -10
  95. package/src/browser/react/use-reverse.ts +106 -0
  96. package/src/browser/react/use-router.ts +46 -11
  97. package/src/browser/react/use-search-params.ts +0 -5
  98. package/src/browser/react/use-segments.ts +11 -21
  99. package/src/browser/response-adapter.ts +52 -1
  100. package/src/browser/rsc-router.tsx +215 -76
  101. package/src/browser/scroll-restoration.ts +46 -39
  102. package/src/browser/segment-reconciler.ts +36 -9
  103. package/src/browser/segment-structure-assert.ts +2 -2
  104. package/src/browser/server-action-bridge.ts +176 -50
  105. package/src/browser/types.ts +95 -11
  106. package/src/browser/validate-redirect-origin.ts +43 -16
  107. package/src/build/collect-fallback-refs.ts +107 -0
  108. package/src/build/generate-manifest.ts +65 -40
  109. package/src/build/generate-route-types.ts +5 -0
  110. package/src/build/index.ts +8 -2
  111. package/src/build/prefix-tree-utils.ts +123 -0
  112. package/src/build/route-trie.ts +137 -32
  113. package/src/build/route-types/codegen.ts +4 -4
  114. package/src/build/route-types/include-resolution.ts +9 -2
  115. package/src/build/route-types/param-extraction.ts +6 -3
  116. package/src/build/route-types/per-module-writer.ts +7 -4
  117. package/src/build/route-types/router-processing.ts +278 -96
  118. package/src/build/route-types/scan-filter.ts +9 -2
  119. package/src/build/route-types/source-scan.ts +118 -0
  120. package/src/build/runtime-discovery.ts +9 -20
  121. package/src/cache/cache-error.ts +104 -0
  122. package/src/cache/cache-policy.ts +68 -28
  123. package/src/cache/cache-runtime.ts +149 -43
  124. package/src/cache/cache-scope.ts +148 -81
  125. package/src/cache/cache-tag.ts +98 -0
  126. package/src/cache/cf/cf-cache-store.ts +2550 -93
  127. package/src/cache/cf/index.ts +11 -17
  128. package/src/cache/document-cache.ts +78 -27
  129. package/src/cache/handle-snapshot.ts +63 -0
  130. package/src/cache/index.ts +23 -20
  131. package/src/cache/memory-segment-store.ts +136 -37
  132. package/src/cache/profile-registry.ts +6 -30
  133. package/src/cache/read-through-swr.ts +41 -11
  134. package/src/cache/segment-codec.ts +0 -16
  135. package/src/cache/tag-invalidation.ts +230 -0
  136. package/src/cache/taint.ts +55 -0
  137. package/src/cache/types.ts +33 -100
  138. package/src/cache/vercel/index.ts +11 -0
  139. package/src/cache/vercel/vercel-cache-store.ts +799 -0
  140. package/src/client.rsc.tsx +6 -21
  141. package/src/client.tsx +108 -290
  142. package/src/component-utils.ts +19 -0
  143. package/src/context-var.ts +84 -2
  144. package/src/debug.ts +2 -2
  145. package/src/decode-loader-results.ts +36 -0
  146. package/src/defer.ts +196 -0
  147. package/src/deps/ssr.ts +0 -1
  148. package/src/errors.ts +30 -4
  149. package/src/handle.ts +70 -22
  150. package/src/handles/MetaTags.tsx +0 -14
  151. package/src/handles/breadcrumbs.ts +16 -5
  152. package/src/handles/meta.ts +0 -39
  153. package/src/host/cookie-handler.ts +0 -36
  154. package/src/host/errors.ts +0 -24
  155. package/src/host/index.ts +8 -2
  156. package/src/host/pattern-matcher.ts +7 -50
  157. package/src/host/router.ts +107 -99
  158. package/src/host/testing.ts +40 -27
  159. package/src/host/types.ts +37 -4
  160. package/src/host/utils.ts +1 -1
  161. package/src/href-client.ts +137 -22
  162. package/src/index.rsc.ts +52 -26
  163. package/src/index.ts +100 -38
  164. package/src/internal-debug.ts +2 -4
  165. package/src/loader-store.ts +500 -0
  166. package/src/loader.rsc.ts +20 -13
  167. package/src/loader.ts +12 -11
  168. package/src/missing-id-error.ts +68 -0
  169. package/src/network-error-thrower.tsx +1 -6
  170. package/src/outlet-context.ts +1 -1
  171. package/src/outlet-provider.tsx +1 -5
  172. package/src/prerender/param-hash.ts +10 -11
  173. package/src/prerender/store.ts +37 -41
  174. package/src/prerender.ts +198 -82
  175. package/src/redirect-origin.ts +100 -0
  176. package/src/response-utils.ts +37 -0
  177. package/src/reverse.ts +65 -15
  178. package/src/root-error-boundary.tsx +1 -19
  179. package/src/route-content-wrapper.tsx +7 -72
  180. package/src/route-definition/dsl-helpers.ts +437 -274
  181. package/src/route-definition/helper-factories.ts +29 -139
  182. package/src/route-definition/helpers-types.ts +113 -37
  183. package/src/route-definition/index.ts +3 -0
  184. package/src/route-definition/redirect.ts +52 -10
  185. package/src/route-definition/resolve-handler-use.ts +161 -0
  186. package/src/route-definition/use-item-types.ts +32 -0
  187. package/src/route-map-builder.ts +7 -17
  188. package/src/route-types.ts +37 -41
  189. package/src/router/basename.ts +14 -0
  190. package/src/router/content-negotiation.ts +108 -9
  191. package/src/router/error-handling.ts +13 -17
  192. package/src/router/find-match.ts +45 -22
  193. package/src/router/handler-context.ts +83 -41
  194. package/src/router/intercept-resolution.ts +25 -23
  195. package/src/router/lazy-includes.ts +19 -53
  196. package/src/router/loader-resolution.ts +213 -30
  197. package/src/router/logging.ts +5 -8
  198. package/src/router/manifest.ts +49 -45
  199. package/src/router/match-api.ts +120 -204
  200. package/src/router/match-context.ts +0 -22
  201. package/src/router/match-handlers.ts +58 -58
  202. package/src/router/match-middleware/background-revalidation.ts +27 -6
  203. package/src/router/match-middleware/cache-lookup.ts +205 -249
  204. package/src/router/match-middleware/cache-store.ts +45 -32
  205. package/src/router/match-middleware/intercept-resolution.ts +8 -28
  206. package/src/router/match-middleware/segment-resolution.ts +52 -18
  207. package/src/router/match-pipelines.ts +1 -42
  208. package/src/router/match-result.ts +104 -40
  209. package/src/router/metrics.ts +5 -34
  210. package/src/router/middleware-types.ts +13 -142
  211. package/src/router/middleware.ts +173 -143
  212. package/src/router/navigation-snapshot.ts +131 -0
  213. package/src/router/params-util.ts +23 -0
  214. package/src/router/pattern-matching.ts +109 -63
  215. package/src/router/prerender-match.ts +190 -54
  216. package/src/router/preview-match.ts +32 -102
  217. package/src/router/request-classification.ts +276 -0
  218. package/src/router/revalidation.ts +63 -55
  219. package/src/router/route-snapshot.ts +244 -0
  220. package/src/router/router-context.ts +6 -28
  221. package/src/router/router-interfaces.ts +100 -35
  222. package/src/router/router-options.ts +91 -11
  223. package/src/router/router-registry.ts +2 -5
  224. package/src/router/segment-resolution/fresh.ts +242 -75
  225. package/src/router/segment-resolution/helpers.ts +63 -24
  226. package/src/router/segment-resolution/loader-cache.ts +41 -37
  227. package/src/router/segment-resolution/revalidation.ts +456 -372
  228. package/src/router/segment-resolution/static-store.ts +19 -5
  229. package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
  230. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  231. package/src/router/segment-resolution.ts +4 -1
  232. package/src/router/segment-wrappers.ts +2 -3
  233. package/src/router/state-cookie-name.ts +33 -0
  234. package/src/router/substitute-pattern-params.ts +56 -0
  235. package/src/router/telemetry-otel.ts +0 -20
  236. package/src/router/telemetry.ts +96 -19
  237. package/src/router/timeout.ts +0 -20
  238. package/src/router/trie-matching.ts +91 -46
  239. package/src/router/types.ts +10 -63
  240. package/src/router/url-params.ts +44 -0
  241. package/src/router.ts +134 -43
  242. package/src/rsc/handler-context.ts +3 -2
  243. package/src/rsc/handler.ts +492 -383
  244. package/src/rsc/helpers.ts +162 -46
  245. package/src/rsc/index.ts +1 -1
  246. package/src/rsc/json-route-result.ts +38 -0
  247. package/src/rsc/loader-fetch.ts +23 -3
  248. package/src/rsc/manifest-init.ts +33 -42
  249. package/src/rsc/origin-guard.ts +39 -25
  250. package/src/rsc/progressive-enhancement.ts +30 -3
  251. package/src/rsc/redirect-guard.ts +99 -0
  252. package/src/rsc/response-error.ts +79 -12
  253. package/src/rsc/response-route-handler.ts +90 -63
  254. package/src/rsc/rsc-rendering.ts +56 -54
  255. package/src/rsc/runtime-warnings.ts +23 -10
  256. package/src/rsc/server-action.ts +74 -67
  257. package/src/rsc/ssr-setup.ts +18 -2
  258. package/src/rsc/types.ts +25 -6
  259. package/src/runtime-env.ts +18 -0
  260. package/src/search-params.ts +4 -20
  261. package/src/segment-content-promise.ts +67 -0
  262. package/src/segment-loader-promise.ts +134 -0
  263. package/src/segment-system.tsx +272 -129
  264. package/src/serialize.ts +243 -0
  265. package/src/server/context.ts +309 -61
  266. package/src/server/cookie-store.ts +80 -5
  267. package/src/server/handle-store.ts +26 -24
  268. package/src/server/loader-registry.ts +10 -28
  269. package/src/server/request-context.ts +338 -126
  270. package/src/ssr/index.tsx +23 -15
  271. package/src/static-handler.ts +27 -18
  272. package/src/testing/cache-status.ts +162 -0
  273. package/src/testing/collect-handle.ts +40 -0
  274. package/src/testing/dispatch.ts +618 -0
  275. package/src/testing/dom.entry.ts +22 -0
  276. package/src/testing/e2e/fixture.ts +188 -0
  277. package/src/testing/e2e/index.ts +128 -0
  278. package/src/testing/e2e/matchers.ts +35 -0
  279. package/src/testing/e2e/page-helpers.ts +272 -0
  280. package/src/testing/e2e/parity.ts +387 -0
  281. package/src/testing/e2e/server.ts +195 -0
  282. package/src/testing/flight-matchers.ts +97 -0
  283. package/src/testing/flight-normalize.ts +11 -0
  284. package/src/testing/flight-runtime.d.ts +57 -0
  285. package/src/testing/flight-tree.ts +682 -0
  286. package/src/testing/flight.entry.ts +52 -0
  287. package/src/testing/flight.ts +232 -0
  288. package/src/testing/generated-routes.ts +183 -0
  289. package/src/testing/index.ts +99 -0
  290. package/src/testing/internal/context.ts +348 -0
  291. package/src/testing/internal/flight-client-globals.ts +30 -0
  292. package/src/testing/internal/seed-vars.ts +54 -0
  293. package/src/testing/render-handler.ts +330 -0
  294. package/src/testing/render-route.tsx +566 -0
  295. package/src/testing/run-loader.ts +378 -0
  296. package/src/testing/run-middleware.ts +205 -0
  297. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  298. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  299. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  300. package/src/testing/vitest-stubs/version.ts +5 -0
  301. package/src/testing/vitest.ts +305 -0
  302. package/src/theme/ThemeProvider.tsx +0 -52
  303. package/src/theme/ThemeScript.tsx +0 -6
  304. package/src/theme/constants.ts +0 -12
  305. package/src/theme/index.ts +0 -7
  306. package/src/theme/theme-context.ts +1 -5
  307. package/src/theme/theme-script.ts +0 -14
  308. package/src/theme/use-theme.ts +0 -3
  309. package/src/types/boundaries.ts +0 -35
  310. package/src/types/cache-types.ts +17 -8
  311. package/src/types/error-types.ts +30 -90
  312. package/src/types/global-namespace.ts +54 -41
  313. package/src/types/handler-context.ts +233 -81
  314. package/src/types/index.ts +1 -10
  315. package/src/types/loader-types.ts +44 -15
  316. package/src/types/request-scope.ts +107 -0
  317. package/src/types/route-config.ts +6 -50
  318. package/src/types/route-entry.ts +19 -7
  319. package/src/types/segments.ts +37 -14
  320. package/src/urls/include-helper.ts +33 -70
  321. package/src/urls/index.ts +1 -11
  322. package/src/urls/path-helper-types.ts +58 -11
  323. package/src/urls/path-helper.ts +57 -111
  324. package/src/urls/pattern-types.ts +48 -19
  325. package/src/urls/response-types.ts +25 -22
  326. package/src/urls/type-extraction.ts +58 -139
  327. package/src/urls/urls-function.ts +1 -18
  328. package/src/use-loader.tsx +346 -89
  329. package/src/vite/debug.ts +185 -0
  330. package/src/vite/discovery/bundle-postprocess.ts +36 -38
  331. package/src/vite/discovery/discover-routers.ts +130 -85
  332. package/src/vite/discovery/discovery-errors.ts +194 -0
  333. package/src/vite/discovery/gate-state.ts +171 -0
  334. package/src/vite/discovery/prerender-collection.ts +192 -99
  335. package/src/vite/discovery/route-types-writer.ts +40 -84
  336. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  337. package/src/vite/discovery/state.ts +51 -6
  338. package/src/vite/discovery/virtual-module-codegen.ts +14 -34
  339. package/src/vite/index.ts +8 -0
  340. package/src/vite/plugin-types.ts +187 -69
  341. package/src/vite/plugins/cjs-to-esm.ts +8 -18
  342. package/src/vite/plugins/client-ref-dedup.ts +16 -11
  343. package/src/vite/plugins/client-ref-hashing.ts +28 -15
  344. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  345. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  346. package/src/vite/plugins/cloudflare-protocol-stub.ts +194 -0
  347. package/src/vite/plugins/expose-action-id.ts +49 -98
  348. package/src/vite/plugins/expose-id-utils.ts +11 -50
  349. package/src/vite/plugins/expose-ids/export-analysis.ts +76 -34
  350. package/src/vite/plugins/expose-ids/handler-transform.ts +10 -48
  351. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -20
  352. package/src/vite/plugins/expose-ids/router-transform.ts +20 -16
  353. package/src/vite/plugins/expose-internal-ids.ts +554 -317
  354. package/src/vite/plugins/performance-tracks.ts +89 -0
  355. package/src/vite/plugins/refresh-cmd.ts +89 -27
  356. package/src/vite/plugins/use-cache-transform.ts +73 -83
  357. package/src/vite/plugins/vercel-output.ts +258 -0
  358. package/src/vite/plugins/version-injector.ts +21 -25
  359. package/src/vite/plugins/version-plugin.ts +41 -20
  360. package/src/vite/plugins/virtual-entries.ts +2 -17
  361. package/src/vite/rango.ts +257 -289
  362. package/src/vite/router-discovery.ts +930 -140
  363. package/src/vite/utils/ast-handler-extract.ts +15 -31
  364. package/src/vite/utils/banner.ts +4 -4
  365. package/src/vite/utils/bundle-analysis.ts +10 -15
  366. package/src/vite/utils/client-chunks.ts +184 -0
  367. package/src/vite/utils/forward-user-plugins.ts +171 -0
  368. package/src/vite/utils/manifest-utils.ts +4 -59
  369. package/src/vite/utils/package-resolution.ts +20 -52
  370. package/src/vite/utils/prerender-utils.ts +27 -29
  371. package/src/vite/utils/shared-utils.ts +92 -42
  372. package/src/browser/action-response-classifier.ts +0 -99
  373. package/src/browser/react/use-client-cache.ts +0 -58
  374. package/src/browser/shallow.ts +0 -40
  375. package/src/handles/index.ts +0 -7
  376. package/src/router/middleware-cookies.ts +0 -55
@@ -0,0 +1,124 @@
1
+ # Testing cache / SWR / prerender — assertCacheStatus
2
+
3
+ **Layer:** e2e + signal · **Import:** the cache-status helpers (`assertCacheStatus`/`parseCacheHeader`/`createCacheSink`/`assertCacheDecision`/`filterCacheDecisions`) are re-exported from BOTH entries — use `@rangojs/router/testing` from a Vitest unit/integration test, and `@rangojs/router/testing/e2e` from a plain Playwright runner (the e2e barrel avoids the Vite-only virtuals the main barrel pulls in). · **DSL it tests:** `cache()` / `"use cache"` / loader cache / `Prerender(...)` (see `/caching`, `/prerender`, `/use-cache`)
4
+
5
+ The router's REAL cache pipeline runs (runtime cache, SWR revalidation, prerender lookup); you SEED nothing — you drive a request through the real fetch path and read the resulting cache decision. The decision surfaces two ways: the `X-Rango-Cache` response header (a debug gate) or a captured `cache.decision` telemetry event.
6
+
7
+ ## Which path to use
8
+
9
+ Both report the SAME coarse route-level signal (keyed by the route NAME). Pick by **transport**, not by meaning:
10
+
11
+ | Path | Helper | Transport | Needs the debug gate? | Production surface | Per-segment `shouldRevalidate`? |
12
+ | ------------- | -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------- | --------------------------------- | ------------------------------- |
13
+ | **Header** | `assertCacheStatus(res, routeKey, expected)` / `parseCacheHeader` | the `X-Rango-Cache` response header — the ONLY signal a black-box Playwright `Response` carries | Yes (`debugCacheSignal` / `RANGO_TEST_SIGNALS=1`) | the header (gated off by default) | no |
14
+ | **Telemetry** | `assertCacheDecision(events, routeKey, expected)` / `filterCacheDecisions` | a captured `cache.decision` event off a `createCacheSink()` sink | No | zero | yes (the only path exposing it) |
15
+
16
+ Use the header path when all you have is a black-box `Response` (a Playwright `APIResponse`); use the telemetry path when you can wire `createRouter({ telemetry: sink })` and want zero production surface or per-segment `shouldRevalidate`. `assertCacheDecision` is the one-call counterpart of `assertCacheStatus` (parallel `(…, routeKey, expected)` shape — captured `events` in place of a `Response`); reach for raw `filterCacheDecisions` only when you need the per-segment event fields directly.
17
+
18
+ ## API
19
+
20
+ ### Options — `assertCacheStatus(target, segment, expected)`
21
+
22
+ | Field | Type | Meaning |
23
+ | ---------- | ------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
24
+ | `target` | `Response \| { headers: Headers }` (`CacheStatusTarget`) | The thing carrying the `X-Rango-Cache` header: a `Response` from `router.fetch(...)`, or any `{ headers: Headers }`. A Playwright `APIResponse` exposes headers as a method, so wrap it: `{ headers: new Headers(res.headers()) }`. |
25
+ | `segment` | `string` | The route NAME (e.g. `product.detail`), the same id the header carries — NOT the URL pattern (`/products/:id`). |
26
+ | `expected` | `"hit" \| "miss" \| "stale" \| "prerendered" \| "passthrough"` (`ExpectedCacheStatus` = `CacheSegmentStatus`) | The cache status you assert for that route. |
27
+
28
+ ### Context — what your code under test emits
29
+
30
+ The header / event is produced by the router's RSC render pipeline from `ctx.routeKey`. Your code does not call these helpers — it just runs under a router with the gate (or telemetry sink) wired. The helpers READ the emitted signal.
31
+
32
+ | Field | Type | Meaning |
33
+ | ------------------------------------- | ----------------------- | ------------------------------------------------------------------------------------------ |
34
+ | `CacheSegmentSignal.id` | `string` | Segment id. v1: the route key, since status is route-level. |
35
+ | `CacheSegmentSignal.type` | `string` | Segment type. v1: `"route"` for the coarse route-level entry. |
36
+ | `CacheSegmentSignal.cacheStatus` | `CacheSegmentStatus` | Resolved status (`hit`/`miss`/`stale`/`prerendered`/`passthrough`). |
37
+ | `CacheSegmentSignal.shouldRevalidate` | `boolean?` | Whether stale-while-revalidate was triggered for this segment. |
38
+ | `CacheDecisionEvent.segments` | `CacheSegmentSignal[]?` | The coarse route-level signal array (present only when telemetry or the debug gate is on). |
39
+
40
+ ### Returns
41
+
42
+ ```ts
43
+ // assertCacheStatus throws on mismatch / missing header / unknown segment; returns void.
44
+ assertCacheStatus(target, segment, expected): void
45
+
46
+ // parseCacheHeader -> the raw { routeKey: status } map. "a=hit, b=stale" -> { a: "hit", b: "stale" }.
47
+ parseCacheHeader(headerValue: string | null | undefined): Record<string, string>
48
+
49
+ // createCacheSink -> a sink to wire via createRouter({ telemetry: sink }), plus the array it records into.
50
+ createCacheSink(): { sink: TelemetrySink; events: TelemetryEvent[] }
51
+
52
+ // assertCacheDecision -> the one-call telemetry assert (counterpart of assertCacheStatus).
53
+ // Throws on mismatch / no matching segment / unknown routeKey; returns void.
54
+ assertCacheDecision(events: readonly TelemetryEvent[], routeKey: string, expected: ExpectedCacheStatus): void
55
+
56
+ // filterCacheDecisions -> narrow captured events to cache.decision events (raw form).
57
+ filterCacheDecisions(events: readonly TelemetryEvent[]): CacheDecisionEvent[]
58
+ ```
59
+
60
+ ## Recipe
61
+
62
+ ```ts
63
+ // In a Playwright e2e, import the cache-status helpers from the e2e entry —
64
+ // the @rangojs/router/testing barrel pulls a build-only virtual that does not
65
+ // resolve in a plain Playwright runner.
66
+ import { assertCacheStatus } from "@rangojs/router/testing/e2e";
67
+
68
+ parityDescribe("product page caches", (f) => {
69
+ test("second request is a hit", async ({ page }) => {
70
+ // The key is the route NAME (the X-Rango-Cache id), NOT the URL pattern.
71
+ // Playwright APIResponse.headers() is a method returning a plain record, so
72
+ // wrap it in a Headers to match CacheStatusTarget (`{ headers: Headers }`).
73
+ const first = await page.request.get(f.url("/products/1"));
74
+ assertCacheStatus(
75
+ { headers: new Headers(first.headers()) },
76
+ "product.detail",
77
+ "miss",
78
+ );
79
+ const second = await page.request.get(f.url("/products/1"));
80
+ assertCacheStatus(
81
+ { headers: new Headers(second.headers()) },
82
+ "product.detail",
83
+ "hit",
84
+ );
85
+ });
86
+ });
87
+ ```
88
+
89
+ Zero-prod-surface alternative — the telemetry sink. No header at all; you inspect captured `cache.decision` events:
90
+
91
+ ```ts
92
+ import {
93
+ createCacheSink,
94
+ assertCacheDecision,
95
+ filterCacheDecisions,
96
+ } from "@rangojs/router/testing";
97
+
98
+ const { sink, events } = createCacheSink();
99
+ const router = createRouter({ telemetry: sink }).routes(urlpatterns);
100
+ // ...drive a request through the router's RSC fetch path...
101
+
102
+ // One-call assert (counterpart of assertCacheStatus), keyed by the route NAME:
103
+ assertCacheDecision(events, "product.detail", "stale");
104
+
105
+ // Or read the raw event when you need per-segment fields (shouldRevalidate):
106
+ const decision = filterCacheDecisions(events)[0];
107
+ expect(decision.segments?.[0].cacheStatus).toBe("stale");
108
+ expect(decision.segments?.[0].shouldRevalidate).toBe(true);
109
+ ```
110
+
111
+ `events` accumulates across requests, so the FIRST matching segment for a `routeKey` wins — slice or recreate the sink between requests for the same route.
112
+
113
+ ## Caveats
114
+
115
+ - The `X-Rango-Cache` header is emitted ONLY when the gate is on: `createRouter({ debugCacheSignal: true })` or `process.env.RANGO_TEST_SIGNALS === "1"`. Off by default — zero production surface. With the gate off, `assertCacheStatus` throws a clear "header missing" error.
116
+ - v1 is COARSE: route-level, keyed by the route NAME (e.g. `product.detail`), NOT the URL pattern (`/products/:id`); not per-individual-segment. The signal is built from `ctx.routeKey`, so a pattern-shaped key never matches. (`parseCacheHeader` exposes the raw `{ routeKey: status }` map if you need it.)
117
+ - Prerender is indistinguishable from a cache hit by design — no static `.html`/`.rsc` files, the worker handles every request and looks up a stored Flight payload; the browser cannot tell. Do not assert "prerendered" from the DOM. Assert via the signal (`assertCacheStatus(res, seg, "prerendered")`) and run prerender assertions in PRODUCTION mode (the build-time artifacts only exist after `pnpm build`).
118
+ - In a Playwright e2e import the cache-status helpers from the `/e2e` entry — the `@rangojs/router/testing` barrel is Vitest-only (it pulls a build-only virtual that does not resolve in a plain Playwright runner). Zero-prod-surface alternative: the telemetry sink (`createCacheSink`/`filterCacheDecisions`), no header at all. Note: the non-RSC `dispatch()` primitive never emits this header — get the Response from the router's real RSC fetch path.
119
+
120
+ ## See also
121
+
122
+ - `/caching`, `/prerender`, `/use-cache` — the DSL this tests
123
+ - Siblings: [`./e2e-parity.md`](./e2e-parity.md), [`./response-routes.md`](./response-routes.md)
124
+ - Long-form prose: [docs/testing.md](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md) — section "Cache, SWR, and prerender"
@@ -0,0 +1,122 @@
1
+ # Testing a client component — renderRoute
2
+
3
+ **Layer:** unit (DOM) · **Import:** `@rangojs/router/testing/dom` · **DSL it tests:** a client component reading router context (see `/hooks`)
4
+
5
+ RTL-style stub (peer of React Router's `createRoutesStub` / Expo's `renderRouter`). It mounts the router's REAL `NavigationProvider` plus a synthetic segment tree built from the `routes` you pass, so client hooks resolve against production context — no server, no Vite build, no Flight round-trip. Loader data, location state, and handle output are SEEDED into client context; nothing is executed.
6
+
7
+ ## API
8
+
9
+ ### Options — `RenderRouteOptions`
10
+
11
+ | Field | Type | Meaning |
12
+ | --------------- | ---------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
13
+ | `request` | `Request \| string` | Initial location. Only the URL is read (client render — headers/method ignored). Defaults to the leaf spec's static prefix or `"/"`. |
14
+ | `loaderData` | `Record<string, unknown>` | Loader data keyed by loader `$$id`. `useLoader(L)` reads `loaderData[L.$$id]`. |
15
+ | `loaders` | `ReadonlyArray<readonly [LoaderDefinition<any>, unknown]>` | Seed by REFERENCE: `[loader, data]` pairs. Robust for real `createLoader()` handles whose `$$id` is empty in a bare test. Prefer over `loaderData`. |
16
+ | `params` | `Record<string, string>` | Explicit params, merged over (and overriding) params extracted from the `request` URL. |
17
+ | `locationState` | `ReadonlyArray<readonly [LocationStateDefinition<any, any>, unknown]>` | Seed `useLocationState(def)` by REFERENCE: `[def, value]` pairs; written to `history.state`. |
18
+ | `handles` | `ReadonlyArray<readonly [Handle<any, any>, unknown[]]>` | Seed `useHandle(handle)` by REFERENCE: `[handle, pushedValues[]]`. Accumulated GLOBALLY (not segment-scoped). |
19
+ | `handle` | `HandleDataSeed` | Advanced: raw wire format `{ [handleId]: { [segmentId]: pushedValues[] } }`. Prefer `handles`. Merged with it. |
20
+ | `routeMap` | `Record<string, string>` | Name -> pattern map (informational; client `useReverse` takes its map as an argument, so this is not consumed). |
21
+ | `basename` | `string` | `createRouter({ basename })` value. Wired into `NavigationProvider` so `useRouter().basename`, `<Link>` prefixing, `useMount`/`useHref` resolve against the mount. Normalized like `createRouter`. Defaults to root. |
22
+ | `mount` | `string` | `include()` mount prefix. Wraps the segment chain in a `MountContext` so `useMount()` returns the prefix. Normalized like a path prefix. Defaults to `"/"`. |
23
+ | `theme` | `ThemeConfig \| true` | Theme config (`createRouter({ theme })` shape) to wrap the tree in a `ThemeProvider`. Defaults to no provider. A component calling `useTheme()` REQUIRES one. |
24
+
25
+ `RenderRouteSpec = { path, Component, layout?, loaderIds?, name? }` — one node of the route definition. The array is the layout chain root-to-leaf; the LAST entry is the leaf route (its pattern is matched against `request` to extract params; layout patterns are informational). `loaderIds` attaches seeded loaders to THIS node's segment; `layout` on the leaf wraps it; `name` is informational.
26
+
27
+ ### Context — client hooks it makes resolve (what your code receives)
28
+
29
+ | Hook | Meaning |
30
+ | ------------------------------ | --------------------------------------------------------------------------------------------- |
31
+ | `useParams` | Params from the matched leaf pattern, with `options.params` merged over. |
32
+ | `useReverse` | Reverse a name->pattern map to a URL; merges `useParams()` and the `mount`/`basename` prefix. |
33
+ | `useHref` | Resolve an href against the mount/basename. |
34
+ | `useMount` | The `include()` mount prefix (`options.mount`), else `"/"`. |
35
+ | `useNavigation` | Navigation controller state — stays `idle` (see caveat). |
36
+ | `useRouter` | The router handle, including `.basename`. |
37
+ | `usePathname` | Current committed pathname. |
38
+ | `useSearchParams` | Search params from the `request` URL. |
39
+ | `useLoader` / `useFetchLoader` | SEEDED loader data (read path, not run path). |
40
+ | `useLocationState` | SEEDED `history.state` value. |
41
+ | `useHandle` | SEEDED handle output (globally accumulated). |
42
+ | `Outlet` | Renders the next segment in the chain (layout nesting). |
43
+ | `useTheme` | Theme; throws without `options.theme` (see caveat). |
44
+
45
+ ### Returns — `RenderRouteResult`
46
+
47
+ Extends RTL's `RenderResult` (`getByTestId`, `getByText`, `getByRole`, `container`, ...) with:
48
+
49
+ ```ts
50
+ type RenderRouteResult = RenderResult & {
51
+ router: {
52
+ navigate(url: string): Promise<void>; // client-only nav, re-resolves the same routes
53
+ pathname(): string;
54
+ params(): Record<string, string>;
55
+ store: NavigationStore; // advanced
56
+ eventController: EventController; // advanced
57
+ };
58
+ };
59
+ ```
60
+
61
+ ## Recipe
62
+
63
+ ```tsx
64
+ // @vitest-environment happy-dom
65
+ import { describe, it, expect, afterEach } from "vitest";
66
+ import { cleanup } from "@testing-library/react";
67
+ import { renderRoute } from "@rangojs/router/testing/dom";
68
+ import { Outlet, useParams, useReverse } from "@rangojs/router/client";
69
+
70
+ afterEach(cleanup);
71
+
72
+ function Layout() {
73
+ return (
74
+ <div>
75
+ <span data-testid="shell">shell</span>
76
+ <Outlet />
77
+ </div>
78
+ );
79
+ }
80
+ function Product() {
81
+ const { productId } = useParams<{ productId: string }>();
82
+ const reverse = useReverse({ product: "/products/:productId" });
83
+ return (
84
+ <a data-testid="link" href={reverse("product", { productId: "2" })}>
85
+ {productId}
86
+ </a>
87
+ );
88
+ }
89
+
90
+ it("resolves params + reverse + Outlet through the layout chain", async () => {
91
+ const { getByTestId, router } = await renderRoute(
92
+ [
93
+ { path: "/products", Component: Layout }, // layout (root)
94
+ { path: "/products/:productId", Component: Product }, // leaf (last)
95
+ ],
96
+ { request: "/products/1" },
97
+ );
98
+ expect(getByTestId("shell").textContent).toBe("shell");
99
+ expect(getByTestId("link").getAttribute("href")).toBe("/products/2");
100
+
101
+ await router.navigate("/products/2"); // client-only nav, re-resolves the same routes
102
+ expect(router.pathname()).toBe("/products/2");
103
+ });
104
+ ```
105
+
106
+ ## Caveats
107
+
108
+ - Client tree ONLY. Does NOT catch server/client boundary reference-identity remount bugs, real Flight serialization errors, loader execution, middleware, or handler ordering — those are `renderServerTree` / `renderHandler` / e2e territory. Loader data is SEEDED, never run.
109
+ - `router.navigate()` bypasses the navigation lifecycle, so the controller never leaves `idle`. `useNavigation()` / `useLinkStatus()` / `useAction()` non-idle states (loading/streaming/pending, action result/error) are NOT reachable — test those at e2e.
110
+ - CATCH — streaming `use(promise)` Suspense content (e.g. an async breadcrumb `content: Promise<ReactNode>`): a plain `Promise.resolve(node)` does NOT flush its Suspense retry in RTL/happy-dom, so the DOM stays on the fallback. Assert the PENDING fallback with `new Promise(() => {})`; for the ARRIVED state pass an already-settled promise so `use()` reads it synchronously: `const p = Promise.resolve(node) as any; p.status = "fulfilled"; p.value = node;`. The real pending->resolved transition is an e2e concern.
111
+ - ARIA gotcha — an explicit `role` on a `<Link>` (e.g. `<Link role="tab">` in a tablist) OVERRIDES the implicit `link` role, so `getByRole("link")` finds nothing. Query the explicit role (`getByRole("tab")`) or fall back to `getByText` / `getByTestId` and assert `getAttribute("href")`.
112
+ - `ctx.theme` is undefined unless `theme` is passed; the typed `ctx.search` defaults to `{}` (seed `searchData` on `runLoader`, not here).
113
+ - Use `mount` only for an `include()` prefix. An OPTIONAL param in the matched pattern (`/:locale?/c/:group` at `/en/c/wine`) auto-fills `locale` from the match — production parity, `useReverse` merges `useParams()` — so no `mount` is needed; a locale "dropping" from a reversed URL is usually a missing `mount` seed, not an auto-fill gap.
114
+ - Needs a DOM env (`// @vitest-environment happy-dom`, or jsdom) and `@testing-library/react` (optional peers).
115
+ - Don't hand-roll a `NavigationProvider`/router-context mock to test a client component — `renderRoute` mounts the REAL provider, so a hand-mock both duplicates effort and drifts from the production context shape.
116
+ - MULTI-APP `href` typing. When a `renderRoute` suite imports client components across apps, the global `Rango.GeneratedRouteMap` augmentations collide and `href()` stops typechecking (app A's route union rejects app B's name). Runtime is unaffected — it is purely the global `href` typing. Keep the suite single-app, or split tsconfig programs per app (see [`./reverse-and-types.md`](./reverse-and-types.md) and `/typesafety`).
117
+
118
+ ## See also
119
+
120
+ - `/hooks` — the DSL this tests
121
+ - Siblings: `./handles.md`, `./reverse-and-types.md`, `./render-handler.md`, `./e2e-parity.md`
122
+ - Long-form prose: [docs/testing.md](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md) — section "Reverse and components" (and the "Catch: streaming `use(promise)` Suspense content" subsection)
@@ -0,0 +1,125 @@
1
+ # E2E with dev/prod and PE parity — createRangoE2E
2
+
3
+ **Layer:** e2e (Playwright) · **Import:** `@rangojs/router/testing/e2e` · **DSL it tests:** navigation, hydration, server actions + revalidation, view transitions, PE parity (see `/hooks`, `/view-transitions`)
4
+
5
+ This is full-stack: the harness builds and serves your real app (`pnpm dev` or `pnpm build` + `pnpm preview`) and drives a real browser. Nothing is seeded — you SEED only the URL you navigate to and the form data you submit; everything else (SSR, hydration, the RSC stream, actions, revalidation) is the real machinery.
6
+
7
+ ## API
8
+
9
+ `createRangoE2E({ test, expect, defaultRoot })` takes your Playwright `test`/`expect` and returns `{ useFixture, parityDescribe, expectParity, rangoMatchers, testNoJs, ...pageHelpers }`. The factory never imports `@playwright/test` at runtime — the helpers run on the objects you pass, so this entry is loadable in a plain Playwright runner.
10
+
11
+ ### Factory options — `createRangoE2E({ ... })`
12
+
13
+ | Field | Type | Meaning |
14
+ | ------------- | ---------- | ------------------------------------------------------------------------ |
15
+ | `test` | `TestType` | Your Playwright `test` (drives `describe`/`beforeAll`/`afterAll`). |
16
+ | `expect` | `Expect` | Your Playwright `expect` (used by helpers + matchers). |
17
+ | `defaultRoot` | `string?` | Fallback app root for `parityDescribe` when a call omits `options.root`. |
18
+
19
+ ### Fixture options — `FixtureOptions` (`useFixture` / `parityDescribe` 3rd arg)
20
+
21
+ | Field | Type | Meaning |
22
+ | ---------------- | ------------------ | ------------------------------------------------------------------------------ |
23
+ | `root` | `string` | App path under test (abs or cwd-relative). Required here or via `defaultRoot`. |
24
+ | `mode` | `"dev" \| "build"` | Server mode. `parityDescribe` sets this for you (dev + build). |
25
+ | `command` | `string?` | Override server command (default `pnpm dev` / `pnpm preview`). |
26
+ | `buildCommand` | `string?` | Override build command (default `pnpm build`). |
27
+ | `cliOptions` | `SpawnOptions?` | Extra spawn options (`env`, etc.). |
28
+ | `isolatedServer` | `boolean?` | Per-suite server with an isolated Vite cache dir (warms dep optimizer; dev). |
29
+ | `readyPath` | `string?` | Readiness poll path (default `/`); use when a basename moves routes off `/`. |
30
+ | `skipBuild` | `boolean?` | Skip the production build (assumes an existing build). |
31
+
32
+ ### Parity intent — `ParityIntent` (what `expectParity` applies)
33
+
34
+ | Shape | Meaning |
35
+ | ------------------------------- | ----------------------------------------------------------------------------- |
36
+ | `{ navigate: string }` | Go to a URL (resolved against `opts.baseURL` if relative). |
37
+ | `{ submit: { testId, data? } }` | Fill `data` into named inputs under `[data-testid=testId]`, click its submit. |
38
+
39
+ ### expectParity options — `ExpectParityOptions`
40
+
41
+ | Field | Type | Meaning |
42
+ | --------------- | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
43
+ | `observe` | `string[]` | data-testid values whose text must match across JS and no-JS. |
44
+ | `baseURL` | `string?` | Base URL for a relative `navigate` intent. |
45
+ | `waitFor` | `(page) => Promise<void>?` | Post-intent settle hook on BOTH transports; for `submit` it REPLACES the generic change/stability wait. |
46
+ | `ignoreCookies` | `ReadonlyArray<string \| RegExp>?` | Cookie names to exclude from the JS/no-JS jar comparison (exact string or RegExp). The rango state cookie (default prefix `rango-state`) is ALWAYS excluded — it is client-only; use this for a custom `stateCookiePrefix` or other volatile/JS-only cookies (analytics, CSRF). |
47
+
48
+ ### Returns
49
+
50
+ `createRangoE2E(...)` -> `RangoE2E`:
51
+
52
+ - `useFixture(options)` -> `Fixture` (`{ mode, root, url(path?), proc() }`). `url(path)` resolves against the running server.
53
+ - `parityDescribe(name, (f) => { ... }, options?)` -> registers a dev describe `name` AND a production describe `` `${name} (production)` ``. Body runs once per describe with that describe's `Fixture`.
54
+ - `expectParity(page, intent, opts) => Promise<void>` — runs `intent` over the JS page and a fresh no-JS context, asserts observed testids' text + pathname/search/hash + `document.cookie` are equal. `opts` is the required `observe` plus optional `baseURL`, `waitFor`, and `ignoreCookies` (the rango state cookie is excluded automatically).
55
+ - `rangoMatchers` — `{ toHaveRangoPathname }` only (pass to `expect.extend`).
56
+ - `testNoJs` — a `test` variant with JavaScript disabled.
57
+ - Page helpers: `waitForHydration`, `expectNoReload`, `expectNoPageError`, `testId`, `waitForNavigation`, `waitForElement`, `goBack`/`goForward`, `getHistoryState`, `waitForTextChange`/`waitForNumericChange`, timing helpers.
58
+
59
+ ## Recipe
60
+
61
+ ```ts
62
+ // helper.ts — wire the harness once around your Playwright test/expect.
63
+ import { test, expect } from "@playwright/test";
64
+ import { createRangoE2E } from "@rangojs/router/testing/e2e";
65
+
66
+ export const { parityDescribe, expectParity, rangoMatchers, useFixture } =
67
+ createRangoE2E({ test, expect, defaultRoot: "." });
68
+ export { test, expect };
69
+ ```
70
+
71
+ ```ts
72
+ // nav.test.ts — one body -> dev describe AND `(production)` describe.
73
+ import {
74
+ test,
75
+ expect,
76
+ parityDescribe,
77
+ expectParity,
78
+ rangoMatchers,
79
+ } from "./helper";
80
+ expect.extend(rangoMatchers);
81
+
82
+ parityDescribe("product navigation", (f) => {
83
+ test("client-navigates without a reload", async ({ page }) => {
84
+ await page.goto(f.url("/"));
85
+ await page.getByTestId("product-link").click();
86
+ await page.waitForURL("**/products/1");
87
+ await expect(page).toHaveRangoPathname("/products/1"); // typed via the shipped augmentation
88
+ });
89
+ });
90
+
91
+ parityDescribe("add to cart parity", (f) => {
92
+ test("JS and no-JS produce the same observable result", async ({ page }) => {
93
+ await page.goto(f.url("/products/1"));
94
+ await expectParity(
95
+ page,
96
+ { submit: { testId: "add-to-cart-form", data: { qty: "2" } } },
97
+ { observe: ["cart-count", "flash-message"] },
98
+ );
99
+ });
100
+ });
101
+ ```
102
+
103
+ This add-to-cart example only works because the cart is **session-scoped**. A `submit` intent runs against the one live server TWICE — once on the JS page, once in a fresh no-JS context — so `cart-count` lands on the same value on both transports only if each context has its own cart (two distinct sessions, each going 0 -> 2). If the cart were a single global counter shared across contexts, the no-JS pass would observe the JS pass's mutation too (2 then 4) and the snapshots would diverge. See the submit caveats below before reaching for a `submit` intent.
104
+
105
+ ## Caveats
106
+
107
+ - Every e2e covers BOTH dev and production — a dev-only e2e is not acceptable. `parityDescribe` enforces it structurally: one body registers the dev describe AND the `(production)` describe.
108
+ - Bucketing footgun: a `useFixture({ mode: "build" })` describe whose title omits `(production)` silently lands in the DEV bucket — prod coverage lost, no error. Never hand-title a build describe; the bucketing matches the literal `(production)`, so `(prod)`, `-build`, `-prod` do NOT count. Use `parityDescribe`.
109
+ - `expectParity` contract: PE parity only holds if the submit target is a real `<form>` (with JS off the browser does a native POST). Cookie observation is `document.cookie` — non-HttpOnly cookies only in v1; an HttpOnly (session/auth) divergence is NOT caught here.
110
+
111
+ ### `submit`-intent parity — two scar-tissue hazards
112
+
113
+ A `submit` intent does NOT replay against a snapshot of the server — it submits for real, twice, against the one running instance, and then compares two whole browser jars. Both of these have bitten before; read them before you write a `submit` parity test.
114
+
115
+ - **Double execution.** The JS path submits on the page you handed `expectParity`. The no-JS pass then reloads the SAME `originUrl` in a fresh, scripting-disabled context and submits AGAIN. So a non-idempotent action (anything that mutates server state) runs twice against one server, and the no-JS snapshot sees BOTH mutations — UNLESS the mutated state is per-session / per-context. The add-to-cart example above only passes because the cart is session-scoped: each context owns its own cart and independently goes 0 -> 2, so both snapshots read 2. A globally-shared counter would read 2 after the JS submit and 4 after the no-JS submit, and the equality assertion would fail with no obvious cause. Increment-shaped actions are the trap; make the observable state session-scoped, or assert the submit path outside `expectParity`.
116
+ - **Whole jar, not the delta.** The cookie assertion compares `document.cookie` of the JS context against `document.cookie` of the fresh no-JS context (`parity.ts`, the final `cookies` equality). The JS context carries every cookie it accumulated before the intent — consent banners, analytics, cookies set during earlier navigation in the same test. The no-JS context starts empty and only picks up what THIS submit sets. So a pre-existing, intent-unrelated cookie in the JS context false-mismatches: the helper is diffing two jars, not the per-submit cookie delta. Keep the JS context's pre-intent cookie state minimal (a fresh page goto, no prior cookie-setting steps), or assert the specific Set-Cookie in a dedicated test.
117
+ - `rangoMatchers` ships `toHaveRangoPathname` only. `toHaveSegments`/`toHaveParams` are a documented future addition — they need a client-emitted signal that does not exist yet; do not assume them.
118
+ - Subset run: add `--no-deps`. `--grep` does NOT filter dependency projects, so grepping one production test otherwise pulls in the whole dev suite. `--grep` is a regex: a pasted title containing `(production)` / `:locale?` / `[...]` mis-matches — grep a metacharacter-free fragment (or escape it). Example: `pnpm exec playwright test --project=production --no-deps --grep "add to cart parity"`.
119
+ - Import the harness from the `/e2e` entry — the unit barrel (`@rangojs/router/testing`) is not loadable in a plain Playwright runner (it pulls a build-only virtual). The helpers take your `test`/`expect`, so this entry never imports `@playwright/test` at runtime.
120
+
121
+ ## See also
122
+
123
+ - `/hooks`, `/view-transitions` — the DSL this tests
124
+ - Siblings: [`./cache-prerender.md`](./cache-prerender.md), [`./client-components.md`](./client-components.md)
125
+ - Long-form prose: [docs/testing.md](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md) — section "E2E with dev/prod and PE parity" (and "Running a subset locally")
@@ -0,0 +1,92 @@
1
+ # Pinning the Flight wire payload — renderToFlightString
2
+
3
+ **Layer:** RSC unit (react-server project) · **Import:** `@rangojs/router/testing/flight` + `@rangojs/router/testing/flight-matchers` · **DSL it tests:** an async Server Component / Flight output (see `/route`)
4
+
5
+ > **Prefer `renderServerTree` (see [`./server-tree.md`](./server-tree.md)) for assertions on a Flight render** — it deserializes to a traversable tree with TYPED boundary props (a `Date` is a `Date`, not the opaque `$D...` encoding). Reach for `renderToFlightString` + the wire matchers (`toMatchFlight`/`toMatchFlightSnapshot`) only to pin the raw wire payload SHAPE — a `toMatchFlightSnapshot` drift snapshot. That is the niche/escape-hatch case; for "testing an async Server Component (assert what it rendered)" start at `./server-tree.md`.
6
+
7
+ `renderToFlightString` runs the REAL react-server-dom serializer the router uses at runtime — your async Server Component genuinely renders to its Flight wire string in plain node, with a request context active for the render. What you SEED is the request, headers, env, params, routeName, and vars that context exposes.
8
+
9
+ ## API
10
+
11
+ ### Options — `RenderToFlightStringOptions`
12
+
13
+ | Field | Type | Meaning |
14
+ | ----------- | ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
15
+ | `request` | `Request \| string` | The request the render runs under: a `Request`, or a URL string (absolute or path). Defaults to `http://localhost/`. A component reading `getRequestContext()` sees this request's url/cookies. When a `Request` is passed, its headers are used and `headers` is ignored. |
16
+ | `headers` | `HeadersInit` | Request headers (e.g. Cookie) visible to the server tree, used only when `request` is a string. |
17
+ | `env` | `unknown` | Env / bindings exposed as `ctx.env`. Defaults to `{}`. |
18
+ | `params` | `Record<string, string>` | Route params exposed via `ctx.params` and loader contexts. |
19
+ | `routeName` | `string` | Matched route name (drives `ctx.routeName` and scoped reverse). |
20
+ | `routeMap` | `Record<string, string>` | Route name -> pattern map scoping `ctx.reverse()` (like `renderHandler`). Without it, a component that reverses resolves against the GLOBAL route map and is order-dependent on whatever router registered last. Pass the router-under-test's map for deterministic reverse. |
21
+ | `vars` | `VarsInit` | Variables a prior middleware would have set, visible via `ctx.get(...)`. Object form (`{ user }`) or `[key, value]` tuples (`[[userVar, u]]`). |
22
+
23
+ ### Context — `RequestContext` (what your component receives)
24
+
25
+ A request context is active for the whole render, so an async Server Component can read it via `getRequestContext()` / the router's server APIs. The notable surfaces seeded from the options above:
26
+
27
+ | Field | Type | Meaning |
28
+ | ----------- | ----------------------------------------- | --------------------------------------------------------------------- |
29
+ | `request` | `Request` | The backing request (from `request`/`headers`). |
30
+ | `url` | `URL` | The request URL. |
31
+ | `env` | `unknown` | Env / bindings (from `env`). |
32
+ | `params` | `Record<string, string>` | Route params (from `params`). |
33
+ | `routeName` | `string \| undefined` | Matched route name (from `routeName`). |
34
+ | `get` | `<T>(v: ContextVar<T>) => T \| undefined` | Read a var seeded via `vars` (by `createVar()` handle or string key). |
35
+ | `cookies` | reader | Cookies parsed from the request's Cookie header. |
36
+
37
+ ### Returns — `Promise<string>`
38
+
39
+ The Flight wire string for the rendered tree. Assert on it with the matchers (register via `expect.extend(flightMatchers)`):
40
+
41
+ ```ts
42
+ expect(await renderToFlightString(<C />)).toMatchFlight("substring"); // containment
43
+ expect(await renderToFlightString(<C />)).toMatchFlightSnapshot(); // normalized snapshot
44
+ ```
45
+
46
+ `toMatchFlight(substring)` is containment (not equality) on the normalized payload; `toMatchFlightSnapshot()` snapshots the normalized payload. Both matchers live at `@rangojs/router/testing/flight-matchers` and run ONLY under the react-server vitest project (see `./setup.md`).
47
+
48
+ ## Recipe
49
+
50
+ Name the file `*.rsc-test.{ts,tsx}` and run `pnpm test:unit:rsc`:
51
+
52
+ ```tsx
53
+ import { it, expect } from "vitest";
54
+ import { renderToFlightString } from "@rangojs/router/testing/flight";
55
+ import { flightMatchers } from "@rangojs/router/testing/flight-matchers";
56
+ expect.extend(flightMatchers);
57
+
58
+ // Pure leaf server components: data comes in as props, not getRequestContext.
59
+ async function Greeting({ name }: { name: string }) {
60
+ const who = await Promise.resolve(name);
61
+ return <h1>Hello {who}</h1>;
62
+ }
63
+
64
+ async function ItemView({ id }: { id: string }) {
65
+ const item = await Promise.resolve({ id, label: `Item ${id}` });
66
+ return <article>{item.label}</article>;
67
+ }
68
+
69
+ it("renders an async server component to Flight", async () => {
70
+ const flight = await renderToFlightString(<Greeting name="Ada" />);
71
+ expect(flight).toMatchFlight("Ada");
72
+ });
73
+
74
+ it("snapshots the normalized payload", async () => {
75
+ const flight = await renderToFlightString(<ItemView id="7" />);
76
+ expect(flight).toMatchFlightSnapshot();
77
+ });
78
+ ```
79
+
80
+ ## Caveats
81
+
82
+ - Leaf / server-only: a client island in the tree emits an un-hydratable `I[...]` import row against the empty client manifest. Keep Flight tests to leaf server components; test full pages at e2e.
83
+ - Requires the react-server vitest project (see `./setup.md`): `resolve.conditions` includes `react-server`, the `@rangojs/router -> index.rsc.ts` alias, `NODE_ENV=production`, and the worker `execArgv`. Name files `*.rsc-test.{ts,tsx}` and run `pnpm test:unit:rsc`. The main vitest project must NOT set `react-server` (it would flip React to the no-hooks server build).
84
+ - A component that imports a server API (`getRequestContext`, `cookies`) from the bare `@rangojs/router` barrel works ONLY with the `index.rsc.ts` alias wired (see `./setup.md`); without it the bare import resolves to the throwing out-of-react-server stub. `renderToFlightString` / `renderServerTree` self-diagnose this exact misconfiguration — they reject with an actionable message naming `rangoTestAliases`, rather than surfacing the opaque stub error. Pure-leaf components that take all data as props need no barrel import and are the simplest case.
85
+ - `toMatchFlight` is containment (substring), not equality — the row framing (prefixes/quoting) is an internal serializer detail, so pin the rendered text/shape, not the framing. `toMatchFlightSnapshot()` snapshots the normalized payload; run under `NODE_ENV=production` for the cleanest, most stable bytes.
86
+ - No hydration / no interaction here — that is the e2e tier. For typed assertions on a client boundary's props (a `Date` back as a `Date`), or to confirm an island actually crossed the boundary, use `renderServerTree` (see `./server-tree.md`).
87
+
88
+ ## See also
89
+
90
+ - `/route` — the DSL this tests
91
+ - Siblings: `./setup.md`, `./server-tree.md`, `./render-handler.md`
92
+ - Long-form prose: [docs/testing.md](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md) — section "renderToFlightString — real async Server Components"
@@ -0,0 +1,129 @@
1
+ # Testing a handle — collectHandle, plus the loader and client read paths
2
+
3
+ **Layer:** unit (node + DOM) · **Import:** `@rangojs/router/testing` (collectHandle), `@rangojs/router/testing/dom` (renderRoute) · **DSL it tests:** a handle e.g. Breadcrumbs/Meta (see `/handler-use`, `/breadcrumbs`)
4
+
5
+ A handle's `collect`/accumulator (the `createHandle(collect)` argument that maps per-segment pushed values into one accumulated result) is otherwise unreachable — `createHandle` keeps it in a private registry keyed by `$$id`. These three primitives test it from different angles: `collectHandle` runs the REAL registered collect on per-segment values you SEED; `runLoader` seeds the POST-collect accumulated value a loader reads after the barrier; `renderRoute` seeds the RAW pushed values for a client component reading `useHandle`. None of them run the real push -> accumulate -> barrier wiring (that stays e2e).
6
+
7
+ ## API
8
+
9
+ ### `collectHandle(handle, segments)` — `src/testing/collect-handle.ts`
10
+
11
+ | Param | Type | Meaning |
12
+ | ---------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
13
+ | `handle` | `Handle<TData, TAccumulated>` | The handle whose registered collect to run. |
14
+ | `segments` | `ReadonlyArray<ReadonlyArray<TData>>` | Per-segment pushed values, one inner array per route segment, in **parent -> child** order. Empty inner arrays are filtered before the collect runs (matching production `collectHandleData` — a segment that pushed nothing is not passed through). |
15
+
16
+ **Returns** `TAccumulated` — exactly what the handle's collect produces (a default-flatten array, or a custom accumulator's value). If the handle's module was never imported (collect unregistered), it warns and falls back to `segments.flat()`.
17
+
18
+ ### runLoader option — `handles` — `src/testing/run-loader.ts`
19
+
20
+ | Field | Type | Meaning |
21
+ | ---------- | ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
22
+ | `handles` | `ReadonlyArray<readonly [Handle, unknown]>` | Seeds the value `ctx.use(SomeHandle)` returns — the POST-collect **ACCUMULATED** value (singular `unknown`), what a loader reads after `await ctx.rendered()`. Matched by handle reference. Pair with `rendered`. |
23
+ | `rendered` | `boolean \| (() => void \| Promise<void>)` | Mocks the `ctx.rendered()` barrier (throws by default). `true` resolves it immediately; a function controls timing/side effects. A `ctx.use(handle)` read before the barrier settles throws, exactly as in production. |
24
+
25
+ ### renderRoute option — `handles` — `src/testing/render-route.tsx`
26
+
27
+ | Field | Type | Meaning |
28
+ | --------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
29
+ | `handles` | `ReadonlyArray<readonly [Handle, unknown[]]>` | Seeds the CLIENT read path for `useHandle(handle)` — the RAW **pushed values array** (`unknown[]`), the values a route's handlers would have pushed. Attached to the leaf route segment under the handle's `$$id`, so `useHandle` runs the handle's REAL collect on them. |
30
+
31
+ **Shape contrast:** `renderRoute` feeds the barrier INPUT (the pushes, `unknown[]`); `runLoader` feeds its OUTPUT (the single accumulated value, `unknown`).
32
+
33
+ **Across navigation:** seeded `handles` are applied once at the initial render and PERSIST across `router.navigate()` within the same test (like `loaderData`) — unlike a real navigation, which re-runs handlers. A layout/page reading `useHandle` still resolves the seeded values after `navigate()`.
34
+
35
+ ## Recipe
36
+
37
+ ```ts
38
+ // collectHandle.test.ts — the pure collect, no route match
39
+ import { describe, it, expect } from "vitest";
40
+ import { collectHandle } from "@rangojs/router/testing";
41
+ import { createHandle } from "@rangojs/router";
42
+
43
+ const Breadcrumbs = createHandle<{ label: string; href: string }>(); // default flatten
44
+
45
+ it("flattens per-segment crumbs in parent->child order", () => {
46
+ const home = { label: "Home", href: "/" };
47
+ const post = { label: "P", href: "/p" };
48
+ expect(collectHandle(Breadcrumbs, [[home], [post]])).toEqual([home, post]);
49
+ });
50
+
51
+ it("runs a custom 'last wins' collect", () => {
52
+ const PageTitle = createHandle<string, string>((s) => s.flat().at(-1) ?? "");
53
+ expect(collectHandle(PageTitle, [["Home"], ["Products"], ["Shoes"]])).toBe(
54
+ "Shoes",
55
+ );
56
+ });
57
+ ```
58
+
59
+ ```ts
60
+ // loader-reads-handle.test.ts — a loader reading accumulated handle data after the barrier
61
+ import { it, expect } from "vitest";
62
+ import { runLoader } from "@rangojs/router/testing";
63
+ import { RenderedProducts } from "../src/handles"; // a createHandle(...)
64
+
65
+ const livePricesBody = async (ctx) => {
66
+ await ctx.rendered(); // barrier: handle data is now readable
67
+ const ids = ctx.use(RenderedProducts) as string[];
68
+ return ids.map((id) => ({ id, price: 9.99 }));
69
+ };
70
+
71
+ it("reads the accumulated handle value (seed the OUTPUT, mock the barrier)", async () => {
72
+ const data = await runLoader(livePricesBody, {
73
+ rendered: true,
74
+ handles: [[RenderedProducts, ["widget-a", "widget-b"]]], // singular accumulated value
75
+ });
76
+ expect(data).toEqual([
77
+ { id: "widget-a", price: 9.99 },
78
+ { id: "widget-b", price: 9.99 },
79
+ ]);
80
+ });
81
+ ```
82
+
83
+ ```tsx
84
+ // breadcrumb-trail.test.tsx — a client component reading useHandle
85
+ // @vitest-environment happy-dom
86
+ import { it, expect, afterEach } from "vitest";
87
+ import { cleanup } from "@testing-library/react";
88
+ import { renderRoute } from "@rangojs/router/testing/dom";
89
+ import { useHandle } from "@rangojs/router/client";
90
+ import { Breadcrumbs } from "../src/handles";
91
+
92
+ afterEach(cleanup);
93
+
94
+ function BreadcrumbTrail() {
95
+ const crumbs = useHandle(Breadcrumbs); // accumulated client-side via the real collect
96
+ return <nav>{crumbs.map((c) => c.label).join(" / ")}</nav>;
97
+ }
98
+
99
+ it("renders the seeded trail (seed the INPUT pushes, the collect runs)", async () => {
100
+ const { getByText } = await renderRoute(
101
+ [{ path: "/p", Component: BreadcrumbTrail }],
102
+ {
103
+ handles: [
104
+ [
105
+ Breadcrumbs,
106
+ [
107
+ { label: "Home", href: "/" },
108
+ { label: "P", href: "/p" },
109
+ ],
110
+ ],
111
+ ], // raw pushes array
112
+ },
113
+ );
114
+ expect(getByText("Home / P")).toBeTruthy();
115
+ });
116
+ ```
117
+
118
+ ## Caveats
119
+
120
+ - `collectHandle` tests the pure collect/accumulator in ISOLATION (parent -> child segment order, empty arrays filtered to match production). It does NOT run the real push -> accumulate -> barrier wiring — that stays e2e.
121
+ - renderRoute `handles` seeds the CLIENT read path with the RAW pushed values array (`unknown[]`), attached to the leaf segment. Handle data accumulates GLOBALLY (not segment-scoped like loaders), so a LAYOUT reading the same handle sees the seeded values too, not just the leaf route.
122
+ - runLoader `handles` seeds the POST-collect ACCUMULATED value (singular `unknown`) a loader reads after `await ctx.rendered()`; pair with `{ rendered: true }`. Shape contrast: renderRoute feeds the barrier INPUT (pushes[]), runLoader feeds its OUTPUT (the accumulated value).
123
+ - The renderRoute path is the CLIENT tree only: it does NOT catch server/client boundary remount bugs, real Flight serialization errors, or loader execution.
124
+
125
+ ## See also
126
+
127
+ - `/handler-use`, `/breadcrumbs` — the DSL this tests
128
+ - Siblings: `./loader.md`, `./client-components.md`, `./render-handler.md`
129
+ - Long-form prose: [docs/testing.md](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md) — section "Testing a handle's collect/accumulator"