@tokenoftrust/storefront-runner 6.11.1 → 6.11.3
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/apps/storefront/src/components/content/storefront/home/Hero.astro +1 -1
- package/apps/storefront/src/lib/checkout/commerce/commerceAdmission.ts +6 -0
- package/apps/storefront/src/lib/checkout/commerce/finalCartAuthorization.ts +52 -1
- package/apps/storefront/src/lib/checkout/commerce/sharedStoreAdmission.ts +14 -8
- package/apps/storefront/src/pages/api/commerce/final-cart-authorization.ts +19 -7
- package/package.json +1 -1
- package/packages/public-runtime/src/checkout-orders.ts +23 -3
- package/scripts/tenant/validate.mjs +62 -1
|
@@ -209,7 +209,7 @@ const notice = block.notice?.trim();
|
|
|
209
209
|
)
|
|
210
210
|
}
|
|
211
211
|
<Heading class="display-hero">
|
|
212
|
-
<span class:list={["block", light
|
|
212
|
+
<span class:list={["block", light ? "text-[var(--color-primary)]" : "text-[var(--color-primary-contrast)]"]}>{block.headline}</span>
|
|
213
213
|
{
|
|
214
214
|
block.headlineAccent && (
|
|
215
215
|
<span class:list={["relative block w-fit", light ? "text-[var(--color-accent-ink)]" : "text-[var(--color-accent)]"]}>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Trusted admission is written in the reservation transaction, never reconstructed from a paid callback. */
|
|
2
2
|
import {
|
|
3
3
|
canonicalJson,
|
|
4
|
+
totalsMatch,
|
|
4
5
|
type CommerceAdmissionSnapshot,
|
|
5
6
|
type CommerceStage,
|
|
6
7
|
type MinorTotals,
|
|
@@ -13,6 +14,8 @@ export function admissionSnapshotStatement(
|
|
|
13
14
|
if (
|
|
14
15
|
!/^[a-f0-9]{64}$/.test(input.providerCartDigest) ||
|
|
15
16
|
!/^[a-f0-9]{64}$/.test(input.destinationDigest) ||
|
|
17
|
+
!input.totals ||
|
|
18
|
+
!totalsMatch(input.totals) ||
|
|
16
19
|
Object.values(input.provenance).some(
|
|
17
20
|
(v) => !v || !v.source || !v.policyRevision || !Number.isFinite(Date.parse(v.expiresAt)),
|
|
18
21
|
) ||
|
|
@@ -91,6 +94,7 @@ function parseCommerceAdmission(
|
|
|
91
94
|
!Array.isArray(snapshot.lines) ||
|
|
92
95
|
!snapshot.lines.length ||
|
|
93
96
|
!snapshot.totals ||
|
|
97
|
+
!totalsMatch(snapshot.totals) ||
|
|
94
98
|
!snapshot.evidence ||
|
|
95
99
|
!snapshot.provenance ||
|
|
96
100
|
!/^[a-f0-9]{64}$/.test(snapshot.providerCartDigest) ||
|
|
@@ -128,6 +132,8 @@ export function admissionMatches(
|
|
|
128
132
|
const sort = (lines: readonly { sku: string; quantity: number; unitMinor: number }[]) =>
|
|
129
133
|
[...lines].sort((a, b) => a.sku.localeCompare(b.sku));
|
|
130
134
|
return (
|
|
135
|
+
totalsMatch(snapshot.totals) &&
|
|
136
|
+
totalsMatch(event.totals) &&
|
|
131
137
|
event.providerCartDigest === snapshot.providerCartDigest &&
|
|
132
138
|
event.destinationDigest === snapshot.destinationDigest &&
|
|
133
139
|
event.currency === snapshot.currency &&
|
|
@@ -12,18 +12,35 @@ import {
|
|
|
12
12
|
finalCartDigest,
|
|
13
13
|
finalCartStillBound,
|
|
14
14
|
sumMinor,
|
|
15
|
+
totalsMatch,
|
|
15
16
|
type CallerToken,
|
|
16
17
|
type CommerceEvidence,
|
|
17
18
|
type CommerceStage,
|
|
18
19
|
type EvidenceState,
|
|
20
|
+
type ExciseCarrier,
|
|
19
21
|
type FinalCartBinding,
|
|
20
22
|
type FulfillmentState,
|
|
21
23
|
type LabelState,
|
|
24
|
+
type MinorTotals,
|
|
22
25
|
type PaymentState,
|
|
23
26
|
type StockState,
|
|
24
27
|
} from "@tot/public-runtime";
|
|
25
28
|
import { readMirrorAuthority, VERIFICATION_PROVIDER_OWNER, type ReserveHold } from "@/lib/inventory/allocations.js";
|
|
26
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Whether the signed excise carrier reconciles with the signed payment totals.
|
|
32
|
+
* When `totals.exciseMinor` is present, the carrier must be `computed` with `amountMinor`
|
|
33
|
+
* equal to `totals.exciseMinor`. When `totals.exciseMinor` is absent (four totals keys, no
|
|
34
|
+
* coupon), a `computed` carrier reconciles only when `amountMinor` is `0`.
|
|
35
|
+
*/
|
|
36
|
+
export function exciseReconcilesWithTotals(totals: MinorTotals, excise?: ExciseCarrier): boolean {
|
|
37
|
+
if (!totalsMatch(totals)) return false;
|
|
38
|
+
if (totals.exciseMinor !== undefined) {
|
|
39
|
+
return excise?.status === "computed" && excise.amountMinor === totals.exciseMinor;
|
|
40
|
+
}
|
|
41
|
+
return excise?.status !== "computed" || excise.amountMinor === 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
27
44
|
export interface SeamView {
|
|
28
45
|
status: "clear" | "absent" | "blocked";
|
|
29
46
|
failedClosed?: boolean;
|
|
@@ -64,6 +81,9 @@ export interface FinalCartRequest {
|
|
|
64
81
|
stock?: StockState;
|
|
65
82
|
fulfillment?: FulfillmentState;
|
|
66
83
|
label?: LabelState;
|
|
84
|
+
totals?: MinorTotals;
|
|
85
|
+
excise?: ExciseCarrier;
|
|
86
|
+
exciseRequired?: boolean;
|
|
67
87
|
}
|
|
68
88
|
|
|
69
89
|
export type FinalCartDecision =
|
|
@@ -128,6 +148,24 @@ function seamFinding(seam: SeamView | null): { finding: string; owner?: string }
|
|
|
128
148
|
return { finding: codes[0] ? `verification-refused:${codes[0]}` : "verification-refused" };
|
|
129
149
|
}
|
|
130
150
|
|
|
151
|
+
function exciseFinding(
|
|
152
|
+
exciseRequired: boolean | undefined,
|
|
153
|
+
excise: ExciseCarrier | undefined,
|
|
154
|
+
evidence: CommerceEvidence | null,
|
|
155
|
+
): string | null {
|
|
156
|
+
if (!exciseRequired) return null;
|
|
157
|
+
if (
|
|
158
|
+
!excise ||
|
|
159
|
+
excise.status !== "computed" ||
|
|
160
|
+
!Number.isSafeInteger(excise.amountMinor) ||
|
|
161
|
+
excise.amountMinor < 0 ||
|
|
162
|
+
evidence?.tax !== "fresh"
|
|
163
|
+
) {
|
|
164
|
+
return "evidence-tax-absent";
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
|
|
131
169
|
function isCatalogShape(value: unknown): boolean {
|
|
132
170
|
return value == null || (typeof value === "object" && value !== null && "available" in value && !("provider" in value));
|
|
133
171
|
}
|
|
@@ -193,7 +231,17 @@ export async function evaluateFinalCart(input: FinalCartRequest): Promise<FinalC
|
|
|
193
231
|
assertMinor(line.quantity, "quantity");
|
|
194
232
|
assertMinor(line.unitMinor, "unitMinor");
|
|
195
233
|
}
|
|
196
|
-
|
|
234
|
+
const lineSubtotal = sumMinor(input.presented.lines.map((line) => line.unitMinor * line.quantity));
|
|
235
|
+
if (input.totals) {
|
|
236
|
+
if (
|
|
237
|
+
!totalsMatch(input.totals) ||
|
|
238
|
+
input.totals.subtotalMinor !== lineSubtotal ||
|
|
239
|
+
!exciseReconcilesWithTotals(input.totals, input.excise)
|
|
240
|
+
) {
|
|
241
|
+
return { ok: false, finding: "malformed-request" };
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
amountMinor = lineSubtotal;
|
|
197
245
|
digest = await finalCartDigest(input.presented);
|
|
198
246
|
const quotedDigest = await finalCartDigest(input.quoted);
|
|
199
247
|
if (!finalCartStillBound(quotedDigest, digest, input.now, expiresAt)) return { ok: false, finding: "cart-unbound" };
|
|
@@ -204,6 +252,9 @@ export async function evaluateFinalCart(input: FinalCartRequest): Promise<FinalC
|
|
|
204
252
|
const seam = seamFinding(input.seam);
|
|
205
253
|
if (seam) return { ok: false, finding: seam.finding, ...(seam.owner ? { owner: seam.owner } : {}) };
|
|
206
254
|
|
|
255
|
+
const excise = exciseFinding(input.exciseRequired, input.excise, input.evidence);
|
|
256
|
+
if (excise) return { ok: false, finding: excise };
|
|
257
|
+
|
|
207
258
|
const command = decideCommerceCommand({
|
|
208
259
|
command: "authorize-final-cart",
|
|
209
260
|
payment: input.payment ?? "pending",
|
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
import {
|
|
11
11
|
commerceTenantId,
|
|
12
12
|
isRegulated,
|
|
13
|
+
resolveComplianceObligations,
|
|
13
14
|
SHARED_DEMO_CHECKOUT,
|
|
15
|
+
totalsMatch,
|
|
14
16
|
type CommerceAuthoritySource,
|
|
15
17
|
type CustomizationEnv,
|
|
16
18
|
type ExciseCarrier,
|
|
@@ -24,6 +26,9 @@ import { resolveAdminTargetTenant } from "@/config/adminTargetTenant";
|
|
|
24
26
|
import { resolveTenantCommerce } from "@/lib/checkout/shared/checkoutCommerce";
|
|
25
27
|
import { readEnv } from "@/lib/publication/shared/runtimeEnv";
|
|
26
28
|
import { readCheckoutRequirements, type CheckoutRequirements } from "./checkoutRequirements";
|
|
29
|
+
import { exciseReconcilesWithTotals } from "./finalCartAuthorization";
|
|
30
|
+
|
|
31
|
+
export { exciseReconcilesWithTotals };
|
|
27
32
|
|
|
28
33
|
export type SharedStoreFinding =
|
|
29
34
|
| "tenant-unregistered"
|
|
@@ -133,6 +138,14 @@ export interface ResolvedTaxEvidence {
|
|
|
133
138
|
provenance: CommerceAuthoritySource;
|
|
134
139
|
}
|
|
135
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Whether the tenant's resolved compliance obligations require excise tax collection at checkout.
|
|
143
|
+
* Derived purely from the authoritative tenant config resolved from the JWT-signed `tenantId`.
|
|
144
|
+
*/
|
|
145
|
+
export function exciseRequiredForTenant(tenant: Pick<TenantConfig, "compliance" | "siteType">): boolean {
|
|
146
|
+
return resolveComplianceObligations(tenant).exciseTax.required;
|
|
147
|
+
}
|
|
148
|
+
|
|
136
149
|
/**
|
|
137
150
|
* Pure tax-evidence resolver for a tax-required final cart.
|
|
138
151
|
* Marks tax evidence `computed` (`evidenceState: "fresh"`) only when the checkout store has a
|
|
@@ -178,14 +191,7 @@ export async function resolveTaxEvidence(input: ResolveTaxEvidenceInput): Promis
|
|
|
178
191
|
if (carrier.destinationDigest !== input.destinationDigest) {
|
|
179
192
|
return notComputed("unavailable:carrier-destination-mismatch", carrier.engineTier);
|
|
180
193
|
}
|
|
181
|
-
|
|
182
|
-
? Number.isSafeInteger(input.totals.exciseMinor) &&
|
|
183
|
-
input.totals.exciseMinor >= 0 &&
|
|
184
|
-
input.totals.subtotalMinor + input.totals.shippingMinor + input.totals.taxMinor + input.totals.exciseMinor === input.totals.totalMinor &&
|
|
185
|
-
carrier.amountMinor === input.totals.exciseMinor
|
|
186
|
-
: input.totals.subtotalMinor + input.totals.shippingMinor + input.totals.taxMinor === input.totals.totalMinor &&
|
|
187
|
-
carrier.amountMinor === input.totals.taxMinor;
|
|
188
|
-
if (!totalsReconcile) {
|
|
194
|
+
if (!exciseReconcilesWithTotals(input.totals, carrier)) {
|
|
189
195
|
return notComputed("unavailable:carrier-amount-unreconciled", carrier.engineTier);
|
|
190
196
|
}
|
|
191
197
|
return {
|
|
@@ -17,7 +17,7 @@ import { evaluatePreflight } from "../compliance/preflight.js";
|
|
|
17
17
|
import {
|
|
18
18
|
admitSharedStoreTenant, testStoreEvidencePolicy, SHARED_STORE_POLICY_REVISION, SHARED_STORE_POLICY_SOURCE,
|
|
19
19
|
SHARED_STORE_VERIFICATION_SOURCE, SHARED_STORE_SANDBOX_VERIFICATION_SOURCE, SANDBOX_CLEARED_REF,
|
|
20
|
-
checkoutStoreHasExciseProducer, exciseLinesDigest, resolveTaxEvidence, type SharedStoreEvidencePolicy,
|
|
20
|
+
checkoutStoreHasExciseProducer, exciseLinesDigest, exciseReconcilesWithTotals, exciseRequiredForTenant, resolveTaxEvidence, type SharedStoreEvidencePolicy,
|
|
21
21
|
} from "@/lib/checkout/commerce/sharedStoreAdmission.js";
|
|
22
22
|
import { resolveTenantCommerce } from "@/lib/checkout/shared/checkoutCommerce";
|
|
23
23
|
import { isSandboxVerificationProvider } from "@/lib/compliance/enforcement.js";
|
|
@@ -69,7 +69,7 @@ async function digest(value: string): Promise<string> {
|
|
|
69
69
|
* verified result for this cart. `amountMinor` only when computed, `reason` only when not computed,
|
|
70
70
|
* from a closed vocabulary; no other key. Optional: a snapshot without it is read as before.
|
|
71
71
|
*/
|
|
72
|
-
export { EXCISE_REASONS, type ExciseCarrier, exciseLinesDigest, resolveTaxEvidence, checkoutStoreHasExciseProducer };
|
|
72
|
+
export { EXCISE_REASONS, type ExciseCarrier, exciseLinesDigest, exciseReconcilesWithTotals, exciseRequiredForTenant, resolveTaxEvidence, checkoutStoreHasExciseProducer };
|
|
73
73
|
type FinalCartWire = CommerceProviderCartSnapshot & { excise?: ExciseCarrier };
|
|
74
74
|
export function readExcise(value: unknown): ExciseCarrier | null {
|
|
75
75
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
@@ -82,6 +82,11 @@ export function readExcise(value: unknown): ExciseCarrier | null {
|
|
|
82
82
|
if (e.status === "computed" ? !Number.isSafeInteger(e.amountMinor) || Number(e.amountMinor) < 0 : !(EXCISE_REASONS as readonly unknown[]).includes(e.reason)) return null;
|
|
83
83
|
return e as unknown as ExciseCarrier;
|
|
84
84
|
}
|
|
85
|
+
/** A computed excise must have been computed over exactly this snapshot's lines and destination. */
|
|
86
|
+
async function exciseBindsSnapshot(wire: FinalCartWire): Promise<boolean> {
|
|
87
|
+
if (wire.excise?.status !== "computed") return true;
|
|
88
|
+
return wire.excise.destinationDigest === wire.destinationDigest && wire.excise.linesDigest === await exciseLinesDigest(wire.lines);
|
|
89
|
+
}
|
|
85
90
|
function readSnapshot(value: unknown): FinalCartWire | null {
|
|
86
91
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
87
92
|
const v = value as Record<string, unknown>;
|
|
@@ -99,13 +104,18 @@ function readSnapshot(value: unknown): FinalCartWire | null {
|
|
|
99
104
|
if (v.lines.some(line => !line || typeof line !== "object" || skuFromToken(line.sku) === null ||
|
|
100
105
|
!Number.isSafeInteger(line.quantity) || line.quantity < 1 || line.quantity > 99 || !Number.isSafeInteger(line.unitMinor) || line.unitMinor < 0 ||
|
|
101
106
|
Object.keys(line).some(k => !["sku", "quantity", "unitMinor"].includes(k)))) return null;
|
|
102
|
-
|
|
103
|
-
|
|
107
|
+
const t = v.totals as Record<string, unknown> | undefined;
|
|
108
|
+
const totalKeys = ["subtotalMinor", "shippingMinor", "taxMinor", "totalMinor"];
|
|
109
|
+
if (!t || typeof t !== "object" || Array.isArray(t) ||
|
|
110
|
+
Object.keys(t).some(k => !totalKeys.includes(k) && k !== "exciseMinor") ||
|
|
111
|
+
totalKeys.some(k => !(k in t)) ||
|
|
112
|
+
Object.keys(t).some(k => !Number.isSafeInteger(t[k]) || Number(t[k]) < 0)) return null;
|
|
104
113
|
const d = v.destination as Record<string, unknown> | undefined;
|
|
105
114
|
if (!d || Object.keys(d).length !== 2 || typeof d.country !== "string" || !/^[A-Z]{2}$/.test(d.country) || typeof d.region !== "string" || !/^[A-Z0-9-]{1,8}$/.test(d.region)) return null;
|
|
106
115
|
const snapshot = v as unknown as FinalCartWire;
|
|
107
116
|
if (new Set(snapshot.lines.map(line => line.sku)).size !== snapshot.lines.length) return null;
|
|
108
|
-
if (!totalsMatch(snapshot.totals) || snapshot.
|
|
117
|
+
if (!totalsMatch(snapshot.totals) || !exciseReconcilesWithTotals(snapshot.totals, snapshot.excise) ||
|
|
118
|
+
snapshot.lines.reduce((sum, line) => sum + line.quantity * line.unitMinor, 0) !== snapshot.totals.subtotalMinor) return null;
|
|
109
119
|
return snapshot;
|
|
110
120
|
}
|
|
111
121
|
/**
|
|
@@ -224,7 +234,7 @@ async function authorize(context: APIContext): Promise<Response> {
|
|
|
224
234
|
if (raw.length > 65536) return deny("malformed-request");
|
|
225
235
|
let wire: FinalCartWire | null;
|
|
226
236
|
try { wire = readSnapshot(JSON.parse(raw)); } catch { wire = null; }
|
|
227
|
-
if (!wire) return deny("malformed-request");
|
|
237
|
+
if (!wire || !await exciseBindsSnapshot(wire)) return deny("malformed-request");
|
|
228
238
|
const snapshot = keyedLines(wire);
|
|
229
239
|
const jwks = publicJwks(await readEnv("COMMERCE_CALLER_JWKS"));
|
|
230
240
|
const issuers = list(await readEnv("COMMERCE_CALLER_ISSUERS"));
|
|
@@ -380,9 +390,11 @@ async function authorize(context: APIContext): Promise<Response> {
|
|
|
380
390
|
evidence.verification = "fresh";
|
|
381
391
|
provenance.verification = { requirement: "required", state: "satisfied", source: seamView.sandbox ? SHARED_STORE_SANDBOX_VERIFICATION_SOURCE : SHARED_STORE_VERIFICATION_SOURCE, policyRevision: SHARED_STORE_POLICY_REVISION, expiresAt };
|
|
382
392
|
}
|
|
393
|
+
const exciseRequired = exciseRequiredForTenant(tenant);
|
|
383
394
|
const decision = await evaluateFinalCart({ now, routedTenantId: commerceTenantId(tenant), quoted: presented, presented, evidence,
|
|
384
395
|
seam: seamView, catalogAvailable: true, browserConfirmed: false, token,
|
|
385
|
-
trustedIssuers: issuers, expectedAudience: audience, activeKids, stockTracked, authorities
|
|
396
|
+
trustedIssuers: issuers, expectedAudience: audience, activeKids, stockTracked, authorities,
|
|
397
|
+
totals: snapshot.totals, excise: snapshot.excise, exciseRequired });
|
|
386
398
|
if (!decision.ok) {
|
|
387
399
|
if (decision.finding.startsWith("verification-refused") && (seamView.flavors || seamView.lines)) {
|
|
388
400
|
return json({ ok: false, finding: decision.finding, message: refusalSentence(decision.finding),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "6.11.
|
|
3
|
+
"version": "6.11.3",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
|
|
6
6
|
"packageManager": "pnpm@11.9.0",
|
|
@@ -320,6 +320,7 @@ export interface MinorTotals {
|
|
|
320
320
|
shippingMinor: number;
|
|
321
321
|
taxMinor: number;
|
|
322
322
|
totalMinor: number;
|
|
323
|
+
exciseMinor?: number;
|
|
323
324
|
}
|
|
324
325
|
|
|
325
326
|
export interface PublicCommerceEvent {
|
|
@@ -458,12 +459,27 @@ export function providerCartDigestMaterial(snapshot: {
|
|
|
458
459
|
contentVersionId?: string;
|
|
459
460
|
} {
|
|
460
461
|
const lines = [...snapshot.lines].sort((a, b) => a.sku.localeCompare(b.sku) || a.unitMinor - b.unitMinor || a.quantity - b.quantity);
|
|
462
|
+
const totals: MinorTotals =
|
|
463
|
+
snapshot.totals.exciseMinor === undefined
|
|
464
|
+
? {
|
|
465
|
+
subtotalMinor: snapshot.totals.subtotalMinor,
|
|
466
|
+
shippingMinor: snapshot.totals.shippingMinor,
|
|
467
|
+
taxMinor: snapshot.totals.taxMinor,
|
|
468
|
+
totalMinor: snapshot.totals.totalMinor,
|
|
469
|
+
}
|
|
470
|
+
: {
|
|
471
|
+
subtotalMinor: snapshot.totals.subtotalMinor,
|
|
472
|
+
shippingMinor: snapshot.totals.shippingMinor,
|
|
473
|
+
taxMinor: snapshot.totals.taxMinor,
|
|
474
|
+
totalMinor: snapshot.totals.totalMinor,
|
|
475
|
+
exciseMinor: snapshot.totals.exciseMinor,
|
|
476
|
+
};
|
|
461
477
|
const material = {
|
|
462
478
|
tenantId: snapshot.tenantId,
|
|
463
479
|
stage: snapshot.stage,
|
|
464
480
|
currency: snapshot.currency,
|
|
465
481
|
lines,
|
|
466
|
-
totals
|
|
482
|
+
totals,
|
|
467
483
|
destinationDigest: snapshot.destinationDigest,
|
|
468
484
|
};
|
|
469
485
|
return snapshot.contentVersionId === undefined ? material : { ...material, contentVersionId: snapshot.contentVersionId };
|
|
@@ -569,9 +585,13 @@ export function sumMinor(values: readonly number[]): number {
|
|
|
569
585
|
}
|
|
570
586
|
|
|
571
587
|
export function totalsMatch(totals: MinorTotals): boolean {
|
|
588
|
+
const parts =
|
|
589
|
+
totals.exciseMinor === undefined
|
|
590
|
+
? [totals.subtotalMinor, totals.shippingMinor, totals.taxMinor]
|
|
591
|
+
: [totals.subtotalMinor, totals.shippingMinor, totals.taxMinor, totals.exciseMinor];
|
|
572
592
|
return (
|
|
573
|
-
|
|
574
|
-
assertMinor(totals.totalMinor, "totalMinor")
|
|
593
|
+
(totals.exciseMinor === undefined || totals.exciseMinor >= 0) &&
|
|
594
|
+
sumMinor(parts) === assertMinor(totals.totalMinor, "totalMinor")
|
|
575
595
|
);
|
|
576
596
|
}
|
|
577
597
|
|
|
@@ -50,7 +50,7 @@ import { isForbiddenPublicPath, publicBinaryFindings } from "../../agency-kit/to
|
|
|
50
50
|
import { pageSlugFindings } from "../../agency-kit/tools/lib/page-slugs.mjs";
|
|
51
51
|
import { ANALYTICS_DECLARATION_FILE, analyticsDeclarationFindings } from "../../agency-kit/tools/lib/analytics-declaration.mjs";
|
|
52
52
|
import { validateVisualParityClosure } from "../../packages/cli/src/visual-parity.mjs";
|
|
53
|
-
import { declaredObligations, isRegulatedCompliance, validateDeclaredConfig } from "../../packages/cli/src/declared-config.mjs";
|
|
53
|
+
import { contrastRatio, declaredObligations, isRegulatedCompliance, validateDeclaredConfig } from "../../packages/cli/src/declared-config.mjs";
|
|
54
54
|
|
|
55
55
|
/** Catalogued embed slugs a tenant may opt into (mirror of EMBED_PROVIDERS). */
|
|
56
56
|
const KNOWN_EMBED_SLUGS = new Set(KNOWN_EMBEDS.map((p) => p.slug));
|
|
@@ -356,6 +356,66 @@ function validateThemeHeadings(theme, file) {
|
|
|
356
356
|
return out;
|
|
357
357
|
}
|
|
358
358
|
|
|
359
|
+
const THEME_HEX_RE = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
360
|
+
const REFERENCE_THEME_COLORS = Object.freeze({
|
|
361
|
+
bg: "#F4F1EA",
|
|
362
|
+
surface: "#FCFAF5",
|
|
363
|
+
text: "#1C2624",
|
|
364
|
+
muted: "#5C6663",
|
|
365
|
+
primary: "#1F3A34",
|
|
366
|
+
"primary-contrast": "#F4F1EA",
|
|
367
|
+
});
|
|
368
|
+
const WCAG_AA_CONTRAST = 4.5;
|
|
369
|
+
|
|
370
|
+
function hexContrastRatio(a, b) {
|
|
371
|
+
if (typeof a !== "string" || typeof b !== "string") return null;
|
|
372
|
+
const ca = a.trim();
|
|
373
|
+
const cb = b.trim();
|
|
374
|
+
if (!THEME_HEX_RE.test(ca) || !THEME_HEX_RE.test(cb)) return null;
|
|
375
|
+
return contrastRatio(ca, cb);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* theme.json WCAG AA contrast: resolved foreground/background pairs (including the
|
|
380
|
+
* bold hero headline's `--color-primary-contrast` on `--color-primary` when
|
|
381
|
+
* `brand.heroVariant` is `"bold"` or omitted) must meet 4.5:1.
|
|
382
|
+
*/
|
|
383
|
+
export function validateThemeContrast(theme, file) {
|
|
384
|
+
const out = [];
|
|
385
|
+
if (theme == null || typeof theme !== "object" || Array.isArray(theme)) return out;
|
|
386
|
+
const rawColor = theme.color && typeof theme.color === "object" && !Array.isArray(theme.color) ? theme.color : {};
|
|
387
|
+
const color = { ...REFERENCE_THEME_COLORS, ...rawColor };
|
|
388
|
+
const heroVariant = theme?.brand?.heroVariant ?? "bold";
|
|
389
|
+
const pairs = [
|
|
390
|
+
{ fg: "text", bg: "bg", label: "body text (color.text on color.bg)", level: ERROR },
|
|
391
|
+
{ fg: "text", bg: "surface", label: "surface text (color.text on color.surface)", level: ERROR },
|
|
392
|
+
heroVariant === "light"
|
|
393
|
+
? { fg: "primary", bg: "bg", label: "light hero headline (color.primary on color.bg)", level: ERROR }
|
|
394
|
+
: {
|
|
395
|
+
fg: "primary-contrast",
|
|
396
|
+
bg: "primary",
|
|
397
|
+
label: "bold hero headline (color.primary-contrast on color.primary)",
|
|
398
|
+
level: ERROR,
|
|
399
|
+
},
|
|
400
|
+
{ fg: "muted", bg: "bg", label: "secondary text (color.muted on color.bg)", level: WARN },
|
|
401
|
+
];
|
|
402
|
+
for (const pair of pairs) {
|
|
403
|
+
const ratio = hexContrastRatio(color[pair.fg], color[pair.bg]);
|
|
404
|
+
if (ratio !== null && ratio < WCAG_AA_CONTRAST) {
|
|
405
|
+
out.push(
|
|
406
|
+
mk(
|
|
407
|
+
pair.level,
|
|
408
|
+
"theme-contrast",
|
|
409
|
+
file,
|
|
410
|
+
`${pair.label} contrast (${color[pair.fg]} on ${color[pair.bg]}) is ${ratio.toFixed(2)}:1 — below WCAG AA ${WCAG_AA_CONTRAST}:1`,
|
|
411
|
+
`adjust color.${pair.fg} or color.${pair.bg} in ${file} to achieve at least ${WCAG_AA_CONTRAST}:1 contrast`,
|
|
412
|
+
),
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return out;
|
|
417
|
+
}
|
|
418
|
+
|
|
359
419
|
const SUPPORTED_BLOCK_CONTRACTS = new Set(BLOCK_PALETTE_SUPPORTED_CONTRACTS);
|
|
360
420
|
const KNOWN_BLOCKS = new Set([
|
|
361
421
|
"hero",
|
|
@@ -1168,6 +1228,7 @@ export function validateTenant(tenantDir, opts = {}) {
|
|
|
1168
1228
|
);
|
|
1169
1229
|
} else {
|
|
1170
1230
|
findings.push(...validateThemeHeadings(theme, rel(themePath)));
|
|
1231
|
+
findings.push(...validateThemeContrast(theme, rel(themePath)));
|
|
1171
1232
|
}
|
|
1172
1233
|
}
|
|
1173
1234
|
|