> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nika.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Test workflows

> Golden testing with nika test: every workflow runs offline under the mock provider, deterministically, and CI gates on the diff.

How do you pin what a workflow returns without spending tokens?

`nika test <file>` runs it under the **mock provider** — offline,
deterministic, zero keys — then **diffs the output values** against
`<file>.golden.json` (deep JSON equality, path-by-path). Schema
acceptance is a **separate** gate: when `infer.schema` is present the
mock must synthesize a valid instance *before* that JSON is compared.

Predict: if you change only the prompt text, does the golden still
match? Not if the recorded values change. `mock/echo` echoes the
prompt, so the golden fails. A schema mock that still synthesizes the
same instance can match even when the prompt changed — the golden
never means “shape only”.

```bash theme={"system"}
nika test triage.nika
```

## Why the mock makes this possible

`nika test` swaps every `infer:` / `agent:` call to the mock provider,
whatever `model:` the file declares — your `ollama/qwen3.5:4b` (or
`openai/…`, or any cloud model) workflow tests offline, unchanged.

The mock is not a stub that returns `"ok"`. It is **schema-conformant**:
when a task declares a `schema:`, the mock synthesizes an instance that
validates against it — required fields present, types respected. Every
schema workflow runs offline, end to end, through the same runtime,
bindings, and typed-outputs validation as a live run.

```yaml triage.nika theme={"system"}
# Classify a support ticket into a typed verdict.
nika: ticket-triage

model: mock/echo                       # swap for ollama/llama3.2:3b or any provider

inputs:
  ticket:
    type: string
    default: "Payment page returns a 502 after checkout."

tasks:
  classify:
    infer:
      prompt: "Classify this ticket: ${{ inputs.ticket }}"
      max_tokens: 300
      schema:
        type: object
        required: [category, urgent]
        properties:
          category: { type: string }
          urgent: { type: boolean }

outputs:
  verdict: "${{ tasks.classify.output }}"
```

> [!TIP]
>
> The golden pins the workflow's `outputs:` block — a workflow without one
> pins `{}`. Declare typed `outputs:` and the golden guards the whole
> callable contract.

## The golden lifecycle

First run — no golden exists yet, so `nika test` teaches instead of
guessing (exit 3):

```text theme={"system"}
$ nika test triage.nika
nika test · no golden yet for triage.nika

  the golden pins this workflow's typed `outputs:` under the simulated
  plane (mock model · effects refused · offline · deterministic) —
  future runs must match it.

  create it:   nika test triage.nika --update
  then commit: triage.nika.golden.json
  a rule table: one case per `--case <name>` (its own golden) · bind
  its inputs with `--var KEY=VALUE`
```

Create it with `--update`, review it once, commit it:

```text theme={"system"}
$ nika test triage.nika --update
✔ golden written · triage.nika.golden.json
  review it once, commit it — `nika test` now guards this workflow
```

The golden is small, readable JSON — the mock-synthesized, schema-conformant
outputs:

```json triage.nika.golden.json theme={"system"}
{
  "verdict": {
    "category": "mock",
    "urgent": false
  }
}
```

From now on `nika test` compares. A match is exit 0; drift renders a
per-path diff and exits 1:

```text theme={"system"}
$ nika test triage.nika
✖ outputs drifted from the golden · triage.nika.golden.json
  + outputs.verdict.queue · not in the golden ("mock")

  intended? re-baseline: nika test triage.nika --update
```

If the drift was intended (you changed the schema, added an output), re-pin
with `--update` and commit the new golden — the diff shows up in code
review, exactly like a snapshot test.

## Proving a rule against cases

A [`nika:decide`](https://docs.nika.sh/reference/builtins#the-decision-bundle) bundle is a rubric
with consequences, so it wants a corpus, not one happy path. The kernel
enforces the bundle's own laws, but a fixture's `class` is only a label:
nothing asserts that a `positive` fixture actually recommends. `nika test` is
how you assert it.

The pattern that works: **the rule lives once, each case is a named golden.**
One workflow takes the case's facts as `inputs:`; `--case <name>` pins its
golden as `<file>.<name>.golden.json`, and `--var` binds that case's inputs.

```text theme={"system"}
decisions/
  refund.bundle.json                         ← the rule · one owned artifact
  refund.nika                                ← the case runner · facts in, outcome out
  refund.nika.fresh-returned.golden.json     ← one pinned outcome per case
  refund.nika.stale-returned.golden.json
  refund.nika.fresh-unreturned.golden.json
```

The workflow builds the evidence snapshot from its inputs, runs the kernel and
returns the outcome. No model, no network, no clock: the only thing under test
is the rule.

```yaml refund.nika theme={"system"}
# One refund rule, a whole table of cases: each case binds its own inputs.
nika: refund

permits:
  fs: { read: ["./refund.bundle.json"] }
  tools: ["nika:decide"]

inputs:
  days:
    type: integer
    required: true
  returned:
    type: bool
    required: true

tasks:
  decide:
    invoke:
      tool: "nika:decide"
      args:
        bundle: "./refund.bundle.json"
        evidence:
          t: "2026-09-02T09:00:00Z"
          evidence:
            - { key: days_since_purchase, value: "${{ inputs.days }}", source: orders-db,
                observed_at: "2026-09-02T09:00:00Z", digest: d1,
                confidentiality: internal, integrity: verified,
                quality: { freshness: fresh, completeness: complete, independence_group: orders } }
            - { key: item_returned, value: "${{ inputs.returned }}", source: warehouse,
                observed_at: "2026-09-02T09:00:00Z", digest: d2,
                confidentiality: internal, integrity: verified,
                quality: { freshness: fresh, completeness: complete, independence_group: warehouse } }

outputs:
  outcome: ${{ tasks.decide.output.outcome }}
```

> [accordion] refund.bundle.json · the rule the cases prove
>
> A refund within 30 days scores 6000 basis points, a returned item 4000, and
> the bundle recommends at 8000. Its own fixtures carry the mandatory
> contradictory case.
>
> ```json refund.bundle.json theme={"system"}
> {
>   "decision_bundle_format": 1,
>   "manifest": { "id": "refund-eligibility", "version": "1.0.0", "owner": "acme-support", "license": "Apache-2.0" },
>   "evidence_schema": {
>     "days_since_purchase": { "type": "integer", "required": true, "sources": ["orders-db"], "integrity": "verified" },
>     "item_returned": { "type": "bool", "required": true, "sources": ["warehouse"], "integrity": "verified" }
>   },
>   "transforms": {
>     "within_window": { "kind": "bucket", "min": 0, "max": 365, "edges": [31], "values": [10000, 0] },
>     "returned_step": { "kind": "bucket", "min": 0, "max": 1, "edges": [1], "values": [0, 10000] }
>   },
>   "rules": {
>     "dimensions": {
>       "refund_eligibility": {
>         "terms": [
>           { "evidence": "days_since_purchase", "transform": "within_window", "weight_bp": 6000, "monotonicity": "decreases" },
>           { "evidence": "item_returned", "transform": "returned_step", "weight_bp": 4000, "monotonicity": "increases" }
>         ]
>       }
>     },
>     "thresholds": [ { "dimension": "refund_eligibility", "recommend_gte_bp": 8000 } ]
>   },
>   "governance": { "never_automatic": [] },
>   "fixtures": [
>     { "name": "fresh-returned", "class": "positive", "evidence": { "days_since_purchase": 5, "item_returned": true } },
>     { "name": "stale-unreturned", "class": "negative", "evidence": { "days_since_purchase": 90, "item_returned": false } },
>     { "name": "fresh-unreturned", "class": "ambiguous", "evidence": { "days_since_purchase": 5, "item_returned": false } },
>     { "name": "missing-return", "class": "contradictory", "evidence": { "days_since_purchase": 5 } }
>   ]
> }
> ```

Pin each case once, read its outcome, commit the goldens:

```bash theme={"system"}
nika test refund.nika --case fresh-returned   --var days=5  --var returned=true  --update
nika test refund.nika --case stale-returned   --var days=90 --var returned=true  --update
nika test refund.nika --case fresh-unreturned --var days=5  --var returned=false --update
```

```json refund.nika.fresh-returned.golden.json theme={"system"}
{
  "outcome": "recommend"
}
```

The other two pin `"defer"`: a stale purchase and an item still out both fall
below the threshold. The rule is now guarded. Change **one character** in the
shared bundle, a weight of `6000` that becomes `600`, and every case that
leaned on it goes red, naming the flip:

```text theme={"system"}
$ nika test refund.nika --case fresh-returned --var days=5 --var returned=true
✖ outputs drifted from the golden · refund.nika.fresh-returned.golden.json
  ~ outputs.outcome · golden "recommend" → run "defer"

  intended? re-baseline: nika test refund.nika --update
```

That line is the point. Not "a test failed" but *this rule used to recommend
this case and now defers*. If the flip was intended, re-run that case's line
with `--update` and let the golden diff carry it into review, which is where a
rubric change belongs.

> [!TIP]
>
> Keep the golden narrow. Pinning `outcome` (or the outcome plus one
> dimension `interval`) makes each red line legible. Pinning the whole receipt
> works, but the receipt carries every `weight_bp` and every contribution, so
> a single weight edit churns every golden in the corpus and buries the one
> flip that matters.

### What to know about cases

* **`--var` needs `--case`.** A bound value needs a golden of its own, so
  `nika test refund.nika --var days=5` without a case name exits 3 and says
  so. A file with no `--case` keeps the single `<file>.golden.json`.
* **A case is a command line.** The golden records the outcome, not the
  inputs that produced it, so keep each case's line in a script beside the
  workflow (`decisions/cases.sh`, without `--update`) and run that script in
  CI. One small workflow per case, with its
  facts in `const:`, remains a valid layout when you prefer files to lines.
* **Pin the instant with `--at`.** `--at <RFC 3339 instant>` places the run
  at that time, and `${{ run.scheduled_for }}` reads it, as in a run fired for
  that slot. Without `--at` that value is the current time, so a golden that
  pins it drifts on the next run.
* **Under the mock, the model leg is a constant.** `nika test` swaps every
  `infer:` to the mock provider, which synthesizes a schema-conformant value.
  A case that runs `infer:` then `nika:decide` therefore proves the *law*, and
  proves nothing about the *extraction*: the facts were handed to it. Keep the
  model out of the cases, bind the evidence as inputs, and prove the rubric.
  Extraction quality is a separate exercise against real models, where it
  costs money and is worth what it costs.

## Exit codes · the CI contract

| Exit | Meaning |
| - | - |
| `0` | Outputs match the golden (or `--update` pinned one) |
| `1` | The mock run failed, or outputs drifted from the golden |
| `2` | The file has validation findings (`nika check` would fail — fix those first) |
| `3` | No golden yet (create with `--update`), the file is missing, or `--var` without `--case` |

`nika test` refuses to run a dirty file the same way `nika run` does: a
workflow with check findings exits 2 before anything executes.

## Wire it into CI

Offline and deterministic means no keys in CI, no flakes, no spend:

```yaml .github/workflows/nika-test.yml (fragment) theme={"system"}
- name: Golden-test the workflows
  run: |
    for wf in workflows/*.nika; do
      nika test "$wf"
    done
    sh decisions/cases.sh    # the rule table: one `nika test --case … --var …` line per case
```

Commit the `*.golden.json` files in the same directory as the workflows
they guard. A PR that edits a workflow either keeps its golden green or
shows the re-pinned golden in the diff — both are reviewable.

## Related

* [Concepts · Workflows](https://docs.nika.sh/concepts/workflows.md): typed `inputs:` in,
  typed `outputs:` out — the callable contract the golden pins.
* [Reference · CLI](https://docs.nika.sh/reference/cli.md): every `nika test` flag.
* [Reference · Builtins](https://docs.nika.sh/reference/builtins.md#the-decision-bundle): the
  `nika:decide` bundle grammar, what the kernel enforces, and the receipt
  the cases above pin.
* [Guides · Troubleshooting](https://docs.nika.sh/guides/troubleshooting.md): when `nika check`
  findings block a test (exit 2).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.