@rangojs/router 0.0.0-experimental.b9cb8739 → 0.0.0-experimental.bd6e11bc

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 (285) hide show
  1. package/README.md +196 -43
  2. package/dist/bin/rango.js +277 -99
  3. package/dist/testing/vitest.js +48 -0
  4. package/dist/vite/index.js +2779 -1064
  5. package/dist/vite/index.js.bak +5448 -0
  6. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  7. package/package.json +57 -11
  8. package/skills/breadcrumbs/SKILL.md +3 -1
  9. package/skills/bundle-analysis/SKILL.md +159 -0
  10. package/skills/cache-guide/SKILL.md +243 -21
  11. package/skills/caching/SKILL.md +155 -6
  12. package/skills/composability/SKILL.md +27 -2
  13. package/skills/document-cache/SKILL.md +78 -55
  14. package/skills/handler-use/SKILL.md +364 -0
  15. package/skills/hooks/SKILL.md +229 -20
  16. package/skills/host-router/SKILL.md +45 -20
  17. package/skills/i18n/SKILL.md +276 -0
  18. package/skills/intercept/SKILL.md +46 -4
  19. package/skills/layout/SKILL.md +28 -7
  20. package/skills/links/SKILL.md +249 -17
  21. package/skills/loader/SKILL.md +273 -53
  22. package/skills/middleware/SKILL.md +49 -12
  23. package/skills/migrate-nextjs/SKILL.md +562 -0
  24. package/skills/migrate-react-router/SKILL.md +769 -0
  25. package/skills/mime-routes/SKILL.md +27 -0
  26. package/skills/observability/SKILL.md +137 -0
  27. package/skills/parallel/SKILL.md +197 -6
  28. package/skills/prerender/SKILL.md +123 -100
  29. package/skills/rango/SKILL.md +242 -22
  30. package/skills/react-compiler/SKILL.md +168 -0
  31. package/skills/response-routes/SKILL.md +66 -9
  32. package/skills/route/SKILL.md +88 -4
  33. package/skills/router-setup/SKILL.md +90 -5
  34. package/skills/server-actions/SKILL.md +751 -0
  35. package/skills/streams-and-websockets/SKILL.md +283 -0
  36. package/skills/testing/SKILL.md +716 -0
  37. package/skills/typesafety/SKILL.md +329 -27
  38. package/skills/use-cache/SKILL.md +34 -5
  39. package/skills/view-transitions/SKILL.md +294 -0
  40. package/src/__augment-tests__/augment.ts +81 -0
  41. package/src/__augment-tests__/augmented.check.ts +117 -0
  42. package/src/__internal.ts +1 -1
  43. package/src/browser/action-coordinator.ts +53 -36
  44. package/src/browser/app-shell.ts +52 -0
  45. package/src/browser/app-version.ts +14 -0
  46. package/src/browser/event-controller.ts +91 -70
  47. package/src/browser/history-state.ts +21 -0
  48. package/src/browser/index.ts +3 -3
  49. package/src/browser/navigation-bridge.ts +102 -16
  50. package/src/browser/navigation-client.ts +164 -59
  51. package/src/browser/navigation-store.ts +75 -17
  52. package/src/browser/navigation-transaction.ts +21 -37
  53. package/src/browser/partial-update.ts +139 -38
  54. package/src/browser/prefetch/cache.ts +175 -15
  55. package/src/browser/prefetch/fetch.ts +180 -33
  56. package/src/browser/prefetch/queue.ts +123 -20
  57. package/src/browser/prefetch/resource-ready.ts +77 -0
  58. package/src/browser/rango-state.ts +53 -13
  59. package/src/browser/react/Link.tsx +81 -9
  60. package/src/browser/react/NavigationProvider.tsx +110 -33
  61. package/src/browser/react/context.ts +7 -2
  62. package/src/browser/react/filter-segment-order.ts +51 -7
  63. package/src/browser/react/index.ts +3 -0
  64. package/src/browser/react/location-state-shared.ts +175 -4
  65. package/src/browser/react/location-state.ts +39 -13
  66. package/src/browser/react/use-handle.ts +23 -64
  67. package/src/browser/react/use-navigation.ts +22 -2
  68. package/src/browser/react/use-params.ts +20 -8
  69. package/src/browser/react/use-reverse.ts +106 -0
  70. package/src/browser/react/use-router.ts +43 -10
  71. package/src/browser/react/use-segments.ts +11 -8
  72. package/src/browser/response-adapter.ts +25 -0
  73. package/src/browser/rsc-router.tsx +191 -74
  74. package/src/browser/scroll-restoration.ts +41 -14
  75. package/src/browser/segment-reconciler.ts +36 -9
  76. package/src/browser/segment-structure-assert.ts +2 -2
  77. package/src/browser/server-action-bridge.ts +31 -36
  78. package/src/browser/types.ts +57 -5
  79. package/src/build/collect-fallback-refs.ts +107 -0
  80. package/src/build/generate-manifest.ts +65 -40
  81. package/src/build/generate-route-types.ts +5 -0
  82. package/src/build/index.ts +2 -0
  83. package/src/build/route-trie.ts +52 -25
  84. package/src/build/route-types/codegen.ts +4 -4
  85. package/src/build/route-types/include-resolution.ts +9 -2
  86. package/src/build/route-types/per-module-writer.ts +7 -4
  87. package/src/build/route-types/router-processing.ts +278 -88
  88. package/src/build/route-types/scan-filter.ts +9 -2
  89. package/src/build/route-types/source-scan.ts +118 -0
  90. package/src/build/runtime-discovery.ts +9 -20
  91. package/src/cache/cache-runtime.ts +15 -11
  92. package/src/cache/cache-scope.ts +76 -49
  93. package/src/cache/cf/cf-cache-store.ts +501 -18
  94. package/src/cache/cf/index.ts +5 -1
  95. package/src/cache/document-cache.ts +17 -7
  96. package/src/cache/index.ts +1 -0
  97. package/src/cache/taint.ts +55 -0
  98. package/src/client.rsc.tsx +3 -0
  99. package/src/client.tsx +94 -238
  100. package/src/context-var.ts +72 -2
  101. package/src/debug.ts +2 -2
  102. package/src/decode-loader-results.ts +36 -0
  103. package/src/errors.ts +30 -1
  104. package/src/handle.ts +65 -12
  105. package/src/host/index.ts +2 -2
  106. package/src/host/router.ts +129 -57
  107. package/src/host/types.ts +31 -2
  108. package/src/host/utils.ts +1 -1
  109. package/src/href-client.ts +140 -20
  110. package/src/index.rsc.ts +12 -5
  111. package/src/index.ts +61 -11
  112. package/src/loader-store.ts +500 -0
  113. package/src/loader.rsc.ts +2 -5
  114. package/src/loader.ts +3 -10
  115. package/src/missing-id-error.ts +68 -0
  116. package/src/outlet-context.ts +1 -1
  117. package/src/prerender/store.ts +5 -4
  118. package/src/prerender.ts +141 -80
  119. package/src/response-utils.ts +37 -0
  120. package/src/reverse.ts +65 -15
  121. package/src/route-content-wrapper.tsx +6 -28
  122. package/src/route-definition/dsl-helpers.ts +435 -260
  123. package/src/route-definition/helper-factories.ts +29 -139
  124. package/src/route-definition/helpers-types.ts +110 -34
  125. package/src/route-definition/index.ts +3 -0
  126. package/src/route-definition/redirect.ts +11 -3
  127. package/src/route-definition/resolve-handler-use.ts +155 -0
  128. package/src/route-definition/use-item-types.ts +32 -0
  129. package/src/route-map-builder.ts +7 -1
  130. package/src/route-types.ts +37 -41
  131. package/src/router/basename.ts +14 -0
  132. package/src/router/content-negotiation.ts +113 -1
  133. package/src/router/error-handling.ts +1 -1
  134. package/src/router/find-match.ts +4 -2
  135. package/src/router/handler-context.ts +77 -38
  136. package/src/router/intercept-resolution.ts +15 -22
  137. package/src/router/lazy-includes.ts +12 -9
  138. package/src/router/loader-resolution.ts +174 -22
  139. package/src/router/logging.ts +5 -2
  140. package/src/router/manifest.ts +31 -16
  141. package/src/router/match-api.ts +128 -192
  142. package/src/router/match-handlers.ts +63 -20
  143. package/src/router/match-middleware/background-revalidation.ts +30 -2
  144. package/src/router/match-middleware/cache-lookup.ts +136 -106
  145. package/src/router/match-middleware/cache-store.ts +54 -10
  146. package/src/router/match-middleware/intercept-resolution.ts +9 -7
  147. package/src/router/match-middleware/segment-resolution.ts +61 -5
  148. package/src/router/match-result.ts +125 -10
  149. package/src/router/metrics.ts +7 -2
  150. package/src/router/middleware-types.ts +21 -34
  151. package/src/router/middleware.ts +103 -90
  152. package/src/router/navigation-snapshot.ts +182 -0
  153. package/src/router/pattern-matching.ts +101 -17
  154. package/src/router/prerender-match.ts +110 -10
  155. package/src/router/preview-match.ts +32 -102
  156. package/src/router/request-classification.ts +286 -0
  157. package/src/router/revalidation.ts +58 -2
  158. package/src/router/route-snapshot.ts +245 -0
  159. package/src/router/router-context.ts +6 -1
  160. package/src/router/router-interfaces.ts +77 -28
  161. package/src/router/router-options.ts +76 -11
  162. package/src/router/router-registry.ts +2 -5
  163. package/src/router/segment-resolution/fresh.ts +223 -24
  164. package/src/router/segment-resolution/helpers.ts +29 -24
  165. package/src/router/segment-resolution/loader-cache.ts +1 -0
  166. package/src/router/segment-resolution/revalidation.ts +466 -285
  167. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  168. package/src/router/segment-wrappers.ts +2 -0
  169. package/src/router/substitute-pattern-params.ts +56 -0
  170. package/src/router/telemetry.ts +99 -0
  171. package/src/router/trie-matching.ts +18 -13
  172. package/src/router/types.ts +9 -0
  173. package/src/router/url-params.ts +49 -0
  174. package/src/router.ts +91 -23
  175. package/src/rsc/handler-context.ts +2 -2
  176. package/src/rsc/handler.ts +440 -381
  177. package/src/rsc/helpers.ts +91 -43
  178. package/src/rsc/index.ts +1 -1
  179. package/src/rsc/loader-fetch.ts +23 -3
  180. package/src/rsc/manifest-init.ts +5 -1
  181. package/src/rsc/origin-guard.ts +28 -10
  182. package/src/rsc/progressive-enhancement.ts +18 -2
  183. package/src/rsc/response-route-handler.ts +46 -53
  184. package/src/rsc/rsc-rendering.ts +41 -48
  185. package/src/rsc/runtime-warnings.ts +9 -10
  186. package/src/rsc/server-action.ts +25 -37
  187. package/src/rsc/ssr-setup.ts +18 -2
  188. package/src/rsc/types.ts +17 -3
  189. package/src/search-params.ts +4 -4
  190. package/src/segment-content-promise.ts +67 -0
  191. package/src/segment-loader-promise.ts +122 -0
  192. package/src/segment-system.tsx +219 -67
  193. package/src/serialize.ts +243 -0
  194. package/src/server/context.ts +277 -61
  195. package/src/server/cookie-store.ts +28 -4
  196. package/src/server/handle-store.ts +19 -0
  197. package/src/server/loader-registry.ts +9 -8
  198. package/src/server/request-context.ts +204 -60
  199. package/src/ssr/index.tsx +9 -1
  200. package/src/static-handler.ts +19 -7
  201. package/src/testing/cache-status.ts +166 -0
  202. package/src/testing/collect-handle.ts +63 -0
  203. package/src/testing/dispatch.ts +440 -0
  204. package/src/testing/dom.entry.ts +22 -0
  205. package/src/testing/e2e/fixture.ts +154 -0
  206. package/src/testing/e2e/index.ts +149 -0
  207. package/src/testing/e2e/matchers.ts +51 -0
  208. package/src/testing/e2e/page-helpers.ts +272 -0
  209. package/src/testing/e2e/parity.ts +306 -0
  210. package/src/testing/e2e/server.ts +183 -0
  211. package/src/testing/flight-matchers.ts +104 -0
  212. package/src/testing/flight-runtime.d.ts +21 -0
  213. package/src/testing/flight.entry.ts +22 -0
  214. package/src/testing/flight.ts +182 -0
  215. package/src/testing/generated-routes.ts +223 -0
  216. package/src/testing/index.ts +106 -0
  217. package/src/testing/internal/context.ts +255 -0
  218. package/src/testing/render-route.tsx +565 -0
  219. package/src/testing/run-loader.ts +296 -0
  220. package/src/testing/run-middleware.ts +179 -0
  221. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  222. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  223. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  224. package/src/testing/vitest-stubs/version.ts +5 -0
  225. package/src/testing/vitest.ts +183 -0
  226. package/src/types/cache-types.ts +4 -4
  227. package/src/types/global-namespace.ts +39 -26
  228. package/src/types/handler-context.ts +194 -72
  229. package/src/types/index.ts +1 -0
  230. package/src/types/loader-types.ts +41 -15
  231. package/src/types/request-scope.ts +126 -0
  232. package/src/types/route-entry.ts +19 -1
  233. package/src/types/segments.ts +37 -1
  234. package/src/urls/include-helper.ts +34 -67
  235. package/src/urls/index.ts +0 -3
  236. package/src/urls/path-helper-types.ts +50 -9
  237. package/src/urls/path-helper.ts +63 -63
  238. package/src/urls/pattern-types.ts +48 -19
  239. package/src/urls/response-types.ts +25 -22
  240. package/src/urls/type-extraction.ts +26 -116
  241. package/src/urls/urls-function.ts +1 -5
  242. package/src/use-loader.tsx +487 -44
  243. package/src/vite/debug.ts +185 -0
  244. package/src/vite/discovery/bundle-postprocess.ts +34 -37
  245. package/src/vite/discovery/discover-routers.ts +105 -51
  246. package/src/vite/discovery/discovery-errors.ts +194 -0
  247. package/src/vite/discovery/gate-state.ts +171 -0
  248. package/src/vite/discovery/prerender-collection.ts +188 -93
  249. package/src/vite/discovery/route-types-writer.ts +40 -84
  250. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  251. package/src/vite/discovery/state.ts +46 -6
  252. package/src/vite/discovery/virtual-module-codegen.ts +13 -23
  253. package/src/vite/index.ts +6 -0
  254. package/src/vite/plugin-types.ts +111 -72
  255. package/src/vite/plugins/cjs-to-esm.ts +8 -7
  256. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  257. package/src/vite/plugins/client-ref-hashing.ts +28 -5
  258. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  259. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  260. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  261. package/src/vite/plugins/expose-action-id.ts +55 -33
  262. package/src/vite/plugins/expose-id-utils.ts +24 -8
  263. package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
  264. package/src/vite/plugins/expose-ids/handler-transform.ts +12 -35
  265. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
  266. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  267. package/src/vite/plugins/expose-internal-ids.ts +544 -317
  268. package/src/vite/plugins/performance-tracks.ts +92 -0
  269. package/src/vite/plugins/refresh-cmd.ts +88 -26
  270. package/src/vite/plugins/use-cache-transform.ts +65 -50
  271. package/src/vite/plugins/version-injector.ts +39 -23
  272. package/src/vite/plugins/version-plugin.ts +72 -3
  273. package/src/vite/plugins/virtual-entries.ts +2 -2
  274. package/src/vite/rango.ts +265 -226
  275. package/src/vite/router-discovery.ts +920 -137
  276. package/src/vite/utils/ast-handler-extract.ts +15 -15
  277. package/src/vite/utils/banner.ts +4 -4
  278. package/src/vite/utils/bundle-analysis.ts +4 -2
  279. package/src/vite/utils/client-chunks.ts +190 -0
  280. package/src/vite/utils/forward-user-plugins.ts +193 -0
  281. package/src/vite/utils/manifest-utils.ts +21 -5
  282. package/src/vite/utils/package-resolution.ts +41 -1
  283. package/src/vite/utils/prerender-utils.ts +38 -5
  284. package/src/vite/utils/shared-utils.ts +109 -27
  285. package/src/browser/action-response-classifier.ts +0 -99
@@ -0,0 +1,716 @@
1
+ ---
2
+ name: testing
3
+ description: Test @rangojs/router apps — unit (loaders/middleware/reverse/components), integration (dispatch/Flight), and e2e (dev+prod parity, progressive enhancement)
4
+ argument-hint: [layer]
5
+ ---
6
+
7
+ # Testing @rangojs/router apps
8
+
9
+ Rango ships six consumer-facing testing entries, one per test runtime/dependency:
10
+ `@rangojs/router/testing` (unit + integration, under a Vite-driven Vitest
11
+ project), `@rangojs/router/testing/vitest` (the `rangoTestConfig`/`rangoTestAliases` setup preset),
12
+ `@rangojs/router/testing/dom` (`renderRoute`, needs RTL + a DOM env),
13
+ `@rangojs/router/testing/e2e` (the Playwright harness),
14
+ `@rangojs/router/testing/flight` (real Flight, react-server condition only), and
15
+ `@rangojs/router/testing/flight-matchers` (the Flight matchers).
16
+ The hard problem in an RSC app is that the layer you reach for is dictated by
17
+ **what the behavior touches** — a pure predicate is a one-line vitest test; a
18
+ real async Server Component cannot be a plain node test at all. Pick the layer
19
+ **first**, then the primitive. Reaching one layer too high (e2e for a reverse
20
+ function) is slow; one too low (a node test for Flight) fails to compile or
21
+ silently asserts nothing.
22
+
23
+ Compatibility (the setup that bit the first installed consumer — read before
24
+ writing `vitest.config.ts`):
25
+
26
+ - **Node >= 23:** use **`rangoTestConfig()`**, not the bare `rangoTestAliases()`.
27
+ `@rangojs/router` is consumed as SOURCE (its exports resolve to `./src/*.ts`),
28
+ and Node >= 23 refuses to type-strip `.ts` under `node_modules`
29
+ (`ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING`). `rangoTestConfig` ships as
30
+ compiled JS (so the config itself loads under Node) AND adds the required
31
+ `server.deps.inline: [/@rangojs[/\\]router/]` so Vite — not Node — transpiles
32
+ rango's source under test. With bare `rangoTestAliases` you must wire
33
+ `deps.inline` yourself.
34
+ - **Vitest:** the rango fragment goes under `test` (`test.alias` +
35
+ `test.server.deps.inline`, both returned by `rangoTestConfig`). The node/DOM
36
+ project keeps React as its CLIENT build; the Flight project uses the
37
+ `react-server` condition in a separate `vitest.rsc.config.ts`.
38
+
39
+ For the prose guide with full setup and migration, see
40
+ [`docs/testing.md`](https://github.com/ivogt/vite-rsc/blob/main/packages/rangojs-router/docs/testing.md)
41
+ (the `docs/` directory is not shipped in the published package, so this is an
42
+ absolute link).
43
+
44
+ ## When to use
45
+
46
+ Use this skill when adding or changing tests for a Rango app: a loader,
47
+ middleware, a route map, a client component, a response route, cache/SWR
48
+ behavior, prerender, or a navigation/PE flow.
49
+
50
+ Two non-negotiable mandates (from the repo's `CLAUDE.md`, and they apply to
51
+ consumer apps too):
52
+
53
+ - **Every e2e covers BOTH dev and production.** A dev-only e2e is not
54
+ acceptable. Use `parityDescribe` — it generates the dev and production
55
+ describes from one body, so you cannot forget the prod half.
56
+ - **Progressive-enhancement parity** is a first-class assertion. A form-driven
57
+ flow must produce the same observable result with JS on and JS off. Use
58
+ `expectParity`.
59
+
60
+ ## The read-first shape
61
+
62
+ Four import roots, each matched to the dependency/runtime that can load it —
63
+ this split is forced by hard walls, not preference:
64
+
65
+ - `@rangojs/router/testing` — unit + integration primitives. Run these under a
66
+ **Vite-driven Vitest** project with the rango Vite plugin active (the router
67
+ internals import the `@rangojs/router:version` virtual module; without the
68
+ plugin, alias `@rangojs/router:version`). It references neither React,
69
+ `@testing-library/react`, Playwright, nor the RSC runtime — a unit suite
70
+ testing only loaders/middleware/`dispatch` pulls in none of them.
71
+ - `@rangojs/router/testing/dom` — `renderRoute` (the RTL component stub). Kept
72
+ separate so the unit barrel above stays free of React/RTL; it lazy-loads
73
+ `@testing-library/react` at call time and needs a DOM env (happy-dom/jsdom).
74
+ - `@rangojs/router/testing/e2e` — the Playwright harness. Kept separate so it
75
+ loads in a plain (non-Vite) Playwright runner; the unit barrel pulls in
76
+ router-manifest code that a Playwright loader cannot resolve. The helpers take
77
+ your `test`/`expect` as parameters, so this entry never imports
78
+ `@playwright/test` at runtime.
79
+ - `@rangojs/router/testing/flight` — real Flight rendering. Its serializer loads
80
+ only under the `react-server` node condition; pulling it elsewhere throws.
81
+
82
+ The single rule that drives everything:
83
+
84
+ > **If the behavior needs a real Flight render, it cannot be a plain vitest node
85
+ > test.** It is either `renderToFlightString` (under the react-server vitest
86
+ > project) or an e2e test. There is no middle ground in node.
87
+
88
+ ## Decision tree: behavior -> layer -> primitive
89
+
90
+ | The behavior is… | Layer | Primitive | Import root |
91
+ | --------------------------------------------------------------------------------------------------------- | ------------ | ---------------------------------------------------------------- | -------------------------------- |
92
+ | a pure function / `reverse` / a predicate (`revalidate`, `isAction`) | unit (node) | call it directly; `runMiddleware`/`runLoader` for ctx | `@rangojs/router/testing` |
93
+ | one loader's data logic | unit (node) | `runLoader` (pass the **raw fn**, not `createLoader`) | `@rangojs/router/testing` |
94
+ | one middleware's ordering / short-circuit / cookie+header merge | unit (node) | `runMiddleware` | `@rangojs/router/testing` |
95
+ | a CLIENT component reading router context (`useParams`/`useReverse`/`Outlet`/`useNavigation`/`useLoader`) | unit (DOM) | `renderRoute` (needs happy-dom/jsdom + `@testing-library/react`) | `@rangojs/router/testing/dom` |
96
+ | a redirect / status / headers / cookies / **response route** (json/text/html/xml/md), no Flight | integration | `dispatch` (router -> Response) | `@rangojs/router/testing` |
97
+ | a real async **Server Component** / Flight serialization shape | RSC unit | `renderToFlightString` + `toMatchFlight` | `@rangojs/router/testing/flight` |
98
+ | navigation, hydration, PE parity, view transitions, real SSR | e2e | `createRangoE2E` -> `parityDescribe`/`expectParity` | `@rangojs/router/testing/e2e` |
99
+ | cache hit/miss/stale, prerender (= a cache hit by design) | e2e + signal | `assertCacheStatus` / telemetry sink (gate on) | `@rangojs/router/testing` |
100
+ | generated route map drift vs runtime | unit (node) | `assertGeneratedRoutesMatch` | `@rangojs/router/testing` |
101
+
102
+ Cross-references: `/loader`, `/middleware`, `/server-actions`, `/caching`,
103
+ `/prerender`, `/typesafety`.
104
+
105
+ ## Unit recipes (vitest, node)
106
+
107
+ ### runMiddleware — ordering, short-circuit, cookie/header merge
108
+
109
+ Runs the chain through the router's **real** `executeMiddleware`, so
110
+ `next()`, return-Response short-circuit, throw-Response short-circuit,
111
+ double-next guards, and header/cookie merging behave exactly as in production.
112
+ `nextCalled` is `0` on short-circuit, `1` on pass-through. The result also
113
+ carries `cookies` (the effective `{ name: value }` view — assert a cookie the
114
+ chain set without casting through the `@internal` `ctx.cookies()`). The returned
115
+ `ctx` is the underlying `RequestContext` for anything else (`ctx.get(...)`,
116
+ `ctx.res.headers`).
117
+
118
+ ```ts
119
+ import { describe, it, expect } from "vitest";
120
+ import { runMiddleware } from "@rangojs/router/testing";
121
+ import type { Middleware } from "@rangojs/router";
122
+
123
+ const requireUser: Middleware = async (ctx, next) => {
124
+ if (!ctx.get("user")) return new Response(null, { status: 401 });
125
+ return next();
126
+ };
127
+
128
+ it("passes through when the user is present", async () => {
129
+ const { response, nextCalled } = await runMiddleware(
130
+ requireUser,
131
+ "/dashboard",
132
+ {
133
+ vars: { user: { id: 1 } }, // object form; or [[key, value]] tuples (key may be a createVar())
134
+ },
135
+ );
136
+ expect(nextCalled).toBe(1);
137
+ expect(response.status).toBe(200);
138
+ });
139
+
140
+ it("short-circuits (return OR throw Response) when unauthenticated", async () => {
141
+ const { response, nextCalled } = await runMiddleware(
142
+ requireUser,
143
+ "/dashboard",
144
+ );
145
+ expect(nextCalled).toBe(0);
146
+ expect(response.status).toBe(401);
147
+ });
148
+ ```
149
+
150
+ Seed prior-middleware state with `vars` (string key or `createVar()` handle).
151
+ Model the downstream route with `next`. Enable `ctx.reverse(...)` by passing
152
+ `routeMap` (and `routeName` for scoped `.name` resolution). Pass an array to run
153
+ several in order. Cookies set via `cookies().set(...)` surface on the result's
154
+ `cookies` and on the merged response `Set-Cookie`.
155
+
156
+ There is no `handles`/`rendered` option (only `runLoader` has them): middleware
157
+ runs BEFORE the render barrier, so it has no post-barrier handle access in
158
+ production — `ctx.use(Handle)` after `ctx.rendered()` is a loader/handler
159
+ capability, not a middleware one. Read handle data in a loader and test it with
160
+ `runLoader`'s `handles`/`rendered`.
161
+
162
+ ### runLoader — one loader's data logic
163
+
164
+ Pass the **RAW loader function** `(ctx) => ...`, NOT a `createLoader(...)`
165
+ handle. `createLoader` relies on the Vite `$$id` injection for RSC
166
+ registration, which does not exist in a bare vitest process — calling it gives
167
+ you a handle with no `fn` to run. `runLoader` invokes your function directly
168
+ against a real `RequestContext`, so cookies, headers, `ctx.get`, and
169
+ `ctx.reverse` resolve.
170
+
171
+ ```ts
172
+ import { runLoader } from "@rangojs/router/testing";
173
+ import { createVar } from "@rangojs/router";
174
+
175
+ // CORRECT: test the function body directly.
176
+ async function productLoaderBody(ctx) {
177
+ return { id: ctx.params.id, region: ctx.env.REGION, user: ctx.get(User) };
178
+ }
179
+
180
+ it("reads params, env, and seeded vars", async () => {
181
+ const User = createVar<{ name: string }>();
182
+ const data = await runLoader(productLoaderBody, {
183
+ params: { id: "42" },
184
+ env: { REGION: "eu" },
185
+ vars: [[User, { name: "Ada" }]],
186
+ });
187
+ expect(data).toEqual({ id: "42", region: "eu", user: { name: "Ada" } });
188
+ });
189
+ ```
190
+
191
+ Options: `params` (also surfaced as `routeParams`), `search`, `env`, `vars`,
192
+ `method`/`body`/`formData`, `routeMap`/`routeName` (for `ctx.reverse`), and
193
+ `use` (a resolver for `ctx.use(OtherLoader)` composition — without it, `ctx.use`
194
+ runs the dependency's own `fn` if it carries one).
195
+
196
+ Two unit-only limitations to document in your test, not work around:
197
+
198
+ - `ctx.reverse(...)` **throws** unless you pass `routeMap`.
199
+ - `ctx.rendered()` **throws** (the DSL render barrier only exists in a full
200
+ match) and `ctx.isAction(...)` (the action-render context) is not available —
201
+ test those with `renderToFlightString` or e2e.
202
+
203
+ If your real loader source is `export const L = createLoader(async (ctx) => {...})`,
204
+ extract the inner async function so it is importable on its own, and register
205
+ the `createLoader` wrapper in `urls()`. Then `runLoader` tests the body and the
206
+ DSL/e2e tests cover registration.
207
+
208
+ COOKIE SEEDING: there is no `cookies`/`headers` option — seed a request cookie by
209
+ passing a full `Request` with the header, `runLoader(body, { request: new
210
+ Request("https://app.test/", { headers: { Cookie: "sid=abc" } }) })`. A loader
211
+ that reads `cookies()` then sees `abc`. (`search`/`method` are baked onto this
212
+ request for you, so pass a `Request` only when you need headers/cookies.)
213
+
214
+ ### runInRequestContext — an action (or any fn) that reads request context
215
+
216
+ For a server ACTION (or any function) that authenticates off the request cookie
217
+ and calls `getRequestContext()` / `cookies()` but has no loader-context shape,
218
+ `runInRequestContext(fn, opts)` builds a real `RequestContext` (same `opts` as the
219
+ other primitives — `env`, `request`, `vars`, ...) AND enters it, so the function
220
+ runs exactly as in production. `fn` may be async; the context stays active across
221
+ its awaits. It returns `{ result, response, cookies, locationState }` so the
222
+ action's OUTPUT is assertable WITHOUT casting through the `@internal` `ctx.res` /
223
+ `ctx.cookies()`:
224
+
225
+ - `result` — fn's return value (awaited)
226
+ - `response` — a Response carrying the status, headers, and Set-Cookie the action set
227
+ - `cookies` — the effective `{ name: value }` cookie view after the run
228
+ - `locationState` — the flash the action set via `ctx.setLocationState()` / `redirect({ state })`, resolved to the `{ key: value }` the client reads
229
+
230
+ ```ts
231
+ import { runInRequestContext } from "@rangojs/router/testing";
232
+ import { loginAction } from "../src/actions/login"; // reads cookies(), sets a session cookie + flash
233
+
234
+ it("authorizes, sets the session cookie, and sets a flash", async () => {
235
+ const { result, cookies, locationState } = await runInRequestContext(
236
+ () => loginAction(input),
237
+ {
238
+ env,
239
+ request: new Request("https://app.test/admin", {
240
+ headers: { Cookie: "sid=abc" },
241
+ }),
242
+ },
243
+ );
244
+ expect(result).toMatchObject({ tenant: "acme" });
245
+ expect(cookies.session).toBeDefined(); // the cookie the action set, no @internal cast
246
+ expect(locationState).toEqual({ flash: { text: "Welcome back" } });
247
+ });
248
+ ```
249
+
250
+ For the low-level case where you already hold a context from
251
+ `createTestRequestContext(...)`, `runWithRequestContext(ctx, fn)` is re-exported
252
+ from `@rangojs/router/testing` to enter it directly; `runInRequestContext` is the
253
+ one-call convenience over the two.
254
+
255
+ ### Your bindings are your seam (env.DB / Durable Objects / R2)
256
+
257
+ The node primitives test the router's seams; the moment your loader/middleware/
258
+ action calls a **platform binding** (`env.DB`, a Durable Object stub, `env.R2`),
259
+ you have crossed out of rango and into your app's I/O. rango deliberately ships
260
+ **no doubles** for these — they are app- and schema-specific — so the double is
261
+ yours to build and inject through the `env` option every primitive already takes:
262
+
263
+ ```ts
264
+ await runLoader(bundleLoaderBody, { env: { DB: fakeD1 } });
265
+ await runMiddleware(requireMembership, "/t/acme/edit", { env: { DB: fakeD1 } });
266
+ await runInRequestContext(() => authorizeAction(input), {
267
+ env: { DB: fakeD1 },
268
+ request,
269
+ });
270
+ ```
271
+
272
+ Plan for this seam — it is usually the single biggest effort in a consumer unit
273
+ suite, and the work is in matching the **driver contract**, not the binding's
274
+ public API. The sharp edge: a `D1Database` double for **`drizzle-orm/d1`** must
275
+ serve **positional row arrays in schema-column order** for drizzle's `.raw()`
276
+ path (with the driver-level encodings so the decoder round-trips `Date`/JSON) —
277
+ NOT `{ column: value }` objects. A naive object-shaped double returns
278
+ silently-wrong or empty rows. That contract is per-method: drizzle-d1 serves
279
+ SELECTs through `.raw()` (the positional rows above), but writes
280
+ (INSERT/UPDATE/DELETE) go through `.run()`, which returns `{ success, meta }` (no
281
+ rows) and bypasses the row responder entirely — model BOTH paths, a read-only
282
+ `.raw()` double silently no-ops every write. Keep the double at the binding
283
+ boundary; never mock a rango primitive to dodge building it.
284
+
285
+ ### renderRoute — a client component reading router context
286
+
287
+ RTL-style stub. Peer of React Router's `createRoutesStub` / Expo's
288
+ `renderRouter`. It mounts the router's real `NavigationProvider` plus a
289
+ synthetic segment tree so `useParams`, `useReverse`, `useNavigation`, `Outlet`,
290
+ `usePathname`, `useSearchParams`, and `useLoader`/`useFetchLoader` (reading
291
+ **seeded** data) resolve — no server, no Vite, no Flight round-trip. It is
292
+ `async` (lazy-loads `@testing-library/react`).
293
+
294
+ ```tsx
295
+ // @vitest-environment happy-dom
296
+ import { describe, it, expect, afterEach } from "vitest";
297
+ import { cleanup } from "@testing-library/react";
298
+ import { renderRoute } from "@rangojs/router/testing/dom";
299
+ import { Outlet, useParams, useReverse } from "@rangojs/router/client";
300
+
301
+ afterEach(cleanup);
302
+
303
+ function Layout() {
304
+ return (
305
+ <div>
306
+ <span data-testid="shell">shell</span>
307
+ <Outlet />
308
+ </div>
309
+ );
310
+ }
311
+ function Product() {
312
+ const { productId } = useParams<{ productId: string }>();
313
+ const reverse = useReverse({ product: "/products/:productId" });
314
+ return (
315
+ <a data-testid="link" href={reverse("product", { productId: "2" })}>
316
+ {productId}
317
+ </a>
318
+ );
319
+ }
320
+
321
+ it("resolves params + reverse + Outlet through the layout chain", async () => {
322
+ const { getByTestId, router } = await renderRoute(
323
+ [
324
+ { path: "/products", Component: Layout }, // layout (root)
325
+ { path: "/products/:productId", Component: Product }, // leaf (last)
326
+ ],
327
+ { initialUrl: "/products/1" },
328
+ );
329
+ expect(getByTestId("shell").textContent).toBe("shell");
330
+ expect(getByTestId("link").getAttribute("href")).toBe("/products/2");
331
+
332
+ await router.navigate("/products/2"); // client-only nav, re-resolves the same routes
333
+ expect(router.pathname()).toBe("/products/2");
334
+ });
335
+ ```
336
+
337
+ `RenderRouteSpec = { path, Component, layout?, loaderIds?, name? }`. The array
338
+ is the layout chain root-to-leaf; the **last** entry is the leaf route. Seed
339
+ loader reads with `options.loaderData` keyed by the loader's `$$id`; attach a
340
+ loader to a specific layout via that spec's `loaderIds`:
341
+
342
+ ```tsx
343
+ const CartLoader = {
344
+ __brand: "loader",
345
+ $$id: "loaders/cart#CartLoader",
346
+ } as any;
347
+ await renderRoute(
348
+ [
349
+ { path: "/shop", Component: CartLayout, loaderIds: [CartLoader.$$id] },
350
+ { path: "/shop/item", Component: Page },
351
+ ],
352
+ { initialUrl: "/shop/item", loaderData: { [CartLoader.$$id]: { count: 3 } } },
353
+ );
354
+ ```
355
+
356
+ Seed `useHandle` reads with `handles: [[handle, pushedValues[]]]` and
357
+ `useLocationState` with `locationState: [[def, value]]` (both by reference).
358
+ Handle data is accumulated GLOBALLY (not segment-scoped like loaders), so a
359
+ LAYOUT component reading a handle (a `DetailLayout`/`ActionToolbar` reading
360
+ `EditTarget`/`PageEyebrow`) sees the seeded values, not just the leaf route.
361
+
362
+ Model an `include('/shop', …)` mount with the `mount` option: it wraps the
363
+ segment chain in a MountContext exactly as production, so `useMount()` returns
364
+ the prefix and `useHref`/`useReverse` resolve mount-prefixed URLs — a
365
+ mount-relative subtree (`/c/:slug` mounted under `/shop`) becomes reproducible at
366
+ the unit layer instead of e2e-only:
367
+
368
+ ```tsx
369
+ await renderRoute([{ path: "/c/wine", Component: PDP }], { mount: "/shop" });
370
+ // useMount() -> "/shop"; useReverse({ product: "/c/:slug" })("product", { slug: "wine" }) -> "/shop/c/wine"
371
+ ```
372
+
373
+ FIDELITY CAVEAT — this is the **client tree only**. It does NOT catch
374
+ server/client boundary reference-identity remount bugs, real Flight
375
+ serialization errors, loader execution, middleware, or handler ordering. Those
376
+ are `renderToFlightString` / e2e territory. Loader data is seeded, never run.
377
+ Needs a DOM env (`// @vitest-environment happy-dom`, or jsdom) and the consumer
378
+ must install `@testing-library/react` (optional peer).
379
+
380
+ CATCH — streaming `use(promise)` Suspense content (e.g. an async breadcrumb
381
+ `content: Promise<ReactNode>`): a plain `Promise.resolve(node)` does NOT flush
382
+ its Suspense retry in RTL/happy-dom (renderRoute renders internally, not inside
383
+ an awaited `act`), so the DOM stays on the fallback. Assert the **pending**
384
+ fallback with a never-resolving `new Promise(() => {})`; for the **arrived**
385
+ state pass an already-settled promise so `use()` reads it synchronously:
386
+ `const p = Promise.resolve(node) as any; p.status = "fulfilled"; p.value = node;`.
387
+ The real pending→resolved transition is an e2e concern.
388
+
389
+ ARIA GOTCHA — query a `<Link>` by `getByRole("link")` only when it renders a bare
390
+ anchor. An explicit `role` on the link (e.g. `<Link role="tab">` in a tablist)
391
+ OVERRIDES the anchor's implicit `link` role, so `getByRole("link")` finds
392
+ nothing — query the explicit role (`getByRole("tab")`) or fall back to
393
+ `getByText`/`getByTestId` and assert `getAttribute("href")`.
394
+
395
+ ### Type-level tests — make misuse fail to compile
396
+
397
+ The reverse/href/params/env types are a real contract; a wrong route name,
398
+ missing param, or unknown binding should be a COMPILE error, not a runtime
399
+ surprise. This is the highest signal-per-cost test in the suite, but it runs at
400
+ typecheck time, not in the vitest runner — so it is its own layer, wired into CI
401
+ as a real step (`pnpm run typecheck` / `tsc --noEmit`). Three recipes, smallest
402
+ first:
403
+
404
+ 1. Negative assertions with `@ts-expect-error` (a runtime test cannot do this) —
405
+ the directive ERRORS if the line below ever starts compiling, so a regressed
406
+ guard fails the typecheck:
407
+
408
+ ```ts
409
+ import { useReverse } from "@rangojs/router/client";
410
+ const reverse = useReverse({ product: "/products/:productId" });
411
+ reverse("product", { productId: "2" }); // ok
412
+ // @ts-expect-error missing required param
413
+ reverse("product", {});
414
+ // @ts-expect-error unknown route name
415
+ reverse("nope", {});
416
+ ```
417
+
418
+ 2. Positive assertions with vitest's `expectTypeOf` — for pinning an INFERRED
419
+ type (a loader's return, a parsed search schema, a handle's accumulated
420
+ shape), in a normal `*.test.ts`:
421
+
422
+ ```ts
423
+ import { expectTypeOf } from "vitest";
424
+ expectTypeOf(await runLoader(cartLoaderBody)).toEqualTypeOf<{
425
+ count: number;
426
+ }>();
427
+ ```
428
+
429
+ 3. A dedicated `*.test-d.ts` + `tsconfig.types.json` (extends base, includes only
430
+ those files; run `tsc -p tsconfig.types.json --noEmit`) for a large type
431
+ suite — the pattern rango itself uses for its augmentation contracts. Recipe 1
432
+ is enough for most apps; reach for 3 only when inline assertions clutter
433
+ runtime tests.
434
+
435
+ ## Integration recipes
436
+
437
+ ### dispatch — request -> Response, without Flight
438
+
439
+ In-process matching + middleware, no RSC render. Covers `308` redirects
440
+ (trailing slash etc.) with `Location`, `404`, response routes
441
+ (json/text/html/xml/md with content negotiation), and **global + route-level
442
+ middleware** short-circuits with full `next()`/throw/header+cookie fidelity. It
443
+ reuses the router's own `previewMatch`, so middleware collection is the router's,
444
+ not a re-implementation. Hitting an RSC (component) route throws a clear
445
+ directive error.
446
+
447
+ So `dispatch` IS the way to exercise a RESPONSE route's real route-level
448
+ middleware chain (the guard stack) against the actual registered tree. The gap:
449
+ a COMPONENT route's guard stack cannot run here (dispatch refuses it, and
450
+ `renderToFlightString`/`renderRoute` don't run route middleware) — assert that at
451
+ e2e, or extract the middleware fn and unit-test it with `runMiddleware`.
452
+
453
+ SETUP CAVEAT (use the preset): `@rangojs/router` resolves to server-only STUBS
454
+ outside the `react-server` condition (urls/createRouter/cookies/getRequestContext
455
+ throw), and importing your router also pulls `@vitejs/plugin-rsc/rsc` (whose body
456
+ imports Vite virtuals). Vitest does not apply the `react-server` condition to
457
+ bare-package resolution. The preset `@rangojs/router/testing/vitest` handles all
458
+ of it — alias `@rangojs/router` to real impls + stub the virtuals — so no
459
+ per-file `vi.mock` is needed. Spread `rangoTestConfig(...)` into your `test`
460
+ block:
461
+
462
+ ```ts
463
+ // vitest.config.ts
464
+ import { defineConfig } from "vitest/config";
465
+ import { rangoTestConfig } from "@rangojs/router/testing/vitest";
466
+ export default defineConfig({
467
+ test: {
468
+ globals: true,
469
+ include: ["test/**/*.test.{ts,tsx}"],
470
+ environment: "node",
471
+ ...rangoTestConfig({ cloudflare: true }),
472
+ },
473
+ });
474
+ ```
475
+
476
+ `rangoTestConfig` returns BOTH the resolve `alias` entries AND
477
+ `server.deps.inline: [/@rangojs[/\\]router/]`. The `deps.inline` half is
478
+ mandatory for an installed (node_modules) consumer: `@rangojs/router` ships as
479
+ TypeScript source, Vitest externalizes node_modules by default, and Node >= 23
480
+ refuses to type-strip `.ts` under `node_modules`
481
+ (`ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING`) — `deps.inline` forces Vite (not
482
+ Node) to transpile rango's source. The preset entry itself ships as compiled JS,
483
+ so the `import { rangoTestConfig }` line loads under plain Node config loading.
484
+ (If you need only the aliases, `rangoTestAliases(...)` is still exported, but then
485
+ you must wire `server.deps.inline` yourself.)
486
+
487
+ LIMITATION: the FULL router usually can't be imported in a bare test —
488
+ `Prerender()`/`createLoader()` need the plugin-injected `$$id` (real `Prerender()`
489
+ throws "missing $$id"). Build a router from a `Prerender`-free include (your API
490
+ routes); `dispatch` accepts the public router type with no cast:
491
+
492
+ ```ts
493
+ import { describe, it, expect } from "vitest";
494
+ import { dispatch } from "@rangojs/router/testing";
495
+ import { createRouter } from "@rangojs/router";
496
+ import { apiPatterns } from "../src/api/urls"; // path.json(...) routes only
497
+
498
+ const router = createRouter().routes(apiPatterns);
499
+
500
+ it("serializes a JSON response route (auto-wrapped under data)", async () => {
501
+ const res = await dispatch(router, "/health");
502
+ expect(res.status).toBe(200);
503
+ expect(await res.json()).toEqual({ data: { status: "ok" } });
504
+ });
505
+
506
+ it("maps a thrown RouterError to its status + typed JSON envelope", async () => {
507
+ const res = await dispatch(router, "/products/999");
508
+ expect(res.status).toBe(404);
509
+ expect((await res.json()).error.code).toBe("NOT_FOUND");
510
+ });
511
+ ```
512
+
513
+ ### renderToFlightString — real async Server Component
514
+
515
+ A REAL Flight render of an async Server Component, in plain node — but ONLY
516
+ under the `react-server` condition (see the next section for the vitest
517
+ project). The render runs inside a request context, so async components can call
518
+ `getRequestContext()`, read params, cookies, etc.
519
+
520
+ ```tsx
521
+ // flight.rsc-test.tsx (note the *.rsc-test suffix)
522
+ import { describe, it, expect } from "vitest";
523
+ import { renderToFlightString } from "@rangojs/router/testing/flight";
524
+ // Matchers are a SEPARATE subpath (they import vitest); renderToFlightString does not.
525
+ import { flightMatchers } from "@rangojs/router/testing/flight-matchers";
526
+
527
+ expect.extend(flightMatchers);
528
+
529
+ // Keep components PURE leaves: take data as props. Do NOT import a server API
530
+ // (getRequestContext, cookies) from the `@rangojs/router` barrel — under the
531
+ // react-server condition the bare specifier resolves to the throwing stub, so
532
+ // it cannot be flight-tested in a bare consumer project.
533
+ async function Greeting({ name }: { name: string }) {
534
+ await Promise.resolve();
535
+ return <div>Hello {name}!</div>;
536
+ }
537
+ async function ItemView({ id }: { id: string }) {
538
+ return <span>id={id}</span>;
539
+ }
540
+
541
+ it("renders text and props", async () => {
542
+ expect(await renderToFlightString(<Greeting name="Ada" />)).toMatchFlight(
543
+ "Ada",
544
+ );
545
+ expect(await renderToFlightString(<ItemView id="42" />)).toMatchFlight("42");
546
+ });
547
+
548
+ it("matches a normalized snapshot", async () => {
549
+ expect(
550
+ await renderToFlightString(<Greeting name="World" />),
551
+ ).toMatchFlightSnapshot();
552
+ });
553
+ ```
554
+
555
+ `toMatchFlight(substring)` asserts the normalized Flight string CONTAINS the
556
+ substring (containment, not equality — the row framing is an internal serializer
557
+ detail). `toMatchFlightSnapshot()` snapshots the normalized payload. SCOPE:
558
+ server-only / leaf trees — a client component emits an unresolved `I[...]` import
559
+ row against the empty client manifest (fine for snapshotting shape, not
560
+ hydratable). A true interactive, clickable DOM `renderServer` is a DEFERRED
561
+ follow-up: the react-server-vs-default condition wall requires a two-environment
562
+ setup. For interactive server-component behavior today, use e2e.
563
+
564
+ ## E2E recipes (Playwright)
565
+
566
+ Wire the harness once, passing your own Playwright `test`/`expect` (so
567
+ `@rangojs/router/testing/e2e` never imports `@playwright/test` at runtime — it is
568
+ an optional peer you install). Import the harness from the **`/e2e` entry** — the
569
+ unit barrel is not loadable in a plain Playwright runner:
570
+
571
+ ```ts
572
+ // e2e/helper.ts
573
+ import { test, expect } from "@playwright/test";
574
+ import { createRangoE2E } from "@rangojs/router/testing/e2e";
575
+
576
+ export const e2e = createRangoE2E({
577
+ test,
578
+ expect,
579
+ defaultRoot: new URL("..", import.meta.url).pathname, // your app root
580
+ });
581
+ export const { useFixture, parityDescribe, expectParity, rangoMatchers } = e2e;
582
+ ```
583
+
584
+ ### parityDescribe REPLACES hand-titling `(production)`
585
+
586
+ This is THE mechanism that satisfies the dev+prod mandate structurally. One
587
+ declaration registers a dev describe (`name`) AND a production describe
588
+ (`` `${name} (production)` ``) from one body — the `(production)` suffix is
589
+ generated, so the prod suite can never drift into the dev bucket. Use `f.url(...)`
590
+ for navigation.
591
+
592
+ ```ts
593
+ import { test, expect } from "@playwright/test";
594
+ import { parityDescribe, rangoMatchers } from "./helper";
595
+ // rangoMatchers ships the type augmentation, so `expect(page).toHaveRangoPathname`
596
+ // is typed after extend.
597
+ expect.extend(rangoMatchers);
598
+
599
+ parityDescribe("product navigation", (f) => {
600
+ test("navigates to a product and updates the pathname", async ({ page }) => {
601
+ await page.goto(f.url("/"));
602
+ await page.getByTestId("product-link").click();
603
+ await expect(page).toHaveRangoPathname("/products/1");
604
+ });
605
+ });
606
+ ```
607
+
608
+ The body runs verbatim against a dev server (`pnpm dev`) and a built+previewed
609
+ server (`pnpm build` + `pnpm preview`). `useFixture` handles spawn, dep-optimizer
610
+ warmup, cross-platform process-group kill, and teardown.
611
+
612
+ ### expectParity — JS path vs no-JS progressive enhancement
613
+
614
+ Runs one intent over the JS path and a fresh no-JS context, asserting the
615
+ observed testids, pathname, and cookies match. CONTRACT: PE parity only holds if
616
+ the submit target is a real `<form>` (no-JS does a native POST). Cookie
617
+ observation is `document.cookie` (non-HttpOnly only) in v1.
618
+
619
+ ```ts
620
+ parityDescribe("add to cart parity", (f) => {
621
+ test("JS and no-JS produce the same result", async ({ page }) => {
622
+ await page.goto(f.url("/products/1"));
623
+ await expectParity(
624
+ page,
625
+ { submit: { testId: "add-to-cart-form", data: { qty: "2" } } },
626
+ { observe: ["cart-count", "flash"] },
627
+ );
628
+ });
629
+ });
630
+ ```
631
+
632
+ `intent` is `{ navigate: string }` or `{ submit: { testId, data? } }`. Other
633
+ helpers from `createRangoE2E`: `waitForHydration`, `expectNoReload`,
634
+ `expectNoPageError`, `testId`, `waitForNavigation`, `goBack`/`goForward`,
635
+ `testNoJs` (a `test` with JS disabled). `rangoMatchers` ships
636
+ `toHaveRangoPathname` only — `toHaveSegments`/`toHaveParams` are a documented
637
+ future addition (they need a client-emitted signal that does not exist yet; do
638
+ not assume them).
639
+
640
+ ## Cache / SWR / prerender recipes
641
+
642
+ The `X-Rango-Cache` header is emitted **only** when the gate is on:
643
+ `createRouter({ debugCacheSignal: true })` or `process.env.RANGO_TEST_SIGNALS === "1"`.
644
+ Off by default — zero production surface. v1 status is COARSE (route-level, keyed
645
+ by the route key — the route NAME, e.g. `product.detail`, NOT the URL pattern),
646
+ not per-individual-segment. `assertCacheStatus` reads that header.
647
+
648
+ ```ts
649
+ // In a Playwright e2e, import cache-status helpers from the e2e entry (the
650
+ // `@rangojs/router/testing` barrel is Vitest-only — it pulls a build virtual).
651
+ import { assertCacheStatus } from "@rangojs/router/testing/e2e";
652
+
653
+ // e2e (the gate must be enabled on the app under test). The segment key is the
654
+ // route NAME the header carries, not the URL pattern ("/products/:id").
655
+ const res = await page.request.get(f.url("/products/1"));
656
+ assertCacheStatus(res, "product.detail", "miss");
657
+ const res2 = await page.request.get(f.url("/products/1"));
658
+ assertCacheStatus(res2, "product.detail", "hit");
659
+ ```
660
+
661
+ Statuses: `"hit" | "miss" | "stale" | "prerendered" | "passthrough"`.
662
+
663
+ Zero-prod-surface alternative — the telemetry sink (no header at all):
664
+
665
+ ```ts
666
+ import { createCacheSink, filterCacheDecisions } from "@rangojs/router/testing";
667
+ const { sink, events } = createCacheSink();
668
+ const router = createRouter({ telemetry: sink /* ... */ }).routes(urlpatterns);
669
+ // ...drive a request...
670
+ const decisions = filterCacheDecisions(events);
671
+ expect(decisions[0].segments?.[0].cacheStatus).toBe("hit");
672
+ ```
673
+
674
+ PRERENDER: a pre-rendered route is **indistinguishable from a cache hit by
675
+ design** — the worker handles every request and looks up a stored Flight payload
676
+ (see `/prerender`). The browser cannot tell. So you cannot assert "prerendered"
677
+ from the rendered DOM; assert it via the signal (`assertCacheStatus(res, seg,
678
+ "prerendered")`), and run prerender assertions in **production** mode (build-time
679
+ artifacts only exist after `pnpm build`).
680
+
681
+ ## Anti-patterns and gotchas
682
+
683
+ - **No dev-only e2e.** A `useFixture({ mode: "build" })` describe whose title
684
+ omits `(production)` silently lands in the dev bucket — prod coverage lost,
685
+ no error. Always use `parityDescribe`; never hand-title. `(prod)`,
686
+ `-build`, `-prod` do NOT count — the bucketing matches the literal
687
+ `(production)`.
688
+ - **Don't hand-mock the router provider** to test a client component — use
689
+ `renderRoute`, which mounts the real `NavigationProvider`.
690
+ - **Don't call `createLoader(...)` in a unit test** and try to invoke it.
691
+ Extract the body and pass it to `runLoader`.
692
+ - **`dispatch` needs the plugin-rsc mock** (or a Vite-RSC env). A bare import of
693
+ your router throws on Vite virtual modules otherwise.
694
+ - **`renderToFlightString` is not a node test.** It only runs under the
695
+ react-server vitest project; name files `*.rsc-test.{ts,tsx}` and run
696
+ `pnpm test:unit:rsc`. The main vitest project must NOT set the react-server
697
+ condition (it would flip React to the no-hooks server build and break every
698
+ `renderRoute`/client test).
699
+ - **Running an e2e subset:** add `--no-deps` — `--grep` does NOT filter
700
+ dependency projects, so grepping one production test otherwise pulls in the
701
+ whole dev suite. And `--grep` is a regex: a pasted title containing
702
+ `(production)` / `:locale?` / `[...]` mis-matches; grep a metacharacter-free
703
+ fragment.
704
+
705
+ ## Pre-push checklist (mirror CLAUDE.md)
706
+
707
+ Before pushing, run all of these and fix any failure:
708
+
709
+ 1. `pnpm run typecheck` (or `pnpm exec tsc --noEmit`)
710
+ 2. `pnpm run test:unit` (node + DOM vitest)
711
+ 3. `pnpm run test:unit:rsc` (the react-server Flight project)
712
+ 4. `pnpm run lint`
713
+ 5. `pnpm run format`
714
+
715
+ And: **every e2e has a production counterpart.** `parityDescribe` makes this
716
+ automatic — if you wrote a plain `test.describe` for a behavior, convert it.