MCPcopy Create free account
hub / github.com/garrytan/gstack / installFakeGbrain

Function installFakeGbrain

test/gstack-memory-ingest.test.ts:331–421  ·  view source on GitHub ↗

* Stand up a fake `gbrain` shim on PATH that: * - advertises `import` in `--help` output (gbrainAvailable() passes) * - records `import ` invocations, args, and a sample of staged files * - emits a valid `--json` summary on stdout (status, imported, etc.) * - optionally drops failures t

(
  home: string,
  opts: { failingPaths?: string[] } = {},
)

Source from the content-addressed store, hash-verified

329 * include `import` and the dir argument.
330 */
331function installFakeGbrain(
332 home: string,
333 opts: { failingPaths?: string[] } = {},
334): { binDir: string; logFile: string; argsFile: string; stagingListFile: string } {
335 const binDir = join(home, "fake-bin");
336 mkdirSync(binDir, { recursive: true });
337 const logFile = join(home, "gbrain-calls.log");
338 const argsFile = join(home, "gbrain-args.log");
339 const stagingListFile = join(home, "gbrain-staging-list.log");
340 // Bash-side: when failingPaths is set, append matching JSONL entries to
341 // ~/.gbrain/sync-failures.jsonl so D7's readNewFailures can read them.
342 const failingList = (opts.failingPaths || []).join("|");
343 const script = `#!/usr/bin/env bash
344set -euo pipefail
345LOG="${logFile}"
346ARGS_LOG="${argsFile}"
347STAGING_LIST="${stagingListFile}"
348FAILING_LIST="${failingList}"
349case "\${1:-}" in
350 --help|-h)
351 cat <<EOF
352Usage: gbrain <command> [options]
353
354Commands:
355 import <dir> Import markdown directory (batch, content-addressed)
356 search <query> Keyword search across pages
357 ask <question> Hybrid semantic + keyword query
358EOF
359 exit 0
360 ;;
361 import)
362 DIR="\${2:-}"
363 NO_EMBED=0
364 JSON=0
365 shift 2 || true
366 for arg in "\$@"; do
367 case "\$arg" in
368 --no-embed) NO_EMBED=1 ;;
369 --json) JSON=1 ;;
370 esac
371 done
372 echo "import \$DIR" >> "\$LOG"
373 {
374 echo "dir=\$DIR no_embed=\$NO_EMBED json=\$JSON"
375 } >> "\$ARGS_LOG"
376 # Capture file tree from staging dir for assertion-on-shape later.
377 if [ -d "\$DIR" ]; then
378 ( cd "\$DIR" && find . -type f | sort ) > "\$STAGING_LIST" 2>/dev/null || true
379 fi
380 # If failingPaths configured, drop fake entries to sync-failures.jsonl
381 # (mtime byte-offset snapshot lets the ingest's readNewFailures pick them up).
382 if [ -n "\$FAILING_LIST" ]; then
383 mkdir -p "\${HOME}/.gbrain"
384 IFS='|' read -ra FAIL_PATHS <<< "\$FAILING_LIST"
385 for p in "\${FAIL_PATHS[@]}"; do
386 echo "{\\"path\\":\\"\$p\\",\\"error\\":\\"File too large\\",\\"code\\":\\"FILE_TOO_LARGE\\",\\"commit\\":\\"\\",\\"ts\\":\\"2026-05-09T22:00:00Z\\"}" >> "\${HOME}/.gbrain/sync-failures.jsonl"
387 done
388 fi

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected