| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package loop |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "reflect" |
| 6 | "sync" |
| 7 | "testing" |
| 8 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 9 | "sketch.dev/llm" |
| 10 | "sketch.dev/llm/conversation" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 11 | ) |
| 12 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 13 | // MockConvo is a custom mock for conversation.Convo interface |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 14 | type MockConvo struct { |
| 15 | mu sync.Mutex |
| 16 | t *testing.T |
| 17 | |
| 18 | // Maps method name to a list of calls with arguments and return values |
| 19 | calls map[string][]*mockCall |
| 20 | // Maps method name to expected calls |
| 21 | expectations map[string][]*mockExpectation |
| 22 | } |
| 23 | |
| 24 | type mockCall struct { |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 25 | args []any |
| 26 | result []any |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | type mockExpectation struct { |
| 30 | until chan any |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 31 | args []any |
| 32 | result []any |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // Return sets up return values for an expectation |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 36 | func (e *mockExpectation) Return(values ...any) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 37 | e.result = values |
| 38 | } |
| 39 | |
| 40 | // Return sets up return values for an expectation |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 41 | func (e *mockExpectation) BlockAndReturn(until chan any, values ...any) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 42 | e.until = until |
| 43 | e.result = values |
| 44 | } |
| 45 | |
| 46 | // NewMockConvo creates a new mock Convo |
| 47 | func NewMockConvo(t *testing.T) *MockConvo { |
| 48 | return &MockConvo{ |
| 49 | t: t, |
| 50 | mu: sync.Mutex{}, |
| 51 | calls: make(map[string][]*mockCall), |
| 52 | expectations: make(map[string][]*mockExpectation), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // ExpectCall sets up an expectation for a method call |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 57 | func (m *MockConvo) ExpectCall(method string, args ...any) *mockExpectation { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 58 | m.mu.Lock() |
| 59 | defer m.mu.Unlock() |
| 60 | expectation := &mockExpectation{args: args} |
| 61 | if _, ok := m.expectations[method]; !ok { |
| 62 | m.expectations[method] = []*mockExpectation{} |
| 63 | } |
| 64 | m.expectations[method] = append(m.expectations[method], expectation) |
| 65 | return expectation |
| 66 | } |
| 67 | |
| 68 | // findMatchingExpectation finds a matching expectation for a method call |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 69 | func (m *MockConvo) findMatchingExpectation(method string, args ...any) (*mockExpectation, bool) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 70 | m.mu.Lock() |
| 71 | defer m.mu.Unlock() |
| 72 | expectations, ok := m.expectations[method] |
| 73 | if !ok { |
| 74 | return nil, false |
| 75 | } |
| 76 | |
| 77 | for i, exp := range expectations { |
| 78 | if matchArgs(exp.args, args) { |
| 79 | if exp.until != nil { |
| 80 | <-exp.until |
| 81 | } |
| 82 | // Remove the matched expectation |
| 83 | m.expectations[method] = append(expectations[:i], expectations[i+1:]...) |
| 84 | return exp, true |
| 85 | } |
| 86 | } |
| 87 | return nil, false |
| 88 | } |
| 89 | |
| 90 | // matchArgs checks if call arguments match expectation arguments |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 91 | func matchArgs(expected, actual []any) bool { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 92 | if len(expected) != len(actual) { |
| 93 | return false |
| 94 | } |
| 95 | |
| 96 | for i, exp := range expected { |
| 97 | // Special case: nil matches anything |
| 98 | if exp == nil { |
| 99 | continue |
| 100 | } |
| 101 | |
| 102 | // Check for equality |
| 103 | if !reflect.DeepEqual(exp, actual[i]) { |
| 104 | return false |
| 105 | } |
| 106 | } |
| 107 | return true |
| 108 | } |
| 109 | |
| 110 | // recordCall records a method call |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 111 | func (m *MockConvo) recordCall(method string, args ...any) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 112 | m.mu.Lock() |
| 113 | defer m.mu.Unlock() |
| 114 | if _, ok := m.calls[method]; !ok { |
| 115 | m.calls[method] = []*mockCall{} |
| 116 | } |
| 117 | m.calls[method] = append(m.calls[method], &mockCall{args: args}) |
| 118 | } |
| 119 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 120 | func (m *MockConvo) SendMessage(message llm.Message) (*llm.Response, error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 121 | m.recordCall("SendMessage", message) |
| 122 | exp, ok := m.findMatchingExpectation("SendMessage", message) |
| 123 | if !ok { |
| 124 | m.t.Errorf("unexpected call to SendMessage: %+v", message) |
| 125 | m.t.FailNow() |
| 126 | } |
| 127 | var retErr error |
| 128 | m.mu.Lock() |
| 129 | defer m.mu.Unlock() |
| 130 | if err, ok := exp.result[1].(error); ok { |
| 131 | retErr = err |
| 132 | } |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 133 | return exp.result[0].(*llm.Response), retErr |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 136 | func (m *MockConvo) SendUserTextMessage(message string, otherContents ...llm.Content) (*llm.Response, error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 137 | m.recordCall("SendUserTextMessage", message, otherContents) |
| 138 | exp, ok := m.findMatchingExpectation("SendUserTextMessage", message, otherContents) |
| 139 | if !ok { |
| 140 | m.t.Error("unexpected call to SendUserTextMessage") |
| 141 | m.t.FailNow() |
| 142 | } |
| 143 | var retErr error |
| 144 | m.mu.Lock() |
| 145 | defer m.mu.Unlock() |
| 146 | if err, ok := exp.result[1].(error); ok { |
| 147 | retErr = err |
| 148 | } |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 149 | return exp.result[0].(*llm.Response), retErr |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 152 | func (m *MockConvo) ToolResultContents(ctx context.Context, resp *llm.Response) ([]llm.Content, error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 153 | m.recordCall("ToolResultContents", resp) |
| 154 | exp, ok := m.findMatchingExpectation("ToolResultContents", resp) |
| 155 | if !ok { |
| 156 | m.t.Error("unexpected call to ToolResultContents") |
| 157 | m.t.FailNow() |
| 158 | } |
| 159 | m.mu.Lock() |
| 160 | defer m.mu.Unlock() |
| 161 | var retErr error |
| 162 | if err, ok := exp.result[1].(error); ok { |
| 163 | retErr = err |
| 164 | } |
| 165 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 166 | return exp.result[0].([]llm.Content), retErr |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 169 | func (m *MockConvo) ToolResultCancelContents(resp *llm.Response) ([]llm.Content, error) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 170 | m.recordCall("ToolResultCancelContents", resp) |
| 171 | exp, ok := m.findMatchingExpectation("ToolResultCancelContents", resp) |
| 172 | if !ok { |
| 173 | m.t.Error("unexpected call to ToolResultCancelContents") |
| 174 | m.t.FailNow() |
| 175 | } |
| 176 | var retErr error |
| 177 | m.mu.Lock() |
| 178 | defer m.mu.Unlock() |
| 179 | if err, ok := exp.result[1].(error); ok { |
| 180 | retErr = err |
| 181 | } |
| 182 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 183 | return exp.result[0].([]llm.Content), retErr |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 186 | func (m *MockConvo) CumulativeUsage() conversation.CumulativeUsage { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 187 | m.recordCall("CumulativeUsage") |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 188 | return conversation.CumulativeUsage{} |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | func (m *MockConvo) OverBudget() error { |
| 192 | m.recordCall("OverBudget") |
| 193 | return nil |
| 194 | } |
| 195 | |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 196 | func (m *MockConvo) GetID() string { |
| 197 | m.recordCall("GetID") |
| 198 | return "mock-conversation-id" |
| 199 | } |
| 200 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 201 | func (m *MockConvo) SubConvoWithHistory() *conversation.Convo { |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 202 | m.recordCall("SubConvoWithHistory") |
| 203 | return nil |
| 204 | } |
| 205 | |
| Josh Bleecher Snyder | 4f84ab7 | 2025-04-22 16:40:54 -0700 | [diff] [blame] | 206 | func (m *MockConvo) ResetBudget(_ conversation.Budget) { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 207 | m.recordCall("ResetBudget") |
| 208 | } |
| 209 | |
| 210 | // AssertExpectations checks that all expectations were met |
| 211 | func (m *MockConvo) AssertExpectations(t *testing.T) { |
| 212 | m.mu.Lock() |
| 213 | defer m.mu.Unlock() |
| 214 | |
| 215 | for method, expectations := range m.expectations { |
| 216 | if len(expectations) > 0 { |
| 217 | t.Errorf("not all expectations were met for method %s:", method) |
| 218 | } |
| 219 | } |
| 220 | } |