Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

4.5 KB
Raw Blame
// The $GROK_HOME probe is WIRED, not merely available.
//
// grok-paths.test.ts proves the probe resolves. This proves somebody asks it at install time — the
// distinction that matters, because the probe is exactly the kind of code the typecheck validates
// and nothing exercises: delete the call and every other test stays green while the user whose
// `export GROK_HOME=…` lives in `.zshrc` gets the hook file written where grok will never look, with
// no badge, no notification and no diagnostic, forever.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import fs from 'fs'
import os from 'os'
import path from 'path'
import { installManagedAgentHooks } from './index'
import { _resetGrokHomeProbeForTests, ensureGrokHomeProbed, grokHomeDir } from '../grok-paths'
import { GROK_HOOK_FILE } from '../grok-paths'
import { fakePlatform } from '../../platform-fake'
import { initPlatform, resetPlatformForTests } from '../../platform'

// The probe's own source is mocked, not the probe: `installManagedAgentHooks` calls
// `ensureGrokHomeProbed()` with NO argument, so the only way to exercise the real wiring is to
// control what the login shell appears to say. Injecting a second `ask` from the test would resolve
// after the install's own call and prove nothing.
let shellAnswer: string | null = null
vi.mock('../../exec-path', async (orig) => ({
  ...((await orig()) as object),
  resolveShellEnvVar: async () => shellAnswer
}))

// A MODULE mock, not `vi.spyOn(os, 'homedir')`: the installers this reaches (codex.ts, grok, …)
// `import { homedir } from 'os'`, a binding the spy never touches. With the spy, this file rewrote
// the developer's real ~/.codex/config.toml and hooks.json — and every other agent's hook file —
// on every run. The run-wide HOME sandbox (src/core/test-home.ts) is the floor under this.
let home = ''
vi.mock('os', async (orig) => {
  const actual = await orig<typeof import('os')>()
  return { ...actual, default: { ...actual, homedir: () => home }, homedir: () => home }
})

beforeEach(() => {
  home = fs.mkdtempSync(path.join(os.tmpdir(), 'grok-home-probe-'))
  initPlatform(fakePlatform({ userDataDir: home }))
  _resetGrokHomeProbeForTests()
})
afterEach(() => {
  _resetGrokHomeProbeForTests()
  resetPlatformForTests()
  vi.restoreAllMocks()
  fs.rmSync(home, { recursive: true, force: true })
})

describe('installManagedAgentHooks — the $GROK_HOME probe (§8.9)', () => {
  it("writes grok's hook where the LOGIN SHELL says grok lives, not only under ~/.grok", async () => {
    const elsewhere = path.join(home, 'relocated-grok')
    shellAnswer = elsewhere
    // Nothing is pre-resolved: the synchronous install runs first and writes at the OLD home, then
    // the probe lands and the re-install puts the file where grok will actually look. That is the
    // real boot sequence, and it is what makes deleting the re-install a red test instead of a
    // silent regression.
    expect(grokHomeDir()).not.toBe(elsewhere)

    installManagedAgentHooks(new Set(['grok']))
    await new Promise((r) => setTimeout(r, 50))

    expect(grokHomeDir()).toBe(elsewhere)
    expect(fs.existsSync(path.join(elsewhere, 'hooks', GROK_HOOK_FILE))).toBe(true)
  })

  it('writes it at the default home when the shell knows nothing — behaviour unchanged', async () => {
    // Asserted against `grokHomeDir()` rather than a hand-built `~/.grok`: this module resolves the
    // home through its own imported `homedir`, so hard-coding the path here would test the test's
    // idea of the default instead of the code's.
    shellAnswer = null
    const target = grokHomeDir()
    installManagedAgentHooks(new Set(['grok']))
    await new Promise((r) => setTimeout(r, 20))
    expect(fs.existsSync(path.join(target, 'hooks', GROK_HOOK_FILE))).toBe(true)
  })
})

describe('installManagedAgentHooks — consent (issue #744)', () => {
  it('writes ONLY the agents in the set: no claude/gemini/codex config for a grok-only consent', async () => {
    shellAnswer = null
    installManagedAgentHooks(new Set(['grok']))
    await new Promise((r) => setTimeout(r, 20))
    expect(fs.existsSync(path.join(grokHomeDir(), 'hooks', GROK_HOOK_FILE))).toBe(true)
    for (const f of ['.claude/settings.json', '.gemini/settings.json', '.codex/hooks.json']) {
      expect(fs.existsSync(path.join(home, f)), f).toBe(false)
    }
  })

  it('an empty set writes nothing at all, not even the hook scripts dir', async () => {
    shellAnswer = null
    installManagedAgentHooks(new Set())
    await new Promise((r) => setTimeout(r, 20))
    expect(fs.readdirSync(home)).toEqual([])
  })
})