Skip to content

Commit b575bbd

Browse files
fix: the test sandbox travels with each engine test, and the engine ignores the test runner
The engine refused to start when NODE_TEST_CONTEXT was in its environment without a sandbox, and the preload npm test adds sandboxed every test file. Node's test runner sets that variable for every process it starts, so an engine command started from a repository's own test exited 1, and such a test could no longer run the engine on its repository or write a file into it. Both worked before the sandbox existed. The sandbox is now a module, tests/engine-sandbox.ts, that every engine test file imports first: the temporary directory for the engine and the node:fs guard on the repository. It holds however the file is started, by hand without the preload included. tests/no-vapi-api.ts is what it was before the sandbox, and src/base-dir.ts never looks at the test runner's environment. A test file that doesn't import the module is left alone: the commands it starts and the engine modules it imports work in its repository, and it writes where it likes. In the engine's own repository (a runner whose GITHUB_REPOSITORY is VapiAI/gitops, or a clone whose origin is) a test file without the import fails the suite. In any other repository that check is skipped. tests/base-dir.test.ts fails when a module under src/ works out the repository's directory for itself. npm test no longer starts a test runner inside a test. The suite run inside copies of a customer-shaped repository, and the other tests that start a runner of their own, move to tests/repository/ and run with npm run test:repository, one file at a time, and as a CI job in the engine's own repository. A suite inside a suite tripled the time npm test took and reached the twenty-second limits some tests give a command sooner on a slow machine. Each copy now holds a test of the customer's own and runs the engine's test files only. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
1 parent 6348a6f commit b575bbd

85 files changed

Lines changed: 1079 additions & 319 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ jobs:
3939

4040
- run: npm test
4141

42+
repository:
43+
# The tests that start a test runner of their own: the suite run inside
44+
# copies of a repository, with every file compared before and after. Only
45+
# in the engine's own repository; a repository built from this one has no
46+
# use for it.
47+
name: The suite leaves its repository alone
48+
if: github.repository == 'VapiAI/gitops'
49+
runs-on: ubuntu-latest
50+
timeout-minutes: 30
51+
52+
steps:
53+
- uses: actions/checkout@v4
54+
55+
- uses: actions/setup-node@v4
56+
with:
57+
node-version: 22
58+
cache: npm
59+
60+
- run: npm ci
61+
62+
- run: npm run test:repository
63+
4264
actionlint:
4365
name: Lint workflows (actionlint)
4466
runs-on: ubuntu-latest

‎AGENTS.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ For changes under `src/`, `tests/` or `.github/`:
375375
use a local HTTP stub, as the existing tests do.
376376
- Run `npx biome check <changed files>` before you finish. It uses the pinned
377377
local Biome version and the config in `biome.json`.
378+
- An engine test file's first import is `import "./engine-sandbox.ts";`. It
379+
makes the engine work in a temporary directory and stops the test changing
380+
files in the repository. A test of your own, for your own repository,
381+
leaves it out and works in the repository as before. The suite asks for
382+
the import only where `origin` is `VapiAI/gitops` itself.
383+
- A test that starts a test runner of its own goes in `tests/repository/`
384+
and runs with `npm run test:repository`, not with `npm test`.
378385
- Changing a command's flags or behaviour? Update
379386
[`docs/guides/commands.md`](docs/guides/commands.md) and the README command
380387
table in the same change.

‎CONTRIBUTING.md‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@ learnings are all welcome.
2626

2727
3. Make your change, with tests for any behaviour change. Tests use
2828
`node:test` and live in `tests/`; they never call the real Vapi API.
29-
4. Run `npx biome check <changed files>`, `npm run build` and `npm test`, then
30-
open a pull request.
29+
Start every new test file with `import "./engine-sandbox.ts";`, before
30+
any other import. It gives the engine a temporary directory to work in
31+
and stops the test changing files in the repository. `npm test` fails
32+
here when a test file is missing it.
33+
4. Run `npx biome check <changed files>`, `npm run build` and `npm test`, then open a pull request.
34+
If you changed a test, or code that writes or removes files, also run
35+
`npm run test:repository`. It runs the suite inside copies of a
36+
repository and checks that no file in them changed. It takes a few
37+
minutes, so it is not part of `npm test`; CI runs it on every pull
38+
request here.
3139

3240
Commit messages and PR titles follow [Conventional Commits](https://www.conventionalcommits.org/)
3341
(`fix(pull): …`, `feat(check): …`, `docs: …`).

‎docs/guides/commands.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The other commands are direct only.
2323
| `npm run promote` | `npm run promote -- --pipeline <p> --from <org> --to <org> [--apply]` | Plan, or apply, a [promotion](promotion.md) between orgs. |
2424
| `npm run build` | `npm run build` | Type-check the code and tests. |
2525
| `npm test` | `npm test` | Run the test suite. |
26+
| `npm run test:repository` | `npm run test:repository` | For work on the engine itself: run the test suite inside copies of a repository and check that no file in them changed. Takes a few minutes. |
2627

2728
Pull and push never automatically rename UUID-suffixed state keys to match
2829
local files. A local file without a UUID mapping remains subject to the

‎improvements.md‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,13 +2163,25 @@ commit it, and the engine can't tell, so the command prints one line: where
21632163
the file goes, why, what has to stage it, and how to start in `state/`
21642164
instead.
21652165

2166-
The test suite never works in the repository it runs in. Every module takes
2167-
the engine's directory from `src/base-dir.ts`, `tests/no-vapi-api.ts` points
2168-
it at a temporary directory and makes every `node:fs` change inside the
2169-
repository throw, and a test started without that file is refused.
2170-
`tests/suite-leaves-the-repository.test.ts` runs the whole suite inside a
2171-
copy of a customer-shaped repository in each layout and compares every file,
2172-
tracked, untracked and ignored, before and after.
2166+
The engine's test suite never works in the repository it runs in. Every
2167+
module takes the engine's directory from `src/base-dir.ts`
2168+
(`tests/base-dir.test.ts` fails when one works it out for itself). Every
2169+
engine test file imports `tests/engine-sandbox.ts` first, which points the
2170+
engine at a temporary directory and makes every `node:fs` change inside the
2171+
repository throw, so the protection holds however the file is started. In
2172+
the engine's own repository a test file without that import fails the suite.
2173+
`tests/repository/suite-leaves-the-repository.test.ts` runs the engine's
2174+
whole suite inside a copy of a customer-shaped repository in each layout and
2175+
compares every file, tracked, untracked and ignored, before and after. It
2176+
and the other tests that start a test runner of their own are not part of
2177+
`npm test`, which takes about as long as it did before them: they run with
2178+
`npm run test:repository` and as a CI job in the engine's own repository.
2179+
2180+
The engine itself never asks whether a test runner is above it. A test that
2181+
doesn't import the sandbox, such as one a repository built from this one
2182+
keeps under `tests/`, is untouched: the commands it starts and the modules
2183+
it imports work in that repository, and it writes where it likes
2184+
(`tests/repository/own-tests-as-before.test.ts`).
21732185

21742186
Two limits remain. The flag is a person's word: if it is given and the commit
21752187
step does not stage `state/`, promotion runs pass while state is never

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"rollback": "tsx src/rollback-cmd.ts",
2424
"promote": "tsx src/promote-cmd.ts",
2525
"build": "tsc --noEmit",
26-
"test": "node --import tsx --import ./tests/no-vapi-api.ts --test tests/*.test.ts"
26+
"test": "node --import tsx --import ./tests/no-vapi-api.ts --test tests/*.test.ts",
27+
"test:repository": "node --import tsx --import ./tests/no-vapi-api.ts --test --test-concurrency=1 tests/repository/*.test.ts"
2728
},
2829
"devDependencies": {
2930
"@biomejs/biome": "2.4.15",

‎src/base-dir.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
// The directory the engine works in: the repository that holds src/.
22
//
3-
// Every module takes it from here, so the test suite can give the engine a
4-
// temporary directory instead. tests/no-vapi-api.ts names the repository the
5-
// suite runs in and a temporary directory to use in its place. Then nothing
6-
// a test does through the engine, and no path a test derives from it, can
7-
// reach that repository. A CLI a test starts from a copy of src/ elsewhere
8-
// keeps working in its own copy.
3+
// Every module takes it from here. tests/base-dir.test.ts fails when one
4+
// works it out for itself; src/user-agent.ts alone reads a file from where
5+
// it sits, the engine's own package.json, and says why.
6+
//
7+
// The engine's own tests each import tests/engine-sandbox.ts first, which
8+
// names the repository they run in and a temporary directory to use in its
9+
// place. Then nothing such a test does through the engine, and no path it
10+
// derives from it, can reach that repository. A command a test starts from
11+
// a copy of src/ elsewhere keeps working in its own copy.
12+
//
13+
// Nothing else changes the answer. In particular the engine never asks
14+
// whether a test runner is somewhere above it: a command run as a command,
15+
// from a test of yours or anywhere else, works in the repository that holds
16+
// its src/.
917

1018
import { dirname, join } from "node:path";
1119
import { fileURLToPath } from "node:url";
1220

1321
export function baseDir(): string {
1422
const own = join(dirname(fileURLToPath(import.meta.url)), "..");
1523
const sandbox = process.env.VAPI_GITOPS_TEST_ROOT;
16-
if (sandbox) return own === process.env.VAPI_GITOPS_TEST_REPO ? sandbox : own;
17-
// Node's test runner sets this for every test file it runs. Without the
18-
// sandbox a test would read and write the real repository: refuse.
19-
if (process.env.NODE_TEST_CONTEXT)
20-
throw new Error(
21-
"The engine was loaded by a test without tests/no-vapi-api.ts, so it would work in this repository instead of a temporary directory. Run tests with `npm test` or `node --import tsx --import ./tests/no-vapi-api.ts --test <file>`.",
22-
);
23-
return own;
24+
return sandbox && own === process.env.VAPI_GITOPS_TEST_REPO ? sandbox : own;
2425
}

‎src/user-agent.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import { readFileSync } from "node:fs";
3232
import { basename, dirname, join } from "node:path";
3333
import { fileURLToPath } from "node:url";
3434

35+
// The engine's own package.json, for its version. Deliberately not
36+
// baseDir() (src/base-dir.ts): in an engine test that is a temporary
37+
// directory with no package.json, and the version sent has to be the real
38+
// one. This is the one module that finds a file from its own location.
3539
const PACKAGE_JSON_PATH = join(
3640
dirname(fileURLToPath(import.meta.url)),
3741
"..",

‎tests/agent-docs.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "./engine-sandbox.ts";
12
import assert from "node:assert/strict";
23
import { readdirSync, readFileSync } from "node:fs";
34
import { join } from "node:path";

‎tests/api-key.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "./engine-sandbox.ts";
12
import assert from "node:assert/strict";
23
import test from "node:test";
34
import {

0 commit comments

Comments
 (0)