From 672d873f7f4625a1f63ad77bc4edb3edd469e09d Mon Sep 17 00:00:00 2001 From: Amm0Bot Date: Thu, 26 Nov 2026 15:53:39 -0800 Subject: [PATCH 1/6] test: give the integration tier a hermetic HOME (#654) An encrypted CDB at the developer's default path failed CHR tests with cdb/password-required before they reached the router. The integration preload now pins quickchr's data dir to the real path, then points HOME and XDG_CONFIG_HOME at a per-run sandbox, so Bun.env, runCli and child processes never see ~/.config/tikoci. Bun fixes os.homedir() at startup, so the 30 in-process calls that passed a bare {} env map now pass HERMETIC_ENV. Session: 4f3a1170-9bf0-4eac-97bd-7c9c9d0e6bbd Co-Authored-By: Claude Opus 5.5 --- test/AGENTS.md | 5 +++ test/integration/btest-client.test.ts | 3 +- test/integration/discover.test.ts | 5 +-- test/integration/ec-srp5.test.ts | 3 +- test/integration/execute-write-gate.test.ts | 28 +++++++++++------ test/integration/explain-live-facets.test.ts | 7 +++-- test/integration/explain-live-types.test.ts | 3 +- test/integration/explain-live.test.ts | 11 ++++--- test/integration/mcp.test.ts | 7 +++-- test/integration/preload.ts | 31 +++++++++++++++++-- test/integration/rest-permission.test.ts | 5 +-- .../routeros-error-classes.test.ts | 7 +++-- 12 files changed, 83 insertions(+), 32 deletions(-) diff --git a/test/AGENTS.md b/test/AGENTS.md index a92f79c2..9a342168 100644 --- a/test/AGENTS.md +++ b/test/AGENTS.md @@ -5,6 +5,11 @@ - Collect coverage for trend visibility, not as a percentage gate. - Put long-running, RouterOS-backed, or platform-specific tests under `test/integration/` and wire them through QA or lab workflows. "Platform-specific" includes process-level tests that spawn the real `src/cli.ts` through `cli-process.ts` — including the **network-free** CLI smoke tier (`cli-smoke.test.ts`), which is not CHR-gated and so runs in the fast push/PR gate (`bun test`). - Keep fixtures under `test/fixtures/` with clear source/provenance notes. +- Both tiers run with a sandbox HOME (`test/unit/preload.ts`, + `test/integration/preload.ts`), but Bun fixes `os.homedir()` at startup. A + call that hands centrs its own env map passes `HERMETIC_ENV` + (`test/unit/hermetic-env.ts`) or a map with its own `HOME`, never a bare `{}` + — that falls back to the developer's real CDB and `centrs.env` (#654). ## Device-dependent expectations: declare the split, and prove it on long-term diff --git a/test/integration/btest-client.test.ts b/test/integration/btest-client.test.ts index aaea81dc..79afb73c 100644 --- a/test/integration/btest-client.test.ts +++ b/test/integration/btest-client.test.ts @@ -31,6 +31,7 @@ import { describe, expect, test } from "bun:test"; import { btestClient } from "../../src/btest.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, recordIntegrationEvidence, @@ -86,7 +87,7 @@ describeFast( direction: "receive", durationMs: CYCLE_MS, intervalMs: 1000, - env: {}, + env: HERMETIC_ENV, ...args, }); diff --git a/test/integration/discover.test.ts b/test/integration/discover.test.ts index ad395d3c..614021f2 100644 --- a/test/integration/discover.test.ts +++ b/test/integration/discover.test.ts @@ -37,6 +37,7 @@ import { import { loadCdb, showDevice } from "../../src/devices.ts"; import { discover } from "../../src/discover.ts"; import { parseCommentKv } from "../../src/resolver/comment-kv.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, MNDP_DISCOVER_INTERVAL_SINCE, @@ -172,7 +173,7 @@ describeFast("discover over MNDP against CHR (socket-connect L2)", () => { sendRefresh: false, save: true, cdbFile: cdbPath, - env: {}, + env: HERMETIC_ENV, signal: controller.signal, onBound: (port) => bridge.setUdpTarget(port, "127.0.0.1"), }); @@ -300,7 +301,7 @@ describeFast("discover over MNDP against CHR (socket-connect L2)", () => { // --save persisted the neighbor (no IPv4 on ether2 → MAC target). expect(envelope.meta.operation?.saved?.added ?? 0).toBeGreaterThan(0); expect(envelope.meta.operation?.saved?.group).toBe("discovered"); - const cdb = await loadCdb({ cdbFile: cdbPath, env: {} }); + const cdb = await loadCdb({ cdbFile: cdbPath, env: HERMETIC_ENV }); const shown = showDevice({ cdb, target: match.mac ?? "" }); expect(shown.ok).toBe(true); if (!shown.ok) return; diff --git a/test/integration/ec-srp5.test.ts b/test/integration/ec-srp5.test.ts index 9e211c4c..242d5293 100644 --- a/test/integration/ec-srp5.test.ts +++ b/test/integration/ec-srp5.test.ts @@ -26,6 +26,7 @@ import { sha256, } from "../../src/protocols/ec-srp5.ts"; import vectors from "../fixtures/ec-srp5.json"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, recordIntegrationEvidence, @@ -148,7 +149,7 @@ describeFast("EC-SRP5 minimal-length RouterOS discriminators (#565)", () => { timeout: 55_000, yes: true, }, - {}, + HERMETIC_ENV, ).then((result) => { if (!result.ok) throw new Error(`${result.error.code}: ${result.error.summary}`); diff --git a/test/integration/execute-write-gate.test.ts b/test/integration/execute-write-gate.test.ts index d5a04aa3..9fb786de 100644 --- a/test/integration/execute-write-gate.test.ts +++ b/test/integration/execute-write-gate.test.ts @@ -3,6 +3,7 @@ import { executeEnvelope } from "../../src/execute.ts"; import { resolveMcpConfig } from "../../src/mcp/config.ts"; import { handleExecute } from "../../src/mcp/tools.ts"; import { retrieve } from "../../src/retrieve.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { makeMcpTestCdb } from "../unit/mcp-cdb-fixture.ts"; import { isChrIntegrationEnabled, @@ -48,7 +49,7 @@ describeFast("output-file write gates against CHR", () => { ); const note = await retrieve( { targetInput: chr.restUrl, path: "/system/note", ...auth }, - {}, + HERMETIC_ENV, ); expect(note.ok).toBe(true); expect((note.data as { note?: string }).note).toBe(""); @@ -61,7 +62,10 @@ describeFast("output-file write gates against CHR", () => { }, ]); cleanupCdb = cdb.cleanup; - const config = resolveMcpConfig({ cdbFile: cdb.path, env: {} }); + const config = resolveMcpConfig({ + cdbFile: cdb.path, + env: HERMETIC_ENV, + }); for (const via of ["rest-api", "native-api"] as const) { const base = { targetInput: via === "rest-api" ? chr.restUrl : "127.0.0.1", @@ -79,7 +83,7 @@ describeFast("output-file write gates against CHR", () => { : `/system identity export fi${separator}"${name}"`; const refused = await executeEnvelope( { ...base, command, stdinIsTty: false }, - {}, + HERMETIC_ENV, ); expect(refused.ok ? undefined : refused.error.code).toBe( "usage/confirmation-required", @@ -93,7 +97,10 @@ describeFast("output-file write gates against CHR", () => { "cdb/write-not-permitted", ); } - const before = await retrieve({ ...base, path: "/file" }, {}); + const before = await retrieve( + { ...base, path: "/file" }, + HERMETIC_ENV, + ); expect(before.ok).toBe(true); expect(Array.isArray(before.data)).toBe(true); expect( @@ -106,13 +113,16 @@ describeFast("output-file write gates against CHR", () => { // This keeps a parser rejection from masquerading as a safe gate. const confirmed = await executeEnvelope( { ...base, command, yes: true }, - {}, + HERMETIC_ENV, ); expect(confirmed).toMatchObject({ ok: true, meta: { validation: { result: "passed" } }, }); - const after = await retrieve({ ...base, path: "/file" }, {}); + const after = await retrieve( + { ...base, path: "/file" }, + HERMETIC_ENV, + ); expect(after.ok).toBe(true); expect( (after.data as { name?: string }[]).some( @@ -167,7 +177,7 @@ describeFast("bare menu path reads against CHR (W6)", () => { // read that answers. const nav = await executeEnvelope( { ...base, command: "/system/identity", stdinIsTty: false }, - {}, + HERMETIC_ENV, ); expect(nav.ok).toBe(false); if (!nav.ok) { @@ -181,12 +191,12 @@ describeFast("bare menu path reads against CHR (W6)", () => { // stays behind the confirmation gate. const read = await executeEnvelope( { ...base, command: "/system/identity/print", stdinIsTty: false }, - {}, + HERMETIC_ENV, ); expect(read.ok).toBe(true); const gated = await executeEnvelope( { ...base, command: "/system/reboot", stdinIsTty: false }, - {}, + HERMETIC_ENV, ); expect(gated.ok ? undefined : gated.error.code).toBe( "usage/confirmation-required", diff --git a/test/integration/explain-live-facets.test.ts b/test/integration/explain-live-facets.test.ts index 29f74938..44d4b410 100644 --- a/test/integration/explain-live-facets.test.ts +++ b/test/integration/explain-live-facets.test.ts @@ -8,6 +8,7 @@ import { } from "../../src/explain-live.ts"; import type { ExplainLiveFacets } from "../../src/explain-live-facets.ts"; import { createProtocolAdapter } from "../../src/protocols/index.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, recordIntegrationEvidence, @@ -331,7 +332,7 @@ describeFast("explain live describe and completion (#492)", () => { const truncated = await explainLiveEnvelope( { ...connection, input: "/ip", schema: true }, {}, - {}, + HERMETIC_ENV, ); expect(truncated.ok).toBe(true); expect(truncated.tips.map((tip) => tip.code)).toContain( @@ -340,7 +341,7 @@ describeFast("explain live describe and completion (#492)", () => { const badCursor = await explainLiveEnvelope( { ...connection, input: "/ip", complete: true, cursor: 4 }, {}, - {}, + HERMETIC_ENV, ); expect(badCursor.ok).toBe(false); if (!badCursor.ok) @@ -398,7 +399,7 @@ describeFast("explain live describe and completion (#492)", () => { async function recordingBackend(connection: { quickchr: string; via: string }) { const resolved = await resolveApiRequest( { ...connection, endpoint: "/system/resource", validate: true }, - {}, + HERMETIC_ENV, ); const adapter = createProtocolAdapter({ protocol: resolved.via.value, diff --git a/test/integration/explain-live-types.test.ts b/test/integration/explain-live-types.test.ts index 9bc0fcc3..ca51156b 100644 --- a/test/integration/explain-live-types.test.ts +++ b/test/integration/explain-live-types.test.ts @@ -9,6 +9,7 @@ import { typeProbeScript, } from "../../src/explain-live-types.ts"; import { createProtocolAdapter } from "../../src/protocols/index.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, recordIntegrationEvidence, @@ -270,7 +271,7 @@ describeFast("explain live value types (#225)", () => { async function recordingBackend(connection: { quickchr: string; via: string }) { const resolved = await resolveApiRequest( { ...connection, endpoint: "/system/resource", validate: true }, - {}, + HERMETIC_ENV, ); const adapter = createProtocolAdapter({ protocol: resolved.via.value, diff --git a/test/integration/explain-live.test.ts b/test/integration/explain-live.test.ts index 20aba08f..0aa0b88e 100644 --- a/test/integration/explain-live.test.ts +++ b/test/integration/explain-live.test.ts @@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"; import { explainCommand } from "../../src/explain.ts"; import { explainLiveEnvelope } from "../../src/explain-live.ts"; import { retrieveEnvelope } from "../../src/retrieve.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, recordIntegrationEvidence, @@ -21,14 +22,14 @@ describeFast("explain live foundation (#491)", () => { if (packages === undefined) { const result = await retrieveEnvelope( { ...connection, path: "/system/package" }, - {}, + HERMETIC_ENV, ); expect(result.ok).toBe(true); if (result.ok) packages = result.data; } const before = await retrieveEnvelope( { ...connection, path: "/system/identity" }, - {}, + HERMETIC_ENV, ); const inputs = [ "/ip/address add address=bogus interface=", @@ -45,7 +46,7 @@ describeFast("explain live foundation (#491)", () => { const result = await explainLiveEnvelope( { ...connection, input }, {}, - {}, + HERMETIC_ENV, ); expect(result.ok).toBe(true); if (!result.ok) throw new Error(JSON.stringify(result)); @@ -142,7 +143,7 @@ describeFast("explain live foundation (#491)", () => { const oversized = await explainLiveEnvelope( { ...connection, input: `#${"x".repeat(32767)}` }, {}, - {}, + HERMETIC_ENV, ); expect(oversized.ok).toBe(true); if (oversized.ok) { @@ -155,7 +156,7 @@ describeFast("explain live foundation (#491)", () => { } const after = await retrieveEnvelope( { ...connection, path: "/system/identity" }, - {}, + HERMETIC_ENV, ); expect(after.ok).toBe(true); if (before.ok && after.ok) expect(after.data).toEqual(before.data); diff --git a/test/integration/mcp.test.ts b/test/integration/mcp.test.ts index 7f1aa784..0413f86e 100644 --- a/test/integration/mcp.test.ts +++ b/test/integration/mcp.test.ts @@ -9,6 +9,7 @@ import { encodeOpenWinBoxCdb } from "../../src/data/winbox-cdb.ts"; import { addDevice, loadCdb } from "../../src/devices.ts"; import { resolveMcpConfig } from "../../src/mcp/config.ts"; import { createCentrsMcpServer } from "../../src/mcp/server.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { exampleIds, isChrIntegrationEnabled, @@ -67,7 +68,7 @@ describeFast("centrs MCP server against CHR", () => { ); await writeFile(cdbFile, encodeOpenWinBoxCdb([])); - let cdb = await loadCdb({ cdbFile, env: {} }); + let cdb = await loadCdb({ cdbFile, env: HERMETIC_ENV }); await addDevice({ cdb, target: chr.restUrl, @@ -76,7 +77,7 @@ describeFast("centrs MCP server against CHR", () => { comment: "identity=mcp-lab mcp=rw via=rest-api", savedPassword: true, }); - cdb = await loadCdb({ cdbFile, env: {} }); + cdb = await loadCdb({ cdbFile, env: HERMETIC_ENV }); await addDevice({ cdb, target: "ro-only", @@ -86,7 +87,7 @@ describeFast("centrs MCP server against CHR", () => { savedPassword: true, }); - const config = resolveMcpConfig({ cdbFile, env: {} }); + const config = resolveMcpConfig({ cdbFile, env: HERMETIC_ENV }); const server = createCentrsMcpServer(config); const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); diff --git a/test/integration/preload.ts b/test/integration/preload.ts index bdf93428..8083aa43 100644 --- a/test/integration/preload.ts +++ b/test/integration/preload.ts @@ -14,13 +14,41 @@ * shared CHR (#612, `chr-reuse.ts`). A hook defined in a preload runs once, * after the last file of the `bun test` process, whether that process runs one * file (the QA loop) or the whole directory (`bun run test:integration`). + * + * And it gives the tier a hermetic HOME (#654), the integration twin of + * `test/unit/preload.ts`: an encrypted CDB or a `centrs.env` in the + * developer's real `~/.config/tikoci` must not reach a test. quickchr keeps its + * image cache and machine state under HOME, so its data dir is pinned to the + * real path first; otherwise every run would download a fresh image into the + * sandbox, and its sockets would outgrow the 108-byte Unix path limit. + * `Bun.env`, `runCli` and child processes (`cli-process.ts` passes + * `process.env` down) all see the sandbox. Bun fixes `os.homedir()` at + * startup, so a test that hands centrs its own env map must still include a + * HOME — use `HERMETIC_ENV` (`../unit/hermetic-env.ts`), never a bare `{}`. */ import { afterAll } from "bun:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { homedir, tmpdir } from "node:os"; +import { join } from "node:path"; import { prefetch } from "../../scripts/chr-prefetch.ts"; import { disposeProcessChrReuse, isChrIntegrationEnabled } from "./chr.ts"; if (isChrIntegrationEnabled()) { + // quickchr's own rule (`getDataDir` in its `state.ts`, not exported): + // QUICKCHR_DATA_DIR, else the local app-data dir on Windows, else HOME. + if (!process.env["QUICKCHR_DATA_DIR"] && process.platform !== "win32") { + process.env["QUICKCHR_DATA_DIR"] = join( + process.env["HOME"] || homedir(), + ".local", + "share", + "quickchr", + ); + } + const sandbox = mkdtempSync(join(tmpdir(), "centrs-int-home-")); + process.env["HOME"] = sandbox; + process.env["XDG_CONFIG_HOME"] = join(sandbox, ".config"); + try { console.error(await prefetch()); } catch (error) { @@ -28,10 +56,9 @@ if (isChrIntegrationEnabled()) { `CHR prefetch failed; the first CHR boot will retry it: ${String(error)}`, ); } -} -if (isChrIntegrationEnabled()) { afterAll(async () => { await disposeProcessChrReuse(); + rmSync(sandbox, { recursive: true, force: true }); }, 120_000); } diff --git a/test/integration/rest-permission.test.ts b/test/integration/rest-permission.test.ts index bfdcddd1..c5ff7fb8 100644 --- a/test/integration/rest-permission.test.ts +++ b/test/integration/rest-permission.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "bun:test"; import { executeEnvelope } from "../../src/execute.ts"; import { retrieveEnvelope } from "../../src/retrieve.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, REST_API_WITHOUT_API_POLICY_SINCE, @@ -242,7 +243,7 @@ describeFast("REST permission refusals (routeros/permission-denied)", () => { const p3 = await retrieveEnvelope( { ...rest(users.noApi[0]), path: "/system/resource" }, - {}, + HERMETIC_ENV, ); if ( routerOsAtLeast(chr.state.version, REST_API_WITHOUT_API_POLICY_SINCE) @@ -255,7 +256,7 @@ describeFast("REST permission refusals (routeros/permission-denied)", () => { const p4 = await retrieveEnvelope( { ...rest(users.ro[0], "centrs-wrong"), path: "/system/resource" }, - {}, + HERMETIC_ENV, ); expect(p4.ok).toBe(false); if (!p4.ok) expect(p4.error.code).toBe("transport/auth-failed"); diff --git a/test/integration/routeros-error-classes.test.ts b/test/integration/routeros-error-classes.test.ts index 1794b1d5..28e863d6 100644 --- a/test/integration/routeros-error-classes.test.ts +++ b/test/integration/routeros-error-classes.test.ts @@ -12,6 +12,7 @@ import { describe, expect, test } from "bun:test"; import { apiEnvelope } from "../../src/api.ts"; import { executeEnvelope } from "../../src/execute.ts"; +import { HERMETIC_ENV } from "../unit/hermetic-env.ts"; import { isChrIntegrationEnabled, readEnv, @@ -85,7 +86,7 @@ describeFast("no such item vs unknown path against CHR (R4, #659)", () => { yes: true, stdinIsTty: false, }, - {}, + HERMETIC_ENV, ); expect(added.ok).toBe(true); for (const via of ["rest-api", "native-api"] as const) { @@ -98,7 +99,7 @@ describeFast("no such item vs unknown path against CHR (R4, #659)", () => { for (const { command, validate, code } of cases) { const envelope = await executeEnvelope( { ...base, command, validate, yes: true, stdinIsTty: false }, - {}, + HERMETIC_ENV, ); expect( envelope.ok ? "ok" : envelope.error.code, @@ -126,7 +127,7 @@ describeFast("no such item vs unknown path against CHR (R4, #659)", () => { yes: true, stdinIsTty: false, }, - {}, + HERMETIC_ENV, ); expect( parsed.ok ? "ok" : parsed.error.code, From dc80a56e172431fc4b94566ae1f13d30a3f158cd Mon Sep 17 00:00:00 2001 From: Amm0Bot Date: Thu, 26 Nov 2026 16:39:14 -0800 Subject: [PATCH 2/6] test: clear exported CDB keys and match quickchr's HOME fallback (#654) Review on #679: a shell-exported CENTRS_CDB_FILE still reached the developer's CDB past the sandbox HOME, so the preload now clears the three CDB keys (tests pass --cdb-file / cdbFile themselves). The data-dir pin now uses quickchr's exact fallback, HOME || USERPROFILE. Session: 4f3a1170-9bf0-4eac-97bd-7c9c9d0e6bbd Co-Authored-By: Claude Opus 5.5 --- test/integration/preload.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/test/integration/preload.ts b/test/integration/preload.ts index 8083aa43..d35b73b7 100644 --- a/test/integration/preload.ts +++ b/test/integration/preload.ts @@ -17,7 +17,8 @@ * * And it gives the tier a hermetic HOME (#654), the integration twin of * `test/unit/preload.ts`: an encrypted CDB or a `centrs.env` in the - * developer's real `~/.config/tikoci` must not reach a test. quickchr keeps its + * developer's real `~/.config/tikoci`, or one exported as `CENTRS_CDB_FILE`, + * must not reach a test. quickchr keeps its * image cache and machine state under HOME, so its data dir is pinned to the * real path first; otherwise every run would download a fresh image into the * sandbox, and its sockets would outgrow the 108-byte Unix path limit. @@ -29,17 +30,18 @@ import { afterAll } from "bun:test"; import { mkdtempSync, rmSync } from "node:fs"; -import { homedir, tmpdir } from "node:os"; +import { tmpdir } from "node:os"; import { join } from "node:path"; import { prefetch } from "../../scripts/chr-prefetch.ts"; import { disposeProcessChrReuse, isChrIntegrationEnabled } from "./chr.ts"; if (isChrIntegrationEnabled()) { // quickchr's own rule (`getDataDir` in its `state.ts`, not exported): - // QUICKCHR_DATA_DIR, else the local app-data dir on Windows, else HOME. + // QUICKCHR_DATA_DIR, else the local app-data dir on Windows, else + // HOME || USERPROFILE. if (!process.env["QUICKCHR_DATA_DIR"] && process.platform !== "win32") { process.env["QUICKCHR_DATA_DIR"] = join( - process.env["HOME"] || homedir(), + process.env["HOME"] || process.env["USERPROFILE"] || "", ".local", "share", "quickchr", @@ -48,6 +50,14 @@ if (isChrIntegrationEnabled()) { const sandbox = mkdtempSync(join(tmpdir(), "centrs-int-home-")); process.env["HOME"] = sandbox; process.env["XDG_CONFIG_HOME"] = join(sandbox, ".config"); + // A shell-exported CDB choice would reach the developer's CDB past the + // sandbox. Tests that need a CDB pass `--cdb-file` / `cdbFile` themselves. + for (const key of [ + "CENTRS_CDB_FILE", + "CENTRS_CDB_PASSWORD", + "CENTRS_CDB_PASSWORD_FILE", + ]) + delete process.env[key]; try { console.error(await prefetch()); From b7bde20856ea3a0de3647b01ae5e61fd9e720eb1 Mon Sep 17 00:00:00 2001 From: Amm0Bot Date: Thu, 26 Nov 2026 16:40:42 -0800 Subject: [PATCH 3/6] test: pin quickchr's data dir as an absolute path (#654) Review on #679: a relative HOME made a relative QUICKCHR_DATA_DIR, which names a different registry per process cwd. Resolve it once in the preload. Session: 4f3a1170-9bf0-4eac-97bd-7c9c9d0e6bbd Co-Authored-By: Claude Opus 5.5 --- test/integration/preload.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/integration/preload.ts b/test/integration/preload.ts index d35b73b7..9a68a063 100644 --- a/test/integration/preload.ts +++ b/test/integration/preload.ts @@ -31,16 +31,17 @@ import { afterAll } from "bun:test"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { join, resolve } from "node:path"; import { prefetch } from "../../scripts/chr-prefetch.ts"; import { disposeProcessChrReuse, isChrIntegrationEnabled } from "./chr.ts"; if (isChrIntegrationEnabled()) { // quickchr's own rule (`getDataDir` in its `state.ts`, not exported): // QUICKCHR_DATA_DIR, else the local app-data dir on Windows, else - // HOME || USERPROFILE. + // HOME || USERPROFILE. Resolved to an absolute path, so a relative HOME + // still names one registry for every process, whatever its cwd. if (!process.env["QUICKCHR_DATA_DIR"] && process.platform !== "win32") { - process.env["QUICKCHR_DATA_DIR"] = join( + process.env["QUICKCHR_DATA_DIR"] = resolve( process.env["HOME"] || process.env["USERPROFILE"] || "", ".local", "share", From cf4908d2bbd29096eb375bf044b9dcdc718e4d75 Mon Sep 17 00:00:00 2001 From: TIKOCI RouterOS Bots Date: Sat, 28 Nov 2026 08:23:33 -0800 Subject: [PATCH 4/6] test: keep the real HOME; isolate the tier with centrs' own overrides (#654) Moving HOME also moved quickchr's data dir, which the preload then had to pin back with a copy of quickchr's private rule. The house testing rules keep the real HOME and, before a tool adopts TIKOCI_SANDBOX, use its own overrides set in a preload. So the preload now sets CENTRS_CDB_FILE to an empty CDB made per run (an explicit CDB file is never auto-created, unlike the default one) and CENTRS_SKIP_ENV_FILE=1, and leaves HOME, XDG_CONFIG_HOME and QUICKCHR_DATA_DIR alone. The test/AGENTS.md rule and MATRIX now call it the stopgap that TIKOCI_SANDBOX replaces. Session: a4719a5d-e103-4d87-affe-8b6ecab1498c Co-Authored-By: Claude Opus 5.5 --- docs/MATRIX.md | 17 ++++++----- test/AGENTS.md | 10 +++--- test/integration/preload.ts | 61 +++++++++++++++---------------------- 3 files changed, 40 insertions(+), 48 deletions(-) diff --git a/docs/MATRIX.md b/docs/MATRIX.md index 6b83a2ad..4b5a7df7 100644 --- a/docs/MATRIX.md +++ b/docs/MATRIX.md @@ -174,8 +174,9 @@ maintainer adds it here. 1. **CI cleanup** (in progress): docs-only CI, unit on every OS, the nightly CHR channel sweep and the main-health gate landed in #631, and snapshot CHR - reuse within a file in #612 PR 1. Left: #612 PR 2 (cross-file CHR reuse), - designed for the nightly, and a hermetic integration tier (#654). + reuse within a file in #612 PR 1, and a stopgap hermetic integration tier + in #679 (#654). Left: #612 PR 2 (cross-file CHR reuse), designed for the + nightly. 2. **Transport selection**, the first work on `0.2.0-next`. native-api becomes the default for `retrieve`, `api` and `execute`. Automatic selection falls back to the next available transport and reports each hop; a pinned `--via` @@ -221,11 +222,13 @@ that keeps growing is being used as an issue. - **Lean, `centrs.db`:** SQLite, derived, deletable and secret-free; no code path depends on it. First tables: `seen` facts from MNDP, ARP and ping. A session log records verb, target and error code only until redaction exists. -- **In progress elsewhere:** the TIKOCI-wide `tikoci-home` skill - ([tikoci.github.io#24](https://github.com/tikoci/tikoci.github.io/pull/24), - in review) is where this theme's layout is being aligned across tools: user - and project scopes under `.tikoci/`, and trust for `local/`. The leans - above follow it once it lands; centrs' review points live on that PR. +- **House standard:** the TIKOCI-wide + [`tikoci-home`](https://tikoci.dev/house/skills/tikoci-home/SKILL.md) + skill sets the layout this theme aligns to: user and project scopes under + `.tikoci/`, and trust for `local/`. centrs adopts its `user` and `sandbox` + profiles first; `TIKOCI_SANDBOX` then replaces the test preloads' CDB and + `centrs.env` overrides (#550, #654). `project`, `project-data` and `names` + follow together as this theme. - **Ready when:** now. It adds a settings layer, so the constitution changes first. - **Absorbed:** #92, #160. diff --git a/test/AGENTS.md b/test/AGENTS.md index 9a342168..73236da2 100644 --- a/test/AGENTS.md +++ b/test/AGENTS.md @@ -5,11 +5,11 @@ - Collect coverage for trend visibility, not as a percentage gate. - Put long-running, RouterOS-backed, or platform-specific tests under `test/integration/` and wire them through QA or lab workflows. "Platform-specific" includes process-level tests that spawn the real `src/cli.ts` through `cli-process.ts` — including the **network-free** CLI smoke tier (`cli-smoke.test.ts`), which is not CHR-gated and so runs in the fast push/PR gate (`bun test`). - Keep fixtures under `test/fixtures/` with clear source/provenance notes. -- Both tiers run with a sandbox HOME (`test/unit/preload.ts`, - `test/integration/preload.ts`), but Bun fixes `os.homedir()` at startup. A - call that hands centrs its own env map passes `HERMETIC_ENV` - (`test/unit/hermetic-env.ts`) or a map with its own `HOME`, never a bare `{}` - — that falls back to the developer's real CDB and `centrs.env` (#654). +- Until centrs adopts `TIKOCI_SANDBOX` (tikoci-home), the preloads keep the + developer's CDB and `centrs.env` out of both tiers (#550, #654). A call that + hands centrs its own env map gets none of that: pass `HERMETIC_ENV` + (`test/unit/hermetic-env.ts`) or a map with its own `HOME`, never a bare + `{}`, which falls back to the real home through `os.homedir()`. ## Device-dependent expectations: declare the split, and prove it on long-term diff --git a/test/integration/preload.ts b/test/integration/preload.ts index 9a68a063..0e913f7d 100644 --- a/test/integration/preload.ts +++ b/test/integration/preload.ts @@ -15,50 +15,39 @@ * after the last file of the `bun test` process, whether that process runs one * file (the QA loop) or the whole directory (`bun run test:integration`). * - * And it gives the tier a hermetic HOME (#654), the integration twin of - * `test/unit/preload.ts`: an encrypted CDB or a `centrs.env` in the - * developer's real `~/.config/tikoci`, or one exported as `CENTRS_CDB_FILE`, - * must not reach a test. quickchr keeps its - * image cache and machine state under HOME, so its data dir is pinned to the - * real path first; otherwise every run would download a fresh image into the - * sandbox, and its sockets would outgrow the 108-byte Unix path limit. + * And it keeps the developer's own CDB and `centrs.env` out of the tier + * (#654): an encrypted CDB at the default path failed tests with + * `cdb/password-required`, and a plaintext one or a `centrs.env` could change + * resolution silently. This is the stopgap until centrs adopts + * `TIKOCI_SANDBOX` (tikoci-home): it sets centrs' own overrides and keeps the + * real HOME, so quickchr finds its image cache and machines as usual. + * `CENTRS_CDB_FILE` names an empty CDB made per run, because an explicit CDB + * file is never created implicitly the way the default one is. A test that + * sets HOME to get a fresh default CDB uses that one instead in this tier. * `Bun.env`, `runCli` and child processes (`cli-process.ts` passes - * `process.env` down) all see the sandbox. Bun fixes `os.homedir()` at - * startup, so a test that hands centrs its own env map must still include a - * HOME — use `HERMETIC_ENV` (`../unit/hermetic-env.ts`), never a bare `{}`. + * `process.env` down) all see the overrides. A test that hands centrs its own + * env map gets none of this; it passes `HERMETIC_ENV` + * (`../unit/hermetic-env.ts`), never a bare `{}`. */ import { afterAll } from "bun:test"; -import { mkdtempSync, rmSync } from "node:fs"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join, resolve } from "node:path"; +import { join } from "node:path"; import { prefetch } from "../../scripts/chr-prefetch.ts"; +import { encodeOpenWinBoxCdb } from "../../src/data/winbox-cdb.ts"; import { disposeProcessChrReuse, isChrIntegrationEnabled } from "./chr.ts"; if (isChrIntegrationEnabled()) { - // quickchr's own rule (`getDataDir` in its `state.ts`, not exported): - // QUICKCHR_DATA_DIR, else the local app-data dir on Windows, else - // HOME || USERPROFILE. Resolved to an absolute path, so a relative HOME - // still names one registry for every process, whatever its cwd. - if (!process.env["QUICKCHR_DATA_DIR"] && process.platform !== "win32") { - process.env["QUICKCHR_DATA_DIR"] = resolve( - process.env["HOME"] || process.env["USERPROFILE"] || "", - ".local", - "share", - "quickchr", - ); - } - const sandbox = mkdtempSync(join(tmpdir(), "centrs-int-home-")); - process.env["HOME"] = sandbox; - process.env["XDG_CONFIG_HOME"] = join(sandbox, ".config"); - // A shell-exported CDB choice would reach the developer's CDB past the - // sandbox. Tests that need a CDB pass `--cdb-file` / `cdbFile` themselves. - for (const key of [ - "CENTRS_CDB_FILE", - "CENTRS_CDB_PASSWORD", - "CENTRS_CDB_PASSWORD_FILE", - ]) - delete process.env[key]; + // An empty, unencrypted CDB per run, removed after the last file. + const scratch = mkdtempSync(join(tmpdir(), "centrs-int-")); + const cdbFile = join(scratch, "winbox.cdb"); + writeFileSync(cdbFile, encodeOpenWinBoxCdb([]), { mode: 0o600 }); + process.env["CENTRS_CDB_FILE"] = cdbFile; + process.env["CENTRS_SKIP_ENV_FILE"] = "1"; + // A shell-exported CDB key would encrypt or unlock the run's CDB. + delete process.env["CENTRS_CDB_PASSWORD"]; + delete process.env["CENTRS_CDB_PASSWORD_FILE"]; try { console.error(await prefetch()); @@ -70,6 +59,6 @@ if (isChrIntegrationEnabled()) { afterAll(async () => { await disposeProcessChrReuse(); - rmSync(sandbox, { recursive: true, force: true }); + rmSync(scratch, { recursive: true, force: true }); }, 120_000); } From 9cef9e0e0f855c1b0c25ad9ce2d798631e7ef6da Mon Sep 17 00:00:00 2001 From: TIKOCI RouterOS Bots Date: Sat, 28 Nov 2026 21:46:57 -0800 Subject: [PATCH 5/6] test: drop inherited CENTRS_* in the CHR tier; own CDB for cli-smoke (#654) Review follow-ups on #679: - The integration preload deletes every inherited CENTRS_* variable (all spellings) except the harness's run controls, so a CENTRS_FORMAT or CENTRS_VIA in a developer's shell no longer changes results. - cli-smoke's devices round-trip and no-devices cases pass their own empty CENTRS_CDB_FILE instead of faking HOME, which the per-run CDB overrides in the CHR tier. - test/AGENTS.md: the bare-{} rule lasts until the tikoci-home `user` profile, not TIKOCI_SANDBOX. HERMETIC_ENV gets a TODO for that. - MATRIX nits: one isolation helper for both preloads; a structural test against bare env maps. Session: 7bb222a8-e543-43d6-bee4-a9544062023f Co-Authored-By: Claude Opus 5.5 --- docs/MATRIX.md | 6 +++ test/AGENTS.md | 12 ++++-- test/integration/cli-smoke.test.ts | 67 ++++++++++++++++++------------ test/integration/preload.ts | 52 +++++++++++++++++------ test/unit/hermetic-env.ts | 5 +++ 5 files changed, 99 insertions(+), 43 deletions(-) diff --git a/docs/MATRIX.md b/docs/MATRIX.md index 4b5a7df7..23c9c263 100644 --- a/docs/MATRIX.md +++ b/docs/MATRIX.md @@ -501,6 +501,12 @@ done or declined. A nit that needs more explanation is an issue. contract (#659 phase 4); every page since follows it. - `docs/errors/routeros/unknown-path.md` does not say that 0.1.x also used the code for a missing item, before #659 split out `routeros/no-such-item`. +- Test isolation: the unit preload fakes HOME and the integration preload + sets centrs' overrides; when `TIKOCI_SANDBOX` lands, both should call one + helper. Only the integration preload drops inherited `CENTRS_*` variables + today (#654). +- Test isolation: a unit test that fails on a bare `env: {}` under `test/` + would enforce the `test/AGENTS.md` rule structurally (#654). - `test/integration/api-stream-command.test.ts` fails intermittently in the unit tier: its fake server sends "Invalid RouterOS API length descriptor 0xfb" (1 of 7 local runs, 2026-11-05). diff --git a/test/AGENTS.md b/test/AGENTS.md index 73236da2..859cc074 100644 --- a/test/AGENTS.md +++ b/test/AGENTS.md @@ -6,10 +6,14 @@ - Put long-running, RouterOS-backed, or platform-specific tests under `test/integration/` and wire them through QA or lab workflows. "Platform-specific" includes process-level tests that spawn the real `src/cli.ts` through `cli-process.ts` — including the **network-free** CLI smoke tier (`cli-smoke.test.ts`), which is not CHR-gated and so runs in the fast push/PR gate (`bun test`). - Keep fixtures under `test/fixtures/` with clear source/provenance notes. - Until centrs adopts `TIKOCI_SANDBOX` (tikoci-home), the preloads keep the - developer's CDB and `centrs.env` out of both tiers (#550, #654). A call that - hands centrs its own env map gets none of that: pass `HERMETIC_ENV` - (`test/unit/hermetic-env.ts`) or a map with its own `HOME`, never a bare - `{}`, which falls back to the real home through `os.homedir()`. + developer's CDB and `centrs.env` out of both tiers (#550, #654); the + integration preload also drops inherited `CENTRS_*` variables. A test that + needs its own empty CDB passes its own `CENTRS_CDB_FILE`, not a fake `HOME`. +- A call that hands centrs its own env map gets none of the preloads' work: + pass `HERMETIC_ENV` (`test/unit/hermetic-env.ts`) or a map with its own + `HOME`, never a bare `{}`, which falls back to the real home through + `os.homedir()`. This holds until centrs' resolver adopts the tikoci-home + `user` profile, where a map without `HOME` has no user scope. ## Device-dependent expectations: declare the split, and prove it on long-term diff --git a/test/integration/cli-smoke.test.ts b/test/integration/cli-smoke.test.ts index c875ac97..c39f22d5 100644 --- a/test/integration/cli-smoke.test.ts +++ b/test/integration/cli-smoke.test.ts @@ -12,11 +12,28 @@ */ import { describe, expect, test } from "bun:test"; -import { mkdtemp, rm } from "node:fs/promises"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { encodeOpenWinBoxCdb } from "../../src/data/winbox-cdb.ts"; import { runCliProcess } from "./cli-process.ts"; +/** + * A throwaway empty CDB, named with `CENTRS_CDB_FILE` so it wins over both the + * default path and the CHR tier's per-run CDB (#654). Setting HOME would not + * do it there, and tikoci-home says tests should not need to change HOME. + * centrs never creates an explicit CDB file, so this writes an empty one. + */ +async function emptyCdbEnv(): Promise<{ + dir: string; + env: Record; +}> { + const dir = await mkdtemp(join(tmpdir(), "centrs-smoke-cdb-")); + const cdbFile = join(dir, "winbox.cdb"); + await writeFile(cdbFile, encodeOpenWinBoxCdb([]), { mode: 0o600 }); + return { dir, env: { CENTRS_CDB_FILE: cdbFile } }; +} + interface Envelope { ok: boolean; error?: { code?: string; detailsUrl?: string; remediation?: string }; @@ -99,12 +116,10 @@ describe("CLI smoke (real subprocess, no network)", () => { ); }); - test("devices CDB round-trip: add → list → show → remove (temp HOME)", async () => { - // A fresh CDB at the *default* path auto-creates on first use, so pointing - // HOME at a temp dir gives a throwaway default CDB — the round-trip touches - // no real CDB and no router. - const home = await mkdtemp(join(tmpdir(), "centrs-smoke-home-")); - const env = { HOME: home }; + test("devices CDB round-trip: add → list → show → remove (temp CDB)", async () => { + // A throwaway CDB of its own: the round-trip touches no real or shared + // CDB and no router. + const { dir, env } = await emptyCdbEnv(); const target = "192.0.2.99"; try { const add = await runCliProcess({ @@ -161,15 +176,15 @@ describe("CLI smoke (real subprocess, no network)", () => { expect(remove.exitCode).toBe(0); expect(parseEnvelope(remove.stdoutText).ok).toBe(true); } finally { - await rm(home, { recursive: true, force: true }); + await rm(dir, { recursive: true, force: true }); } }); test("terminal MAC over --via ssh with no CDB record errors + tips L2 (JG-01)", async () => { - // A temp HOME means the default CDB has no record for the MAC, so the - // IP-transport (ssh) MAC→IP resolution fails hermetically — no router, no - // ARP opt-in. The tip must lead with the L2 alternative. - const home = await mkdtemp(join(tmpdir(), "centrs-smoke-home-")); + // An empty CDB has no record for the MAC, so the IP-transport (ssh) + // MAC→IP resolution fails hermetically — no router, no ARP opt-in. The + // tip must lead with the L2 alternative. + const { dir, env } = await emptyCdbEnv(); try { const res = await runCliProcess({ args: [ @@ -181,7 +196,7 @@ describe("CLI smoke (real subprocess, no network)", () => { "admin", "--json", ], - env: { HOME: home }, + env, }); expect(res.exitCode).toBe(1); const envelope = parseEnvelope(res.stderrText); @@ -189,23 +204,23 @@ describe("CLI smoke (real subprocess, no network)", () => { expect(envelope.error?.code).toBe("target/mac-unresolved"); expect(res.stderrText).toContain("--via mac-telnet"); } finally { - await rm(home, { recursive: true, force: true }); + await rm(dir, { recursive: true, force: true }); } }); test("--user and -u are accepted aliases for --username (JG-23)", async () => { - // A temp HOME keeps the default CDB empty, so the missing-target tip is - // deterministic and no real inventory leaks into the assertion. With neither + // An empty CDB of its own keeps the missing-target tip + // deterministic, with no real inventory in the assertion. With neither // a target nor a command, `execute` leads with the missing-target error — but // the alias still has to be *parsed* (consuming "admin") to get there; // otherwise "admin" is read as the target and we'd hit the missing-command // error instead. So the missing-target code path itself proves the alias. - const home = await mkdtemp(join(tmpdir(), "centrs-smoke-home-")); + const { dir, env } = await emptyCdbEnv(); try { for (const alias of ["--user", "-u"]) { const res = await runCliProcess({ args: ["execute", alias, "admin", "--json"], - env: { HOME: home }, + env, }); expect(res.exitCode).toBe(1); const envelope = parseEnvelope(res.stderrText); @@ -216,7 +231,7 @@ describe("CLI smoke (real subprocess, no network)", () => { expect(res.stderrText).toContain("tip/no-devices"); } } finally { - await rm(home, { recursive: true, force: true }); + await rm(dir, { recursive: true, force: true }); } }); @@ -349,25 +364,25 @@ describe("CLI smoke (real subprocess, no network)", () => { }); test("api with no tips toward discover/devices", async () => { - const home = await mkdtemp(join(tmpdir(), "centrs-smoke-home-")); + const { dir, env } = await emptyCdbEnv(); try { - const res = await runCliProcess({ args: ["api"], env: { HOME: home } }); + const res = await runCliProcess({ args: ["api"], env }); expect(res.exitCode).toBe(1); expect(res.stderrText).toContain("requires a "); expect(res.stderrText).toContain("tip/no-devices"); } finally { - await rm(home, { recursive: true, force: true }); + await rm(dir, { recursive: true, force: true }); } }); test("a missing tips toward discover/devices (terminal, JG-?)", async () => { - // Empty HOME ⇒ empty registry ⇒ the tip points at `discover --save`. This is + // Empty CDB ⇒ empty registry ⇒ the tip points at `discover --save`. This is // the text path, so it exercises the `Tips:` footer the error renderer adds. - const home = await mkdtemp(join(tmpdir(), "centrs-smoke-home-")); + const { dir, env } = await emptyCdbEnv(); try { const res = await runCliProcess({ args: ["terminal"], - env: { HOME: home }, + env, }); expect(res.exitCode).toBe(1); expect(res.stderrText).toContain("requires a "); @@ -375,7 +390,7 @@ describe("CLI smoke (real subprocess, no network)", () => { expect(res.stderrText).toContain("tip/no-devices"); expect(res.stderrText).toContain("centrs discover --save"); } finally { - await rm(home, { recursive: true, force: true }); + await rm(dir, { recursive: true, force: true }); } }); diff --git a/test/integration/preload.ts b/test/integration/preload.ts index 0e913f7d..aa3d0186 100644 --- a/test/integration/preload.ts +++ b/test/integration/preload.ts @@ -15,17 +15,25 @@ * after the last file of the `bun test` process, whether that process runs one * file (the QA loop) or the whole directory (`bun run test:integration`). * - * And it keeps the developer's own CDB and `centrs.env` out of the tier - * (#654): an encrypted CDB at the default path failed tests with - * `cdb/password-required`, and a plaintext one or a `centrs.env` could change - * resolution silently. This is the stopgap until centrs adopts - * `TIKOCI_SANDBOX` (tikoci-home): it sets centrs' own overrides and keeps the - * real HOME, so quickchr finds its image cache and machines as usual. - * `CENTRS_CDB_FILE` names an empty CDB made per run, because an explicit CDB - * file is never created implicitly the way the default one is. A test that - * sets HOME to get a fresh default CDB uses that one instead in this tier. + * And it keeps the developer's own settings out of the tier (#654): an + * encrypted CDB at the default path failed tests with `cdb/password-required`, + * and a plaintext one, a `centrs.env` or a `CENTRS_*` variable exported in the + * shell could change resolution silently. This is the stopgap until centrs + * adopts `TIKOCI_SANDBOX` (tikoci-home): it keeps the real HOME, so quickchr + * finds its image cache and machines as usual, and instead + * 1. drops every inherited `CENTRS_*` variable except the run's own + * controls (`isRunControl`), in every spelling, because centrs cannot + * tell an inherited variable from one a test set; + * 2. sets `CENTRS_SKIP_ENV_FILE=1`; + * 3. points `CENTRS_CDB_FILE` at an empty CDB made per run, because an + * explicit CDB file is never created implicitly the way the default one + * is. + * Step 1 stays when the sandbox lands; steps 2 and 3 become + * `TIKOCI_SANDBOX=`. Because `CENTRS_CDB_FILE` wins over HOME, a + * test that needs its own empty CDB passes its own `CENTRS_CDB_FILE` (as + * `cli-smoke.test.ts` does); setting HOME does not give it one here. * `Bun.env`, `runCli` and child processes (`cli-process.ts` passes - * `process.env` down) all see the overrides. A test that hands centrs its own + * `process.env` down) all see the result. A test that hands centrs its own * env map gets none of this; it passes `HERMETIC_ENV` * (`../unit/hermetic-env.ts`), never a bare `{}`. */ @@ -38,16 +46,34 @@ import { prefetch } from "../../scripts/chr-prefetch.ts"; import { encodeOpenWinBoxCdb } from "../../src/data/winbox-cdb.ts"; import { disposeProcessChrReuse, isChrIntegrationEnabled } from "./chr.ts"; +/** + * A `CENTRS_*` variable the test harness reads to configure the run, not one + * centrs resolves as a setting. Matched on the upper-cased name. + */ +function isRunControl(upperKey: string): boolean { + return ( + upperKey === "CENTRS_RUN_FAST_INTEGRATION" || + upperKey === "CENTRS_INTEGRATION_REPORT" || + upperKey === "CENTRS_CMR_VERSION" || + upperKey.startsWith("CENTRS_CHR_") || + upperKey.endsWith("_CAPTURE") + ); +} + if (isChrIntegrationEnabled()) { + // Windows env names are case-insensitive, so match every spelling. + for (const key of Object.keys(process.env)) { + const upper = key.toUpperCase(); + if (upper.startsWith("CENTRS_") && !isRunControl(upper)) { + delete process.env[key]; + } + } // An empty, unencrypted CDB per run, removed after the last file. const scratch = mkdtempSync(join(tmpdir(), "centrs-int-")); const cdbFile = join(scratch, "winbox.cdb"); writeFileSync(cdbFile, encodeOpenWinBoxCdb([]), { mode: 0o600 }); process.env["CENTRS_CDB_FILE"] = cdbFile; process.env["CENTRS_SKIP_ENV_FILE"] = "1"; - // A shell-exported CDB key would encrypt or unlock the run's CDB. - delete process.env["CENTRS_CDB_PASSWORD"]; - delete process.env["CENTRS_CDB_PASSWORD_FILE"]; try { console.error(await prefetch()); diff --git a/test/unit/hermetic-env.ts b/test/unit/hermetic-env.ts index e11d89e2..d4d8bc81 100644 --- a/test/unit/hermetic-env.ts +++ b/test/unit/hermetic-env.ts @@ -10,6 +10,11 @@ * `CENTRS_SKIP_ENV_FILE` so the config tier stays skipped even if a future * path scheme stops deriving from HOME. Extend per test with a spread * (`{ ...HERMETIC_ENV, CENTRS_PASSWORD: "x" }`), never by mutation. + * + * TODO(tikoci-home `user` profile): once centrs' resolver adopts it, drop the + * fake HOME here. A map with neither `HOME` nor `TIKOCI_HOME` then has no + * user scope, while a fake HOME moves the host scope (and, once centrs passes + * its env on to `QuickCHR.get()`, quickchr's data dir). Both tiers use this. */ export const HERMETIC_ENV: Record = { CENTRS_SKIP_ENV_FILE: "1", From 897d436322005ccbabb6e6c7ea8e6eb9a0062e43 Mon Sep 17 00:00:00 2001 From: TIKOCI RouterOS Bots Date: Sun, 29 Nov 2026 05:28:06 -0800 Subject: [PATCH 6/6] test: launch runCliProcess with --no-env-file; tighten isolation wording (#654) Codex review follow-ups on #679: - runCliProcess passes `--no-env-file`, so a checkout `.env` cannot put back the CENTRS_* settings the CHR preload strips. With CENTRS_FORMAT=json in both the shell and `.env`, cli-smoke failed 14 cases without it and passes 50/50 with it. The #666 dotenv tests launch directly and keep autoload. - test/AGENTS.md: the unit preload isolates default paths only; an exported CENTRS_CDB_FILE still reaches the fast tier. Under the `user` profile, a caller-supplied empty map has no user scope. - MATRIX nit: the unit preload leaks its HOME sandbox under /tmp. Session: 7bb222a8-e543-43d6-bee4-a9544062023f Co-Authored-By: Claude Opus 5.5 --- docs/MATRIX.md | 3 +++ test/AGENTS.md | 11 +++++++---- test/integration/cli-process.ts | 4 +++- test/unit/hermetic-env.ts | 4 ++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/MATRIX.md b/docs/MATRIX.md index 23c9c263..383d4364 100644 --- a/docs/MATRIX.md +++ b/docs/MATRIX.md @@ -505,6 +505,9 @@ done or declined. A nit that needs more explanation is an issue. sets centrs' overrides; when `TIKOCI_SANDBOX` lands, both should call one helper. Only the integration preload drops inherited `CENTRS_*` variables today (#654). +- Test isolation: `test/unit/preload.ts` never removes its `centrs-unit-home-*` + sandbox (about 3.7 MB a run); 512 of them filled a `/tmp` quota and failed + the fast gate with `cdb/create-failed` (2026-11-29). - Test isolation: a unit test that fails on a bare `env: {}` under `test/` would enforce the `test/AGENTS.md` rule structurally (#654). - `test/integration/api-stream-command.test.ts` fails intermittently in the diff --git a/test/AGENTS.md b/test/AGENTS.md index 859cc074..385e5bd9 100644 --- a/test/AGENTS.md +++ b/test/AGENTS.md @@ -6,14 +6,17 @@ - Put long-running, RouterOS-backed, or platform-specific tests under `test/integration/` and wire them through QA or lab workflows. "Platform-specific" includes process-level tests that spawn the real `src/cli.ts` through `cli-process.ts` — including the **network-free** CLI smoke tier (`cli-smoke.test.ts`), which is not CHR-gated and so runs in the fast push/PR gate (`bun test`). - Keep fixtures under `test/fixtures/` with clear source/provenance notes. - Until centrs adopts `TIKOCI_SANDBOX` (tikoci-home), the preloads keep the - developer's CDB and `centrs.env` out of both tiers (#550, #654); the - integration preload also drops inherited `CENTRS_*` variables. A test that - needs its own empty CDB passes its own `CENTRS_CDB_FILE`, not a fake `HOME`. + developer's CDB and `centrs.env` at their default paths out of both tiers + (#550, #654). Only the CHR preload also drops inherited `CENTRS_*` + variables, so an exported `CENTRS_CDB_FILE` still reaches the fast tier. + `runCliProcess` launches with `--no-env-file`, so a checkout `.env` does not + put them back. A test that needs its own empty CDB passes its own + `CENTRS_CDB_FILE`, not a fake `HOME`. - A call that hands centrs its own env map gets none of the preloads' work: pass `HERMETIC_ENV` (`test/unit/hermetic-env.ts`) or a map with its own `HOME`, never a bare `{}`, which falls back to the real home through `os.homedir()`. This holds until centrs' resolver adopts the tikoci-home - `user` profile, where a map without `HOME` has no user scope. + `user` profile, under which a caller-supplied empty map has no user scope. ## Device-dependent expectations: declare the split, and prove it on long-term diff --git a/test/integration/cli-process.ts b/test/integration/cli-process.ts index 70c67cbd..53da5ef9 100644 --- a/test/integration/cli-process.ts +++ b/test/integration/cli-process.ts @@ -72,7 +72,9 @@ export async function runCliProcess( ? Buffer.from(options.stdin) : options.stdin; - const proc = Bun.spawn(["bun", CLI_ENTRY, ...options.args], { + // `--no-env-file`: a checkout `.env` would otherwise put back settings the + // CHR preload strips (#654). The dotenv tests (#666) launch directly. + const proc = Bun.spawn(["bun", "--no-env-file", CLI_ENTRY, ...options.args], { stdin, stdout: "pipe", stderr: "pipe", diff --git a/test/unit/hermetic-env.ts b/test/unit/hermetic-env.ts index d4d8bc81..d935baa5 100644 --- a/test/unit/hermetic-env.ts +++ b/test/unit/hermetic-env.ts @@ -12,8 +12,8 @@ * (`{ ...HERMETIC_ENV, CENTRS_PASSWORD: "x" }`), never by mutation. * * TODO(tikoci-home `user` profile): once centrs' resolver adopts it, drop the - * fake HOME here. A map with neither `HOME` nor `TIKOCI_HOME` then has no - * user scope, while a fake HOME moves the host scope (and, once centrs passes + * fake HOME here. A caller-supplied empty map then has no user scope, while a + * fake HOME moves the host scope (and, once centrs passes * its env on to `QuickCHR.get()`, quickchr's data dir). Both tiers use this. */ export const HERMETIC_ENV: Record = {