]> git.giacomolli.uk - onechat/blobdiff - daemon_start_helper_test.go
Order model and chat declarations
[onechat] / daemon_start_helper_test.go
index fd89b34bddfa0ae5b176efe7eb0cabb00e9c3b0d..b09c11be0679f2e2b1457e08622c98fa4ab26b24 100644 (file)
@@ -2,23 +2,46 @@ package main
 
 import (
        "context"
+       "os"
        "path/filepath"
        "testing"
+       "time"
 
        "github.com/stretchr/testify/require"
 )
 
 // daemonStartTestWithBot is daemonStartTest with bot, which can be nil.
+// WhatsApp is not linked.
 func daemonStartTestWithBot(t *testing.T, server *ModelFake, bot *TelegramBot) string {
        t.Helper()
 
-       dir := t.TempDir()
+       whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
+       socket, _ := daemonStartTestWithWhatsApp(t, server, bot, whatsapp)
+
+       return socket
+}
+
+// daemonStartTestWithWhatsApp is daemonStartTestWithBot with whatsapp. It also
+// returns the memory of the agent.
+func daemonStartTestWithWhatsApp(t *testing.T, server *ModelFake, bot *TelegramBot, whatsapp *WhatsApp) (string, *Memory) {
+       t.Helper()
+
+       clock := newTestClock()
+
+       return daemonStartTestWithSchedules(t, server, bot, whatsapp, newTestSchedules(t, clock.Now), clock.Now)
+}
+
+// daemonStartTestWithSchedules is daemonStartTestWithWhatsApp with schedules,
+// and with clock for the memory and the turns.
+func daemonStartTestWithSchedules(t *testing.T, server *ModelFake, bot *TelegramBot, whatsapp *WhatsApp, schedules *Schedules, clock func() time.Time) (string, *Memory) {
+       t.Helper()
+
+       dir := socketTestDir(t)
        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())
+       memory, err := openMemory(t.Context(), agent.dir, clock, newFakeSummarizer(), discardLog())
        require.NoError(t, err)
 
        socket := filepath.Join(dir, socketFileName)
@@ -28,9 +51,10 @@ func daemonStartTestWithBot(t *testing.T, server *ModelFake, bot *TelegramBot) s
        client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl"))
        ctx, cancel := context.WithCancel(t.Context())
        done := make(chan error)
+       subagents := newTestSubagents(t)
        go func() {
-               done <- daemonServe(ctx, listener, bot, func(notify func(Event)) (*TurnLoop, error) {
-                       return newTurnLoop(agent, memory, client, clock.Now, notify)
+               done <- daemonServe(ctx, listener, bot, whatsapp, schedules, func(notify func(Event)) (*TurnLoop, error) {
+                       return newTurnLoop(agent, memory, subagents, whatsapp, schedules, client, clock, discardLog(), notify)
                })
        }()
 
@@ -40,5 +64,21 @@ func daemonStartTestWithBot(t *testing.T, server *ModelFake, bot *TelegramBot) s
                require.NoError(t, memory.Close())
        })
 
-       return socket
+       return socket, memory
+}
+
+// socketTestDir returns a new short directory under /tmp for data that holds a
+// daemon socket. t.TempDir holds the test name and the TMPDIR of the shell, so
+// its paths can pass the 107-byte limit of a Unix socket path.
+func socketTestDir(t *testing.T) string {
+       t.Helper()
+
+       dir, err := os.MkdirTemp("/tmp", "onechat-")
+       require.NoError(t, err)
+
+       t.Cleanup(func() {
+               require.NoError(t, os.RemoveAll(dir))
+       })
+
+       return dir
 }