Skip to content

Commit 6348a6f

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 c6262a8 commit 6348a6f

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
@@ -313,19 +313,45 @@ writing command stops before it changes anything and names the two files. If
313313
they hold the same content, the engine reads the one in `state/`, keeps the
314314
old copy identical, and says that `npm run migrate` removes it.
315315

316+
### A repository with no state yet
317+
318+
A new repository starts in `state/` when a workflow in `.github/workflows/`
319+
is seen to stage it, as upstream's `promotion.yml` does, or when
320+
`state/commit-step.json` says your commit step does. Otherwise the first state
321+
file is written at the root, as `.vapi-state.<org>.json`, where every older
322+
commit step looks for it, and the command says so in one line:
323+
324+
```
325+
ℹ️ 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. …
326+
```
327+
328+
If your commit step stages only `state/` and the engine can't read it (a
329+
script, a commit action, another CI system), that file would never be
330+
committed. Before the first `pull`, either make the step stage
331+
`.vapi-state.*.json` as well, or tell the engine the step stages `state/` and
332+
commit the file it writes:
333+
334+
```bash
335+
npm run migrate -- --commit-step-stages-state
336+
git add state/commit-step.json
337+
```
338+
316339
### What you'll see after upgrading
317340

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

321348
After `npm run migrate`:
322349

323350
- **`📦 Moved … into state/<org>/`**, once per org, from `migrate` itself.
324-
- **`📦 1 locally modified file(s) will be preserved`** on a `pull` before you
325-
commit the move. The file is the old org pin,
326-
`resources/<org>/.vapi-org.json`. It moved to `state/<org>/org.json`, so git
327-
shows it as deleted until you commit. Pull does nothing differently because
328-
of it.
351+
- **Nothing new from `pull` before you commit the move.** Git shows the old
352+
org pin, `resources/<org>/.vapi-org.json`, as deleted until you commit. Pull
353+
doesn't count that, or any other file the engine writes, as an edit of
354+
yours, so it does nothing differently because of it.
329355
- **`📦 Moved .vapi-state-hash/<org>/ … into state/<org>/local/`**, once, on
330356
each teammate's machine after they pull your commit. Those are that
331357
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
@@ -2150,7 +2150,26 @@ the state file, and in a fresh clone that is the order git wrote the files
21502150
in: every file was kept with state at the root, and every file would have
21512151
been overwritten with state in `state/`. Pull now compares against the moment
21522152
this machine's engine last saved state, kept beside the baselines, and a
2153-
clone with no such save keeps every file.
2153+
clone with no such save keeps every file. The same rule is off whenever git
2154+
shows a change under `resources/<org>/`, and the engine keeps files of its own
2155+
there in the older layout. Pull leaves those out (`engineFiles` in
2156+
`src/state-paths.ts`, built from the list `migrate` moves), so a file the
2157+
engine wrote, or one `migrate` moved and git shows as deleted, is never
2158+
counted as a person's edit.
2159+
2160+
A repository with no state at all, whose commit step can't be read, gets its
2161+
first state file at the root. A step that stages only `state/` would never
2162+
commit it, and the engine can't tell, so the command prints one line: where
2163+
the file goes, why, what has to stage it, and how to start in `state/`
2164+
instead.
2165+
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.
21542173

21552174
Two limits remain. The flag is a person's word: if it is given and the commit
21562175
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,7 +1,8 @@
11
import { execSync } from "child_process";
2-
import { dirname, join, resolve } from "path";
2+
import { join, resolve } from "path";
33
import { fileURLToPath } from "url";
44
import { parseAdoptArgs } from "./adopt.ts";
5+
import { baseDir } from "./base-dir.ts";
56
import { assertStateMigrated } from "./migrate-hash-store.ts";
67
import {
78
stateFileRead,
@@ -19,8 +20,7 @@ import "./user-agent.ts";
1920
// 3. If conflicts, stop — user resolves, then runs push manually
2021
// ─────────────────────────────────────────────────────────────────────────────
2122

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

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

‎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,8 +1,8 @@
11
import { existsSync, readFileSync } from "fs";
2-
import { basename, dirname, join, relative, resolve } from "path";
3-
import { fileURLToPath } from "url";
2+
import { basename, join, relative, resolve } from "path";
43
import { parseAdoptArgs } from "./adopt.ts";
54
import { missingApiKeyMessage, resolveOrgApiKey } from "./api-key.ts";
5+
import { baseDir } from "./base-dir.ts";
66
import {
77
ignorePatternsRead,
88
matchesIgnore as matchesIgnorePatterns,
@@ -235,8 +235,7 @@ function loadEnvFile(env: string, baseDir: string): Record<string, string> {
235235
// ─────────────────────────────────────────────────────────────────────────────
236236

237237
// Base directory for the gitops project
238-
const __dirname = dirname(fileURLToPath(import.meta.url));
239-
export const BASE_DIR = join(__dirname, "..");
238+
export const BASE_DIR = baseDir();
240239

241240
// Parse environment, flags, and load env files
242241
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)