12 "github.com/stretchr/testify/require"
15 func TestClientsSeeTheTurnAsItHappens(t *testing.T) {
16 server := newModelFake(t)
17 server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
19 modelWriteEvents(w, modelCompletedEvent([]any{modelFunctionCallItem("fc_1", "call_1", "shell", `{"command":"echo hi"}`)}, 100, 0, 5))
23 modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("It said hi.")}, 100, 0, 5))
26 socket := startTestDaemon(t, server)
27 first := terminalStartTestClient(t, socket)
28 second := terminalStartTestClient(t, socket)
29 first.WaitFor(t, "[connected to OneChat]")
30 second.WaitFor(t, "[connected to OneChat]")
32 first.Type("Run echo hi.")
33 for _, client := range []*TerminalTestClient{first, second} {
34 client.WaitFor(t, "[done]")
35 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())
38 first.Type(stopCommand)
39 first.WaitFor(t, "[error] no turn runs")
40 require.NotContains(t, second.Output(), "no turn runs")
41 require.NoError(t, first.Close())
44 func TestStopInterruptsTheTurn(t *testing.T) {
45 server := newModelFake(t)
46 arrived := make(chan struct{})
47 release := make(chan struct{})
52 server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
57 socket := startTestDaemon(t, server)
58 client := terminalStartTestClient(t, socket)
59 client.Type("a long task")
62 late := terminalStartTestClient(t, socket)
63 late.WaitFor(t, "[connected to OneChat]\n[working]")
65 client.Type(stopCommand)
66 client.WaitFor(t, "[error] interrupted\n[done]")
69 func TestListenSocketReplacesAnOldSocketFile(t *testing.T) {
70 path := filepath.Join(t.TempDir(), socketFileName)
71 require.NoError(t, os.WriteFile(path, nil, 0o600))
73 listener, err := listenSocket(path)
74 require.NoError(t, err)
76 info, err := os.Stat(path)
77 require.NoError(t, err)
78 require.Equal(t, os.ModeSocket, info.Mode().Type())
79 require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
80 require.NoError(t, listener.Close())
83 func TestListenSocketRefusesALongPath(t *testing.T) {
84 _, err := listenSocket(filepath.Join(t.TempDir(), strings.Repeat("d", 100), socketFileName))
85 require.ErrorContains(t, err, "set ONECHAT_DIR to a shorter path")
88 func TestDaemonWritesNoFileForALongSocketPath(t *testing.T) {
89 dir := filepath.Join(t.TempDir(), strings.Repeat("d", 100))
90 t.Setenv("ONECHAT_DIR", dir)
92 err := daemon(t.Context(), io.Discard)
93 require.ErrorContains(t, err, "set ONECHAT_DIR to a shorter path")
95 entries, err := os.ReadDir(dir)
96 require.NoError(t, err)
97 require.Empty(t, entries)
100 func TestOneProcessHoldsTheTokens(t *testing.T) {
102 lock, err := lockTokens(dir)
103 require.NoError(t, err)
105 _, err = lockTokens(dir)
106 require.ErrorIs(t, err, errTokensInUse)
107 require.NoError(t, lock.Close())
109 lock, err = lockTokens(dir)
110 require.NoError(t, err)
111 require.NoError(t, lock.Close())
114 // startTestDaemon serves a new agent on a socket in a temporary directory and
115 // returns the socket path. The commands of the agent run in a temporary
117 func startTestDaemon(t *testing.T, server *ModelFake) string {
120 return startTestDaemonWithBot(t, server, nil)
123 // startTestDaemonWithBot is startTestDaemon with bot, which can be nil.
124 func startTestDaemonWithBot(t *testing.T, server *ModelFake, bot *TelegramBot) string {
128 agent, err := openAgent(filepath.Join(dir, agentDirName))
129 require.NoError(t, err)
131 agent.config.Workdir = t.TempDir()
132 clock := newTestClock()
133 memory, err := openMemory(t.Context(), agent.dir, clock.Now, newFakeSummarizer())
134 require.NoError(t, err)
136 socket := filepath.Join(dir, socketFileName)
137 listener, err := listenSocket(socket)
138 require.NoError(t, err)
140 client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl"))
141 ctx, cancel := context.WithCancel(t.Context())
142 done := make(chan error)
144 done <- serveAgent(ctx, listener, bot, func(notify func(Event)) (*loop, error) {
145 return newLoop(agent, memory, client, clock.Now, notify)
151 require.NoError(t, <-done)
152 require.NoError(t, memory.Close())