|
| 1 | +package agent |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +// priorOCRResult 用于「同一图片路径已 OCR 过就别重复」的短路判定(issue #146)。 |
| 6 | +func TestPriorOCRResult(t *testing.T) { |
| 7 | + convo := []ChatMessage{ |
| 8 | + {Role: "user", Content: "图在 /tmp/a.png,别识别了"}, |
| 9 | + {Role: "assistant", ToolCalls: []ToolCall{ |
| 10 | + {ID: "c1", Function: ToolCallFunc{Name: "OCR", Arguments: `{"path":"/tmp/a.png"}`}}, |
| 11 | + }}, |
| 12 | + {Role: "tool", ToolCallID: "c1", Name: "OCR", Content: "(图片中未识别到文字)"}, |
| 13 | + } |
| 14 | + |
| 15 | + t.Run("同一路径命中并回上次结果", func(t *testing.T) { |
| 16 | + prev, ok := priorOCRResult(`{"path":"/tmp/a.png"}`, convo) |
| 17 | + if !ok || prev != "(图片中未识别到文字)" { |
| 18 | + t.Fatalf("应命中并返回上次结果,got %q ok=%v", prev, ok) |
| 19 | + } |
| 20 | + }) |
| 21 | + |
| 22 | + t.Run("等价路径经Clean后仍命中", func(t *testing.T) { |
| 23 | + if _, ok := priorOCRResult(`{"path":"/tmp/./a.png"}`, convo); !ok { |
| 24 | + t.Fatalf("等价路径应命中") |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("不同路径不命中", func(t *testing.T) { |
| 29 | + if _, ok := priorOCRResult(`{"path":"/tmp/b.png"}`, convo); ok { |
| 30 | + t.Fatalf("不同路径不应命中") |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("无历史OCR不命中", func(t *testing.T) { |
| 35 | + fresh := []ChatMessage{{Role: "user", Content: "hi"}} |
| 36 | + if _, ok := priorOCRResult(`{"path":"/tmp/a.png"}`, fresh); ok { |
| 37 | + t.Fatalf("无先前 OCR 不应命中") |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("坏参数不panic不命中", func(t *testing.T) { |
| 42 | + if _, ok := priorOCRResult(`not json`, convo); ok { |
| 43 | + t.Fatalf("坏参数不应命中") |
| 44 | + } |
| 45 | + }) |
| 46 | +} |
0 commit comments