Skip to content

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

test(hooks): one fixture reader for every runtime

Kyle Elliott4350e565a306
diff --git a/cli/beacon-hooks/cmd/cline_event_test.go b/cli/beacon-hooks/cmd/cline_event_test.go
index babf124e..15a8b260 100644
--- a/cli/beacon-hooks/cmd/cline_event_test.go
+++ b/cli/beacon-hooks/cmd/cline_event_test.go
@@ -1,7 +1,6 @@
package cmd
import (
- "encoding/json"
"os"
"path/filepath"
"strings"
@@ -42,19 +41,6 @@ func clineEventWithAction(t *testing.T, logPath, action string) map[string]inter
return nil
}
-func readClineFixture(t *testing.T, name string) map[string]interface{} {
- t.Helper()
- data, err := os.ReadFile(filepath.Join("..", "testdata", "cline", name))
- if err != nil {
- t.Fatalf("read cline fixture %s: %v", name, err)
- }
- var payload map[string]interface{}
- if err := json.Unmarshal(data, &payload); err != nil {
- t.Fatalf("decode cline fixture %s: %v", name, err)
- }
- return payload
-}
-
func nested(t *testing.T, event map[string]interface{}, keys ...string) map[string]interface{} {
t.Helper()
current := event
@@ -222,7 +208,7 @@ func TestClineEventResolvesWorkspaceRelativePaths(t *testing.T) {
func TestClineEventWriteToFileRecordsDiff(t *testing.T) {
logPath := clineTestLog(t)
- runHookWithInput(t, runClineEvent, readClineFixture(t, "tool_after_write.json"))
+ runHookWithInput(t, runClineEvent, readHookFixture(t, "cline", "tool_after_write.json"))
event := clineEventWithAction(t, logPath, "file.modified")
file := nested(t, event, "file")
@@ -280,7 +266,7 @@ func TestClineEventReplaceInFilePassesTheDiffThrough(t *testing.T) {
func TestClineEventExecuteCommandRecordsCommand(t *testing.T) {
logPath := clineTestLog(t)
- runHookWithInput(t, runClineEvent, readClineFixture(t, "tool_after_command.json"))
+ runHookWithInput(t, runClineEvent, readHookFixture(t, "cline", "tool_after_command.json"))
event := clineEventWithAction(t, logPath, "command.executed")
command := nested(t, event, "command")
@@ -381,7 +367,7 @@ func TestClineEventFileActionWithoutAPathBecomesToolCompleted(t *testing.T) {
func TestClineEventTaskEndRecordsUsage(t *testing.T) {
logPath := clineTestLog(t)
- runHookWithInput(t, runClineEvent, readClineFixture(t, "run_end_usage.json"))
+ runHookWithInput(t, runClineEvent, readHookFixture(t, "cline", "run_end_usage.json"))
event := clineEventWithAction(t, logPath, "session.ended")
usage := nested(t, event, "gen_ai", "usage")
diff --git a/cli/beacon-hooks/cmd/opencode_event_test.go b/cli/beacon-hooks/cmd/opencode_event_test.go
index a4445a4f..be8caf03 100644
--- a/cli/beacon-hooks/cmd/opencode_event_test.go
+++ b/cli/beacon-hooks/cmd/opencode_event_test.go
@@ -1,7 +1,6 @@
package cmd
import (
- "encoding/json"
"os"
"path/filepath"
"regexp"
@@ -61,7 +60,7 @@ func TestOpenCodeEventFixtureRecordsPrompt(t *testing.T) {
t.Setenv("BEACON_ENDPOINT_LOG", logPath)
t.Setenv("BEACON_CONTENT_RETENTION", "full")
- input := readOpenCodeFixture(t, "chat_message.json")
+ input := readHookFixture(t, "opencode", "chat_message.json")
runHookWithInput(t, runOpenCodeEvent, input)
event := lastEndpointEvent(t, logPath)
if action := event["event"].(map[string]interface{})["action"]; action != "prompt.submitted" {
@@ -83,7 +82,7 @@ func TestOpenCodeFixtureContracts(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
- input := readOpenCodeFixture(t, tt.file)
+ input := readHookFixture(t, "opencode", tt.file)
sessionID := resolveSessionID(input, "opencode")
action, category, _, _, fields := opencodeEndpointEvent(input, sessionID)
if action != tt.action || category != tt.category {
@@ -592,19 +591,6 @@ func forwardedEventsFromPluginSource(t *testing.T, source string) []string {
return events
}
-func readOpenCodeFixture(t *testing.T, name string) map[string]interface{} {
- t.Helper()
- data, err := os.ReadFile(filepath.Join("..", "testdata", "opencode", name))
- if err != nil {
- t.Fatalf("read opencode fixture %s: %v", name, err)
- }
- var payload map[string]interface{}
- if err := json.Unmarshal(data, &payload); err != nil {
- t.Fatalf("decode opencode fixture %s: %v", name, err)
- }
- return payload
-}
-
// OpenCode stores tokens.output with reasoning already subtracted (getUsage in
// packages/opencode/src/session/session.ts sets output to outputTokens minus
// reasoningTokens). Beacon's gen_ai.usage counts reasoning inside
diff --git a/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go b/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go
index ab1ce199..de078537 100644
--- a/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go
+++ b/cli/beacon-hooks/cmd/post_tool_claude_mcp_test.go
@@ -5,8 +5,6 @@ import (
"encoding/base64"
"encoding/hex"
"encoding/json"
- "os"
- "path/filepath"
"reflect"
"strings"
"testing"
@@ -14,25 +12,12 @@ import (
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
-func readClaudeFixture(t *testing.T, name string) map[string]interface{} {
- t.Helper()
- data, err := os.ReadFile(filepath.Join("..", "testdata", "claude", name))
- if err != nil {
- t.Fatalf("read claude fixture %s: %v", name, err)
- }
- var payload map[string]interface{}
- if err := json.Unmarshal(data, &payload); err != nil {
- t.Fatalf("decode claude fixture %s: %v", name, err)
- }
- return payload
-}
-
// The result of an MCP tool, as Claude Code 2.1.291 hands it to PostToolUse: a bare list of
// content blocks, with the server's image as an Anthropic block and everything else as text. The
// live event keeps every block in order, records the image's size and digest instead of its bytes,
// and its result text is exactly the text blocks the model read.
func TestClaudeMCPPostToolRecordsCapturedContentBlocks(t *testing.T) {
- payload := readClaudeFixture(t, "post_tool_use_mcp_media.json")
+ payload := readHookFixture(t, "claude", "post_tool_use_mcp_media.json")
sent := payload["tool_response"].([]interface{})
var texts []string
var png []byte
@@ -80,7 +65,7 @@ func TestClaudeMCPPostToolRecordsCapturedContentBlocks(t *testing.T) {
// A tool that returned no content blocks reaches the hook as an empty list, and is recorded with
// no result rather than an empty one.
func TestClaudeMCPPostToolWithNoContentRecordsNoResult(t *testing.T) {
- _, events := runClaudePostTool(t, readClaudeFixture(t, "post_tool_use_mcp_empty.json"))
+ _, events := runClaudePostTool(t, readHookFixture(t, "claude", "post_tool_use_mcp_empty.json"))
if len(events) != 1 {
t.Fatalf("events = %#v, want one", events)
}
@@ -89,4 +74,3 @@ func TestClaudeMCPPostToolWithNoContentRecordsNoResult(t *testing.T) {
t.Errorf("gen_ai.tool.call.result = %#v, want none", result)
}
}
-
diff --git a/cli/beacon-hooks/cmd/pre_tool_test.go b/cli/beacon-hooks/cmd/pre_tool_test.go
index 4d1b8d5a..77505eb0 100644
--- a/cli/beacon-hooks/cmd/pre_tool_test.go
+++ b/cli/beacon-hooks/cmd/pre_tool_test.go
@@ -996,6 +996,20 @@ func setupHookConfigDirs(t *testing.T) {
})
}
+// readHookFixture decodes a runtime's hook payload fixture from cli/beacon-hooks/testdata/<runtime>.
+func readHookFixture(t *testing.T, runtime, name string) map[string]interface{} {
+ t.Helper()
+ data, err := os.ReadFile(filepath.Join("..", "testdata", runtime, name))
+ if err != nil {
+ t.Fatalf("read %s fixture %s: %v", runtime, name, err)
+ }
+ var payload map[string]interface{}
+ if err := json.Unmarshal(data, &payload); err != nil {
+ t.Fatalf("decode %s fixture %s: %v", runtime, name, err)
+ }
+ return payload
+}
+
func runHookWithInput(t *testing.T, run func(cmd *cobra.Command, args []string), input map[string]interface{}) map[string]interface{} {
t.Helper()
stdinR, stdinW, err := os.Pipe()
diff --git a/cli/beacon-hooks/cmd/qwen_event_test.go b/cli/beacon-hooks/cmd/qwen_event_test.go
index 58d545a5..3676e34a 100644
--- a/cli/beacon-hooks/cmd/qwen_event_test.go
+++ b/cli/beacon-hooks/cmd/qwen_event_test.go
@@ -1,27 +1,12 @@
package cmd
import (
- "encoding/json"
"fmt"
- "os"
"path/filepath"
"strings"
"testing"
)
-func readQwenFixture(t *testing.T, name string) map[string]interface{} {
- t.Helper()
- data, err := os.ReadFile(filepath.Join("..", "testdata", "qwen", name))
- if err != nil {
- t.Fatalf("read qwen fixture %s: %v", name, err)
- }
- var payload map[string]interface{}
- if err := json.Unmarshal(data, &payload); err != nil {
- t.Fatalf("decode qwen fixture %s: %v", name, err)
- }
- return payload
-}
-
// setupQwenHook wires a hook run for --platform qwen against a fresh runtime log.
func setupQwenHook(t *testing.T) string {
t.Helper()
@@ -133,7 +118,7 @@ func TestQwenTaxonomyIsScopedToTheQwenPlatform(t *testing.T) {
func TestQwenFailedToolIsNotRecordedAsASuccessfulEdit(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_failure.json"))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", "post_tool_failure.json"))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "tool.failed" {
@@ -153,7 +138,7 @@ func TestQwenFailedToolIsNotRecordedAsASuccessfulEdit(t *testing.T) {
func TestQwenInterruptedToolIsNotRecordedAsASuccessfulEdit(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_interrupt.json"))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", "post_tool_interrupt.json"))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "tool.failed" {
@@ -169,7 +154,7 @@ func TestQwenInterruptedToolIsNotRecordedAsASuccessfulEdit(t *testing.T) {
func TestQwenShellCommandIsRecordedAsACommand(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_shell.json"))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", "post_tool_shell.json"))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "command.executed" {
@@ -195,7 +180,7 @@ func TestQwenShellCommandIsRecordedAsACommand(t *testing.T) {
func TestQwenWriteFileRecordsPathOperationAndDiff(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_write_file.json"))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", "post_tool_write_file.json"))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "file.modified" {
@@ -255,7 +240,7 @@ func TestQwenPreToolDistinguishesAWholeFileWriteFromAnEdit(t *testing.T) {
func TestQwenEditRecordsBothSidesOfTheReplacement(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_edit.json"))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", "post_tool_edit.json"))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "file.modified" {
@@ -277,7 +262,7 @@ func TestQwenEditRecordsBothSidesOfTheReplacement(t *testing.T) {
func TestQwenLegacyReplaceIdBehavesLikeEdit(t *testing.T) {
logPath := setupQwenHook(t)
- payload := readQwenFixture(t, "post_tool_edit.json")
+ payload := readHookFixture(t, "qwen", "post_tool_edit.json")
payload["tool_name"] = "replace"
runHookWithInput(t, runPostTool, payload)
@@ -333,7 +318,7 @@ func TestQwenReadFileResolvesTheAbsolutePathParameter(t *testing.T) {
func TestQwenRawPayloadPreservesFieldsWithNoSchemaHome(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runPreTool, readQwenFixture(t, "pre_tool_shell.json"))
+ runHookWithInput(t, runPreTool, readHookFixture(t, "qwen", "pre_tool_shell.json"))
event := lastEndpointEvent(t, logPath)
raw, ok := event["raw"].(map[string]interface{})
@@ -371,7 +356,7 @@ func TestQwenRawPayloadPreservesFieldsWithNoSchemaHome(t *testing.T) {
func TestQwenStopContextFieldsAreNotNormalizedIntoTokenUsage(t *testing.T) {
logPath := setupQwenHook(t)
- input := readQwenFixture(t, "stop.json")
+ input := readHookFixture(t, "qwen", "stop.json")
sessionID, _ := resolveSessionIDWithTranscript(input, platformFlag)
if sessionID == "" {
t.Fatal("stop fixture has no session id; the mapping under test would not run")
@@ -413,7 +398,7 @@ func TestQwenStopContextFieldsAreNotNormalizedIntoTokenUsage(t *testing.T) {
func TestQwenStopContextIsNormalizedIntoGenAIContext(t *testing.T) {
logPath := setupQwenHook(t)
- input := readQwenFixture(t, "stop.json")
+ input := readHookFixture(t, "qwen", "stop.json")
sessionID, _ := resolveSessionIDWithTranscript(input, platformFlag)
logger := newHookLogger("stop", platformFlag, sessionID)
emitHookEvent(logger, "tool.completed", "tool", "info", "Agent response completed", input, sessionFields(sessionID, input))
@@ -452,7 +437,7 @@ func TestQwenStopContextIsNormalizedIntoGenAIContext(t *testing.T) {
func TestQwenSessionLifecycleIsRecorded(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runSessionStart, readQwenFixture(t, "session_start.json"))
+ runHookWithInput(t, runSessionStart, readHookFixture(t, "qwen", "session_start.json"))
start := lastEndpointEvent(t, logPath)
if got := qwenAction(t, start); got != "session.started" {
t.Fatalf("event.action = %q, want session.started", got)
@@ -461,7 +446,7 @@ func TestQwenSessionLifecycleIsRecorded(t *testing.T) {
t.Fatalf("model = %q, want the session model", got)
}
- runHookWithInput(t, runPromptSubmit, readQwenFixture(t, "user_prompt_submit.json"))
+ runHookWithInput(t, runPromptSubmit, readHookFixture(t, "qwen", "user_prompt_submit.json"))
prompt := lastEndpointEvent(t, logPath)
if got := qwenAction(t, prompt); got != "prompt.submitted" {
t.Fatalf("event.action = %q, want prompt.submitted", got)
@@ -502,7 +487,7 @@ func TestQwenRawPayloadSurvivesTheFileEditPath(t *testing.T) {
for _, fixture := range []string{"post_tool_write_file.json", "post_tool_edit.json"} {
t.Run(fixture, func(t *testing.T) {
- runHookWithInput(t, runPostTool, readQwenFixture(t, fixture))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", fixture))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "file.modified" {
t.Fatalf("event.action = %q, want file.modified", got)
@@ -543,7 +528,7 @@ func TestQwenEveryFailureSignalIsClassifiedAsAFailure(t *testing.T) {
} {
t.Run(name, func(t *testing.T) {
logPath := setupQwenHook(t)
- payload := readQwenFixture(t, "post_tool_write_file.json")
+ payload := readHookFixture(t, "qwen", "post_tool_write_file.json")
for key, value := range overrides {
payload[key] = value
}
@@ -570,7 +555,7 @@ func TestQwenEveryFailureSignalIsClassifiedAsAFailure(t *testing.T) {
// The control: the same payload with no failure signal is still a successful edit, so the
// guard above cannot be satisfied by classifying everything as a failure.
logPath := setupQwenHook(t)
- runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_write_file.json"))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", "post_tool_write_file.json"))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "file.modified" {
t.Fatalf("a clean write was classified %q, want file.modified", got)
@@ -635,7 +620,7 @@ func TestQwenNotebookEditRecordsThePathWithoutClaimingADiff(t *testing.T) {
func TestQwenStopEventCarriesNoModel(t *testing.T) {
logPath := setupQwenHook(t)
- input := readQwenFixture(t, "stop.json")
+ input := readHookFixture(t, "qwen", "stop.json")
if _, ok := input["model"]; ok {
t.Fatalf("stop fixture now has a model; the reader-side session lookup may no longer be needed: %#v", input)
}
diff --git a/cli/beacon-hooks/cmd/tool_call_id_test.go b/cli/beacon-hooks/cmd/tool_call_id_test.go
index 260c7594..3d3edb0f 100644
--- a/cli/beacon-hooks/cmd/tool_call_id_test.go
+++ b/cli/beacon-hooks/cmd/tool_call_id_test.go
@@ -214,7 +214,7 @@ func TestHookEventsCarryAnEventID(t *testing.T) {
func TestQwenFileEditCarriesBothTheCallIDAndTheRawPayload(t *testing.T) {
logPath := setupQwenHook(t)
- runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_write_file.json"))
+ runHookWithInput(t, runPostTool, readHookFixture(t, "qwen", "post_tool_write_file.json"))
event := lastEndpointEvent(t, logPath)
if got := qwenAction(t, event); got != "file.modified" {