* fix(sync): report partial (pull_failed) instead of up_to_date when git pull fails with zero imports (#3068) A warn-and-continue internal git pull failure (e.g. a local-path origin rejected by protocol.file.allow=never) combined with a zero-import run previously reported `up_to_date`, exited 0, and bumped the last_sync_at freshness heartbeat. A permanently-failing pull was therefore invisible forever: doctor's sync_freshness never fired and every scheduled sync looked clean while the source silently went stale. Now, when the pull failed and the run imported nothing, sync returns `partial` with the new reason `pull_failed`, leaves last_commit AND last_sync_at untouched (so staleness monitoring fires), and prints a dedicated non-success message. The fall-through-to-working-tree design is unchanged: local commits still import when the remote is unreachable, and the anchor still advances over commits that were actually imported. Addresses the report in #3068. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(sync): surface pull_failed to CLI exit codes, sync --all JSON, and the cycle phase (#3068 review round) Codex review round 1 follow-ups: - Single-source `gbrain sync` sets exit code 1 on partial/pull_failed (timeout-class partials keep exit 0 — they converge on retry; a failing pull does not). - `sync --all` exits 1 when any source reports pull_failed, and the --json envelope carries the per-source partial `reason`. - The autopilot cycle's sync phase maps partial/pull_failed to `warn` with a dedicated summary and a `syncReason` detail, so a scheduled cycle no longer reports a clean run over a wedged source. - The regression test now isolates GBRAIN_HOME to a temp dir so the first full sync cannot touch the real sync-failure ledger. - Current-state docs: KEY_FILES.md sync.ts entry + TESTING.md inventory describe the pull_failed contract and the new test. Addresses the report in #3068. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(sync): route pull_failed exit through the owned verdict channel; make the regression test serial (#3068 review round 2) Codex review round 2 follow-ups: - Single-source exit now uses setCliExitVerdict(1) instead of a raw process.exitCode assignment, which the CLI teardown deliberately ignores (PGLite's Emscripten runtime clobbers process.exitCode mid-run; the owned channel in src/core/cli-force-exit.ts is the only trusted verdict). Pinned by test/cli-exit-verdict-pin.test.ts. - The regression test is renamed to *.serial.test.ts because it pins GBRAIN_HOME for the whole file (scripts/check-test-isolation.sh R1); docs updated to the new name. Addresses the report in #3068. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
47 KiB
Testing (gbrain repo)
On-demand reference (see CLAUDE.md Reference map). Current behavior + invariants only.
test/e2e/serve-http-oauth.test.ts additionally pins confidential POST/Basic revocation, public-client SDK fallthrough, malformed/mixed authentication rejection, cross-client isolation, unknown-token opacity, metadata auth methods, no-store responses, strict post-revoke 401, and retryable backend 503 semantics.
Test command tiers
Seven test command tiers, each with a clear scope:
| Command | What it runs | Wallclock | When to use |
|---|---|---|---|
bun run test |
Parallel unit-test fast loop. 8-shard fan-out via scripts/run-unit-parallel.sh, then a serial pass over *.serial.test.ts. Excludes *.slow.test.ts and test/e2e/*. No pre-checks, no typecheck. |
~85s on a Mac dev box (3650+ tests) | Inner edit loop. Default. |
bun run verify |
CI's authoritative pre-test gate set, fanned out in parallel by scripts/run-verify-parallel.sh: the full check:* battery (~30 checks — privacy, jsonb, progress, source-id, test-isolation, wasm, …) plus bun run typecheck. The CHECKS array in that script is the single source of truth — CI literally calls bun run verify in a dedicated job. |
~16s (parallel; typecheck dominates) | Before pushing; before /ship. |
bun run test:full |
verify && bun run test && bun run test:slow && [smart e2e]. The local equivalent of "everything CI runs." Smart e2e: runs e2e only when DATABASE_URL is set; else loud skip notice to stderr. |
~3-5min depending on slow + e2e | Pre-merge sanity, before opening a PR. |
bun run test:slow |
Just the *.slow.test.ts set (intentional cold-path correctness checks). |
seconds-to-minutes | When touching slow-path code. |
bun run test:serial |
Just the *.serial.test.ts set (cross-file-contention quarantine; one bun process per file for true module-registry isolation). |
~1s per quarantined file | Debugging a specific quarantined file. |
bun run test:e2e |
Real Postgres E2E. Requires Docker + DATABASE_URL. Sequential. |
~5-10min | Pre-ship; nightly. |
bun run check:all |
The historical pre-check scripts (22, chained sequentially in package.json). Overlaps verify heavily but is NOT a superset — verify's CHECKS array in scripts/run-verify-parallel.sh (~30 entries incl. typecheck) is the authoritative gate; check:all keeps a few local-only extras (trailing-newline, exports-count, no-legacy-getconnection). |
~10s | Local-only sweep for the extras. |
CI vs local: intentionally divergent file sets
- CI matrix (
.github/workflows/test.yml) runsscripts/test-shard.shacross 10 matrix shards partitioned by weight-aware LPT bin-packing (scripts/sharding.ts) and INCLUDES*.slow.test.ts(the two outlier slow files run as dedicated jobs alongside the matrix). CI EXCLUDES*.serial.test.tsfrom the shards and runs them in a dedicated job viabun run test:serial, one bun process per file — keeping serial files out of the shard processes is what preserves themock.modulequarantine (a top-level mock in one file leaks into every other file sharing its process).bun run verifygets its own job too. CI is the ground truth for "did everything pass." - Local fast loop (
scripts/run-unit-shard.shvia the parallel wrapper) uses round-robin-by-index sharding and EXCLUDES*.slow.test.tsAND*.serial.test.ts. Local trades coverage for inner-loop speed; CI catches what local skips.
This divergence is intentional. Don't try to make them equal — the two scripts deliberately solve different problems. The regression test at test/scripts/run-unit-shard.test.ts pins what the local fast loop should and shouldn't include.
Failure-first logging
When bun run test finds any failure, the wrapper:
- Writes failure blocks (each prefixed with
--- shard N: <test name> ---) to.context/test-failures.log(workspace-local, gitignored). On systems without a writable.context/, falls back to/tmp/gbrain-test-failures.log. - Prints a loud stderr banner with the absolute log path, plus the last 30 lines of the failure log inlined. Banner survives
| head/| tail/ agent-side log truncation. - Writes a one-line-per-shard summary to
.context/test-summary.txt(shard N/M: pass=X fail=Y skip=Z rc=W). - Exits non-zero. Empty failure log + non-zero exit = infrastructure problem (wedged shard, killed child); the banner says so.
If a shard wedges (per-shard GBRAIN_TEST_SHARD_TIMEOUT cap, default 600s), the wrapper writes --- shard N: WEDGED after ${SHARD_TIMEOUT}s --- to the failure log, includes the last 50 lines of the shard log, and proceeds with other shards' results.
File taxonomy
*.test.ts→ fast loop (parallel 8-shard fan-out).*.slow.test.ts→ run viabun run test:slowonly (intentional cold-path tests; would dominate the fast loop's wallclock).*.serial.test.ts→ run viabun run test:serialafter the parallel pass completes; one bun process per file (--max-concurrency=1within a shared process is not enough — the module registry still leaksmock.module). Quarantine for tests that share file-wide state and race when run alongside other files in the samebun testprocess. Several dozen files, discovered by the*.serial.test.tsglob — no list to maintain. Typical residents:mock.module(...)users (top-level mocks leak across files in a shard process, e.g.test/embed.serial.test.ts), env-coupled files (e.g.test/brain-registry.serial.test.ts), and process-lifecycle suites that assert onprocess.exitCode(e.g.test/pglite-engine-disconnect.serial.test.ts). Do not put the parallelism back on a serial file unless you've fixed the contention root cause (it just re-introduces the flake).test/e2e/*.test.ts→ real-Postgres E2E. Skipped whenDATABASE_URLis unset.tests/heavy/*.sh→ ops-shape shell scripts. Cost minutes per run; NOT in defaultbun test. Run viabun run test:heavyor scheduled nightly via.github/workflows/heavy-tests.yml. Examples: pg_upgrade matrix (boot legacy brain → walk to head), RSS budget gate (measure peak worker RSS vs committed baseline), read-latency-under-sync (p50/p95/p99 under concurrent writer load), sync lock regression (N concurrent syncs assert 1 winner + N-1 lock-busy + zero leakedgbrain_cycle_locksrows). Seetests/heavy/README.mdfor when to add a script here vs*.slow.test.ts. Files prefixed with_(e.g.tests/heavy/_build_legacy_fixtures.sh) are helpers/libs invoked by sibling tests — the runner skips them.test/fuzz/*.test.ts→ property-based fuzz harness. Pure-validator targets inpure-validators.test.tsare guarded byscripts/check-fuzz-purity.sh(inbun run verify), whichbun build --target=bunbundles each target and greps the resulting bundle for banned transitive imports (node:fs,node:child_process, engine modules). Anything that fails the guard moves tomixed-validators.test.ts(still property-tested, but no purity guarantee) orfilesystem-validators.test.ts(fs-backed, uses temp dirs). Fuzz tests run in the defaultbun testloop because they're fast (~3s for ~12 properties × 1000 runs each).
Test-isolation lint and helpers
The cross-file flake class is enforced statically by scripts/check-test-isolation.sh, wired into bun run verify and bun run check:all. Rules (non-serial unit files only; *.serial.test.ts and test/e2e/* are skipped):
| Rule | What it bans | Fix |
|---|---|---|
| R1 | process.env.X = ..., bracket assignment, delete process.env.X, Object.assign(process.env, ...), Reflect.set(process.env, ...) |
Use withEnv() from test/helpers/with-env.ts, OR rename file to *.serial.test.ts |
| R2 | mock.module(...) anywhere in the file |
Rename file to *.serial.test.ts (no DI on production code for testability) |
| R3 | new PGLiteEngine( outside ~50 lines after a beforeAll( line |
Use the canonical block (below) inside beforeAll( |
| R4 | Files creating new PGLiteEngine( without engine.disconnect( inside an afterAll( block |
Add afterAll(() => engine.disconnect()) |
Files that violated these rules at the isolation-lint baseline are listed in scripts/check-test-isolation.allowlist. The allow-list MUST shrink over time — never add new entries.
Canonical PGLite block (R3 + R4 compliant)
Every test file that needs a PGLite engine should use this exact pattern:
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { resetPgliteState } from './helpers/reset-pglite.ts';
let engine: PGLiteEngine;
beforeAll(async () => {
engine = new PGLiteEngine();
await engine.connect({});
await engine.initSchema();
});
afterAll(async () => {
await engine.disconnect();
});
beforeEach(async () => {
await resetPgliteState(engine);
});
Why this exact shape: beforeAll creates a single engine per file (PGLite WASM cold-start + initSchema is ~20s); beforeEach truncates user data via resetPgliteState ("two orders of magnitude faster" than fresh-engine-per-test); afterAll disconnects so the engine doesn't leak across file boundaries within a shard process.
withEnv pattern (R1 fix)
import { withEnv } from './helpers/with-env.ts';
test('reads OPENAI_API_KEY', async () => {
await withEnv({ OPENAI_API_KEY: 'sk-test' }, async () => {
expect(loadConfig().openai_key).toBe('sk-test');
});
});
// Delete a var (override is undefined):
await withEnv({ GBRAIN_HOME: undefined }, fn);
// Multiple keys:
await withEnv({ A: '1', B: '2', C: undefined }, fn);
withEnv saves the prior value of every key it touches and restores via try/finally — including when the callback throws. It is cross-test safe but NOT intra-file concurrent-safe. process.env is process-global; two test.concurrent() calls in the same file both touching the same key will race. Files using withEnv stay outside the test.concurrent() codemod's eligibility filter.
When to quarantine instead of fix
Rename to *.serial.test.ts when:
- The file uses
mock.module(...)(R2 — there's no clean fix without changing production code). - The file is genuinely env-coupled (e.g.
gbrain-home-isolation.test.ts,claw-test-cli.test.ts) — module-load env readers + ESM caching defeat dynamic-import-after-env tricks. - The file's tests intentionally share state across
it()boundaries.
The quarantine has grown to dozens of files — treat it as debt: every addition needs a reason from the list above, and prefer fixing the contention root cause when one exists.
Unit test inventory
bun test runs all tests without a database. E2E tests skip gracefully when DATABASE_URL is not set.
Unit tests and what they cover:
test/markdown.test.ts— frontmatter parsing;splitBodysentinel precedence, horizontal-rule preservation,inferTypewiki subtypes.test/chunkers/recursive.test.ts— chunking.test/parity.test.ts— operations contract parity.test/cli.test.ts— CLI structure.test/cli-finish-teardown.test.ts— the #2084 teardown contract:computeTeardownDeadlineMsformula/floor/live-registry scaling +GBRAIN_TEARDOWN_DEADLINE_MSoverride (garbage/zero/negative values fall back to the formula);finishCliTeardownclean path (drain BEFORE disconnect, no exit, no warn), backstop on hung drain or disconnect (honors an errored op's exit code), throwing drain/disconnect warned + swallowed; the gbrain-owned verdict channel is immune to PGLite WASMprocess.exitCodewrites;flushThenExitunit coverage with mocked streams (exits once after both stream callbacks, non-TTY aliveness grace, blocked-pipe guard, EPIPE-safe,GBRAIN_FLUSH_GRACE_MSoverride).test/flush-then-exit-harness.test.ts— real spawned-Bun pipe semantics forflushThenExit(fixture:test/fixtures/flush-then-exit-harness.ts): a 4MB piped stdout payload arrives byte-complete with the exit code even with a late reader, small output survives exit with a concurrent reader, and the fence resolves promptly (wall time well under the guard + grace ceiling).test/cli-should-force-exit.test.ts—shouldForceExitAfterMaindaemon-survival gate:serve(stdio and--http) never force-exits, including with preceding global flags; op commands / empty / flag-only argv do; the #2084 case that space-separated global-flag VALUES can't fake a command (--timeout 30s serveresolves to theservedaemon, not a30scommand).test/cli-exit-verdict-pin.test.ts— #2084 structural class pin: grepssrc/so the NEXT rawprocess.exitCode =write fails CI (a raw write bypasses the gbrain-owned verdict channel and gets silently zeroed by the deliberate flush-exit — the bug that made doctor's FAIL path exit 0). Runtime variants live intest/cli-finish-teardown.test.ts; this is the review-time guard.test/cli-pipe-truncation.test.ts— real-CLI pipe completeness (the #1959 incident class), implementation-agnostic: the actual CLI run the way agents run it (piped stdout) produces complete, parseable, byte-stable--tools-jsonoutput and exits deliberately, well under the teardown backstop. Synthetic flush-mechanism coverage stays intest/flush-then-exit-harness.test.ts.test/volunteer-context.test.ts— push-based context core (#2095), hermetic in-memory PGLite:parseWindowlenientuser:/assistant:parsing, multi-turn window extraction, confidence-gated volunteering (arm confidences, multi-turn/newest-turn boosts,min_confidencegate, max-pages cap), slug-only suppression, privacy (rationales are deterministic templates; synopses pass the takes/facts fence), and the approximate usage-stats join.test/watch-command.test.ts—gbrain watchpush transport (#2095): streaming loop, rolling window, session dedupe,--jsonJSONL shape,channel: 'watch'event logging, clean EOF return. Hermetic PGLite + injected line/write deps (no subprocess, no real stdin).test/watch-sigint.serial.test.ts—gbrain watchSIGINT lifecycle against a real spawned CLI subprocess with a tmpdir brain. SERIAL: parallel unit shards flake on concurrent subprocess spawns (same rationale asapply-migrations-pglite-spawn.serial.test.ts).test/cli-format-volunteer.test.ts—formatResult'svolunteer_contexthuman rendering: pointer lines with confidence/arm/rationale, the empty-result message, the approximate stats summary.test/config.test.ts— config redaction.test/files.test.ts— MIME/hash.test/import-file.test.ts— import pipeline.test/upgrade.test.ts— schema migrations.test/file-migration.test.ts— file migration.test/file-resolver.test.ts— file resolution.test/import-resume.test.ts— import checkpoints.test/migrate.test.ts— migration: v8/v9 helper-btree-index SQL structural assertions; 1000-row wall-clock fixtures guarding the O(n²)→O(n log n) fix; v12/v13 SQL shape;sqlFor+transaction:falserunner semantics; themax_stalled DEFAULT 1regression guard; v24sqlFor.pglite: ''no-op assertion; v117context_volunteer_events(named + idempotent entry, documented columns + both source-scoped indexes afterinitSchema, insert + 90-daypurgeStaleVolunteerEventsround-trip).test/bootstrap.test.ts— bootstrap contract: no-op on fresh install, idempotent across twoinitSchema()calls, no-op on modern brain that already has every probed column, full bootstrap path on a simulated legacy brain, fresh-install regression guard, legacylinksshape coverage.test/schema-bootstrap-coverage.test.ts— CI guard.REQUIRED_BOOTSTRAP_COVERAGElists every forward reference inPGLITE_SCHEMA_SQL; the test fails loudly ifapplyForwardReferenceBootstrapskips one (extend both arrays when adding a column-with-index to the embedded schema blob). Also parsessrc/core/migrate.tssource text for everyALTER TABLE ... ADD COLUMN(top-levelsql:,sqlFor.{postgres,pglite}overrides, AND handler-bodyengine.runMigration(N, \ALTER TABLE ...`)) and asserts each (table, column) pair is covered by the bootstrap OR by the schema blob's CREATE TABLE bodies — catching the column-only forward-reference class (e.g.sources.archived,oauth_clients.source_id) that a CREATE INDEX parser alone can't see.parseBaseTableColumns` strips SQL line + block comments before identifying column names so commented-out lines don't hide adjacent columns.test/helpers/schema-diff.ts+test/helpers/schema-diff.test.ts+test/e2e/schema-drift.test.ts— cross-engine schema parity gate. Helper exports puresnapshotSchema(query)/diffSnapshots(pg, pglite, opts)/formatDiffForFailure(diff)/isCleanDiff(diff)over a four-tuple per column (data_type,udt_name,is_nullable,column_default). E2E test spins up fresh PGLite + Postgres, runsengine.initSchema()on each, snapshotsinformation_schema.columns, then diffs. 2-table allowlist (files,file_migration_ledger) — every other Postgres table must reach PGLite viaPGLITE_SCHEMA_SQLor a migration'ssqlFor.pglitebranch. Sentinels foroauth_clients,mcp_request_log,access_tokens,eval_candidatesgive tighter blame messages. Skips withoutDATABASE_URL. Wired intoscripts/e2e-test-map.tsso changes tosrc/schema.sql,src/core/pglite-schema.ts, orsrc/core/migrate.tstrigger it. The failure message names every drift with a paste-ready hint pointing atsrc/core/pglite-schema.ts.test/setup-branching.test.ts— setup flow.test/slug-validation.test.ts— slug validation.test/storage.test.ts— storage backends.test/supabase-admin.test.ts— Supabase admin.test/yaml-lite.test.ts— YAML parsing.test/check-update.test.ts— version check + update CLI.test/pglite-engine.test.ts— PGLite engine, all BrainEngine methods includingaddLinksBatch/addTimelineEntriesBatch(empty batch, missing optionals, within-batch dedup via ON CONFLICT, missing-slug rows dropped by JOIN, half-existing batch, batch of 100) plusconnect()error-wrap assertion (original error nested, #223 link in message, lock released).test/links-timeline-jsonb-poison.test.ts— gbrain#1861 PGLite half (always-on, noDATABASE_URL). Locks thejsonb_to_recordsetbatch-insert path for links/timeline/takes against free-text "poison" payloads (commas, quotes, backslashes, braces, em-dashes) and asserts NUL is stripped from free-text body fields but rejected in identity fields. gbrain#2011 adds lone-UTF-16-surrogate cases: every free-text field (link context; timeline summary/detail/source; take claim/source) well-forms to U+FFFD across batch + scalar write paths, while a surrogate in an identity field (slug) still fail-closed rejects the batch. The Postgres lane (test/e2e/jsonb-batch-poison-postgres.test.ts) is the one that actually reproduced the original crash.test/engine-factory.test.ts— engine factory + dynamic imports.test/integrations.test.ts— recipe parsing, CLI routing, recipe validation.test/publish.test.ts— content stripping, encryption, password generation, HTML output.test/backlinks.test.ts— entity extraction, back-link detection, timeline entry generation.test/lint.test.ts— LLM artifact detection, code fence stripping, frontmatter validation.test/report.test.ts— report format, directory structure.test/skills-conformance.test.ts— skill frontmatter + required sections validation.test/resolver.test.ts— RESOLVER.md coverage, routing validation; round-trip that every quoted RESOLVER.md trigger matches a frontmattertriggers:entry in the target skill, and everyname="<word>"reference in any SKILL.md resolves to a declared op insrc/core/operations.tsor a Minions handler inPROTECTED_JOB_NAMES.test/search.test.ts— RRF normalization, compiled truth boost, cosine similarity, dedup key.test/sql-ranking.test.ts— source-boost helpers: longest-prefix-match in SQL CASE,detail=hightemporal-bypass, three-meta-char LIKE escape (%,_,\), single-quote SQL-literal doubling, env override parsing forGBRAIN_SOURCE_BOOST+GBRAIN_SEARCH_EXCLUDE,resolveBoostMap/resolveHardExcludesmerge semantics.test/dedup.test.ts— source-aware dedup, compiled truth guarantee, layer interactions.test/intent.test.ts— query intent classification: entity/temporal/event/general.test/eval.test.ts— retrieval metrics:precisionAtK,recallAtK,mrr,ndcgAtK,parseQrels.test/check-resolvable.test.ts— resolver reachability, MECE overlap, gap detection, proximity-based DRY detection,extractDelegationTargetscoverage.test/dry-fix.test.ts— auto-fix: three shape-aware expander pure-function tests; five guards (working-tree-dirty, no-git-backup, inside-code-fence, already-delegated within 40 lines, ambiguous-multi-match, block-is-callout).test/doctor-fix.test.ts—gbrain doctor --fixCLI integration: dry-run preview, apply path, JSON output shape.test/backoff.test.ts— load-aware throttling, concurrency limits, active hours.test/fail-improve.test.ts— deterministic/LLM cascade, JSONL logging, test generation, rotation.test/transcription.test.ts— provider detection, format validation, API key errors.test/enrichment-service.test.ts— entity slugification, extraction, tier escalation.test/data-research.test.ts— recipe validation, MRR/ARR extraction, dedup, tracker parsing, HTML stripping.test/minions.test.ts— Minions job queue: CRUD, state machine, backoff, stall detection, dependencies, worker lifecycle, lock management, claim mechanics, depth/child-cap, timeouts, cascade kill, idempotency,child_doneinbox, attachments, removeOnComplete/Fail,max_stalledclamp/default/plumbing coverage.test/extract.test.ts— link extraction, timeline extraction, frontmatter parsing, directory type inference.test/extract-db.test.ts—gbrain extract --source db: typed link inference, idempotency,--typefilter,--dry-runJSON output.test/extract-fs.test.ts—gbrain extract --source fs: first-run inserts + second-run reports zero, dry-run dedups candidates across files, second-run perf regression guard for the N+1 dedup bug.test/link-extraction.test.ts— canonicalextractEntityRefsboth formats,extractPageLinksdedup,inferLinkTypeheuristics,parseTimelineEntriesdate variants,isAutoLinkEnabledconfig.test/graph-query.test.ts— direction in/out/both, type filter, indented tree output.test/features.test.ts— feature scanning, brain_score calculation, CLI routing, persistence.test/file-upload-security.test.ts— symlink traversal, cwd confinement, slug + filename allowlists, remote vs local trust.test/query-sanitization.test.ts— prompt-injection stripping, output sanitization, structural boundary.test/search-limit.test.ts—clampSearchLimitdefault/cap behavior acrosslist_pagesandget_ingest_log.test/repair-jsonb.test.ts— JSONB repair: TARGETS list, idempotency, engine-awareness.test/migrations-v0_12_2.test.ts— JSONB-repair orchestrator phases: schema → repair → verify → record.test/orphans.test.ts— orphans command: detection, pseudo filtering, text/json/count outputs, MCP op.test/postgres-engine.test.ts—statement_timeoutscoping:sql.begin+SET LOCALshape, source-level grep guardrail against a reintroduced bareSET statement_timeout.test/sync.test.ts— sync logic + regression guard asserting top-levelengine.transactionis not called.test/sync-pull-failed-anchor.serial.test.ts— #3068 regression: a failed internalgit pull(local-path origin vsprotocol.file.allow=never) with zero imports returnspartial/pull_failed(notup_to_date), freezeslast_commit+last_sync_at, recovers after a manual pull; fall-through import of local commits preserved. Serial: pinsGBRAIN_HOMEto a temp dir for the whole file.test/sync-concurrency.test.ts—autoConcurrency()thresholds + PGLite-forces-serial + explicit-override clamping;shouldRunParallel()explicit-bypasses-floor contract;parseWorkers()validation rejecting'0'/'-3'/'foo'/'1.5'/trailing chars.test/sync-parallel.test.ts— PGLite-routed coverage of the bookmark gate under concurrency, head-drift gate, vanished-file failure capture, PGLite-stays-serial, and thegbrain-syncwriter-lock contract.test/sync-failures.test.ts—classifyErrorCoderegex coverage for all 12 codes against literal production message strings frommarkdown.tsandimport-file.ts;summarizeFailuresByCodesort + pre-classified-honor;recordSyncFailurescode-field persistence;acknowledgeSyncFailuresAcknowledgeResultshape + backfill on legacy entries.test/doctor.test.ts— doctor command; assertions thatjsonb_integrityscans the four JSONB write sites andmarkdown_body_completenessis present.test/utils.test.ts— shared SQL utilities +tryParseEmbeddingnull-return and single-warn semantics.test/build-llms.test.ts—llms.txt/llms-full.txtgenerator: path resolution, idempotence, spec shape, regen-drift guard, content contract, AGENTS.md install-path mirror, size-budget enforcement.test/oauth.test.ts— OAuth 2.1 provider: register, getClient,client_credentialsgrant exchange,authorization_codeflow with PKCE challenge/verifier, refresh token rotation,verifyAccessTokenwith both OAuth + legacyaccess_tokensfallback,revokeToken,sweepExpiredTokens; contract test assertingscope+localOnlyannotations on all operations;coerceTimestampunit cases (null/undefined/string/number/throw-on-NaN); NULL-expires_at-as-expired contract for both refresh + access token paths; cascade-delete contract assertingrevoke-clientpurgesoauth_tokens+oauth_codesvia FK CASCADE; cross-client isolation (wrong-client attempt MUST reject AND rightful owner MUST still succeed atomically afterward); empty-stringredirect_uribypass guard; PKCE DCR public-client gate (token_endpoint_auth_method: "none"returns noclient_secret, defaultclient_secret_postclients get the one-time-reveal secret,getClientNULL→undefined normalization, full PKCE/authorize→/tokenround-trip against a public client).test/mcp-dispatch-summarize.test.ts—summarizeMcpParamsinvariants: declared-keys allow-list intersection, attacker-key-name leak guard (unknown keys counted not named), 1KB byte bucketing for size-probe defense, missing op falls through to fully-redacted shape, declared-keys sorted for deterministic output.test/trust-boundary-contract.test.ts— fail-closed trust semantics under cast bypass:ctx.remote === undefinedtreated as remote/untrusted at every flipped call site;as anyandPartial<>spreads can't downgrade trust by accident.test/check-resolvable-cli.test.ts— CLI wrapper: exit codes, JSON envelope shape, AGENTS.md fallback chain.test/regression-v0_16_4.test.ts—findRepoRootregression guard, hermetic startDir parameterization.test/repo-root.test.ts—findRepoRootwalk semantics + default-arg parity; the 4-tierautoDetectSkillsDirfallback chain ($OPENCLAW_WORKSPACE→~/.openclaw/workspace→ repo-root →./skills); RESOLVER.md/AGENTS.md filename precedence; explicit-env-wins-over-repo-root; tier-0$GBRAIN_SKILLS_DIRvalid/invalid/precedence-over-OPENCLAW_WORKSPACE; the install-path walk inautoDetectSkillsDirReadOnly; no-drift on primary success;AUTO_DETECT_HINT+AUTO_DETECT_HINT_READ_ONLYcontent; regression guard asserting the sharedautoDetectSkillsDirMUST NEVER return'install_path'source (how the read-path/write-path split stays safe).test/resolver-merge.test.ts— multi-file resolver merge:findAllResolverFilesempty / RESOLVER.md-only / AGENTS.md-only / both-present (RESOLVER.md first);checkResolvablemerge semantics acrossskills/RESOLVER.md+../AGENTS.mdfor the OpenClaw layout where the skillpack ships a thin RESOLVER.md and the real dispatcher lives at the workspace root; dedup byskillPath(first occurrence wins); AGENTS.md-at-workspace-root works alone.test/filing-audit.test.ts— filing audit:writes_pages/writes_tofrontmatter, filing-rules JSON validation.test/skill-brain-first.test.ts— shared frontmatter parser;analyzeSkillBrainFirstcompliance ladder across 9 fixtures undertest/fixtures/brain-first-skills/(compliant-callout, compliant-phase, compliant-position, exempt-frontmatter, missing-brain-first, multi-pattern, negation-prose, no-external, typo-frontmatter); offset helpers; external-lookup regex shape; audit snapshot+diff transition logic;FORMERLY_HARDCODED_EXEMPTregression absorption.test/routing-eval.test.ts— fixture parsing, structural routing,ambiguous_with, Haiku tie-break layer.test/skill-manifest.test.ts— skill manifest parser: drift detection, managed-block markers.test/skillify-scaffold.test.ts—gbrain skillify scaffoldstubs: SKILL.md, script, tests, routing-eval fixtures.test/skillpack-install.test.ts—gbrain skillpack installmanaged-block install / update / no-clobber semantics.test/skillpack-sync-guard.test.ts— sync-guard: bundled skills stay byte-identical toskills/source.test/http-transport.test.ts— HTTP transport: bearer auth + missing/no-Bearer/unknown/revoked +/healthbypass; dispatch.ts round-trip; invalid_params; application/json response shape (not SSE); CORS default-deny + allowlist; body cap on Content-Length AND chunked; two-bucket rate limit (refill, exhaust+Retry-After, LRU eviction, TTL prune, pre-auth IP fires before DB);mcp_request_logaudit on success + auth_failed.test/restart-sweep.test.ts—recipes/restart-sweep.mdinlined script: sentinel-anchored fenced-block extraction with salted tmp filenames to bypass ESM cache; constructor-time env reads (proves no module-load snapshot); idempotency layer load/save/atomic-tmp-rename/corrupt-JSON-recovery/30-day-prune;(sessionKey, lastAlertedAt)cooldown gate with 6h threshold; AGGRESSIVE-gate two-state tests; execFile argv shape proving shell metachars inOPENCLAW_TELEGRAM_GROUPcannot reach/bin/sh; real-\n-not-literal alert formatting;GBRAIN_HOMEstate path override.test/eval-longmemeval.test.ts— LongMemEval harness, hermetic with noDATABASE_URLand no API keys: PGLite create + reset over runtime-enumeratedpg_tables, infrastructure-table preservation across resets, JSONL question parsing, retrieval-only and answer-gen modes via stubbedThinkLLMClient,--limitcutoff,--keyword-onlyvs hybrid, default--expansion=offbehavior, perf gate (p50 < 30ms / p99 < 50ms warm reset+import+search on Apple Silicon),--helpworks without a configured brain, fixture round-trip viatest/fixtures/longmemeval-mini.jsonl.test/longmemeval-sanitize.test.ts— sanitization parity pinning thatINJECTION_PATTERNSfromsrc/core/think/sanitize.tsis the single source of truth (adding a pattern there must cover both<take>framing and<chat_session>framing, no per-surface regex drift).test/openai-compat-multimodal.test.ts— gateway's openai-compatible multimodal path: happy-path single + multi-input embedding, unauthenticated proxy mode, dimension-mismatch guard (throwsAIConfigErrorwith model id + observed + expected pre-storage), default-dim fallback when recipe declaresdefault_dims, HTTP 401 / 400 / malformed-JSON / non-array error paths, regression that the existing Voyage/multimodalembeddingsrecipe still routes through its dedicated path. Hermetic via the__setEmbedTransportForTestsseam.test/serve-stdio-lifecycle.test.ts—MCP_STDIO=1env guard: stdin EOF does NOT trigger shutdown when the env is set, SIGTERM still does (guard scope is correct), unset env preserves the CLI lifecycle. Exercises theServeOptions.mcpStdio?: booleantest seam directly so tests don't mutateprocess.env.
E2E test inventory
E2E tests live in test/e2e/ and run against real Postgres+pgvector (require DATABASE_URL), except where noted as PGLite in-memory (no DATABASE_URL needed).
bun run test:e2eruns Tier 1 (mechanical, all operations, no API keys). Includes dedicated cases for the postgres-engineaddLinksBatch/addTimelineEntriesBatchbind path — postgres-js's JSONB bind (jsonb_to_recordset(($1::jsonb)->'rows')) differs from PGLite's and gets its own coverage.test/e2e/search-quality.test.ts— search quality against PGLite (no API keys, in-memory).test/e2e/graph-quality.test.ts— knowledge graph pipeline (auto-link via put_page, reconciliation, traversePaths) against PGLite in-memory.test/e2e/jsonb-batch-poison-postgres.test.ts— gbrain#1861 regression, the engine that actually crashed. Seeds free-text "poison" context (Zoom URL with?pwd=, commas, quotes, Windows backslash path, braces, em-dash) and asserts the links/timeline/takes batch writers no longer error with "malformed array literal"; also asserts NUL is stripped from free-text bodies (context/summary/detail/claim) and still rejected in identity fields. gbrain#2011 adds the lone-surrogate crash lock: a lone UTF-16 surrogate in free text (the value that abortedextract --stalewith22P02on Supabase) well-forms to U+FFFD across batch + scalar paths (incl. timeline + takesource), while a surrogate in an identity field still rejects the batch.DATABASE_URL-gated.test/e2e/postgres-jsonb.test.ts— round-trips all 5 JSONB write sites (pages.frontmatter,raw_data.data,ingest_log.pages_updated,files.metadata,page_versions.frontmatter) against real Postgres and assertsjsonb_typeof='object'plus->>'key'returns the expected scalar. Guards against the double-encode bug.test/e2e/integrity-batch.test.ts— parity forscanIntegrity's batch-load fast path vs sequential. Cases (dedup, hits, validate, topPages) seed a fixture and assert both paths return identical results. Dedup case uses raw SQL viagetConn().unsafe()to seed a(test-source-2, people/alice)row alongside the default-source row, sinceengine.putPagedoesn't take asource_id. Pins multi-source overcounting; the "multi-source duplicate slugs scan once" case expects both batch + sequential paths to report 2.test/e2e/jsonb-roundtrip.test.ts— companion regression against the 4 doctor-scanned JSONB sites. Assertion-level overlap withpostgres-jsonb.test.tsis intentional defense-in-depth: if doctor's scan surface drifts from the actual write surface, one of these tests catches it.test/e2e/sync.test.ts—--skip-failedfailure-loop test alongside happy-path tests: broken file →performSyncreturnsblocked_by_failureswith grouped breakdown →performSync({skipFailed: true})advances bookmark and returnsAcknowledgeResultwith code summary → second broken file → second cycle. Saves and restores the user's real~/.gbrain/sync-failures.jsonlso the test is hermetic. Asserts bookmark gating, JSONL state, dedup across paths, summary aggregation, and the literal doctor-rendering string format.test/e2e/upgrade.test.ts— check-update against real GitHub API (network required).test/e2e/minions-shell-pglite.test.ts— PGLite--followinline shell-job path (in-memory, noDATABASE_URLrequired) — the path the minion-orchestrator skill documents for dev use.test/e2e/pglite-cli-exit.serial.test.ts— real spawned-CLI exit behavior on PGLite (in-memory, noDATABASE_URL): read commands (search/get/query) exit 0 promptly; CLI_ONLYcaptureexits clean and frees the single-writer lock; the#2084describes pin every swept disconnect site — a failed op exits 1 with the error on stderr, and the dashboard, read-only-timeout, doctor, anddream --dry-runpaths all exit with no force-exit banner.test/e2e/pgbouncer-teardown.test.ts— PgBouncer TRANSACTION-mode teardown (#2084 / the #1972→#2015→#2084 class). Pins the bug CLASS, not timings: a CLI op against a txn-mode pooled URL exits 0 with intact stdout and does NOT ride the 10s hard-deadline backstop (theengine.disconnect() did not returnbanner is the smoking gun — pre-#2084 it printed on 100% of query-shaped ops). Gated byGBRAIN_PGBOUNCER_URL+GBRAIN_PGBOUNCER_DIRECT_URL(NOTDATABASE_URL) — set automatically bybun run ci:local'spgbouncercompose service; skips gracefully elsewhere. Uses a DEDICATEDgbrain_pgbouncerdatabase so it never races thegbrain_testTRUNCATE fixtures.test/e2e/volunteer-context-postgres.test.ts—volunteer_contexton REAL Postgres (#2095; engine parity beyond the hermetic PGLite unit suite): resolution arms through the actual op handler, the fire-and-forget volunteer-event sink landing rows, the stats join, and the RLS pin thatcontext_volunteer_eventshas ROW LEVEL SECURITY enabled (keeps the v35 auto-RLS event trigger honest for migration-created tables).DATABASE_URL-gated.test/e2e/openclaw-reference-compat.test.ts—check-resolvable+skillpack installagainst a minimal AGENTS.md workspace fixture (test/fixtures/openclaw-reference-minimal/), regression guard for the OpenClaw deployment shape.test/e2e/search-swamp.test.ts— reproduces the source-swamp case. Seeds a curatedoriginals/talks/article-outline-fat-codepage against two<fork>/chat/pages stuffed with the same multi-word phrase. Asserts the article wins keyword AND vector ranking, thatdetail=highlets the chat swamp re-surface, and thatsource_idpasses through the two-stage CTE intact. PGLite in-memory.test/e2e/search-exclude.test.ts—test/+archive/pages hidden by default,include_slug_prefixesopts back in, caller-suppliedexclude_slug_prefixesadds to defaults. Both keyword and vector search paths.test/e2e/engine-parity.test.ts— Postgres ↔ PGLite top-result and result-set parity forsearchKeyword+searchVector(Postgres ranks pages then picks best chunk while PGLite returns chunks directly, so the source-boost behavior needs parity coverage). Skips withoutDATABASE_URL.test/e2e/postgres-bootstrap.test.ts— exercisesPostgresEngine.initSchema()directly against a fresh real Postgres database. Asserts the bootstrap path is no-op on fresh installs and that SCHEMA_SQL replays cleanly through the engine path (not via the standalonedb.initSchemafromsrc/core/db.ts).test/e2e/http-transport.test.ts—gbrain serve --httpend-to-end against real Postgres: bearer auth round-trip,last_used_atSQL-level debounce,mcp_request_logrow insertion on success and auth_failed paths,/healthDB-down → 503 (DB-probing health check), and the dispatch round-trip with a real operation. Skips withoutDATABASE_URL.test/e2e/serve-http-oauth.test.ts— real-Postgres E2E againstgbrain serve --httpwith full OAuth 2.1. Spawns a subprocess server, registers a client via the CLI, mintsclient_credentialstokens, exercises the/mcpJSON-RPC pipeline. Real DCR/registerHTTP-level response-shape test (assertstypeof body.client_id_issued_at === 'number'over the wire, RFC 7591 §3.2.1); real CLI subprocess test forrevoke-client(registers → mints token → revokes viaexecSync→ asserts token rejected at/mcp→ asserts re-run exits 1); server fixture flips on--enable-dcrso/registeris reachable. bun execSync env-inheritance contract: bun'sexecSyncdoes NOT inherit env mutations done viaprocess.env.X = ..., only OS-level env from before bun started. helpers.ts loads.env.testingand setsDATABASE_URLviaprocess.envmutation, which is invisible to subprocesses unlessenv: { ...process.env }is passed explicitly — every subprocess call in this file passesenv: { ...process.env }. Reference fix for the same failure mode in sibling sync/cycle/dream/claw-test E2Es.afterAllcleanup is guarded onclientId(won't throw ifbeforeAllfailed before registration); cleanup errors surface to stderr without throwing so real test failures aren't masked. Also covers the trust-boundary fix: an HTTP MCPsubmit_jobforname: "shell"MUST reject with a permission error (request handler setsremote: trueandsubmit_job's protected-name guard fires), and the same guard rejects subagent submission. Skips withoutDATABASE_URL.test/e2e/sync-parallel.test.ts—DATABASE_URL-gated. 60-file Postgres sync at concurrency=4 imports all + no connection leak (probespg_stat_activitybefore/after to confirm worker engines disconnected). 120-file serial-vs-parallel benchmark printsSYNC_PARALLEL_BENCH N files | serial=Xms | parallel(4)=Yms | speedup=Zx. Asserts parallel ≤ serial × 1.5 (CI-noise tolerant; not a strict speedup gate).test/e2e/multi-source-bug-class.test.ts— PGLite in-memory regression suite pinning every multi-source bug site:listAllPageRefsordering by(source_id, slug),getPagewith sourceId picks the right(source, slug)row,extract-takesprocesses both overlappingpeople/alicerows independently,listPagesfilters correctly withPageFilters.sourceId,addLinksBatchwithfrom/to_source_idtargets the right rows,validateSourceIdrejects path traversal, reverse-write disk layout usesbrainDir/.sources/<id>/<slug>.mdfor non-default sources,copyMigrationSourceslands source metadata before overlapping-slug pages. NoDATABASE_URLneeded. Wired intoscripts/e2e-test-map.tsso changes to extract-takes / patterns / synthesize / embed / extract / migrate-engine auto-trigger it.test/e2e/migrate-engine-sources-postgres.test.ts—DATABASE_URL-gated companion forgbrain migrate --to: migrates a PGLite brain carrying two non-default sources with overlapping slugs into real Postgres and assertscopyMigrationSourcescreated everysourcesFK parent (config JSONB intact, not double-encoded) before any page write. Unit-level manifest identity (crash manifest resumes only against the SAME target; legacy engine-only manifests start fresh) istest/migrate-engine-resume.test.ts.test/e2e/facts-fence-reconcile-postgres.test.ts—DATABASE_URL-gated round-trip for the escape-aware fence parser: renders a## Factsfence whose cells carry literal pipes, backslashes (Windows paths), and empty cells viarenderFactsTable, runs the wipe-and-reinsert reconcile (runExtractFacts) on real Postgres, and asserts every cell survives byte-identically with no column shift.test/e2e/source-isolation-pglite.test.ts— PGLite in-memory regression suite pinning the source-isolation seal at two layers. Engine layer:searchKeyword/searchVector/searchKeywordChunks/listPages/getPage/traverseGraph/traversePathsapplysourceId(scalar fast path) andsourceIds(array path) correctly across both engines. Op-handler layer: routes throughsourceScopeOpts(ctx)so aread+write-scoped OAuth client bound to--source dept-xcannot see rows from neighboring sources viasearch,query,list_pages,get_page, orfind_experts. Covers bothctx.sourceId(single-source clients) andctx.auth.allowedSources(federated_read clients) precedence; federated array wins over scalar wins over nothing. NoDATABASE_URLneeded.test/e2e/think-source-isolation-pglite.test.ts— PGLite in-memory suite pinning thethinkgather stage's source scope: seeds three sources with cross-source links and embedded takes, then assertsrunGatherunder a federatedsourceIdsgrant (and under a scalarsourceId) keeps every stream — hybrid retrieval, takes keyword + vector (searchTakes/searchTakesVector), and thetraversePathsgraph walk — inside the grant while still reaching authorized neighboring sources. NoDATABASE_URLneeded.test/e2e/skill-brain-first.test.ts— doctor reportsskill_brain_firstcheck with structured issues;--fix --dry-runpreviews insertion without writing;--fixapplies the canonical Convention callout idempotently;brain_first: exemptfrontmatter resolves the warn;brain_first_typosurfaces a paste-ready hint; audit JSONL recordsdetected/resolved/fixedtransitions; stable brain emits 0 audit lines/run.- Tier 2 (
test/e2e/skills.test.ts) requires OpenClaw + API keys, runs nightly in CI. - If
.env.testingdoesn't exist in this directory, check sibling worktrees:find ../ -maxdepth 2 -name .env.testing -print -quitand copy it here if found. - Run E2E tests without asking permission. When you want to verify behavior, there's a relevant E2E test, or you're shipping anything covered by an E2E suite — spin up the test DB, run the tests, tear down. Don't ask, don't propose it, don't defer. The lifecycle is short (~2-30s startup, sub-minute tests, instant teardown) and the gate value is high. Skipping with "DATABASE_URL unset" is silent regression, not caution.
API keys and running ALL tests
ALWAYS source the user's shell profile before running tests:
source ~/.zshrc 2>/dev/null || true
This loads OPENAI_API_KEY and ANTHROPIC_API_KEY. Without these, Tier 2 tests
skip silently. Do NOT skip Tier 2 tests just because they require API keys — load
the keys and run them.
When asked to "run all E2E tests" or "run tests", that means ALL tiers:
- Tier 1:
bun run test:e2e(mechanical, sync, upgrade — no API keys needed) - Tier 2:
test/e2e/skills.test.ts(requires OpenAI + Anthropic + openclaw CLI) - Always spin up the test DB, source zshrc, run everything, tear down.
E2E test DB lifecycle (ALWAYS follow this)
You are responsible for spinning up and tearing down the test Postgres container. Do not leave containers running after tests. Do not skip E2E tests, do not ask permission to run them — see the "run without asking" rule above.
- Check for
.env.testing— if missing, copy from sibling worktree. Read it to get the DATABASE_URL (it has the port number). - Check if the port is free:
docker ps --filter "publish=PORT"— if another container is on that port, pick a different port (try 5435, 5436, 5437) and start on that one instead. - Start the test DB:
Wait for ready:
docker run -d --name gbrain-test-pg \ -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres \ -e POSTGRES_DB=gbrain_test \ -p PORT:5432 pgvector/pgvector:pg16docker exec gbrain-test-pg pg_isready -U postgres - Bootstrap the schema (required — fresh containers have no
oauth_clients,mcp_request_log,pagesetc.; tests likeserve-http-oauth.test.tswill fail withrelation "oauth_clients" does not existif you skip this):DATABASE_URL=postgresql://postgres:postgres@localhost:PORT/gbrain_test \ bun run src/cli.ts doctor --json > /dev/null 2>&1gbrain doctortriggersinitSchema()on first connect, which is the canonical way to bring a fresh DB to head.apply-migrations --yesalone does NOT seed the base schema — it runs ALTER-style migrations on top ofinitSchema. Tests that bypass the engine (rawexecSync-spawnedauth register-client) hit the schema directly and need this step to have run first. - Run E2E tests:
DATABASE_URL=postgresql://postgres:postgres@localhost:PORT/gbrain_test bun run test:e2e - Tear down immediately after tests finish (pass or fail):
docker stop gbrain-test-pg && docker rm gbrain-test-pg
Never leave gbrain-test-pg running. If you find a stale one from a previous run,
stop and remove it before starting a new one.