|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestHandleTree_BasicOutput(t *testing.T) { |
| 12 | + tempDir := t.TempDir() |
| 13 | + h := NewHandler([]string{tempDir}) |
| 14 | + |
| 15 | + // Create structure: src/handler/read.go, src/server.go, README.md |
| 16 | + os.MkdirAll(filepath.Join(tempDir, "src", "handler"), 0755) |
| 17 | + os.WriteFile(filepath.Join(tempDir, "src", "handler", "read.go"), []byte(""), 0644) |
| 18 | + os.WriteFile(filepath.Join(tempDir, "src", "server.go"), []byte(""), 0644) |
| 19 | + os.WriteFile(filepath.Join(tempDir, "README.md"), []byte(""), 0644) |
| 20 | + |
| 21 | + input := TreeInput{Path: tempDir} |
| 22 | + _, output, err := h.HandleTree(context.Background(), nil, input) |
| 23 | + |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("unexpected error: %v", err) |
| 26 | + } |
| 27 | + if output.FileCount != 3 { |
| 28 | + t.Errorf("expected 3 files, got %d", output.FileCount) |
| 29 | + } |
| 30 | + if output.DirCount != 2 { |
| 31 | + t.Errorf("expected 2 dirs, got %d", output.DirCount) |
| 32 | + } |
| 33 | + // Check indented format |
| 34 | + if !strings.Contains(output.Tree, "src/") { |
| 35 | + t.Error("expected 'src/' in output") |
| 36 | + } |
| 37 | + if !strings.Contains(output.Tree, " handler/") { |
| 38 | + t.Error("expected indented 'handler/' in output") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestHandleTree_MaxDepth(t *testing.T) { |
| 43 | + tempDir := t.TempDir() |
| 44 | + h := NewHandler([]string{tempDir}) |
| 45 | + |
| 46 | + os.MkdirAll(filepath.Join(tempDir, "a", "b", "c"), 0755) |
| 47 | + os.WriteFile(filepath.Join(tempDir, "a", "b", "c", "deep.txt"), []byte(""), 0644) |
| 48 | + |
| 49 | + input := TreeInput{Path: tempDir, MaxDepth: 2} |
| 50 | + _, output, _ := h.HandleTree(context.Background(), nil, input) |
| 51 | + |
| 52 | + // Should see a/ and a/b/ but not a/b/c/ |
| 53 | + if !strings.Contains(output.Tree, "a/") { |
| 54 | + t.Error("expected 'a/' at depth 1") |
| 55 | + } |
| 56 | + if !strings.Contains(output.Tree, " b/") { |
| 57 | + t.Error("expected 'b/' at depth 2") |
| 58 | + } |
| 59 | + if strings.Contains(output.Tree, "c/") { |
| 60 | + t.Error("should NOT see 'c/' at depth 3 when maxDepth=2") |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestHandleTree_MaxFiles(t *testing.T) { |
| 65 | + tempDir := t.TempDir() |
| 66 | + h := NewHandler([]string{tempDir}) |
| 67 | + |
| 68 | + for i := 0; i < 20; i++ { |
| 69 | + os.WriteFile(filepath.Join(tempDir, string(rune('a'+i))+".txt"), []byte(""), 0644) |
| 70 | + } |
| 71 | + |
| 72 | + input := TreeInput{Path: tempDir, MaxFiles: 5} |
| 73 | + _, output, _ := h.HandleTree(context.Background(), nil, input) |
| 74 | + |
| 75 | + if output.FileCount+output.DirCount > 5 { |
| 76 | + t.Errorf("expected max 5 entries, got %d", output.FileCount+output.DirCount) |
| 77 | + } |
| 78 | + if !output.Truncated { |
| 79 | + t.Error("expected truncated=true") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestHandleTree_DirsOnly(t *testing.T) { |
| 84 | + tempDir := t.TempDir() |
| 85 | + h := NewHandler([]string{tempDir}) |
| 86 | + |
| 87 | + os.MkdirAll(filepath.Join(tempDir, "src"), 0755) |
| 88 | + os.WriteFile(filepath.Join(tempDir, "file.txt"), []byte(""), 0644) |
| 89 | + os.WriteFile(filepath.Join(tempDir, "src", "code.go"), []byte(""), 0644) |
| 90 | + |
| 91 | + input := TreeInput{Path: tempDir, DirsOnly: true} |
| 92 | + _, output, _ := h.HandleTree(context.Background(), nil, input) |
| 93 | + |
| 94 | + if output.FileCount != 0 { |
| 95 | + t.Errorf("expected 0 files with dirsOnly, got %d", output.FileCount) |
| 96 | + } |
| 97 | + if output.DirCount != 1 { |
| 98 | + t.Errorf("expected 1 dir, got %d", output.DirCount) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestHandleTree_Exclude(t *testing.T) { |
| 103 | + tempDir := t.TempDir() |
| 104 | + h := NewHandler([]string{tempDir}) |
| 105 | + |
| 106 | + os.MkdirAll(filepath.Join(tempDir, "node_modules"), 0755) |
| 107 | + os.MkdirAll(filepath.Join(tempDir, "src"), 0755) |
| 108 | + os.WriteFile(filepath.Join(tempDir, "node_modules", "pkg.js"), []byte(""), 0644) |
| 109 | + |
| 110 | + input := TreeInput{Path: tempDir, Exclude: []string{"node_modules"}} |
| 111 | + _, output, _ := h.HandleTree(context.Background(), nil, input) |
| 112 | + |
| 113 | + if strings.Contains(output.Tree, "node_modules") { |
| 114 | + t.Error("expected node_modules to be excluded") |
| 115 | + } |
| 116 | + if !strings.Contains(output.Tree, "src/") { |
| 117 | + t.Error("expected src/ to be present") |
| 118 | + } |
| 119 | +} |
0 commit comments