X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/b2fc2d6a737dd5bab25e57ba822dd07258de93e6..e86df5fad9aa2f280aad845adeefd239b0e944ae:/turn_test.go?ds=inline diff --git a/turn_test.go b/turn_test.go index 3b86f0c..3c6bb06 100644 --- a/turn_test.go +++ b/turn_test.go @@ -18,7 +18,7 @@ func TestTurnRunsToolsUntilTheFinalAnswer(t *testing.T) { server.reply = func(w http.ResponseWriter, call ModelFakeCall) { if call.number == 0 { modelWriteEvents(w, modelCompletedEvent([]any{ - modelReasoningItem("rs_1", "opaque thought"), + turnReasoningItem("rs_1", "opaque thought"), modelFunctionCallItem("fc_1", "call_1", "shell", `{"command":"echo hi"}`), }, 100, 0, 5)) @@ -57,6 +57,7 @@ func TestTurnRunsToolsUntilTheFinalAnswer(t *testing.T) { "echo: hi\n", "reply: It said hi.", }, test.Log(t)) + require.Equal(t, []string{"user", "busy", "tool", "echo", "reply", "idle"}, test.events.Kinds()) var turns []string @@ -263,6 +264,59 @@ type TurnTestLoop struct { usagePath string } +// TurnEventRecorder keeps the events of a loop. +type TurnEventRecorder struct { + mutex sync.Mutex + events []Event +} + +func newTestTurnLoop(t *testing.T, server *ModelFake) *TurnTestLoop { + t.Helper() + + return newTestTurnLoopWith(t, server, newFakeSummarizer()) +} + +// newTestTurnLoopWith returns a running loop on a new agent whose builds use +// summarizer and whose commands run in a temporary directory. +func newTestTurnLoopWith(t *testing.T, server *ModelFake, summarizer Summarizer) *TurnTestLoop { + t.Helper() + + agent, err := openAgent(filepath.Join(t.TempDir(), "agent")) + require.NoError(t, err) + + agent.config.Workdir = t.TempDir() + clock := newTestClock() + memory, err := openMemory(t.Context(), t.TempDir(), clock.Now, summarizer, discardLog()) + require.NoError(t, err) + + test := &TurnTestLoop{ + agent: agent, + memory: memory, + events: &TurnEventRecorder{}, + logs: &SyncBuffer{}, + usagePath: filepath.Join(t.TempDir(), "usage.jsonl"), + } + + client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), test.usagePath) + whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), clock) + test.loop, err = newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, newTestSchedules(t, clock.Now), client, clock.Now, daemonTestLogger(test.logs), test.events.Add) + require.NoError(t, err) + + ctx, cancel := context.WithCancel(t.Context()) + done := make(chan error) + go func() { + done <- test.loop.Run(ctx) + }() + + t.Cleanup(func() { + cancel() + require.ErrorIs(t, <-done, context.Canceled) + require.NoError(t, memory.Close()) + }) + + return test +} + // Log returns the logged messages as "kind: text". func (l *TurnTestLoop) Log(t *testing.T) []string { t.Helper() @@ -279,12 +333,6 @@ func (l *TurnTestLoop) Log(t *testing.T) []string { return lines } -// TurnEventRecorder keeps the events of a loop. -type TurnEventRecorder struct { - mutex sync.Mutex - events []Event -} - // WaitFor waits until count events of kind came. func (r *TurnEventRecorder) WaitFor(t *testing.T, kind string, count int) { t.Helper() @@ -322,6 +370,13 @@ func (r *TurnEventRecorder) Last(kind string) string { return r.LastEvent(kind).Text } +func (r *TurnEventRecorder) Add(event Event) { + r.mutex.Lock() + defer r.mutex.Unlock() + + r.events = append(r.events, event) +} + // LastEvent returns the last event of kind. func (r *TurnEventRecorder) LastEvent(kind string) Event { r.mutex.Lock() @@ -336,17 +391,13 @@ func (r *TurnEventRecorder) LastEvent(kind string) Event { return Event{} } -func (r *TurnEventRecorder) Add(event Event) { - r.mutex.Lock() - defer r.mutex.Unlock() - - r.events = append(r.events, event) -} - -func newTestTurnLoop(t *testing.T, server *ModelFake) *TurnTestLoop { - t.Helper() - - return newTestTurnLoopWith(t, server, newFakeSummarizer()) +func turnReasoningItem(id, encrypted string) map[string]any { + return map[string]any{ + "type": "reasoning", + "id": id, + "encrypted_content": encrypted, + "summary": []any{}, + } } // turnInputTexts returns the text of each user message of the input of call. @@ -382,44 +433,3 @@ func turnInputTexts(t *testing.T, call ModelFakeCall) []string { return texts } - -// newTestTurnLoopWith returns a running loop on a new agent whose builds use -// summarizer and whose commands run in a temporary directory. -func newTestTurnLoopWith(t *testing.T, server *ModelFake, summarizer Summarizer) *TurnTestLoop { - t.Helper() - - agent, err := openAgent(filepath.Join(t.TempDir(), "agent")) - require.NoError(t, err) - - agent.config.Workdir = t.TempDir() - clock := newTestClock() - memory, err := openMemory(t.Context(), t.TempDir(), clock.Now, summarizer, discardLog()) - require.NoError(t, err) - - test := &TurnTestLoop{ - agent: agent, - memory: memory, - events: &TurnEventRecorder{}, - logs: &SyncBuffer{}, - usagePath: filepath.Join(t.TempDir(), "usage.jsonl"), - } - - client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), test.usagePath) - whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), clock) - test.loop, err = newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, newTestSchedules(t, clock.Now), client, clock.Now, daemonTestLogger(test.logs), test.events.Add) - require.NoError(t, err) - - ctx, cancel := context.WithCancel(t.Context()) - done := make(chan error) - go func() { - done <- test.loop.Run(ctx) - }() - - t.Cleanup(func() { - cancel() - require.ErrorIs(t, <-done, context.Canceled) - require.NoError(t, memory.Close()) - }) - - return test -}