@rangojs/router 0.0.0-experimental.121 → 0.0.0-experimental.124
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.
- package/dist/bin/rango.js +7 -2
- package/dist/vite/index.js +47 -6
- package/package.json +61 -21
- package/skills/cache-guide/SKILL.md +8 -6
- package/skills/caching/SKILL.md +148 -1
- package/skills/hooks/SKILL.md +38 -27
- package/skills/host-router/SKILL.md +16 -2
- package/skills/intercept/SKILL.md +4 -2
- package/skills/layout/SKILL.md +11 -6
- package/skills/loader/SKILL.md +6 -2
- package/skills/middleware/SKILL.md +4 -2
- package/skills/migrate-nextjs/SKILL.md +38 -16
- package/skills/parallel/SKILL.md +9 -4
- package/skills/rango/SKILL.md +27 -15
- package/skills/route/SKILL.md +4 -2
- package/skills/testing/SKILL.md +129 -0
- package/skills/testing/bindings.md +89 -0
- package/skills/testing/cache-prerender.md +98 -0
- package/skills/testing/client-components.md +122 -0
- package/skills/testing/e2e-parity.md +125 -0
- package/skills/testing/flight.md +89 -0
- package/skills/testing/handles.md +129 -0
- package/skills/testing/loader.md +128 -0
- package/skills/testing/middleware.md +99 -0
- package/skills/testing/render-handler.md +118 -0
- package/skills/testing/response-routes.md +95 -0
- package/skills/testing/reverse-and-types.md +84 -0
- package/skills/testing/server-actions.md +107 -0
- package/skills/testing/server-tree.md +128 -0
- package/skills/testing/setup.md +120 -0
- package/skills/use-cache/SKILL.md +9 -7
- package/src/browser/action-fence.ts +37 -0
- package/src/browser/cookie-name.ts +140 -0
- package/src/browser/invalidate-client-cache.ts +52 -0
- package/src/browser/navigation-bridge.ts +14 -1
- package/src/browser/navigation-client.ts +14 -1
- package/src/browser/navigation-store-handle.ts +39 -0
- package/src/browser/navigation-store.ts +26 -12
- package/src/browser/prefetch/fetch.ts +7 -0
- package/src/browser/rango-state.ts +176 -97
- package/src/browser/react/index.ts +0 -6
- package/src/browser/rsc-router.tsx +12 -4
- package/src/browser/server-action-bridge.ts +77 -15
- package/src/browser/types.ts +7 -1
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-policy.ts +95 -1
- package/src/cache/cache-runtime.ts +79 -13
- package/src/cache/cache-scope.ts +55 -4
- package/src/cache/cache-tag.ts +135 -0
- package/src/cache/cf/cf-cache-store.ts +2080 -224
- package/src/cache/cf/index.ts +15 -1
- package/src/cache/document-cache.ts +74 -7
- package/src/cache/index.ts +17 -0
- package/src/cache/memory-segment-store.ts +164 -14
- package/src/cache/tag-invalidation.ts +230 -0
- package/src/cache/types.ts +27 -0
- package/src/client.rsc.tsx +1 -1
- package/src/client.tsx +0 -6
- package/src/component-utils.ts +19 -0
- package/src/handle.ts +29 -9
- package/src/host/testing.ts +43 -14
- package/src/index.rsc.ts +29 -1
- package/src/index.ts +43 -1
- package/src/loader.rsc.ts +24 -3
- package/src/loader.ts +16 -2
- package/src/prerender.ts +24 -3
- package/src/router/basename.ts +14 -0
- package/src/router/match-handlers.ts +62 -20
- package/src/router/prerender-match.ts +6 -0
- package/src/router/router-interfaces.ts +7 -0
- package/src/router/router-options.ts +30 -0
- package/src/router/segment-resolution/loader-cache.ts +8 -17
- package/src/router/state-cookie-name.ts +33 -0
- package/src/router/telemetry.ts +99 -0
- package/src/router.ts +36 -7
- package/src/rsc/handler.ts +13 -1
- package/src/rsc/helpers.ts +19 -0
- package/src/rsc/progressive-enhancement.ts +2 -0
- package/src/rsc/response-route-handler.ts +8 -1
- package/src/rsc/rsc-rendering.ts +2 -0
- package/src/rsc/types.ts +2 -0
- package/src/runtime-env.ts +18 -0
- package/src/server/cookie-store.ts +52 -1
- package/src/server/request-context.ts +105 -2
- package/src/static-handler.ts +25 -3
- package/src/testing/cache-status.ts +166 -0
- package/src/testing/collect-handle.ts +63 -0
- package/src/testing/dispatch.ts +581 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +188 -0
- package/src/testing/e2e/index.ts +149 -0
- package/src/testing/e2e/matchers.ts +51 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +387 -0
- package/src/testing/e2e/server.ts +195 -0
- package/src/testing/flight-matchers.ts +110 -0
- package/src/testing/flight-normalize.ts +38 -0
- package/src/testing/flight-runtime.d.ts +57 -0
- package/src/testing/flight-tree.ts +682 -0
- package/src/testing/flight.entry.ts +52 -0
- package/src/testing/flight.ts +234 -0
- package/src/testing/generated-routes.ts +223 -0
- package/src/testing/index.ts +119 -0
- package/src/testing/internal/context.ts +390 -0
- package/src/testing/internal/flight-client-globals.ts +30 -0
- package/src/testing/internal/seed-vars.ts +80 -0
- package/src/testing/render-handler.ts +360 -0
- package/src/testing/render-route.tsx +594 -0
- package/src/testing/run-loader.ts +474 -0
- package/src/testing/run-middleware.ts +231 -0
- package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
- package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
- package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
- package/src/testing/vitest-stubs/version.ts +5 -0
- package/src/testing/vitest.ts +305 -0
- package/src/types/cache-types.ts +13 -4
- package/src/types/error-types.ts +5 -1
- package/src/types/global-namespace.ts +11 -1
- package/src/types/handler-context.ts +16 -5
- package/src/browser/react/use-client-cache.ts +0 -58
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared internals for the consumer testing primitives.
|
|
3
|
+
*
|
|
4
|
+
* Builds a real RequestContext via the same createRequestContext the RSC
|
|
5
|
+
* handler uses, with test-friendly defaults, so loaders and middleware run
|
|
6
|
+
* with production-fidelity context (cookies, headers, get/set, use, reverse)
|
|
7
|
+
* instead of a hand-rolled mock.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
createRequestContext,
|
|
12
|
+
runWithRequestContext,
|
|
13
|
+
type RequestContext,
|
|
14
|
+
} from "../../server/request-context.js";
|
|
15
|
+
import { resolveLocationStateEntries } from "../../browser/react/location-state-shared.js";
|
|
16
|
+
import { createReverseFunction } from "../../router/handler-context.js";
|
|
17
|
+
import { normalizeBasename } from "../../router/basename.js";
|
|
18
|
+
import {
|
|
19
|
+
seedVariables,
|
|
20
|
+
resolveSeededStateCookieName,
|
|
21
|
+
type VarsInit,
|
|
22
|
+
type StateCookieSeed,
|
|
23
|
+
} from "./seed-vars.js";
|
|
24
|
+
import type { ThemeConfig } from "../../theme/types.js";
|
|
25
|
+
import { resolveThemeConfig } from "../../theme/constants.js";
|
|
26
|
+
import type { SegmentCacheStore } from "../../cache/types.js";
|
|
27
|
+
import type { CacheProfile } from "../../cache/profile-registry.js";
|
|
28
|
+
|
|
29
|
+
const DEFAULT_ORIGIN = "http://localhost/";
|
|
30
|
+
|
|
31
|
+
// VarsInit + seedVariables live in ./seed-vars.js (react-server-safe) so the
|
|
32
|
+
// Flight tier can seed vars too; re-exported here for existing importers.
|
|
33
|
+
export type { VarsInit, StateCookieSeed };
|
|
34
|
+
export { seedVariables };
|
|
35
|
+
|
|
36
|
+
/** Normalize a Request | string | undefined into a concrete Request. */
|
|
37
|
+
export function toRequest(
|
|
38
|
+
request: Request | string | undefined,
|
|
39
|
+
init?: RequestInit,
|
|
40
|
+
): Request {
|
|
41
|
+
if (request instanceof Request) return request;
|
|
42
|
+
if (typeof request === "string") {
|
|
43
|
+
return new Request(new URL(request, DEFAULT_ORIGIN), init);
|
|
44
|
+
}
|
|
45
|
+
return new Request(DEFAULT_ORIGIN, init);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface CreateTestContextOptions<TEnv> {
|
|
49
|
+
env?: TEnv;
|
|
50
|
+
request?: Request | string;
|
|
51
|
+
requestInit?: RequestInit;
|
|
52
|
+
/** Backing store for ctx.get()/ctx.set(); pre-seeded from `vars`. */
|
|
53
|
+
variables?: Record<string, unknown>;
|
|
54
|
+
/** Variables a prior middleware would have set (object or [key, value] list). */
|
|
55
|
+
vars?: VarsInit;
|
|
56
|
+
/** Route name -> pattern map enabling ctx.reverse() without global state. */
|
|
57
|
+
routeMap?: Record<string, string>;
|
|
58
|
+
routeName?: string;
|
|
59
|
+
params?: Record<string, string>;
|
|
60
|
+
/**
|
|
61
|
+
* Router basename for this request (what the RSC handler stores on the
|
|
62
|
+
* context). Drives redirect() prefixing. Normalized exactly like
|
|
63
|
+
* createRouter({ basename }) (leading slash forced, trailing stripped, bare
|
|
64
|
+
* "/" -> undefined) so passing the same value your router takes yields the
|
|
65
|
+
* same redirect Location. Defaults to undefined (no basename).
|
|
66
|
+
*/
|
|
67
|
+
basename?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Cache store backing `use cache` functions invoked during the test, the
|
|
70
|
+
* same shape `createRouter({ cache })` resolves. Without it,
|
|
71
|
+
* registerCachedFunction bypasses (it checks for a store FIRST), so a cached
|
|
72
|
+
* function runs uncached and its taint/profile guards never fire. Wire one
|
|
73
|
+
* (e.g. `new MemorySegmentCacheStore()`) to exercise real cache behavior.
|
|
74
|
+
*/
|
|
75
|
+
cacheStore?: SegmentCacheStore;
|
|
76
|
+
/**
|
|
77
|
+
* Cache profiles in the `createRouter({ cacheProfiles })` shape. Required for
|
|
78
|
+
* a `use cache: "profileName"` function to resolve its profile (an unknown
|
|
79
|
+
* profile throws), once a `cacheStore` is wired.
|
|
80
|
+
*/
|
|
81
|
+
cacheProfiles?: Record<string, CacheProfile>;
|
|
82
|
+
/**
|
|
83
|
+
* Theme config in the same shape `createRouter({ theme })` takes (resolved
|
|
84
|
+
* internally). Without it `ctx.theme`/`ctx.setTheme` are inert (undefined),
|
|
85
|
+
* mirroring an app with no theme configured. Pass one (e.g. `true`, or
|
|
86
|
+
* `{ themes: [...] }`) to exercise a handler that reads them.
|
|
87
|
+
*/
|
|
88
|
+
theme?: ThemeConfig | true;
|
|
89
|
+
/**
|
|
90
|
+
* Customize the rango state cookie that `invalidateClientCache()` rotates.
|
|
91
|
+
* The name is ALWAYS seeded (default `rango-state_router_0`) so a call to
|
|
92
|
+
* `invalidateClientCache()` rotates and emits the `Set-Cookie` exactly as in
|
|
93
|
+
* production, rather than silently no-opping. Override `prefix`/`routerId` to
|
|
94
|
+
* match your `createRouter({ stateCookiePrefix, id })` so the test asserts the
|
|
95
|
+
* same name, or `version` (the build identifier prefixed to the rotated
|
|
96
|
+
* `{version}:{timestamp}` value, default `"0"`). Assert
|
|
97
|
+
* `response.headers.getSetCookie()` against the resolved `stateCookieName`
|
|
98
|
+
* (returned by `runInRequestContext`).
|
|
99
|
+
*/
|
|
100
|
+
stateCookie?: StateCookieSeed;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The seeded RequestContext with its `reverse` RELAXED to accept any route NAME
|
|
105
|
+
* from the `routeMap` you passed, rather than the global `Rango.GeneratedRouteMap`
|
|
106
|
+
* union — so reversing a test-only route name is not a type error (it works at
|
|
107
|
+
* runtime; the names come from your `routeMap`). Mirrors runLoader's
|
|
108
|
+
* `TestLoaderContext.reverse`. Everything else is the real `RequestContext`.
|
|
109
|
+
*/
|
|
110
|
+
export type TestRequestContextObject<TEnv> = Omit<
|
|
111
|
+
RequestContext<TEnv>,
|
|
112
|
+
"reverse"
|
|
113
|
+
> & {
|
|
114
|
+
reverse: (
|
|
115
|
+
name: string,
|
|
116
|
+
params?: Record<string, string>,
|
|
117
|
+
search?: Record<string, unknown>,
|
|
118
|
+
) => string;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export interface TestRequestContext<TEnv> {
|
|
122
|
+
ctx: TestRequestContextObject<TEnv>;
|
|
123
|
+
request: Request;
|
|
124
|
+
url: URL;
|
|
125
|
+
variables: Record<string, unknown>;
|
|
126
|
+
/**
|
|
127
|
+
* The resolved rango state cookie name seeded into the context (default
|
|
128
|
+
* `rango-state_router_0`, or composed from `opts.stateCookie`). The name a
|
|
129
|
+
* call to `invalidateClientCache()` rotates.
|
|
130
|
+
*/
|
|
131
|
+
stateCookieName: string;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Create a real RequestContext for unit-testing loaders/middleware.
|
|
136
|
+
*
|
|
137
|
+
* The returned `ctx` must be ENTERED before use — wrap your call in
|
|
138
|
+
* `runWithRequestContext(ctx, fn)` (re-exported from `@rangojs/router/testing`)
|
|
139
|
+
* so that cookie/header mutations and `getRequestContext()` resolve. For the
|
|
140
|
+
* common case prefer {@link runInRequestContext}, which builds AND enters the
|
|
141
|
+
* context in a single call.
|
|
142
|
+
*/
|
|
143
|
+
export function createTestRequestContext<TEnv>(
|
|
144
|
+
opts: CreateTestContextOptions<TEnv> = {},
|
|
145
|
+
): TestRequestContext<TEnv> {
|
|
146
|
+
const request = toRequest(opts.request, opts.requestInit);
|
|
147
|
+
const url = new URL(request.url);
|
|
148
|
+
const variables = seedVariables(opts.variables ?? {}, opts.vars);
|
|
149
|
+
// Always seed a resolved name so invalidateClientCache() rotates (and emits
|
|
150
|
+
// the Set-Cookie) like production instead of no-opping; opts.stateCookie
|
|
151
|
+
// customizes the name/version. Surfaced on the result so a consumer asserts
|
|
152
|
+
// the rotation against the same name without recomputing it.
|
|
153
|
+
const stateCookieName = resolveSeededStateCookieName(opts.stateCookie);
|
|
154
|
+
const ctx = createRequestContext<TEnv>({
|
|
155
|
+
env: (opts.env ?? {}) as TEnv,
|
|
156
|
+
request,
|
|
157
|
+
url,
|
|
158
|
+
variables,
|
|
159
|
+
themeConfig:
|
|
160
|
+
opts.theme === undefined ? undefined : resolveThemeConfig(opts.theme),
|
|
161
|
+
cacheStore: opts.cacheStore,
|
|
162
|
+
cacheProfiles: opts.cacheProfiles,
|
|
163
|
+
stateCookieName,
|
|
164
|
+
version: opts.stateCookie?.version,
|
|
165
|
+
});
|
|
166
|
+
if (opts.basename !== undefined)
|
|
167
|
+
ctx._basename = normalizeBasename(opts.basename);
|
|
168
|
+
if (opts.params) ctx.params = opts.params;
|
|
169
|
+
if (opts.routeMap) {
|
|
170
|
+
ctx._routeName = opts.routeName;
|
|
171
|
+
ctx.reverse = createReverseFunction(
|
|
172
|
+
opts.routeMap,
|
|
173
|
+
opts.routeName,
|
|
174
|
+
opts.params ?? {},
|
|
175
|
+
) as RequestContext<TEnv>["reverse"];
|
|
176
|
+
}
|
|
177
|
+
// ctx.reverse is assigned the routeMap-scoped reverse (string-accepting) above;
|
|
178
|
+
// expose it through the relaxed type so a test reverses a local route name
|
|
179
|
+
// without casting. The runtime value matches; RequestContext's reverse is
|
|
180
|
+
// declared against the narrower global route-name union, hence the cast.
|
|
181
|
+
return {
|
|
182
|
+
ctx: ctx as unknown as TestRequestContextObject<TEnv>,
|
|
183
|
+
request,
|
|
184
|
+
url,
|
|
185
|
+
variables,
|
|
186
|
+
stateCookieName,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* What a run accumulated on the request context, surfaced as PUBLIC values so a
|
|
192
|
+
* test never has to cast through the `@internal` `ctx.res` / `ctx.cookies()` to
|
|
193
|
+
* assert what an action produced.
|
|
194
|
+
*/
|
|
195
|
+
export interface RunInRequestContextResult<T> {
|
|
196
|
+
/**
|
|
197
|
+
* The value `fn` returned (awaited), or `undefined` if `fn` threw — in which
|
|
198
|
+
* case the thrown value is on {@link thrown}. The snapshot below is captured
|
|
199
|
+
* either way.
|
|
200
|
+
*/
|
|
201
|
+
result: T | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* The value `fn` threw, or `undefined` if it returned normally. Commonly a
|
|
204
|
+
* `Response` from `throw redirect(...)` / `throw notFound()` — the dominant
|
|
205
|
+
* cookie+flash case is an action that sets them then throws a redirect — so
|
|
206
|
+
* this (and the snapshot below) is observable WITHOUT wrapping the action in
|
|
207
|
+
* your own try/catch. NOTE: the value is captured, NOT re-thrown; assert on it
|
|
208
|
+
* for a throwing action.
|
|
209
|
+
*/
|
|
210
|
+
thrown: unknown;
|
|
211
|
+
/**
|
|
212
|
+
* A Response carrying the status, headers, and Set-Cookie the run set (via
|
|
213
|
+
* `cookies().set()`, `ctx.header()`, etc.). Assert Set-Cookie with
|
|
214
|
+
* `response.headers.getSetCookie()`. When `fn` threw a `Response` (a redirect),
|
|
215
|
+
* THIS is that Response with the accumulated Set-Cookie/headers merged in
|
|
216
|
+
* (mirroring how the framework merges them in production), so a redirect's
|
|
217
|
+
* Location AND the cookies it set are both observable here.
|
|
218
|
+
*/
|
|
219
|
+
response: Response;
|
|
220
|
+
/**
|
|
221
|
+
* The effective cookie view after the run: request cookies merged with
|
|
222
|
+
* anything the run set or deleted (last-write-wins), as `{ name: value }`.
|
|
223
|
+
*/
|
|
224
|
+
cookies: Record<string, string>;
|
|
225
|
+
/**
|
|
226
|
+
* The response headers the run set (via `ctx.header(...)`, plus a thrown
|
|
227
|
+
* redirect's `Location`), as a plain `{ name: value }` object — the same view
|
|
228
|
+
* as `response.headers`, but assertable like `cookies`/`locationState`.
|
|
229
|
+
* EXCLUDES `set-cookie` (use `cookies`, or `response.headers.getSetCookie()`).
|
|
230
|
+
* Header names are lowercased (HTTP headers are case-insensitive).
|
|
231
|
+
*/
|
|
232
|
+
headers: Record<string, string>;
|
|
233
|
+
/**
|
|
234
|
+
* Location state the run set via `ctx.setLocationState()` / `redirect({ state })`,
|
|
235
|
+
* resolved to the flat `{ key: value }` shape the client reads off
|
|
236
|
+
* `history.state` (empty object when none) — so a post-action flash ("Saved!")
|
|
237
|
+
* is assertable at the unit layer.
|
|
238
|
+
*/
|
|
239
|
+
locationState: Record<string, unknown>;
|
|
240
|
+
/**
|
|
241
|
+
* The resolved rango state cookie name seeded into the run (default
|
|
242
|
+
* `rango-state_router_0`, or composed from `opts.stateCookie`). Assert an
|
|
243
|
+
* action's `invalidateClientCache()` rotation against it without recomputing:
|
|
244
|
+
* `response.headers.getSetCookie().some((c) => c.startsWith(stateCookieName + "="))`.
|
|
245
|
+
*/
|
|
246
|
+
stateCookieName: string;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Snapshot the observable effects a run left on `ctx` (cookies + location
|
|
251
|
+
* state). Reads the fields directly off the ctx object, so it works both inside
|
|
252
|
+
* and outside the AsyncLocalStorage scope (no `getRequestContext()`). Headers are
|
|
253
|
+
* snapshotted separately from the final {@link Response} (via
|
|
254
|
+
* {@link headersToObject}) so a thrown redirect's `Location` is included.
|
|
255
|
+
*/
|
|
256
|
+
export function snapshotRunEffects<TEnv>(ctx: RequestContext<TEnv>): {
|
|
257
|
+
cookies: Record<string, string>;
|
|
258
|
+
locationState: Record<string, unknown>;
|
|
259
|
+
} {
|
|
260
|
+
return {
|
|
261
|
+
cookies: { ...ctx.cookies() },
|
|
262
|
+
locationState: resolveLocationStateEntries(ctx._locationState ?? []),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* The response headers as a plain `{ name: value }` object, EXCLUDING
|
|
268
|
+
* `set-cookie` (surfaced parsed on `cookies`). Names are lowercased (HTTP header
|
|
269
|
+
* names are case-insensitive). Read from the final response so a thrown
|
|
270
|
+
* redirect's `Location` and any `ctx.header(...)` both appear.
|
|
271
|
+
*/
|
|
272
|
+
export function headersToObject(headers: Headers): Record<string, string> {
|
|
273
|
+
const out: Record<string, string> = {};
|
|
274
|
+
headers.forEach((value, name) => {
|
|
275
|
+
if (name.toLowerCase() === "set-cookie") return;
|
|
276
|
+
out[name] = value;
|
|
277
|
+
});
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Build the observable response from what the run accumulated on `ctx.res`. When
|
|
283
|
+
* `fn` threw a `Response` (a `redirect()`/`notFound()`), that Response IS the
|
|
284
|
+
* response — merge the accumulated Set-Cookie/other headers into it (the
|
|
285
|
+
* framework does this when it catches the thrown Response in production), with
|
|
286
|
+
* its status/Location preserved. Otherwise snapshot the stub (status + headers).
|
|
287
|
+
* The `Response`/`Headers` constructors copy, so the result is immutable.
|
|
288
|
+
*/
|
|
289
|
+
export function buildRunResponse<TEnv>(
|
|
290
|
+
ctx: RequestContext<TEnv>,
|
|
291
|
+
thrown: unknown,
|
|
292
|
+
): Response {
|
|
293
|
+
const stub = ctx.res;
|
|
294
|
+
if (thrown instanceof Response) {
|
|
295
|
+
const headers = new Headers(thrown.headers);
|
|
296
|
+
for (const cookie of stub.headers.getSetCookie()) {
|
|
297
|
+
headers.append("set-cookie", cookie);
|
|
298
|
+
}
|
|
299
|
+
stub.headers.forEach((value, name) => {
|
|
300
|
+
if (name.toLowerCase() === "set-cookie") return;
|
|
301
|
+
if (!headers.has(name)) headers.set(name, value);
|
|
302
|
+
});
|
|
303
|
+
return new Response(null, { status: thrown.status, headers });
|
|
304
|
+
}
|
|
305
|
+
return new Response(null, { status: stub.status, headers: stub.headers });
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Snapshot the shared run-result envelope fields (everything except the primary
|
|
310
|
+
* value) from what a run left on `ctx`: the captured `thrown`, the merged
|
|
311
|
+
* `response`, the effective `cookies`/`headers` views, the `locationState`, and
|
|
312
|
+
* the resolved `stateCookieName`. Shared by `runInRequestContext` and
|
|
313
|
+
* `runLoaderResult` so the snapshot sequence lives once; each spreads it next to
|
|
314
|
+
* its own primary field (`result` / `data`).
|
|
315
|
+
*/
|
|
316
|
+
export function buildRunSnapshot<TEnv>(
|
|
317
|
+
ctx: RequestContext<TEnv>,
|
|
318
|
+
thrown: unknown,
|
|
319
|
+
stateCookieName: string,
|
|
320
|
+
): {
|
|
321
|
+
thrown: unknown;
|
|
322
|
+
response: Response;
|
|
323
|
+
cookies: Record<string, string>;
|
|
324
|
+
headers: Record<string, string>;
|
|
325
|
+
locationState: Record<string, unknown>;
|
|
326
|
+
stateCookieName: string;
|
|
327
|
+
} {
|
|
328
|
+
const { cookies, locationState } = snapshotRunEffects(ctx);
|
|
329
|
+
const response = buildRunResponse(ctx, thrown);
|
|
330
|
+
const headers = headersToObject(response.headers);
|
|
331
|
+
return { thrown, response, cookies, headers, locationState, stateCookieName };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Build a seeded RequestContext (via {@link createTestRequestContext}) and run
|
|
336
|
+
* `fn` inside it, so code under test that calls `getRequestContext()`,
|
|
337
|
+
* `cookies()`, or reads/mutates request headers resolves exactly as in
|
|
338
|
+
* production.
|
|
339
|
+
*
|
|
340
|
+
* This is the entry point for the advanced cases the unit wrappers
|
|
341
|
+
* (`runLoader` / `runMiddleware`) do not model — most notably a server ACTION
|
|
342
|
+
* that authenticates off the request cookie or sets a session cookie / flash:
|
|
343
|
+
* an action has no loader context, so `runLoader` is the wrong shape, yet it
|
|
344
|
+
* still needs a real request context to read the cookie and resolve
|
|
345
|
+
* `getRequestContext()`.
|
|
346
|
+
*
|
|
347
|
+
* Returns `{ result, thrown, response, cookies, headers, locationState }` so the
|
|
348
|
+
* action's OUTPUT (Set-Cookie, response headers, flash) is assertable without
|
|
349
|
+
* casting through the `@internal` `ctx.res` / `ctx.cookies()`. `fn` may be async — the context stays
|
|
350
|
+
* active across its awaits (AsyncLocalStorage), and the snapshot is captured
|
|
351
|
+
* whether `fn` returns OR throws. The throw path matters: the most common
|
|
352
|
+
* cookie+flash case is an auth action that sets a cookie + flash then
|
|
353
|
+
* `throw redirect(...)` on success — the thrown redirect is on `thrown` (NOT
|
|
354
|
+
* re-thrown) and its Location plus the cookies are on `response`/`cookies`.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* const { result, cookies, response, thrown } = await runInRequestContext(
|
|
359
|
+
* () => loginAction(input), // sets a session cookie, then `throw redirect("/app")`
|
|
360
|
+
* {
|
|
361
|
+
* env,
|
|
362
|
+
* request: new Request("https://app.test/", {
|
|
363
|
+
* headers: { Cookie: "sid=abc" },
|
|
364
|
+
* }),
|
|
365
|
+
* },
|
|
366
|
+
* );
|
|
367
|
+
* expect(cookies.session).toBe("new-token");
|
|
368
|
+
* expect(headers.location).toBe("/app"); // response headers as a plain object
|
|
369
|
+
* expect((thrown as Response).headers.get("Location")).toBe("/app");
|
|
370
|
+
* expect(response.headers.getSetCookie()).toContainEqual(
|
|
371
|
+
* expect.stringContaining("session="),
|
|
372
|
+
* );
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
375
|
+
export async function runInRequestContext<T, TEnv = unknown>(
|
|
376
|
+
fn: (ctx: RequestContext<TEnv>) => T | Promise<T>,
|
|
377
|
+
opts: CreateTestContextOptions<TEnv> = {},
|
|
378
|
+
): Promise<RunInRequestContextResult<T>> {
|
|
379
|
+
const { ctx, stateCookieName } = createTestRequestContext<TEnv>(opts);
|
|
380
|
+
let result: T | undefined;
|
|
381
|
+
let thrown: unknown;
|
|
382
|
+
try {
|
|
383
|
+
result = (await runWithRequestContext(ctx, () => fn(ctx))) as T;
|
|
384
|
+
} catch (error) {
|
|
385
|
+
// Capture (do NOT re-throw): a redirect/notFound action throws its Response
|
|
386
|
+
// on the SUCCESS path, and its cookie/flash output must stay observable.
|
|
387
|
+
thrown = error;
|
|
388
|
+
}
|
|
389
|
+
return { result, ...buildRunSnapshot(ctx, thrown, stateCookieName) };
|
|
390
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side-effect module: define the webpack-style globals the vendored
|
|
3
|
+
* react-server-dom CLIENT deserializer reads at module-eval time.
|
|
4
|
+
*
|
|
5
|
+
* In a real app the plugin-rsc Vite plugin rewrites `__webpack_require__` ->
|
|
6
|
+
* `__vite_rsc_require__` and `__webpack_require__.u` -> `({}).u`
|
|
7
|
+
* (@vitejs/plugin-rsc `core/plugin.js`). That transform does NOT run in a bare
|
|
8
|
+
* Vitest process, so the vendored client's free `__webpack_require__` /
|
|
9
|
+
* `__webpack_chunk_load__` references would be undefined. We provide minimal
|
|
10
|
+
* shims: `__webpack_require__` routes to the loader installed via
|
|
11
|
+
* `setRequireModule`, and `__webpack_chunk_load__` is a no-op (renderServerTree
|
|
12
|
+
* serializes with empty `chunks`, so no chunk fetch ever happens).
|
|
13
|
+
*
|
|
14
|
+
* MUST be imported (for side effect) BEFORE `@vitejs/plugin-rsc/react/browser`,
|
|
15
|
+
* which is why flight-tree.ts lists it first.
|
|
16
|
+
*/
|
|
17
|
+
const g = globalThis as unknown as {
|
|
18
|
+
__webpack_require__?: ((id: string) => unknown) & { u?: unknown };
|
|
19
|
+
__webpack_chunk_load__?: (chunkId: string) => Promise<unknown>;
|
|
20
|
+
__vite_rsc_client_require__?: (id: string) => unknown;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (!g.__webpack_require__) {
|
|
24
|
+
g.__webpack_require__ = (id: string) => g.__vite_rsc_client_require__!(id);
|
|
25
|
+
}
|
|
26
|
+
if (!g.__webpack_chunk_load__) {
|
|
27
|
+
g.__webpack_chunk_load__ = async () => {};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Variable seeding shared by the node/DOM testing tier (internal/context.ts)
|
|
3
|
+
* AND the react-server Flight tier (flight.ts). Depends only on the
|
|
4
|
+
* dependency-free `context-var` module and the env-agnostic state-cookie-name
|
|
5
|
+
* composition (no window/document), so it is safe to import under the
|
|
6
|
+
* `react-server` condition (unlike internal/context.ts, which pulls
|
|
7
|
+
* client/browser modules).
|
|
8
|
+
*/
|
|
9
|
+
import { contextSet, type ContextVar } from "../../context-var.js";
|
|
10
|
+
import { resolveStateCookieName } from "../../router/state-cookie-name.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Seed for the rango state cookie a handler/action/loader rotates when it calls
|
|
14
|
+
* `invalidateClientCache()`. Production always resolves a name at router init
|
|
15
|
+
* (so rotation always fires); the test stub did not, so the call silently
|
|
16
|
+
* no-opped. Supplying this (or accepting the defaults) closes that gap. Lives in
|
|
17
|
+
* the react-server-safe seed module so both the node tier (createTestRequestContext)
|
|
18
|
+
* and the Flight tier (renderHandler) share one shape and one default.
|
|
19
|
+
*/
|
|
20
|
+
export interface StateCookieSeed {
|
|
21
|
+
/**
|
|
22
|
+
* Cookie-name prefix, sanitized then composed with `routerId` exactly like
|
|
23
|
+
* `createRouter({ stateCookiePrefix })`. Defaults to `"rango-state"`.
|
|
24
|
+
*/
|
|
25
|
+
prefix?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Router id; the resolved name is `{sanitizedPrefix}_{sanitizedRouterId}`.
|
|
28
|
+
* Defaults to `"router_0"` (the name a single default router resolves to), so
|
|
29
|
+
* the default name is `rango-state_router_0`.
|
|
30
|
+
*/
|
|
31
|
+
routerId?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Build version used as the rotated value's prefix (`{version}:{timestamp}`).
|
|
34
|
+
* Defaults to `"0"` (resolved inside createRequestContext).
|
|
35
|
+
*/
|
|
36
|
+
version?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Resolve the state cookie name a seed maps to, mirroring `createRouter`'s
|
|
41
|
+
* `resolveStateCookieName` so a test asserts the SAME name production writes.
|
|
42
|
+
* The default routerId `"router_0"` matches a single default router.
|
|
43
|
+
*/
|
|
44
|
+
export function resolveSeededStateCookieName(seed?: StateCookieSeed): string {
|
|
45
|
+
return resolveStateCookieName(seed?.prefix, seed?.routerId ?? "router_0");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Initializer for seeded context variables (as a prior middleware would have
|
|
50
|
+
* set, or a server component would read during render). Either a plain object
|
|
51
|
+
* keyed by var name (the common, best-inferring form: `{ user: u }`) or a list
|
|
52
|
+
* of `[key, value]` tuples where the key may be a `createVar()` handle or a
|
|
53
|
+
* string (`[[userVar, u], ["flag", true]]`).
|
|
54
|
+
*/
|
|
55
|
+
export type VarsInit =
|
|
56
|
+
| Record<string, unknown>
|
|
57
|
+
| ReadonlyArray<readonly [ContextVar<unknown> | string, unknown]>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Preload variables as if set by upstream middleware (or visible to a rendered
|
|
61
|
+
* server tree). Accepts entries keyed by either a ContextVar (from createVar) or
|
|
62
|
+
* a string, matching ctx.set().
|
|
63
|
+
*/
|
|
64
|
+
export function seedVariables(
|
|
65
|
+
variables: Record<string, unknown>,
|
|
66
|
+
vars?: VarsInit,
|
|
67
|
+
): Record<string, unknown> {
|
|
68
|
+
if (!vars) return variables;
|
|
69
|
+
// Array/iterable form -> use the tuples as-is; plain object -> its entries.
|
|
70
|
+
const entries: Iterable<readonly [ContextVar<unknown> | string, unknown]> =
|
|
71
|
+
Symbol.iterator in (vars as object)
|
|
72
|
+
? (vars as ReadonlyArray<
|
|
73
|
+
readonly [ContextVar<unknown> | string, unknown]
|
|
74
|
+
>)
|
|
75
|
+
: Object.entries(vars as Record<string, unknown>);
|
|
76
|
+
for (const [key, value] of entries) {
|
|
77
|
+
contextSet(variables, key as ContextVar<unknown>, value);
|
|
78
|
+
}
|
|
79
|
+
return variables;
|
|
80
|
+
}
|