# Testing contract and verification

## The testing contract

`devworkspace analyze` infers which verification commands the *project* has and stores them in `.devworkspace/context/commands.json`:

| Check | Available | Command | Required |
| --- | --- | --- | --- |
| typecheck | yes | `npm run typecheck` | yes |
| unit | yes | `npm test` | yes |
| integration | yes | `npm run test:integration` | context |
| e2e | yes | `npm run test:e2e` | context |
| lint | yes | `npm run lint` | yes |
| build | yes | `npm run build` | yes |

`required` is `yes` (always applies to code changes), `context` (only when relevant to the change) or `no`. Entries you or the agent edit by hand get `origin: "user"` and are never overwritten by later analysis.

Every `package.json` in the repository is read (not only the root; manifests in `fixtures/`, `examples/`, `docs/` and `node_modules` are ignored), and each package's commands are recorded **with the directory to run them in** (`cwd`, relative, POSIX), only for scripts that package actually defines. A backend `npm run test:unit` is never mixed up with a frontend `npm test`. Each package uses the package manager its own lockfile indicates. Commands are never executed by DevWorkspace.

Re-running `analyze` keeps any command with `origin: "user"` (confirmed by a person or by Claude during onboarding) exactly as written, even when a later analysis finds nothing; a user command with no `cwd` covers its whole check, one with a `cwd` covers only that directory. `discovery.json` always holds the pure detection; `commands.json` holds the merged contract.

Sources: `package.json` scripts (matched by common names), plus conventions for Python (pytest/ruff/mypy/black/alembic), Go, Rust, Maven and Gradle. npm's default `"no test specified"` placeholder is ignored.

Formatters are listed too. A `format` script that rewrites files is recorded with `"mutates": true` and `required: "no"`; a `format:check` (or a `format` that only checks) is read-only. DevWorkspace never runs a command marked `mutates`.

### Commands are untrusted input

Everything marked `origin: "repository-script"` comes from files in the repository. `analyze`, `status`, `doctor` and `refresh` never run any of them. Only `devworkspace task run` executes a contract command, as an argument vector (never through a shell), and only after the safety rules below. The generated instructions still tell the agent to read what an unfamiliar script does first.

### What `task run` will and will not run

| Command | Runs? |
| --- | --- |
| typecheck, lint, build, unit tests, read-only format checks | yes |
| migrations; anything whose definition mentions drop, truncate, reset, seed, migrate, flush; any command marked `mutates` | **never** |
| integration, security and e2e checks; a catch-all `test` script in a package that depends on a database or Redis | only after you declare a test environment for that package and check |

DevWorkspace does not read `.env` files or connection settings, so it **cannot verify which database a test would use**, and it does not guess from names (a database called `test` is not treated as safe). The only way to allow such a check is an explicit, local declaration by a person:

```bash
devworkspace testenv declare --package backend --check integration --target "disposable docker compose stack" --yes
```

This records your assertion in `.devworkspace/local/test-environment.json` (git-ignored). Targets that look like connection strings or credentials are refused. A refused check is recorded as `COULD_NOT_RUN`, never as a pass.

## Task-specific verification

Questions need none; docs need only documentation checks; bugs, features and refactors derive their checks from the contract (see [routing](routing.md)). Baseline handling:

```bash
devworkspace task tests <id> --phase baseline --passed 214 --failed 3   # before changing code
devworkspace task tests <id> --phase after    --passed 218 --failed 3
devworkspace task run <id> typecheck          # executes the contract command for every package the task changed
devworkspace task verify <id> review pass     # a report; recorded as a claim, not as an executed result
```

### Package-aware completion

A task records, when it is created, which files were already modified. When you complete it, DevWorkspace works out which files changed since, assigns them to packages (the `cwd` directories of the contract; a change outside every package counts for all of them) and requires **executed** evidence for each applicable check **in each changed package**, taken after the last change. A backend-only build is not evidence for frontend changes. A pass that was only reported with `task verify` is a claim and does not satisfy the gate unless you add `--reason user-override`. Tasks created by older versions keep the original rules.

Pre-existing failures are recorded, not blamed on new work. Regressions block completion.

## Failure reporting

```text
STATUS: BLOCKED
Unit tests: FAIL   Typecheck: PASS   Lint: PASS   Build: FAIL
Failure: <actual error>
Task is not considered complete.
```

## Testing DevWorkspace itself

```bash
npm test                 # unit + integration + e2e (vitest)
npm run test:unit | test:integration | test:e2e
npm run coverage         # thresholds enforced (stricter on security, paths, tasks, knowledge, portability)
npm run typecheck && npm run lint
npm run smoke            # pack → install tarball into clean dirs → run the real CLI (local, --prefix, npx, PowerShell)
npm run release:check    # the full pre-publish gate; never publishes
```

Integration manager tests use a scriptable `FakeRunner`, file-effect emulators that mirror the real Graphify CLI, and a real child-process fake `claude`/`uv`/`graphify` (`tests/helpers/fake-tool.mjs`, run as `.cmd` shims in folders with spaces on Windows). Routine tests never touch your real Claude configuration or install anything on the machine.

Layers: **unit** (pure logic, security, paths incl. `path.win32`, schemas, routing scenarios, prompts), **integration** (command flows through the real CLI in-process with a hermetic home directory), **e2e** (the ten acceptance scenarios, Windows-style paths, large repo simulation, concurrency, hostile imports). Fixtures live in `fixtures/` (Express, React/Vite, Next.js, NestJS, FastAPI, monorepo, no tests, failing tests, existing AI files, prompt injection); scenarios that need Git state, secrets or unusual paths are built at test time.

The generated prompts are tested for *behaviour*, not snapshots: `prompts/requirements.ts` lists what every onboarding/refresh prompt must instruct (untrusted-data rule, no secrets, evidence, git safety, testing contract, completion gate, memory rules, …), and `validatePrompt` is used by the tests and by `doctor`.
