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
}
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
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 {
}
}
-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 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()
}()
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)
}
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",