X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/6b21fcf78372995c6d8130c63f2f8aa2609bf7fe..fc0503be49438775c5d08499046852df59bd7377:/model_fake_helper_test.go?ds=inline diff --git a/model_fake_helper_test.go b/model_fake_helper_test.go index e3fb4c6..af94776 100644 --- a/model_fake_helper_test.go +++ b/model_fake_helper_test.go @@ -34,26 +34,26 @@ type ModelFake struct { reply func(http.ResponseWriter, ModelFakeCall) } -// Call returns call number n. -func (f *ModelFake) Call(n int) ModelFakeCall { - f.mutex.Lock() - defer f.mutex.Unlock() +func newModelFake(t *testing.T) *ModelFake { + t.Helper() - return f.calls[n] -} + f := &ModelFake{ + reply: func(w http.ResponseWriter, _ ModelFakeCall) { + modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("a line")}, 100, 0, 5)) + }, + } -// Calls returns the number of calls. -func (f *ModelFake) Calls() int { - f.mutex.Lock() - defer f.mutex.Unlock() + f.Server = httptest.NewServer(http.HandlerFunc(f.serve)) + t.Cleanup(f.Close) - return len(f.calls) + return f } func (f *ModelFake) serve(w http.ResponseWriter, r *http.Request) { raw, err := io.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + return } @@ -65,10 +65,12 @@ func (f *ModelFake) serve(w http.ResponseWriter, r *http.Request) { err = json.Unmarshal(raw, &call.body) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + return } f.mutex.Lock() + call.number = len(f.calls) f.calls = append(f.calls, call) reply := f.reply @@ -77,32 +79,26 @@ func (f *ModelFake) serve(w http.ResponseWriter, r *http.Request) { reply(w, call) } -func newModelFake(t *testing.T) *ModelFake { - t.Helper() - - f := &ModelFake{ - reply: func(w http.ResponseWriter, call ModelFakeCall) { - modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("a line")}, 100, 0, 5)) - }, - } - - f.Server = httptest.NewServer(http.HandlerFunc(f.serve)) - t.Cleanup(f.Close) +// Call returns call number n. +func (f *ModelFake) Call(n int) ModelFakeCall { + f.mutex.Lock() + defer f.mutex.Unlock() - return f + return f.calls[n] } -func modelTestRequest() ModelRequest { - return ModelRequest{ - Model: "gpt-test", - Effort: "low", - Input: []any{modelUserMessage("hello")}, - } +// Calls returns the number of calls. +func (f *ModelFake) Calls() int { + f.mutex.Lock() + defer f.mutex.Unlock() + + return len(f.calls) } // modelWriteEvents writes events as a server-sent event stream. func modelWriteEvents(w http.ResponseWriter, events ...any) { w.Header().Set("Content-Type", "text/event-stream") + for _, event := range events { data, err := json.Marshal(event) if err != nil { @@ -116,16 +112,6 @@ func modelWriteEvents(w http.ResponseWriter, events ...any) { } } -func modelMessageItem(text string) map[string]any { - return map[string]any{ - "type": "message", - "role": "assistant", - "content": []any{ - map[string]any{"type": "output_text", "text": text}, - }, - } -} - func modelCompletedEvent(output []any, input, cached, out int) map[string]any { if output == nil { output = []any{} @@ -144,6 +130,24 @@ func modelCompletedEvent(output []any, input, cached, out int) map[string]any { } } +func modelMessageItem(text string) map[string]any { + return map[string]any{ + "type": "message", + "role": "assistant", + "content": []any{ + map[string]any{"type": "output_text", "text": text}, + }, + } +} + +func modelTestRequest() ModelRequest { + return ModelRequest{ + Model: "gpt-test", + Effort: "low", + Input: []any{modelUserMessage("hello")}, + } +} + func modelReadUsage(t *testing.T, path string) []ModelUsageRecord { t.Helper() @@ -155,10 +159,13 @@ func modelReadUsage(t *testing.T, path string) []ModelUsageRecord { }() var records []ModelUsageRecord + scanner := bufio.NewScanner(file) for scanner.Scan() { var record ModelUsageRecord + require.NoError(t, json.Unmarshal(scanner.Bytes(), &record)) + records = append(records, record) } @@ -167,15 +174,6 @@ func modelReadUsage(t *testing.T, path string) []ModelUsageRecord { return records } -func modelReasoningItem(id, encrypted string) map[string]any { - return map[string]any{ - "type": "reasoning", - "id": id, - "encrypted_content": encrypted, - "summary": []any{}, - } -} - func modelFunctionCallItem(id, callID, name, arguments string) map[string]any { return map[string]any{ "type": "function_call",