This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/beads/cmd/bd/codex_hook_test.go
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

127 lines
4.1 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func stubCodexHookPrime(t *testing.T, fn func(memoriesOnly bool) (string, error)) {
t.Helper()
orig := codexHookExecPrime
codexHookExecPrime = func(_ context.Context, memoriesOnly bool) (string, error) {
return fn(memoriesOnly)
}
t.Cleanup(func() { codexHookExecPrime = orig })
}
func TestCodexHookSessionStartInjectsPrimeContext(t *testing.T) {
stubCodexHookPrime(t, func(memoriesOnly bool) (string, error) {
if memoriesOnly {
t.Fatal("SessionStart should request full prime output")
}
return "BEADS PRIME\nbd ready --json\n", nil
})
var out bytes.Buffer
input := `{"session_id":"s1","cwd":"/repo","hook_event_name":"SessionStart","source":"startup"}`
if err := runCodexHook(context.Background(), codexHookSessionStart, strings.NewReader(input), &out); err != nil {
t.Fatalf("runCodexHook: %v", err)
}
var got codexHookResponse
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("parse output: %v\n%s", err, out.String())
}
if got.HookSpecificOutput.HookEventName != codexHookSessionStart {
t.Fatalf("hook event = %q", got.HookSpecificOutput.HookEventName)
}
if !strings.Contains(got.HookSpecificOutput.AdditionalContext, "bd ready --json") {
t.Fatalf("expected prime output in additionalContext: %#v", got)
}
}
func TestCodexHookPreCompactWarnsWhenMemoriesUnavailable(t *testing.T) {
stubCodexHookPrime(t, func(memoriesOnly bool) (string, error) {
if !memoriesOnly {
t.Fatal("PreCompact should request memories-only prime output")
}
return "", errors.New("workspace unavailable")
})
var out bytes.Buffer
input := `{"session_id":"s1","cwd":"/repo","hook_event_name":"PreCompact","trigger":"manual"}`
if err := runCodexHook(context.Background(), codexHookPreCompact, strings.NewReader(input), &out); err != nil {
t.Fatalf("runCodexHook: %v", err)
}
var got codexHookResponse
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatalf("parse output: %v\n%s", err, out.String())
}
if !strings.Contains(got.SystemMessage, "Beads context check failed") {
t.Fatalf("expected warning systemMessage, got %#v", got)
}
}
func TestCodexHookPostCompactMarksAndUserPromptRefreshesOnce(t *testing.T) {
dir := t.TempDir()
codexHookMarkerDirOverride = dir
t.Cleanup(func() { codexHookMarkerDirOverride = "" })
calls := 0
stubCodexHookPrime(t, func(memoriesOnly bool) (string, error) {
calls++
if memoriesOnly {
t.Fatal("UserPromptSubmit refresh should request full prime output")
}
return "REFRESHED BEADS CONTEXT\n", nil
})
input := codexHookInput{SessionID: "s1", CWD: filepath.Join("repo", "sub"), HookEventName: codexHookPostCompact}
postJSON, _ := json.Marshal(input)
if err := runCodexHook(context.Background(), codexHookPostCompact, bytes.NewReader(postJSON), ioDiscard{}); err != nil {
t.Fatalf("PostCompact hook: %v", err)
}
marker := codexHookRefreshMarkerPath(input)
if _, err := os.Stat(marker); err != nil {
t.Fatalf("expected refresh marker: %v", err)
}
input.HookEventName = codexHookUserPromptSubmit
promptJSON, _ := json.Marshal(input)
var out bytes.Buffer
if err := runCodexHook(context.Background(), codexHookUserPromptSubmit, bytes.NewReader(promptJSON), &out); err != nil {
t.Fatalf("UserPromptSubmit hook: %v", err)
}
if calls != 1 {
t.Fatalf("prime calls = %d, want 1", calls)
}
if _, err := os.Stat(marker); !os.IsNotExist(err) {
t.Fatalf("expected refresh marker removed, stat err=%v", err)
}
if !strings.Contains(out.String(), "REFRESHED BEADS CONTEXT") {
t.Fatalf("expected refreshed context, got %s", out.String())
}
out.Reset()
if err := runCodexHook(context.Background(), codexHookUserPromptSubmit, bytes.NewReader(promptJSON), &out); err != nil {
t.Fatalf("second UserPromptSubmit hook: %v", err)
}
if calls != 1 {
t.Fatalf("refresh should run once, prime calls = %d", calls)
}
if out.Len() != 0 {
t.Fatalf("expected no second refresh output, got %s", out.String())
}
}
type ioDiscard struct{}
func (ioDiscard) Write(p []byte) (int, error) { return len(p), nil }