package tomd import ( "os" "path/filepath" "strings" "testing" "github.com/Warky-Devs/go-mdtopdf-helper.git/internal/document" "github.com/Warky-Devs/go-mdtopdf-helper.git/internal/engine" ) const sample = "# Title\n\nSome **bold**, *italic* and `code` with a [link](http://example.com).\n\n" + "## Lists\n\n- one\n- two\n - nested\n\n1. first\n2. second\n\n" + "```\nfunc main() {\n run()\n}\n```\n\n| a | b |\n|---|---|\n| 1 | 2 |\n" func requireContains(t *testing.T, got string, wants ...string) { t.Helper() for _, want := range wants { if !strings.Contains(got, want) { t.Errorf("output missing %q\n--- output ---\n%s", want, got) } } } func TestDOCXRoundTrip(t *testing.T) { data, err := document.DOCX([]byte(sample)) if err != nil { t.Fatal(err) } md, err := DOCX(data) if err != nil { t.Fatal(err) } requireContains(t, string(md), "# Title", "## Lists", "**bold**", "*italic*", "`code`", "[link](http://example.com)", "- one\n- two\n - nested", "1. first\n1. second", "```\nfunc main() {\n run()\n}\n```", "| a | b |\n| --- | --- |\n| 1 | 2 |") } func TestHTMLRoundTrip(t *testing.T) { md, err := HTML(document.HTML([]byte(sample))) if err != nil { t.Fatal(err) } requireContains(t, string(md), "# Title", "## Lists", "**bold**", "*italic*", "`code`", "[link](http://example.com)", "- one", "1. first", "```") if strings.Contains(string(md), "font-family") { t.Error("stylesheet leaked into Markdown") } } func TestPDFRoundTrip(t *testing.T) { eng, err := engine.New(engine.GoPDF) if err != nil { t.Fatal(err) } out := filepath.Join(t.TempDir(), "doc.pdf") if err := eng.Render([]byte(sample), out); err != nil { t.Fatal(err) } data, err := os.ReadFile(out) if err != nil { t.Fatal(err) } md, err := PDF(data) if err != nil { t.Fatal(err) } requireContains(t, string(md), "# Title", "## Lists", "**bold**", "*italic*", "`code`", "- one", " - nested", "1. first", "2. second", "```\nfunc main() {\n run()\n}\n```") } func TestInvalidInputs(t *testing.T) { if _, err := DOCX([]byte("not a zip")); err == nil { t.Error("expected error for invalid docx") } if _, err := PDF([]byte("not a pdf")); err == nil { t.Error("expected error for invalid pdf") } } func TestParseFormats(t *testing.T) { got, err := ParseFormats("HTML, docx,html") if err != nil || len(got) != 2 || got[0] != FormatHTML || got[1] != FormatDOCX { t.Errorf("got %v, %v", got, err) } for _, bad := range []string{"", "md", "pdf,txt"} { if _, err := ParseFormats(bad); err == nil { t.Errorf("ParseFormats(%q) should fail", bad) } } } func TestRunnerSkipsExistingUnlessForced(t *testing.T) { dir := t.TempDir() write := func(name, content string) { if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { t.Fatal(err) } } write("page.html", "
hi
"), 0o644); err != nil { t.Fatal(err) } r := &Runner{InputDir: dir, Formats: []string{FormatDOCX}} if err := r.Run(); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(dir, "a.md")); err == nil { t.Error("html should be ignored when only docx is selected") } } func writeDoc(t *testing.T, path, content string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(content), 0o644); err != nil { t.Fatal(err) } } func fileExists(path string) bool { _, err := os.Stat(path) return err == nil } func TestInputFileWithOutputName(t *testing.T) { dir := t.TempDir() writeDoc(t, filepath.Join(dir, "a.html"), "x
") writeDoc(t, filepath.Join(dir, "notes.txt"), "text") in := filepath.Join(dir, "a.html") cases := map[string]*Runner{ "missing input": {InputFile: filepath.Join(dir, "nope.html")}, "input is a dir": {InputFile: dir}, "unsupported input": {InputFile: filepath.Join(dir, "notes.txt")}, "output is a dir": {InputFile: in, Output: dir}, "output not markdown": {InputFile: in, Output: filepath.Join(dir, "x.pdf")}, "scan output is file": {InputDir: dir, Output: in}, } for name, r := range cases { if err := r.Validate(); err == nil { t.Errorf("%s: expected a validation error", name) } } } func TestOutputDirMirrorsLayout(t *testing.T) { src, out := t.TempDir(), filepath.Join(t.TempDir(), "docs") writeDoc(t, filepath.Join(src, "a.html"), "