diff --git a/internal/cmd/agent_setup_docs_test.go b/internal/cmd/agent_setup_docs_test.go index e2008ab3..bb677a10 100644 --- a/internal/cmd/agent_setup_docs_test.go +++ b/internal/cmd/agent_setup_docs_test.go @@ -68,6 +68,13 @@ the manifest fields and the scopes are documented on the docs site, and the - Reference (manifest, scopes, hooks, message bridge, CLI): https://developer.civitai.com/apps/reference/ - Guide: https://developer.civitai.com/apps/guide/ +- **` + "`https://developer.civitai.com/apps/guide/earning.md`" + `** — the three money + rails. Fetch it before declaring a ` + "`goods`" + ` catalog. The three rules below are + repeated in this file deliberately, because they are the ones a manifest can + break while passing an older ` + "`civitai app validate`" + `: a paid-for-access app (` + "`kind: \"app_unlock\"`" + `) + is capped at **5000** Buzz rather than the general 50000, may declare **at + most one** unlock per manifest, and MUST carry a ` + "`justification`" + `. All three are + enforced at submit; the published JSON Schema does not declare them. - Example apps you can read end-to-end: https://developer.civitai.com/apps/examples - Full doc index for agents: https://developer.civitai.com/llms.txt diff --git a/internal/cmd/templates/agents-app.md b/internal/cmd/templates/agents-app.md index 0f5eefa1..8e1f4af2 100644 --- a/internal/cmd/templates/agents-app.md +++ b/internal/cmd/templates/agents-app.md @@ -17,6 +17,13 @@ the manifest fields and the scopes are documented on the docs site, and the - Reference (manifest, scopes, hooks, message bridge, CLI): https://developer.civitai.com/apps/reference/ - Guide: https://developer.civitai.com/apps/guide/ +- **`https://developer.civitai.com/apps/guide/earning.md`** — the three money + rails. Fetch it before declaring a `goods` catalog. The three rules below are + repeated in this file deliberately, because they are the ones a manifest can + break while passing an older `civitai app validate`: a paid-for-access app (`kind: "app_unlock"`) + is capped at **5000** Buzz rather than the general 50000, may declare **at + most one** unlock per manifest, and MUST carry a `justification`. All three are + enforced at submit; the published JSON Schema does not declare them. - Example apps you can read end-to-end: https://developer.civitai.com/apps/examples - Full doc index for agents: https://developer.civitai.com/llms.txt diff --git a/internal/validate/semantic.go b/internal/validate/semantic.go index 8881ec5e..24960bdc 100644 --- a/internal/validate/semantic.go +++ b/internal/validate/semantic.go @@ -11,8 +11,7 @@ import ( // block-goods.constants.ts. It is here rather than read out of the vendored // schema because the schema does not contain it and cannot: this bounds the // LENGTH OF THE JSON ENCODING of an opaque object, which JSON Schema has no -// vocabulary for. That makes it the one goods number this package legitimately -// holds a copy of. +// vocabulary for. // // ⚠ The server measures `new TextEncoder().encode(JSON.stringify(payload))`, // i.e. UTF-8 bytes of the compact encoding. Go's `json.Marshal` also emits @@ -21,6 +20,78 @@ import ( // bound is where to look if they ever disagree. const blockGoodPayloadMaxBytes = 2048 +// The `app_unlock` bounds, mirrored from the same server module +// (BLOCK_APP_UNLOCK_MAX_PRICE_BUZZ, BLOCK_APP_UNLOCK_MAX_PER_MANIFEST). +// +// 🔴 WHY THESE ARE HERE, STATED CORRECTLY — the first draft said "JSON Schema +// cannot express them" and that is FALSE, so do not re-derive it. The dialect is +// draft/2020-12, where all three ARE expressible: `if`/`then` + `required` for +// the conditional `justification`, `contains` + `maxContains: 1` for the arity, +// `if`/`then` + `maximum` for the kind's ceiling. The upstream constants file +// says so itself — *"a conditional `if/then` on `kind` there is more surface than +// this one property is worth"* — i.e. CONSIDERED AND DECLINED, not impossible. +// +// The real reason is ownership: `schema/app-block.manifest.schema.json` is an +// auto-revendored BYTE-MIRROR of `civitai:public/schemas/app-block/v1.json`, and +// this repo may not unilaterally edit it — a local change would be reverted by +// the next re-vendor. So the Go port is the only layer the CLI controls. +// +// ⚠ THAT MAKES THIS A SECOND-BEST FIX, AND SAYING SO IS THE POINT: declaring the +// three rules upstream would cover the SDK, developer.civitai.com and every +// third-party validator, and would make most of this block redundant. The false +// "cannot express" version foreclosed that option by making it look unavailable. +// If you are reading this while touching these rules, the upstream change is the +// better one to argue for. +// +// Measured consequence of the current state: an `app_unlock` priced at 5001, and +// a second `app_unlock` in one manifest, BOTH validate against the vendored +// schema and are rejected at submit. +// +// ⚠ `blockGoodMinPriceBuzz` IS part of a check, and an earlier draft of this +// very block said it was not — "NOT a check; the schema's `minimum` owns the +// general floor". That was true while the price check was clamped and became +// false the moment the clamp came off: the condition below tests +// `price < blockGoodMinPriceBuzz`, so `{"kind":"app_unlock","priceBuzz":0}` +// emits the mirrored sentence AND the schema's `minimum: got 0, want 2`. +// +// 🔴 AND THE FLOOR ARM'S JUSTIFICATION IS NOT THE CEILING'S — do not collapse +// them. The wrong-bound argument below is CEILING-SPECIFIC: the floor is 2 in +// the schema, in the server and here, so on that arm the second message corrects +// no bound and is pure duplication. It is kept anyway, because the arm is how +// the mirror stays a faithful copy of the server's single condition rather than +// a hand-picked subset of it — and because a reader who deletes it on the +// "pure duplication" reasoning silently drops a server rule. The `app_unlock at +// 1` test row is what catches that deletion. +// +// 🔴 THE RULE THIS FILE ACTUALLY HOLDS IS "MIRROR THE SERVER'S SENTENCE", NOT +// "NEVER REPORT TWICE" — and an earlier draft of this block asserted the latter, +// which is false about the code it sits in. A goods entry with no `title` already +// emits BOTH `goods[0].title must be a non-empty string` (here) and +// `goods[0]: missing property 'title'` (schema), and has since before this +// block existed. So a non-report rule was never the invariant. +// +// That mattered, because the false invariant bought a real defect: an earlier +// draft scoped the `app_unlock` price check to 2..50000 "so as not to report +// twice", and an `app_unlock` at 60000 then got ONLY the schema's +// `maximum: got 60,000, want 50,000` — 10x the real ceiling. A developer is told +// to come down to 50000, does, and is rejected again with a different number. +// 🔴 NAMING THE WRONG BOUND IS WORSE THAN NAMING IT TWICE. The check below is +// therefore unclamped above: for an `app_unlock` it fires on the server's own +// condition, and the schema may fire alongside it. +const ( + blockGoodMinPriceBuzz = 2 + blockAppUnlockMaxPriceBuzz = 5_000 + blockAppUnlockMaxPerManifest = 1 +) + +// The two `kind` values, mirroring BLOCK_GOOD_KINDS / BLOCK_GOOD_DEFAULT_KIND. +// `app_unlock` is the one carrying extra rules; `good` is the default an absent +// `kind` resolves to. +const ( + blockGoodDefaultKind = "good" + blockGoodKindAppUnlock = "app_unlock" +) + // semantic.go ports the *semantic* manifest rules the server runs at approve // time in BlockManifestValidator // (civitai/civitai → src/server/services/block-manifest-validator.service.ts). @@ -185,6 +256,7 @@ func goodsChecks(generic map[string]any) []Finding { var out []Finding seen := make(map[string]struct{}, len(raw)) + appUnlockCount := 0 for i, entry := range raw { e, ok := entry.(map[string]any) if !ok { @@ -211,6 +283,80 @@ func goodsChecks(generic map[string]any) []Finding { continue } + // Resolve the kind exactly as the server does: absent or unset means the + // default. A kind that is PRESENT but not a member is the schema's enum to + // reject, and the server `return`s on it — so the entry's kind-specific + // rules below are unreachable for it here too. + // + // ⚠ AUDIT FINDING REJECTED, WITH THE EVIDENCE, SO IT IS NOT RE-RAISED: a + // round-0 pass read this `continue` as suppressing the `payload` byte-size + // finding — the one goods rule JSON Schema provably cannot express — for an + // entry whose `kind` is invalid. It does, and that is CORRECT: the server's + // kind check `return`s at block-goods.constants.ts:385-387, well before its + // own payload check at :464, so it suppresses the identical finding. Making + // the CLI continue to the payload check here would be a DIVERGENCE from the + // server, which is the one thing this file exists not to do. The cost is + // real but is the server's: an author with both defects learns about the + // kind first and the payload on the next run. + kind := blockGoodDefaultKind + if raw, present := e["kind"]; present { + k, isString := raw.(string) + if !isString || (k != blockGoodDefaultKind && k != blockGoodKindAppUnlock) { + continue // `kind must be one of good, app_unlock` — schema-covered. + } + kind = k + } + + // The `app_unlock` price ceiling, mirroring the server's own condition for + // this kind: a whole number in 2..5000. Deliberately NOT clamped to the + // window where the vendored schema passes — see the constant block for why + // that clamp was a defect rather than a courtesy. Above 50000 the schema + // fires too; two findings naming the right bound beat one naming a bound + // 10x too high. + // + // A non-numeric `priceBuzz` is left to the schema's `type`: `toNumber` + // cannot evaluate the server's condition on it, so there is nothing to + // mirror. + // + // 🔴 The message names the KIND'S ceiling, not the general one, verbatim + // including the ` for an app_unlock good` suffix. The server's own comment + // is explicit that for an `app_unlock` this string is the only + // machine-delivered statement of the real limit. + if kind == blockGoodKindAppUnlock { + if price, isNumber := toNumber(e["priceBuzz"]); isNumber && + (price != float64(int64(price)) || + price < blockGoodMinPriceBuzz || + price > blockAppUnlockMaxPriceBuzz) { + out = append(out, newFinding(at+".priceBuzz", fmt.Sprintf( + "%s.priceBuzz must be a whole number between %d and %d Buzz for an %s good", + at, blockGoodMinPriceBuzz, blockAppUnlockMaxPriceBuzz, blockGoodKindAppUnlock))) + continue + } + } + + // `justification`. Shape-checked for ANY good so a developer who explains an + // ordinary item is not rejected for it; REQUIRED only for `app_unlock`. + // + // Only the two rules the schema cannot express are here. The schema already + // carries `minLength: 1` and `maxLength: 500`, and it measures the RAW + // string while the server measures the TRIMMED one — which makes the schema + // never MORE permissive, so its length bound cannot pass something the + // server's rejects. What it CANNOT see is whitespace-only (`" "` satisfies + // `minLength: 1`), and absence (`justification` is not in the schema's + // `required`, and could not be: the requirement is conditional on `kind`). + if raw, present := e["justification"]; present { + if s, isString := raw.(string); !isString || strings.TrimSpace(s) == "" { + out = append(out, newFinding(at+".justification", fmt.Sprintf( + "%s.justification must be a non-empty string", at))) + continue + } + } else if kind == blockGoodKindAppUnlock { + out = append(out, newFinding(at+".justification", fmt.Sprintf( + "%s.justification is required for an %s good — it makes the app paid, so a moderator must be told why", + at, blockGoodKindAppUnlock))) + continue + } + if payload, present := e["payload"]; present { p, ok := payload.(map[string]any) if !ok { @@ -230,7 +376,60 @@ func goodsChecks(generic map[string]any) []Finding { continue } } + + // Reached only by an entry that survived every check above — which is what + // the server's parsed `goods` array holds, and what its arity check counts. + if kind == blockGoodKindAppUnlock { + appUnlockCount++ + } } + + // ARITY, a whole-array property: at most one `app_unlock` per manifest, so + // "is this viewer admitted?" has exactly one answer. Unlike every check in the + // loop this one is ADDITIVE rather than entry-terminating — the server pushes + // it and falls through to return, so it can accompany a per-entry finding. + // Its path is `goods`, not `goods[i]`. + // + // ⚠ A FAILING ENTRY IS NOT COUNTED, WHICH CAN TAKE THE SURVIVING COUNT BELOW + // THE LIMIT — it does not suppress this check in general. Every per-entry + // check above `continue`s, so a failing entry never reaches the counter; the + // arity finding then disappears only when what SURVIVES is one unlock or + // fewer. With three unlocks and one over-cap, two survive and BOTH findings + // appear — which is the ADDITIVE property stated directly above, and is + // pinned by the `arity is ADDITIVE` row below. + // + // ⚠ Do not restate this as "an earlier finding suppresses the arity one". An + // earlier draft of this paragraph did, and it was false while reading + // plausibly, because BOTH server consequences quoted next are 2-unlock cases + // where the surviving count happens to fall to 1. It also contradicted the + // paragraph three lines above it and a test row named after that paragraph. + // + // The two consequences, in the server's own words at + // block-goods.constants.ts:501-509 — "two unlocks where one is over-cap + // reports ONLY the price error, and two unlocks sharing an id report ONLY the + // duplicate error" — both hold here, each pinned by its own row below. + // + // Note the price arm of that only became true when the clamp came off: while + // the check was scoped to 2..50000, an unlock at 60000 did NOT `continue`, so + // it was counted and a second over-cap unlock reported the arity error the + // server would not have reported. Removing the clamp fixed a mirror defect + // nobody had looked for. + // + // ⚠ THE LIMIT, STATED: the server counts only goods that PARSED, and it + // enforces rules this package deliberately leaves to the schema (the `id` + // pattern and length, `title` length, `description`, the `kind` enum, the + // general price range). An entry failing one of those is dropped from the + // server's count and cannot be dropped from this one, so on a manifest that is + // ALREADY failing the schema the CLI can report an arity violation the server + // would not. Same bounded-and-cosmetic trade as the duplicate-id note above, + // and the same reason: re-implementing the schema's rules here to close it + // would put schema content in a second place. + if appUnlockCount > blockAppUnlockMaxPerManifest { + out = append(out, newFinding("goods", fmt.Sprintf( + "goods may declare at most %d %s good (found %d) — app access is one question with one answer", + blockAppUnlockMaxPerManifest, blockGoodKindAppUnlock, appUnlockCount))) + } + return out } diff --git a/internal/validate/semantic_test.go b/internal/validate/semantic_test.go index 84585be9..dc433f91 100644 --- a/internal/validate/semantic_test.go +++ b/internal/validate/semantic_test.go @@ -513,6 +513,299 @@ func TestGoodsChecksMirrorTheServerRulesJSONSchemaCannotExpress(t *testing.T) { } } +// TestAppUnlockGoodsChecksMirrorTheServerRulesTheSchemaWontExpress pins the +// three rules a `kind: "app_unlock"` good carries and an ordinary one does not. +// +// 🔴 WHY THESE BELONG IN THE CLI AT ALL. Unlike the general price range, the +// vendored schema does not reject any of them and deliberately will not: its +// `priceBuzz.maximum` stays 50000 for every kind with the narrow bound in a +// prose `description`, `justification` is absent from `required` (the +// requirement is conditional on `kind`, which a per-item subschema cannot +// state), and arity is a whole-array property. So before this, an `app_unlock` +// priced at 5001 — or a second one, or one with no justification — passed +// `civitai app validate` and was rejected at submit. A clean local verdict for a +// manifest the server refuses is the failure this closes. +// +// Every `want` is spelled out, never built from the code under test, for the +// same reason as the sibling table above: these strings are a contract with the +// server's 400. +func TestAppUnlockGoodsChecksMirrorTheServerRulesTheSchemaWontExpress(t *testing.T) { + // 7_777 and 5_001: neither equals a bound nor is a multiple of one, so a + // mutant hardcoding 5000 — or substituting the general 50000 ceiling — cannot + // survive on them. 4_999/5_000 pin the boundary from below and exactly on it. + unlock := func(id string, priceBuzz int, extra map[string]any) map[string]any { + e := map[string]any{"id": id, "title": "Full access", "priceBuzz": float64(priceBuzz), "kind": "app_unlock"} + for k, v := range extra { + e[k] = v + } + return e + } + justified := map[string]any{"justification": "reviewed: unlocks the editor"} + + cases := []struct { + name string + in map[string]any + want []string + }{ + // ---- justification: required only for app_unlock ---- + { + name: "app_unlock with NO justification → the server's required message", + in: map[string]any{"goods": []any{unlock("access", 500, nil)}}, + want: []string{"goods[0].justification is required for an app_unlock good — it makes the app paid, so a moderator must be told why"}, + }, + { + name: "an ORDINARY good with no justification → nothing (the rule is kind-keyed)", + in: map[string]any{"goods": []any{map[string]any{"id": "credits", "title": "Credits", "priceBuzz": float64(500)}}}, + want: nil, + }, + { + name: "kind omitted entirely resolves to `good` → still nothing", + in: map[string]any{"goods": []any{map[string]any{"id": "credits", "title": "Credits", "priceBuzz": float64(7777)}}}, + want: nil, + }, + { + name: "app_unlock WITH a justification → nothing", + in: map[string]any{"goods": []any{unlock("access", 500, justified)}}, + want: nil, + }, + { + name: "whitespace-only justification → non-empty message, NOT the required one (schema minLength:1 accepts it)", + in: map[string]any{"goods": []any{unlock("access", 500, map[string]any{"justification": " "})}}, + want: []string{"goods[0].justification must be a non-empty string"}, + }, + { + name: "an ORDINARY good's justification is shape-checked too (explaining an item must not be punished, but an empty explanation is still wrong)", + in: map[string]any{"goods": []any{map[string]any{ + "id": "credits", "title": "Credits", "priceBuzz": float64(500), "justification": ""}}}, + want: []string{"goods[0].justification must be a non-empty string"}, + }, + + // ---- priceBuzz: the kind's own ceiling, named in the message ---- + { + name: "app_unlock at 5001 → the message names 5000, the bound it broke, not 50000", + in: map[string]any{"goods": []any{unlock("access", 5001, justified)}}, + want: []string{"goods[0].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + { + name: "app_unlock at 7777 → same message (a mutant using the general ceiling passes 7777)", + in: map[string]any{"goods": []any{unlock("access", 7777, justified)}}, + want: []string{"goods[0].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + { + name: "app_unlock exactly AT 5000 → nothing (the bound is inclusive)", + in: map[string]any{"goods": []any{unlock("access", 5000, justified)}}, + want: nil, + }, + { + name: "app_unlock at 4999 → nothing", + in: map[string]any{"goods": []any{unlock("access", 4999, justified)}}, + want: nil, + }, + { + name: "an ORDINARY good at 20000 → nothing; the general ceiling is the schema's business", + in: map[string]any{"goods": []any{map[string]any{ + "id": "credits", "title": "Credits", "priceBuzz": float64(20000)}}}, + want: nil, + }, + { + // 🔴 REGRESSION GUARD for the defect a round-0 audit found. An earlier + // draft clamped this check to 2..50000 "so as not to double-report with + // the schema", so a 60000 unlock got ONLY the schema's + // `maximum: got 60,000, want 50,000` — 10x the real ceiling, telling a + // developer to come down to a bound they are still 55000 over. The + // schema fires here too; naming the right bound twice beats naming the + // wrong one once. + name: "app_unlock at 60000 → still names 5000, NOT the general 50000 the schema would quote", + in: map[string]any{"goods": []any{unlock("access", 60000, justified)}}, + want: []string{"goods[0].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + { + name: "app_unlock at 1 → below the floor, and the message states the real range", + in: map[string]any{"goods": []any{unlock("access", 1, justified)}}, + want: []string{"goods[0].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + { + name: "app_unlock at a FRACTIONAL price → not a whole number, same message", + in: map[string]any{"goods": []any{map[string]any{ + "id": "access", "title": "T", "priceBuzz": 500.5, "kind": "app_unlock", + "justification": "reviewed"}}}, + want: []string{"goods[0].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + { + name: "a NON-NUMERIC priceBuzz is left to the schema's `type` — nothing to mirror", + in: map[string]any{"goods": []any{map[string]any{ + "id": "access", "title": "T", "priceBuzz": "500", "kind": "app_unlock", + "justification": "reviewed"}}}, + want: nil, + }, + + // ---- the order contract: first hit ends the entry ---- + { + name: "app_unlock over-priced AND unjustified → ONLY the price finding, because the server returns there", + in: map[string]any{"goods": []any{unlock("access", 7777, nil)}}, + want: []string{"goods[0].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + { + name: "a whitespace title beats both, mirroring the server's earlier return", + in: map[string]any{"goods": []any{map[string]any{ + "id": "access", "title": " ", "priceBuzz": float64(7777), "kind": "app_unlock"}}}, + want: []string{"goods[0].title must be a non-empty string"}, + }, + + // ---- arity: a whole-array property, path `goods`, additive ---- + { + name: "two app_unlock goods → the arity message, at path `goods`", + in: map[string]any{"goods": []any{unlock("a", 500, justified), unlock("b", 500, justified)}}, + want: []string{"goods may declare at most 1 app_unlock good (found 2) — app access is one question with one answer"}, + }, + { + name: "three app_unlock goods → the count reported is 3 (a mutant hardcoding 2 dies here)", + in: map[string]any{"goods": []any{ + unlock("a", 500, justified), unlock("b", 500, justified), unlock("c", 500, justified)}}, + want: []string{"goods may declare at most 1 app_unlock good (found 3) — app access is one question with one answer"}, + }, + { + name: "one app_unlock beside several ordinary goods → nothing; only unlocks are counted", + in: map[string]any{"goods": []any{ + map[string]any{"id": "c1", "title": "C1", "priceBuzz": float64(500)}, + unlock("access", 500, justified), + map[string]any{"id": "c2", "title": "C2", "priceBuzz": float64(500)}, + }}, + want: nil, + }, + { + name: "arity is ADDITIVE, not entry-terminating: a per-entry finding and the arity finding both appear", + in: map[string]any{"goods": []any{ + unlock("a", 500, justified), + unlock("b", 500, justified), + unlock("c", 7777, justified), + }}, + want: []string{ + "goods[2].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good", + "goods may declare at most 1 app_unlock good (found 2) — app access is one question with one answer", + }, + }, + { + // The server's SECOND stated arity consequence + // (block-goods.constants.ts:501-509): "two unlocks sharing an id report + // ONLY the duplicate error (the duplicate is dropped before the count, + // so it never reaches the arity rule at all)". + name: "two unlocks sharing an id → ONLY the duplicate error, never the arity one", + in: map[string]any{"goods": []any{unlock("access", 500, justified), unlock("access", 600, justified)}}, + want: []string{`goods[1].id duplicates an earlier good id ("access")`}, + }, + { + // The server's FIRST stated consequence, at a price ABOVE the general + // ceiling — the case the removed clamp got wrong. While the check was + // scoped to 2..50000 this entry did not `continue`, so it WAS counted + // and the arity error fired where the server's would not have. + name: "two unlocks, one at 60000 → ONLY the price error; the over-cap entry never reaches the counter", + in: map[string]any{"goods": []any{unlock("a", 500, justified), unlock("b", 60000, justified)}}, + want: []string{"goods[1].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + { + name: "an entry that FAILED is not counted toward arity, mirroring the server's parsed-goods array", + in: map[string]any{"goods": []any{ + unlock("a", 500, justified), + unlock("b", 7777, justified), // fails on price, so it never counts + }}, + want: []string{"goods[1].priceBuzz must be a whole number between 2 and 5000 Buzz for an app_unlock good"}, + }, + + // ---- an invalid kind is the schema's enum to reject ---- + { + name: "kind present but not a member → skipped here, so no app_unlock rule is applied to it", + in: map[string]any{"goods": []any{map[string]any{ + "id": "access", "title": "Access", "priceBuzz": float64(7777), "kind": "app_subscription"}}}, + want: nil, + }, + { + // 🔴 PINS AN AUDIT FINDING THAT WAS **REJECTED**, so nobody "fixes" it + // into a divergence. A round-0 pass read the invalid-kind skip as + // suppressing the payload byte-size finding. It does — and the SERVER + // does the same: its kind check returns at + // block-goods.constants.ts:385-387, before its payload check at :464. + // Mirroring that is this file's contract. If this case ever starts + // reporting the payload finding, the CLI has diverged from the server. + name: "an invalid kind suppresses the payload finding, exactly as the server's early return does", + in: map[string]any{"goods": []any{map[string]any{ + "id": "access", "title": "T", "priceBuzz": float64(500), "kind": "nope", + "payload": map[string]any{"note": strings.Repeat("x", 4096)}}}, + }, + want: nil, + }, + { + name: "the SAME oversized payload under a VALID kind DOES report — the control proving the case above is about `kind`, not about payload detection", + in: map[string]any{"goods": []any{map[string]any{ + "id": "access", "title": "T", "priceBuzz": float64(500), "kind": "good", + "payload": map[string]any{"note": strings.Repeat("x", 4096)}}}, + }, + want: []string{"goods[0].payload must serialize to at most 2048 bytes"}, + }, + } + + produced := 0 + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := goodsChecks(tc.in) + produced += len(got) + if len(got) != len(tc.want) { + t.Fatalf("got %d finding(s), want %d\n got: %v\n want: %v", + len(got), len(tc.want), got, tc.want) + } + for i := range tc.want { + if got[i].Message != tc.want[i] { + t.Errorf("finding %d message mismatch\n got: %q\n want: %q", + i, got[i].Message, tc.want[i]) + } + } + }) + } + + // A table whose rows mostly expect nil passes against a goodsChecks wired to + // nothing, so assert the table did make it speak. + if produced == 0 { + t.Fatal("no case produced a finding — the app_unlock checks may be unreachable and every nil row would still pass") + } +} + +// TestAppUnlockFindingsCarryTheOffendingFieldPath pins the Field. The arity +// finding is the interesting one: it is the only goods finding whose path is the +// ARRAY rather than an element, because the rule is a property of the whole +// catalog and there is no single entry to blame. +// +// It also pins the subtlety that cost this test a round: TWO entries must +// SURVIVE for arity to fire, because a failing entry is excluded from the count +// (mirroring the server's parsed-goods array). A fixture of three unlocks where +// two of them fail produces no arity finding at all. +func TestAppUnlockFindingsCarryTheOffendingFieldPath(t *testing.T) { + justified := map[string]any{"justification": "reviewed"} + unlock := func(id string, price int, extra map[string]any) map[string]any { + e := map[string]any{"id": id, "title": "T", "priceBuzz": float64(price), "kind": "app_unlock"} + for k, v := range extra { + e[k] = v + } + return e + } + + got := goodsChecks(map[string]any{"goods": []any{ + unlock("a", 500, justified), // survives → counted + unlock("b", 500, nil), // missing justification → NOT counted + unlock("c", 500, justified), // survives → counted, so the count reaches 2 + unlock("d", 7777, justified), // over the unlock ceiling → NOT counted + }}) + + want := []string{"goods[1].justification", "goods[3].priceBuzz", "goods"} + if len(got) != len(want) { + t.Fatalf("got %d finding(s), want %d: %v", len(got), len(want), got) + } + for i := range want { + if got[i].Field != want[i] { + t.Errorf("finding %d field: got %q, want %q", i, got[i].Field, want[i]) + } + } +} + // TestGoodsFindingsCarryTheOffendingFieldPath pins the Field, not the Message. // The two are separate claims: `--json` consumers key on Field, and a finding // whose text names goods[1] while its Field says something else sends a tool to