@decocms/start 2.25.0 → 2.27.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.
@@ -0,0 +1,230 @@
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
+ });
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Search-suggestions hook factory.
3
+ *
4
+ * Both casaevideo-storefront and baggagio-tanstack independently
5
+ * invented the same shape for autocomplete-style suggestions:
6
+ *
7
+ * - module-level signal for the current payload + loading flag
8
+ * - serial in-flight queue so older requests can't race past newer ones
9
+ * - "is this still the latest query?" cancel guard
10
+ * - posts to `/deco/invoke/<__resolveType>` with the loader's
11
+ * extra props + the live `query` string
12
+ *
13
+ * This factory is the canonical version. Sites instantiate it once
14
+ * at module load with their concrete suggestion type and (optionally)
15
+ * an `onError` hook for observability (Sentry / OpenTelemetry / etc.).
16
+ *
17
+ * Why a factory and not a plain hook:
18
+ * - Each call to `createUseSuggestions()` produces an isolated
19
+ * `payload` / `loading` / queue. Keeps the door open for sites
20
+ * with multiple independent suggestion streams (e.g. searchbar
21
+ * *and* a category-jump suggester) without globally-shared state.
22
+ * - Type narrowing happens at the factory boundary, not at hook
23
+ * usage — the returned `useSuggestions` is already specialised
24
+ * on `T` so callers don't need to re-pass generics.
25
+ * - Mirrors the existing `createUseCart` / `createUseUser` /
26
+ * `createUseWishlist` factory pattern from
27
+ * `@decocms/apps/vtex/hooks/*`. See
28
+ * `references/platform-hooks-factories.md`.
29
+ */
30
+
31
+ import { useCallback } from "react";
32
+ import type { Resolved } from "../types";
33
+ import { signal, type ReactiveSignal } from "./signal";
34
+
35
+ /**
36
+ * Optional dependencies the factory accepts at instantiation time.
37
+ *
38
+ * Most are pure observability hooks — the factory itself runs a
39
+ * `console.error()` after invoking `onError`, so callers don't have
40
+ * to remember to forward the error to the console themselves.
41
+ */
42
+ export interface UseSuggestionsOptions {
43
+ /**
44
+ * Called once per failed fetch with the original error and the
45
+ * query that triggered it. Use for Sentry / OTEL captures.
46
+ *
47
+ * The factory still calls `console.error` after this returns so
48
+ * sites that don't wire `onError` keep the same console output.
49
+ */
50
+ onError?: (error: unknown, query: string) => void;
51
+
52
+ /**
53
+ * Override the fetch implementation. Tests pass a stub here; the
54
+ * default is the global `fetch`. Production sites have no reason
55
+ * to set this.
56
+ */
57
+ fetchImpl?: typeof fetch;
58
+ }
59
+
60
+ /**
61
+ * Shape returned by the hook produced by `createUseSuggestions`.
62
+ *
63
+ * `loading` and `payload` are reactive signals — subscribe with
64
+ * `useStore()` from `@tanstack/react-store` (or read `.value`
65
+ * directly inside an `useEffect`).
66
+ */
67
+ export interface UseSuggestionsReturn<T> {
68
+ loading: ReactiveSignal<boolean>;
69
+ payload: ReactiveSignal<T | null>;
70
+ /**
71
+ * Trigger a suggestion fetch for `query`. Calls coalesce through
72
+ * a serial promise queue, and only the *latest* query's result
73
+ * is allowed to flip `loading` back to `false` — so rapid keystrokes
74
+ * don't leave the UI permanently in a stale loading state.
75
+ */
76
+ setQuery: (query: string) => void;
77
+ }
78
+
79
+ /**
80
+ * Returned by {@link createUseSuggestions}.
81
+ *
82
+ * `useSuggestions` is the React hook bound to this factory's state.
83
+ * The `_internal` field exposes the underlying signals and a non-
84
+ * React `setQuery` for advanced cases (SSR pre-population, unit
85
+ * tests, server-side warmup). Sites almost never need it.
86
+ */
87
+ export interface CreateUseSuggestionsReturn<T> {
88
+ useSuggestions: (loader: Resolved<T | null>) => UseSuggestionsReturn<T>;
89
+ _internal: {
90
+ readonly payload: ReactiveSignal<T | null>;
91
+ readonly loading: ReactiveSignal<boolean>;
92
+ /**
93
+ * Same semantics as `useSuggestions(...).setQuery`, but pure JS —
94
+ * no React hook context required. Useful in unit tests and for
95
+ * SSR pre-fetch helpers.
96
+ */
97
+ setQuery: (query: string, loader: Resolved<T | null>) => void;
98
+ /**
99
+ * Promise that resolves once every queued fetch has settled.
100
+ * Tests await this to assert post-cancellation state.
101
+ */
102
+ readonly drain: () => Promise<unknown>;
103
+ };
104
+ }
105
+
106
+ /**
107
+ * Build a typed `useSuggestions` hook bound to a private
108
+ * `payload` / `loading` / queue tuple. Call once per stream at
109
+ * module load.
110
+ *
111
+ * @example
112
+ * // site/src/sdk/useSuggestions.ts
113
+ * import { createUseSuggestions } from "@decocms/start/sdk/useSuggestions";
114
+ * import * as Sentry from "@sentry/react";
115
+ * import type { Suggestion } from "@decocms/apps/commerce/types";
116
+ *
117
+ * export const { useSuggestions } = createUseSuggestions<Suggestion>({
118
+ * onError: (err) => Sentry.captureException(err),
119
+ * });
120
+ */
121
+ export function createUseSuggestions<T>(
122
+ options: UseSuggestionsOptions = {},
123
+ ): CreateUseSuggestionsReturn<T> {
124
+ const payload = signal<T | null>(null);
125
+ const loading = signal<boolean>(false);
126
+ let queue: Promise<unknown> = Promise.resolve();
127
+ let latestQuery = "";
128
+
129
+ const fetchImpl = options.fetchImpl ?? fetch;
130
+ const onError = options.onError;
131
+
132
+ async function doFetch(
133
+ query: string,
134
+ resolved: Resolved<T | null>,
135
+ ): Promise<void> {
136
+ if (latestQuery !== query) return;
137
+
138
+ const { __resolveType, ...extraProps } = resolved as {
139
+ __resolveType: string;
140
+ [k: string]: unknown;
141
+ };
142
+
143
+ try {
144
+ const response = await fetchImpl(`/deco/invoke/${__resolveType}`, {
145
+ method: "POST",
146
+ headers: { "Content-Type": "application/json" },
147
+ body: JSON.stringify({ query, ...extraProps }),
148
+ });
149
+ if (!response.ok) {
150
+ throw new Error(`Suggestions invoke failed: ${response.status}`);
151
+ }
152
+ payload.value = (await response.json()) as T | null;
153
+ } catch (error) {
154
+ onError?.(error, query);
155
+ console.error("[useSuggestions] fetch failed:", error);
156
+ } finally {
157
+ // Only the latest query is allowed to flip the loading flag —
158
+ // otherwise rapid keystrokes can leave the UI in a stale
159
+ // "still loading" state because an older fetch resolved last.
160
+ if (latestQuery === query) loading.value = false;
161
+ }
162
+ }
163
+
164
+ function setQueryImpl(query: string, loader: Resolved<T | null>): void {
165
+ loading.value = true;
166
+ latestQuery = query;
167
+ queue = queue.then(() => doFetch(query, loader));
168
+ }
169
+
170
+ function useSuggestions(loader: Resolved<T | null>): UseSuggestionsReturn<T> {
171
+ const setQuery = useCallback(
172
+ (query: string) => setQueryImpl(query, loader),
173
+ [loader],
174
+ );
175
+
176
+ return { loading, payload, setQuery };
177
+ }
178
+
179
+ return {
180
+ useSuggestions,
181
+ _internal: {
182
+ payload,
183
+ loading,
184
+ setQuery: setQueryImpl,
185
+ drain: () => queue,
186
+ },
187
+ };
188
+ }