# Worked Example: Standards Browser Snapshot And Alignment Proof

This example regenerates a small report from the live demo tenant using only CASE surface calls. It does not parse schemas, classify standards, repair graph rows, or store data outside the API.

Prerequisites: `curl` and `jq`.

## 1. Set Base URL And Mint Demo Token

```bash
export CASE_BASE_URL="https://platform3-andymontgomery-9773s-projects.vercel.app/case/1edtech/implementation/api"
export CASE_TOKEN="$(curl -s -X POST "$CASE_BASE_URL/dev/mint?tenantId=demo" | jq -r '.token')"
```

## 2. Read The Demo Framework And Items

The demo framework below is the stable sample framework used by the shipped verifier.

```bash
export CASE_DOC_ID="3ea5c35f-5b3f-5e4d-905a-483c134316ec"
export CASE_DOC_URI="https://alpha.school/case/cfdocuments/3ea5c35f-5b3f-5e4d-905a-483c134316ec"
export CASE_ITEM_ID="91910dfe-0045-55d7-8fc3-cf1149fe4637"
export CASE_ITEM_URI="https://alpha.school/case/items/91910dfe-0045-55d7-8fc3-cf1149fe4637"

curl -s "$CASE_BASE_URL/ims/case/v1p1/CFDocuments/$CASE_DOC_ID" \
  -H "Authorization: Bearer $CASE_TOKEN" \
  > /tmp/case-skill-document.json

curl -s "$CASE_BASE_URL/ims/case/v1p1/CFItems?CFDocumentURI=$(printf '%s' "$CASE_DOC_URI" | jq -sRr @uri)&limit=25&offset=0" \
  -H "Authorization: Bearer $CASE_TOKEN" \
  > /tmp/case-skill-items.json

curl -s "$CASE_BASE_URL/ims/case/v1p1/CFItemAssociations/$CASE_ITEM_ID" \
  -H "Authorization: Bearer $CASE_TOKEN" \
  > /tmp/case-skill-associations.json
```

Expected result:

- `/tmp/case-skill-document.json` contains a `CFDocument`.
- `/tmp/case-skill-items.json` contains the framework's `CFItems`.
- `/tmp/case-skill-associations.json` contains the selected item's CASE association set.

If the documented `CFDocumentURI` filter returns zero rows for a real imported framework, do not infer membership from publisher URL patterns. Report that as a surface/data issue unless the approved customer website documents another path for the job.

## 3. Create And Verify An Alignment

Alignment is a TimeBack resource that links an external-system identifier to a CFItem without contaminating the canonical CASE graph.

```bash
export RUN_ID="skill-pack-$(date +%s)"
export ALIGNMENT_ID="align-$RUN_ID"
export EXTERNAL_ID="lesson-$RUN_ID"
export IDEMPOTENCY_KEY="align-$RUN_ID"

jq -n \
  --arg alignmentId "$ALIGNMENT_ID" \
  --arg externalId "$EXTERNAL_ID" \
  --arg cfItemUri "$CASE_ITEM_URI" \
  --arg cfItemIdentifier "$CASE_ITEM_ID" \
  '{
    alignmentId: $alignmentId,
    externalSystem: "skill-pack-demo",
    externalId: $externalId,
    cfItemUri: $cfItemUri,
    cfItemIdentifier: $cfItemIdentifier,
    label: "Skill pack fractions lesson",
    metadata: {source: "case_skill_pack_worked_example"}
  }' > /tmp/case-skill-alignment-request.json

curl -sD /tmp/case-skill-alignment.headers \
  -o /tmp/case-skill-alignment-created.json \
  -X POST "$CASE_BASE_URL/ims/case/v1p1/Alignments" \
  -H "Authorization: Bearer $CASE_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
  -d @/tmp/case-skill-alignment-request.json

curl -s "$CASE_BASE_URL/ims/case/v1p1/Alignments?externalSystem=skill-pack-demo&externalId=$EXTERNAL_ID" \
  -H "Authorization: Bearer $CASE_TOKEN" \
  > /tmp/case-skill-alignment-verified.json
```

Expected result:

- The POST returns `201`.
- The verification list returns `count: 1`.
- The Alignment contains `cfItemUri` or `cfItemIdentifier`.
- The Alignment is not a CFAssociation and does not appear in a CFPackage export.

## 4. Produce A Report From API Responses

This report is presentation formatting over API responses. It does not define CASE semantics.

```bash
{
  echo "# CASE Standards Browser Snapshot"
  echo
  echo "Base URL: $CASE_BASE_URL"
  echo "Tenant mode: demo"
  echo
  echo "## Framework"
  jq -r '.CFDocument | "- Title: \(.title)\n- Identifier: \(.identifier)\n- URI: \(.uri)\n- Creator: \(.creator)\n- CASE version: \(.caseVersion)"' /tmp/case-skill-document.json
  echo
  echo "## Items"
  jq -r '"- Count: \(.count)", (.CFItems[] | "- \(.humanCodingScheme // .identifier): \(.fullStatement)")' /tmp/case-skill-items.json
  echo
  echo "## Association Set"
  jq -r '(.CFAssociationSet // {}) as $set | ["parents", "children", "related"][] as $k | "- \($k): \(($set[$k] // []) | length)"' /tmp/case-skill-associations.json
  echo
  echo "## Alignment Proof"
  jq -r '.Alignments[0] | "- Alignment ID: \(.alignmentId)\n- External system: \(.externalSystem)\n- External ID: \(.externalId)\n- CFItem identifier: \(.cfItemIdentifier)\n- Label: \(.label)"' /tmp/case-skill-alignment-verified.json
} > /tmp/case-standards-browser-snapshot.md

cat /tmp/case-standards-browser-snapshot.md
```

The final report is the deliverable. Its facts come from live `CFDocument`, `CFItem`, `CFItemAssociations`, and `Alignment` responses.

## 5. Prove The Pack Against The Platform Answer Key

Run the shipped check when you need a machine-verifiable proof that the browser recipe reproduces the platform's own answer. The check uses `GET /ims/case/v1p1/CFPackages/{sourcedId}` as the answer key for one document graph, then confirms the documented browser calls reproduce that graph without local CASE logic.

```bash
./checks/case_surface_check.sh
```

Use the optional write probe only when the requested job needs Alignment write evidence:

```bash
./checks/case_surface_check.sh --write-alignment
```

The check is an API orchestration probe. It is not a CASE validator, standards catalog, graph-repair layer, or schema parser.

## 6. Optional Write-Validator Probe

Use this only when the requested deliverable needs to prove write safety.

```bash
ETAG="$(awk 'BEGIN{IGNORECASE=1} /^etag:/ {sub(/\r$/,"",$2); print $2}' /tmp/case-skill-alignment.headers)"

curl -sD /tmp/case-skill-alignment-patch.headers \
  -o /tmp/case-skill-alignment-patched.json \
  -X PATCH "$CASE_BASE_URL/ims/case/v1p1/Alignments/$ALIGNMENT_ID" \
  -H "Authorization: Bearer $CASE_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: patch-$RUN_ID" \
  -H "If-Match: $ETAG" \
  -d '{"label":"Skill pack fractions lesson - patched"}'

awk 'BEGIN{IGNORECASE=1} /^x-case-etag:/ {print}' /tmp/case-skill-alignment-patch.headers
```

For a second write, use the `X-Case-ETag` value from the first PATCH response or fetch the detail route again and use that response's `ETag`.
