206 lines
4.6 KiB
Go
206 lines
4.6 KiB
Go
package reporter
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.nakama.town/fmartingr/dharma/pkg/scraper"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
format string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Pretty format",
|
|
format: "pretty",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "JSON format",
|
|
format: "json",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "CSV format",
|
|
format: "csv",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Unsupported format",
|
|
format: "xml",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := New(tt.format)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !tt.wantErr && got == nil {
|
|
t.Errorf("New() = nil, want non-nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestJSONReporter_Generate(t *testing.T) {
|
|
// Create test results
|
|
results := &scraper.Results{
|
|
BaseURL: "https://example.com",
|
|
Errors: []scraper.Result{
|
|
{
|
|
URL: "https://example.com/error",
|
|
SourceURL: "https://example.com",
|
|
Status: 404,
|
|
Error: "HTTP Error: 404 Not Found",
|
|
Type: "link",
|
|
},
|
|
},
|
|
Successes: []scraper.Result{
|
|
{
|
|
URL: "https://example.com/success",
|
|
SourceURL: "https://example.com",
|
|
Status: 200,
|
|
Type: "link",
|
|
},
|
|
},
|
|
Total: 2,
|
|
}
|
|
|
|
// Create reporter and buffer
|
|
reporter := &JSONReporter{}
|
|
buf := &bytes.Buffer{}
|
|
|
|
// Generate report
|
|
if err := reporter.Generate(results, buf); err != nil {
|
|
t.Fatalf("Generate() error = %v", err)
|
|
}
|
|
|
|
// Parse output
|
|
var output scraper.Results
|
|
if err := json.Unmarshal(buf.Bytes(), &output); err != nil {
|
|
t.Fatalf("Failed to parse JSON: %v", err)
|
|
}
|
|
|
|
// Verify output
|
|
if output.BaseURL != results.BaseURL {
|
|
t.Errorf("BaseURL = %v, want %v", output.BaseURL, results.BaseURL)
|
|
}
|
|
if len(output.Errors) != len(results.Errors) {
|
|
t.Errorf("Errors count = %v, want %v", len(output.Errors), len(results.Errors))
|
|
}
|
|
if len(output.Successes) != len(results.Successes) {
|
|
t.Errorf("Successes count = %v, want %v", len(output.Successes), len(results.Successes))
|
|
}
|
|
if output.Total != results.Total {
|
|
t.Errorf("Total = %v, want %v", output.Total, results.Total)
|
|
}
|
|
}
|
|
|
|
func TestCSVReporter_Generate(t *testing.T) {
|
|
// Create test results
|
|
results := &scraper.Results{
|
|
BaseURL: "https://example.com",
|
|
Errors: []scraper.Result{
|
|
{
|
|
URL: "https://example.com/error",
|
|
SourceURL: "https://example.com",
|
|
Status: 404,
|
|
Error: "HTTP Error: 404 Not Found",
|
|
Type: "link",
|
|
},
|
|
},
|
|
Successes: []scraper.Result{},
|
|
Total: 1,
|
|
}
|
|
|
|
// Create reporter and buffer
|
|
reporter := &CSVReporter{}
|
|
buf := &bytes.Buffer{}
|
|
|
|
// Generate report
|
|
if err := reporter.Generate(results, buf); err != nil {
|
|
t.Fatalf("Generate() error = %v", err)
|
|
}
|
|
|
|
// Verify output
|
|
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
|
|
if len(lines) != 2 { // Header + 1 error
|
|
t.Errorf("Expected 2 lines, got %d", len(lines))
|
|
}
|
|
|
|
// Check header
|
|
expectedHeader := "Status,Type,URL,Source URL,Error"
|
|
if lines[0] != expectedHeader {
|
|
t.Errorf("Header = %v, want %v", lines[0], expectedHeader)
|
|
}
|
|
}
|
|
|
|
func TestPrettyReporter_Generate(t *testing.T) {
|
|
// Test with errors
|
|
results := &scraper.Results{
|
|
BaseURL: "https://example.com",
|
|
Errors: []scraper.Result{
|
|
{
|
|
URL: "https://example.com/error",
|
|
SourceURL: "https://example.com",
|
|
Status: 404,
|
|
Error: "HTTP Error: 404 Not Found",
|
|
Type: "link",
|
|
},
|
|
},
|
|
Successes: []scraper.Result{
|
|
{
|
|
URL: "https://example.com/success",
|
|
SourceURL: "https://example.com",
|
|
Status: 200,
|
|
Type: "link",
|
|
},
|
|
},
|
|
Total: 2,
|
|
}
|
|
|
|
// Create reporter and buffer
|
|
reporter := &PrettyReporter{}
|
|
buf := &bytes.Buffer{}
|
|
|
|
// Generate report
|
|
if err := reporter.Generate(results, buf); err != nil {
|
|
t.Fatalf("Generate() error = %v", err)
|
|
}
|
|
|
|
// Check that output contains key sections
|
|
output := buf.String()
|
|
if !strings.Contains(output, "Website scan report for") {
|
|
t.Error("Output doesn't contain report title")
|
|
}
|
|
if !strings.Contains(output, "Internal Errors:") {
|
|
t.Error("Output doesn't contain errors section")
|
|
}
|
|
|
|
// Test with no errors
|
|
results = &scraper.Results{
|
|
BaseURL: "https://example.com",
|
|
Errors: []scraper.Result{},
|
|
Successes: []scraper.Result{},
|
|
Total: 0,
|
|
}
|
|
|
|
buf = &bytes.Buffer{}
|
|
if err := reporter.Generate(results, buf); err != nil {
|
|
t.Fatalf("Generate() error = %v", err)
|
|
}
|
|
|
|
output = buf.String()
|
|
if !strings.Contains(output, "No errors found") {
|
|
t.Error("Output doesn't contain 'No errors found' message")
|
|
}
|
|
}
|