| 1 | package evidence |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestEvidenceConsumesSharedCommandEffectMatrix(t *testing.T) { |
| 10 | raw, err := os.ReadFile("../shellsafe/testdata/command_effects.json") |
| 11 | if err != nil { |
| 12 | t.Fatal(err) |
| 13 | } |
| 14 | var cases []struct { |
| 15 | Name, Command string |
| 16 | Certainty string |
| 17 | TaskPolicyBlocked, ContentMutation bool |
| 18 | } |
| 19 | if err := json.Unmarshal(raw, &cases); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | for _, tc := range cases { |
| 23 | t.Run(tc.Name, func(t *testing.T) { |
| 24 | args, _ := json.Marshal(map[string]string{"command": tc.Command}) |
| 25 | got := ClassifyToolCall("bash", args, false) |
| 26 | if got.Known != (tc.Certainty == "known") || got.StateMutation != tc.TaskPolicyBlocked || got.ContentMutation != tc.ContentMutation { |
| 27 | t.Fatalf("ClassifyToolCall(%q) = %+v, matrix=%+v", tc.Command, got, tc) |
| 28 | } |
| 29 | }) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestClassifyToolCallSeparatesMutationDomains(t *testing.T) { |
| 34 | tests := []struct { |
| 35 | name, tool, args string |
| 36 | readOnly bool |
| 37 | want ToolEffects |
| 38 | }{ |
| 39 | {"branch listing", "bash", `{"command":"git branch -a"}`, false, ToolEffects{Known: true}}, |
| 40 | {"tag creation", "bash", `{"command":"git tag v1.2.3"}`, true, ToolEffects{Known: true, StateMutation: true, WorkspaceMutation: true, RepositoryMutation: true}}, |
| 41 | {"pure commit", "bash", `{"command":"git commit -q -m checkpoint"}`, false, ToolEffects{Known: true, StateMutation: true, WorkspaceMutation: true, RepositoryMutation: true}}, |
| 42 | {"commit all", "bash", `{"command":"git commit -am checkpoint"}`, false, ToolEffects{Known: true, StateMutation: true, WorkspaceMutation: true, ContentMutation: true, RepositoryMutation: true}}, |
| 43 | {"host clock", "bash", `{"command":"date --set tomorrow"}`, false, ToolEffects{Known: true, StateMutation: true}}, |
| 44 | {"audit fix", "bash", `{"command":"npm audit fix"}`, false, ToolEffects{Known: true, StateMutation: true, WorkspaceMutation: true, ContentMutation: true}}, |
| 45 | {"verification", "bash", `{"command":"go test ./..."}`, false, ToolEffects{Known: true}}, |
| 46 | {"env prefixed test", "bash", `{"command":"GOROOT=/x go test ./..."}`, false, ToolEffects{Known: true}}, |
| 47 | {"unknown shell", "bash", `{"command":"custom-tool --run"}`, false, ToolEffects{StateMutation: true, WorkspaceMutation: true, ContentMutation: true}}, |
| 48 | {"trusted reader", "read_file", `{}`, true, ToolEffects{Known: true}}, |
| 49 | {"fleet meta tool", "fleet", `{}`, false, ToolEffects{Known: true}}, |
| 50 | {"session title", "set_session_title", `{"title":"Current task"}`, false, ToolEffects{Known: true, StateMutation: true, Reason: "host session metadata write"}}, |
| 51 | {"background kill", "kill_shell", `{"job_id":"task-1"}`, false, ToolEffects{Known: true, StateMutation: true}}, |
| 52 | {"generic writer", "edit_file", `{}`, false, ToolEffects{Known: true, StateMutation: true, WorkspaceMutation: true, ContentMutation: true}}, |
| 53 | } |
| 54 | for _, tt := range tests { |
| 55 | t.Run(tt.name, func(t *testing.T) { |
| 56 | got := ClassifyToolCall(tt.tool, json.RawMessage(tt.args), tt.readOnly) |
| 57 | got.Reason, tt.want.Reason = "", "" |
| 58 | if got != tt.want { |
| 59 | t.Fatalf("ClassifyToolCall(%q, %s) = %+v, want %+v", tt.tool, tt.args, got, tt.want) |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestReceiptMutationTracksContentNotRepositoryCement(t *testing.T) { |
| 66 | pure := ReceiptFromToolCall("bash", json.RawMessage(`{"command":"git commit -m checkpoint"}`), true, false) |
| 67 | all := ReceiptFromToolCall("bash", json.RawMessage(`{"command":"git commit -am checkpoint"}`), true, false) |
| 68 | if pure.Mutation || !all.Mutation { |
| 69 | t.Fatalf("commit receipt domains: pure=%+v all=%+v", pure, all) |
| 70 | } |
| 71 | } |
| 72 |