How to Test
Testing is important. See the docs.
Setup
- Install Go
- Run
go test ./...
Example
package main
import "fmt"
func main() { fmt.Println("hi") }
| Input | Output |
|---|---|
| 1 | one |
package reader_test import ( "strings" "testing" "github.com/lukaszraczylo/mcp-readitall/internal/reader" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // ============================================================================= // HTMLToMarkdown // ============================================================================= func TestHTMLToMarkdown_BasicElements(t *testing.T) { tests := []struct { name string html string domain string want []string // substrings that must all appear in the output, in order // optional: substrings that must NOT appear notWant []string }{ // ===== GOOD CASES ===== { name: "headings hierarchy", html: `
This is bold and italic text.
`, want: []string{"This is **bold** and _italic_ text."}, }, { name: "unordered list", html: `Use fmt.Println to print.
package main
func main() {}
`,
want: []string{"```", "package main", "func main()"},
},
{
name: "table",
html: `| Name | Age |
|---|---|
| Alice | 30 |
| Bob | 25 |
before
after
`, want: []string{"before", "---", "after"}, }, // ===== RELATIVE URLS ===== { name: "absolute links stay absolute", html: `other`, domain: "https://this.test", want: []string{`[other](https://other.test/x)`}, }, { name: "relative link is resolved against domain", html: `about`, domain: "https://this.test", want: []string{`[about](https://this.test/about)`}, }, { name: "relative image is resolved", html: `
`,
domain: "https://this.test",
want: []string{``},
},
{
name: "no domain leaves relative links alone",
html: `about`,
domain: "",
want: []string{`[about](/about)`},
},
// ===== STRIKETHROUGH =====
{
name: "strikethrough",
html: `old removed new
visible
also visible
`, want: []string{"visible", "also visible"}, notWant: []string{ "alert(1)", "p{}", }, }, { name: "html comments are stripped", html: `before
after
`, want: []string{"before", "after"}, notWant: []string{ "a comment", }, }, { name: "nested tags are flattened correctly", html: `outer with nested emphasis
5 < 10 & 10 > 5
`, // In EscapeModeSmart the library decodes & to & but // preserves </> as entities so the output cannot be // interpreted as raw HTML/Markdown by downstream renderers. want: []string{"5 < 10 & 10 > 5"}, }, { name: "deeply nested lists", html: `line one
line two
line four
Testing is important. See the docs.
go test ./...package main
import "fmt"
func main() { fmt.Println("hi") }
| Input | Output |
|---|---|
| 1 | one |
safe
also safe
` md, err := reader.HTMLToMarkdown(html, "") require.NoError(t, err) // Output should not contain the malicious payload assert.NotContains(t, md, "ABCDEFG") assert.NotContains(t, md, "evil.test") assert.NotContains(t, md, "document.cookie") // But it should still contain the visible text assert.Contains(t, strings.ToLower(md), "safe") assert.Contains(t, strings.ToLower(md), "also safe") }