test: add end-to-end integration test for HTML filter to image extraction

This commit is contained in:
Mortdecai
2026-04-07 20:14:27 -04:00
parent b13bd1d71c
commit abf8feb229
+72
View File
@@ -0,0 +1,72 @@
// lib/parse/integration_test.go
package parse
import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestIntegration_HTMLFilterToExtractImages(t *testing.T) {
// Skip if html-kitty filter or w3m not available
filterPath := filepath.Join(os.Getenv("HOME"),
".local/libexec/aerc/filters/html-kitty")
if _, err := os.Stat(filterPath); err != nil {
t.Skip("html-kitty filter not found:", err)
}
if _, err := exec.LookPath("w3m"); err != nil {
t.Skip("w3m not found:", err)
}
// HTML with a data: URI image
html := `<html><body>
<p>Hello World</p>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" alt="tiny red pixel">
<p>Goodbye</p>
</body></html>`
cmd := exec.Command("python3", filterPath)
cmd.Stdin = strings.NewReader(html)
cmd.Env = append(os.Environ(), "COLUMNS=80")
out, err := cmd.Output()
if err != nil {
t.Fatalf("filter failed: %v", err)
}
// Extract images from filter output
cleaned, images := ExtractImages(strings.NewReader(string(out)))
if len(images) == 0 {
t.Fatal("expected at least 1 image from filter output")
}
// Verify image file exists on disk
if _, err := os.Stat(images[0].Path); err != nil {
t.Errorf("image file not found: %s", images[0].Path)
}
// Verify alt text
if images[0].Alt != "tiny red pixel" {
t.Errorf("alt = %q, want 'tiny red pixel'", images[0].Alt)
}
// Verify cleaned output has placeholder
cleanedBytes, _ := io.ReadAll(cleaned)
cleanedStr := string(cleanedBytes)
if !strings.Contains(cleanedStr, "\x00IMG:0\x00") {
t.Error("cleaned output missing placeholder")
}
// Verify text content preserved
if !strings.Contains(cleanedStr, "Hello World") {
t.Error("text content 'Hello World' lost")
}
// Clean up temp files
for _, img := range images {
dir := filepath.Dir(img.Path)
os.RemoveAll(dir)
}
}