write_test.go

 1package tools
 2
 3import (
 4	"context"
 5	"encoding/json"
 6	"os"
 7	"path/filepath"
 8	"testing"
 9	"time"
10
11	"charm.land/fantasy"
12	"github.com/stretchr/testify/require"
13)
14
15type mockFileTrackerService struct{}
16
17func (m mockFileTrackerService) RecordRead(ctx context.Context, sessionID, path string) {}
18
19func (m mockFileTrackerService) LastReadTime(ctx context.Context, sessionID, path string) time.Time {
20	return time.Now()
21}
22
23func (m mockFileTrackerService) ListReadFiles(ctx context.Context, sessionID string) ([]string, error) {
24	return nil, nil
25}
26
27func TestWriteToolWritesEmptyNewFile(t *testing.T) {
28	t.Parallel()
29
30	workingDir := t.TempDir()
31	ctx := context.WithValue(context.Background(), SessionIDContextKey, "test-session")
32
33	tool := NewWriteTool(nil, &mockPermissionService{}, &mockHistoryService{}, mockFileTrackerService{}, workingDir)
34
35	input, err := json.Marshal(WriteParams{FilePath: "empty.txt", Content: ""})
36	require.NoError(t, err)
37
38	resp, err := tool.Run(ctx, fantasy.ToolCall{
39		ID:    "test-call",
40		Name:  WriteToolName,
41		Input: string(input),
42	})
43	require.NoError(t, err)
44	require.False(t, resp.IsError)
45
46	b, err := os.ReadFile(filepath.Join(workingDir, "empty.txt"))
47	require.NoError(t, err)
48	require.Equal(t, "", string(b))
49}