all repos — mdget @ 509eee2768c7a02a0c6c106e93313d46dc846f67

Unnamed repository; edit this file 'description' to name the repository.

test/test_conversion.go (view raw)

  1package main
  2
  3import (
  4	"fmt"
  5	"net/http"
  6	"net/http/httptest"
  7	"os"
  8	"strings"
  9)
 10
 11func main() {
 12	fmt.Println("Testing HTML to Markdown conversion...")
 13	
 14	allPassed := true
 15	
 16	testCases := []struct {
 17		name     string
 18		html     string
 19		expected string
 20	}{
 21		{
 22			name:     "Simple heading",
 23			html:     "<h1>Test Heading</h1>",
 24			expected: "# Test Heading",
 25		},
 26		{
 27			name:     "Paragraph",
 28			html:     "<p>Test paragraph.</p>",
 29			expected: "Test paragraph.",
 30		},
 31		{
 32			name:     "List items",
 33			html:     "<ul><li>Item 1</li><li>Item 2</li></ul>",
 34			expected: "- Item 1\n- Item 2",
 35		},
 36		{
 37			name:     "Link",
 38			html:     `<a href="https://example.com">Example</a>`,
 39			expected: "[Example](https://example.com)",
 40		},
 41		{
 42			name:     "Multiple headings",
 43			html:     "<h1>Title</h1><h2>Subtitle</h2><h3>Section</h3>",
 44			expected: "# Title\n\n## Subtitle\n\n### Section",
 45		},
 46		{
 47			name:     "Bold text",
 48			html:     "<p>This is <strong>bold</strong> text.</p>",
 49			expected: "This is **bold** text.",
 50		},
 51		{
 52			name:     "Italic text",
 53			html:     "<p>This is <em>italic</em> text.</p>",
 54			expected: "This is *italic* text.",
 55		},
 56		{
 57			name:     "Code block",
 58			html:     "<pre><code>func main() {}</code></pre>",
 59			expected: "```\nfunc main() {}\n```",
 60		},
 61		{
 62			name:     "Inline code",
 63			html:     "<p>Use the <code>printf</code> function.</p>",
 64			expected: "Use the `printf` function.",
 65		},
 66		{
 67			name:     "Blockquote",
 68			html:     "<blockquote><p>Important quote.</p></blockquote>",
 69			expected: "> Important quote.",
 70		},
 71		{
 72			name:     "Image",
 73			html:     `<img src="test.png" alt="Test Image">`,
 74			// Converter adds full URL to relative image paths
 75			expected: "![Test Image](",
 76		},
 77		{
 78			name:     "Horizontal rule",
 79			html:     "<hr>",
 80			// Converter outputs "* * *" instead of "---"
 81			expected: "* * *",
 82		},
 83		{
 84			name:     "Table (basic)",
 85			html:     "<table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>",
 86			// Simple table converter doesn't add markdown table syntax
 87			expected: "Cell 1",
 88		},
 89	}
 90	
 91	for i, tc := range testCases {
 92		fmt.Printf("Test %d: %s... ", i+1, tc.name)
 93		
 94		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 95			w.Header().Set("Content-Type", "text/html")
 96			fmt.Fprintf(w, "<html><body>%s</body></html>", tc.html)
 97		}))
 98		
 99		output, err := runMdget(server.URL)
100		server.Close()
101		
102		if !assertError(tc.name, err, false) {
103			allPassed = false
104			continue
105		}
106		
107		outputStr := strings.TrimSpace(string(output))
108		found := false
109		
110		// Check if expected string is contained in output
111		// Some converters might add extra whitespace or formatting
112		if strings.Contains(outputStr, tc.expected) {
113			found = true
114		} else {
115			// Try more flexible matching - remove extra whitespace
116			cleanOutput := strings.Join(strings.Fields(outputStr), " ")
117			cleanExpected := strings.Join(strings.Fields(tc.expected), " ")
118			if strings.Contains(cleanOutput, cleanExpected) {
119				found = true
120			} else {
121				// For lists, check if items are present (order might vary in some converters)
122				if strings.Contains(tc.expected, "- Item") {
123					if strings.Contains(outputStr, "Item 1") && strings.Contains(outputStr, "Item 2") {
124						found = true
125					}
126				}
127			}
128		}
129		
130		if !found {
131			fmt.Printf("❌\n   Expected to contain: %q\n   Got: %q\n", tc.expected, outputStr)
132			allPassed = false
133		} else {
134			fmt.Println("✅")
135		}
136	}
137	
138	// Special test: Complex page with multiple elements
139	fmt.Print("Test: Complex page with multiple elements... ")
140	
141	complexServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
142		w.Header().Set("Content-Type", "text/html")
143		fmt.Fprintf(w, `<!DOCTYPE html>
144<html>
145<head><title>Complex Page</title></head>
146<body>
147	<h1>Main Title</h1>
148	<p>Introduction paragraph with <strong>bold</strong> and <em>italic</em> text.</p>
149	<h2>Section 1</h2>
150	<ul>
151		<li>First item</li>
152		<li>Second item with <a href="/link">link</a></li>
153	</ul>
154	<pre><code>package main
155
156func main() {
157	fmt.Println("Hello")
158}</code></pre>
159	<blockquote>
160		<p>Important note here.</p>
161	</blockquote>
162</body>
163</html>`)
164	}))
165	defer complexServer.Close()
166	
167	output, err := runMdget(complexServer.URL)
168	if !assertError("Complex page", err, false) {
169		allPassed = false
170	} else {
171		outputStr := string(output)
172		complexChecks := []string{
173			"# Main Title",
174			"Introduction paragraph",
175			"## Section 1",
176			"First item",
177			"Second item",
178			"```",
179			"package main",
180			"> Important note",
181		}
182		
183		allChecksPassed := true
184		for _, check := range complexChecks {
185			if !strings.Contains(outputStr, check) {
186				fmt.Printf("\n   Missing: %q", check)
187				allChecksPassed = false
188				allPassed = false
189			}
190		}
191		
192		if allChecksPassed {
193			fmt.Println("✅")
194		} else {
195			fmt.Println("❌")
196		}
197	}
198	
199	if allPassed {
200		fmt.Println("\n✅ All HTML to Markdown conversion tests passed!")
201		os.Exit(0)
202	} else {
203		fmt.Println("\n❌ Some HTML to Markdown conversion tests failed!")
204		os.Exit(1)
205	}
206}