Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

5.6 KB
Raw Blame
package hooks

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"
)

// withoutHookExtensions clears the hook extension points (see version.Edition) for the rest of the
// test and restores whatever they held afterwards. The golden tests pin what this repository's
// installer writes on its own; a build that fills the extension points still runs them, and they
// must still hold there.
func withoutHookExtensions(t *testing.T) {
	t.Helper()
	claudeGroups, claudeChanged := claudeExtraHookGroups, claudeSettingsChanged
	cursorExtra, cursorChanged := cursorExtraHooks, cursorHooksChanged
	codexGroups, codexChanged := codexExtraHookGroups, codexHooksChanged
	claudeExtraHookGroups, claudeSettingsChanged = nil, nil
	cursorExtraHooks, cursorHooksChanged = nil, nil
	codexExtraHookGroups, codexHooksChanged = nil, nil
	t.Cleanup(func() {
		claudeExtraHookGroups, claudeSettingsChanged = claudeGroups, claudeChanged
		cursorExtraHooks, cursorHooksChanged = cursorExtra, cursorChanged
		codexExtraHookGroups, codexHooksChanged = codexGroups, codexChanged
	})
}

// requireGolden compares got with testdata/<name> byte for byte. The golden files were captured
// from the installer as it is and are never regenerated by a test run.
func requireGolden(t *testing.T, got []byte, name string) {
	t.Helper()
	want, err := os.ReadFile(filepath.Join("testdata", name))
	if err != nil {
		t.Fatal(err)
	}
	// A Windows checkout turns the golden file's line endings into CRLF; Beacon writes LF.
	want = bytes.ReplaceAll(want, []byte("\r\n"), []byte("\n"))
	if !bytes.Equal(got, want) {
		t.Fatalf("%s changed:\ngot:\n%s\nwant:\n%s", name, got, want)
	}
}

// A Cursor hooks.json holding the user's own entries (a fail-closed command, a prompt hook, an
// extra top-level key) beside Beacon entries an older version wrote: one in an event Beacon still
// installs, which the install replaces, and one in an event it no longer installs, which only an
// uninstall removes.
const cursorGoldenExisting = `{
  "version": 1,
  "note": {"owner": "user"},
  "hooks": {
    "beforeSubmitPrompt": [
      {"command": "/usr/local/bin/prompt-guard", "timeout": 5, "failClosed": true},
      {"command": "BEACON_ENDPOINT_MODE=1 BEACON_ENDPOINT_LOG=/tmp/old.jsonl /old/beacon-hooks --platform cursor prompt-submit", "timeout": 30}
    ],
    "beforeShellExecution": [
      {"type": "prompt", "prompt": "Is this command safe to run?", "model": "fast"},
      {"command": "BEACON_ENDPOINT_MODE=1 BEACON_ENDPOINT_LOG=/tmp/old.jsonl /old/beacon-hooks --platform cursor pre-tool"}
    ],
    "afterFileEdit": [
      {"command": "echo edited"}
    ]
  }
}`

func TestCursorHooksGolden(t *testing.T) {
	withoutHookExtensions(t)
	path := filepath.Join(t.TempDir(), "hooks.json")
	if err := os.WriteFile(path, []byte(cursorGoldenExisting), 0644); err != nil {
		t.Fatal(err)
	}
	if err := installCursorHooksJSON(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {
		t.Fatal(err)
	}
	installed, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	requireGolden(t, installed, "cursor_hooks_golden.json")

	if err := installCursorHooksJSON(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {
		t.Fatal(err)
	}
	again, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	requireGolden(t, again, "cursor_hooks_golden.json")

	if _, err := removeEndpointHooks(path); err != nil {
		t.Fatal(err)
	}
	removed, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	requireGolden(t, removed, "cursor_hooks_removed_golden.json")
}

// A Codex hooks.json holding the user's own groups (one with a field Beacon does not model) and an
// extra top-level key, beside groups an older version of Beacon wrote, including one for an event
// Beacon no longer hooks.
const codexGoldenExisting = `{
  "note": "user-owned",
  "hooks": {
    "PreToolUse": [
      {"matcher": "Bash", "hooks": [{"type": "command", "command": "/usr/local/bin/codex-guard", "timeout": 5, "statusMessage": "Checking the command"}]}
    ],
    "SessionStart": [
      {"hooks": [{"type": "command", "command": "echo hello"}, {"type": "command", "command": "'/old/beacon-hooks' --platform codex --log '/tmp/old.jsonl' codex-session-context", "timeout": 10}]}
    ],
    "UserPromptSubmit": [
      {"hooks": [{"type": "command", "command": "BEACON_ENDPOINT_MODE=1 /old/beacon-hooks --platform codex inventory-heartbeat"}]}
    ],
    "Stop": [
      {"hooks": [{"type": "command", "command": "echo done"}]}
    ]
  }
}`

func TestCodexHooksGolden(t *testing.T) {
	withoutHookExtensions(t)
	path := filepath.Join(t.TempDir(), "hooks.json")
	if err := os.WriteFile(path, []byte(codexGoldenExisting), 0600); err != nil {
		t.Fatal(err)
	}
	if err := installCodexHooks(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {
		t.Fatal(err)
	}
	installed, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	requireGolden(t, installed, "codex_hooks_golden.json")

	if err := installCodexHooks(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {
		t.Fatal(err)
	}
	again, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	requireGolden(t, again, "codex_hooks_golden.json")

	if _, err := removeCodexEndpointHooks(path); err != nil {
		t.Fatal(err)
	}
	removed, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	requireGolden(t, removed, "codex_hooks_removed_golden.json")
}