|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// TestParseHookFilePathsAcceptsHostPayloadShapes pins the payload shapes the |
| 12 | +// hooks actually receive. Claude Code nests the path under tool_input for |
| 13 | +// Edit/Write/NotebookEdit; only a legacy shape puts it at the top level. Missing |
| 14 | +// the nested form silently disables both agent-edit provenance and the |
| 15 | +// post-edit blast-radius report, because the parser returns no paths and the |
| 16 | +// hook returns early. |
| 17 | +func TestParseHookFilePathsAcceptsHostPayloadShapes(t *testing.T) { |
| 18 | + target := filepath.Join("cmd", "doctor.go") |
| 19 | + quoted := strings.ReplaceAll(target, `\`, `\\`) |
| 20 | + |
| 21 | + for _, tt := range []struct { |
| 22 | + name string |
| 23 | + input string |
| 24 | + want []string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "legacy top-level file_path", |
| 28 | + input: `{"session_id":"s1","file_path":"` + quoted + `"}`, |
| 29 | + want: []string{target}, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Claude Edit tool_input.file_path", |
| 33 | + input: `{"session_id":"s1","tool_name":"Edit","tool_input":{"file_path":"` + quoted + `"}}`, |
| 34 | + want: []string{target}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "Claude Write tool_input.file_path", |
| 38 | + input: `{"session_id":"s1","tool_name":"Write","tool_input":{"file_path":"` + quoted + `","content":"x"}}`, |
| 39 | + want: []string{target}, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Claude NotebookEdit tool_input.notebook_path", |
| 43 | + input: `{"session_id":"s1","tool_name":"NotebookEdit","tool_input":{"notebook_path":"` + quoted + `"}}`, |
| 44 | + want: []string{target}, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Codex apply_patch tool_input.command", |
| 48 | + input: `{"session_id":"s1","tool_input":{"command":"*** Update File: ` + quoted + `\n@@\n-a\n+b\n"}}`, |
| 49 | + want: []string{target}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "no path present", |
| 53 | + input: `{"session_id":"s1","tool_name":"Bash","tool_input":{"command":"go test ./..."}}`, |
| 54 | + want: nil, |
| 55 | + }, |
| 56 | + } { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + got := parseHookFilePaths([]byte(tt.input)) |
| 59 | + if len(got) != len(tt.want) { |
| 60 | + t.Fatalf("parseHookFilePaths() = %v, want %v", got, tt.want) |
| 61 | + } |
| 62 | + for i := range tt.want { |
| 63 | + if got[i] != tt.want[i] { |
| 64 | + t.Fatalf("parseHookFilePaths()[%d] = %q, want %q", i, got[i], tt.want[i]) |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// TestHookPostEditRecordsAgentEditForNestedPayload covers the whole path rather |
| 72 | +// than just the parser: the parser was arguably fine, it simply never saw the |
| 73 | +// shape it needed, so the regression has to be pinned end to end. |
| 74 | +func TestHookPostEditRecordsAgentEditForNestedPayload(t *testing.T) { |
| 75 | + root := t.TempDir() |
| 76 | + target := filepath.Join(root, "main.go") |
| 77 | + if err := os.WriteFile(target, []byte("package main\n"), 0o644); err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + |
| 81 | + quoted := strings.ReplaceAll(target, `\`, `\\`) |
| 82 | + payload := `{"session_id":"nested-session","tool_name":"Edit","tool_input":{"file_path":"` + quoted + `"}}` |
| 83 | + |
| 84 | + withStdinInput(t, payload, func() { |
| 85 | + if err := hookPostEdit(root); err != nil { |
| 86 | + t.Fatalf("hookPostEdit() = %v", err) |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + edits := loadAgentEdits(root, time.Time{}) |
| 91 | + if !edits.paths[normalizeAgentEditPath(root, target)] { |
| 92 | + t.Fatalf("expected agent edit recorded for %s, got %v", target, edits.paths) |
| 93 | + } |
| 94 | +} |
0 commit comments