X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/b2fc2d6a737dd5bab25e57ba822dd07258de93e6..c781f79f3c8cb9a7577ee1574d196988da2719b3:/turn_test.go?ds=sidebyside diff --git a/turn_test.go b/turn_test.go index 3b86f0c..5341de5 100644 --- a/turn_test.go +++ b/turn_test.go @@ -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() @@ -343,12 +391,6 @@ func (r *TurnEventRecorder) Add(event Event) { r.events = append(r.events, event) } -func newTestTurnLoop(t *testing.T, server *ModelFake) *TurnTestLoop { - t.Helper() - - return newTestTurnLoopWith(t, server, newFakeSummarizer()) -} - // turnInputTexts returns the text of each user message of the input of call. func turnInputTexts(t *testing.T, call ModelFakeCall) []string { t.Helper() @@ -383,43 +425,11 @@ 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"), +func modelReasoningItem(id, encrypted string) map[string]any { + return map[string]any{ + "type": "reasoning", + "id": id, + "encrypted_content": encrypted, + "summary": []any{}, } - - 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 }