X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/6d96a69d33eb634f61f809001aca19ae57113180..d7a967f43fae27bcabc6072da3b5a2f923d47764:/turn_test.go diff --git a/turn_test.go b/turn_test.go index 69c9236..9bb0e99 100644 --- a/turn_test.go +++ b/turn_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "log" "net/http" "path/filepath" "strings" @@ -170,7 +171,7 @@ func TestTurnWaitsForTheSummaries(t *testing.T) { close(summarizer.gate) test.events.WaitFor(t, "idle", 1) require.Equal(t, 1, server.Calls()) - require.NotContains(t, turnInputTexts(t, server.Call(0))[0], pending) + require.NotContains(t, turnInputTexts(t, server.Call(0))[0], viewPending) } func TestInterruptEndsTheTurn(t *testing.T) { @@ -203,6 +204,7 @@ func TestInterruptEndsTheTurn(t *testing.T) { require.NoError(t, err) test.events.WaitFor(t, "idle", 2) require.Equal(t, "reply: answer", test.Log(t)[2]) + require.Empty(t, test.logs.String()) } func TestFailedCallEndsTheTurnWithoutARetry(t *testing.T) { @@ -219,6 +221,10 @@ func TestFailedCallEndsTheTurnWithoutARetry(t *testing.T) { time.Sleep(50 * time.Millisecond) require.Equal(t, 1, server.Calls()) require.Contains(t, test.events.Last("error"), "HTTP 503") + require.Equal(t, 1, strings.Count(test.logs.String(), "\n")) + require.Contains(t, test.logs.String(), "turn failed: ") + require.Contains(t, test.logs.String(), "HTTP 503") + require.NotContains(t, test.logs.String(), "hello") } func TestViewMessagesOnlyGrowBetweenTurns(t *testing.T) { @@ -250,6 +256,7 @@ type TurnTestLoop struct { memory *Memory loop *TurnLoop events *TurnEventRecorder + logs *SyncBuffer usagePath string } @@ -308,16 +315,21 @@ func (r *TurnEventRecorder) Count(kind string) int { // Last returns the text of the last event of kind. func (r *TurnEventRecorder) Last(kind string) string { + return r.LastEvent(kind).Text +} + +// LastEvent returns the last event of kind. +func (r *TurnEventRecorder) LastEvent(kind string) Event { r.mutex.Lock() defer r.mutex.Unlock() for n := len(r.events) - 1; n >= 0; n-- { if r.events[n].Kind == kind { - return r.events[n].Text + return r.events[n] } } - return "" + return Event{} } func (r *TurnEventRecorder) Add(event Event) { @@ -375,18 +387,20 @@ func newTestTurnLoopWith(t *testing.T, server *ModelFake, summarizer Summarizer) agent.config.Workdir = t.TempDir() clock := newTestClock() - memory, err := openMemory(t.Context(), t.TempDir(), clock.Now, summarizer) + 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) - test.loop, err = newTurnLoop(agent, memory, client, clock.Now, test.events.Add) + whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), clock) + test.loop, err = newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, newTestSchedules(t, clock.Now), client, clock.Now, log.New(test.logs, "", 0), test.events.Add) require.NoError(t, err) ctx, cancel := context.WithCancel(t.Context()) done := make(chan error)