X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/f2d227db9c3b556c7b0e1cfe735ccc54311850e5..de170709599e849c6b409d247b800dc68b0c7ab7:/tools_test.go diff --git a/tools_test.go b/tools_test.go index a5f8f18..8960b8f 100644 --- a/tools_test.go +++ b/tools_test.go @@ -16,7 +16,7 @@ import ( func TestToolRunsAfterItsCallIsDurable(t *testing.T) { dir := t.TempDir() - log := &recordingLog{ + log := &ToolRecordingLog{ marker: filepath.Join(dir, "ran"), } @@ -34,7 +34,7 @@ func TestToolRunsAfterItsCallIsDurable(t *testing.T) { func TestToolDoesNotRunAfterAnInterruptBeforeTheFlush(t *testing.T) { dir := t.TempDir() - log := &recordingLog{ + log := &ToolRecordingLog{ marker: filepath.Join(dir, "ran"), } @@ -51,7 +51,7 @@ func TestToolDoesNotRunAfterAnInterruptBeforeTheFlush(t *testing.T) { func TestShellRunsInTheWorkingDirectory(t *testing.T) { dir := t.TempDir() - box := newTestToolbox(t, &recordingLog{}, dir) + box := newTestToolbox(t, &ToolRecordingLog{}, dir) testCases := []struct { name string command string @@ -87,7 +87,7 @@ func TestShellRunsInTheWorkingDirectory(t *testing.T) { } func TestShellInterruptKillsTheCommandAndItsChildren(t *testing.T) { - box := newTestToolbox(t, &recordingLog{}, t.TempDir()) + box := newTestToolbox(t, &ToolRecordingLog{}, t.TempDir()) ctx, cancel := context.WithTimeout(t.Context(), 200*time.Millisecond) defer cancel() @@ -99,7 +99,7 @@ func TestShellInterruptKillsTheCommandAndItsChildren(t *testing.T) { func TestFileTools(t *testing.T) { dir := t.TempDir() - box := newTestToolbox(t, &recordingLog{}, dir) + box := newTestToolbox(t, &ToolRecordingLog{}, dir) path := filepath.Join(dir, "notes", "a.txt") require.Equal(t, "wrote 12 bytes to "+path, box.run(t.Context(), "write", `{"path":"notes/a.txt","text":"one two two\n"}`)) @@ -125,7 +125,7 @@ func TestZoomAndDateReadTheChat(t *testing.T) { require.NoError(t, err) } - box := &toolbox{ + box := &Toolbox{ log: memory, chat: memory, workdir: t.TempDir(), @@ -170,20 +170,20 @@ func TestZoomAndDateReadTheChat(t *testing.T) { } func TestClipKeepsTheHeadAndTheTail(t *testing.T) { - short := strings.Repeat("a", clipCharacters) - require.Equal(t, short, clip(short)) + short := strings.Repeat("a", toolClipCharacters) + require.Equal(t, short, toolClip(short)) - text := strings.Repeat("h", clipCharacters/2) + strings.Repeat("é", 5000) + strings.Repeat("t", clipCharacters/2) - require.Equal(t, strings.Repeat("h", clipCharacters/2)+"\n[... 10000 bytes cut ...]\n"+strings.Repeat("t", clipCharacters/2), clip(text)) + text := strings.Repeat("h", toolClipCharacters/2) + strings.Repeat("é", 5000) + strings.Repeat("t", toolClipCharacters/2) + require.Equal(t, strings.Repeat("h", toolClipCharacters/2)+"\n[... 10000 bytes cut ...]\n"+strings.Repeat("t", toolClipCharacters/2), toolClip(text)) - half := strings.Repeat("é", clipCharacters/2) - clipped := clip(strings.Repeat("é", clipCharacters+2)) + half := strings.Repeat("é", toolClipCharacters/2) + clipped := toolClip(strings.Repeat("é", toolClipCharacters+2)) require.True(t, utf8.ValidString(clipped)) require.Equal(t, half+"\n[... 4 bytes cut ...]\n"+half, clipped) } func TestHeadTailKeepsTheEndsOfALongOutput(t *testing.T) { - output := &headTail{} + output := &ToolHeadTail{} for n := range 1000 { _, err := fmt.Fprintf(output, "%04d%s\n", n, strings.Repeat("x", 995)) require.NoError(t, err) @@ -192,19 +192,19 @@ func TestHeadTailKeepsTheEndsOfALongOutput(t *testing.T) { text := output.String() 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-clipCharacters)) - require.Equal(t, clipCharacters+len(fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-clipCharacters)), len(text)) + 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)) } -// recordingLog 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 // records whether the marker file exists. -type recordingLog struct { +type ToolRecordingLog struct { mutex sync.Mutex marker string events []string } -func (r *recordingLog) Append(kind, text string) (Message, error) { +func (r *ToolRecordingLog) Append(kind, text string) (Message, error) { r.mutex.Lock() defer r.mutex.Unlock() @@ -213,7 +213,7 @@ func (r *recordingLog) Append(kind, text string) (Message, error) { return Message{}, nil } -func (r *recordingLog) Flush(ctx context.Context) error { +func (r *ToolRecordingLog) Flush(ctx context.Context) error { r.mutex.Lock() defer r.mutex.Unlock() @@ -228,7 +228,7 @@ func (r *recordingLog) Flush(ctx context.Context) error { return ctx.Err() } -func (r *recordingLog) Events() []string { +func (r *ToolRecordingLog) Events() []string { r.mutex.Lock() defer r.mutex.Unlock() @@ -237,7 +237,7 @@ func (r *recordingLog) Events() []string { // newTestToolbox returns a toolbox with log, an empty chat, and the working // directory dir. -func newTestToolbox(t *testing.T, log chatLog, dir string) *toolbox { +func newTestToolbox(t *testing.T, log ToolLog, dir string) *Toolbox { t.Helper() memory, err := openMemory(t.Context(), t.TempDir(), newTestClock().Now, newFakeSummarizer()) @@ -247,7 +247,7 @@ func newTestToolbox(t *testing.T, log chatLog, dir string) *toolbox { require.NoError(t, memory.Close()) }) - return &toolbox{ + return &Toolbox{ log: log, chat: memory, workdir: dir,