@decocms/blocks 8.0.0 → 8.1.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +39 -3
  3. package/src/cms/applySectionConventions.ts +26 -2
  4. package/src/cms/blockSource.ts +37 -0
  5. package/src/cms/client.ts +5 -0
  6. package/src/cms/deferredTrigger.ts +58 -0
  7. package/src/cms/draftSource.ts +171 -57
  8. package/src/cms/index.ts +32 -3
  9. package/src/cms/loader.ts +318 -59
  10. package/src/cms/registry.ts +22 -0
  11. package/src/cms/renderJson.ts +179 -0
  12. package/src/cms/resolve.ts +396 -95
  13. package/src/cms/schema.ts +9 -5
  14. package/src/cms/sectionLoaders.ts +106 -56
  15. package/src/hooks/Image.tsx +70 -6
  16. package/src/hooks/LiveControls.tsx +30 -8
  17. package/src/hooks/Stats.tsx +134 -0
  18. package/src/hooks/index.ts +5 -0
  19. package/src/hooks/useLoadMore.ts +100 -0
  20. package/src/index.ts +37 -0
  21. package/src/middleware/observability.ts +173 -16
  22. package/src/preview/DraftPreviewBadge.tsx +306 -0
  23. package/src/preview/decoMark.ts +11 -0
  24. package/src/preview/index.ts +8 -0
  25. package/src/protocol/assets.ts +124 -0
  26. package/src/protocol/canonical.ts +7 -0
  27. package/src/protocol/ciphertext.ts +6 -0
  28. package/src/protocol/client.ts +170 -0
  29. package/src/protocol/conformance/cases/assets.ts +81 -0
  30. package/src/protocol/conformance/cases/guards.ts +314 -0
  31. package/src/protocol/conformance/cases/reads.ts +168 -0
  32. package/src/protocol/conformance/cases/types.ts +9 -0
  33. package/src/protocol/conformance/cases/wire.ts +138 -0
  34. package/src/protocol/conformance/cases/writes.ts +345 -0
  35. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  36. package/src/protocol/conformance/context.ts +222 -0
  37. package/src/protocol/conformance/index.ts +140 -0
  38. package/src/protocol/errors.ts +142 -0
  39. package/src/protocol/index.ts +39 -0
  40. package/src/protocol/keys.ts +279 -0
  41. package/src/protocol/params.ts +67 -0
  42. package/src/protocol/secrets.ts +231 -0
  43. package/src/protocol/server/assets.ts +119 -0
  44. package/src/protocol/server/auth.ts +49 -0
  45. package/src/protocol/server/bodyCache.ts +84 -0
  46. package/src/protocol/server/content.ts +175 -0
  47. package/src/protocol/server/core.ts +112 -0
  48. package/src/protocol/server/handler.ts +102 -0
  49. package/src/protocol/server/http.ts +139 -0
  50. package/src/protocol/server/index.ts +11 -0
  51. package/src/protocol/server/methods/apply.ts +341 -0
  52. package/src/protocol/server/methods/read.ts +156 -0
  53. package/src/protocol/server/rpc.ts +136 -0
  54. package/src/protocol/storage/fs/hash.ts +21 -0
  55. package/src/protocol/storage/fs/index.ts +338 -0
  56. package/src/protocol/storage/fs/lock.ts +68 -0
  57. package/src/protocol/storage/fs/transaction.ts +183 -0
  58. package/src/protocol/storage/fs/unsupported.ts +15 -0
  59. package/src/protocol/storage/memory.ts +238 -0
  60. package/src/protocol/storage.ts +182 -0
  61. package/src/protocol/types.ts +172 -0
  62. package/src/sdk/abTesting.ts +60 -3
  63. package/src/sdk/analytics.ts +72 -2
  64. package/src/sdk/cacheHeaders.ts +210 -10
  65. package/src/sdk/cacheStorage.ts +287 -0
  66. package/src/sdk/cachedLoader.ts +143 -90
  67. package/src/sdk/crypto.ts +16 -2
  68. package/src/sdk/deepOmit.ts +52 -0
  69. package/src/sdk/encoding.ts +2 -5
  70. package/src/sdk/experiments.ts +314 -0
  71. package/src/sdk/fetchCache.ts +269 -0
  72. package/src/sdk/fetchTimeout.ts +58 -0
  73. package/src/sdk/flags.ts +38 -8
  74. package/src/sdk/index.ts +7 -0
  75. package/src/sdk/instrumentedFetch.ts +25 -2
  76. package/src/sdk/nonce.ts +46 -0
  77. package/src/sdk/normalizeUrls.ts +2 -2
  78. package/src/sdk/observability.ts +8 -1
  79. package/src/sdk/otel.ts +131 -25
  80. package/src/sdk/otelHttpLog.ts +4 -4
  81. package/src/sdk/otelHttpMeter.ts +39 -13
  82. package/src/sdk/otelHttpTracer.ts +4 -4
  83. package/src/sdk/redirects.ts +113 -5
  84. package/src/sdk/responseCache.ts +82 -0
  85. package/src/sdk/urlUtils.ts +49 -0
  86. package/src/sdk/useSuggestions.ts +1 -1
  87. package/src/v8/analytics.ts +50 -0
  88. package/src/v8/background.ts +107 -0
  89. package/src/v8/builtins/data.ts +23 -0
  90. package/src/v8/builtins/index.ts +26 -0
  91. package/src/v8/builtins/lazy.ts +23 -0
  92. package/src/v8/builtins/legacy.ts +65 -0
  93. package/src/v8/builtins/matchers.ts +31 -0
  94. package/src/v8/builtins/multivariate.ts +25 -0
  95. package/src/v8/builtins/secret.ts +82 -0
  96. package/src/v8/canonical.ts +117 -0
  97. package/src/v8/ciphertext.ts +158 -0
  98. package/src/v8/cli/builtins.ts +20 -0
  99. package/src/v8/cli/check/index.ts +774 -0
  100. package/src/v8/cli/check/messages.ts +168 -0
  101. package/src/v8/cli/content.ts +287 -0
  102. package/src/v8/cli/index.ts +17 -0
  103. package/src/v8/cli/log.ts +18 -0
  104. package/src/v8/cli/main.ts +12 -0
  105. package/src/v8/cli/root.ts +66 -0
  106. package/src/v8/cli/run.ts +236 -0
  107. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  108. package/src/v8/cli/schema/generate.ts +507 -0
  109. package/src/v8/cli/schema/index.ts +63 -0
  110. package/src/v8/cli/schema/tsProgram.ts +497 -0
  111. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  112. package/src/v8/cli/serve/server.ts +303 -0
  113. package/src/v8/cli/watch.ts +86 -0
  114. package/src/v8/client.ts +147 -0
  115. package/src/v8/cms.ts +237 -0
  116. package/src/v8/content.ts +195 -0
  117. package/src/v8/draft.ts +136 -0
  118. package/src/v8/errors.ts +59 -0
  119. package/src/v8/fetch.ts +152 -0
  120. package/src/v8/identity.ts +65 -0
  121. package/src/v8/index.ts +39 -0
  122. package/src/v8/json.ts +74 -0
  123. package/src/v8/matchRoute.ts +204 -0
  124. package/src/v8/remoteLoader.ts +282 -0
  125. package/src/v8/resolver.ts +278 -0
  126. package/src/v8/secrets.ts +17 -0
  127. package/src/v8/telemetry.ts +533 -0
  128. package/src/v8/types.ts +240 -0
  129. package/src/cms/blockSource.test.ts +0 -76
  130. package/src/cms/client.browserBundle.test.ts +0 -44
  131. package/src/cms/draftSource.test.ts +0 -343
  132. package/src/cms/layoutCacheRace.test.ts +0 -181
  133. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  134. package/src/cms/loader.test.ts +0 -200
  135. package/src/cms/registry.test.ts +0 -110
  136. package/src/cms/resolve.test.ts +0 -980
  137. package/src/cms/schema.test.ts +0 -356
  138. package/src/cms/sectionLoaderContext.test.ts +0 -111
  139. package/src/cms/sectionLoaders.test.ts +0 -533
  140. package/src/cms/sectionMixins.test.ts +0 -167
  141. package/src/cms/stickyFlags.test.ts +0 -117
  142. package/src/flags/flags.test.ts +0 -148
  143. package/src/hooks/Image.test.ts +0 -100
  144. package/src/hooks/RenderSection.test.tsx +0 -81
  145. package/src/matchers/builtins.test.ts +0 -318
  146. package/src/matchers/override.test.ts +0 -205
  147. package/src/middleware/hydrationContext.test.ts +0 -61
  148. package/src/middleware/observability.test.ts +0 -242
  149. package/src/middleware/validateSection.test.ts +0 -147
  150. package/src/sdk/abTesting.test.ts +0 -326
  151. package/src/sdk/cacheHeaders.test.ts +0 -115
  152. package/src/sdk/cachedLoader.test.ts +0 -122
  153. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  154. package/src/sdk/cn.test.ts +0 -34
  155. package/src/sdk/composite.test.ts +0 -121
  156. package/src/sdk/cookie.test.ts +0 -108
  157. package/src/sdk/encoding.test.ts +0 -71
  158. package/src/sdk/env.test.ts +0 -33
  159. package/src/sdk/flags.test.ts +0 -91
  160. package/src/sdk/http.test.ts +0 -71
  161. package/src/sdk/inflightTimeout.test.ts +0 -35
  162. package/src/sdk/instrumentedFetch.test.ts +0 -559
  163. package/src/sdk/invoke.test.ts +0 -115
  164. package/src/sdk/logger.test.ts +0 -432
  165. package/src/sdk/otel.test.ts +0 -526
  166. package/src/sdk/otelAdapters.test.ts +0 -89
  167. package/src/sdk/otelHttpLog.test.ts +0 -457
  168. package/src/sdk/otelHttpMeter.test.ts +0 -292
  169. package/src/sdk/otelHttpTracer.test.ts +0 -474
  170. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  171. package/src/sdk/urlRedaction.test.ts +0 -73
  172. package/src/sdk/useDevice.test.ts +0 -130
  173. package/src/sdk/useScript.test.ts +0 -128
  174. package/src/sdk/useSuggestions.test.ts +0 -230
  175. package/tsconfig.json +0 -7
@@ -1,533 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import { configureTracer, type Span } from "../sdk/observability";
3
- import { RequestContext } from "../sdk/requestContext";
4
- import type { ResolvedSection } from "./resolve";
5
- import {
6
- getDegradedSections,
7
- isLayoutSection,
8
- registerCacheableSections,
9
- registerLayoutSections,
10
- registerNonCriticalSections,
11
- registerSectionLoader,
12
- registerSectionLoaders,
13
- runSectionLoaders,
14
- runSingleSectionLoader,
15
- unregisterLayoutSections,
16
- } from "./sectionLoaders";
17
- import { compose, withDevice, withMobile, withSearchParam } from "./sectionMixins";
18
-
19
- const G = globalThis as any;
20
-
21
- beforeEach(() => {
22
- G.__deco.sectionLoaderRegistry.clear();
23
- G.__deco.layoutSections.clear();
24
- G.__deco.cacheableSections.clear();
25
- G.__deco.nonCriticalSections.clear();
26
- });
27
-
28
- const makeSection = (component: string, props: Record<string, unknown> = {}): ResolvedSection => ({
29
- component,
30
- props,
31
- key: component,
32
- index: 0,
33
- });
34
-
35
- describe("section degradation tracking (anti-cache-poisoning)", () => {
36
- const failing = async () => {
37
- throw new Error("VTEX down");
38
- };
39
-
40
- it("marks a critical section as degraded when its loader throws", async () => {
41
- registerSectionLoader("site/sections/ProductShelf.tsx", failing);
42
- const section = makeSection("site/sections/ProductShelf.tsx");
43
- const request = new Request("https://store.com/p");
44
-
45
- const degraded = await RequestContext.run(request, async () => {
46
- // Loader throws → section degrades to raw props (does not reject).
47
- const result = await runSingleSectionLoader(section, request);
48
- expect(result.component).toBe("site/sections/ProductShelf.tsx");
49
- return getDegradedSections();
50
- });
51
-
52
- expect(degraded).toContain("site/sections/ProductShelf.tsx");
53
- });
54
-
55
- it("does NOT mark a non-critical section as degraded", async () => {
56
- registerSectionLoader("site/sections/Decorative.tsx", failing);
57
- registerNonCriticalSections(["site/sections/Decorative.tsx"]);
58
- const section = makeSection("site/sections/Decorative.tsx");
59
- const request = new Request("https://store.com/p");
60
-
61
- const degraded = await RequestContext.run(request, async () => {
62
- await runSingleSectionLoader(section, request);
63
- return getDegradedSections();
64
- });
65
-
66
- expect(degraded).toEqual([]);
67
- });
68
-
69
- it("does NOT mark a failing LAYOUT section as degraded (avoids site-wide poisoning)", async () => {
70
- registerSectionLoader("site/sections/Header.tsx", failing);
71
- registerLayoutSections(["site/sections/Header.tsx"]);
72
- const section = makeSection("site/sections/Header.tsx");
73
- const request = new Request("https://store.com/p");
74
-
75
- const degraded = await RequestContext.run(request, async () => {
76
- await runSingleSectionLoader(section, request);
77
- return getDegradedSections();
78
- });
79
-
80
- // A flaky Header/Footer must not flip every page to X-Deco-Degraded.
81
- expect(degraded).toEqual([]);
82
- });
83
-
84
- it("does not flag a page whose loaders all succeed", async () => {
85
- registerSectionLoader("site/sections/Ok.tsx", async (p: Record<string, unknown>) => p);
86
- const section = makeSection("site/sections/Ok.tsx");
87
- const request = new Request("https://store.com/p");
88
-
89
- const degraded = await RequestContext.run(request, async () => {
90
- await runSingleSectionLoader(section, request);
91
- return getDegradedSections();
92
- });
93
-
94
- expect(degraded).toEqual([]);
95
- });
96
-
97
- it("is a no-op outside a request scope (does not throw)", async () => {
98
- registerSectionLoader("site/sections/NoScope.tsx", failing);
99
- const section = makeSection("site/sections/NoScope.tsx");
100
- const request = new Request("https://store.com/p");
101
- // No RequestContext.run wrapper — must not throw.
102
- await expect(runSingleSectionLoader(section, request)).resolves.toBeTruthy();
103
- expect(getDegradedSections()).toEqual([]);
104
- });
105
- });
106
-
107
- describe("runSingleSectionLoader — page context injection", () => {
108
- it("injects __pageUrl and __pagePath into loader props", async () => {
109
- // Two-arg signature so loader.mock.calls[0] types as [props, req].
110
- const loader = vi.fn(async (props: Record<string, unknown>, _req: Request) => props);
111
- registerSectionLoader("site/sections/SearchBanner.tsx", loader);
112
-
113
- const section = makeSection("site/sections/SearchBanner.tsx", { foo: "bar" });
114
- const request = new Request("https://store.com/lingerie?q=preto");
115
-
116
- await runSingleSectionLoader(section, request);
117
-
118
- expect(loader).toHaveBeenCalledTimes(1);
119
- const [calledProps, calledReq] = loader.mock.calls[0];
120
- expect(calledProps).toMatchObject({
121
- foo: "bar",
122
- __pageUrl: "https://store.com/lingerie?q=preto",
123
- __pagePath: "/lingerie",
124
- });
125
- expect(calledReq).toBe(request);
126
- });
127
-
128
- it("preserves existing __pageUrl / __pagePath from props (site workaround compat)", async () => {
129
- const loader = vi.fn(async (props: Record<string, unknown>) => props);
130
- registerSectionLoader("site/sections/Custom.tsx", loader);
131
-
132
- const section = makeSection("site/sections/Custom.tsx", {
133
- __pageUrl: "https://override.example/page",
134
- __pagePath: "/override",
135
- });
136
- const request = new Request("https://store.com/real-path");
137
-
138
- await runSingleSectionLoader(section, request);
139
-
140
- const [calledProps] = loader.mock.calls[0];
141
- expect(calledProps.__pageUrl).toBe("https://override.example/page");
142
- expect(calledProps.__pagePath).toBe("/override");
143
- });
144
-
145
- it("does not throw when request.url is invalid", async () => {
146
- const loader = vi.fn(async (props: Record<string, unknown>) => props);
147
- registerSectionLoader("site/sections/X.tsx", loader);
148
-
149
- const section = makeSection("site/sections/X.tsx", { foo: 1 });
150
- const badReq = { url: "not a url" } as unknown as Request;
151
-
152
- await expect(runSingleSectionLoader(section, badReq)).resolves.toBeDefined();
153
- expect(loader).toHaveBeenCalled();
154
- });
155
-
156
- it("returns section unchanged when no loader is registered", async () => {
157
- const section = makeSection("site/sections/NoLoader.tsx", { foo: 1 });
158
- const result = await runSingleSectionLoader(section, new Request("https://store.com/"));
159
- expect(result).toBe(section);
160
- });
161
-
162
- it("passes a 3rd-arg compat ctx (device from UA) — regular path (#305)", async () => {
163
- const loader = vi.fn(
164
- async (props: Record<string, unknown>, _req: Request, ctx?: { device?: string }) => ({
165
- ...props,
166
- seenDevice: ctx?.device,
167
- }),
168
- );
169
- registerSectionLoader("site/sections/Ctx.tsx", loader);
170
-
171
- const section = makeSection("site/sections/Ctx.tsx", {});
172
- const mobileReq = new Request("https://store.com/", {
173
- headers: { "user-agent": "iPhone Mobile Safari" },
174
- });
175
- const result = await runSingleSectionLoader(section, mobileReq);
176
-
177
- expect((result.props as Record<string, unknown>).seenDevice).toBe("mobile");
178
- });
179
-
180
- it("passes the compat ctx through layout and cacheable paths (#305)", async () => {
181
- const layoutLoader = vi.fn(
182
- async (props: Record<string, unknown>, _req: Request, ctx?: { device?: string }) => ({
183
- ...props,
184
- seenDevice: ctx?.device,
185
- }),
186
- );
187
- const cacheableLoader = vi.fn(
188
- async (props: Record<string, unknown>, _req: Request, ctx?: { device?: string }) => ({
189
- ...props,
190
- seenDevice: ctx?.device,
191
- }),
192
- );
193
- registerSectionLoader("site/sections/CtxHeader.tsx", layoutLoader);
194
- registerLayoutSections(["site/sections/CtxHeader.tsx"]);
195
- registerSectionLoader("site/sections/CtxShelf.tsx", cacheableLoader);
196
- registerCacheableSections({ "site/sections/CtxShelf.tsx": { maxAge: 60_000 } });
197
-
198
- const mobileReq = new Request("https://store.com/", {
199
- headers: { "user-agent": "iPhone Mobile Safari" },
200
- });
201
-
202
- const layoutResult = await runSingleSectionLoader(
203
- makeSection("site/sections/CtxHeader.tsx"),
204
- mobileReq,
205
- );
206
- const cacheableResult = await runSingleSectionLoader(
207
- makeSection("site/sections/CtxShelf.tsx"),
208
- mobileReq,
209
- );
210
-
211
- expect((layoutResult.props as Record<string, unknown>).seenDevice).toBe("mobile");
212
- expect((cacheableResult.props as Record<string, unknown>).seenDevice).toBe("mobile");
213
- });
214
- });
215
-
216
- describe("runSingleSectionLoader — cache keying", () => {
217
- it("cacheable sections share a cache entry across different page URLs", async () => {
218
- const loader = vi.fn(async (props: Record<string, unknown>) => ({
219
- ...props,
220
- enriched: true,
221
- }));
222
- registerSectionLoader("site/sections/Shelf.tsx", loader);
223
- registerCacheableSections({ "site/sections/Shelf.tsx": { maxAge: 60_000 } });
224
-
225
- const section = makeSection("site/sections/Shelf.tsx", { title: "Best" });
226
-
227
- await runSingleSectionLoader(section, new Request("https://store.com/page-a"));
228
- await runSingleSectionLoader(section, new Request("https://store.com/page-b"));
229
- await runSingleSectionLoader(section, new Request("https://store.com/page-c"));
230
-
231
- // Without URL-agnostic cache keys, this would be 3.
232
- expect(loader).toHaveBeenCalledTimes(1);
233
- });
234
-
235
- it("layout sections cache by component name and reuse across pages", async () => {
236
- const loader = vi.fn(async (props: Record<string, unknown>) => props);
237
- registerSectionLoader("site/sections/Header.tsx", loader);
238
- registerLayoutSections(["site/sections/Header.tsx"]);
239
-
240
- const section = makeSection("site/sections/Header.tsx", { variant: "default" });
241
-
242
- await runSingleSectionLoader(section, new Request("https://store.com/a"));
243
- await runSingleSectionLoader(section, new Request("https://store.com/b"));
244
-
245
- expect(loader).toHaveBeenCalledTimes(1);
246
- });
247
-
248
- it("regular (non-cached) sections re-run on every request", async () => {
249
- const loader = vi.fn(async (props: Record<string, unknown>) => props);
250
- registerSectionLoader("site/sections/Reg.tsx", loader);
251
-
252
- const section = makeSection("site/sections/Reg.tsx", {});
253
- await runSingleSectionLoader(section, new Request("https://store.com/a"));
254
- await runSingleSectionLoader(section, new Request("https://store.com/b"));
255
-
256
- expect(loader).toHaveBeenCalledTimes(2);
257
- });
258
- });
259
-
260
- // ---------------------------------------------------------------------------
261
- // Layout cache opt-out (#206)
262
- // ---------------------------------------------------------------------------
263
-
264
- describe("unregisterLayoutSections", () => {
265
- it("drops a key from the layout set so the loader re-runs per request", async () => {
266
- const loader = vi.fn(async (props: Record<string, unknown>) => props);
267
- registerSectionLoader("site/sections/Header.tsx", loader);
268
- registerLayoutSections(["site/sections/Header.tsx"]);
269
- expect(isLayoutSection("site/sections/Header.tsx")).toBe(true);
270
-
271
- unregisterLayoutSections(["site/sections/Header.tsx"]);
272
- expect(isLayoutSection("site/sections/Header.tsx")).toBe(false);
273
-
274
- const section = makeSection("site/sections/Header.tsx", {});
275
- await runSingleSectionLoader(section, new Request("https://store.com/a"));
276
- await runSingleSectionLoader(section, new Request("https://store.com/b"));
277
-
278
- // No layout caching after unregister — loader runs per request.
279
- expect(loader).toHaveBeenCalledTimes(2);
280
- });
281
-
282
- it("is a no-op for keys that were never registered", () => {
283
- expect(() => unregisterLayoutSections(["site/sections/Never.tsx"])).not.toThrow();
284
- expect(isLayoutSection("site/sections/Never.tsx")).toBe(false);
285
- });
286
- });
287
-
288
- // ---------------------------------------------------------------------------
289
- // Dev warning: layout + request-dependent loader → cache contamination (#206)
290
- // ---------------------------------------------------------------------------
291
-
292
- describe("registerSectionLoaders — request-dependent + layout warning (#206)", () => {
293
- const originalNodeEnv = process.env.NODE_ENV;
294
- let warnSpy: ReturnType<typeof vi.spyOn>;
295
-
296
- beforeEach(() => {
297
- process.env.NODE_ENV = "development";
298
- warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
299
- });
300
-
301
- afterEach(() => {
302
- process.env.NODE_ENV = originalNodeEnv;
303
- warnSpy.mockRestore();
304
- });
305
-
306
- it("warns when a request-dependent loader is registered for a layout section", () => {
307
- registerLayoutSections(["site/sections/Header.tsx"]);
308
- registerSectionLoaders({
309
- "site/sections/Header.tsx": withDevice(),
310
- });
311
- const calls = warnSpy.mock.calls.map((c) => String(c[0]));
312
- expect(calls.some((m) => m.includes("Header.tsx"))).toBe(true);
313
- expect(calls.some((m) => m.includes("layout section"))).toBe(true);
314
- expect(calls.some((m) => m.includes("unregisterLayoutSections"))).toBe(true);
315
- });
316
-
317
- it("warns when a composed loader contains any request-dependent mixin", () => {
318
- registerLayoutSections(["site/sections/Footer.tsx"]);
319
- registerSectionLoaders({
320
- "site/sections/Footer.tsx": compose(withSearchParam(), async (props) => props),
321
- });
322
- expect(warnSpy).toHaveBeenCalled();
323
- });
324
-
325
- it("does NOT warn for a layout section whose loader is request-independent", () => {
326
- registerLayoutSections(["site/sections/Theme.tsx"]);
327
- registerSectionLoaders({
328
- "site/sections/Theme.tsx": async (props) => props,
329
- });
330
- expect(warnSpy).not.toHaveBeenCalled();
331
- });
332
-
333
- it("does NOT warn after unregisterLayoutSections drops the key", () => {
334
- registerLayoutSections(["site/sections/Header.tsx"]);
335
- unregisterLayoutSections(["site/sections/Header.tsx"]);
336
- registerSectionLoaders({
337
- "site/sections/Header.tsx": withDevice(),
338
- });
339
- expect(warnSpy).not.toHaveBeenCalled();
340
- });
341
-
342
- it("does NOT warn in production", () => {
343
- process.env.NODE_ENV = "production";
344
- registerLayoutSections(["site/sections/Header.tsx"]);
345
- registerSectionLoaders({
346
- "site/sections/Header.tsx": withMobile(),
347
- });
348
- expect(warnSpy).not.toHaveBeenCalled();
349
- });
350
- });
351
-
352
- describe("runSingleSectionLoader — error handling", () => {
353
- it("falls back to original section when loader throws", async () => {
354
- const loader = vi.fn(async () => {
355
- throw new Error("boom");
356
- });
357
- registerSectionLoader("site/sections/Boom.tsx", loader);
358
-
359
- const section = makeSection("site/sections/Boom.tsx", { x: 1 });
360
- const result = await runSingleSectionLoader(section, new Request("https://store.com/"));
361
- expect(result).toEqual(section);
362
- });
363
- });
364
-
365
- describe("runSingleSectionLoader — nested section recursion", () => {
366
- it("runs the loader of a nested section in props", async () => {
367
- const childLoader = vi.fn(async (props: Record<string, unknown>) => ({
368
- ...props,
369
- enriched: true,
370
- }));
371
- registerSectionLoader("site/sections/CategoryBanner.tsx", childLoader);
372
-
373
- // Parent has no own loader, only a nested section in props
374
- const parent = makeSection("site/sections/BackgroundWrapper.tsx", {
375
- child: {
376
- Component: "site/sections/CategoryBanner.tsx",
377
- props: { matcher: "/foo" },
378
- },
379
- });
380
-
381
- const result = await runSingleSectionLoader(parent, new Request("https://store.com/foo"));
382
-
383
- expect(childLoader).toHaveBeenCalledTimes(1);
384
- expect(result.props).toEqual({
385
- child: {
386
- Component: "site/sections/CategoryBanner.tsx",
387
- props: {
388
- matcher: "/foo",
389
- enriched: true,
390
- // page context is injected for nested sections too
391
- __pageUrl: "https://store.com/foo",
392
- __pagePath: "/foo",
393
- },
394
- },
395
- });
396
- });
397
-
398
- it("runs nested loaders in arrays (e.g. sections: Section[])", async () => {
399
- const banner = vi.fn(async (props: any) => ({ ...props, ranBanner: true }));
400
- const shelf = vi.fn(async (props: any) => ({ ...props, ranShelf: true }));
401
- registerSectionLoader("site/sections/Banner.tsx", banner);
402
- registerSectionLoader("site/sections/Shelf.tsx", shelf);
403
-
404
- const parent = makeSection("site/sections/Wrapper.tsx", {
405
- sections: [
406
- { Component: "site/sections/Banner.tsx", props: { id: 1 } },
407
- { Component: "site/sections/Shelf.tsx", props: { id: 2 } },
408
- ],
409
- });
410
-
411
- const result = await runSingleSectionLoader(parent, new Request("https://store.com/"));
412
-
413
- expect(banner).toHaveBeenCalledTimes(1);
414
- expect(shelf).toHaveBeenCalledTimes(1);
415
- const sections = (result.props as any).sections;
416
- expect(sections[0].props).toMatchObject({ id: 1, ranBanner: true });
417
- expect(sections[1].props).toMatchObject({ id: 2, ranShelf: true });
418
- });
419
-
420
- it("returns same props reference when no nested sections (zero-alloc leaf path)", async () => {
421
- const loader = vi.fn(async (props: Record<string, unknown>) => props);
422
- registerSectionLoader("site/sections/Leaf.tsx", loader);
423
-
424
- const props = { foo: "bar" };
425
- const section = makeSection("site/sections/Leaf.tsx", props);
426
-
427
- const result = await runSingleSectionLoader(section, new Request("https://store.com/"));
428
-
429
- // Loader returned the SAME props ref → enrichNestedSections must also
430
- // return the same ref → no { ...result, props } wrapping happens
431
- expect(result.props).toMatchObject({
432
- foo: "bar",
433
- __pageUrl: "https://store.com/",
434
- __pagePath: "/",
435
- });
436
- });
437
-
438
- it("recurses into deeply nested sections (wrapper inside wrapper)", async () => {
439
- const inner = vi.fn(async (props: any) => ({ ...props, deep: true }));
440
- registerSectionLoader("site/sections/Inner.tsx", inner);
441
-
442
- const parent = makeSection("site/sections/Outer.tsx", {
443
- child: {
444
- Component: "site/sections/MidWrapper.tsx",
445
- props: {
446
- grandchild: {
447
- Component: "site/sections/Inner.tsx",
448
- props: { tag: "deep" },
449
- },
450
- },
451
- },
452
- });
453
-
454
- const result = await runSingleSectionLoader(parent, new Request("https://store.com/"));
455
-
456
- expect(inner).toHaveBeenCalledTimes(1);
457
- const grandchild = (result.props as any).child.props.grandchild;
458
- expect(grandchild.props).toMatchObject({ tag: "deep", deep: true });
459
- });
460
-
461
- it("ignores nested objects that do not look like sections", async () => {
462
- const loader = vi.fn(async (props: Record<string, unknown>) => props);
463
- registerSectionLoader("site/sections/Leaf.tsx", loader);
464
-
465
- const section = makeSection("site/sections/Leaf.tsx", {
466
- // Plain config object, not a section. Has `Component: string` but
467
- // missing the `props` field — must NOT be treated as a nested section.
468
- config: { Component: "ButtonStyle", color: "red" },
469
- });
470
-
471
- const result = await runSingleSectionLoader(section, new Request("https://store.com/"));
472
-
473
- expect(loader).toHaveBeenCalledTimes(1);
474
- expect((result.props as any).config).toEqual({
475
- Component: "ButtonStyle",
476
- color: "red",
477
- });
478
- });
479
-
480
- it("runs nested loaders even when parent has no own loader", async () => {
481
- const childLoader = vi.fn(async (props: any) => ({ ...props, ran: true }));
482
- registerSectionLoader("site/sections/Child.tsx", childLoader);
483
-
484
- // Parent has no entry in registry — but it has a nested section in props
485
- // (typical of a pure layout container that just renders children).
486
- const parent = makeSection("site/sections/UnregisteredLayout.tsx", {
487
- child: {
488
- Component: "site/sections/Child.tsx",
489
- props: { foo: "bar" },
490
- },
491
- });
492
-
493
- const result = await runSingleSectionLoader(parent, new Request("https://store.com/"));
494
-
495
- expect(childLoader).toHaveBeenCalledTimes(1);
496
- expect((result.props as any).child.props).toMatchObject({
497
- foo: "bar",
498
- ran: true,
499
- });
500
- });
501
- });
502
-
503
- describe("runSectionLoaders — batch span", () => {
504
- afterEach(() => {
505
- configureTracer({ startSpan: () => ({ end: () => {} }) });
506
- });
507
-
508
- it("emits one deco.section.loaders.batch parent with section.count", async () => {
509
- const spans: Array<{ name: string; attrs?: Record<string, unknown> }> = [];
510
- configureTracer({
511
- startSpan: (name, attrs) => {
512
- spans.push({ name, attrs });
513
- const s: Span = { end: () => {} };
514
- return s;
515
- },
516
- });
517
-
518
- registerSectionLoader("a", async (p) => p);
519
- registerSectionLoader("b", async (p) => p);
520
-
521
- await runSectionLoaders(
522
- [makeSection("a"), makeSection("b"), makeSection("c-no-loader")],
523
- new Request("https://site/"),
524
- );
525
-
526
- const batch = spans.find((s) => s.name === "deco.section.loaders.batch");
527
- expect(batch).toBeDefined();
528
- expect(batch?.attrs).toMatchObject({ "section.count": 3 });
529
-
530
- const perSection = spans.filter((s) => s.name === "deco.section.loader");
531
- expect(perSection).toHaveLength(3);
532
- });
533
- });