package markdown import ( "context" "strings" "testing" "time" "gitea.maximumdirect.net/eric/distributor/internal/bundle" "gitea.maximumdirect.net/eric/distributor/internal/storage" "gitea.maximumdirect.net/eric/distributor/internal/storage/fake" "gitea.maximumdirect.net/eric/distributor/internal/testutil" "gitea.maximumdirect.net/eric/distributor/internal/transform" ) func TestGenerateMarkdownSidecar(t *testing.T) { backend, sourceBundle := markdownFixture(t, "# Title\n\nHello.\n") outputs, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle}) if err != nil { t.Fatalf("Generate() error = %v", err) } if got, want := len(outputs), 1; got != want { t.Fatalf("output count = %d, want %d", got, want) } output := outputs[0] if output.Path != "report.html" { t.Fatalf("path = %q, want report.html", output.Path) } if output.SourcePath != "report.md" || output.Transform != transform.MarkdownToHTML { t.Fatalf("metadata = %#v", output) } html := string(output.Data) for _, want := range []string{"", "

Title

", "

Hello.

"} { if !strings.Contains(html, want) { t.Fatalf("html = %q, want substring %q", html, want) } } if output.SHA256 != bundle.FileDigest(output.Data) || output.Size != int64(len(output.Data)) { t.Fatalf("digest/size metadata = %s/%d", output.SHA256, output.Size) } } func TestGenerateMarkdownIndexExplicitInput(t *testing.T) { backend := fake.New() sourceBundle := testutil.WriteFakeSourceBundle(t, backend, "", testutil.BundleOptions{ ID: "bundle", Files: []testutil.SourceFile{ {Path: "report.md", Data: "# Report\n"}, {Path: "notes.md", Data: "# Notes\n"}, }, }) outputs, err := New().Generate(context.Background(), transform.Request{ SourceBackend: backend, SourceBundle: sourceBundle, Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex, Input: "notes.md"}, }) if err != nil { t.Fatalf("Generate() error = %v", err) } if got, want := len(outputs), 1; got != want { t.Fatalf("output count = %d, want %d", got, want) } output := outputs[0] if output.Path != "index.html" || output.SourcePath != "notes.md" || output.Transform != transform.MarkdownToHTML { t.Fatalf("output metadata = %#v, want index from notes.md", output) } if !strings.Contains(string(output.Data), "

Notes

") { t.Fatalf("html = %q, want notes content", output.Data) } if output.SHA256 != bundle.FileDigest(output.Data) || output.Size != int64(len(output.Data)) { t.Fatalf("digest/size metadata = %s/%d", output.SHA256, output.Size) } } func TestGenerateMarkdownIndexSelectsOnlyMarkdownFile(t *testing.T) { backend, sourceBundle := markdownFixture(t, "# Title\n\nHello.\n") outputs, err := New().Generate(context.Background(), transform.Request{ SourceBackend: backend, SourceBundle: sourceBundle, Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex}, }) if err != nil { t.Fatalf("Generate() error = %v", err) } if got, want := outputs[0].Path, "index.html"; got != want { t.Fatalf("path = %q, want %q", got, want) } if got, want := outputs[0].SourcePath, "report.md"; got != want { t.Fatalf("source path = %q, want %q", got, want) } } func TestGenerateMarkdownIndexRejectsAmbiguousInput(t *testing.T) { backend := fake.New() sourceBundle := testutil.WriteFakeSourceBundle(t, backend, "", testutil.BundleOptions{ ID: "bundle", Files: []testutil.SourceFile{ {Path: "report.md", Data: "# Report\n"}, {Path: "notes.md", Data: "# Notes\n"}, }, }) _, err := New().Generate(context.Background(), transform.Request{ SourceBackend: backend, SourceBundle: sourceBundle, Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex}, }) if err == nil || !strings.Contains(err.Error(), "multiple markdown source files") { t.Fatalf("Generate() error = %v, want ambiguous input error", err) } } func TestGenerateMarkdownIndexRejectsMissingMarkdown(t *testing.T) { backend := fake.New() sourceBundle := testutil.WriteFakeSourceBundle(t, backend, "", testutil.BundleOptions{ ID: "bundle", Files: []testutil.SourceFile{{Path: "summary.txt", Data: "Summary\n"}}, }) _, err := New().Generate(context.Background(), transform.Request{ SourceBackend: backend, SourceBundle: sourceBundle, Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex}, }) if err == nil || !strings.Contains(err.Error(), "requires one markdown source file") { t.Fatalf("Generate() error = %v, want missing markdown error", err) } } func TestGenerateMarkdownIndexRejectsInvalidExplicitInput(t *testing.T) { backend, sourceBundle := markdownFixture(t, "# Title\n") tests := []struct { name string input string wantError string }{ {name: "unsafe", input: "../report.md", wantError: "markdown input"}, {name: "not listed", input: "missing.md", wantError: "not listed"}, {name: "not markdown", input: "summary.txt", wantError: "must end in .md"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { _, err := New().Generate(context.Background(), transform.Request{ SourceBackend: backend, SourceBundle: sourceBundle, Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex, Input: tt.input}, }) if err == nil || !strings.Contains(err.Error(), tt.wantError) { t.Fatalf("Generate() error = %v, want substring %q", err, tt.wantError) } }) } } func TestGenerateIgnoresNonMarkdown(t *testing.T) { backend := fake.New() if _, err := backend.WriteFile(context.Background(), "summary.txt", []byte("Summary\n"), storage.WriteOptions{}); err != nil { t.Fatalf("WriteFile() error = %v", err) } sourceBundle := bundle.Bundle{Manifest: bundle.Manifest{ SchemaVersion: 1, ID: "bundle", Created: time.Date(2026, 5, 30, 11, 10, 0, 0, time.UTC), Files: []bundle.ManifestFile{{Path: "summary.txt", SHA256: bundle.FileDigest([]byte("Summary\n")), Size: 8}}, }} outputs, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle}) if err != nil { t.Fatalf("Generate() error = %v", err) } if len(outputs) != 0 { t.Fatalf("outputs = %#v, want none", outputs) } } func TestGenerateDoesNotPassRawHTML(t *testing.T) { backend, sourceBundle := markdownFixture(t, "# Title\n\n\n") outputs, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle}) if err != nil { t.Fatalf("Generate() error = %v", err) } html := string(outputs[0].Data) if strings.Contains(html, "