@decocms/blocks 7.77.10 → 7.77.12

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.
@@ -0,0 +1,328 @@
1
+ /**
2
+ * Redirect semantics, pinned to the Fresh loaders and handlers this module
3
+ * replaced (deco-cx/apps `website/loaders/redirectsFromCsv.ts`,
4
+ * `website/handlers/router.ts`, `website/handlers/redirect.ts`).
5
+ *
6
+ * The rows are real lines of a storefront's `redirects.csv` (5 835 rows), and
7
+ * every case below is a URL where the TanStack port answered differently from
8
+ * the Fresh site it replaced — three of them as an infinite loop on a live
9
+ * product page.
10
+ *
11
+ * Everything here runs under `semantics: "fresh"`, the opt-in. The default
12
+ * (`"legacy"`, what sites already on `@decocms/*` answer) is pinned in
13
+ * `redirects.legacy.test.ts`.
14
+ */
15
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
16
+ import {
17
+ addRedirects,
18
+ loadRedirects,
19
+ matchRedirect,
20
+ parseRedirectsCsv,
21
+ redirectLocation,
22
+ selectExactRedirect,
23
+ setRedirectSemantics,
24
+ sourcePath,
25
+ splitExactRedirects,
26
+ } from "./redirects";
27
+
28
+ beforeEach(() => setRedirectSemantics("fresh"));
29
+ afterEach(() => setRedirectSemantics("legacy"));
30
+
31
+ const fromCsv = (csv: string, options?: Parameters<typeof parseRedirectsCsv>[1]) => {
32
+ const map = loadRedirects({});
33
+ addRedirects(map, parseRedirectsCsv(csv, options));
34
+ return map;
35
+ };
36
+
37
+ describe("source matching is byte for byte, as Fresh's router", () => {
38
+ it("does not fold case — a rule from /Calca-x/p to /calca-x/p is a redirect, not a loop", () => {
39
+ // Three live PDPs looped on this: lower-casing the source made the key
40
+ // equal to its own target.
41
+ const map = fromCsv(
42
+ "from,to,type\n/Calca-Linho-Santorini-Off-White-0077631037/p,/calca-linho-santorini-off-white-0077631037/p,PERMANENT\n",
43
+ );
44
+
45
+ expect(matchRedirect("/calca-linho-santorini-off-white-0077631037/p", map)).toBeNull();
46
+ expect(matchRedirect("/Calca-Linho-Santorini-Off-White-0077631037/p", map)).toMatchObject({
47
+ to: "/calca-linho-santorini-off-white-0077631037/p",
48
+ status: 307,
49
+ });
50
+ });
51
+
52
+ it("keeps rows that differ only by case or trailing slash as distinct rules", () => {
53
+ // Folding collapsed these onto one key, last row winning: the first URL
54
+ // was sent to the second row's destination.
55
+ const map = fromCsv(
56
+ [
57
+ "from,to,type",
58
+ "/meia-pima-cano-alto-casual-poa-preta-0075365040/p,/meia-canelada-pima-cinza-0102768020/p,permanent",
59
+ "/meia-pima-cano-alto-casual-poa-preta-0075365040/p/,/meia-social-modal-marrom-0058971035/p,permanent",
60
+ "/Camiseta-Basica-0001/p,/camisetas/basica,permanent",
61
+ "/camiseta-basica-0001/p,/camisetas/outra,permanent",
62
+ ].join("\n"),
63
+ );
64
+
65
+ expect(matchRedirect("/meia-pima-cano-alto-casual-poa-preta-0075365040/p", map)?.to).toBe(
66
+ "/meia-canelada-pima-cinza-0102768020/p",
67
+ );
68
+ expect(matchRedirect("/meia-pima-cano-alto-casual-poa-preta-0075365040/p/", map)?.to).toBe(
69
+ "/meia-social-modal-marrom-0058971035/p",
70
+ );
71
+ expect(matchRedirect("/Camiseta-Basica-0001/p", map)?.to).toBe("/camisetas/basica");
72
+ expect(matchRedirect("/camiseta-basica-0001/p", map)?.to).toBe("/camisetas/outra");
73
+ });
74
+
75
+ it("matches the percent-encoded pathname the request carries", () => {
76
+ // `URL.pathname` is percent-encoded, so a source written raw with non-ASCII
77
+ // never matches (same in Fresh); one written encoded does.
78
+ const map = fromCsv("from,to\n/calcas/paletos/maleável,/linha-maleavel\n/caf%C3%A9,/cafe\n");
79
+ expect(matchRedirect("/calcas/paletos/male%C3%A1vel", map)).toBeNull();
80
+ expect(matchRedirect("/caf%C3%A9", map)?.to).toBe("/cafe");
81
+ });
82
+
83
+ it("never matches a source without a leading slash", () => {
84
+ const map = fromCsv("from,to\ncamisa-roma-linho-verde-0060535048,/roupas/camisas\n");
85
+ expect(matchRedirect("/camisa-roma-linho-verde-0060535048", map)).toBeNull();
86
+ });
87
+
88
+ it("strips the origin of an absolute source and keeps the rest as written", () => {
89
+ expect(sourcePath("https://www.example.com/Old/")).toBe("/Old/");
90
+ expect(sourcePath("https://www.example.com/x?map=c")).toBe("/x?map=c");
91
+ expect(sourcePath("/Keep-Case/")).toBe("/Keep-Case/");
92
+ const map = fromCsv("from,to\nhttps://www.example.com/Old/,/new\n");
93
+ expect(matchRedirect("/Old/", map)?.to).toBe("/new");
94
+ expect(matchRedirect("/old", map)).toBeNull();
95
+ });
96
+
97
+ it("scans glob patterns case-sensitively too", () => {
98
+ const map = fromCsv("from,to\n/Blog/*,/news/*\n");
99
+ expect(matchRedirect("/Blog/post-1", map)?.to).toBe("/news/post-1");
100
+ expect(matchRedirect("/blog/post-1", map)).toBeNull();
101
+ });
102
+ });
103
+
104
+ describe("query-scoped sources", () => {
105
+ // Built per test: a map keys its rules for the semantics in force when built.
106
+ let map: ReturnType<typeof fromCsv>;
107
+ beforeEach(() => {
108
+ map = fromCsv(
109
+ [
110
+ "from,to,type",
111
+ "/kits-3-camisas-kit0000008010/p,/camisa-paris-branca-0100441014/p,PERMANENT",
112
+ "/kits-3-camisas-kit0000008010/p?skuId=4432,/roupas/kits,PERMANENT",
113
+ "/garrafa-kouda-preta-0059962040/p?listName=Kouda,/roupas/acessorios,PERMANENT",
114
+ ].join("\n"),
115
+ );
116
+ });
117
+
118
+ it("matches pathname + search before the bare pathname", () => {
119
+ expect(matchRedirect("/kits-3-camisas-kit0000008010/p", map, "?skuId=4432")?.to).toBe(
120
+ "/roupas/kits",
121
+ );
122
+ expect(matchRedirect("/kits-3-camisas-kit0000008010/p", map)?.to).toBe(
123
+ "/camisa-paris-branca-0100441014/p",
124
+ );
125
+ expect(matchRedirect("/kits-3-camisas-kit0000008010/p", map, "?skuId=1")?.to).toBe(
126
+ "/camisa-paris-branca-0100441014/p",
127
+ );
128
+ });
129
+
130
+ it("is unreachable from the bare pathname", () => {
131
+ expect(matchRedirect("/garrafa-kouda-preta-0059962040/p", map)).toBeNull();
132
+ expect(matchRedirect("/garrafa-kouda-preta-0059962040/p", map, "?listName=Kouda")?.to).toBe(
133
+ "/roupas/acessorios",
134
+ );
135
+ });
136
+ });
137
+
138
+ describe("status", () => {
139
+ const statusOf = (type: string) => parseRedirectsCsv(`from,to,type\n/a,/b,${type}\n`)[0].status;
140
+
141
+ it("is 301 for `permanent` and 307 for everything else, compared exactly as Fresh does", () => {
142
+ expect(statusOf("permanent")).toBe(301);
143
+ expect(statusOf("301")).toBe(301);
144
+ // 82% of the real file spells it this way, and the Fresh site served 307.
145
+ expect(statusOf("PERMANENT")).toBe(307);
146
+ expect(statusOf("temporary")).toBe(307);
147
+ expect(statusOf("")).toBe(307);
148
+ expect(parseRedirectsCsv("from,to\n/a,/b\n")[0].status).toBe(307);
149
+ });
150
+
151
+ it("is 307 for a temporary CMS block too", () => {
152
+ const map = loadRedirects({
153
+ r: {
154
+ __resolveType: "website/loaders/redirect.ts",
155
+ redirect: { from: "/promo", to: "/sale", type: "temporary" },
156
+ },
157
+ });
158
+ expect(matchRedirect("/promo", map)?.status).toBe(307);
159
+ });
160
+
161
+ it("defaults rows without a type to 301 under forcePermanentRedirects", () => {
162
+ const map = fromCsv("from,to\n/a,/b\n/c,/d,temporary\n", { forcePermanentRedirects: true });
163
+ expect(matchRedirect("/a", map)?.status).toBe(301);
164
+ expect(matchRedirect("/c", map)?.status).toBe(307);
165
+ });
166
+ });
167
+
168
+ describe("CSV columns", () => {
169
+ it("reads type and discardQueryParameters by value, in any column", () => {
170
+ const rows = parseRedirectsCsv(
171
+ "from,to,type\n/a,/b,true,permanent\n/c,/d,permanent,false\n/e,/f,true\n",
172
+ );
173
+ expect(rows).toEqual([
174
+ { from: "/a", to: "/b", status: 301, discardQueryParameters: true },
175
+ { from: "/c", to: "/d", status: 301 },
176
+ { from: "/e", to: "/f", status: 307, discardQueryParameters: true },
177
+ ]);
178
+ });
179
+
180
+ it("splits on a semicolon outside quotes and on every comma, like Fresh", () => {
181
+ expect(parseRedirectsCsv("from;to;type\n/a;/b;permanent\n")).toEqual([
182
+ { from: "/a", to: "/b", status: 301 },
183
+ ]);
184
+ expect(parseRedirectsCsv('from,to\n"/a;x",/b\n')).toEqual([
185
+ { from: '"/a;x"', to: "/b", status: 307 },
186
+ ]);
187
+ });
188
+
189
+ it("accepts CRLF line endings", () => {
190
+ expect(parseRedirectsCsv("from,to,type\r\n/a,/b,permanent\r\n")).toEqual([
191
+ { from: "/a", to: "/b", status: 301 },
192
+ ]);
193
+ });
194
+
195
+ it("carries discardQueryParameters from a CMS block entry", () => {
196
+ const map = loadRedirects({
197
+ r: {
198
+ __resolveType: "website/loaders/redirects.ts",
199
+ redirects: [{ from: "/a", to: "/b", type: "permanent", discardQueryParameters: true }],
200
+ },
201
+ });
202
+ expect(matchRedirect("/a", map)).toEqual({
203
+ from: "/a",
204
+ to: "/b",
205
+ status: 301,
206
+ discardQueryParameters: true,
207
+ });
208
+ });
209
+ });
210
+
211
+ describe("self-redirects are dropped at load time", () => {
212
+ it("drops a row that points at itself", () => {
213
+ expect(parseRedirectsCsv("from,to\n/a,/a\n")).toEqual([]);
214
+ });
215
+
216
+ it("drops an absolute source that collapses onto its target", () => {
217
+ expect(parseRedirectsCsv("from,to\nhttp://blog.example.com/,/\n")).toEqual([]);
218
+ });
219
+
220
+ it("drops a self-redirect declared as a CMS block", () => {
221
+ const map = loadRedirects({
222
+ r: {
223
+ __resolveType: "website/loaders/redirect.ts",
224
+ redirect: { from: "/loop", to: "/loop", type: "permanent" },
225
+ },
226
+ });
227
+ expect(matchRedirect("/loop", map)).toBeNull();
228
+ });
229
+
230
+ it("keeps a redirect that only changes case or trailing slash — those are different URLs", () => {
231
+ expect(parseRedirectsCsv("from,to\n/A,/a\n/b/,/b\n")).toHaveLength(2);
232
+ });
233
+ });
234
+
235
+ describe("redirectLocation", () => {
236
+ it("appends the request's query string sorted by key, so campaign parameters survive", () => {
237
+ // What the Fresh site answered for `?utm_source=qa&gclid=x`.
238
+ expect(redirectLocation({ to: "/new" }, "?utm_source=qa&gclid=x")).toBe(
239
+ "/new?gclid=x&utm_source=qa",
240
+ );
241
+ expect(redirectLocation({ to: "/new?a=1" }, "?b=2")).toBe("/new?a=1&b=2");
242
+ // Pairs are kept as sent: no re-encoding, duplicates keep their order.
243
+ expect(redirectLocation({ to: "/n" }, "?q=a%20b&q=c&a")).toBe("/n?a&q=a%20b&q=c");
244
+ expect(redirectLocation({ to: "/new" }, "")).toBe("/new");
245
+ expect(redirectLocation({ to: "/new" })).toBe("/new");
246
+ });
247
+
248
+ it("honors discardQueryParameters", () => {
249
+ expect(redirectLocation({ to: "/new", discardQueryParameters: true }, "?utm_source=qa")).toBe(
250
+ "/new",
251
+ );
252
+ });
253
+
254
+ it("leaves an escape already in `to` alone instead of double-encoding it", () => {
255
+ // `encodeURI` turned this into `%2520`, and the CDN answered 403.
256
+ const to = "https://cdn.example.com/Regulamento%20Oficina%20Reserva%20e%20Livelo.pdf";
257
+ expect(redirectLocation({ to })).toBe(to);
258
+ });
259
+
260
+ it("percent-encodes what a header cannot carry", () => {
261
+ expect(redirectLocation({ to: "/promoção" })).toBe("/promo%C3%A7%C3%A3o");
262
+ expect(redirectLocation({ to: "/a b" })).toBe("/a%20b");
263
+ expect(redirectLocation({ to: "/emoji-😀" })).toBe("/emoji-%F0%9F%98%80");
264
+ });
265
+ });
266
+
267
+ describe("splitExactRedirects", () => {
268
+ it("keys KV by the folded path and carries the source as written", () => {
269
+ const { exact } = splitExactRedirects({
270
+ r: {
271
+ __resolveType: "website/loaders/redirects.ts",
272
+ redirects: [
273
+ { from: "https://site.com/Old/", to: "/new" },
274
+ { from: "/drop", to: "/kept", type: "permanent", discardQueryParameters: true },
275
+ ],
276
+ },
277
+ });
278
+ expect(exact).toEqual([
279
+ { path: "/old", from: "/Old/", to: "/new", status: 307 },
280
+ { path: "/drop", from: "/drop", to: "/kept", status: 301, discardQueryParameters: true },
281
+ ]);
282
+ expect(selectExactRedirect(exact[0], "/Old/")).toMatchObject({ to: "/new", status: 307 });
283
+ expect(selectExactRedirect(exact[0], "/old")).toBeNull();
284
+ });
285
+
286
+ it("keeps every source that folds to one key, and serves the one written byte for byte", () => {
287
+ const { exact } = splitExactRedirects({
288
+ r: {
289
+ __resolveType: "website/loaders/redirects.ts",
290
+ redirects: [
291
+ { from: "/meia/p", to: "/a", type: "permanent" },
292
+ { from: "/meia/p/", to: "/b", type: "permanent" },
293
+ { from: "/Meia/p", to: "/c", type: "permanent" },
294
+ ],
295
+ },
296
+ });
297
+ expect(exact).toHaveLength(1);
298
+ const [rule] = exact;
299
+ expect(rule.path).toBe("/meia/p");
300
+ expect(selectExactRedirect([...rule.shadowed!, rule], "/meia/p")?.to).toBe("/a");
301
+ expect(selectExactRedirect([...rule.shadowed!, rule], "/meia/p/")?.to).toBe("/b");
302
+ expect(selectExactRedirect([...rule.shadowed!, rule], "/Meia/p")?.to).toBe("/c");
303
+ expect(selectExactRedirect([...rule.shadowed!, rule], "/MEIA/p")).toBeNull();
304
+ });
305
+
306
+ it("keeps query-scoped rules in the decofile, where the in-memory lookup can see them", () => {
307
+ const input = {
308
+ r: {
309
+ __resolveType: "website/loaders/redirects.ts",
310
+ redirects: [
311
+ { from: "/x?map=c", to: "/scoped" },
312
+ { from: "/x", to: "/bare" },
313
+ ],
314
+ },
315
+ };
316
+ const { blocks, exact } = splitExactRedirects(input);
317
+ expect(exact).toEqual([{ path: "/x", from: "/x", to: "/bare", status: 307 }]);
318
+ expect(matchRedirect("/x", loadRedirects(blocks), "?map=c")?.to).toBe("/scoped");
319
+ expect(matchRedirect("/x", loadRedirects(blocks))).toBeNull();
320
+ });
321
+
322
+ it("drops a self-redirect instead of writing it to KV", () => {
323
+ const { exact } = splitExactRedirects({
324
+ r: { __resolveType: "website/loaders/redirects.ts", redirects: [{ from: "/a", to: "/a" }] },
325
+ });
326
+ expect(exact).toEqual([]);
327
+ });
328
+ });