114 lines
4.3 KiB
Go
114 lines
4.3 KiB
Go
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/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{"<!doctype html>", "<h1>Title</h1>", "<p>Hello.</p>"} {
|
|
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 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<script>alert('x')</script>\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, "<script>") {
|
|
t.Fatalf("html contains raw script: %q", html)
|
|
}
|
|
if !strings.Contains(html, "raw HTML omitted") && !strings.Contains(html, "<script>") {
|
|
t.Fatalf("html = %q, want raw HTML disabled or escaped", html)
|
|
}
|
|
}
|
|
|
|
func TestGenerateDeterministicOutput(t *testing.T) {
|
|
backend, sourceBundle := markdownFixture(t, "# Title\n\nHello.\n")
|
|
first, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle})
|
|
if err != nil {
|
|
t.Fatalf("first Generate() error = %v", err)
|
|
}
|
|
second, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle})
|
|
if err != nil {
|
|
t.Fatalf("second Generate() error = %v", err)
|
|
}
|
|
if string(first[0].Data) != string(second[0].Data) {
|
|
t.Fatalf("outputs differ:\n%s\n%s", first[0].Data, second[0].Data)
|
|
}
|
|
}
|
|
|
|
func markdownFixture(t *testing.T, markdown string) (*fake.Backend, bundle.Bundle) {
|
|
t.Helper()
|
|
backend := fake.New()
|
|
if _, err := backend.WriteFile(context.Background(), "report.md", []byte(markdown), storage.WriteOptions{}); err != nil {
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
}
|
|
if _, err := backend.WriteFile(context.Background(), "summary.txt", []byte("Summary\n"), storage.WriteOptions{}); err != nil {
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
}
|
|
files := []bundle.ManifestFile{
|
|
{Path: "report.md", SHA256: bundle.FileDigest([]byte(markdown)), Size: int64(len(markdown))},
|
|
{Path: "summary.txt", SHA256: bundle.FileDigest([]byte("Summary\n")), Size: 8},
|
|
}
|
|
manifest := bundle.Manifest{
|
|
SchemaVersion: 1,
|
|
ID: "bundle",
|
|
Created: time.Date(2026, 5, 30, 11, 10, 0, 0, time.UTC),
|
|
Files: files,
|
|
}
|
|
manifest.Digest = bundle.BundleDigest(manifest.Files)
|
|
return backend, bundle.Bundle{Manifest: manifest}
|
|
}
|