Files
ARTEX/agent/knowledge_tool_test.go
T
BaaRai 9cd000dbd0 feat(agent): add versioned knowledge pack and knowledge_lookup tool
Embed MITRE ATT&CK, OWASP WSTG and MITRE CWE ids, names, categories and
capped descriptions generated by scripts/knowledge/build.py. The pack records
the source URL, license, retrieval date and SHA-256; the license texts and
attribution live in knowledge/NOTICE and knowledge/licenses.

Expose a read-only knowledge_lookup tool to the planner and the workers so an
agent can resolve an entry by id or keyword and cite the id in findings and
facts. The data files keep their own licenses; the repository's AGPL-3.0
covers the code.

Refs #521.
2026-11-24 01:13:15 +01:00

91 lines
2.5 KiB
Go

package agent
import (
"encoding/json"
"strings"
"testing"
)
func TestKnowledgeLookupRunExactID(t *testing.T) {
res, err := knowledgeLookupRun(t.Context(), json.RawMessage(`{"source":"attack","query":"T1190"}`))
if err != nil {
t.Fatalf("run: %v", err)
}
if res.IsError {
t.Fatalf("unexpected tool error: %s", res.Flatten())
}
out := res.Flatten()
for _, want := range []string{`"T1190"`, "Exploit Public-Facing Application", "attack.mitre.org"} {
if !strings.Contains(out, want) {
t.Errorf("output missing %q: %s", want, out)
}
}
}
func TestKnowledgeLookupRunKeyword(t *testing.T) {
res, err := knowledgeLookupRun(t.Context(), json.RawMessage(`{"source":"wstg","query":"default credentials","limit":3}`))
if err != nil {
t.Fatalf("run: %v", err)
}
if res.IsError {
t.Fatalf("unexpected tool error: %s", res.Flatten())
}
if !strings.Contains(res.Flatten(), "WSTG-ATHN-01") {
t.Errorf("output does not contain WSTG-ATHN-01: %s", res.Flatten())
}
}
func TestKnowledgeLookupRunLimitIsClamped(t *testing.T) {
res, err := knowledgeLookupRun(t.Context(), json.RawMessage(`{"source":"cwe","query":"data","limit":100}`))
if err != nil {
t.Fatalf("run: %v", err)
}
if res.IsError {
t.Fatalf("unexpected tool error: %s", res.Flatten())
}
var out struct {
Matches []map[string]any `json:"matches"`
Total int `json:"total"`
}
if err := json.Unmarshal([]byte(res.Flatten()), &out); err != nil {
t.Fatalf("decode: %v", err)
}
if len(out.Matches) > 20 {
t.Fatalf("matches = %d, want at most 20", len(out.Matches))
}
if out.Total <= len(out.Matches) {
t.Fatalf("total = %d, matches = %d", out.Total, len(out.Matches))
}
}
func TestKnowledgeLookupRunErrors(t *testing.T) {
cases := []struct {
name, in, want string
}{
{"unknown source", `{"source":"nope","query":"x"}`, "unknown knowledge source"},
{"empty query", `{"source":"attack","query":" "}`, "query is required"},
{"invalid json", `{`, "invalid input"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
res, err := knowledgeLookupRun(t.Context(), json.RawMessage(c.in))
if err != nil {
t.Fatalf("run: %v", err)
}
if !res.IsError || !strings.Contains(res.Flatten(), c.want) {
t.Fatalf("result = %q (isError=%v), want error containing %q", res.Flatten(), res.IsError, c.want)
}
})
}
}
func TestKnowledgeLookupToolIsReadOnly(t *testing.T) {
tool := (&ToolSet{}).knowledgeLookup()
if tool.Name() != "knowledge_lookup" {
t.Fatalf("name = %q", tool.Name())
}
if !tool.IsReadOnly(nil) {
t.Fatal("tool is not read-only")
}
}