|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/dimitar-grigorov/mcp-file-tools/internal/encoding" |
| 11 | +) |
| 12 | + |
| 13 | +func TestHandleReadMultipleFiles_Success(t *testing.T) { |
| 14 | + tempDir := t.TempDir() |
| 15 | + h := NewHandler([]string{tempDir}) |
| 16 | + |
| 17 | + file1 := filepath.Join(tempDir, "file1.txt") |
| 18 | + file2 := filepath.Join(tempDir, "file2.txt") |
| 19 | + os.WriteFile(file1, []byte("content1"), 0644) |
| 20 | + os.WriteFile(file2, []byte("content2"), 0644) |
| 21 | + |
| 22 | + input := ReadMultipleFilesInput{Paths: []string{file1, file2}} |
| 23 | + result, output, err := h.HandleReadMultipleFiles(context.Background(), nil, input) |
| 24 | + |
| 25 | + if err != nil || result.IsError { |
| 26 | + t.Fatal("expected success") |
| 27 | + } |
| 28 | + if output.SuccessCount != 2 || output.ErrorCount != 0 { |
| 29 | + t.Errorf("expected 2 successes, got %d successes, %d errors", output.SuccessCount, output.ErrorCount) |
| 30 | + } |
| 31 | + if output.Results[0].Content != "content1" || output.Results[1].Content != "content2" { |
| 32 | + t.Errorf("unexpected content") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestHandleReadMultipleFiles_PartialFailure(t *testing.T) { |
| 37 | + tempDir := t.TempDir() |
| 38 | + h := NewHandler([]string{tempDir}) |
| 39 | + |
| 40 | + file1 := filepath.Join(tempDir, "exists.txt") |
| 41 | + file2 := filepath.Join(tempDir, "nonexistent.txt") |
| 42 | + os.WriteFile(file1, []byte("content1"), 0644) |
| 43 | + |
| 44 | + input := ReadMultipleFilesInput{Paths: []string{file1, file2}} |
| 45 | + result, output, _ := h.HandleReadMultipleFiles(context.Background(), nil, input) |
| 46 | + |
| 47 | + if result.IsError { |
| 48 | + t.Error("expected partial success, not tool error") |
| 49 | + } |
| 50 | + if output.SuccessCount != 1 || output.ErrorCount != 1 { |
| 51 | + t.Errorf("expected 1 success, 1 error, got %d/%d", output.SuccessCount, output.ErrorCount) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestHandleReadMultipleFiles_EmptyPaths(t *testing.T) { |
| 56 | + h := NewHandler([]string{t.TempDir()}) |
| 57 | + result, _, _ := h.HandleReadMultipleFiles(context.Background(), nil, ReadMultipleFilesInput{Paths: []string{}}) |
| 58 | + if !result.IsError { |
| 59 | + t.Error("expected error for empty paths") |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestHandleReadMultipleFiles_WithEncoding(t *testing.T) { |
| 64 | + tempDir := t.TempDir() |
| 65 | + h := NewHandler([]string{tempDir}) |
| 66 | + |
| 67 | + enc, _ := encoding.Get("cp1251") |
| 68 | + cp1251Bytes, _ := enc.NewEncoder().Bytes([]byte("Здравей свят!")) |
| 69 | + file1 := filepath.Join(tempDir, "cyrillic.txt") |
| 70 | + os.WriteFile(file1, cp1251Bytes, 0644) |
| 71 | + |
| 72 | + input := ReadMultipleFilesInput{Paths: []string{file1}, Encoding: "cp1251"} |
| 73 | + _, output, _ := h.HandleReadMultipleFiles(context.Background(), nil, input) |
| 74 | + |
| 75 | + if !strings.Contains(output.Results[0].Content, "Здравей свят!") { |
| 76 | + t.Errorf("expected Cyrillic content, got %q", output.Results[0].Content) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestHandleReadMultipleFiles_PathOutsideAllowed(t *testing.T) { |
| 81 | + tempDir := t.TempDir() |
| 82 | + h := NewHandler([]string{tempDir}) |
| 83 | + |
| 84 | + input := ReadMultipleFilesInput{Paths: []string{filepath.Join(tempDir, "..", "..", "etc", "passwd")}} |
| 85 | + _, output, _ := h.HandleReadMultipleFiles(context.Background(), nil, input) |
| 86 | + |
| 87 | + if !strings.Contains(output.Results[0].Error, "access denied") { |
| 88 | + t.Errorf("expected 'access denied' error, got %q", output.Results[0].Error) |
| 89 | + } |
| 90 | +} |
0 commit comments