@decocms/blocks 7.72.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 (148) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +32 -3
  3. package/src/cms/schema.ts +1 -1
  4. package/src/hooks/LiveControls.tsx +2 -0
  5. package/src/hooks/useLoadMore.ts +2 -0
  6. package/src/index.ts +37 -0
  7. package/src/protocol/assets.ts +124 -0
  8. package/src/protocol/canonical.ts +7 -0
  9. package/src/protocol/ciphertext.ts +6 -0
  10. package/src/protocol/client.ts +170 -0
  11. package/src/protocol/conformance/cases/assets.ts +81 -0
  12. package/src/protocol/conformance/cases/guards.ts +314 -0
  13. package/src/protocol/conformance/cases/reads.ts +168 -0
  14. package/src/protocol/conformance/cases/types.ts +9 -0
  15. package/src/protocol/conformance/cases/wire.ts +138 -0
  16. package/src/protocol/conformance/cases/writes.ts +345 -0
  17. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  18. package/src/protocol/conformance/context.ts +222 -0
  19. package/src/protocol/conformance/index.ts +140 -0
  20. package/src/protocol/errors.ts +142 -0
  21. package/src/protocol/index.ts +39 -0
  22. package/src/protocol/keys.ts +279 -0
  23. package/src/protocol/params.ts +67 -0
  24. package/src/protocol/secrets.ts +231 -0
  25. package/src/protocol/server/assets.ts +119 -0
  26. package/src/protocol/server/auth.ts +49 -0
  27. package/src/protocol/server/bodyCache.ts +84 -0
  28. package/src/protocol/server/content.ts +175 -0
  29. package/src/protocol/server/core.ts +112 -0
  30. package/src/protocol/server/handler.ts +102 -0
  31. package/src/protocol/server/http.ts +139 -0
  32. package/src/protocol/server/index.ts +11 -0
  33. package/src/protocol/server/methods/apply.ts +341 -0
  34. package/src/protocol/server/methods/read.ts +156 -0
  35. package/src/protocol/server/rpc.ts +136 -0
  36. package/src/protocol/storage/fs/hash.ts +21 -0
  37. package/src/protocol/storage/fs/index.ts +338 -0
  38. package/src/protocol/storage/fs/lock.ts +68 -0
  39. package/src/protocol/storage/fs/transaction.ts +183 -0
  40. package/src/protocol/storage/fs/unsupported.ts +15 -0
  41. package/src/protocol/storage/memory.ts +238 -0
  42. package/src/protocol/storage.ts +182 -0
  43. package/src/protocol/types.ts +172 -0
  44. package/src/v8/analytics.ts +50 -0
  45. package/src/v8/background.ts +107 -0
  46. package/src/v8/builtins/data.ts +23 -0
  47. package/src/v8/builtins/index.ts +26 -0
  48. package/src/v8/builtins/lazy.ts +23 -0
  49. package/src/v8/builtins/legacy.ts +65 -0
  50. package/src/v8/builtins/matchers.ts +31 -0
  51. package/src/v8/builtins/multivariate.ts +25 -0
  52. package/src/v8/builtins/secret.ts +82 -0
  53. package/src/v8/canonical.ts +117 -0
  54. package/src/v8/ciphertext.ts +158 -0
  55. package/src/v8/cli/builtins.ts +20 -0
  56. package/src/v8/cli/check/index.ts +774 -0
  57. package/src/v8/cli/check/messages.ts +168 -0
  58. package/src/v8/cli/content.ts +287 -0
  59. package/src/v8/cli/index.ts +17 -0
  60. package/src/v8/cli/log.ts +18 -0
  61. package/src/v8/cli/main.ts +12 -0
  62. package/src/v8/cli/root.ts +66 -0
  63. package/src/v8/cli/run.ts +236 -0
  64. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  65. package/src/v8/cli/schema/generate.ts +507 -0
  66. package/src/v8/cli/schema/index.ts +63 -0
  67. package/src/v8/cli/schema/tsProgram.ts +497 -0
  68. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  69. package/src/v8/cli/serve/server.ts +303 -0
  70. package/src/v8/cli/watch.ts +86 -0
  71. package/src/v8/client.ts +147 -0
  72. package/src/v8/cms.ts +237 -0
  73. package/src/v8/content.ts +195 -0
  74. package/src/v8/draft.ts +136 -0
  75. package/src/v8/errors.ts +59 -0
  76. package/src/v8/fetch.ts +152 -0
  77. package/src/v8/identity.ts +65 -0
  78. package/src/v8/index.ts +39 -0
  79. package/src/v8/json.ts +74 -0
  80. package/src/v8/matchRoute.ts +204 -0
  81. package/src/v8/remoteLoader.ts +282 -0
  82. package/src/v8/resolver.ts +278 -0
  83. package/src/v8/secrets.ts +17 -0
  84. package/src/v8/telemetry.ts +533 -0
  85. package/src/v8/types.ts +240 -0
  86. package/src/cms/blockSource.test.ts +0 -76
  87. package/src/cms/client.browserBundle.test.ts +0 -44
  88. package/src/cms/deferredTrigger.test.ts +0 -32
  89. package/src/cms/draftSource.test.ts +0 -519
  90. package/src/cms/layoutCacheRace.test.ts +0 -181
  91. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  92. package/src/cms/loader.test.ts +0 -557
  93. package/src/cms/registry.test.ts +0 -110
  94. package/src/cms/renderJson.test.ts +0 -167
  95. package/src/cms/resolve.test.ts +0 -1492
  96. package/src/cms/schema.test.ts +0 -356
  97. package/src/cms/sectionLoaderContext.test.ts +0 -111
  98. package/src/cms/sectionLoaders.test.ts +0 -625
  99. package/src/cms/sectionMixins.test.ts +0 -167
  100. package/src/cms/stickyFlags.test.ts +0 -117
  101. package/src/flags/flags.test.ts +0 -148
  102. package/src/hooks/Image.test.ts +0 -239
  103. package/src/hooks/RenderSection.test.tsx +0 -81
  104. package/src/hooks/Stats.test.ts +0 -112
  105. package/src/hooks/useLoadMore.test.ts +0 -205
  106. package/src/matchers/builtins.test.ts +0 -318
  107. package/src/matchers/override.test.ts +0 -205
  108. package/src/middleware/hydrationContext.test.ts +0 -61
  109. package/src/middleware/observability.test.ts +0 -335
  110. package/src/middleware/validateSection.test.ts +0 -147
  111. package/src/preview/DraftPreviewBadge.render.test.tsx +0 -67
  112. package/src/preview/DraftPreviewBadge.test.ts +0 -55
  113. package/src/sdk/abTesting.test.ts +0 -381
  114. package/src/sdk/cacheHeaders.test.ts +0 -258
  115. package/src/sdk/cacheStorage.test.ts +0 -242
  116. package/src/sdk/cachedLoader.test.ts +0 -122
  117. package/src/sdk/cachedLoaderMaxBytes.test.ts +0 -23
  118. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  119. package/src/sdk/cachedLoaderSizeMetric.test.ts +0 -93
  120. package/src/sdk/cn.test.ts +0 -34
  121. package/src/sdk/composite.test.ts +0 -121
  122. package/src/sdk/cookie.test.ts +0 -108
  123. package/src/sdk/crypto.test.ts +0 -65
  124. package/src/sdk/deepOmit.test.ts +0 -45
  125. package/src/sdk/encoding.test.ts +0 -71
  126. package/src/sdk/env.test.ts +0 -33
  127. package/src/sdk/experiments.test.ts +0 -328
  128. package/src/sdk/fetchCache.test.ts +0 -164
  129. package/src/sdk/flags.test.ts +0 -91
  130. package/src/sdk/http.test.ts +0 -71
  131. package/src/sdk/inflightTimeout.test.ts +0 -35
  132. package/src/sdk/instrumentedFetch.test.ts +0 -559
  133. package/src/sdk/invoke.test.ts +0 -115
  134. package/src/sdk/logger.test.ts +0 -432
  135. package/src/sdk/otel.test.ts +0 -691
  136. package/src/sdk/otelAdapters.test.ts +0 -89
  137. package/src/sdk/otelHttpLog.test.ts +0 -457
  138. package/src/sdk/otelHttpMeter.test.ts +0 -395
  139. package/src/sdk/otelHttpTracer.test.ts +0 -474
  140. package/src/sdk/replaceStream.test.ts +0 -117
  141. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  142. package/src/sdk/splitExactRedirects.test.ts +0 -154
  143. package/src/sdk/urlRedaction.test.ts +0 -73
  144. package/src/sdk/urlUtils.test.ts +0 -34
  145. package/src/sdk/useDevice.test.ts +0 -130
  146. package/src/sdk/useScript.test.ts +0 -128
  147. package/src/sdk/useSuggestions.test.ts +0 -230
  148. 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
- });