]> git.giacomolli.uk - onechat/blobdiff - model_fake_helper_test.go
Prove direct cancellation during a pending sync
[onechat] / model_fake_helper_test.go
index e3fb4c6f8caf156831e6751305545c5fb966ae67..af94776633dbde9cc779a3dd46014aa71401b3bf 100644 (file)
@@ -34,26 +34,26 @@ type ModelFake struct {
        reply func(http.ResponseWriter, ModelFakeCall)
 }
 
        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)
 }
 
 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
        }
 
                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)
        err = json.Unmarshal(raw, &call.body)
        if err != nil {
                http.Error(w, err.Error(), http.StatusBadRequest)
+
                return
        }
 
        f.mutex.Lock()
                return
        }
 
        f.mutex.Lock()
+
        call.number = len(f.calls)
        f.calls = append(f.calls, call)
        reply := f.reply
        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)
 }
 
        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")
 }
 
 // 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 {
        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{}
 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()
 
 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
        }()
 
        var records []ModelUsageRecord
+
        scanner := bufio.NewScanner(file)
        for scanner.Scan() {
                var record ModelUsageRecord
        scanner := bufio.NewScanner(file)
        for scanner.Scan() {
                var record ModelUsageRecord
+
                require.NoError(t, json.Unmarshal(scanner.Bytes(), &record))
                require.NoError(t, json.Unmarshal(scanner.Bytes(), &record))
+
                records = append(records, record)
        }
 
                records = append(records, record)
        }
 
@@ -167,15 +174,6 @@ func modelReadUsage(t *testing.T, path string) []ModelUsageRecord {
        return records
 }
 
        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",
 func modelFunctionCallItem(id, callID, name, arguments string) map[string]any {
        return map[string]any{
                "type":      "function_call",