@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,318 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import type { MatcherContext } from "../cms/resolve";
3
- import { evaluateMatcher } from "../cms/resolve";
4
- import { registerBuiltinMatchers } from "./builtins";
5
-
6
- const LOCATION_KEY = "website/matchers/location.ts";
7
- const DATE_KEY = "website/matchers/date.ts";
8
-
9
- beforeEach(() => {
10
- registerBuiltinMatchers();
11
- });
12
-
13
- function ctxFromCookies(cookies: Record<string, string>): MatcherContext {
14
- return { cookies };
15
- }
16
-
17
- function ctxFromCf(cf: Record<string, unknown>): MatcherContext {
18
- const request = new Request("https://example.com/");
19
- Object.defineProperty(request, "cf", { value: cf, configurable: true });
20
- return { request };
21
- }
22
-
23
- function ctxFromHeaders(headers: Record<string, string>): MatcherContext {
24
- return {
25
- request: new Request("https://example.com/", { headers }),
26
- };
27
- }
28
-
29
- function match(rule: Record<string, unknown>, ctx: MatcherContext): boolean {
30
- return evaluateMatcher({ ...rule, __resolveType: LOCATION_KEY }, ctx);
31
- }
32
-
33
- describe("locationMatcher — typed mode (Location)", () => {
34
- it("matches when regionCode equals Cloudflare's cf-region-code (SP)", () => {
35
- const ctx = ctxFromHeaders({ "cf-region-code": "SP", "cf-ipcountry": "BR" });
36
- expect(match({ includeLocations: [{ regionCode: "SP" }] }, ctx)).toBe(true);
37
- });
38
-
39
- it("matches a raw numeric regionCode (e.g. '47') for parity with deco-cx/apps", () => {
40
- const ctx = ctxFromHeaders({ "cf-region-code": "47", "cf-ipcountry": "BR" });
41
- expect(match({ includeLocations: [{ regionCode: "47" }] }, ctx)).toBe(true);
42
- });
43
-
44
- it("is case-insensitive on regionCode", () => {
45
- const ctx = ctxFromHeaders({ "cf-region-code": "SP" });
46
- expect(match({ includeLocations: [{ regionCode: "sp" }] }, ctx)).toBe(true);
47
- });
48
-
49
- it("does NOT match a region NAME against cf-region-code (parity with original)", () => {
50
- const ctx = ctxFromHeaders({ "cf-region-code": "SP" });
51
- expect(
52
- match({ includeLocations: [{ regionCode: "São Paulo" }] }, ctx),
53
- ).toBe(false);
54
- });
55
-
56
- it("does not match when the region differs", () => {
57
- const ctx = ctxFromHeaders({ "cf-region-code": "SP" });
58
- expect(match({ includeLocations: [{ regionCode: "RJ" }] }, ctx)).toBe(false);
59
- });
60
-
61
- it("AND's multiple fields on the same entry (regionCode + country)", () => {
62
- const ctx = ctxFromHeaders({ "cf-region-code": "SP", "cf-ipcountry": "BR" });
63
- expect(
64
- match(
65
- { includeLocations: [{ regionCode: "SP", country: "BR" }] },
66
- ctx,
67
- ),
68
- ).toBe(true);
69
- expect(
70
- match(
71
- { includeLocations: [{ regionCode: "SP", country: "AR" }] },
72
- ctx,
73
- ),
74
- ).toBe(false);
75
- });
76
-
77
- it("resolves country aliases (Brasil → BR)", () => {
78
- const ctx = ctxFromHeaders({ "cf-ipcountry": "BR", "cf-region-code": "SP" });
79
- expect(
80
- match({ includeLocations: [{ country: "Brasil" }] }, ctx),
81
- ).toBe(true);
82
- });
83
-
84
- it("OR's across multiple include entries", () => {
85
- const ctx = ctxFromHeaders({ "cf-region-code": "RJ" });
86
- expect(
87
- match(
88
- {
89
- includeLocations: [{ regionCode: "SP" }, { regionCode: "RJ" }],
90
- },
91
- ctx,
92
- ),
93
- ).toBe(true);
94
- });
95
- });
96
-
97
- describe("locationMatcher — empty / shape edge cases", () => {
98
- it("empty includeLocations array → match (no constraint)", () => {
99
- const ctx = ctxFromHeaders({ "cf-region-code": "SP" });
100
- expect(match({ includeLocations: [] }, ctx)).toBe(true);
101
- });
102
-
103
- it("missing includeLocations → match", () => {
104
- const ctx = ctxFromHeaders({ "cf-region-code": "SP" });
105
- expect(match({}, ctx)).toBe(true);
106
- });
107
-
108
- it("entry {} inside includeLocations matches everyone (parity with original)", () => {
109
- const ctx = ctxFromHeaders({ "cf-region-code": "SP", "cf-ipcountry": "BR" });
110
- expect(match({ includeLocations: [{}] }, ctx)).toBe(true);
111
- });
112
-
113
- it("entry {} inside excludeLocations does NOT exclude anyone (parity)", () => {
114
- const ctx = ctxFromHeaders({ "cf-region-code": "SP", "cf-ipcountry": "BR" });
115
- expect(match({ excludeLocations: [{}] }, ctx)).toBe(true);
116
- });
117
-
118
- it("excludeLocations short-circuits over includeLocations", () => {
119
- const ctx = ctxFromHeaders({ "cf-region-code": "SP" });
120
- expect(
121
- match(
122
- {
123
- includeLocations: [{ regionCode: "SP" }],
124
- excludeLocations: [{ regionCode: "SP" }],
125
- },
126
- ctx,
127
- ),
128
- ).toBe(false);
129
- });
130
-
131
- it("includeLocations with [{regionCode}] fails when geo is empty", () => {
132
- expect(
133
- match({ includeLocations: [{ regionCode: "SP" }] }, {}),
134
- ).toBe(false);
135
- });
136
- });
137
-
138
- describe("locationMatcher — Map mode (haversine)", () => {
139
- it("matches when source coords are within target radius", () => {
140
- // Target: São Paulo center, 5km. Source: ~500m away.
141
- const ctx = ctxFromHeaders({
142
- "cf-iplatitude": "-23.5510",
143
- "cf-iplongitude": "-46.6340",
144
- });
145
- expect(
146
- match(
147
- { includeLocations: [{ coordinates: "-23.5505,-46.6333,5000" }] },
148
- ctx,
149
- ),
150
- ).toBe(true);
151
- });
152
-
153
- it("does NOT match when source coords are outside the radius", () => {
154
- // Target: São Paulo, 5km. Source: Rio (~360km away).
155
- const ctx = ctxFromHeaders({
156
- "cf-iplatitude": "-22.9068",
157
- "cf-iplongitude": "-43.1729",
158
- });
159
- expect(
160
- match(
161
- { includeLocations: [{ coordinates: "-23.5505,-46.6333,5000" }] },
162
- ctx,
163
- ),
164
- ).toBe(false);
165
- });
166
-
167
- it("Map-only rule does NOT match when source has no coordinates", () => {
168
- // Deliberate divergence from deco-cx/apps: upstream lets coord-only rules
169
- // vacuously pass when the visitor has no lat/lng, which matches every
170
- // such visitor — a footgun in production. We require both sides to have
171
- // coordinates before the haversine check passes.
172
- const ctx = ctxFromHeaders({ "cf-region-code": "SP" });
173
- expect(
174
- match(
175
- { includeLocations: [{ coordinates: "-23.5505,-46.6333,5000" }] },
176
- ctx,
177
- ),
178
- ).toBe(false);
179
- });
180
-
181
- it("AND's coordinates with regionCode on the same entry", () => {
182
- // Source: SP coords, region=SP.
183
- const ctx = ctxFromHeaders({
184
- "cf-region-code": "SP",
185
- "cf-iplatitude": "-23.5510",
186
- "cf-iplongitude": "-46.6340",
187
- });
188
- // Entry asks for region=SP AND within 5km of SP center — matches.
189
- expect(
190
- match(
191
- {
192
- includeLocations: [
193
- { regionCode: "SP", coordinates: "-23.5505,-46.6333,5000" },
194
- ],
195
- },
196
- ctx,
197
- ),
198
- ).toBe(true);
199
- // Entry asks for region=SP AND within 5km of Rio — fails on coords.
200
- expect(
201
- match(
202
- {
203
- includeLocations: [
204
- { regionCode: "SP", coordinates: "-22.9068,-43.1729,5000" },
205
- ],
206
- },
207
- ctx,
208
- ),
209
- ).toBe(false);
210
- });
211
- });
212
-
213
- describe("locationMatcher — data source fallbacks", () => {
214
- it("reads from request.cf when headers are absent", () => {
215
- const ctx = ctxFromCf({ country: "BR", regionCode: "SP" });
216
- expect(match({ includeLocations: [{ regionCode: "SP" }] }, ctx)).toBe(true);
217
- });
218
-
219
- it("reads from __cf_geo_* cookies as fallback", () => {
220
- const ctx = ctxFromCookies({
221
- __cf_geo_country: "BR",
222
- __cf_geo_region_code: "SP",
223
- });
224
- expect(match({ includeLocations: [{ regionCode: "SP" }] }, ctx)).toBe(true);
225
- });
226
-
227
- it("decodes URL-encoded cookie values (e.g. city with accent)", () => {
228
- const ctx = ctxFromCookies({
229
- __cf_geo_country: "BR",
230
- __cf_geo_city: encodeURIComponent("São Paulo"),
231
- });
232
- expect(
233
- match({ includeLocations: [{ city: "São Paulo" }] }, ctx),
234
- ).toBe(true);
235
- });
236
-
237
- it("headers take precedence over request.cf and cookies", () => {
238
- const request = new Request("https://example.com/", {
239
- headers: { "cf-region-code": "SP" },
240
- });
241
- Object.defineProperty(request, "cf", {
242
- value: { regionCode: "RJ", country: "BR" },
243
- configurable: true,
244
- });
245
- const ctx: MatcherContext = {
246
- request,
247
- cookies: { __cf_geo_region_code: "MG" },
248
- };
249
- expect(match({ includeLocations: [{ regionCode: "SP" }] }, ctx)).toBe(true);
250
- expect(match({ includeLocations: [{ regionCode: "RJ" }] }, ctx)).toBe(false);
251
- });
252
- });
253
-
254
- function matchDate(rule: Record<string, unknown>): boolean {
255
- return evaluateMatcher({ ...rule, __resolveType: DATE_KEY }, {});
256
- }
257
-
258
- describe("dateMatcher — parity with deco-cx/apps website/matchers/date.ts", () => {
259
- beforeEach(() => {
260
- vi.useFakeTimers();
261
- vi.setSystemTime(new Date("2026-07-07T12:00:00Z"));
262
- });
263
-
264
- afterEach(() => {
265
- vi.useRealTimers();
266
- });
267
-
268
- it("matches when neither start nor end is given (vacuously true)", () => {
269
- expect(matchDate({})).toBe(true);
270
- });
271
-
272
- it("matches when now is after start and no end is given", () => {
273
- expect(matchDate({ start: "2026-07-01T00:00:00Z" })).toBe(true);
274
- });
275
-
276
- it("does not match when now is before start", () => {
277
- expect(matchDate({ start: "2026-08-01T00:00:00Z" })).toBe(false);
278
- });
279
-
280
- it("matches when now is before end and no start is given", () => {
281
- expect(matchDate({ end: "2026-08-01T00:00:00Z" })).toBe(true);
282
- });
283
-
284
- it("does not match when now is after end", () => {
285
- expect(matchDate({ end: "2026-06-01T00:00:00Z" })).toBe(false);
286
- });
287
-
288
- it("matches when now is strictly between start and end", () => {
289
- expect(
290
- matchDate({ start: "2026-07-01T00:00:00Z", end: "2026-07-31T00:00:00Z" }),
291
- ).toBe(true);
292
- });
293
-
294
- it("does not match outside a [start, end] window in either direction", () => {
295
- expect(
296
- matchDate({ start: "2026-08-01T00:00:00Z", end: "2026-09-01T00:00:00Z" }),
297
- ).toBe(false);
298
- expect(
299
- matchDate({ start: "2026-01-01T00:00:00Z", end: "2026-02-01T00:00:00Z" }),
300
- ).toBe(false);
301
- });
302
-
303
- it("does not match at the exact start boundary instant (strict >, not >=)", () => {
304
- expect(matchDate({ start: "2026-07-07T12:00:00Z" })).toBe(false);
305
- });
306
-
307
- it("does not match at the exact end boundary instant (strict <, not <=)", () => {
308
- expect(matchDate({ end: "2026-07-07T12:00:00Z" })).toBe(false);
309
- });
310
-
311
- it("treats an invalid start date as non-matching (NaN comparison is always false)", () => {
312
- expect(matchDate({ start: "not-a-date" })).toBe(false);
313
- });
314
-
315
- it("treats an invalid end date as non-matching", () => {
316
- expect(matchDate({ end: "not-a-date" })).toBe(false);
317
- });
318
- });
@@ -1,205 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
- import { setBlocks } from "../cms/loader";
3
- import type { MatcherContext } from "../cms/resolve";
4
- import { evaluateMatcher, registerMatcher } from "../cms/resolve";
5
- import { DECO_MATCHERS_OVERRIDE_PARAM, getMatchersOverride } from "./override";
6
-
7
- const TEST_MATCHER_KEY = "test/matchers/flag.ts";
8
- // Block names with spaces are only addressable via query string — the header
9
- // format splits pairs on spaces (same limitation as the Deno runtime).
10
- const SAVED_BLOCK = "Segmento Mobile";
11
- const SAVED_BLOCK_NO_SPACE = "PromoAtiva";
12
-
13
- const matcherFn = vi.fn(
14
- (rule: Record<string, unknown>) => (rule.result as boolean | undefined) ?? false,
15
- );
16
-
17
- function ctxWithHeader(value: string): MatcherContext {
18
- return {
19
- url: "https://example.com/",
20
- request: new Request("https://example.com/", {
21
- headers: { [DECO_MATCHERS_OVERRIDE_PARAM]: value },
22
- }),
23
- };
24
- }
25
-
26
- function ctxWithQS(search: string): MatcherContext {
27
- const url = `https://example.com/?${search}`;
28
- return { url, request: new Request(url) };
29
- }
30
-
31
- beforeEach(() => {
32
- matcherFn.mockClear();
33
- registerMatcher(TEST_MATCHER_KEY, matcherFn);
34
- setBlocks({
35
- [SAVED_BLOCK]: { __resolveType: TEST_MATCHER_KEY, result: true },
36
- [SAVED_BLOCK_NO_SPACE]: { __resolveType: TEST_MATCHER_KEY, result: true },
37
- });
38
- });
39
-
40
- describe("getMatchersOverride", () => {
41
- it("parses space-separated pairs from the header", () => {
42
- const overrides = getMatchersOverride(ctxWithHeader("a=1 b=0"));
43
- expect(overrides).toEqual({ a: true, b: false });
44
- });
45
-
46
- it("parses repeated query string params, including names with spaces", () => {
47
- const overrides = getMatchersOverride(
48
- ctxWithQS(
49
- `${DECO_MATCHERS_OVERRIDE_PARAM}=Segmento%20Mobile%3D1&${DECO_MATCHERS_OVERRIDE_PARAM}=b%3D0`,
50
- ),
51
- );
52
- expect(overrides).toEqual({ "Segmento Mobile": true, b: false });
53
- });
54
-
55
- it("prefers the header over the query string", () => {
56
- const url = `https://example.com/?${DECO_MATCHERS_OVERRIDE_PARAM}=a%3D0`;
57
- const ctx: MatcherContext = {
58
- url,
59
- request: new Request(url, {
60
- headers: { [DECO_MATCHERS_OVERRIDE_PARAM]: "a=1" },
61
- }),
62
- };
63
- expect(getMatchersOverride(ctx)).toEqual({ a: true });
64
- });
65
-
66
- it("falls back to the headers record when no Request is present", () => {
67
- const overrides = getMatchersOverride({
68
- headers: { [DECO_MATCHERS_OVERRIDE_PARAM]: "a=1" },
69
- });
70
- expect(overrides).toEqual({ a: true });
71
- });
72
-
73
- it("returns an empty object when no override is present", () => {
74
- expect(getMatchersOverride({ url: "https://example.com/" })).toEqual({});
75
- expect(getMatchersOverride({})).toEqual({});
76
- });
77
- });
78
-
79
- describe("evaluateMatcher with overrides", () => {
80
- it("forces a saved matcher block to false via header without running it", () => {
81
- const result = evaluateMatcher(
82
- { __resolveType: SAVED_BLOCK_NO_SPACE },
83
- ctxWithHeader(`${SAVED_BLOCK_NO_SPACE}=0`),
84
- );
85
- expect(result).toBe(false);
86
- expect(matcherFn).not.toHaveBeenCalled();
87
- });
88
-
89
- it("forces a spaced-name block to false via query string", () => {
90
- const result = evaluateMatcher(
91
- { __resolveType: SAVED_BLOCK },
92
- ctxWithQS(`${DECO_MATCHERS_OVERRIDE_PARAM}=${encodeURIComponent(`${SAVED_BLOCK}=0`)}`),
93
- );
94
- expect(result).toBe(false);
95
- expect(matcherFn).not.toHaveBeenCalled();
96
- });
97
-
98
- it("forces a saved matcher block to true without running it", () => {
99
- setBlocks({
100
- [SAVED_BLOCK]: { __resolveType: TEST_MATCHER_KEY, result: false },
101
- });
102
- const result = evaluateMatcher(
103
- { __resolveType: SAVED_BLOCK },
104
- ctxWithQS(`${DECO_MATCHERS_OVERRIDE_PARAM}=${encodeURIComponent(`${SAVED_BLOCK}=1`)}`),
105
- );
106
- expect(result).toBe(true);
107
- expect(matcherFn).not.toHaveBeenCalled();
108
- });
109
-
110
- it("runs the matcher normally when no override targets it", () => {
111
- const result = evaluateMatcher(
112
- { __resolveType: SAVED_BLOCK },
113
- ctxWithHeader("Outro Segmento=0"),
114
- );
115
- expect(result).toBe(true);
116
- expect(matcherFn).toHaveBeenCalledTimes(1);
117
- });
118
-
119
- it("does not affect inline matchers keyed by raw type", () => {
120
- const result = evaluateMatcher(
121
- { __resolveType: TEST_MATCHER_KEY, result: true },
122
- ctxWithHeader(`${TEST_MATCHER_KEY}=0`),
123
- );
124
- expect(result).toBe(true);
125
- expect(matcherFn).toHaveBeenCalledTimes(1);
126
- });
127
- });
128
-
129
- describe("evaluateMatcher with inline rule path overrides", () => {
130
- const PAGE_ID = "pages-home-c4bcbfb771e9";
131
-
132
- function setPageBlocks() {
133
- const page = {
134
- __resolveType: "website/pages/Page.tsx",
135
- sections: [
136
- { __resolveType: "site/sections/Header.tsx" },
137
- {
138
- __resolveType: "website/flags/multivariate/section.ts",
139
- variants: [
140
- {
141
- rule: { __resolveType: TEST_MATCHER_KEY, result: true },
142
- value: { __resolveType: "site/sections/A.tsx" },
143
- },
144
- {
145
- rule: { __resolveType: TEST_MATCHER_KEY, result: false },
146
- value: { __resolveType: "site/sections/B.tsx" },
147
- },
148
- ],
149
- },
150
- ],
151
- };
152
- setBlocks({ [PAGE_ID]: page });
153
- return page;
154
- }
155
-
156
- it("forces an inline rule by <blockId>@<prop.path> without running it", () => {
157
- const page = setPageBlocks();
158
- const rule = page.sections[1].variants![0].rule as Record<string, unknown>;
159
- const result = evaluateMatcher(
160
- rule,
161
- ctxWithQS(
162
- `${DECO_MATCHERS_OVERRIDE_PARAM}=${encodeURIComponent(
163
- `${PAGE_ID}@sections.1.variants.0.rule=0`,
164
- )}`,
165
- ),
166
- );
167
- expect(result).toBe(false);
168
- expect(matcherFn).not.toHaveBeenCalled();
169
- });
170
-
171
- it("forces an inline rule to true via header path key", () => {
172
- const page = setPageBlocks();
173
- const rule = page.sections[1].variants![1].rule as Record<string, unknown>;
174
- const result = evaluateMatcher(rule, ctxWithHeader(`${PAGE_ID}@sections.1.variants.1.rule=1`));
175
- expect(result).toBe(true);
176
- expect(matcherFn).not.toHaveBeenCalled();
177
- });
178
-
179
- it("evaluates normally when the path key targets a different position", () => {
180
- const page = setPageBlocks();
181
- const rule = page.sections[1].variants![0].rule as Record<string, unknown>;
182
- const result = evaluateMatcher(rule, ctxWithHeader(`${PAGE_ID}@sections.1.variants.1.rule=0`));
183
- expect(result).toBe(true);
184
- expect(matcherFn).toHaveBeenCalledTimes(1);
185
- });
186
-
187
- it("rebuilds the index when blocks are swapped", () => {
188
- const first = setPageBlocks();
189
- const overrideCtx = () => ctxWithHeader(`${PAGE_ID}@sections.1.variants.0.rule=0`);
190
- expect(
191
- evaluateMatcher(
192
- first.sections[1].variants![0].rule as Record<string, unknown>,
193
- overrideCtx(),
194
- ),
195
- ).toBe(false);
196
-
197
- const second = setPageBlocks();
198
- expect(
199
- evaluateMatcher(
200
- second.sections[1].variants![0].rule as Record<string, unknown>,
201
- overrideCtx(),
202
- ),
203
- ).toBe(false);
204
- });
205
- });
@@ -1,61 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { buildHydrationContext } from "./hydrationContext";
3
-
4
- function makeRequest(headers: Record<string, string> = {}): Request {
5
- return new Request("https://store.com/", { headers });
6
- }
7
-
8
- describe("buildHydrationContext", () => {
9
- it("extracts locale and timezone from cookies", () => {
10
- const req = makeRequest({ cookie: "locale=pt-BR; tz=America/Sao_Paulo" });
11
- const ctx = buildHydrationContext(req);
12
- expect(ctx.locale).toBe("pt-BR");
13
- expect(ctx.timeZone).toBe("America/Sao_Paulo");
14
- });
15
-
16
- it("falls back to Accept-Language when no locale cookie", () => {
17
- const req = makeRequest({ "accept-language": "pt-BR,pt;q=0.9,en;q=0.8" });
18
- const ctx = buildHydrationContext(req);
19
- expect(ctx.locale).toBe("pt-BR");
20
- });
21
-
22
- it("defaults to en-US and UTC when no headers or cookies", () => {
23
- const req = makeRequest();
24
- const ctx = buildHydrationContext(req);
25
- expect(ctx.locale).toBe("en-US");
26
- expect(ctx.timeZone).toBe("UTC");
27
- });
28
-
29
- it("extracts country from cf-ipcountry header", () => {
30
- const req = makeRequest({ "cf-ipcountry": "BR" });
31
- const ctx = buildHydrationContext(req);
32
- expect(ctx.country).toBe("BR");
33
- });
34
-
35
- it("extracts country from cookie when no cf header", () => {
36
- const req = makeRequest({ cookie: "country=US" });
37
- const ctx = buildHydrationContext(req);
38
- expect(ctx.country).toBe("US");
39
- });
40
-
41
- it("country is undefined when not available", () => {
42
- const req = makeRequest();
43
- const ctx = buildHydrationContext(req);
44
- expect(ctx.country).toBeUndefined();
45
- });
46
-
47
- it("cf-ipcountry takes precedence over cookie", () => {
48
- const req = makeRequest({
49
- "cf-ipcountry": "BR",
50
- cookie: "country=US",
51
- });
52
- const ctx = buildHydrationContext(req);
53
- expect(ctx.country).toBe("BR");
54
- });
55
-
56
- it("handles cookies with = in values", () => {
57
- const req = makeRequest({ cookie: "locale=en-US; token=abc=def=ghi" });
58
- const ctx = buildHydrationContext(req);
59
- expect(ctx.locale).toBe("en-US");
60
- });
61
- });