@tokenoftrust/cli 8.2.0 → 8.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/cli",
3
- "version": "8.2.0",
3
+ "version": "8.4.0",
4
4
  "description": "Token of Trust developer CLI — clone a tenant store, run it locally with save→reload, and submit it for preview. Installs the `tot` command.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Token of Trust",
@@ -31,6 +31,9 @@ export const MAX_SHIPPING_AMOUNT: number;
31
31
  export const MAX_SHIPPING_TIERS: number;
32
32
  export const MAX_SHIPPING_LABEL_LENGTH: number;
33
33
  export function shippingDeclarationProblems(shipping: unknown): string[];
34
+ export const COMMERCE_KEYS: readonly string[];
35
+ export const ORDER_NUMBERING_KEYS: readonly string[];
36
+ export function commerceDeclarationProblems(commerce: unknown): string[];
34
37
  export const COMPLIANCE_NOTICES: Record<string, string>;
35
38
  export const MANDATED_NOTICE_KEYS: string[];
36
39
  export function nearestKey(key: string, known: readonly string[]): string | null;
@@ -19,7 +19,7 @@
19
19
 
20
20
  export const TOT_CONFIG_KEYS = [
21
21
  "$schema", "tenant", "scope", "mappings", "hostPlatform", "sample", "seed",
22
- "displayName", "siteType", "compliance", "capabilities", "features", "embeds", "seo", "currency", "timezone", "shipping", "catalog", "accessPolicy", "verification",
22
+ "displayName", "siteType", "compliance", "capabilities", "features", "embeds", "seo", "currency", "timezone", "shipping", "catalog", "commerce", "accessPolicy", "verification",
23
23
  ];
24
24
  // Keys other tooling has written that the platform does NOT read, with the key it does.
25
25
  const RENAMED_CONFIG_KEYS = { appDomain: "scope", tenantId: "tenant" };
@@ -324,6 +324,67 @@ function catalogProblemMessage(problems) {
324
324
  "Owned catalog images stay refused until it is fixed.";
325
325
  }
326
326
 
327
+ /** Every `commerce` key a tenant may declare in `.tot/config.json`. Platform checkout/program fields stay outside. */
328
+ export const COMMERCE_KEYS = Object.freeze(["orderNumbering"]);
329
+
330
+ /** Every `commerce.orderNumbering` key (mirrors packages/public-runtime/src/declared-tenant-config.mjs). */
331
+ export const ORDER_NUMBERING_KEYS = Object.freeze(["startAt", "prefix", "padWidth"]);
332
+
333
+ const ORDER_NUMBER_PREFIX_RE = /^[A-Za-z0-9-]*$/;
334
+ const ORDER_NUMBER_MAX_PREFIX_LENGTH = 32;
335
+ const ORDER_NUMBER_MAX_PAD_WIDTH = 16;
336
+
337
+ /**
338
+ * What is wrong with a declared `commerce` value, one sentence per problem; `[]` when it is valid.
339
+ * @param {unknown} commerce
340
+ * @returns {string[]}
341
+ */
342
+ export function commerceDeclarationProblems(commerce) {
343
+ if (!isObject(commerce)) return ["`commerce` must be an object"];
344
+ const source = /** @type {Record<string, unknown>} */ (commerce);
345
+ const problems = [];
346
+ for (const key of Object.keys(source)) {
347
+ if (!COMMERCE_KEYS.includes(key)) {
348
+ problems.push(`\`commerce.${key}\` is not a commerce key (${COMMERCE_KEYS.join(", ")})`);
349
+ }
350
+ }
351
+ const on = source.orderNumbering;
352
+ if (on !== undefined) {
353
+ if (!isObject(on)) {
354
+ problems.push("`commerce.orderNumbering` must be an object");
355
+ } else {
356
+ const rec = /** @type {Record<string, unknown>} */ (on);
357
+ for (const key of Object.keys(rec)) {
358
+ if (!ORDER_NUMBERING_KEYS.includes(key)) {
359
+ problems.push(`\`commerce.orderNumbering.${key}\` is not an orderNumbering key (${ORDER_NUMBERING_KEYS.join(", ")})`);
360
+ }
361
+ }
362
+ if (typeof rec.startAt !== "number" || !Number.isSafeInteger(rec.startAt) || rec.startAt < 1) {
363
+ problems.push("`commerce.orderNumbering.startAt` must be a positive safe integer");
364
+ }
365
+ if (
366
+ rec.prefix !== undefined &&
367
+ (typeof rec.prefix !== "string" || rec.prefix.length > ORDER_NUMBER_MAX_PREFIX_LENGTH || !ORDER_NUMBER_PREFIX_RE.test(rec.prefix))
368
+ ) {
369
+ problems.push(`\`commerce.orderNumbering.prefix\` must be empty or match /^[A-Za-z0-9-]+$/ (max ${ORDER_NUMBER_MAX_PREFIX_LENGTH} chars)`);
370
+ }
371
+ if (
372
+ rec.padWidth !== undefined &&
373
+ (typeof rec.padWidth !== "number" || !Number.isSafeInteger(rec.padWidth) || rec.padWidth < 0 || rec.padWidth > ORDER_NUMBER_MAX_PAD_WIDTH)
374
+ ) {
375
+ problems.push(`\`commerce.orderNumbering.padWidth\` must be an integer between 0 and ${ORDER_NUMBER_MAX_PAD_WIDTH}`);
376
+ }
377
+ }
378
+ }
379
+ return problems;
380
+ }
381
+
382
+ /** The owner-facing sentence for an invalid `commerce` declaration. */
383
+ function commerceProblemMessage(problems) {
384
+ return `\`commerce\` is not a valid commerce declaration (${problems.join("; ")}). ` +
385
+ "Order numbering uses the default sequence until it is fixed.";
386
+ }
387
+
327
388
  /** Every `shipping` key (mirrors packages/public-runtime/src/declared-tenant-config.mjs). */
328
389
  export const SHIPPING_KEYS = Object.freeze(["tiers"]);
329
390
 
@@ -805,6 +866,8 @@ export function declarationFindings(current, previous) {
805
866
  if (shipping.length > 0) out.push(`CONFIG: ${shippingProblemMessage(shipping)}`);
806
867
  const catalog = current.catalog === undefined ? [] : catalogDeclarationProblems(current.catalog);
807
868
  if (catalog.length > 0) out.push(`CONFIG: ${catalogProblemMessage(catalog)}`);
869
+ const commerce = current.commerce === undefined ? [] : commerceDeclarationProblems(current.commerce);
870
+ if (commerce.length > 0) out.push(`CONFIG: ${commerceProblemMessage(commerce)}`);
808
871
  }
809
872
  for (const { key, notice } of droppedMandatedNotices(previous, current)) {
810
873
  out.push(
@@ -922,6 +985,13 @@ export function validateDeclaredConfig(config, file = ".tot/config.json") {
922
985
  'declare `"catalog": { "cdnSegment": "shop" }` as one lowercase DNS label, or remove `catalog`'));
923
986
  }
924
987
  }
988
+ if (config.commerce !== undefined) {
989
+ const problems = commerceDeclarationProblems(config.commerce);
990
+ if (problems.length > 0) {
991
+ out.push(mk(ERROR, "config-commerce", `${file} commerce`, commerceProblemMessage(problems),
992
+ 'declare `"commerce": { "orderNumbering": { "startAt": 270101 } }`, or remove `commerce`'));
993
+ }
994
+ }
925
995
  return out;
926
996
  }
927
997
 
@@ -105,12 +105,39 @@ const STRING_FACTS = new Set([
105
105
  const STRING_LIST_FACTS = new Set(["flavor_labels", "registry_listings"]);
106
106
 
107
107
  /**
108
- * Regulatory fact declarations on catalog products (`variants[].catalog_link.facts`, storefront-catalog
109
- * v2.1.0): a known fact carries a value of its key's type, any other state carries none, and a key is
110
- * declared once per variant. Errors, because checkout reads these as the primary eligibility truth.
108
+ * Reject foreign-owned catalog attributes (`catalog_link.facts` or `catalog_link.identifiers`)
109
+ * committed into tenant catalog files (ADR 0048). Merchant git stores pointer-only links;
110
+ * Storefront hydrates Layer 1 facts from `product-catalog` into derived projections.
111
111
  * @param {Array<{ file: string, record: any }>} products
112
112
  */
113
113
  export function catalogFactFindings(products) {
114
+ const out = [];
115
+ for (const { file, record } of products) {
116
+ const variants = Array.isArray(record?.variants) ? record.variants : [];
117
+ variants.forEach((variant, vi) => {
118
+ const link = variant?.catalog_link;
119
+ if (!isObject(link)) return;
120
+ if (link.facts !== undefined || link.identifiers !== undefined || link.facts_state !== undefined) {
121
+ out.push({
122
+ level: "error",
123
+ rule: "catalog-link-committed-facts",
124
+ file: `${file} variants[${vi}].catalog_link`,
125
+ message: "Tenant catalog files must not commit catalog_link.facts, catalog_link.identifiers, or catalog_link.facts_state (ADR 0048).",
126
+ fix: "Store only the catalog_link pointer (item_id, accepted_revision_id); Storefront hydrates facts from product-catalog during reconcile and publish.",
127
+ });
128
+ }
129
+ });
130
+ }
131
+ return out;
132
+ }
133
+
134
+ /**
135
+ * Validate hydrated regulatory fact arrays (`variants[].catalog_link.facts`, storefront-catalog
136
+ * v2.1.0): a known fact carries a value of its key's type, any other state carries none, and a
137
+ * key is declared once per variant.
138
+ * @param {Array<{ file: string, record: any }>} products
139
+ */
140
+ export function catalogHydratedFactFindings(products) {
114
141
  const out = [];
115
142
  for (const { file, record } of products) {
116
143
  const variants = Array.isArray(record?.variants) ? record.variants : [];
@@ -120,7 +147,7 @@ export function catalogFactFindings(products) {
120
147
  const at = `variants[${vi}].catalog_link.facts`;
121
148
  const err = (rule, i, message, fix) => out.push({ level: "error", rule, file: `${file} ${at}[${i}]`, message, fix });
122
149
  if (!Array.isArray(facts)) {
123
- out.push({ level: "error", rule: "catalog-fact-shape", file: `${file} ${at}`, message: "`catalog_link.facts` must be an array.", fix: "Re-accept the catalog item to rewrite its facts." });
150
+ out.push({ level: "error", rule: "catalog-fact-shape", file: `${file} ${at}`, message: "`catalog_link.facts` must be an array.", fix: "Re-hydrate the catalog facts projection from product-catalog." });
124
151
  return;
125
152
  }
126
153
  const seen = new Set();
package/src/validate.mjs CHANGED
@@ -157,14 +157,26 @@ function collectCatalogFlavorLabels(product, variant) {
157
157
  const catalogFactOf = (variant, key) =>
158
158
  (Array.isArray(variant?.catalog_link?.facts) ? variant.catalog_link.facts : []).find((f) => f?.key === key);
159
159
 
160
+ /** True when a variant carries a valid pointer-only `catalog_link` pin (ADR 0048). */
161
+ const hasCatalogLinkPointer = (variant) =>
162
+ Boolean(
163
+ variant?.catalog_link &&
164
+ typeof variant.catalog_link === "object" &&
165
+ typeof variant.catalog_link.item_id === "string" &&
166
+ variant.catalog_link.item_id.trim().length > 0 &&
167
+ typeof variant.catalog_link.accepted_revision_id === "string" &&
168
+ variant.catalog_link.accepted_revision_id.trim().length > 0,
169
+ );
170
+
160
171
  /** A catalog fact settles a field unless it is unknown; conflicting/invalid facts settle it restrictively. */
161
172
  const factDecides = (fact) => fact !== undefined && fact.state !== "unknown";
162
173
 
163
174
  /**
164
- * The classification a product still lacks. The accepted catalog facts
165
- * (`variants[].catalog_link.facts`) are the primary truth; merchant fields are the secondary
166
- * declaration. Nothing is inferred from a title, tag or product type: a product whose category is
167
- * not recorded is unclassified, and a nicotine-bearing one needs a flavour reading.
175
+ * The classification a product still lacks. A variant carrying a valid pointer-only `catalog_link`
176
+ * receives its Layer 1 facts via derived hydration (ADR 0048) and does not trigger an offline gap.
177
+ * For unlinked variants (or hydrated records), accepted catalog facts are the primary truth and
178
+ * merchant fields are the secondary declaration; nothing is inferred from a title, tag, or
179
+ * product type.
168
180
  */
169
181
  function productClassificationGaps(product) {
170
182
  if (!product || typeof product !== "object") return [];
@@ -174,6 +186,7 @@ function productClassificationGaps(product) {
174
186
  const variants = Array.isArray(product.variants) && product.variants.length > 0 ? product.variants : [null];
175
187
  const gaps = new Set();
176
188
  for (const variant of variants) {
189
+ if (hasCatalogLinkPointer(variant) && !Array.isArray(variant.catalog_link.facts)) continue;
177
190
  const categoryFact = catalogFactOf(variant, "compliance_category");
178
191
  const category = categoryFact?.state === "known" && typeof categoryFact.value === "string" ? categoryFact.value : explicitCategory;
179
192
  const categoryClassified = factDecides(categoryFact) || Boolean(category);