@snowcone-app/ui 0.12.0 → 0.12.2

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,125 @@
1
+ /**
2
+ * The realtime path of the gallery skip: a session's typed "cannot draw this design" refusal for one mockup
3
+ * (E_HOLD_NO_GRIP; E_HOLD_GRIP_BUSY after its one retry) leaves that picture out of the gallery (GallerySkip) for this
4
+ * design: never the session's error bar, never the "Mockup unavailable" overlay. A new design, or a new frame for it,
5
+ * brings it back. Without a gallery listening, the one picture says it is unavailable (never silent).
6
+ */
7
+ import { describe, expect, it, beforeEach, vi } from "vitest";
8
+ import React from "react";
9
+ import { render, waitFor, act, cleanup } from "@testing-library/react";
10
+ import { RealtimeProvider, useRealtime, type RealtimeContextValue } from "../RealtimeProvider";
11
+ import { useGallerySkipState, type GallerySkipValue } from "../GallerySkip";
12
+
13
+ const h = vi.hoisted(() => ({
14
+ opts: null as null | { onDesignRefusal?: (r: { mockupId: string; code: string; message: string }) => void },
15
+ state: { results: [] as Array<Record<string, unknown>> },
16
+ sendConfig: vi.fn(),
17
+ rerender: vi.fn((_ids: string[]) => true),
18
+ stable: {
19
+ sendCanvasBlob: vi.fn(), sendColorBlob: vi.fn(), updateMockupIds: vi.fn(), updatePlacementSettings: vi.fn(),
20
+ connect: vi.fn(), disconnect: vi.fn(), flushPendingBlobs: vi.fn(), hasPendingBlobs: vi.fn(() => false),
21
+ getLatestSentVersions: vi.fn(() => ({})), isFrameSuperseded: vi.fn(() => false),
22
+ sendCanvasState: vi.fn(() => true), ackFrame: vi.fn(() => false),
23
+ },
24
+ }));
25
+ vi.mock("@snowcone-app/sdk/react", () => ({
26
+ useRealtimeMockup: (opts: typeof h.opts) => {
27
+ h.opts = opts;
28
+ return {
29
+ isConnected: true, isConfigured: true, renderError: null, acks: null, mockupResults: h.state.results,
30
+ sendConfig: h.sendConfig, getLastSentVersion: () => 0, rerender: h.rerender, ...h.stable,
31
+ };
32
+ },
33
+ }));
34
+ vi.mock("@snowcone-app/sdk", async (importOriginal) => ({
35
+ ...(await importOriginal<typeof import("@snowcone-app/sdk")>()),
36
+ mockupRole: () => "ordinary",
37
+ gallerySizePicture: () => null,
38
+ resolveVariantId: () => "gv1",
39
+ resolvePreviewVariantId: (_c: unknown, v: string) => v,
40
+ }));
41
+ vi.mock("../Product", () => {
42
+ const product = {
43
+ id: "MXRC3J",
44
+ placements: [{ label: "Front", type: "image" }],
45
+ mockups: [{ id: "VJ6VEC" }, { id: "NQKEK3" }, { id: "VWCBAP" }],
46
+ };
47
+ return { useProductOptional: () => ({ product, selection: {} }) };
48
+ });
49
+ vi.mock("../MockupBackground", () => ({ useMockupBackgroundOptional: () => undefined }));
50
+
51
+ let ctx: RealtimeContextValue;
52
+ let skip: GallerySkipValue;
53
+ function Capture() { ctx = useRealtime(); return null; }
54
+ function Gallery({ design }: { design: string }) { skip = useGallerySkipState(design); return null; }
55
+ function Harness({ design, gallery = true, v = 0 }: { design: string; gallery?: boolean; v?: number }) {
56
+ void v;
57
+ return (
58
+ <RealtimeProvider autoEnable delivery="full-frame">
59
+ <Capture />
60
+ {gallery && <Gallery design={design} />}
61
+ </RealtimeProvider>
62
+ );
63
+ }
64
+ const refuse = (mockupId: string, code: string) =>
65
+ act(async () => h.opts?.onDesignRefusal?.({ mockupId, code, message: `${mockupId} ${code}` }));
66
+ const frame = (id: string, n: number) =>
67
+ ({ mockupId: id, imageUrl: `https://r/${id}-${n}`, renderUrl: `https://r/${id}-${n}`, imageSize: 1, placement: "Front", requestVersion: n });
68
+
69
+ beforeEach(() => {
70
+ cleanup();
71
+ vi.clearAllMocks();
72
+ h.opts = null;
73
+ h.state.results = [];
74
+ });
75
+
76
+ describe("realtime: a mockup that cannot draw this design is left out of the gallery", () => {
77
+ it("E_HOLD_NO_GRIP: skipped for this design, no unavailable overlay, no session error", async () => {
78
+ render(<Harness design="giraffe" />);
79
+ await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
80
+ await refuse("NQKEK3", "E_HOLD_NO_GRIP");
81
+ expect([...skip.skipped]).toEqual([["NQKEK3", "E_HOLD_NO_GRIP"]]);
82
+ expect(ctx.unavailableMockups.size).toBe(0);
83
+ expect(ctx.renderError).toBeNull();
84
+ });
85
+
86
+ it("E_HOLD_GRIP_BUSY: asked once more (rerender), then left out quietly", async () => {
87
+ render(<Harness design="giraffe" />);
88
+ await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
89
+ await refuse("VWCBAP", "E_HOLD_GRIP_BUSY");
90
+ expect(h.rerender).toHaveBeenCalledWith(["VWCBAP"]);
91
+ expect(skip.skipped.size).toBe(0);
92
+ await refuse("VWCBAP", "E_HOLD_GRIP_BUSY");
93
+ expect([...skip.skipped]).toEqual([["VWCBAP", "E_HOLD_GRIP_BUSY"]]);
94
+ expect(h.rerender).toHaveBeenCalledTimes(1);
95
+ });
96
+
97
+ it("a new design asks again; a NEW frame for the mockup brings it back, a re-delivered old one does not", async () => {
98
+ h.state.results = [frame("VJ6VEC", 1), frame("NQKEK3", 1)];
99
+ const { rerender } = render(<Harness design="giraffe" v={1} />);
100
+ await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
101
+ await refuse("NQKEK3", "E_HOLD_NO_GRIP");
102
+ expect(skip.skipped.has("NQKEK3")).toBe(true);
103
+ // the same frames again (a summary re-delivers every picture's latest): still out
104
+ h.state.results = [frame("VJ6VEC", 1), frame("NQKEK3", 1)];
105
+ await act(async () => rerender(<Harness design="giraffe" v={2} />));
106
+ expect(skip.skipped.has("NQKEK3")).toBe(true);
107
+ // a new design: asked again
108
+ await act(async () => rerender(<Harness design="toucan" v={2} />));
109
+ expect(skip.skipped.size).toBe(0);
110
+ // refused on the new design too, then an edit it can hold renders a NEW frame: back
111
+ await refuse("NQKEK3", "E_HOLD_NO_GRIP");
112
+ expect(skip.skipped.has("NQKEK3")).toBe(true);
113
+ h.state.results = [frame("VJ6VEC", 2), frame("NQKEK3", 2)];
114
+ await act(async () => rerender(<Harness design="toucan" v={3} />));
115
+ expect(skip.skipped.size).toBe(0);
116
+ });
117
+
118
+ it("no gallery listening: that one picture says it is unavailable (never silent), the session error stays clear", async () => {
119
+ render(<Harness design="giraffe" gallery={false} />);
120
+ await waitFor(() => expect(h.sendConfig).toHaveBeenCalled());
121
+ await refuse("NQKEK3", "E_HOLD_NO_GRIP");
122
+ expect(ctx.unavailableMockups.get("NQKEK3")?.code).toBe("E_HOLD_NO_GRIP");
123
+ expect(ctx.renderError).toBeNull();
124
+ });
125
+ });
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The gallery's skip rule (gallerySkipRules.ts): which answers leave a picture out (a hand that cannot hold THIS design)
3
+ * and which stay loud, and the gallery's order after a skip (MXRC3J: the clean shot VJ6VEC first, the two hand
4
+ * pictures NQKEK3 / VWCBAP left out for a design they cannot hold).
5
+ */
6
+ import { describe, expect, it } from "vitest";
7
+ import type { DesignRefusalCode } from "@snowcone-app/sdk";
8
+ import { gallerySkippedStamp, gallerySkipVerdict, galleryWithoutSkipped, NO_GALLERY_SKIPS } from "../gallerySkipRules";
9
+
10
+ describe("gallerySkipVerdict: which codes skip, which stay loud", () => {
11
+ it("E_HOLD_NO_GRIP is left out at once", () => {
12
+ expect(gallerySkipVerdict("E_HOLD_NO_GRIP", 0)).toBe("skip");
13
+ expect(gallerySkipVerdict("E_HOLD_NO_GRIP", 1)).toBe("skip");
14
+ });
15
+
16
+ it("E_HOLD_GRIP_BUSY is retried once, then left out quietly", () => {
17
+ expect(gallerySkipVerdict("E_HOLD_GRIP_BUSY", 0)).toBe("retry");
18
+ expect(gallerySkipVerdict("E_HOLD_GRIP_BUSY", 1)).toBe("skip");
19
+ expect(gallerySkipVerdict("E_HOLD_GRIP_BUSY", 2)).toBe("skip");
20
+ });
21
+
22
+ it("a genuine render error stays loud: other typed codes, untyped answers, no answer", () => {
23
+ for (const code of ["E_MOCKUP_NOT_BLESSED", "E_PREWARP_EMPTY", "E_HOLD_NOTHING_TO_HOLD", "E_SERVE_BUSY", "E_RENDER_ORIGIN_TIMEOUT", "render_failed", null, undefined, ""]) {
24
+ expect(gallerySkipVerdict(code, 0)).toBe("loud");
25
+ expect(gallerySkipVerdict(code, 1)).toBe("loud");
26
+ }
27
+ });
28
+ });
29
+
30
+ type Img = { mockupId?: string; isRealMockup?: boolean; label?: string };
31
+ const gallery: Img[] = [
32
+ { mockupId: "VJ6VEC", isRealMockup: true }, // the clean shot
33
+ { mockupId: "LAPTOP", isRealMockup: true },
34
+ { mockupId: "NQKEK3", isRealMockup: true }, // a woman's hand
35
+ { mockupId: "BOTTLE", isRealMockup: true },
36
+ { mockupId: "VWCBAP", isRealMockup: true }, // a man's hand
37
+ { isRealMockup: false, label: "AI Generated" },
38
+ ];
39
+ const skips = (entries: Array<[string, DesignRefusalCode]>) => new Map(entries);
40
+
41
+ describe("galleryWithoutSkipped: the gallery after a skip", () => {
42
+ it("leaves out the skipped pictures; the clean shot stays first and the rest keep their order", () => {
43
+ const out = galleryWithoutSkipped(gallery, skips([["NQKEK3", "E_HOLD_NO_GRIP"], ["VWCBAP", "E_HOLD_NO_GRIP"]]));
44
+ expect(out.map((i) => i.mockupId ?? i.label)).toEqual(["VJ6VEC", "LAPTOP", "BOTTLE", "AI Generated"]);
45
+ });
46
+
47
+ it("nothing skipped: the same array (no re-render churn)", () => {
48
+ expect(galleryWithoutSkipped(gallery, NO_GALLERY_SKIPS)).toBe(gallery);
49
+ expect(galleryWithoutSkipped(gallery, skips([["OTHER1", "E_HOLD_NO_GRIP"]]))).toBe(gallery);
50
+ });
51
+
52
+ it("only real mockups are left out; never an empty gallery (then every picture stays and says so)", () => {
53
+ const one: Img[] = [{ mockupId: "NQKEK3", isRealMockup: true }];
54
+ expect(galleryWithoutSkipped(one, skips([["NQKEK3", "E_HOLD_NO_GRIP"]]))).toBe(one);
55
+ const notReal: Img[] = [{ mockupId: "VJ6VEC", isRealMockup: true }, { mockupId: "NQKEK3", isRealMockup: false }];
56
+ expect(galleryWithoutSkipped(notReal, skips([["NQKEK3", "E_HOLD_NO_GRIP"]]))).toBe(notReal);
57
+ });
58
+ });
59
+
60
+ describe("gallerySkippedStamp: data-gallery-skipped", () => {
61
+ it("names each left-out picture as <mockupId>:<code>, in a stable order", () => {
62
+ const s = skips([["VWCBAP", "E_HOLD_GRIP_BUSY"], ["NQKEK3", "E_HOLD_NO_GRIP"]]);
63
+ expect(gallerySkippedStamp(s, galleryWithoutSkipped(gallery, s))).toBe("NQKEK3:E_HOLD_NO_GRIP VWCBAP:E_HOLD_GRIP_BUSY");
64
+ expect(gallerySkippedStamp(skips([["NQKEK3", "E_HOLD_NO_GRIP"]]), [{ mockupId: "VJ6VEC" }])).toBe("NQKEK3:E_HOLD_NO_GRIP");
65
+ });
66
+
67
+ it("says nothing when nothing is left out, or when the host did not leave it out (truthful)", () => {
68
+ expect(gallerySkippedStamp(NO_GALLERY_SKIPS, gallery)).toBeUndefined();
69
+ expect(gallerySkippedStamp(skips([["NQKEK3", "E_HOLD_NO_GRIP"]]), gallery)).toBeUndefined();
70
+ });
71
+ });
@@ -0,0 +1,57 @@
1
+ /**
2
+ * The gallery's skip rule: a picture whose render cannot draw THIS design (a hand that cannot hold it,
3
+ * `@snowcone-app/sdk` DESIGN_REFUSALS) is not in that shopper's gallery, for that design. Pure, so the rule is
4
+ * testable without a render.
5
+ *
6
+ * - `E_HOLD_NO_GRIP` (422): left out at once.
7
+ * - `E_HOLD_GRIP_BUSY` (503): asked once more; a second design refusal leaves it out quietly.
8
+ * - Anything else (another `E_*`, a 500, a network failure, no code): LOUD, exactly as before (the picture says it is
9
+ * unavailable). The list is closed: a code nobody classified is never hidden.
10
+ *
11
+ * Machine-readable: a gallery that left pictures out stamps `data-gallery-skipped="<mockupId>:<code> …"` on its root.
12
+ */
13
+ import { designRefusalKind, type DesignRefusalCode } from "@snowcone-app/sdk";
14
+
15
+ export type GallerySkipVerdict = "skip" | "retry" | "loud";
16
+
17
+ /** What a gallery does with a picture whose render answered `code` (null: no typed code), `attempt` answers in (0 = the first). */
18
+ export function gallerySkipVerdict(code: string | null | undefined, attempt: number): GallerySkipVerdict {
19
+ const kind = designRefusalKind(code);
20
+ if (kind === "skip") return "skip";
21
+ if (kind === "retry-once") return attempt < 1 ? "retry" : "skip";
22
+ return "loud";
23
+ }
24
+
25
+ /** The pictures left out of the gallery for the design shown now: mockupId -> the code its render answered. */
26
+ export type GallerySkips = ReadonlyMap<string, DesignRefusalCode>;
27
+
28
+ export const NO_GALLERY_SKIPS: GallerySkips = new Map();
29
+
30
+ /**
31
+ * The gallery without the pictures left out, every other picture in its order (the first stays first). Only real
32
+ * mockups are left out (an AI picture or a placeholder never is). Never empties a gallery: when every picture would
33
+ * go, all stay and each says it is unavailable (loud). The same array comes back when nothing is left out.
34
+ */
35
+ export function galleryWithoutSkipped<T extends { mockupId?: string; isRealMockup?: boolean }>(
36
+ images: T[],
37
+ skipped: GallerySkips,
38
+ ): T[] {
39
+ if (skipped.size === 0) return images;
40
+ const kept = images.filter((img) => !(img.isRealMockup && img.mockupId && skipped.has(img.mockupId)));
41
+ if (kept.length === images.length || kept.length === 0) return images;
42
+ return kept;
43
+ }
44
+
45
+ /**
46
+ * `data-gallery-skipped` for a gallery showing `shown`: each left-out picture as `<mockupId>:<code>`, space separated,
47
+ * in a stable order. Only pictures really absent from `shown` (a host that did not filter stamps nothing).
48
+ */
49
+ export function gallerySkippedStamp(skipped: GallerySkips, shown: ReadonlyArray<{ mockupId?: string }>): string | undefined {
50
+ if (skipped.size === 0) return undefined;
51
+ const shownIds = new Set(shown.map((img) => img.mockupId).filter(Boolean));
52
+ const parts = [...skipped]
53
+ .filter(([id]) => !shownIds.has(id))
54
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
55
+ .map(([id, code]) => `${id}:${code}`);
56
+ return parts.length ? parts.join(" ") : undefined;
57
+ }