Skip to content

Commit 7025678

Browse files
fix: keep the test suite out of the repository it runs in, and engine files out of pull's local edits
Running the test suite in a repository with a state file at its root removed the repository: two tests cleaned up two folders above the engine's baselines folder, and in the older layout that is the repository root. The engine's directory now comes from one place, src/base-dir.ts. The test preload points it at a temporary directory, makes every node:fs change inside the repository throw, and the engine refuses a test started without the preload. A new test runs the whole suite inside a copy of a customer-shaped repository in the older layout, in state/, and with no state, and compares every tracked, untracked and ignored file before and after. That run found one more writer, tests/org-connection.test.ts, which created tmp/ in the repository. Pull's keep rule for a file with no baseline is off whenever git shows a change under resources/<org>/. The engine's own files there (the org pin in the older layout, also shown as deleted between migrate and its commit) are no longer counted. A repository with no state whose commit step can't be read still gets its first state file at the root, and the command now says where the file goes, why, and what has to stage it. The upgrade guide gains one line: on a machine with no baselines, the first pull after upgrading can bring a dashboard edit down one pull late. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
1 parent c451ede commit 7025678

24 files changed

Lines changed: 1117 additions & 98 deletions

‎docs/guides/commands.md‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,45 @@ writing command stops before it changes anything and names the two files. If
229229
they hold the same content, the engine reads the one in `state/`, keeps the
230230
old copy identical, and says that `npm run migrate` removes it.
231231

232+
### A repository with no state yet
233+
234+
A new repository starts in `state/` when a workflow in `.github/workflows/`
235+
is seen to stage it, as upstream's `promotion.yml` does, or when
236+
`state/commit-step.json` says your commit step does. Otherwise the first state
237+
file is written at the root, as `.vapi-state.<org>.json`, where every older
238+
commit step looks for it, and the command says so in one line:
239+
240+
```
241+
ℹ️ No engine state in this repository yet: it goes to .vapi-state.<org>.json at the repository root, because … Whatever commits state after a run has to stage .vapi-state.*.json. …
242+
```
243+
244+
If your commit step stages only `state/` and the engine can't read it (a
245+
script, a commit action, another CI system), that file would never be
246+
committed. Before the first `pull`, either make the step stage
247+
`.vapi-state.*.json` as well, or tell the engine the step stages `state/` and
248+
commit the file it writes:
249+
250+
```bash
251+
npm run migrate -- --commit-step-stages-state
252+
git add state/commit-step.json
253+
```
254+
232255
### What you'll see after upgrading
233256

234257
- **`ℹ️ Engine state here is in the older layout …`**, one line per writing
235258
command, until you run `npm run migrate`. Nothing else changes.
259+
- **A dashboard edit can arrive one pull late, once.** On a machine with no
260+
baselines yet, the first `pull` after upgrading keeps every local file, and
261+
an edit made in the dashboard comes down on the next `pull`. Nothing is
262+
lost.
236263

237264
After `npm run migrate`:
238265

239266
- **`📦 Moved … into state/<org>/`**, once per org, from `migrate` itself.
240-
- **`📦 1 locally modified file(s) will be preserved`** on a `pull` before you
241-
commit the move. The file is the old org pin,
242-
`resources/<org>/.vapi-org.json`. It moved to `state/<org>/org.json`, so git
243-
shows it as deleted until you commit. Pull does nothing differently because
244-
of it.
267+
- **Nothing new from `pull` before you commit the move.** Git shows the old
268+
org pin, `resources/<org>/.vapi-org.json`, as deleted until you commit. Pull
269+
doesn't count that, or any other file the engine writes, as an edit of
270+
yours, so it does nothing differently because of it.
245271
- **`📦 Moved .vapi-state-hash/<org>/ … into state/<org>/local/`**, once, on
246272
each teammate's machine after they pull your commit. Those are that
247273
machine's own baselines and snapshots, which are not in git.

‎improvements.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,26 @@ the state file, and in a fresh clone that is the order git wrote the files
21312131
in: every file was kept with state at the root, and every file would have
21322132
been overwritten with state in `state/`. Pull now compares against the moment
21332133
this machine's engine last saved state, kept beside the baselines, and a
2134-
clone with no such save keeps every file.
2134+
clone with no such save keeps every file. The same rule is off whenever git
2135+
shows a change under `resources/<org>/`, and the engine keeps files of its own
2136+
there in the older layout. Pull leaves those out (`engineFiles` in
2137+
`src/state-paths.ts`, built from the list `migrate` moves), so a file the
2138+
engine wrote, or one `migrate` moved and git shows as deleted, is never
2139+
counted as a person's edit.
2140+
2141+
A repository with no state at all, whose commit step can't be read, gets its
2142+
first state file at the root. A step that stages only `state/` would never
2143+
commit it, and the engine can't tell, so the command prints one line: where
2144+
the file goes, why, what has to stage it, and how to start in `state/`
2145+
instead.
2146+
2147+
The test suite never works in the repository it runs in. Every module takes
2148+
the engine's directory from `src/base-dir.ts`, `tests/no-vapi-api.ts` points
2149+
it at a temporary directory and makes every `node:fs` change inside the
2150+
repository throw, and a test started without that file is refused.
2151+
`tests/suite-leaves-the-repository.test.ts` runs the whole suite inside a
2152+
copy of a customer-shaped repository in each layout and compares every file,
2153+
tracked, untracked and ignored, before and after.
21352154

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

‎src/apply.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { execSync } from "child_process";
2-
import { dirname, join, resolve } from "path";
2+
import { join, resolve } from "path";
33
import { fileURLToPath } from "url";
4+
import { baseDir } from "./base-dir.ts";
45
import { assertStateMigrated } from "./migrate-hash-store.ts";
56
import {
67
stateFileRead,
@@ -18,8 +19,7 @@ import "./user-agent.ts";
1819
// 3. If conflicts, stop — user resolves, then runs push manually
1920
// ─────────────────────────────────────────────────────────────────────────────
2021

21-
const __dirname = dirname(fileURLToPath(import.meta.url));
22-
const BASE_DIR = join(__dirname, "..");
22+
const BASE_DIR = baseDir();
2323

2424
const SLUG_RE = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/;
2525

‎src/base-dir.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// The directory the engine works in: the repository that holds src/.
2+
//
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.
9+
10+
import { dirname, join } from "node:path";
11+
import { fileURLToPath } from "node:url";
12+
13+
export function baseDir(): string {
14+
const own = join(dirname(fileURLToPath(import.meta.url)), "..");
15+
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+
}

‎src/call.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { missingApiKeyMessage, resolveOrgApiKey } from "./api-key.ts";
22
import { execSync } from "child_process";
33
import { existsSync, readFileSync } from "fs";
44
import { createRequire } from "module";
5-
import { dirname, join, resolve } from "path";
5+
import { join, resolve } from "path";
66
import * as readline from "readline";
77
import { fileURLToPath } from "url";
8+
import { baseDir } from "./base-dir.ts";
89
import { stateFileLabel, stateFileRead } from "./state-paths.ts";
910
import type { Environment, StateFile } from "./types.ts";
1011
import { userAgentGet, vapiClientGet } from "./user-agent.ts";
@@ -15,8 +16,7 @@ const require = createRequire(import.meta.url);
1516
// Configuration
1617
// ─────────────────────────────────────────────────────────────────────────────
1718

18-
const __dirname = dirname(fileURLToPath(import.meta.url));
19-
const BASE_DIR = join(__dirname, "..");
19+
const BASE_DIR = baseDir();
2020

2121
type ResourceType = "assistant" | "squad";
2222

‎src/check-cmd.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import { appendFileSync, mkdirSync, writeFileSync } from "node:fs";
1414
import { dirname, join, relative, resolve } from "node:path";
1515
import { fileURLToPath } from "node:url";
16+
import { baseDir } from "./base-dir.ts";
1617
import type { CheckJob } from "./check-build.ts";
1718
import { checkJobsBuild, checkTargetLabel } from "./check-build.ts";
1819
import type { CheckDefinition, ChecksConfig } from "./check-config.ts";
@@ -399,8 +400,7 @@ async function checksRun(
399400
export async function checkCommandRun(
400401
args = process.argv.slice(2),
401402
root = resolve(
402-
process.env.VAPI_GITOPS_ROOT ??
403-
fileURLToPath(new URL("..", import.meta.url)),
403+
process.env.VAPI_GITOPS_ROOT ?? baseDir(),
404404
),
405405
): Promise<number> {
406406
let parsed: CheckArgs;

‎src/config.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { missingApiKeyMessage, resolveOrgApiKey } from "./api-key.ts";
22
import { existsSync, readFileSync } from "fs";
3-
import { basename, dirname, join, relative, resolve } from "path";
4-
import { fileURLToPath } from "url";
3+
import { basename, join, relative, resolve } from "path";
4+
import { baseDir } from "./base-dir.ts";
55
import type { Environment, ResourceType } from "./types.ts";
66
import {
77
ignorePatternsRead,
@@ -226,8 +226,7 @@ function loadEnvFile(env: string, baseDir: string): Record<string, string> {
226226
// ─────────────────────────────────────────────────────────────────────────────
227227

228228
// Base directory for the gitops project
229-
const __dirname = dirname(fileURLToPath(import.meta.url));
230-
export const BASE_DIR = join(__dirname, "..");
229+
export const BASE_DIR = baseDir();
231230

232231
// Parse environment, flags, and load env files
233232
export const VAPI_ENV = parseEnvironment();

‎src/hash-store.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
import { existsSync, mkdirSync, readFileSync } from "fs";
2222
import { rename, writeFile } from "fs/promises";
23-
import { dirname, join } from "path";
24-
import { fileURLToPath } from "url";
23+
import { join } from "path";
24+
import { baseDir } from "./base-dir.ts";
2525
import {
2626
baselinesDir,
2727
baselinesLegacyDir,
2828
baselinesWriteDir,
2929
} from "./state-paths.ts";
3030

3131
// Same expression as config.ts: the repo root is one level up from src/.
32-
const BASE_DIR = join(dirname(fileURLToPath(import.meta.url)), "..");
32+
const BASE_DIR = baseDir();
3333

3434
// Exported so the migration works on the same repository root.
3535
export const HASH_STORE_BASE_DIR = BASE_DIR;

‎src/interactive.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { missingApiKeyMessage, resolveOrgApiKey } from "./api-key.ts";
22
import { confirm, select } from "@inquirer/prompts";
33
import { execSync } from "child_process";
44
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
5-
import { dirname, extname, join, relative } from "path";
6-
import { fileURLToPath } from "url";
5+
import { extname, join, relative } from "path";
6+
import { baseDir } from "./base-dir.ts";
77
import searchableCheckbox, { BACK_SENTINEL } from "./searchableCheckbox.js";
88
// slug-utils is config-free (no config.ts side effects) — safe to import in
99
// the launcher, which runs before any org/token is selected.
@@ -16,8 +16,7 @@ import { userAgentGet, vapiClientGet } from "./user-agent.ts";
1616
// Constants
1717
// ─────────────────────────────────────────────────────────────────────────────
1818

19-
const __dirname = dirname(fileURLToPath(import.meta.url));
20-
const BASE_DIR = join(__dirname, "..");
19+
const BASE_DIR = baseDir();
2120

2221
const SLUG_RE = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/;
2322

‎src/promote-cmd.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { execFileSync } from "node:child_process";
22
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
33
import { dirname, resolve } from "node:path";
44
import { fileURLToPath } from "node:url";
5+
import { baseDir } from "./base-dir.ts";
56
import type { CheckDefinition } from "./check-config.ts";
67
import type { OrgConnection } from "./org-connection.ts";
78
import {
@@ -41,9 +42,7 @@ interface PromotionTransition {
4142
definition: PromotionPipeline;
4243
}
4344

44-
const ROOT_DIR = resolve(
45-
process.env.VAPI_GITOPS_ROOT ?? fileURLToPath(new URL("..", import.meta.url)),
46-
);
45+
const ROOT_DIR = resolve(process.env.VAPI_GITOPS_ROOT ?? baseDir());
4746

4847
// Files that transitions which finished applying left changed, one
4948
// `<blob>\t<path>` line each: the content as it stood when that transition

0 commit comments

Comments
 (0)