/ internal / agent / approval_cache_test.go
approval_cache_test.go
 1  package agent
 2  
 3  import "testing"
 4  
 5  func TestApprovalCache_EmptyCache(t *testing.T) {
 6  	c := NewApprovalCache()
 7  	if c.WasApproved("bash", `{"command":"rm foo.txt"}`) {
 8  		t.Fatal("empty cache should not report any approvals")
 9  	}
10  }
11  
12  func TestApprovalCache_SameToolSameArgs(t *testing.T) {
13  	c := NewApprovalCache()
14  	args := `{"command":"rm foo.txt"}`
15  	c.RecordApproval("bash", args)
16  	if !c.WasApproved("bash", args) {
17  		t.Fatal("same tool+args should be approved after recording")
18  	}
19  }
20  
21  func TestApprovalCache_SameToolDifferentArgs(t *testing.T) {
22  	c := NewApprovalCache()
23  	c.RecordApproval("bash", `{"command":"rm foo.txt"}`)
24  	if c.WasApproved("bash", `{"command":"rm bar.txt"}`) {
25  		t.Fatal("same tool with different args should NOT be auto-approved")
26  	}
27  }
28  
29  func TestApprovalCache_DifferentToolSameArgs(t *testing.T) {
30  	c := NewApprovalCache()
31  	args := `{"command":"rm foo.txt"}`
32  	c.RecordApproval("bash", args)
33  	if c.WasApproved("file_write", args) {
34  		t.Fatal("different tool with same args should NOT be auto-approved")
35  	}
36  }
37  
38  func TestApprovalCache_MultipleApprovals(t *testing.T) {
39  	c := NewApprovalCache()
40  	c.RecordApproval("bash", `{"command":"rm foo.txt"}`)
41  	c.RecordApproval("bash", `{"command":"rm bar.txt"}`)
42  	c.RecordApproval("file_write", `{"path":"/tmp/x"}`)
43  
44  	if !c.WasApproved("bash", `{"command":"rm foo.txt"}`) {
45  		t.Fatal("first approval should still be cached")
46  	}
47  	if !c.WasApproved("bash", `{"command":"rm bar.txt"}`) {
48  		t.Fatal("second approval should be cached")
49  	}
50  	if !c.WasApproved("file_write", `{"path":"/tmp/x"}`) {
51  		t.Fatal("third approval should be cached")
52  	}
53  	if c.WasApproved("bash", `{"command":"rm baz.txt"}`) {
54  		t.Fatal("unapproved combination should not be cached")
55  	}
56  }