87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"search-cli/adapters"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestFormatResults(t *testing.T) {
|
||
|
|
results := &adapters.SearchResponse{
|
||
|
|
Results: []adapters.SearchResult{
|
||
|
|
{
|
||
|
|
URL: "https://example.com",
|
||
|
|
Content: "Test content",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
// Capture stdout
|
||
|
|
old := os.Stdout
|
||
|
|
r, w, _ := os.Pipe()
|
||
|
|
os.Stdout = w
|
||
|
|
|
||
|
|
formatResults(results)
|
||
|
|
|
||
|
|
w.Close()
|
||
|
|
os.Stdout = old
|
||
|
|
|
||
|
|
var buf bytes.Buffer
|
||
|
|
buf.ReadFrom(r)
|
||
|
|
output := buf.String()
|
||
|
|
|
||
|
|
if !strings.Contains(output, "https://example.com") {
|
||
|
|
t.Error("Expected output to contain URL")
|
||
|
|
}
|
||
|
|
if !strings.Contains(output, "Test content") {
|
||
|
|
t.Error("Expected output to contain content")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFormatExtractResults(t *testing.T) {
|
||
|
|
results := &adapters.ExtractResponse{
|
||
|
|
Results: []adapters.ExtractResult{
|
||
|
|
{
|
||
|
|
URL: "https://example.com",
|
||
|
|
RawContent: "Test content",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
FailedResults: []adapters.FailedResult{
|
||
|
|
{
|
||
|
|
URL: "https://failed.com",
|
||
|
|
Error: "Failed to extract",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
// Capture stdout
|
||
|
|
old := os.Stdout
|
||
|
|
r, w, _ := os.Pipe()
|
||
|
|
os.Stdout = w
|
||
|
|
|
||
|
|
formatExtractResults(results)
|
||
|
|
|
||
|
|
w.Close()
|
||
|
|
os.Stdout = old
|
||
|
|
|
||
|
|
var buf bytes.Buffer
|
||
|
|
buf.ReadFrom(r)
|
||
|
|
output := buf.String()
|
||
|
|
|
||
|
|
if !strings.Contains(output, "https://example.com") {
|
||
|
|
t.Error("Expected output to contain URL")
|
||
|
|
}
|
||
|
|
if !strings.Contains(output, "Test content") {
|
||
|
|
t.Error("Expected output to contain content")
|
||
|
|
}
|
||
|
|
if !strings.Contains(output, "Failed URLs") {
|
||
|
|
t.Error("Expected output to contain failed URLs section")
|
||
|
|
}
|
||
|
|
if !strings.Contains(output, "https://failed.com") {
|
||
|
|
t.Error("Expected output to contain failed URL")
|
||
|
|
}
|
||
|
|
}
|