X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/6d96a69d33eb634f61f809001aca19ae57113180..d9fddc0f506eb4ead7049f5638674b2b8773fcaa:/daemon_test.go?ds=inline diff --git a/daemon_test.go b/daemon_test.go index 2dfe7f1..6a8cd3e 100644 --- a/daemon_test.go +++ b/daemon_test.go @@ -1,7 +1,6 @@ package main import ( - "context" "io" "net/http" "os" @@ -37,6 +36,8 @@ func TestClientsSeeTheTurnAsItHappens(t *testing.T) { first.Type(stopCommand) first.WaitFor(t, "[error] no turn runs") + first.Type(stopCommand + " BlueFox") + first.WaitFor(t, "[error] no subagent BlueFox runs") require.NotContains(t, second.Output(), "no turn runs") require.NoError(t, first.Close()) } @@ -49,7 +50,7 @@ func TestStopInterruptsTheTurn(t *testing.T) { close(release) }) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(_ http.ResponseWriter, _ ModelFakeCall) { close(arrived) <-release } @@ -97,6 +98,35 @@ func TestDaemonWritesNoFileForALongSocketPath(t *testing.T) { require.Empty(t, entries) } +func TestDaemonLogHasATimeStampOnlyOutsideTheJournal(t *testing.T) { + testCases := []struct { + name string + journal string + line string + }{ + { + name: "the journal adds the time itself", + journal: "8:12345", + line: `^onechat: the agent is ready\n$`, + }, + { + name: "a terminal gets the time in the line", + journal: "", + line: `^onechat: \d{4}/\d\d/\d\d \d\d:\d\d:\d\d the agent is ready\n$`, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("JOURNAL_STREAM", tc.journal) + + var out strings.Builder + daemonLogger(&out, "onechat: ").Print("the agent is ready") + require.Regexp(t, tc.line, out.String()) + }) + } +} + // daemonStartTest serves a new agent on a socket in a temporary directory and // returns the socket path. The commands of the agent run in a temporary // directory. @@ -105,38 +135,3 @@ func daemonStartTest(t *testing.T, server *ModelFake) string { return daemonStartTestWithBot(t, server, nil) } - -// daemonStartTestWithBot is daemonStartTest with bot, which can be nil. -func daemonStartTestWithBot(t *testing.T, server *ModelFake, bot *TelegramBot) string { - t.Helper() - - dir := t.TempDir() - agent, err := openAgent(filepath.Join(dir, daemonAgentDirName)) - require.NoError(t, err) - - agent.config.Workdir = t.TempDir() - clock := newTestClock() - memory, err := openMemory(t.Context(), agent.dir, clock.Now, newFakeSummarizer()) - require.NoError(t, err) - - socket := filepath.Join(dir, socketFileName) - listener, err := daemonListenSocket(socket) - require.NoError(t, err) - - client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl")) - ctx, cancel := context.WithCancel(t.Context()) - done := make(chan error) - go func() { - done <- daemonServe(ctx, listener, bot, func(notify func(Event)) (*TurnLoop, error) { - return newTurnLoop(agent, memory, client, clock.Now, notify) - }) - }() - - t.Cleanup(func() { - cancel() - require.NoError(t, <-done) - require.NoError(t, memory.Close()) - }) - - return socket -}