X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/b2873a2a0bf8b0ba648aebac51759e5fed89a2e2..153f3f9b4b09d521412eacca30eddeed395c3f41:/turn_test.go?ds=inline diff --git a/turn_test.go b/turn_test.go index 0de7dd7..c378469 100644 --- a/turn_test.go +++ b/turn_test.go @@ -15,18 +15,18 @@ import ( ) func TestTurnRunsToolsUntilTheFinalAnswer(t *testing.T) { - server := newFakeResponses(t) - server.reply = func(w http.ResponseWriter, call fakeCall) { + server := newModelFake(t) + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if call.number == 0 { - writeEvents(w, completedEvent([]any{ - reasoningItem("rs_1", "opaque thought"), - functionCallItem("fc_1", "call_1", "shell", `{"command":"echo hi"}`), + modelWriteEvents(w, modelCompletedEvent([]any{ + modelReasoningItem("rs_1", "opaque thought"), + modelFunctionCallItem("fc_1", "call_1", "shell", `{"command":"echo hi"}`), }, 100, 0, 5)) return } - writeEvents(w, completedEvent([]any{messageItem("It said hi.")}, 120, 100, 5)) + modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("It said hi.")}, 120, 100, 5)) } test := newTestLoop(t, server) @@ -61,7 +61,7 @@ func TestTurnRunsToolsUntilTheFinalAnswer(t *testing.T) { require.Equal(t, []string{"user", "busy", "tool", "echo", "reply", "idle"}, test.events.Kinds()) var turns []string - for _, record := range readUsage(t, test.usagePath) { + for _, record := range modelReadUsage(t, test.usagePath) { turns = append(turns, record.Turn) } @@ -69,7 +69,7 @@ func TestTurnRunsToolsUntilTheFinalAnswer(t *testing.T) { } func TestFirstRequestHoldsTheViewTheStateAndTheMessage(t *testing.T) { - server := newFakeResponses(t) + server := newModelFake(t) test := newTestLoop(t, server) _, err := test.memory.Append("user", "an earlier message") require.NoError(t, err) @@ -86,19 +86,19 @@ func TestFirstRequestHoldsTheViewTheStateAndTheMessage(t *testing.T) { } func TestMessageDuringATurnJoinsTheNextRequest(t *testing.T) { - server := newFakeResponses(t) + server := newModelFake(t) arrived := make(chan struct{}) release := make(chan struct{}) - server.reply = func(w http.ResponseWriter, call fakeCall) { + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if call.number == 0 { close(arrived) <-release - writeEvents(w, completedEvent([]any{functionCallItem("fc_1", "call_1", "date", `{"id":0}`)}, 100, 0, 5)) + modelWriteEvents(w, modelCompletedEvent([]any{modelFunctionCallItem("fc_1", "call_1", "date", `{"id":0}`)}, 100, 0, 5)) return } - writeEvents(w, completedEvent([]any{messageItem("done")}, 100, 0, 5)) + modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("done")}, 100, 0, 5)) } test := newTestLoop(t, server) @@ -123,16 +123,16 @@ func TestMessageDuringATurnJoinsTheNextRequest(t *testing.T) { } func TestMessageDuringTheFinalRequestStartsANewTurn(t *testing.T) { - server := newFakeResponses(t) + server := newModelFake(t) arrived := make(chan struct{}) release := make(chan struct{}) - server.reply = func(w http.ResponseWriter, call fakeCall) { + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if call.number == 0 { close(arrived) <-release } - writeEvents(w, completedEvent([]any{messageItem("answer")}, 100, 0, 5)) + modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("answer")}, 100, 0, 5)) } test := newTestLoop(t, server) @@ -153,7 +153,7 @@ func TestMessageDuringTheFinalRequestStartsANewTurn(t *testing.T) { } func TestTurnWaitsForTheSummaries(t *testing.T) { - server := newFakeResponses(t) + server := newModelFake(t) summarizer := newFakeSummarizer() summarizer.gate = make(chan struct{}) test := newTestLoopWith(t, server, summarizer) @@ -174,16 +174,16 @@ func TestTurnWaitsForTheSummaries(t *testing.T) { } func TestInterruptEndsTheTurn(t *testing.T) { - server := newFakeResponses(t) + server := newModelFake(t) arrived := make(chan struct{}) release := make(chan struct{}) - server.reply = func(w http.ResponseWriter, call fakeCall) { + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if call.number == 0 { close(arrived) <-release } - writeEvents(w, completedEvent([]any{messageItem("answer")}, 100, 0, 5)) + modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("answer")}, 100, 0, 5)) } test := newTestLoop(t, server) @@ -206,8 +206,8 @@ func TestInterruptEndsTheTurn(t *testing.T) { } func TestFailedCallEndsTheTurnWithoutARetry(t *testing.T) { - server := newFakeResponses(t) - server.reply = func(w http.ResponseWriter, call fakeCall) { + server := newModelFake(t) + server.reply = func(w http.ResponseWriter, call ModelFakeCall) { http.Error(w, "busy", http.StatusServiceUnavailable) } @@ -222,7 +222,7 @@ func TestFailedCallEndsTheTurnWithoutARetry(t *testing.T) { } func TestViewMessagesOnlyGrowBetweenTurns(t *testing.T) { - server := newFakeResponses(t) + server := newModelFake(t) test := newTestLoop(t, server) for n, text := range []string{"one", "two", "three"} { _, err := test.loop.Submit(terminalSource, text) @@ -327,7 +327,7 @@ func (r *eventRecorder) WaitFor(t *testing.T, kind string, count int) { }, 10*time.Second, time.Millisecond) } -func newTestLoop(t *testing.T, server *fakeResponses) *testLoop { +func newTestLoop(t *testing.T, server *ModelFake) *testLoop { t.Helper() return newTestLoopWith(t, server, newFakeSummarizer()) @@ -335,7 +335,7 @@ func newTestLoop(t *testing.T, server *fakeResponses) *testLoop { // newTestLoopWith returns a running loop on a new agent whose builds use // summarizer and whose commands run in a temporary directory. -func newTestLoopWith(t *testing.T, server *fakeResponses, summarizer Summarizer) *testLoop { +func newTestLoopWith(t *testing.T, server *ModelFake, summarizer Summarizer) *testLoop { t.Helper() agent, err := openAgent(t.TempDir()) @@ -353,7 +353,7 @@ func newTestLoopWith(t *testing.T, server *fakeResponses, summarizer Summarizer) usagePath: filepath.Join(t.TempDir(), "usage.jsonl"), } - client := newClient(server.URL, apiKey(testKey), agent.CacheKey(), test.usagePath) + client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), test.usagePath) test.loop, err = newLoop(agent, memory, client, clock.Now, test.events.Add) require.NoError(t, err) ctx, cancel := context.WithCancel(t.Context()) @@ -372,7 +372,7 @@ func newTestLoopWith(t *testing.T, server *fakeResponses, summarizer Summarizer) } // inputTexts returns the text of each user message of the input of call. -func inputTexts(t *testing.T, call fakeCall) []string { +func inputTexts(t *testing.T, call ModelFakeCall) []string { t.Helper() var body struct { @@ -402,22 +402,3 @@ func inputTexts(t *testing.T, call fakeCall) []string { return texts } - -func reasoningItem(id, encrypted string) map[string]any { - return map[string]any{ - "type": "reasoning", - "id": id, - "encrypted_content": encrypted, - "summary": []any{}, - } -} - -func functionCallItem(id, callID, name, arguments string) map[string]any { - return map[string]any{ - "type": "function_call", - "id": id, - "call_id": callID, - "name": name, - "arguments": arguments, - } -}