@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,230 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for the `createUseSuggestions` factory.
|
|
3
|
-
*
|
|
4
|
-
* The hook itself depends on React (useCallback). This file exercises
|
|
5
|
-
* the parts of the factory that don't need a React renderer:
|
|
6
|
-
* - factory shape + isolation between calls
|
|
7
|
-
* - the non-React `_internal.setQuery` flow which carries every bit
|
|
8
|
-
* of behaviour the React hook delegates to (queue, cancel guard,
|
|
9
|
-
* loading-flag invariants, error path)
|
|
10
|
-
*
|
|
11
|
-
* Hook-level integration is exercised by the site-level smoke (the
|
|
12
|
-
* factory has shipped to two production sites with the same shape).
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { describe, expect, it, vi } from "vitest";
|
|
16
|
-
import { createUseSuggestions } from "./useSuggestions";
|
|
17
|
-
|
|
18
|
-
interface FakeSuggestion {
|
|
19
|
-
products: string[];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const FAKE_LOADER = {
|
|
23
|
-
__resolveType: "site/loaders/search/suggestions.ts",
|
|
24
|
-
limit: 5,
|
|
25
|
-
} as unknown as FakeSuggestion;
|
|
26
|
-
|
|
27
|
-
function makeOkFetch(payload: unknown, delayMs = 0): typeof fetch {
|
|
28
|
-
return ((_input: RequestInfo | URL, _init?: RequestInit) =>
|
|
29
|
-
new Promise((resolve) => {
|
|
30
|
-
setTimeout(
|
|
31
|
-
() =>
|
|
32
|
-
resolve(
|
|
33
|
-
new Response(JSON.stringify(payload), {
|
|
34
|
-
status: 200,
|
|
35
|
-
headers: { "Content-Type": "application/json" },
|
|
36
|
-
}),
|
|
37
|
-
),
|
|
38
|
-
delayMs,
|
|
39
|
-
);
|
|
40
|
-
})) as typeof fetch;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
describe("createUseSuggestions — factory shape", () => {
|
|
44
|
-
it("returns useSuggestions + _internal", () => {
|
|
45
|
-
const f = createUseSuggestions<FakeSuggestion>();
|
|
46
|
-
expect(typeof f.useSuggestions).toBe("function");
|
|
47
|
-
expect(typeof f._internal.setQuery).toBe("function");
|
|
48
|
-
expect(typeof f._internal.drain).toBe("function");
|
|
49
|
-
expect(f._internal.payload.value).toBeNull();
|
|
50
|
-
expect(f._internal.loading.value).toBe(false);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("two factory calls produce independent state + functions", () => {
|
|
54
|
-
const a = createUseSuggestions<FakeSuggestion>();
|
|
55
|
-
const b = createUseSuggestions<FakeSuggestion>();
|
|
56
|
-
expect(a.useSuggestions).not.toBe(b.useSuggestions);
|
|
57
|
-
expect(a._internal.payload).not.toBe(b._internal.payload);
|
|
58
|
-
expect(a._internal.loading).not.toBe(b._internal.loading);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
describe("createUseSuggestions — fetch happy path", () => {
|
|
63
|
-
it("posts to /deco/invoke/<__resolveType> with the query + extra props", async () => {
|
|
64
|
-
const spy = vi.fn(makeOkFetch({ products: ["a", "b"] }));
|
|
65
|
-
const f = createUseSuggestions<FakeSuggestion>({ fetchImpl: spy });
|
|
66
|
-
f._internal.setQuery("samsung", FAKE_LOADER);
|
|
67
|
-
await f._internal.drain();
|
|
68
|
-
|
|
69
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
|
70
|
-
const [url, init] = spy.mock.calls[0];
|
|
71
|
-
expect(url).toBe("/deco/invoke/site/loaders/search/suggestions.ts");
|
|
72
|
-
expect(init?.method).toBe("POST");
|
|
73
|
-
expect(JSON.parse(init?.body as string)).toEqual({
|
|
74
|
-
query: "samsung",
|
|
75
|
-
limit: 5,
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("populates payload with the parsed response", async () => {
|
|
80
|
-
const f = createUseSuggestions<FakeSuggestion>({
|
|
81
|
-
fetchImpl: makeOkFetch({ products: ["a", "b"] }),
|
|
82
|
-
});
|
|
83
|
-
f._internal.setQuery("samsung", FAKE_LOADER);
|
|
84
|
-
await f._internal.drain();
|
|
85
|
-
expect(f._internal.payload.value).toEqual({ products: ["a", "b"] });
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("flips loading to true synchronously, back to false after fetch settles", async () => {
|
|
89
|
-
const f = createUseSuggestions<FakeSuggestion>({
|
|
90
|
-
fetchImpl: makeOkFetch({ products: [] }),
|
|
91
|
-
});
|
|
92
|
-
expect(f._internal.loading.value).toBe(false);
|
|
93
|
-
f._internal.setQuery("hi", FAKE_LOADER);
|
|
94
|
-
expect(f._internal.loading.value).toBe(true);
|
|
95
|
-
await f._internal.drain();
|
|
96
|
-
expect(f._internal.loading.value).toBe(false);
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe("createUseSuggestions — cancel + queue semantics", () => {
|
|
101
|
-
it("cancels older queries BEFORE they fetch — only the latest hits the network", async () => {
|
|
102
|
-
// Mock echoes the body's `query` field so we can tell which
|
|
103
|
-
// invocation actually reached the network.
|
|
104
|
-
const calls: string[] = [];
|
|
105
|
-
const fetchImpl: typeof fetch = ((_url, init) => {
|
|
106
|
-
const body = JSON.parse(init?.body as string) as { query: string };
|
|
107
|
-
calls.push(body.query);
|
|
108
|
-
return Promise.resolve(
|
|
109
|
-
new Response(JSON.stringify({ products: [body.query] }), {
|
|
110
|
-
status: 200,
|
|
111
|
-
}),
|
|
112
|
-
);
|
|
113
|
-
}) as typeof fetch;
|
|
114
|
-
|
|
115
|
-
const f = createUseSuggestions<FakeSuggestion>({ fetchImpl });
|
|
116
|
-
// Three queries kicked off back-to-back synchronously.
|
|
117
|
-
f._internal.setQuery("a", FAKE_LOADER);
|
|
118
|
-
f._internal.setQuery("b", FAKE_LOADER);
|
|
119
|
-
f._internal.setQuery("c", FAKE_LOADER);
|
|
120
|
-
await f._internal.drain();
|
|
121
|
-
|
|
122
|
-
// Only the latest query reaches the network — the cancel
|
|
123
|
-
// guard short-circuits the first two before they fetch.
|
|
124
|
-
expect(calls).toEqual(["c"]);
|
|
125
|
-
expect(f._internal.payload.value).toEqual({ products: ["c"] });
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("the latest-query guard prevents stale fetches from clearing loading prematurely", async () => {
|
|
129
|
-
// If the cancel guard ever regresses, this is the test that
|
|
130
|
-
// catches it: we kick off fetch #1, immediately call setQuery
|
|
131
|
-
// again, await drain, and expect the FINAL state to reflect
|
|
132
|
-
// the latest query — not an inconsistent "loading false but
|
|
133
|
-
// payload stale" mid-state.
|
|
134
|
-
const fetchImpl = makeOkFetch({ products: ["latest"] }, 5);
|
|
135
|
-
const f = createUseSuggestions<FakeSuggestion>({ fetchImpl });
|
|
136
|
-
f._internal.setQuery("a", FAKE_LOADER);
|
|
137
|
-
f._internal.setQuery("b", FAKE_LOADER);
|
|
138
|
-
f._internal.setQuery("c", FAKE_LOADER);
|
|
139
|
-
await f._internal.drain();
|
|
140
|
-
expect(f._internal.loading.value).toBe(false);
|
|
141
|
-
expect(f._internal.payload.value).toEqual({ products: ["latest"] });
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it("queues serially — fetches don't run concurrently", async () => {
|
|
145
|
-
// Race detector: track whether the count of in-flight fetches
|
|
146
|
-
// ever exceeds 1.
|
|
147
|
-
let inflight = 0;
|
|
148
|
-
let maxInflight = 0;
|
|
149
|
-
const fetchImpl: typeof fetch = (() =>
|
|
150
|
-
new Promise<Response>((resolve) => {
|
|
151
|
-
inflight += 1;
|
|
152
|
-
maxInflight = Math.max(maxInflight, inflight);
|
|
153
|
-
setTimeout(() => {
|
|
154
|
-
inflight -= 1;
|
|
155
|
-
resolve(
|
|
156
|
-
new Response(JSON.stringify({ products: [] }), { status: 200 }),
|
|
157
|
-
);
|
|
158
|
-
}, 5);
|
|
159
|
-
})) as typeof fetch;
|
|
160
|
-
|
|
161
|
-
const f = createUseSuggestions<FakeSuggestion>({ fetchImpl });
|
|
162
|
-
f._internal.setQuery("a", FAKE_LOADER);
|
|
163
|
-
f._internal.setQuery("b", FAKE_LOADER);
|
|
164
|
-
f._internal.setQuery("c", FAKE_LOADER);
|
|
165
|
-
await f._internal.drain();
|
|
166
|
-
expect(maxInflight).toBe(1);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
describe("createUseSuggestions — error path", () => {
|
|
171
|
-
it("forwards thrown errors to onError + console.error, does NOT update payload", async () => {
|
|
172
|
-
const onError = vi.fn();
|
|
173
|
-
const consoleError = vi
|
|
174
|
-
.spyOn(console, "error")
|
|
175
|
-
.mockImplementation(() => {});
|
|
176
|
-
|
|
177
|
-
const fetchImpl = (() =>
|
|
178
|
-
Promise.reject(new Error("network down"))) as typeof fetch;
|
|
179
|
-
const f = createUseSuggestions<FakeSuggestion>({ fetchImpl, onError });
|
|
180
|
-
|
|
181
|
-
f._internal.setQuery("samsung", FAKE_LOADER);
|
|
182
|
-
await f._internal.drain();
|
|
183
|
-
|
|
184
|
-
expect(onError).toHaveBeenCalledTimes(1);
|
|
185
|
-
const [err, query] = onError.mock.calls[0];
|
|
186
|
-
expect((err as Error).message).toBe("network down");
|
|
187
|
-
expect(query).toBe("samsung");
|
|
188
|
-
expect(consoleError).toHaveBeenCalled();
|
|
189
|
-
expect(f._internal.payload.value).toBeNull();
|
|
190
|
-
expect(f._internal.loading.value).toBe(false);
|
|
191
|
-
|
|
192
|
-
consoleError.mockRestore();
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it("non-2xx responses surface as errors", async () => {
|
|
196
|
-
const onError = vi.fn();
|
|
197
|
-
const consoleError = vi
|
|
198
|
-
.spyOn(console, "error")
|
|
199
|
-
.mockImplementation(() => {});
|
|
200
|
-
const fetchImpl = (() =>
|
|
201
|
-
Promise.resolve(
|
|
202
|
-
new Response("internal error", { status: 500 }),
|
|
203
|
-
)) as typeof fetch;
|
|
204
|
-
const f = createUseSuggestions<FakeSuggestion>({ fetchImpl, onError });
|
|
205
|
-
|
|
206
|
-
f._internal.setQuery("x", FAKE_LOADER);
|
|
207
|
-
await f._internal.drain();
|
|
208
|
-
|
|
209
|
-
expect(onError).toHaveBeenCalledTimes(1);
|
|
210
|
-
expect((onError.mock.calls[0][0] as Error).message).toContain("500");
|
|
211
|
-
expect(f._internal.payload.value).toBeNull();
|
|
212
|
-
expect(f._internal.loading.value).toBe(false);
|
|
213
|
-
|
|
214
|
-
consoleError.mockRestore();
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it("does NOT throw if onError is omitted (still console.errors)", async () => {
|
|
218
|
-
const consoleError = vi
|
|
219
|
-
.spyOn(console, "error")
|
|
220
|
-
.mockImplementation(() => {});
|
|
221
|
-
const fetchImpl = (() =>
|
|
222
|
-
Promise.reject(new Error("boom"))) as typeof fetch;
|
|
223
|
-
const f = createUseSuggestions<FakeSuggestion>({ fetchImpl });
|
|
224
|
-
|
|
225
|
-
f._internal.setQuery("x", FAKE_LOADER);
|
|
226
|
-
await expect(f._internal.drain()).resolves.toBeUndefined();
|
|
227
|
-
expect(consoleError).toHaveBeenCalled();
|
|
228
|
-
consoleError.mockRestore();
|
|
229
|
-
});
|
|
230
|
-
});
|