| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/provider" |
| 8 | ) |
| 9 | |
| 10 | func TestCaptureShapeNormalizesToolSchemaOrder(t *testing.T) { |
| 11 | schemas := []provider.ToolSchema{ |
| 12 | { |
| 13 | Name: "write_file", |
| 14 | Description: "write a file", |
| 15 | Parameters: json.RawMessage(`{"type":"object","properties":{"path":{"type":"string"}}}`), |
| 16 | }, |
| 17 | { |
| 18 | Name: "read_file", |
| 19 | Description: "read a file", |
| 20 | Parameters: json.RawMessage(`{"type":"object","properties":{"path":{"type":"string"}}}`), |
| 21 | }, |
| 22 | } |
| 23 | reordered := []provider.ToolSchema{schemas[1], schemas[0]} |
| 24 | |
| 25 | first := CaptureShape("system", schemas, 1) |
| 26 | second := CaptureShape("system", reordered, 1) |
| 27 | |
| 28 | if first.ToolsHash != second.ToolsHash { |
| 29 | t.Fatalf("ToolsHash should be stable across schema order: %q != %q", first.ToolsHash, second.ToolsHash) |
| 30 | } |
| 31 | if first.PrefixHash != second.PrefixHash { |
| 32 | t.Fatalf("PrefixHash should be stable across schema order: %q != %q", first.PrefixHash, second.PrefixHash) |
| 33 | } |
| 34 | if schemas[0].Name != "write_file" || schemas[1].Name != "read_file" { |
| 35 | t.Fatalf("CaptureShape mutated caller schema order: got [%s %s]", schemas[0].Name, schemas[1].Name) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestCompareShapeIgnoresBareLogRewriteVersionDrift(t *testing.T) { |
| 40 | before := CaptureShape("system", nil, 5) |
| 41 | // Simulates AddDecisionReceipt/UpdateToolCallPreview/UpdateToolCallResolution/ |
| 42 | // ReplaceLocalMetadata bumping rewriteVersion alone, with no drained content |
| 43 | // reason: none of those touch provider-visible bytes, so this must not be |
| 44 | // reported as a cache-prefix change. |
| 45 | after := CaptureShape("system", nil, 9) |
| 46 | if diag := CompareShape(before, after, nil, nil); diag.PrefixChanged { |
| 47 | t.Fatalf("LogRewriteVersion drift with no drained reason must not report a change: %+v", diag) |
| 48 | } |
| 49 | |
| 50 | if diag := CompareShape(before, after, nil, []string{"compact_auto"}); !diag.PrefixChanged || len(diag.PrefixChangeReasons) != 1 || diag.PrefixChangeReasons[0] != "compact_auto" { |
| 51 | t.Fatalf("a real drained reason must be reported: %+v", diag) |
| 52 | } |
| 53 | } |
| 54 |