@decocms/blocks 8.0.0 → 8.1.0-next.1

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 (220) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +32 -82
  3. package/src/index.ts +35 -8
  4. package/src/protocol/assets.ts +124 -0
  5. package/src/protocol/canonical.ts +7 -0
  6. package/src/protocol/ciphertext.ts +6 -0
  7. package/src/protocol/client.ts +170 -0
  8. package/src/protocol/conformance/cases/assets.ts +81 -0
  9. package/src/protocol/conformance/cases/guards.ts +314 -0
  10. package/src/protocol/conformance/cases/reads.ts +168 -0
  11. package/src/protocol/conformance/cases/types.ts +9 -0
  12. package/src/protocol/conformance/cases/wire.ts +138 -0
  13. package/src/protocol/conformance/cases/writes.ts +345 -0
  14. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  15. package/src/protocol/conformance/context.ts +222 -0
  16. package/src/protocol/conformance/index.ts +140 -0
  17. package/src/protocol/errors.ts +142 -0
  18. package/src/protocol/index.ts +39 -0
  19. package/src/protocol/keys.ts +279 -0
  20. package/src/protocol/params.ts +67 -0
  21. package/src/protocol/secrets.ts +231 -0
  22. package/src/protocol/server/assets.ts +119 -0
  23. package/src/protocol/server/auth.ts +49 -0
  24. package/src/protocol/server/bodyCache.ts +84 -0
  25. package/src/protocol/server/content.ts +175 -0
  26. package/src/protocol/server/core.ts +112 -0
  27. package/src/protocol/server/handler.ts +102 -0
  28. package/src/protocol/server/http.ts +139 -0
  29. package/src/protocol/server/index.ts +11 -0
  30. package/src/protocol/server/methods/apply.ts +341 -0
  31. package/src/protocol/server/methods/read.ts +156 -0
  32. package/src/protocol/server/rpc.ts +136 -0
  33. package/src/protocol/storage/fs/hash.ts +21 -0
  34. package/src/protocol/storage/fs/index.ts +338 -0
  35. package/src/protocol/storage/fs/lock.ts +68 -0
  36. package/src/protocol/storage/fs/transaction.ts +183 -0
  37. package/src/protocol/storage/fs/unsupported.ts +15 -0
  38. package/src/protocol/storage/memory.ts +238 -0
  39. package/src/protocol/storage.ts +182 -0
  40. package/src/protocol/types.ts +172 -0
  41. package/src/v8/analytics.ts +50 -0
  42. package/src/v8/background.ts +107 -0
  43. package/src/v8/builtins/data.ts +23 -0
  44. package/src/v8/builtins/index.ts +26 -0
  45. package/src/v8/builtins/lazy.ts +23 -0
  46. package/src/v8/builtins/legacy.ts +65 -0
  47. package/src/v8/builtins/matchers.ts +31 -0
  48. package/src/v8/builtins/multivariate.ts +25 -0
  49. package/src/v8/builtins/secret.ts +82 -0
  50. package/src/v8/canonical.ts +117 -0
  51. package/src/v8/ciphertext.ts +158 -0
  52. package/src/v8/cli/builtins.ts +20 -0
  53. package/src/v8/cli/check/index.ts +774 -0
  54. package/src/v8/cli/check/messages.ts +168 -0
  55. package/src/v8/cli/content.ts +287 -0
  56. package/src/v8/cli/index.ts +17 -0
  57. package/src/v8/cli/log.ts +18 -0
  58. package/src/v8/cli/main.ts +12 -0
  59. package/src/v8/cli/root.ts +66 -0
  60. package/src/v8/cli/run.ts +236 -0
  61. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  62. package/src/v8/cli/schema/generate.ts +507 -0
  63. package/src/v8/cli/schema/index.ts +63 -0
  64. package/src/v8/cli/schema/tsProgram.ts +497 -0
  65. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  66. package/src/v8/cli/serve/server.ts +303 -0
  67. package/src/v8/cli/watch.ts +86 -0
  68. package/src/v8/client.ts +147 -0
  69. package/src/v8/cms.ts +237 -0
  70. package/src/v8/content.ts +195 -0
  71. package/src/v8/draft.ts +136 -0
  72. package/src/v8/errors.ts +59 -0
  73. package/src/v8/fetch.ts +133 -0
  74. package/src/v8/identity.ts +65 -0
  75. package/src/v8/index.ts +39 -0
  76. package/src/v8/json.ts +74 -0
  77. package/src/v8/matchRoute.ts +204 -0
  78. package/src/v8/remoteLoader.ts +282 -0
  79. package/src/v8/resolver.ts +278 -0
  80. package/src/v8/secrets.ts +17 -0
  81. package/src/v8/telemetry.ts +533 -0
  82. package/src/v8/types.ts +240 -0
  83. package/src/cms/applySectionConventions.ts +0 -124
  84. package/src/cms/blockSource.test.ts +0 -76
  85. package/src/cms/blockSource.ts +0 -191
  86. package/src/cms/client.browserBundle.test.ts +0 -44
  87. package/src/cms/client.ts +0 -80
  88. package/src/cms/draftSource.test.ts +0 -343
  89. package/src/cms/draftSource.ts +0 -362
  90. package/src/cms/index.ts +0 -151
  91. package/src/cms/layoutCacheRace.test.ts +0 -181
  92. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  93. package/src/cms/loadDecofileDirectory.ts +0 -89
  94. package/src/cms/loader.test.ts +0 -200
  95. package/src/cms/loader.ts +0 -304
  96. package/src/cms/registry.test.ts +0 -110
  97. package/src/cms/registry.ts +0 -242
  98. package/src/cms/resolve.test.ts +0 -980
  99. package/src/cms/resolve.ts +0 -2282
  100. package/src/cms/schema.test.ts +0 -356
  101. package/src/cms/schema.ts +0 -1215
  102. package/src/cms/sectionLoaderContext.test.ts +0 -111
  103. package/src/cms/sectionLoaderContext.ts +0 -113
  104. package/src/cms/sectionLoaders.test.ts +0 -533
  105. package/src/cms/sectionLoaders.ts +0 -636
  106. package/src/cms/sectionMixins.test.ts +0 -167
  107. package/src/cms/sectionMixins.ts +0 -153
  108. package/src/cms/stickyFlags.test.ts +0 -117
  109. package/src/flags/audience.ts +0 -56
  110. package/src/flags/everyone.ts +0 -29
  111. package/src/flags/flag.ts +0 -15
  112. package/src/flags/flags.test.ts +0 -148
  113. package/src/flags/multivariate/image.ts +0 -11
  114. package/src/flags/multivariate/message.ts +0 -11
  115. package/src/flags/multivariate/page.ts +0 -16
  116. package/src/flags/multivariate/section.ts +0 -16
  117. package/src/flags/multivariate.ts +0 -1
  118. package/src/flags/types.ts +0 -59
  119. package/src/flags/utils/multivariate.ts +0 -20
  120. package/src/hooks/Image.test.ts +0 -100
  121. package/src/hooks/Image.tsx +0 -250
  122. package/src/hooks/JsonLd.tsx +0 -353
  123. package/src/hooks/LazySection.tsx +0 -121
  124. package/src/hooks/LiveControls.tsx +0 -122
  125. package/src/hooks/Picture.tsx +0 -127
  126. package/src/hooks/RenderSection.test.tsx +0 -81
  127. package/src/hooks/RenderSection.tsx +0 -91
  128. package/src/hooks/SectionErrorFallback.tsx +0 -97
  129. package/src/hooks/index.ts +0 -30
  130. package/src/matchers/builtins.test.ts +0 -318
  131. package/src/matchers/builtins.ts +0 -459
  132. package/src/matchers/countryNames.ts +0 -104
  133. package/src/matchers/override.test.ts +0 -205
  134. package/src/matchers/override.ts +0 -136
  135. package/src/matchers/posthog.ts +0 -154
  136. package/src/middleware/decoState.ts +0 -55
  137. package/src/middleware/healthMetrics.ts +0 -133
  138. package/src/middleware/hydrationContext.test.ts +0 -61
  139. package/src/middleware/hydrationContext.ts +0 -79
  140. package/src/middleware/index.ts +0 -88
  141. package/src/middleware/liveness.ts +0 -21
  142. package/src/middleware/observability.test.ts +0 -242
  143. package/src/middleware/observability.ts +0 -643
  144. package/src/middleware/validateSection.test.ts +0 -147
  145. package/src/middleware/validateSection.ts +0 -100
  146. package/src/sdk/abTesting.test.ts +0 -326
  147. package/src/sdk/abTesting.ts +0 -499
  148. package/src/sdk/analytics.ts +0 -77
  149. package/src/sdk/cacheHeaders.test.ts +0 -115
  150. package/src/sdk/cacheHeaders.ts +0 -433
  151. package/src/sdk/cachedLoader.test.ts +0 -122
  152. package/src/sdk/cachedLoader.ts +0 -503
  153. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  154. package/src/sdk/clx.ts +0 -5
  155. package/src/sdk/cn.test.ts +0 -34
  156. package/src/sdk/cn.ts +0 -28
  157. package/src/sdk/composite.test.ts +0 -121
  158. package/src/sdk/composite.ts +0 -114
  159. package/src/sdk/cookie.test.ts +0 -108
  160. package/src/sdk/cookie.ts +0 -129
  161. package/src/sdk/crypto.ts +0 -185
  162. package/src/sdk/csp.ts +0 -59
  163. package/src/sdk/detectDevice.ts +0 -45
  164. package/src/sdk/djb2.ts +0 -20
  165. package/src/sdk/encoding.test.ts +0 -71
  166. package/src/sdk/encoding.ts +0 -47
  167. package/src/sdk/env.test.ts +0 -33
  168. package/src/sdk/env.ts +0 -54
  169. package/src/sdk/flags.test.ts +0 -91
  170. package/src/sdk/flags.ts +0 -108
  171. package/src/sdk/http.test.ts +0 -71
  172. package/src/sdk/http.ts +0 -124
  173. package/src/sdk/index.ts +0 -90
  174. package/src/sdk/inflightTimeout.test.ts +0 -35
  175. package/src/sdk/inflightTimeout.ts +0 -53
  176. package/src/sdk/instrumentedFetch.test.ts +0 -559
  177. package/src/sdk/instrumentedFetch.ts +0 -339
  178. package/src/sdk/invoke.test.ts +0 -115
  179. package/src/sdk/invoke.ts +0 -298
  180. package/src/sdk/logger.test.ts +0 -432
  181. package/src/sdk/logger.ts +0 -304
  182. package/src/sdk/mergeCacheControl.ts +0 -150
  183. package/src/sdk/normalizeUrls.ts +0 -91
  184. package/src/sdk/observability.ts +0 -109
  185. package/src/sdk/otel.test.ts +0 -526
  186. package/src/sdk/otel.ts +0 -1012
  187. package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
  188. package/src/sdk/otelAdapters.test.ts +0 -89
  189. package/src/sdk/otelAdapters.ts +0 -144
  190. package/src/sdk/otelHttpLog.test.ts +0 -457
  191. package/src/sdk/otelHttpLog.ts +0 -419
  192. package/src/sdk/otelHttpMeter.test.ts +0 -292
  193. package/src/sdk/otelHttpMeter.ts +0 -506
  194. package/src/sdk/otelHttpTracer.test.ts +0 -474
  195. package/src/sdk/otelHttpTracer.ts +0 -543
  196. package/src/sdk/redirects.ts +0 -228
  197. package/src/sdk/requestContext.ts +0 -281
  198. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  199. package/src/sdk/requestContextStorage.browser.ts +0 -43
  200. package/src/sdk/requestContextStorage.ts +0 -35
  201. package/src/sdk/retry.ts +0 -45
  202. package/src/sdk/serverTimings.ts +0 -68
  203. package/src/sdk/signal.ts +0 -42
  204. package/src/sdk/sitemap.ts +0 -160
  205. package/src/sdk/urlRedaction.test.ts +0 -73
  206. package/src/sdk/urlRedaction.ts +0 -82
  207. package/src/sdk/urlUtils.ts +0 -134
  208. package/src/sdk/useDevice.test.ts +0 -130
  209. package/src/sdk/useDevice.ts +0 -89
  210. package/src/sdk/useDeviceContext.tsx +0 -108
  211. package/src/sdk/useId.ts +0 -7
  212. package/src/sdk/useScript.test.ts +0 -128
  213. package/src/sdk/useScript.ts +0 -210
  214. package/src/sdk/useSuggestions.test.ts +0 -230
  215. package/src/sdk/useSuggestions.ts +0 -188
  216. package/src/sdk/wrapCaughtErrors.ts +0 -107
  217. package/src/setup.ts +0 -120
  218. package/src/types/index.ts +0 -53
  219. package/src/types/widgets.ts +0 -14
  220. 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
- });