// Package integrationtests runs end-to-end tests of the snonux generator pipeline. // Each test creates temporary input/output directories, places fixture files, runs // the full processor+generator pipeline, and asserts the expected outputs. package integrationtests import ( "encoding/xml" "fmt" "image" "image/color" "image/color/palette" "image/gif" "image/jpeg" "image/png" "os" "path/filepath" "strings" "testing" "codeberg.org/snonux/snonux/internal/config" "codeberg.org/snonux/snonux/internal/generator" "codeberg.org/snonux/snonux/internal/processor" ) // runPipeline executes both pipeline stages and returns the config used. func runPipeline(t *testing.T, inputDir, outputDir string) *config.Config { t.Helper() cfg := &config.Config{ InputDir: inputDir, OutputDir: outputDir, BaseURL: "https://snonux.foo", Theme: "neon", } _, err := processor.Run(cfg) if err != nil { t.Fatalf("processor.Run: %v", err) } if err := generator.Run(cfg); err != nil { t.Fatalf("generator.Run: %v", err) } return cfg } // makeDirs creates temporary input and output directories for a test. func makeDirs(t *testing.T) (inputDir, outputDir string) { t.Helper() base := t.TempDir() inputDir = filepath.Join(base, "inbox") outputDir = filepath.Join(base, "outdir") if err := os.MkdirAll(inputDir, 0o755); err != nil { t.Fatal(err) } if err := os.MkdirAll(outputDir, 0o755); err != nil { t.Fatal(err) } return inputDir, outputDir } // readFile is a helper that reads a file and fails the test on error. func readFile(t *testing.T, path string) string { t.Helper() data, err := os.ReadFile(path) if err != nil { t.Fatalf("read %s: %v", path, err) } return string(data) } // assertContains fails the test if content does not contain substr. func assertContains(t *testing.T, content, substr, label string) { t.Helper() if !strings.Contains(content, substr) { t.Errorf("%s: expected to contain %q\ngot:\n%s", label, substr, content[:min(len(content), 500)]) } } func min(a, b int) int { if a < b { return a } return b } // TestTxtInput verifies plain text files are converted to posts. func TestTxtInput(t *testing.T) { inputDir, outputDir := makeDirs(t) if err := os.WriteFile(filepath.Join(inputDir, "hello.txt"), []byte("Hello, Nexus!"), 0o644); err != nil { t.Fatal(err) } runPipeline(t, inputDir, outputDir) // Source file should have been removed after processing. if _, err := os.Stat(filepath.Join(inputDir, "hello.txt")); !os.IsNotExist(err) { t.Error("source file should have been deleted from input dir") } // A post directory should exist under outdir/posts/. entries, err := os.ReadDir(filepath.Join(outputDir, "posts")) if err != nil { t.Fatalf("read posts dir: %v", err) } if len(entries) != 1 { t.Fatalf("expected 1 post dir, got %d", len(entries)) } // index.html must contain the post text. index := readFile(t, filepath.Join(outputDir, "index.html")) assertContains(t, index, "Hello, Nexus!", "index.html") assertContains(t, index, "splash-gl-canvas", "index.html splash WebGL canvas") assertContains(t, index, `href="atom.xml"`, "index.html atom feed link") } // TestMarkdownInput verifies Markdown files are converted to HTML. func TestMarkdownInput(t *testing.T) { inputDir, outputDir := makeDirs(t) md := "# Hello Nexus\n\nThis is **bold** text." if err := os.WriteFile(filepath.Join(inputDir, "post.md"), []byte(md), 0o644); err != nil { t.Fatal(err) } runPipeline(t, inputDir, outputDir) index := readFile(t, filepath.Join(outputDir, "index.html")) assertContains(t, index, "bold", "index.html markdown bold") assertContains(t, index, "

", "index.html markdown h1") } // assertStandaloneImagePost checks index.html and posts//image.jpg after a lone image input. func assertStandaloneImagePost(t *testing.T, outputDir string) { t.Helper() index := readFile(t, filepath.Join(outputDir, "index.html")) assertContains(t, index, `