]> git.giacomolli.uk - onechat/blobdiff - daemon_test.go
Show stored images to the user
[onechat] / daemon_test.go
index e27c6ae52e27260f5df00f8bee89215132762623..6a4d98c5a040456d0baf8e06fb4307542bcbb56a 100644 (file)
@@ -1,7 +1,6 @@
 package main
 
 import (
-       "context"
        "io"
        "net/http"
        "os"
@@ -17,6 +16,7 @@ func TestClientsSeeTheTurnAsItHappens(t *testing.T) {
        server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
                if call.number == 0 {
                        modelWriteEvents(w, modelCompletedEvent([]any{modelFunctionCallItem("fc_1", "call_1", "shell", `{"command":"echo hi"}`)}, 100, 0, 5))
+
                        return
                }
 
@@ -30,6 +30,7 @@ func TestClientsSeeTheTurnAsItHappens(t *testing.T) {
        second.WaitFor(t, "[connected to OneChat]")
 
        first.Type("Run echo hi.")
+
        for _, client := range []*TerminalTestClient{first, second} {
                client.WaitFor(t, "[done]")
                require.Equal(t, "[connected to OneChat]\n[working]\n[tool] shell {\"command\":\"echo hi\"}\n[result] hi\nIt said hi.\n[done]\n", client.Output())
@@ -37,6 +38,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 +52,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 +100,36 @@ 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:    `^<6>onechat: the agent is ready\n$`,
+               },
+               {
+                       name:    "a terminal gets the time in the line",
+                       journal: "",
+                       line:    `^\d{4}/\d\d/\d\d \d\d:\d\d:\d\d INFO onechat: 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: ").Info("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 +138,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)) (*loop, error) {
-                       return newLoop(agent, memory, client, clock.Now, notify)
-               })
-       }()
-
-       t.Cleanup(func() {
-               cancel()
-               require.NoError(t, <-done)
-               require.NoError(t, memory.Close())
-       })
-
-       return socket
-}