|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestExtractTitle(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + content string |
| 13 | + filename string |
| 14 | + want string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "extract from h1 heading", |
| 18 | + content: "# My Issue Title\n\nSome body content", |
| 19 | + filename: "issue.md", |
| 20 | + want: "My Issue Title", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "fallback to filename without extension", |
| 24 | + content: "No heading here\nJust body text", |
| 25 | + filename: "my-issue.md", |
| 26 | + want: "my-issue", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "skip h2 heading", |
| 30 | + content: "## This is h2\n# This is h1", |
| 31 | + filename: "test.md", |
| 32 | + want: "This is h1", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "heading with leading spaces", |
| 36 | + content: " # Trimmed Title\nBody", |
| 37 | + filename: "test.md", |
| 38 | + want: "Trimmed Title", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "empty file uses filename", |
| 42 | + content: "", |
| 43 | + filename: "empty-doc.md", |
| 44 | + want: "empty-doc", |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + for _, tt := range tests { |
| 49 | + t.Run(tt.name, func(t *testing.T) { |
| 50 | + got := extractTitle(tt.content, tt.filename) |
| 51 | + if got != tt.want { |
| 52 | + t.Errorf("extractTitle() = %q, want %q", got, tt.want) |
| 53 | + } |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestReadDocsAsIssues(t *testing.T) { |
| 59 | + dir := t.TempDir() |
| 60 | + |
| 61 | + t.Run("reads markdown files as issues", func(t *testing.T) { |
| 62 | + os.WriteFile(filepath.Join(dir, "issue1.md"), []byte("# First Issue\n\nBody of first issue"), 0644) |
| 63 | + os.WriteFile(filepath.Join(dir, "issue2.md"), []byte("# Second Issue\n\nBody of second issue"), 0644) |
| 64 | + |
| 65 | + issues, err := ReadDocsAsIssues(dir) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("ReadDocsAsIssues() error = %v", err) |
| 68 | + } |
| 69 | + if len(issues) != 2 { |
| 70 | + t.Fatalf("expected 2 issues, got %d", len(issues)) |
| 71 | + } |
| 72 | + |
| 73 | + titles := map[string]bool{issues[0].Title: true, issues[1].Title: true} |
| 74 | + if !titles["First Issue"] || !titles["Second Issue"] { |
| 75 | + t.Errorf("expected titles 'First Issue' and 'Second Issue', got %q and %q", issues[0].Title, issues[1].Title) |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("returns error for empty directory", func(t *testing.T) { |
| 80 | + emptyDir := t.TempDir() |
| 81 | + _, err := ReadDocsAsIssues(emptyDir) |
| 82 | + if err == nil { |
| 83 | + t.Error("expected error for empty directory") |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("ignores non-markdown files", func(t *testing.T) { |
| 88 | + mixedDir := t.TempDir() |
| 89 | + os.WriteFile(filepath.Join(mixedDir, "doc.md"), []byte("# Doc\nContent"), 0644) |
| 90 | + os.WriteFile(filepath.Join(mixedDir, "ignore.txt"), []byte("Not a doc"), 0644) |
| 91 | + |
| 92 | + issues, err := ReadDocsAsIssues(mixedDir) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("ReadDocsAsIssues() error = %v", err) |
| 95 | + } |
| 96 | + if len(issues) != 1 { |
| 97 | + t.Fatalf("expected 1 issue, got %d", len(issues)) |
| 98 | + } |
| 99 | + if issues[0].Title != "Doc" { |
| 100 | + t.Errorf("expected title 'Doc', got %q", issues[0].Title) |
| 101 | + } |
| 102 | + }) |
| 103 | +} |
0 commit comments