@decocms/blocks 7.71.6 → 8.0.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.
- package/bin/deco.js +13 -0
- package/package.json +32 -3
- package/src/cms/schema.ts +1 -1
- package/src/hooks/LiveControls.tsx +2 -0
- package/src/hooks/useLoadMore.ts +2 -0
- package/src/index.ts +37 -0
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +152 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/deferredTrigger.test.ts +0 -32
- package/src/cms/draftSource.test.ts +0 -519
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loader.test.ts +0 -557
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/renderJson.test.ts +0 -167
- package/src/cms/resolve.test.ts +0 -1492
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaders.test.ts +0 -625
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/flags.test.ts +0 -148
- package/src/hooks/Image.test.ts +0 -239
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/hooks/Stats.test.ts +0 -112
- package/src/hooks/useLoadMore.test.ts +0 -205
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/override.test.ts +0 -205
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/observability.test.ts +0 -335
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/preview/DraftPreviewBadge.render.test.tsx +0 -67
- package/src/preview/DraftPreviewBadge.test.ts +0 -55
- package/src/sdk/abTesting.test.ts +0 -381
- package/src/sdk/cacheHeaders.test.ts +0 -258
- package/src/sdk/cacheStorage.test.ts +0 -242
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoaderMaxBytes.test.ts +0 -23
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/cachedLoaderSizeMetric.test.ts +0 -93
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/crypto.test.ts +0 -65
- package/src/sdk/deepOmit.test.ts +0 -45
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/experiments.test.ts +0 -328
- package/src/sdk/fetchCache.test.ts +0 -164
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/otel.test.ts +0 -691
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpMeter.test.ts +0 -395
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/replaceStream.test.ts +0 -117
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/splitExactRedirects.test.ts +0 -154
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/urlUtils.test.ts +0 -34
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/tsconfig.json +0 -7
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
loadRedirects,
|
|
4
|
-
matchExactRedirect,
|
|
5
|
-
matchPatternRedirect,
|
|
6
|
-
matchRedirect,
|
|
7
|
-
splitExactRedirects,
|
|
8
|
-
} from "./redirects";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Exact rules move to their own KV keys; globs stay in the decofile because
|
|
12
|
-
* they must be scanned in order and can never be a key lookup.
|
|
13
|
-
*
|
|
14
|
-
* The invariant that matters: whatever leaves the decofile must be recoverable
|
|
15
|
-
* from the extracted list, and whatever stays must still match the same way.
|
|
16
|
-
*/
|
|
17
|
-
describe("splitExactRedirects", () => {
|
|
18
|
-
it("extracts exact rules and drops them from the block", () => {
|
|
19
|
-
const { blocks, exact } = splitExactRedirects({
|
|
20
|
-
r: {
|
|
21
|
-
__resolveType: "website/loaders/redirects.ts",
|
|
22
|
-
redirects: [
|
|
23
|
-
{ from: "/old", to: "/new", type: "permanent" },
|
|
24
|
-
{ from: "/tmp", to: "/other" },
|
|
25
|
-
],
|
|
26
|
-
},
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
expect(exact).toEqual([
|
|
30
|
-
{ path: "/old", to: "/new", status: 301 },
|
|
31
|
-
{ path: "/tmp", to: "/other", status: 302 },
|
|
32
|
-
]);
|
|
33
|
-
// Block had nothing but exact rules ⇒ gone entirely, not an empty husk.
|
|
34
|
-
expect(blocks.r).toBeUndefined();
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("keeps a path too long for a KV key in the decofile, still matched in memory", () => {
|
|
38
|
-
const long = `/${"x".repeat(500)}`;
|
|
39
|
-
const { blocks, exact } = splitExactRedirects({
|
|
40
|
-
r: {
|
|
41
|
-
__resolveType: "website/loaders/redirects.ts",
|
|
42
|
-
redirects: [{ from: long, to: "/new", type: "permanent" }],
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
expect(exact).toEqual([]);
|
|
47
|
-
expect(matchExactRedirect(long, loadRedirects(blocks))?.to).toBe("/new");
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("keeps glob rules in the decofile", () => {
|
|
51
|
-
const { blocks, exact } = splitExactRedirects({
|
|
52
|
-
r: {
|
|
53
|
-
__resolveType: "website/loaders/redirects.ts",
|
|
54
|
-
redirects: [
|
|
55
|
-
{ from: "/old/*", to: "/new/*" },
|
|
56
|
-
{ from: "/exact", to: "/x" },
|
|
57
|
-
],
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
expect(exact.map((e) => e.path)).toEqual(["/exact"]);
|
|
62
|
-
expect((blocks.r as { redirects: unknown[] }).redirects).toEqual([
|
|
63
|
-
{ from: "/old/*", to: "/new/*" },
|
|
64
|
-
]);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("normalizes the path to the KV key contract", () => {
|
|
68
|
-
const { exact } = splitExactRedirects({
|
|
69
|
-
r: {
|
|
70
|
-
__resolveType: "website/loaders/redirect.ts",
|
|
71
|
-
redirect: { from: "https://site.com/Old/", to: "/new" },
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
// Origin stripped, trailing slash dropped, lower-cased — whatever the
|
|
75
|
-
// writer keys by, the request-time lookup must produce byte for byte.
|
|
76
|
-
expect(exact).toEqual([{ path: "/old", to: "/new", status: 302 }]);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("drops the singular `redirect` field when rebuilding a block", () => {
|
|
80
|
-
// Otherwise the extracted rule would be silently reintroduced.
|
|
81
|
-
const { blocks } = splitExactRedirects({
|
|
82
|
-
r: {
|
|
83
|
-
__resolveType: "website/loaders/redirects.ts",
|
|
84
|
-
redirect: { from: "/a", to: "/b" },
|
|
85
|
-
redirects: [{ from: "/glob/*", to: "/g" }],
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
expect(blocks.r).not.toHaveProperty("redirect");
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("leaves non-redirect blocks untouched, by identity", () => {
|
|
92
|
-
const page = { __resolveType: "website/pages/Page.tsx", sections: [] };
|
|
93
|
-
const { blocks } = splitExactRedirects({ page });
|
|
94
|
-
expect(blocks.page).toBe(page);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("dedupes by path, last rule winning — same as loadRedirects", () => {
|
|
98
|
-
const input = {
|
|
99
|
-
a: { __resolveType: "website/loaders/redirects.ts", redirects: [{ from: "/x", to: "/1" }] },
|
|
100
|
-
b: { __resolveType: "website/loaders/redirects.ts", redirects: [{ from: "/x", to: "/2" }] },
|
|
101
|
-
};
|
|
102
|
-
expect(splitExactRedirects(input).exact).toEqual([{ path: "/x", to: "/2", status: 302 }]);
|
|
103
|
-
expect(loadRedirects(input).exact.get("/x")?.to).toBe("/2");
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("round-trips: nothing matchable is lost", () => {
|
|
107
|
-
const input = {
|
|
108
|
-
r: {
|
|
109
|
-
__resolveType: "website/loaders/redirects.ts",
|
|
110
|
-
redirects: [
|
|
111
|
-
{ from: "/a", to: "/1", type: "permanent" },
|
|
112
|
-
{ from: "/b/*", to: "/2/*" },
|
|
113
|
-
],
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
const before = loadRedirects(input);
|
|
117
|
-
const { blocks, exact } = splitExactRedirects(input);
|
|
118
|
-
const after = loadRedirects(blocks);
|
|
119
|
-
|
|
120
|
-
// Exact left the decofile...
|
|
121
|
-
expect(matchRedirect("/a", after)).toBeNull();
|
|
122
|
-
// ...into the extracted list, unchanged.
|
|
123
|
-
expect(exact).toContainEqual({ path: "/a", to: "/1", status: 301 });
|
|
124
|
-
// Globs still match exactly as before.
|
|
125
|
-
expect(matchRedirect("/b/deep", after)).toEqual(matchRedirect("/b/deep", before));
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
describe("matchExactRedirect / matchPatternRedirect", () => {
|
|
130
|
-
const map = loadRedirects({
|
|
131
|
-
r: {
|
|
132
|
-
__resolveType: "website/loaders/redirects.ts",
|
|
133
|
-
redirects: [
|
|
134
|
-
{ from: "/shop/*", to: "/store/*" },
|
|
135
|
-
{ from: "/shop/sale", to: "/promo", type: "permanent" },
|
|
136
|
-
],
|
|
137
|
-
},
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it("compose back into matchRedirect unchanged", () => {
|
|
141
|
-
for (const path of ["/shop/sale", "/shop/deep", "/nothing"]) {
|
|
142
|
-
expect(matchExactRedirect(path, map) ?? matchPatternRedirect(path, map)).toEqual(
|
|
143
|
-
matchRedirect(path, map),
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it("splits the two halves so KV can be interleaved between them", () => {
|
|
149
|
-
// The whole reason for the split: with exact rules in KV, "matchRedirect
|
|
150
|
-
// then KV" would let /shop/* win over the exact /shop/sale.
|
|
151
|
-
expect(matchExactRedirect("/shop/sale", map)?.to).toBe("/promo");
|
|
152
|
-
expect(matchPatternRedirect("/shop/sale", map)?.to).toBe("/store/sale");
|
|
153
|
-
});
|
|
154
|
-
});
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { redactUrl } from "./urlRedaction";
|
|
3
|
-
|
|
4
|
-
describe("redactUrl", () => {
|
|
5
|
-
it("redacts every query value by default", () => {
|
|
6
|
-
expect(redactUrl("https://api.test/path?token=abc&page=2")).toBe(
|
|
7
|
-
"https://api.test/path?token=REDACTED&page=REDACTED",
|
|
8
|
-
);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it("preserves query keys listed in keepQueryKeys", () => {
|
|
12
|
-
expect(
|
|
13
|
-
redactUrl("https://api.test/path?token=abc&page=2&sort=name", {
|
|
14
|
-
keepQueryKeys: ["page", "sort"],
|
|
15
|
-
}),
|
|
16
|
-
).toBe("https://api.test/path?token=REDACTED&page=2&sort=name");
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it("preserves empty query values verbatim", () => {
|
|
20
|
-
expect(redactUrl("https://api.test/path?page=&token=abc")).toBe(
|
|
21
|
-
"https://api.test/path?page=&token=REDACTED",
|
|
22
|
-
);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("strips userinfo from authority component", () => {
|
|
26
|
-
expect(redactUrl("https://user:secret@api.test/path?x=1")).toBe(
|
|
27
|
-
"https://api.test/path?x=REDACTED",
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("drops the fragment", () => {
|
|
32
|
-
expect(redactUrl("https://api.test/path?x=1#client-only-state-token=abc")).toBe(
|
|
33
|
-
"https://api.test/path?x=REDACTED",
|
|
34
|
-
);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("preserves host and path verbatim", () => {
|
|
38
|
-
expect(redactUrl("https://api.test/products/12345/details?x=1")).toBe(
|
|
39
|
-
"https://api.test/products/12345/details?x=REDACTED",
|
|
40
|
-
);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("falls back to substring-before-? on unparseable URLs", () => {
|
|
44
|
-
// Missing scheme — URL constructor throws.
|
|
45
|
-
expect(redactUrl("not-a-url?token=abc")).toBe("not-a-url");
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("falls back to substring-before-# on unparseable URLs (no `?`)", () => {
|
|
49
|
-
// Fragments can also carry secrets (`#access_token=…` in OAuth implicit
|
|
50
|
-
// grant). The defensive path must drop them too.
|
|
51
|
-
expect(redactUrl("not-a-url#access_token=abc")).toBe("not-a-url");
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("falls back to whichever of `?` / `#` appears first on unparseable URLs", () => {
|
|
55
|
-
expect(redactUrl("not-a-url?x=1#y=2")).toBe("not-a-url");
|
|
56
|
-
expect(redactUrl("not-a-url#frag?then=secret")).toBe("not-a-url");
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("returns the raw value when there is no query and the URL is unparseable", () => {
|
|
60
|
-
expect(redactUrl("relative/path")).toBe("relative/path");
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("handles URLs without query string unchanged", () => {
|
|
64
|
-
expect(redactUrl("https://api.test/products")).toBe("https://api.test/products");
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("redacts multi-value query parameters (e.g. ?fq=a&fq=b)", () => {
|
|
68
|
-
// URLSearchParams collapses repeated keys to a single value when .set() is
|
|
69
|
-
// called — the OTel guideline accepts this as acceptable cardinality loss.
|
|
70
|
-
const out = redactUrl("https://api.test/path?fq=a&fq=b");
|
|
71
|
-
expect(out).toBe("https://api.test/path?fq=REDACTED");
|
|
72
|
-
});
|
|
73
|
-
});
|
package/src/sdk/urlUtils.test.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { cleanPathForCacheKey, isTrackingParam } from "./urlUtils";
|
|
3
|
-
|
|
4
|
-
describe("tracking params that used to fragment the cache key", () => {
|
|
5
|
-
// Each of these was measured missing the edge cache on every route probed on a
|
|
6
|
-
// production storefront, because it was absent from UTM_PARAMS.
|
|
7
|
-
const cases = [
|
|
8
|
-
["GA4 campaign id", "utm_id"],
|
|
9
|
-
["GA4 source platform", "utm_source_platform"],
|
|
10
|
-
["Google Ads source", "gad_source"],
|
|
11
|
-
["Google Ads iOS click id", "gbraid"],
|
|
12
|
-
["Google Ads web-to-app click id", "wbraid"],
|
|
13
|
-
["GA cross-domain linker", "_gl"],
|
|
14
|
-
["Instagram", "igshid"],
|
|
15
|
-
["Pinterest", "epik"],
|
|
16
|
-
["Marketo", "mkt_tok"],
|
|
17
|
-
["Yandex", "yclid"],
|
|
18
|
-
] as const;
|
|
19
|
-
|
|
20
|
-
for (const [label, param] of cases) {
|
|
21
|
-
it(`strips ${label} (${param})`, () => {
|
|
22
|
-
expect(isTrackingParam(param)).toBe(true);
|
|
23
|
-
expect(cleanPathForCacheKey(`https://x.com/aneis?color=ouro&${param}=abc`)).toBe(
|
|
24
|
-
"/aneis?color=ouro",
|
|
25
|
-
);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
it("still keeps functional params", () => {
|
|
30
|
-
for (const p of ["page", "order", "map", "PS", "filter.color", "skuId", "q"]) {
|
|
31
|
-
expect(isTrackingParam(p)).toBe(false);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
});
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { createElement } from "react";
|
|
2
|
-
import { renderToStaticMarkup } from "react-dom/server";
|
|
3
|
-
import { describe, expect, it } from "vitest";
|
|
4
|
-
import { DeviceProvider, detectDevice, isMobileUA, useDevice } from "./useDevice";
|
|
5
|
-
|
|
6
|
-
describe("detectDevice", () => {
|
|
7
|
-
it("detects iPhone as mobile", () => {
|
|
8
|
-
expect(
|
|
9
|
-
detectDevice(
|
|
10
|
-
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
|
|
11
|
-
),
|
|
12
|
-
).toBe("mobile");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it("detects Android phone as mobile", () => {
|
|
16
|
-
expect(
|
|
17
|
-
detectDevice(
|
|
18
|
-
"Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36",
|
|
19
|
-
),
|
|
20
|
-
).toBe("mobile");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("detects iPad as tablet", () => {
|
|
24
|
-
expect(
|
|
25
|
-
detectDevice(
|
|
26
|
-
"Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
|
|
27
|
-
),
|
|
28
|
-
).toBe("tablet");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("detects Android tablet as tablet", () => {
|
|
32
|
-
expect(
|
|
33
|
-
detectDevice(
|
|
34
|
-
"Mozilla/5.0 (Linux; Android 14; SM-X200) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
35
|
-
),
|
|
36
|
-
).toBe("tablet");
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("detects Chrome desktop as desktop", () => {
|
|
40
|
-
expect(
|
|
41
|
-
detectDevice(
|
|
42
|
-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
43
|
-
),
|
|
44
|
-
).toBe("desktop");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("returns desktop for empty UA", () => {
|
|
48
|
-
expect(detectDevice("")).toBe("desktop");
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("detects Googlebot as desktop", () => {
|
|
52
|
-
expect(
|
|
53
|
-
detectDevice(
|
|
54
|
-
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
|
|
55
|
-
),
|
|
56
|
-
).toBe("desktop");
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("detects Googlebot mobile as mobile", () => {
|
|
60
|
-
expect(
|
|
61
|
-
detectDevice(
|
|
62
|
-
"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36 (compatible; Googlebot/2.1)",
|
|
63
|
-
),
|
|
64
|
-
).toBe("mobile");
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
describe("isMobileUA", () => {
|
|
69
|
-
it("returns true for mobile UAs", () => {
|
|
70
|
-
expect(isMobileUA("iPhone")).toBe(true);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("returns true for tablet UAs", () => {
|
|
74
|
-
expect(isMobileUA("iPad")).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("returns false for desktop UAs", () => {
|
|
78
|
-
expect(
|
|
79
|
-
isMobileUA(
|
|
80
|
-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
|
81
|
-
),
|
|
82
|
-
).toBe(false);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe("useDevice (isomorphic)", () => {
|
|
87
|
-
it("uses navigator.userAgent on client, not viewport width", () => {
|
|
88
|
-
// In jsdom, document exists → useDevice takes the client path.
|
|
89
|
-
// It should use navigator.userAgent (same mechanism as server-side)
|
|
90
|
-
// to ensure consistent values between SSR and hydration.
|
|
91
|
-
// jsdom's navigator.userAgent is a desktop-like string.
|
|
92
|
-
const device = useDevice();
|
|
93
|
-
const expected = detectDevice(navigator.userAgent);
|
|
94
|
-
expect(device).toBe(expected);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("returns consistent result with detectDevice(navigator.userAgent)", () => {
|
|
98
|
-
// This is the key SSR/hydration consistency check:
|
|
99
|
-
// server calls detectDevice(req.headers["user-agent"])
|
|
100
|
-
// client calls detectDevice(navigator.userAgent)
|
|
101
|
-
// Both use the same detectDevice() function → same result for same UA.
|
|
102
|
-
const clientResult = useDevice();
|
|
103
|
-
const directResult = detectDevice(navigator.userAgent);
|
|
104
|
-
expect(clientResult).toBe(directResult);
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
describe("DeviceProvider seeds useDevice() with the serialized value (#278)", () => {
|
|
109
|
-
function Probe() {
|
|
110
|
-
// Inside a render, useDevice() reads DeviceContext — this is the path that
|
|
111
|
-
// makes hydration stable when the value comes from the page loader.
|
|
112
|
-
return createElement("span", null, useDevice());
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
it("useDevice() returns the provider value, overriding runtime detection", () => {
|
|
116
|
-
// jsdom's navigator is desktop-like, so a "tablet" result can only come
|
|
117
|
-
// from the provider value (the server-resolved device), not runtime.
|
|
118
|
-
const html = renderToStaticMarkup(
|
|
119
|
-
createElement(DeviceProvider, { value: "tablet", children: createElement(Probe) }),
|
|
120
|
-
);
|
|
121
|
-
expect(html).toBe("<span>tablet</span>");
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("falls back to runtime resolution when no value is provided", () => {
|
|
125
|
-
const html = renderToStaticMarkup(
|
|
126
|
-
createElement(DeviceProvider, { children: createElement(Probe) }),
|
|
127
|
-
);
|
|
128
|
-
expect(html).toBe(`<span>${detectDevice(navigator.userAgent)}</span>`);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
HTMX_LEGACY_URL,
|
|
4
|
-
inlineScript,
|
|
5
|
-
usePartialSection,
|
|
6
|
-
useScript,
|
|
7
|
-
useSection,
|
|
8
|
-
} from "./useScript";
|
|
9
|
-
|
|
10
|
-
describe("inlineScript", () => {
|
|
11
|
-
it("returns dangerouslySetInnerHTML with the provided string", () => {
|
|
12
|
-
const result = inlineScript('alert("hello")');
|
|
13
|
-
expect(result).toEqual({
|
|
14
|
-
dangerouslySetInnerHTML: { __html: 'alert("hello")' },
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("handles empty string", () => {
|
|
19
|
-
const result = inlineScript("");
|
|
20
|
-
expect(result).toEqual({
|
|
21
|
-
dangerouslySetInnerHTML: { __html: "" },
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("handles multiline scripts", () => {
|
|
26
|
-
const js = `
|
|
27
|
-
const el = document.getElementById("btn");
|
|
28
|
-
el.addEventListener("click", () => { console.log("clicked"); });
|
|
29
|
-
`;
|
|
30
|
-
const result = inlineScript(js);
|
|
31
|
-
expect(result.dangerouslySetInnerHTML.__html).toBe(js);
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe("useScript", () => {
|
|
36
|
-
it("serializes a function into an IIFE string", () => {
|
|
37
|
-
function greet(name: string) {
|
|
38
|
-
console.log(name);
|
|
39
|
-
}
|
|
40
|
-
const result = useScript(greet, "world");
|
|
41
|
-
expect(result).toContain("(");
|
|
42
|
-
expect(result).toContain('"world"');
|
|
43
|
-
expect(result).toContain("console.log");
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("serializes multiple arguments", () => {
|
|
47
|
-
function add(a: number, b: number) {
|
|
48
|
-
return a + b;
|
|
49
|
-
}
|
|
50
|
-
const result = useScript(add, 1, 2);
|
|
51
|
-
expect(result).toContain("1,2");
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("handles functions with no arguments", () => {
|
|
55
|
-
function noop() {}
|
|
56
|
-
const result = useScript(noop);
|
|
57
|
-
expect(result).toMatch(/^\(.*\)\(\)$/);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
// Legacy HTMX stubs — useSection / usePartialSection
|
|
63
|
-
// ---------------------------------------------------------------------------
|
|
64
|
-
|
|
65
|
-
describe("useSection / usePartialSection (legacy HTMX stubs)", () => {
|
|
66
|
-
let warnSpy: ReturnType<typeof vi.spyOn>;
|
|
67
|
-
let prevNodeEnv: string | undefined;
|
|
68
|
-
|
|
69
|
-
beforeEach(() => {
|
|
70
|
-
// Reset the dedup set so each test sees a fresh warning fire.
|
|
71
|
-
delete (globalThis as any).__DECO_LEGACY_HTMX_WARNED;
|
|
72
|
-
prevNodeEnv = process.env.NODE_ENV;
|
|
73
|
-
process.env.NODE_ENV = "development";
|
|
74
|
-
warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
afterEach(() => {
|
|
78
|
-
warnSpy.mockRestore();
|
|
79
|
-
if (prevNodeEnv === undefined) delete process.env.NODE_ENV;
|
|
80
|
-
else process.env.NODE_ENV = prevNodeEnv;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it("useSection returns a stable placeholder URL instead of throwing", () => {
|
|
84
|
-
expect(useSection()).toBe(HTMX_LEGACY_URL);
|
|
85
|
-
expect(useSection({ props: { x: 1 } })).toBe(HTMX_LEGACY_URL);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("usePartialSection returns the same placeholder URL", () => {
|
|
89
|
-
expect(usePartialSection()).toBe(HTMX_LEGACY_URL);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it("warns on first call (in development)", () => {
|
|
93
|
-
useSection();
|
|
94
|
-
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
95
|
-
expect(warnSpy.mock.calls[0][0]).toMatch(/useSection/);
|
|
96
|
-
expect(warnSpy.mock.calls[0][0]).toMatch(/were removed/);
|
|
97
|
-
expect(warnSpy.mock.calls[0][0]).toMatch(/htmx-residue/);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("dedups warnings: a second call to the same stub is silent", () => {
|
|
101
|
-
useSection();
|
|
102
|
-
useSection();
|
|
103
|
-
useSection();
|
|
104
|
-
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it("tracks warnings per-stub: useSection and usePartialSection warn independently", () => {
|
|
108
|
-
useSection();
|
|
109
|
-
usePartialSection();
|
|
110
|
-
expect(warnSpy).toHaveBeenCalledTimes(2);
|
|
111
|
-
const messages = warnSpy.mock.calls.map((c) => c[0] as string);
|
|
112
|
-
expect(messages.some((m) => m.includes("useSection"))).toBe(true);
|
|
113
|
-
expect(messages.some((m) => m.includes("usePartialSection"))).toBe(true);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it("does NOT warn in production", () => {
|
|
117
|
-
process.env.NODE_ENV = "production";
|
|
118
|
-
useSection();
|
|
119
|
-
usePartialSection();
|
|
120
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("returns the placeholder URL even in production (still safe to embed)", () => {
|
|
124
|
-
process.env.NODE_ENV = "production";
|
|
125
|
-
expect(useSection()).toBe(HTMX_LEGACY_URL);
|
|
126
|
-
expect(usePartialSection()).toBe(HTMX_LEGACY_URL);
|
|
127
|
-
});
|
|
128
|
-
});
|