@snowcone-app/mockup-resolver 1.0.3 → 1.0.4
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/CHANGELOG.md +7 -0
- package/package.json +2 -2
- package/src/index.ts +71 -51
- package/src/resolve.ts +143 -1
- package/test/public-params.test.ts +11 -2
- package/test/resolve.test.ts +169 -0
- package/wrangler.jsonc +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @snowcone-app/mockup-resolver
|
|
2
2
|
|
|
3
|
+
## 1.0.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`ada5ed2`](https://github.com/snowcone-app/snowcone-monorepo/commit/ada5ed200f219787bc97018e0c5811ea8e4c2fa1), [`f66c9c3`](https://github.com/snowcone-app/snowcone-monorepo/commit/f66c9c3b1ca2ab50bc390feb4a691cd95bb53abd)]:
|
|
8
|
+
- @snowcone-app/mockup-url@0.3.0
|
|
9
|
+
|
|
3
10
|
## 1.0.3
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/mockup-resolver",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "URL signing and verification Cloudflare Worker",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "",
|
|
7
7
|
"license": "ISC",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@snowcone-app/mockup-url": "0.
|
|
9
|
+
"@snowcone-app/mockup-url": "0.3.0"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@cloudflare/workers-types": "^4.20250303.0",
|
package/src/index.ts
CHANGED
|
@@ -316,6 +316,15 @@ function addCorsHeaders(response: Response, request: Request): Response {
|
|
|
316
316
|
"Access-Control-Allow-Headers",
|
|
317
317
|
"Content-Type, X-API-Key, X-Client-ID"
|
|
318
318
|
);
|
|
319
|
+
// This reflects the request Origin into ACAO, so shared caches MUST key on
|
|
320
|
+
// Origin — otherwise a cached entry for origin A (or a no-Origin request) is
|
|
321
|
+
// replayed to origin B and the crossorigin <img> fails (CORS poisoning).
|
|
322
|
+
// Preserve any upstream Vary rather than clobbering it.
|
|
323
|
+
const prevVary = corsResponse.headers.get("Vary");
|
|
324
|
+
corsResponse.headers.set(
|
|
325
|
+
"Vary",
|
|
326
|
+
prevVary && !/\borigin\b/i.test(prevVary) ? `${prevVary}, Origin` : prevVary || "Origin"
|
|
327
|
+
);
|
|
319
328
|
|
|
320
329
|
return corsResponse;
|
|
321
330
|
}
|
|
@@ -776,13 +785,14 @@ import { fetchDesign } from "./design-ref";
|
|
|
776
785
|
import {
|
|
777
786
|
buildMockupUrl,
|
|
778
787
|
canonicalForSig,
|
|
788
|
+
normalizeBgColor,
|
|
779
789
|
signDevSig,
|
|
780
790
|
parsePublicUrl,
|
|
781
791
|
PARAMS,
|
|
782
792
|
type DesignElement,
|
|
783
|
-
type ImageAlignment,
|
|
784
793
|
} from "@snowcone-app/mockup-url";
|
|
785
794
|
import {
|
|
795
|
+
assembleArtwork,
|
|
786
796
|
fetchProduct,
|
|
787
797
|
looksLikeResolveRequest,
|
|
788
798
|
resolveAlbumPagePlacement,
|
|
@@ -1083,11 +1093,17 @@ async function handlePublicResolve(
|
|
|
1083
1093
|
// for invalidation + per-request auth/rate (ADR-0076 §7). 4xx must never be
|
|
1084
1094
|
// cached as the image.
|
|
1085
1095
|
const NO_STORE = { "Cache-Control": "no-store" } as const;
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1096
|
+
// `code` is a stable machine-readable error code (the `E_*` family, ADR-0089 /
|
|
1097
|
+
// canvas error house style) included alongside the human message so agents
|
|
1098
|
+
// branch on the code, not the prose. Omitted for the legacy untyped 400s.
|
|
1099
|
+
const errResp = (status: number, message: string, code?: string): Response =>
|
|
1100
|
+
new Response(
|
|
1101
|
+
JSON.stringify(code ? { error: message, code } : { error: message }),
|
|
1102
|
+
{
|
|
1103
|
+
status,
|
|
1104
|
+
headers: { "Content-Type": "application/json", ...NO_STORE },
|
|
1105
|
+
}
|
|
1106
|
+
);
|
|
1091
1107
|
|
|
1092
1108
|
const reqUrl = new URL(request.url);
|
|
1093
1109
|
const q = reqUrl.searchParams;
|
|
@@ -1119,6 +1135,33 @@ async function handlePublicResolve(
|
|
|
1119
1135
|
const aspectParam = q.get(PARAMS.aspect);
|
|
1120
1136
|
const aspect: "16:9" | "2:3" | undefined =
|
|
1121
1137
|
aspectParam === "2:3" ? "2:3" : aspectParam === "16:9" ? "16:9" : undefined;
|
|
1138
|
+
// Per-render background override (`bg=`): painted behind the mockup, visible
|
|
1139
|
+
// only through scene transparency. Validated here (via the grammar SSOT) so a
|
|
1140
|
+
// malformed color fails loudly at the edge, and required in CANONICAL form
|
|
1141
|
+
// (lowercase, no '#') because the URL is the cache key — accepting spelling
|
|
1142
|
+
// variants would cache the same pixels under multiple entries. When ABSENT
|
|
1143
|
+
// the render server falls back to the shop's configured default
|
|
1144
|
+
// (`mockup_bg_color`), else transparent; the reserved `bg=transparent` token
|
|
1145
|
+
// suppresses that default for this one render (forwarded verbatim — the
|
|
1146
|
+
// render server skips its shop-default fallback for it).
|
|
1147
|
+
const bg = q.get(PARAMS.bg) ?? undefined;
|
|
1148
|
+
if (bg !== undefined) {
|
|
1149
|
+
const canonical = normalizeBgColor(bg);
|
|
1150
|
+
if (canonical === null) {
|
|
1151
|
+
return errResp(
|
|
1152
|
+
400,
|
|
1153
|
+
`bg= must be a 6-digit hex color (e.g. bg=f5f0ea) or bg=transparent, got "${bg}". ` +
|
|
1154
|
+
"Omit it to use the shop's configured default background."
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
if (canonical !== bg) {
|
|
1158
|
+
return errResp(
|
|
1159
|
+
400,
|
|
1160
|
+
`bg= must be the canonical lowercase form without '#' — use bg=${canonical}. ` +
|
|
1161
|
+
"(URLs are cache keys, so one value must always be one spelling.)"
|
|
1162
|
+
);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1122
1165
|
|
|
1123
1166
|
if (!productCode) return errResp(400, "Missing productCode in path");
|
|
1124
1167
|
if (!asset && designKeys.length === 0 && !designRef) {
|
|
@@ -1353,12 +1396,9 @@ async function handlePublicResolve(
|
|
|
1353
1396
|
// placement becomes the repeating page placement's geometry.
|
|
1354
1397
|
if (pagePlacement) scene.placement = pagePlacement;
|
|
1355
1398
|
|
|
1356
|
-
// 4b. Build the design elements:
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
"center", "top", "far-top", "bottom", "far-bottom",
|
|
1360
|
-
"left", "far-left", "right", "far-right",
|
|
1361
|
-
]);
|
|
1399
|
+
// 4b. Build the design elements: one base broadcast across every placement
|
|
1400
|
+
// plus per-placement overrides (ADR-0089), or a catalog design verbatim.
|
|
1401
|
+
//
|
|
1362
1402
|
// Strict: the URL key must equal the placement's canonical catalog `key`
|
|
1363
1403
|
// EXACTLY — no label/slug/case fallbacks. A wrong key fails loudly with
|
|
1364
1404
|
// "Unknown placement" instead of being silently coerced, and this matches the
|
|
@@ -1402,46 +1442,23 @@ async function handlePublicResolve(
|
|
|
1402
1442
|
alignment: "center",
|
|
1403
1443
|
});
|
|
1404
1444
|
}
|
|
1405
|
-
} else
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
});
|
|
1418
|
-
} else if (fill.src) {
|
|
1419
|
-
const el: DesignElement = {
|
|
1420
|
-
type: "image",
|
|
1421
|
-
imageUrl: isDefaultToken(fill.src) ? house : fill.src,
|
|
1422
|
-
placement: pl.label,
|
|
1423
|
-
width: pl.width,
|
|
1424
|
-
height: pl.height,
|
|
1425
|
-
alignment:
|
|
1426
|
-
fill.align && ALIGNMENTS.has(fill.align)
|
|
1427
|
-
? (fill.align as ImageAlignment)
|
|
1428
|
-
: "center",
|
|
1429
|
-
};
|
|
1430
|
-
if (fill.tile !== undefined && ALLOWED_TILES.includes(fill.tile)) {
|
|
1431
|
-
el.tiles = fill.tile as 0.25 | 0.5 | 1 | 2 | 4;
|
|
1432
|
-
}
|
|
1433
|
-
designElements.push(el);
|
|
1434
|
-
}
|
|
1435
|
-
}
|
|
1436
|
-
} else if (asset) {
|
|
1437
|
-
designElements.push({
|
|
1438
|
-
type: "image",
|
|
1439
|
-
imageUrl: isDefaultToken(asset) ? house : asset,
|
|
1440
|
-
placement: scene.placement.label,
|
|
1441
|
-
width: scene.placement.width,
|
|
1442
|
-
height: scene.placement.height,
|
|
1443
|
-
alignment: "center",
|
|
1445
|
+
} else {
|
|
1446
|
+
// ADR-0089 base+override: the scalar `asset=` SEEDS every placement (each
|
|
1447
|
+
// with ITS OWN geometry), then each `asset.<key>=` OVERRIDES the base on
|
|
1448
|
+
// that one placement. `default` blanks (house) at either scope. An unknown
|
|
1449
|
+
// override key fails loudly (E_PLACEMENT_NOT_FOUND, listing valid keys).
|
|
1450
|
+
// The base broadcast fans out across `product.placements` directly — it no
|
|
1451
|
+
// longer rides the single default-placement resolution.
|
|
1452
|
+
const assembled = assembleArtwork(product, {
|
|
1453
|
+
asset,
|
|
1454
|
+
design,
|
|
1455
|
+
house,
|
|
1456
|
+
findPlacement,
|
|
1444
1457
|
});
|
|
1458
|
+
if (!assembled.ok) {
|
|
1459
|
+
return errResp(assembled.status, assembled.message, assembled.code);
|
|
1460
|
+
}
|
|
1461
|
+
designElements.push(...assembled.elements);
|
|
1445
1462
|
}
|
|
1446
1463
|
if (designElements.length === 0) {
|
|
1447
1464
|
return errResp(400, "Missing artwork: provide asset= or asset.<placement>=");
|
|
@@ -1467,6 +1484,7 @@ async function handlePublicResolve(
|
|
|
1467
1484
|
variantId: vr.variantId,
|
|
1468
1485
|
width,
|
|
1469
1486
|
...(aspect ? { aspect } : {}),
|
|
1487
|
+
...(bg ? { bg } : {}),
|
|
1470
1488
|
...(hasOptionSelections ? { optionSelections: vr.optionSelections } : {}),
|
|
1471
1489
|
design: designElements,
|
|
1472
1490
|
},
|
|
@@ -1550,6 +1568,8 @@ export default {
|
|
|
1550
1568
|
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
1551
1569
|
"Access-Control-Allow-Headers":
|
|
1552
1570
|
"Content-Type, X-API-Key, X-Client-ID",
|
|
1571
|
+
// Reflected Origin → cache must vary by Origin (see CORS notes above).
|
|
1572
|
+
Vary: "Origin",
|
|
1553
1573
|
},
|
|
1554
1574
|
});
|
|
1555
1575
|
}
|
package/src/resolve.ts
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
// logic in `apps/www/app/api/harness/mockup/route.ts` (the prototype) and the
|
|
5
5
|
// SDK's `getProduct`. Edge-safe: plain fetch + JSON. (ADR-0073 §10.)
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
PARAMS,
|
|
9
|
+
type DesignElement,
|
|
10
|
+
type ImageAlignment,
|
|
11
|
+
type ParsedFill,
|
|
12
|
+
} from "@snowcone-app/mockup-url";
|
|
8
13
|
|
|
9
14
|
/** Paths reserved for the signer/utility surface — never treated as a productCode. */
|
|
10
15
|
const RESERVED_RESOLVE_PATHS = new Set([
|
|
@@ -200,6 +205,143 @@ export function resolveAlbumPagePlacement(
|
|
|
200
205
|
return { ...base, key };
|
|
201
206
|
}
|
|
202
207
|
|
|
208
|
+
// ─── Artwork assembly: one base design → all placements + per-key overrides ──
|
|
209
|
+
// ADR-0089. The mockup-URL grammar resolves artwork as ONE base design layer
|
|
210
|
+
// that every placement inherits, plus per-placement overrides:
|
|
211
|
+
//
|
|
212
|
+
// asset=<url> → SEED every placement with this base (each using
|
|
213
|
+
// ITS OWN geometry, alignment "center").
|
|
214
|
+
// asset.<key>=<url> → OVERRIDE the base on that one placement.
|
|
215
|
+
// asset=default → blank ALL placements as base (house placeholder).
|
|
216
|
+
// asset.<key>=default → blank ONE placement while the others keep the base.
|
|
217
|
+
//
|
|
218
|
+
// Placements with neither a base nor an override stay blank (no element). The
|
|
219
|
+
// base broadcast does NOT depend on the single default-placement resolution —
|
|
220
|
+
// it fans out across `product.placements` directly, so every placement renders
|
|
221
|
+
// the design by default (the pit of success).
|
|
222
|
+
|
|
223
|
+
/** The reserved house-blank token — see the resolver's `DEFAULT_ASSET_TOKEN`. */
|
|
224
|
+
const DEFAULT_ASSET_TOKEN = "default";
|
|
225
|
+
|
|
226
|
+
/** Image alignment values accepted on `align.<key>` fills. */
|
|
227
|
+
const ALIGNMENTS = new Set<string>([
|
|
228
|
+
"center", "top", "far-top", "bottom", "far-bottom",
|
|
229
|
+
"left", "far-left", "right", "far-right",
|
|
230
|
+
]);
|
|
231
|
+
/** Tile scales accepted on `tile.<key>` fills (mirrors rendercenter). */
|
|
232
|
+
const ALLOWED_TILES = [0.25, 0.5, 1, 2, 4];
|
|
233
|
+
|
|
234
|
+
/** Inputs the assembler needs from the request, decoupled from the Worker env. */
|
|
235
|
+
export interface AssembleArtworkInput {
|
|
236
|
+
/** The base `asset=` value (URL, the `default` token, or null when absent). */
|
|
237
|
+
asset: string | null;
|
|
238
|
+
/** Per-placement overrides parsed from `asset.<key>`/`color.<key>`/etc. */
|
|
239
|
+
design: Record<string, ParsedFill>;
|
|
240
|
+
/** The URL the reserved `default` token resolves to (the house blank PNG). */
|
|
241
|
+
house: string;
|
|
242
|
+
/** Resolve a wire key to a placement (catalog key + album page expansion). */
|
|
243
|
+
findPlacement: (key: string) => ResolvedPlacement | null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The assembler either yields render elements or a typed, listing error. */
|
|
247
|
+
export type AssembleArtworkResult =
|
|
248
|
+
| { ok: true; elements: DesignElement[] }
|
|
249
|
+
| { ok: false; status: number; code: string; message: string };
|
|
250
|
+
|
|
251
|
+
/** Build one image/color `DesignElement` for a placement from a parsed fill. */
|
|
252
|
+
function elementFromFill(
|
|
253
|
+
fill: ParsedFill,
|
|
254
|
+
pl: ResolvedPlacement,
|
|
255
|
+
house: string
|
|
256
|
+
): DesignElement | null {
|
|
257
|
+
if (fill.color) {
|
|
258
|
+
return {
|
|
259
|
+
type: "color",
|
|
260
|
+
hex: fill.color,
|
|
261
|
+
placement: pl.label,
|
|
262
|
+
width: pl.width,
|
|
263
|
+
height: pl.height,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
if (fill.src) {
|
|
267
|
+
const el: DesignElement = {
|
|
268
|
+
type: "image",
|
|
269
|
+
imageUrl: fill.src === DEFAULT_ASSET_TOKEN ? house : fill.src,
|
|
270
|
+
placement: pl.label,
|
|
271
|
+
width: pl.width,
|
|
272
|
+
height: pl.height,
|
|
273
|
+
alignment:
|
|
274
|
+
fill.align && ALIGNMENTS.has(fill.align)
|
|
275
|
+
? (fill.align as ImageAlignment)
|
|
276
|
+
: "center",
|
|
277
|
+
};
|
|
278
|
+
if (fill.tile !== undefined && ALLOWED_TILES.includes(fill.tile)) {
|
|
279
|
+
el.tiles = fill.tile as 0.25 | 0.5 | 1 | 2 | 4;
|
|
280
|
+
}
|
|
281
|
+
return el;
|
|
282
|
+
}
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Assemble the render `DesignElement[]` from the base+override grammar
|
|
288
|
+
* (ADR-0089). Pure — no env, no fetch — so the base-broadcast / per-key
|
|
289
|
+
* override / `default`-token / unknown-key contract is unit-testable.
|
|
290
|
+
*
|
|
291
|
+
* A `default` token (base OR per-key) means house-blank; a per-key override
|
|
292
|
+
* REPLACES the base on that placement only; an unknown `asset.<key>` key fails
|
|
293
|
+
* loudly with `E_PLACEMENT_NOT_FOUND`, naming the bad key and listing every
|
|
294
|
+
* valid placement key for this product.
|
|
295
|
+
*/
|
|
296
|
+
export function assembleArtwork(
|
|
297
|
+
product: ResolvedProduct,
|
|
298
|
+
input: AssembleArtworkInput
|
|
299
|
+
): AssembleArtworkResult {
|
|
300
|
+
const { asset, design, house, findPlacement } = input;
|
|
301
|
+
// Per-placement element, keyed by the placement LABEL (the wire key for a
|
|
302
|
+
// page expands onto the base label, so two page_N overrides would otherwise
|
|
303
|
+
// collide — but each carries its own page key/label, so labels stay unique).
|
|
304
|
+
const byKey = new Map<string, DesignElement>();
|
|
305
|
+
|
|
306
|
+
// 1. Base broadcast: seed EVERY placement with the base, each using its own
|
|
307
|
+
// geometry. `default` seeds the house blank as the base for all.
|
|
308
|
+
if (asset !== null) {
|
|
309
|
+
const baseUrl = asset === DEFAULT_ASSET_TOKEN ? house : asset;
|
|
310
|
+
for (const pl of product.placements) {
|
|
311
|
+
byKey.set(pl.key, {
|
|
312
|
+
type: "image",
|
|
313
|
+
imageUrl: baseUrl,
|
|
314
|
+
placement: pl.label,
|
|
315
|
+
width: pl.width,
|
|
316
|
+
height: pl.height,
|
|
317
|
+
alignment: "center",
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// 2. Per-key overrides REPLACE the base on the named placement. Unknown keys
|
|
323
|
+
// fail loudly (E_PLACEMENT_NOT_FOUND, listing valid keys). A `default`
|
|
324
|
+
// override blanks that one placement; the others keep the base.
|
|
325
|
+
for (const key of Object.keys(design)) {
|
|
326
|
+
const pl = findPlacement(key);
|
|
327
|
+
if (!pl) {
|
|
328
|
+
const valid = product.placements.map((p) => p.key);
|
|
329
|
+
return {
|
|
330
|
+
ok: false,
|
|
331
|
+
status: 400,
|
|
332
|
+
code: "E_PLACEMENT_NOT_FOUND",
|
|
333
|
+
message:
|
|
334
|
+
`Unknown placement "${key}" for product ${product.id}. ` +
|
|
335
|
+
`Valid placements: ${valid.length > 0 ? valid.join(", ") : "(none)"}.`,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
const el = elementFromFill(design[key], pl, house);
|
|
339
|
+
if (el) byKey.set(pl.key, el);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return { ok: true, elements: [...byKey.values()] };
|
|
343
|
+
}
|
|
344
|
+
|
|
203
345
|
/** Fetch a product from Meilisearch (KV-cached). Returns null if not found. */
|
|
204
346
|
export async function fetchProduct(
|
|
205
347
|
productCode: string,
|
|
@@ -14,12 +14,12 @@ import { rejectUnknownPublicParams } from '../src/public-params'
|
|
|
14
14
|
|
|
15
15
|
describe('isKnownPublicParam', () => {
|
|
16
16
|
it('accepts every exact public param name', () => {
|
|
17
|
-
for (const name of ['asset', 'design', 'shop', 'placement', 'page', 'variant', 'mockup', 'width', 'aspect', 'signature']) {
|
|
17
|
+
for (const name of ['asset', 'design', 'shop', 'placement', 'page', 'variant', 'mockup', 'width', 'aspect', 'bg', 'signature']) {
|
|
18
18
|
expect(isKnownPublicParam(name), name).toBe(true)
|
|
19
19
|
}
|
|
20
20
|
// The exported list matches the grammar exactly (no extras, no omissions).
|
|
21
21
|
expect([...PUBLIC_PARAM_NAMES].sort()).toEqual(
|
|
22
|
-
['aspect', 'asset', 'design', 'mockup', 'page', 'placement', 'shop', 'signature', 'variant', 'width'],
|
|
22
|
+
['aspect', 'asset', 'bg', 'design', 'mockup', 'page', 'placement', 'shop', 'signature', 'variant', 'width'],
|
|
23
23
|
)
|
|
24
24
|
})
|
|
25
25
|
|
|
@@ -69,6 +69,11 @@ describe('unknownPublicParams', () => {
|
|
|
69
69
|
expect(unknownPublicParams(url.searchParams)).toEqual([])
|
|
70
70
|
})
|
|
71
71
|
|
|
72
|
+
it('returns [] for a background override (bg=)', () => {
|
|
73
|
+
const url = new URL('https://img.snowcone.app/tee?asset=default&shop=abc123&bg=f5f0ea')
|
|
74
|
+
expect(unknownPublicParams(url.searchParams)).toEqual([])
|
|
75
|
+
})
|
|
76
|
+
|
|
72
77
|
it('names unknown keys, deduped, in first-seen order', () => {
|
|
73
78
|
const url = new URL(
|
|
74
79
|
'https://img.snowcone.app/tee?asset=x&view=a&shop=abc123&view=b&foo=1',
|
|
@@ -100,6 +105,10 @@ describe('unknownPublicParamsMessage', () => {
|
|
|
100
105
|
expect(unknownPublicParamsMessage(['key'])).toContain('shop=<shopId>')
|
|
101
106
|
})
|
|
102
107
|
|
|
108
|
+
it('tells `background=` users to use bg=', () => {
|
|
109
|
+
expect(unknownPublicParamsMessage(['background'])).toContain('bg=<6-hex>')
|
|
110
|
+
})
|
|
111
|
+
|
|
103
112
|
it('pluralizes and lists every offending key', () => {
|
|
104
113
|
const msg = unknownPublicParamsMessage(['view', 'foo'])
|
|
105
114
|
expect(msg).toContain('Unknown query parameters "view", "foo"')
|
package/test/resolve.test.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import {
|
|
3
|
+
assembleArtwork,
|
|
3
4
|
looksLikeResolveRequest,
|
|
4
5
|
normalizeAlbum,
|
|
5
6
|
resolveAlbumPagePlacement,
|
|
6
7
|
resolveVariant,
|
|
7
8
|
slugify,
|
|
9
|
+
type ResolvedPlacement,
|
|
8
10
|
type ResolvedProduct,
|
|
9
11
|
} from "../src/resolve";
|
|
10
12
|
|
|
@@ -267,3 +269,170 @@ describe("slugify", () => {
|
|
|
267
269
|
expect(slugify("Front Left")).toBe("front_left");
|
|
268
270
|
});
|
|
269
271
|
});
|
|
272
|
+
|
|
273
|
+
// ─── ADR-0089: base broadcast + per-placement override ───────────────────────
|
|
274
|
+
describe("assembleArtwork (base + override, ADR-0089)", () => {
|
|
275
|
+
const HOUSE = "https://img.snowcone.app/_blank.png";
|
|
276
|
+
|
|
277
|
+
// A three-placement product with distinct geometry per placement, so we can
|
|
278
|
+
// assert the base broadcast uses EACH placement's own width/height.
|
|
279
|
+
const shirt: ResolvedProduct = {
|
|
280
|
+
id: "SHIRT1",
|
|
281
|
+
variants: [{ gvid: "v1" }],
|
|
282
|
+
mockups: [],
|
|
283
|
+
placements: [
|
|
284
|
+
{ key: "front", label: "Front", type: "image", width: 4000, height: 5000 },
|
|
285
|
+
{ key: "back", label: "Back", type: "image", width: 3000, height: 6000 },
|
|
286
|
+
{ key: "sleeve", label: "Sleeve", type: "image", width: 800, height: 1200 },
|
|
287
|
+
],
|
|
288
|
+
defaultGvid: "v1",
|
|
289
|
+
attributes: {},
|
|
290
|
+
combinations: [],
|
|
291
|
+
album: null,
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
// Strict findPlacement, mirroring the resolver: exact catalog key + album
|
|
295
|
+
// page expansion (none here).
|
|
296
|
+
const find = (k: string): ResolvedPlacement | null =>
|
|
297
|
+
shirt.placements.find((p) => p.key === k) ?? null;
|
|
298
|
+
|
|
299
|
+
const ART = "https://cdn.example.com/art.png";
|
|
300
|
+
const OTHER = "https://cdn.example.com/other.png";
|
|
301
|
+
|
|
302
|
+
it("scalar asset broadcasts the base to EVERY placement, each with its own geometry", () => {
|
|
303
|
+
const r = assembleArtwork(shirt, {
|
|
304
|
+
asset: ART,
|
|
305
|
+
design: {},
|
|
306
|
+
house: HOUSE,
|
|
307
|
+
findPlacement: find,
|
|
308
|
+
});
|
|
309
|
+
expect(r.ok).toBe(true);
|
|
310
|
+
if (!r.ok) return;
|
|
311
|
+
expect(r.elements).toHaveLength(3);
|
|
312
|
+
// Each placement gets the base art at ITS OWN geometry (not the primary's).
|
|
313
|
+
expect(r.elements).toEqual([
|
|
314
|
+
{ type: "image", imageUrl: ART, placement: "Front", width: 4000, height: 5000, alignment: "center" },
|
|
315
|
+
{ type: "image", imageUrl: ART, placement: "Back", width: 3000, height: 6000, alignment: "center" },
|
|
316
|
+
{ type: "image", imageUrl: ART, placement: "Sleeve", width: 800, height: 1200, alignment: "center" },
|
|
317
|
+
]);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("a keyed override fills ONLY the named placement (no base → others blank)", () => {
|
|
321
|
+
const r = assembleArtwork(shirt, {
|
|
322
|
+
asset: null,
|
|
323
|
+
design: { back: { src: OTHER } },
|
|
324
|
+
house: HOUSE,
|
|
325
|
+
findPlacement: find,
|
|
326
|
+
});
|
|
327
|
+
expect(r.ok).toBe(true);
|
|
328
|
+
if (!r.ok) return;
|
|
329
|
+
expect(r.elements).toEqual([
|
|
330
|
+
{ type: "image", imageUrl: OTHER, placement: "Back", width: 3000, height: 6000, alignment: "center" },
|
|
331
|
+
]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("base + override COMPOSES: base everywhere, override replaces just its placement", () => {
|
|
335
|
+
const r = assembleArtwork(shirt, {
|
|
336
|
+
asset: ART,
|
|
337
|
+
design: { back: { src: OTHER } },
|
|
338
|
+
house: HOUSE,
|
|
339
|
+
findPlacement: find,
|
|
340
|
+
});
|
|
341
|
+
expect(r.ok).toBe(true);
|
|
342
|
+
if (!r.ok) return;
|
|
343
|
+
const byLabel = Object.fromEntries(r.elements.map((e) => [e.placement, e]));
|
|
344
|
+
expect(byLabel.Front.imageUrl).toBe(ART);
|
|
345
|
+
expect(byLabel.Sleeve.imageUrl).toBe(ART);
|
|
346
|
+
// Back is overridden, and keeps Back's own geometry.
|
|
347
|
+
expect(byLabel.Back).toEqual({
|
|
348
|
+
type: "image", imageUrl: OTHER, placement: "Back", width: 3000, height: 6000, alignment: "center",
|
|
349
|
+
});
|
|
350
|
+
expect(r.elements).toHaveLength(3);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it("asset=default blanks ALL placements as the base (house placeholder)", () => {
|
|
354
|
+
const r = assembleArtwork(shirt, {
|
|
355
|
+
asset: "default",
|
|
356
|
+
design: {},
|
|
357
|
+
house: HOUSE,
|
|
358
|
+
findPlacement: find,
|
|
359
|
+
});
|
|
360
|
+
expect(r.ok).toBe(true);
|
|
361
|
+
if (!r.ok) return;
|
|
362
|
+
expect(r.elements).toHaveLength(3);
|
|
363
|
+
expect(r.elements.every((e) => e.imageUrl === HOUSE)).toBe(true);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it("asset.<key>=default blanks ONE placement while the others keep the base", () => {
|
|
367
|
+
const r = assembleArtwork(shirt, {
|
|
368
|
+
asset: ART,
|
|
369
|
+
design: { sleeve: { src: "default" } },
|
|
370
|
+
house: HOUSE,
|
|
371
|
+
findPlacement: find,
|
|
372
|
+
});
|
|
373
|
+
expect(r.ok).toBe(true);
|
|
374
|
+
if (!r.ok) return;
|
|
375
|
+
const byLabel = Object.fromEntries(r.elements.map((e) => [e.placement, e]));
|
|
376
|
+
expect(byLabel.Front.imageUrl).toBe(ART);
|
|
377
|
+
expect(byLabel.Back.imageUrl).toBe(ART);
|
|
378
|
+
expect(byLabel.Sleeve.imageUrl).toBe(HOUSE); // house-blanked
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("carries align + tile from a positioned override fill", () => {
|
|
382
|
+
const r = assembleArtwork(shirt, {
|
|
383
|
+
asset: null,
|
|
384
|
+
design: { front: { src: ART, align: "top", tile: 2 } },
|
|
385
|
+
house: HOUSE,
|
|
386
|
+
findPlacement: find,
|
|
387
|
+
});
|
|
388
|
+
expect(r.ok).toBe(true);
|
|
389
|
+
if (!r.ok) return;
|
|
390
|
+
expect(r.elements[0]).toEqual({
|
|
391
|
+
type: "image", imageUrl: ART, placement: "Front", width: 4000, height: 5000, alignment: "top", tiles: 2,
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it("a color override emits a color element on its placement", () => {
|
|
396
|
+
const r = assembleArtwork(shirt, {
|
|
397
|
+
asset: null,
|
|
398
|
+
design: { front: { color: "#ff0000" } },
|
|
399
|
+
house: HOUSE,
|
|
400
|
+
findPlacement: find,
|
|
401
|
+
});
|
|
402
|
+
expect(r.ok).toBe(true);
|
|
403
|
+
if (!r.ok) return;
|
|
404
|
+
expect(r.elements[0]).toEqual({
|
|
405
|
+
type: "color", hex: "#ff0000", placement: "Front", width: 4000, height: 5000,
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("an unknown override key fails loudly (E_PLACEMENT_NOT_FOUND) and LISTS valid keys", () => {
|
|
410
|
+
const r = assembleArtwork(shirt, {
|
|
411
|
+
asset: ART,
|
|
412
|
+
design: { chest: { src: OTHER } },
|
|
413
|
+
house: HOUSE,
|
|
414
|
+
findPlacement: find,
|
|
415
|
+
});
|
|
416
|
+
expect(r.ok).toBe(false);
|
|
417
|
+
if (r.ok) return;
|
|
418
|
+
expect(r.code).toBe("E_PLACEMENT_NOT_FOUND");
|
|
419
|
+
expect(r.status).toBe(400);
|
|
420
|
+
expect(r.message).toContain('"chest"');
|
|
421
|
+
// Names every valid placement key for the product.
|
|
422
|
+
expect(r.message).toContain("front");
|
|
423
|
+
expect(r.message).toContain("back");
|
|
424
|
+
expect(r.message).toContain("sleeve");
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it("no base and no overrides → no elements (caller 400s on empty)", () => {
|
|
428
|
+
const r = assembleArtwork(shirt, {
|
|
429
|
+
asset: null,
|
|
430
|
+
design: {},
|
|
431
|
+
house: HOUSE,
|
|
432
|
+
findPlacement: find,
|
|
433
|
+
});
|
|
434
|
+
expect(r.ok).toBe(true);
|
|
435
|
+
if (!r.ok) return;
|
|
436
|
+
expect(r.elements).toHaveLength(0);
|
|
437
|
+
});
|
|
438
|
+
});
|
package/wrangler.jsonc
CHANGED
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
// MEILISEARCH_API_KEY is the public read-only search key (same as apps/www);
|
|
36
36
|
// URL_SIGNING_SECRET + KV_CACHE_SERVICE_TOKEN stay secrets (wrangler secret put).
|
|
37
37
|
"MEILISEARCH_HOST": "https://search.snowcone.app",
|
|
38
|
-
"MEILISEARCH_API_KEY": "
|
|
38
|
+
"MEILISEARCH_API_KEY": "73f573ead9be75531c56ee89d104caa49d79154db387f119a013d33eccfe654b",
|
|
39
39
|
"MEILISEARCH_INDEX": "snowcone",
|
|
40
40
|
"MOCKUP_BASE_URL": "https://cdn.snowcone.app",
|
|
41
41
|
"KV_CACHE_BASE_URL": "https://api.snowcone.app",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"TTL_DEFAULT": "3600",
|
|
64
64
|
"LOG_LEVEL": "debug",
|
|
65
65
|
"MEILISEARCH_HOST": "https://search.snowcone.app",
|
|
66
|
-
"MEILISEARCH_API_KEY": "
|
|
66
|
+
"MEILISEARCH_API_KEY": "73f573ead9be75531c56ee89d104caa49d79154db387f119a013d33eccfe654b",
|
|
67
67
|
"MEILISEARCH_INDEX": "snowcone-dev",
|
|
68
68
|
"MOCKUP_BASE_URL": "https://cdn.snowcone.app",
|
|
69
69
|
"KV_CACHE_BASE_URL": "http://localhost:4000",
|