X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/56d959f2d573eef1be5c04d0bf1659fa30d36e7c..c781f79f3c8cb9a7577ee1574d196988da2719b3:/daemon_start_helper_test.go?ds=sidebyside diff --git a/daemon_start_helper_test.go b/daemon_start_helper_test.go index f90d2d7..b09c11b 100644 --- a/daemon_start_helper_test.go +++ b/daemon_start_helper_test.go @@ -2,8 +2,10 @@ package main import ( "context" + "os" "path/filepath" "testing" + "time" "github.com/stretchr/testify/require" ) @@ -24,13 +26,22 @@ func daemonStartTestWithBot(t *testing.T, server *ModelFake, bot *TelegramBot) s func daemonStartTestWithWhatsApp(t *testing.T, server *ModelFake, bot *TelegramBot, whatsapp *WhatsApp) (string, *Memory) { t.Helper() - dir := t.TempDir() + 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) @@ -40,9 +51,10 @@ func daemonStartTestWithWhatsApp(t *testing.T, server *ModelFake, bot *TelegramB 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, whatsapp, func(notify func(Event)) (*TurnLoop, error) { - return newTurnLoop(agent, memory, newTestSubagents(t), 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) }) }() @@ -54,3 +66,19 @@ func daemonStartTestWithWhatsApp(t *testing.T, server *ModelFake, bot *TelegramB 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 +}