# Test Data Lifecycle

Test data discipline keeps workflows reliable and concurrent-safe.

## Strong tests: defaults and their workarounds

These are strong defaults, not absolutes. A test may deviate when the
application makes a rule impossible — but it defaults to the rule, uses the
workaround named here, and says so in `## Preflight` / `## Cleanup` so the
deviation is visible, never silent.

`## Preflight` and `## Cleanup` are the workflow's own preflight and cleanup
notes (its preflight and cleanup fields, set with `cofactor workflow patch
--preflight` / `--cleanup`), never the workflow description. People read them in the
web UI: write them as short markdown bullet lists and name seeds,
teardowns, connections and records, never raw IDs.

1. **Idempotent** — running the test again, including after a FAILED run,
   passes without a manual reset. Unique-name macros ("QA Test —
   `$RANDOM_STRING(6)`", `$EMAIL()`) make this true even when cleanup never
   happened.

   Workaround: if the app forbids duplicates (a singleton setting, one
   config record), revert the change in `## Cleanup` and say so.

2. **Controls its own data** — the test creates every record it operates on.
   Inline creation is the normal case: if the data is unique to this test,
   create it in the test. Use a seed workflow only when the data is (or
   should be) reused across several tests — a seed with one consumer is
   overhead, not hygiene. (Setup nodes are deprecated; do not use them.)

   Workaround: depending on data that already exists on the environment
   is fine AS LONG AS the test only reads it and never mutates it —
   confirm with the user that the discovered fixture is reliable for
   repeated runs, then declare it in `## Preflight` ("Property 'Acme Demo' exists with ≥1
   issue"). A test that must mutate pre-existing data creates its own
   copy first, or states in Preflight why it can't.

3. **Isolated** — the test works only on data it created and never modifies
   shared resources as a side effect (billing, integrations, API keys, org
   config). Match the isolation level to the blast radius:

   - record-level: unique-id macros in a shared tenant — ordinary CRUD
   - tenant/org-level: a seeded org — the test mutates org-wide state
   - application-level: an ephemeral env / sandbox — it mutates global state

   Workaround: tests that genuinely depend on one another run as ordered
   LAYERS in a test suite (`cofactor test-suite ... --layer`, optionally
   `--failure-mode stop_on_layer_failure`). Layers run sequentially; members
   within a layer run in parallel. Never rely on implicit ordering between
   standalone flows.

4. **Cleanup survives failure** — a fatal assertion halts the run, so trailing
   inline cleanup nodes do NOT run. Seed-paired teardown workflows run on
   both pass and fail. Inline cleanup is therefore best-effort and pairs
   with unique naming ("QA Test" prefix + macro) so orphans are harmless
   and identifiable. When a record MUST be removed even after a failure
   (quota-bound, billed, visible to real users), that is the reason to move
   it into a seed + teardown pair.

5. **External dependencies are declared or captured** — email delivery: give a
   seed credential output "Create email inbox" (the Email Inbox feature;
   sandbox email needs no setup, see Settings > Email) so the flow can read the message. Payments, third-party
   APIs, feature flags: declare them in `## Preflight` (`cofactor feature-flag
   list --env <env>` for exact flag names). Never assume an external
   system's state silently.

## Seed only what is reused

A seed workflow runs before your test, creates data, and exports variables
downstream workflows reference as `$var.{dataKey}`. Reach for one when the
same data serves several tests. When it does, you also get:

- Faster setup — API sequences instead of browser interactions
- Cleaner separation of setup and test logic
- Paired teardown that runs on pass AND fail

If only this test needs the data, create it inline (see Creating test data
below). See the Seed Workflows section for how to build seeds.

## Sandbox base-seed data (`$var.SEED.*`)

On a sandbox environment, core baseline data (the application/property, a
credential, sometimes a starter workflow) is provisioned automatically by a
base seed and exposed as `$var.SEED.{key}` — e.g. `$var.SEED.property_id`.

Prefer these over hardcoded names when you need a core entity that already
exists on every tenant:

- Good: "Open the property `$var.SEED.property_id`"

Base seeds are core-only. Anything test-specific (an issue, a notification,
run history) is NOT in the base seed — create it yourself (see below) or via
a per-test seed/custom action.

## The data ownership principle

Own your data:

- Data you create → clean it up when the workflow finishes
- Settings you modify → revert them when the workflow finishes
- Data that was already there → never delete it

It's fine to depend on pre-existing data (existing integrations, org
settings, reference data) as long as the test only reads it — declare
it in `## Preflight` (rule 2 above). Create everything the test mutates.

## Creating test data

If the test operates on a record (edit, delete, archive), create that
record as an early step. Operating on a record that already existed is
a documented deviation, not the default (rule 2 above).

Never reference "any" resource:

- Bad: "Pick a project from the list" (which one? may not exist)
- Good: "Create a project named 'QA Test — `$RANDOM_STRING(6)`'"
- Good: "Use the seed output `$var.project_id`"

Use macros in newly created names to ensure uniqueness across parallel runs:

- Bad: "Test Item" (generic, will collide with other runs)
- Bad: "QA Test Project" (still collides if two runs overlap)
- Good: "QA Test — `$RANDOM_STRING(6)`" (unique every run)
- Good: "QA Test `$FIRST_NAME()` `$LAST_NAME()`" (unique and readable)

After creating data, verify it exists before continuing.

## Naming convention

All test data should follow this pattern:

```text
Name/Title:    "QA Test $FIRST_NAME() $LAST_NAME()"
Email:         "$EMAIL()"
Phone:         "$PHONE()"
Project/Item:  "QA Test — $RANDOM_STRING(6)"
Unique ID:     "QA$TIMESTAMP()"
```

The "QA Test" prefix makes test data instantly recognizable. The macro
suffix ensures every run creates unique data that won't collide with
other runs or leftover data from previous executions.

## Cleaning up (strongly recommended)

Workflows that create data should include cleanup steps at the end.
Workflows that modify settings should revert them.

This isn't strictly required, but it's strongly recommended:

- Cleanup prevents data accumulation over time
- It keeps the test environment clean for other workflows
- The "QA Test" naming convention makes orphaned data identifiable
  even when cleanup is skipped or fails

Inline cleanup is best-effort: a fatal assertion halts the run before the
cleanup nodes execute. Seed-paired teardowns run on pass and fail. Don't
over-engineer inline cleanup — "navigate to the item and delete it" is
enough; if a record must be gone even after a failure, provision it from a
seed and let its teardown remove it (rule 4 above).

## What not to touch

- Don't create users, roles, or org-level config unless that's the goal
- Don't modify shared resources (billing, integrations, API keys) as a side effect
- Don't delete or modify data not owned by this workflow

## Avoiding collisions with macros

Use macros for newly created test names, emails, and identifiers. Keep
confirmed existing reference names and captured identities intact. When every run generates unique values,
there's no risk of conflicting with parallel runs or leftover data:

| Macro | Description |
| --- | --- |
| `$EMAIL()` | Unique email every run |
| `$RANDOM_STRING(6)` | Short unique suffix for names |
| `$UUID()` | Globally unique identifier |
| `$TIMESTAMP()` | Unique-per-millisecond numeric ID |

Combined with the "QA Test" prefix, this means you never need to
worry about whether data from a previous run still exists. Each run
creates its own isolated dataset.

## Read-only flows

Workflows that only read data (verify dashboard loads, check reports)
don't need record creation or deletion. Restore any view state the journey
changes. The presence of unrelated create/edit/delete controls does not
expand the test's scope: exercise the specified behavior. A journey that
tests writes owns those records and verifies their lifecycle. Only create
what the test specifically requires.
