]> git.giacomolli.uk - onechat/blobdiff - tools_test.go
Separate multiline Go statements
[onechat] / tools_test.go
index 3ab784551cc56b76f7071a9372279078c8eea14d..23507f61785bb3e962529169274d9670999cf4aa 100644 (file)
@@ -29,6 +29,7 @@ func TestToolRunsAfterItsCallIsDurable(t *testing.T) {
                "flush, marker absent",
                "echo: done\n",
        }, log.Events())
                "flush, marker absent",
                "echo: done\n",
        }, log.Events())
+
        require.FileExists(t, filepath.Join(dir, "ran"))
 }
 
        require.FileExists(t, filepath.Join(dir, "ran"))
 }
 
@@ -93,6 +94,7 @@ func TestShellInterruptKillsTheCommandAndItsChildren(t *testing.T) {
 
        start := time.Now()
        output := box.shell(ctx, "echo started; sleep 30 & sleep 30")
 
        start := time.Now()
        output := box.shell(ctx, "echo started; sleep 30 & sleep 30")
+
        require.Less(t, time.Since(start), 10*time.Second)
        require.Equal(t, "started\n\n[interrupted]", output)
 }
        require.Less(t, time.Since(start), 10*time.Second)
        require.Equal(t, "started\n\n[interrupted]", output)
 }
@@ -107,7 +109,7 @@ func TestFileTools(t *testing.T) {
        require.Equal(t, "error: old occurs 2 times in "+path+", not once", box.run(t.Context(), "edit", `{"path":"notes/a.txt","old":"two","new":"2"}`))
        require.Equal(t, "edited "+path, box.run(t.Context(), "edit", `{"path":"notes/a.txt","old":"one","new":"1"}`))
        require.Equal(t, "1 two two\n", box.run(t.Context(), "read", `{"path":"notes/a.txt"}`))
        require.Equal(t, "error: old occurs 2 times in "+path+", not once", box.run(t.Context(), "edit", `{"path":"notes/a.txt","old":"two","new":"2"}`))
        require.Equal(t, "edited "+path, box.run(t.Context(), "edit", `{"path":"notes/a.txt","old":"one","new":"1"}`))
        require.Equal(t, "1 two two\n", box.run(t.Context(), "read", `{"path":"notes/a.txt"}`))
-       require.Contains(t, box.run(t.Context(), "read", `{"path":"missing.txt"}`), "error: open "+filepath.Join(dir, "missing.txt"))
+       require.Contains(t, box.run(t.Context(), "read", `{"path":"missing.txt"}`), "error: open tool file: open "+filepath.Join(dir, "missing.txt"))
        require.Equal(t, `error: arguments: json: unknown field "file"`, box.run(t.Context(), "read", `{"file":"a.txt"}`))
        require.Equal(t, `error: unknown tool "grep"`, box.run(t.Context(), "grep", `{}`))
 }
        require.Equal(t, `error: arguments: json: unknown field "file"`, box.run(t.Context(), "read", `{"file":"a.txt"}`))
        require.Equal(t, `error: unknown tool "grep"`, box.run(t.Context(), "grep", `{}`))
 }
@@ -211,7 +213,7 @@ func TestHeadTailKeepsTheEndsOfALongOutput(t *testing.T) {
        require.True(t, strings.HasPrefix(text, "0000xxx"))
        require.True(t, strings.HasSuffix(text, "0999"+strings.Repeat("x", 995)+"\n"))
        require.Contains(t, text, fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-toolClipCharacters))
        require.True(t, strings.HasPrefix(text, "0000xxx"))
        require.True(t, strings.HasSuffix(text, "0999"+strings.Repeat("x", 995)+"\n"))
        require.Contains(t, text, fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-toolClipCharacters))
-       require.Equal(t, toolClipCharacters+len(fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-toolClipCharacters)), len(text))
+       require.Len(t, text, toolClipCharacters+len(fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-toolClipCharacters)))
 }
 
 // ToolRecordingLog is a chat log that records each append and flush. The flush
 }
 
 // ToolRecordingLog is a chat log that records each append and flush. The flush
@@ -222,6 +224,26 @@ type ToolRecordingLog struct {
        events []string
 }
 
        events []string
 }
 
+// newTestToolbox returns a toolbox with log, an empty chat, and the working
+// directory dir.
+func newTestToolbox(t *testing.T, log ToolLog, dir string) *Toolbox {
+       t.Helper()
+
+       memory, err := openMemory(t.Context(), t.TempDir(), newTestClock().Now, newFakeSummarizer(), discardLog())
+       require.NoError(t, err)
+
+       t.Cleanup(func() {
+               require.NoError(t, memory.Close())
+       })
+
+       return &Toolbox{
+               log:       log,
+               chat:      memory,
+               subagents: newTestSubagents(t),
+               workdir:   dir,
+       }
+}
+
 func (r *ToolRecordingLog) Append(kind, text string) (Message, error) {
        r.mutex.Lock()
        defer r.mutex.Unlock()
 func (r *ToolRecordingLog) Append(kind, text string) (Message, error) {
        r.mutex.Lock()
        defer r.mutex.Unlock()
@@ -252,23 +274,3 @@ func (r *ToolRecordingLog) Events() []string {
 
        return append([]string(nil), r.events...)
 }
 
        return append([]string(nil), r.events...)
 }
-
-// newTestToolbox returns a toolbox with log, an empty chat, and the working
-// directory dir.
-func newTestToolbox(t *testing.T, log ToolLog, dir string) *Toolbox {
-       t.Helper()
-
-       memory, err := openMemory(t.Context(), t.TempDir(), newTestClock().Now, newFakeSummarizer(), discardLog())
-       require.NoError(t, err)
-
-       t.Cleanup(func() {
-               require.NoError(t, memory.Close())
-       })
-
-       return &Toolbox{
-               log:       log,
-               chat:      memory,
-               subagents: newTestSubagents(t),
-               workdir:   dir,
-       }
-}