package handler import ( "os" "path/filepath" "testing" ) func TestRecognizeVirtualConvert_MatrixAndPrecedence(t *testing.T) { root := t.TempDir() write := func(rel string) { p := filepath.Join(root, filepath.FromSlash(rel)) if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(p, []byte("x"), 0o644); err != nil { t.Fatal(err) } } // Sources on disk: doc.md, only.docx, both.md + both.docx, page.html. write("doc.md") write("only.docx") write("both.md") write("both.docx") write("page.html") cases := []struct { name string url string wantOK bool wantSrcExt string wantFormat string }{ {"md→docx", "/doc.docx", true, ".md", "docx"}, {"md→html", "/doc.html", true, ".md", "html"}, {"md→pdf", "/doc.pdf", true, ".md", "pdf"}, {"docx→md (only docx present)", "/only.md", true, ".docx", "md"}, {"docx→html (only docx present)", "/only.html", true, ".docx", "html"}, {"docx has no pdf source", "/only.pdf", false, "", ""}, {"both present, html prefers md source", "/both.html", true, ".md", "html"}, {"html→md", "/page.md", true, ".html", "md"}, {"html→docx", "/page.docx", true, ".html", "docx"}, {"no source at all", "/missing.html", false, "", ""}, {"directory url ignored", "/doc/", false, "", ""}, {"non-convertible target", "/doc.txt", false, "", ""}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { src, format, ok := RecognizeVirtualConvert(root, c.url) if ok != c.wantOK { t.Fatalf("ok=%v want %v (src=%q format=%q)", ok, c.wantOK, src, format) } if !ok { return } if format != c.wantFormat { t.Errorf("format=%q want %q", format, c.wantFormat) } if filepath.Ext(src) != c.wantSrcExt { t.Errorf("source ext=%q want %q (src=%q)", filepath.Ext(src), c.wantSrcExt, src) } }) } }