@rangojs/router 0.0.0-experimental.122 → 0.0.0-experimental.125

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 (260) hide show
  1. package/dist/bin/rango.js +10 -6
  2. package/dist/testing/vitest.js +82 -0
  3. package/dist/vite/index.js +55 -48
  4. package/package.json +61 -21
  5. package/skills/caching/SKILL.md +2 -1
  6. package/skills/hooks/SKILL.md +40 -29
  7. package/skills/host-router/SKILL.md +16 -2
  8. package/skills/intercept/SKILL.md +4 -2
  9. package/skills/layout/SKILL.md +11 -6
  10. package/skills/loader/SKILL.md +6 -2
  11. package/skills/middleware/SKILL.md +4 -2
  12. package/skills/migrate-nextjs/SKILL.md +3 -1
  13. package/skills/parallel/SKILL.md +9 -4
  14. package/skills/rango/SKILL.md +12 -0
  15. package/skills/route/SKILL.md +10 -2
  16. package/skills/testing/SKILL.md +129 -0
  17. package/skills/testing/bindings.md +89 -0
  18. package/skills/testing/cache-prerender.md +98 -0
  19. package/skills/testing/client-components.md +122 -0
  20. package/skills/testing/e2e-parity.md +125 -0
  21. package/skills/testing/flight.md +89 -0
  22. package/skills/testing/handles.md +129 -0
  23. package/skills/testing/loader.md +128 -0
  24. package/skills/testing/middleware.md +99 -0
  25. package/skills/testing/render-handler.md +118 -0
  26. package/skills/testing/response-routes.md +95 -0
  27. package/skills/testing/reverse-and-types.md +84 -0
  28. package/skills/testing/server-actions.md +107 -0
  29. package/skills/testing/server-tree.md +128 -0
  30. package/skills/testing/setup.md +120 -0
  31. package/src/__internal.ts +0 -65
  32. package/src/browser/action-coordinator.ts +1 -1
  33. package/src/browser/action-fence.ts +47 -0
  34. package/src/browser/cookie-name.ts +140 -0
  35. package/src/browser/event-controller.ts +1 -83
  36. package/src/browser/invalidate-client-cache.ts +52 -0
  37. package/src/browser/navigation-bridge.ts +14 -1
  38. package/src/browser/navigation-client.ts +14 -1
  39. package/src/browser/navigation-store-handle.ts +38 -0
  40. package/src/browser/navigation-store.ts +26 -51
  41. package/src/browser/navigation-transaction.ts +0 -32
  42. package/src/browser/partial-update.ts +1 -83
  43. package/src/browser/prefetch/cache.ts +6 -45
  44. package/src/browser/prefetch/fetch.ts +7 -0
  45. package/src/browser/prefetch/queue.ts +6 -3
  46. package/src/browser/rango-state.ts +157 -99
  47. package/src/browser/react/Link.tsx +0 -2
  48. package/src/browser/react/NavigationProvider.tsx +2 -1
  49. package/src/browser/react/ScrollRestoration.tsx +10 -6
  50. package/src/browser/react/filter-segment-order.ts +0 -2
  51. package/src/browser/react/index.ts +0 -51
  52. package/src/browser/react/location-state-shared.ts +0 -13
  53. package/src/browser/react/location-state.ts +0 -1
  54. package/src/browser/react/use-action.ts +6 -15
  55. package/src/browser/react/use-handle.ts +0 -5
  56. package/src/browser/react/use-link-status.ts +0 -4
  57. package/src/browser/react/use-navigation.ts +0 -3
  58. package/src/browser/react/use-params.ts +0 -2
  59. package/src/browser/react/use-search-params.ts +0 -5
  60. package/src/browser/react/use-segments.ts +0 -13
  61. package/src/browser/rsc-router.tsx +12 -4
  62. package/src/browser/server-action-bridge.ts +77 -15
  63. package/src/browser/types.ts +7 -2
  64. package/src/browser/validate-redirect-origin.ts +4 -5
  65. package/src/build/route-trie.ts +3 -0
  66. package/src/build/route-types/param-extraction.ts +6 -3
  67. package/src/build/route-types/router-processing.ts +0 -8
  68. package/src/cache/cache-policy.ts +0 -54
  69. package/src/cache/cache-runtime.ts +27 -24
  70. package/src/cache/cache-scope.ts +0 -27
  71. package/src/cache/cache-tag.ts +0 -37
  72. package/src/cache/cf/cf-cache-store.ts +94 -46
  73. package/src/cache/cf/index.ts +0 -24
  74. package/src/cache/document-cache.ts +11 -36
  75. package/src/cache/handle-snapshot.ts +0 -40
  76. package/src/cache/index.ts +0 -27
  77. package/src/cache/memory-segment-store.ts +2 -48
  78. package/src/cache/profile-registry.ts +7 -3
  79. package/src/cache/read-through-swr.ts +41 -11
  80. package/src/cache/segment-codec.ts +0 -16
  81. package/src/cache/types.ts +0 -98
  82. package/src/client.rsc.tsx +1 -22
  83. package/src/client.tsx +14 -38
  84. package/src/component-utils.ts +19 -0
  85. package/src/deps/ssr.ts +0 -1
  86. package/src/handle.ts +28 -18
  87. package/src/handles/MetaTags.tsx +0 -14
  88. package/src/handles/meta.ts +0 -39
  89. package/src/host/cookie-handler.ts +0 -36
  90. package/src/host/errors.ts +0 -24
  91. package/src/host/index.ts +6 -0
  92. package/src/host/pattern-matcher.ts +7 -50
  93. package/src/host/router.ts +1 -65
  94. package/src/host/testing.ts +40 -27
  95. package/src/host/types.ts +6 -2
  96. package/src/href-client.ts +0 -4
  97. package/src/index.rsc.ts +42 -3
  98. package/src/index.ts +31 -1
  99. package/src/internal-debug.ts +2 -4
  100. package/src/loader.rsc.ts +19 -9
  101. package/src/loader.ts +12 -4
  102. package/src/network-error-thrower.tsx +1 -6
  103. package/src/outlet-provider.tsx +1 -5
  104. package/src/prerender/param-hash.ts +10 -11
  105. package/src/prerender/store.ts +23 -30
  106. package/src/prerender.ts +58 -3
  107. package/src/root-error-boundary.tsx +1 -19
  108. package/src/route-content-wrapper.tsx +1 -44
  109. package/src/route-definition/dsl-helpers.ts +7 -19
  110. package/src/route-definition/helpers-types.ts +3 -3
  111. package/src/route-definition/redirect.ts +11 -1
  112. package/src/route-map-builder.ts +0 -16
  113. package/src/router/basename.ts +14 -0
  114. package/src/router/content-negotiation.ts +0 -13
  115. package/src/router/error-handling.ts +12 -16
  116. package/src/router/find-match.ts +4 -30
  117. package/src/router/intercept-resolution.ts +10 -1
  118. package/src/router/lazy-includes.ts +1 -57
  119. package/src/router/loader-resolution.ts +3 -2
  120. package/src/router/logging.ts +0 -6
  121. package/src/router/manifest.ts +1 -25
  122. package/src/router/match-api.ts +0 -20
  123. package/src/router/match-context.ts +0 -22
  124. package/src/router/match-handlers.ts +57 -58
  125. package/src/router/match-middleware/background-revalidation.ts +0 -7
  126. package/src/router/match-middleware/cache-lookup.ts +1 -54
  127. package/src/router/match-middleware/cache-store.ts +0 -31
  128. package/src/router/match-middleware/intercept-resolution.ts +0 -22
  129. package/src/router/match-middleware/segment-resolution.ts +0 -21
  130. package/src/router/match-pipelines.ts +1 -42
  131. package/src/router/match-result.ts +1 -52
  132. package/src/router/metrics.ts +0 -34
  133. package/src/router/middleware-cookies.ts +0 -13
  134. package/src/router/middleware-types.ts +0 -115
  135. package/src/router/middleware.ts +7 -30
  136. package/src/router/navigation-snapshot.ts +0 -51
  137. package/src/router/params-util.ts +23 -0
  138. package/src/router/pattern-matching.ts +1 -33
  139. package/src/router/prerender-match.ts +33 -45
  140. package/src/router/request-classification.ts +1 -38
  141. package/src/router/revalidation.ts +5 -58
  142. package/src/router/router-context.ts +0 -26
  143. package/src/router/router-interfaces.ts +7 -0
  144. package/src/router/router-options.ts +30 -0
  145. package/src/router/segment-resolution/fresh.ts +25 -57
  146. package/src/router/segment-resolution/helpers.ts +34 -0
  147. package/src/router/segment-resolution/loader-cache.ts +10 -13
  148. package/src/router/segment-resolution/revalidation.ts +5 -42
  149. package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
  150. package/src/router/segment-resolution.ts +4 -1
  151. package/src/router/state-cookie-name.ts +33 -0
  152. package/src/router/telemetry-otel.ts +0 -20
  153. package/src/router/telemetry.ts +96 -19
  154. package/src/router/timeout.ts +0 -20
  155. package/src/router/trie-matching.ts +63 -40
  156. package/src/router/types.ts +1 -63
  157. package/src/router/url-params.ts +0 -5
  158. package/src/router.ts +40 -9
  159. package/src/rsc/handler.ts +14 -2
  160. package/src/rsc/helpers.ts +34 -0
  161. package/src/rsc/origin-guard.ts +0 -12
  162. package/src/rsc/progressive-enhancement.ts +4 -1
  163. package/src/rsc/rsc-rendering.ts +4 -7
  164. package/src/rsc/runtime-warnings.ts +14 -0
  165. package/src/rsc/server-action.ts +30 -28
  166. package/src/rsc/types.ts +2 -1
  167. package/src/runtime-env.ts +18 -0
  168. package/src/search-params.ts +0 -16
  169. package/src/segment-loader-promise.ts +14 -2
  170. package/src/segment-system.tsx +79 -88
  171. package/src/server/cookie-store.ts +52 -1
  172. package/src/server/handle-store.ts +7 -24
  173. package/src/server/loader-registry.ts +5 -24
  174. package/src/server/request-context.ts +74 -77
  175. package/src/ssr/index.tsx +14 -14
  176. package/src/static-handler.ts +10 -13
  177. package/src/testing/cache-status.ts +119 -0
  178. package/src/testing/collect-handle.ts +40 -0
  179. package/src/testing/dispatch.ts +581 -0
  180. package/src/testing/dom.entry.ts +22 -0
  181. package/src/testing/e2e/fixture.ts +188 -0
  182. package/src/testing/e2e/index.ts +127 -0
  183. package/src/testing/e2e/matchers.ts +35 -0
  184. package/src/testing/e2e/page-helpers.ts +272 -0
  185. package/src/testing/e2e/parity.ts +387 -0
  186. package/src/testing/e2e/server.ts +195 -0
  187. package/src/testing/flight-matchers.ts +97 -0
  188. package/src/testing/flight-normalize.ts +11 -0
  189. package/src/testing/flight-runtime.d.ts +57 -0
  190. package/src/testing/flight-tree.ts +682 -0
  191. package/src/testing/flight.entry.ts +52 -0
  192. package/src/testing/flight.ts +186 -0
  193. package/src/testing/generated-routes.ts +183 -0
  194. package/src/testing/index.ts +98 -0
  195. package/src/testing/internal/context.ts +348 -0
  196. package/src/testing/internal/flight-client-globals.ts +30 -0
  197. package/src/testing/internal/seed-vars.ts +54 -0
  198. package/src/testing/render-handler.ts +311 -0
  199. package/src/testing/render-route.tsx +504 -0
  200. package/src/testing/run-loader.ts +378 -0
  201. package/src/testing/run-middleware.ts +205 -0
  202. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  203. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  204. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  205. package/src/testing/vitest-stubs/version.ts +5 -0
  206. package/src/testing/vitest.ts +305 -0
  207. package/src/theme/ThemeProvider.tsx +0 -52
  208. package/src/theme/ThemeScript.tsx +0 -6
  209. package/src/theme/constants.ts +0 -12
  210. package/src/theme/index.ts +0 -7
  211. package/src/theme/theme-context.ts +1 -5
  212. package/src/theme/theme-script.ts +0 -14
  213. package/src/theme/use-theme.ts +0 -3
  214. package/src/types/boundaries.ts +0 -35
  215. package/src/types/error-types.ts +25 -89
  216. package/src/types/global-namespace.ts +15 -15
  217. package/src/types/handler-context.ts +16 -13
  218. package/src/types/index.ts +0 -10
  219. package/src/types/request-scope.ts +0 -19
  220. package/src/types/route-config.ts +6 -50
  221. package/src/types/route-entry.ts +0 -6
  222. package/src/types/segments.ts +0 -13
  223. package/src/urls/include-helper.ts +0 -4
  224. package/src/urls/index.ts +0 -6
  225. package/src/urls/path-helper-types.ts +2 -2
  226. package/src/urls/path-helper.ts +0 -54
  227. package/src/urls/urls-function.ts +0 -13
  228. package/src/use-loader.tsx +0 -186
  229. package/src/vite/discovery/bundle-postprocess.ts +2 -1
  230. package/src/vite/discovery/discover-routers.ts +6 -7
  231. package/src/vite/discovery/virtual-module-codegen.ts +1 -11
  232. package/src/vite/plugin-types.ts +3 -1
  233. package/src/vite/plugins/cjs-to-esm.ts +0 -11
  234. package/src/vite/plugins/client-ref-dedup.ts +0 -11
  235. package/src/vite/plugins/client-ref-hashing.ts +0 -10
  236. package/src/vite/plugins/cloudflare-protocol-stub.ts +0 -20
  237. package/src/vite/plugins/expose-action-id.ts +2 -73
  238. package/src/vite/plugins/expose-id-utils.ts +0 -55
  239. package/src/vite/plugins/expose-ids/export-analysis.ts +0 -38
  240. package/src/vite/plugins/expose-ids/handler-transform.ts +0 -15
  241. package/src/vite/plugins/expose-ids/loader-transform.ts +0 -15
  242. package/src/vite/plugins/expose-ids/router-transform.ts +0 -13
  243. package/src/vite/plugins/expose-internal-ids.ts +10 -0
  244. package/src/vite/plugins/performance-tracks.ts +0 -3
  245. package/src/vite/plugins/use-cache-transform.ts +0 -36
  246. package/src/vite/plugins/version-injector.ts +0 -20
  247. package/src/vite/plugins/version-plugin.ts +1 -49
  248. package/src/vite/plugins/virtual-entries.ts +0 -15
  249. package/src/vite/rango.ts +1 -108
  250. package/src/vite/router-discovery.ts +2 -1
  251. package/src/vite/utils/ast-handler-extract.ts +0 -16
  252. package/src/vite/utils/bundle-analysis.ts +6 -13
  253. package/src/vite/utils/client-chunks.ts +0 -6
  254. package/src/vite/utils/forward-user-plugins.ts +0 -22
  255. package/src/vite/utils/manifest-utils.ts +0 -4
  256. package/src/vite/utils/package-resolution.ts +1 -73
  257. package/src/vite/utils/prerender-utils.ts +0 -35
  258. package/src/vite/utils/shared-utils.ts +3 -35
  259. package/src/browser/react/use-client-cache.ts +0 -58
  260. package/src/browser/shallow.ts +0 -40
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @rangojs/router/testing/flight
3
+ *
4
+ * Real React Server Component (Flight) rendering for unit tests. This entry is
5
+ * SEPARATE from the main ./testing barrel because its serializer (the vendored
6
+ * react-server-dom build) can only be imported under the `react-server` node
7
+ * condition; importing it elsewhere throws. Use it only from a Vitest project
8
+ * configured with that condition (see vitest.rsc.config.ts) — name those test
9
+ * files `*.rsc-test.{ts,tsx}` and run `pnpm test:unit:rsc`.
10
+ *
11
+ * This entry deliberately does NOT pull in Vitest. The `toMatchFlight` /
12
+ * `toMatchFlightSnapshot` matchers (which import `vitest`) live at the separate
13
+ * `@rangojs/router/testing/flight-matchers` subpath, so a consumer can import
14
+ * `renderToFlightString` without taking a hard dependency on Vitest.
15
+ *
16
+ * `renderToFlightString` returns the wire STRING (for `toMatchFlight`).
17
+ * `renderServerTree` additionally deserializes it back to an inspectable React
18
+ * element tree, so you can assert typed prop fidelity across the client boundary
19
+ * (a `Date` comes back a `Date`) and detect inlined-vs-island. Serialize +
20
+ * deserialize only — no hydration/interaction (that is the e2e tier).
21
+ */
22
+
23
+ export {
24
+ renderToFlightString,
25
+ normalizeFlight,
26
+ assertFlightRuntimeAvailable,
27
+ } from "./flight.js";
28
+ export type { RenderToFlightStringOptions } from "./flight.js";
29
+
30
+ export {
31
+ renderServerTree,
32
+ findClientBoundaries,
33
+ findElements,
34
+ textContent,
35
+ assertFlightTreeRuntimeAvailable,
36
+ } from "./flight-tree.js";
37
+ export type {
38
+ RenderServerTreeOptions,
39
+ RenderServerTreeResult,
40
+ ClientBoundary,
41
+ BoundarySelector,
42
+ FoundElement,
43
+ ElementSelector,
44
+ } from "./flight-tree.js";
45
+
46
+ export { renderHandler } from "./render-handler.js";
47
+ export type {
48
+ TestableHandler,
49
+ RenderHandlerOptions,
50
+ RenderHandlerResult,
51
+ StateCookieSeed,
52
+ } from "./render-handler.js";
@@ -0,0 +1,186 @@
1
+ /// <reference path="./flight-runtime.d.ts" />
2
+ /**
3
+ * renderToFlightString — REAL React Server Component (Flight) rendering for
4
+ * unit tests of @rangojs/router consumer apps.
5
+ *
6
+ * This module renders a server component tree to its Flight wire string using
7
+ * the same react-server-dom serializer the router uses at runtime. It runs in
8
+ * plain node (no Vite, no browser), but ONLY under the `react-server` export
9
+ * condition. The serializer is the VENDORED build shipped with
10
+ * @vitejs/plugin-rsc — the public `@vitejs/plugin-rsc/rsc` entry top-level
11
+ * imports Vite virtual modules and is not usable outside a Vite build.
12
+ *
13
+ * Run the example/tests for this module via the dedicated rsc vitest project
14
+ * (vitest.rsc.config.ts), which forces `--conditions=react-server` on the
15
+ * worker. The main vitest project must NOT use that condition (it would flip
16
+ * React to the no-hooks server build and break the ~50 tests that mock
17
+ * @vitejs/plugin-rsc/rsc).
18
+ *
19
+ * Scope / limitations (v1):
20
+ * - Server-only / leaf trees. A tree containing a CLIENT component emits an
21
+ * `I[...]` import row whose module id will not resolve against the empty `{}`
22
+ * client manifest used here — fine for snapshotting the SHAPE of the payload.
23
+ * To inspect a client boundary's deserialized props instead, use
24
+ * `renderServerTree` (flight-tree.ts). Interactive hydration stays at the e2e tier.
25
+ * - The vendored subpath is a private plugin-rsc path; a minor bump could move
26
+ * it. `assertFlightRuntimeAvailable()` provides a smoke check.
27
+ * - For stable snapshots, run under NODE_ENV=production: the production
28
+ * serializer drops the dev-only debug-info rows (the `N<timestamp>` reference
29
+ * row, the per-component `stack`/`env` rows, and `D{...}` timing rows),
30
+ * leaving just the rendered tree row(s).
31
+ */
32
+
33
+ import type { ReactNode } from "react";
34
+ // Vendored react-server-dom serializer. Resolves via plugin-rsc's
35
+ // `"./*": "./dist/*.js"` export to
36
+ // dist/vendor/react-server-dom/server.edge.js. Only loadable under the
37
+ // `react-server` export condition.
38
+ import * as RSDServer from "@vitejs/plugin-rsc/vendor/react-server-dom/server.edge";
39
+ import {
40
+ createRequestContext,
41
+ runWithRequestContext,
42
+ setRequestContextParams,
43
+ } from "../server/request-context.js";
44
+ import { seedVariables, type VarsInit } from "./internal/seed-vars.js";
45
+ import { normalizeFlight } from "./flight-normalize.js";
46
+ import type { RscPayload } from "../rsc/types.js";
47
+ import type { ResolvedSegment } from "../types.js";
48
+
49
+ // Re-export from the serializer-free module so this entry's public surface is
50
+ // unchanged while flight-matchers can import normalizeFlight without pulling in
51
+ // the vendored serializer (which throws outside the react-server condition).
52
+ export { normalizeFlight };
53
+
54
+ /**
55
+ * Options for {@link renderToFlightString}.
56
+ */
57
+ export interface RenderToFlightStringOptions {
58
+ /**
59
+ * The request the render runs under: a `Request`, or a URL string (absolute or
60
+ * path). Defaults to `http://localhost/`. A server component reading
61
+ * `getRequestContext()` sees this request's url/cookies. When a `Request` is
62
+ * passed, its headers are used and `headers` below is ignored.
63
+ */
64
+ request?: Request | string;
65
+ /** Request headers (e.g. Cookie) visible to the server tree (when `request` is a string). */
66
+ headers?: HeadersInit;
67
+ /** Env / bindings exposed as `ctx.env`. Defaults to `{}`. */
68
+ env?: unknown;
69
+ /** Route params exposed via `ctx.params` and loader contexts. */
70
+ params?: Record<string, string>;
71
+ /** Matched route name (drives `ctx.routeName` and scoped reverse). */
72
+ routeName?: string;
73
+ /**
74
+ * Context variables visible to the rendered tree via `ctx.get(...)` — as a
75
+ * prior middleware would have set them. Seeds the SAME way the handler-test
76
+ * primitives (`runInRequestContext`/`runLoader`) do, so a server component
77
+ * that reads `getRequestContext().get(MyVar)` during render is testable.
78
+ * Object form (`{ user }`) or `[key, value]` tuples (`[[userVar, u]]`).
79
+ */
80
+ vars?: VarsInit;
81
+ }
82
+
83
+ const DEFAULT_URL = "http://localhost/";
84
+
85
+ export function assertNoLegacyUrlOption(opts: object, fnName: string): void {
86
+ if ("url" in opts) {
87
+ throw new Error(
88
+ `${fnName}: the \`url\` option was renamed to \`request\`. Pass ` +
89
+ `{ request: "<url-or-path>" } (or a Request) instead of { url }. ` +
90
+ `The legacy \`url\` key is ignored, so the render would silently use ` +
91
+ `the default origin.`,
92
+ );
93
+ }
94
+ }
95
+
96
+ function wrapAsPayload(element: ReactNode, pathname: string): RscPayload {
97
+ const segment: ResolvedSegment = {
98
+ id: "test",
99
+ namespace: "",
100
+ type: "route",
101
+ index: 0,
102
+ component: element,
103
+ };
104
+ return {
105
+ metadata: {
106
+ pathname,
107
+ segments: [segment],
108
+ version: "test",
109
+ },
110
+ };
111
+ }
112
+
113
+ /**
114
+ * Render a server component (or any ReactNode) to its Flight wire string.
115
+ *
116
+ * The element is wrapped in a minimal Rango segment + payload, then serialized
117
+ * with the vendored react-server-dom server. A request context is active for
118
+ * the duration of the render (drained INSIDE runWithRequestContext) so async
119
+ * server components can call getRequestContext(), read params, cookies, etc.
120
+ *
121
+ * Must run under the `react-server` export condition (see module header).
122
+ */
123
+ export async function renderToFlightString(
124
+ element: ReactNode,
125
+ opts: RenderToFlightStringOptions = {},
126
+ ): Promise<string> {
127
+ assertNoLegacyUrlOption(opts, "renderToFlightString");
128
+ return serializeToFlightString(element, opts, {});
129
+ }
130
+
131
+ export async function serializeToFlightString(
132
+ element: ReactNode,
133
+ opts: RenderToFlightStringOptions,
134
+ clientManifest: unknown,
135
+ ): Promise<string> {
136
+ const request =
137
+ opts.request instanceof Request
138
+ ? opts.request
139
+ : new Request(new URL(opts.request ?? DEFAULT_URL, DEFAULT_URL), {
140
+ headers: opts.headers,
141
+ });
142
+ const url = new URL(request.url);
143
+ const ctx = createRequestContext({
144
+ env: opts.env ?? {},
145
+ request,
146
+ url,
147
+ variables: seedVariables({}, opts.vars),
148
+ });
149
+
150
+ return runWithRequestContext(ctx, () => {
151
+ setRequestContextParams(opts.params ?? {}, opts.routeName);
152
+ return serializeNodeToFlight(element, clientManifest, url.pathname);
153
+ });
154
+ }
155
+
156
+ export async function serializeNodeToFlight(
157
+ node: ReactNode,
158
+ clientManifest: unknown,
159
+ pathname: string,
160
+ ): Promise<string> {
161
+ const payload = wrapAsPayload(node, pathname);
162
+ let renderError: unknown;
163
+ let didError = false;
164
+ const stream = RSDServer.renderToReadableStream(payload, clientManifest, {
165
+ onError(error: unknown) {
166
+ if (!didError) {
167
+ didError = true;
168
+ renderError = error;
169
+ }
170
+ },
171
+ });
172
+ const text = await new Response(stream).text();
173
+ if (didError) throw renderError;
174
+ return text;
175
+ }
176
+
177
+ export function assertFlightRuntimeAvailable(): void {
178
+ if (typeof RSDServer.renderToReadableStream !== "function") {
179
+ throw new Error(
180
+ "Vendored react-server-dom serializer not available: " +
181
+ "@vitejs/plugin-rsc/vendor/react-server-dom/server.edge did not export " +
182
+ "renderToReadableStream. The private vendored subpath may have moved in " +
183
+ "a plugin-rsc upgrade.",
184
+ );
185
+ }
186
+ }
@@ -0,0 +1,183 @@
1
+ /**
2
+ * assertGeneratedRoutesMatch — pin the generated named-routes map against the
3
+ * router's runtime route map.
4
+ *
5
+ * The Vite plugin writes a `*.named-routes.gen.ts` file mapping route names to
6
+ * URL patterns; consumers import that map and pass it here. The check compares
7
+ * it to the router's runtime `routeMap`, catching drift when a route is added,
8
+ * removed, renamed, or its pattern changes without regenerating the file.
9
+ *
10
+ * Directionality (relative to the generated map):
11
+ * - missing: present in the generated map but NOT at runtime (stale entry).
12
+ * - extra: present at runtime but NOT in the generated map (ungenerated route).
13
+ * Auto-generated internal names (the "$path_" / "$prefix_" prefixes)
14
+ * are excluded — they live in the runtime map but are never written
15
+ * to the generated file, so they are not drift.
16
+ * - mismatch: present in both under the same name, but the patterns differ.
17
+ *
18
+ * When `generatedMap` is omitted, the global route map (getGlobalRouteMap()) is
19
+ * used as the generated side — useful when a single router has registered into
20
+ * the global map.
21
+ */
22
+
23
+ import { getGlobalRouteMap } from "../route-map-builder.js";
24
+ import { isAutoGeneratedRouteName } from "../route-name.js";
25
+
26
+ /**
27
+ * Router shape this check depends on: a runtime route map, plus the optional
28
+ * `findMatch` used to force-expand lazy `include()`d routes (see below).
29
+ */
30
+ interface RouterWithRouteMap {
31
+ routeMap: Record<string, unknown>;
32
+ findMatch?: (pathname: string) => unknown;
33
+ }
34
+
35
+ function concretePath(pattern: string): string {
36
+ return (
37
+ pattern
38
+ .replace(/:[A-Za-z0-9_]+\([^)]*\)\??/g, "x") // :p(constraint) / optional
39
+ .replace(/:[A-Za-z0-9_]+\??/g, "x") // :p or :p?
40
+ .replace(/\*/g, "x") // wildcard
41
+ .replace(/\/{2,}/g, "/") || "/"
42
+ );
43
+ }
44
+
45
+ function expandLazyIncludes(
46
+ router: RouterWithRouteMap,
47
+ patterns: Iterable<string>,
48
+ ): void {
49
+ const findMatch = router.findMatch;
50
+ if (typeof findMatch !== "function") return;
51
+ for (const pattern of patterns) {
52
+ try {
53
+ findMatch.call(router, concretePath(pattern));
54
+ } catch {}
55
+ }
56
+ }
57
+
58
+ /**
59
+ * A single name/pattern mismatch: [routeName, generatedPattern, runtimePattern].
60
+ */
61
+ export type GeneratedRouteMismatch = [
62
+ name: string,
63
+ generated: string,
64
+ runtime: string,
65
+ ];
66
+
67
+ /**
68
+ * Structured diff between the generated route map and the runtime route map.
69
+ */
70
+ export interface GeneratedRoutesDiff {
71
+ /** Names in the generated map but absent at runtime. */
72
+ missing: string[];
73
+ /** Names at runtime but absent from the generated map. */
74
+ extra: string[];
75
+ /** Names in both with differing patterns. */
76
+ mismatch: GeneratedRouteMismatch[];
77
+ /** True when missing, extra, and mismatch are all empty. */
78
+ ok: boolean;
79
+ }
80
+
81
+ function patternOf(value: unknown): string {
82
+ if (typeof value === "string") return value;
83
+ if (
84
+ value &&
85
+ typeof value === "object" &&
86
+ "path" in value &&
87
+ typeof (value as { path: unknown }).path === "string"
88
+ ) {
89
+ return (value as { path: string }).path;
90
+ }
91
+ return String(value);
92
+ }
93
+
94
+ export function diffGeneratedRoutes(
95
+ router: RouterWithRouteMap,
96
+ generatedMap?: Record<string, unknown>,
97
+ ): GeneratedRoutesDiff {
98
+ const generated = generatedMap ?? getGlobalRouteMap();
99
+
100
+ expandLazyIncludes(
101
+ router,
102
+ Object.values(generated).map((v) => patternOf(v)),
103
+ );
104
+
105
+ const runtime = router.routeMap as Record<string, unknown>;
106
+
107
+ const missing: string[] = [];
108
+ const extra: string[] = [];
109
+ const mismatch: GeneratedRouteMismatch[] = [];
110
+
111
+ for (const name of Object.keys(generated)) {
112
+ if (!(name in runtime)) {
113
+ missing.push(name);
114
+ continue;
115
+ }
116
+ const gen = patternOf(generated[name]);
117
+ const run = patternOf(runtime[name]);
118
+ if (gen !== run) {
119
+ mismatch.push([name, gen, run]);
120
+ }
121
+ }
122
+
123
+ for (const name of Object.keys(runtime)) {
124
+ if (isAutoGeneratedRouteName(name)) continue;
125
+ if (!(name in generated)) {
126
+ extra.push(name);
127
+ }
128
+ }
129
+
130
+ return {
131
+ missing,
132
+ extra,
133
+ mismatch,
134
+ ok: missing.length === 0 && extra.length === 0 && mismatch.length === 0,
135
+ };
136
+ }
137
+
138
+ /**
139
+ * Assert the router's runtime route map matches the generated map. Throws a
140
+ * descriptive Error listing every missing, extra, and mismatched route when
141
+ * they diverge.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * import generated from "./router.named-routes.gen";
146
+ * import { router } from "./router";
147
+ *
148
+ * assertGeneratedRoutesMatch(router, generated);
149
+ * ```
150
+ */
151
+ export function assertGeneratedRoutesMatch(
152
+ router: RouterWithRouteMap,
153
+ generatedMap?: Record<string, unknown>,
154
+ ): void {
155
+ const diff = diffGeneratedRoutes(router, generatedMap);
156
+ if (diff.ok) return;
157
+
158
+ const lines: string[] = [
159
+ "Generated routes do not match the router's runtime route map.",
160
+ ];
161
+
162
+ if (diff.missing.length > 0) {
163
+ lines.push(
164
+ ` Missing (generated but not at runtime): ${diff.missing.join(", ")}`,
165
+ );
166
+ }
167
+ if (diff.extra.length > 0) {
168
+ lines.push(
169
+ ` Extra (at runtime but not generated): ${diff.extra.join(", ")}`,
170
+ );
171
+ }
172
+ if (diff.mismatch.length > 0) {
173
+ lines.push(" Pattern mismatches (name: generated -> runtime):");
174
+ for (const [name, gen, run] of diff.mismatch) {
175
+ lines.push(` ${name}: ${gen} -> ${run}`);
176
+ }
177
+ }
178
+ lines.push(
179
+ "Re-run the build / `rango` route generation to regenerate the *.named-routes.gen.ts file.",
180
+ );
181
+
182
+ throw new Error(lines.join("\n"));
183
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @rangojs/router/testing
3
+ *
4
+ * Consumer-facing testing primitives for apps built on @rangojs/router.
5
+ *
6
+ * This is the entry for UNIT and INTEGRATION tests, meant to run under a
7
+ * Vite-driven Vitest project (the rango Vite plugin must be active so the
8
+ * `@rangojs/router:version` and related virtual modules the router internals
9
+ * import can resolve; alternatively alias `@rangojs/router:version`). Importing
10
+ * this module references neither React, @testing-library/react, @playwright/test,
11
+ * nor the RSC runtime — a unit suite testing only loaders/middleware/dispatch
12
+ * pulls in none of them.
13
+ *
14
+ * The other surfaces are SEPARATE entries because each pulls a dependency or
15
+ * runtime this barrel deliberately keeps out:
16
+ * - `@rangojs/router/testing/dom` — `renderRoute` (the RTL component stub). Kept
17
+ * separate so this barrel never references React, the browser runtime, or
18
+ * `@testing-library/react` types — a unit suite testing only loaders/middleware
19
+ * needs none of them.
20
+ * - `@rangojs/router/testing/e2e` — the Playwright harness (createRangoE2E,
21
+ * useFixture, parityDescribe, expectParity, matchers). Kept separate so it is
22
+ * loadable in a plain (non-Vite) Playwright runner, which cannot resolve the
23
+ * router-manifest virtual modules this barrel pulls in.
24
+ * - `@rangojs/router/testing/flight` — real Flight rendering. Its serializer
25
+ * (vendored react-server-dom) loads only under the `react-server` node
26
+ * condition and would throw if pulled into this barrel.
27
+ *
28
+ * Layers:
29
+ * - Unit: runMiddleware, runLoader
30
+ * - Integration: dispatch (request -> Response)
31
+ * - Cross-cut: assertCacheStatus, assertGeneratedRoutesMatch
32
+ * - Component: see @rangojs/router/testing/dom (renderRoute)
33
+ * - E2E: see @rangojs/router/testing/e2e
34
+ * - RSC: see @rangojs/router/testing/flight
35
+ */
36
+
37
+ export { runMiddleware } from "./run-middleware.js";
38
+ export type {
39
+ RunMiddlewareOptions,
40
+ RunMiddlewareResult,
41
+ } from "./run-middleware.js";
42
+ export { runLoader, runLoaderResult } from "./run-loader.js";
43
+ export type {
44
+ RunLoaderOptions,
45
+ RunLoaderResult,
46
+ UseResolver,
47
+ TestLoaderContext,
48
+ } from "./run-loader.js";
49
+
50
+ export { dispatch } from "./dispatch.js";
51
+ export type { DispatchOptions } from "./dispatch.js";
52
+
53
+ export {
54
+ assertCacheStatus,
55
+ parseCacheHeader,
56
+ createCacheSink,
57
+ filterCacheDecisions,
58
+ } from "./cache-status.js";
59
+ export type {
60
+ ExpectedCacheStatus,
61
+ CacheStatusTarget,
62
+ CacheSink,
63
+ } from "./cache-status.js";
64
+ export type {
65
+ TelemetryEvent,
66
+ TelemetrySink,
67
+ CacheDecisionEvent,
68
+ CacheSegmentSignal,
69
+ CacheSegmentStatus,
70
+ } from "../router/telemetry.js";
71
+
72
+ export { collectHandle } from "./collect-handle.js";
73
+
74
+ export {
75
+ diffGeneratedRoutes,
76
+ assertGeneratedRoutesMatch,
77
+ } from "./generated-routes.js";
78
+ export type {
79
+ GeneratedRoutesDiff,
80
+ GeneratedRouteMismatch,
81
+ } from "./generated-routes.js";
82
+
83
+ export {
84
+ createTestRequestContext,
85
+ runInRequestContext,
86
+ toRequest,
87
+ seedVariables,
88
+ } from "./internal/context.js";
89
+ export type {
90
+ CreateTestContextOptions,
91
+ TestRequestContext,
92
+ TestRequestContextObject,
93
+ RunInRequestContextResult,
94
+ VarsInit,
95
+ StateCookieSeed,
96
+ } from "./internal/context.js";
97
+
98
+ export { runWithRequestContext } from "../server/request-context.js";