# Tests

## First tests: existing Playwright tests or a first regression suite

Research before you ask. Look for Playwright tests in the code: a `playwright.config.*`, `@playwright/test` in any `package.json`, spec files (`*.spec.*`, `e2e/`, `tests/`), and the CI job that runs them. Note how many specs there are, what they cover, and whether CI still runs them.

Then ask the human what they want to do first, offering our two recommended paths with your recommendation and what you found ("Found 40 Playwright specs under e2e/, run on every pull request" or "Found no Playwright tests"):

- Bring in their existing Playwright tests: they run on a fresh sandbox, unchanged apart from the config fixes below. Recommend this when you found a suite CI still runs.
- Set up a first regression suite: 3 to 5 Cofactor tests of the core journeys, kept working by Cofactor. Recommend this when there are no Playwright tests, or only a few or stale ones.

Tell them that either way, `nightly` puts the suite on a nightly schedule with failure alerts to Slack or email, and you run it once before you report.

Find the sandbox environment's name in `cofactor environment list --application <app>` (type sandbox); it may differ from the definition's.

### Existing Playwright tests

Before the first run, run a gap check: read their CI job and Playwright config, list each item, and fix the config now (the tests run on a fresh copy of the app, not on the hosts they use today):

- fixed hosts or base URLs to repoint at the sandbox (the runner sets `BASE_URL`, `APP_BASE_URL` and `PLAYWRIGHT_BASE_URL` to the copy's URL); real third-party accounts or services the sandbox cannot provide (the preview blockers); outside data the tests assume exists, to become a seed (`cofactor sandbox guide seeds-and-actions`);
- secrets and personas: the runner passes each persona's login as `COFACTOR_PERSONAS` JSON, so change their global setup to sign in from it (`cofactor sandbox guide personas`). Frontend-only: take only each persona's email, never its password; the human stores each backend password as a sensitive env var (`<PERSONA>_PASSWORD`), mapped as `'$var.ENV.<PERSONA>_PASSWORD'`;
- specs to skip first, so the first run is green: move the failures into a skip list to fix later.

Choose the source without asking: `--source image` if the app was pushed as an image and the tests are packaged in it; otherwise upload from the local checkout, or GitHub when the tests live in a repository the human wants pinned by commit. Every source needs a `cofactor.tests.yaml` (for an image, in the image's WORKDIR) with the install command, the Playwright config and `${app.url}` and `${personas.<name>.email}` references; run `cofactor test-suite validate <dir>` on the local copy before building or pushing, and read `cofactor test-suite guide declaration`. `@playwright/test` and `playwright` must be exactly 1.62.1, on Node 22.16 and linux/amd64. The suite runs against the sandbox environment, so pass `--environment` with every source.

```bash
cofactor test-suite create --name "<App> Playwright" --kind external --provider playwright --source image --application <app> --environment <sandbox environment>
cofactor test-suite create --name "<App> Playwright" --kind external --provider playwright --source upload --application <app> --environment <sandbox environment>   # bundles the checkout without .git, node_modules and .env files
cofactor test-suite push <suite> <directory>   # upload: push again after every change to the tests
cofactor application repo link --application <app> --repo <owner/name>   # needs the Cofactor GitHub App: `cofactor github connect`
cofactor test-suite create --name "<App> Playwright" --kind external --provider playwright --source github --repo <owner/name> --environment <sandbox environment>
cofactor test-suite update <suite> --env-map <VAR>=<value>   # only if the tests read variables: their CI's vars, never the base URLs; replaces the whole map
cofactor test-suite run --suite <suite> --external-spec <one spec path>   # one spec first, then the whole suite
```

A spec that needs a secret itself: ask the human to run, in their own shell with the value in an environment variable, `printf %s "$VALUE" | cofactor env set-var --application <app> --environment <sandbox environment> --sensitive --stdin NAME`, and map `NAME='$var.ENV.NAME'`, never the literal.

Done when one spec passes on the sandbox, then the whole suite has run once.

### A first regression suite

`cofactor skill install cofactor-write-test` and write 3 to 5 tests for the core journeys (sign-in, the main thing a user creates, the main thing they pay for); `cofactor workflow guide write-a-test` has the format. Before a test's first run, point it at the sandbox: `cofactor workflow patch <wf_...> --default-environment <sandbox environment>`; then `cofactor workflow run <wf_...>` runs it on a fresh copy. New map items default to in_dev; use `--stage <in_dev|alpha|beta|ga|deprecated>` on area/capability create|update and rule add|update only when instructed. Never set a child above its parent; promote parents first and deprecate children first. Record what each proves in the product map (`cofactor product-map guide`, `product-map area create <title>`, `product-map capability create <title> --area <CODE>`, `product-map link <CAP> <wf_...>`).

Done when every test has passed once on the sandbox and is linked to a capability.

### How cost works (use it when choosing what tests to write)

- The base plan is $99/month and includes a few thousand test executions. One execution is any flow run (in design or draft mode, or in a suite) or any single Playwright test run: one test paired with one environment. Fully cached, deterministic executions (Playwright tests included) are plentiful and count within those.
- Agent time is metered separately as agent hours: live healing, failure diagnosis and agent-only testing (MCP tests, exploratory tests). Plans include a free allotment; never quote a number.
- Prefer deterministic tests for anything that runs every night. Before setting up an MCP or exploratory test, tell the human it spends agent hours on every run and weigh that against what it covers that a deterministic test cannot.

Next: `cofactor setup guide nightly`
