10 "github.com/stretchr/testify/require"
13 // daemonStartTestWithBot is daemonStartTest with bot, which can be nil.
14 // WhatsApp is not linked.
15 func daemonStartTestWithBot(t *testing.T, server *ModelFake, bot *TelegramBot) string {
18 whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
19 socket, _ := daemonStartTestWithWhatsApp(t, server, bot, whatsapp)
24 // daemonStartTestWithWhatsApp is daemonStartTestWithBot with whatsapp. It also
25 // returns the memory of the agent.
26 func daemonStartTestWithWhatsApp(t *testing.T, server *ModelFake, bot *TelegramBot, whatsapp *WhatsApp) (string, *Memory) {
29 clock := newTestClock()
31 return daemonStartTestWithSchedules(t, server, bot, whatsapp, newTestSchedules(t, clock.Now), clock.Now)
34 // daemonStartTestWithSchedules is daemonStartTestWithWhatsApp with schedules,
35 // and with clock for the memory and the turns.
36 func daemonStartTestWithSchedules(t *testing.T, server *ModelFake, bot *TelegramBot, whatsapp *WhatsApp, schedules *Schedules, clock func() time.Time) (string, *Memory) {
39 dir := socketTestDir(t)
40 agent, err := openAgent(filepath.Join(dir, daemonAgentDirName))
41 require.NoError(t, err)
43 agent.config.Workdir = t.TempDir()
44 memory, err := openMemory(t.Context(), agent.dir, clock, newFakeSummarizer(), discardLog())
45 require.NoError(t, err)
47 socket := filepath.Join(dir, socketFileName)
48 listener, err := daemonListenSocket(socket)
49 require.NoError(t, err)
51 client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl"))
52 ctx, cancel := context.WithCancel(t.Context())
53 done := make(chan error)
54 subagents := newTestSubagents(t)
56 done <- daemonServe(ctx, listener, bot, whatsapp, schedules, func(notify func(Event)) (*TurnLoop, error) {
57 return newTurnLoop(agent, memory, subagents, whatsapp, schedules, client, clock, discardLog(), notify)
63 require.NoError(t, <-done)
64 require.NoError(t, memory.Close())
70 // socketTestDir returns a new short directory under /tmp for data that holds a
71 // daemon socket. t.TempDir holds the test name and the TMPDIR of the shell, so
72 // its paths can pass the 107-byte limit of a Unix socket path.
73 func socketTestDir(t *testing.T) string {
76 dir, err := os.MkdirTemp("/tmp", "onechat-")
77 require.NoError(t, err)
80 require.NoError(t, os.RemoveAll(dir))