summaryrefslogtreecommitdiff
path: root/internal/processor
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processor')
-rw-r--r--internal/processor/markdown_test.go90
-rw-r--r--internal/processor/txt_test.go120
2 files changed, 210 insertions, 0 deletions
diff --git a/internal/processor/markdown_test.go b/internal/processor/markdown_test.go
new file mode 100644
index 0000000..a17b704
--- /dev/null
+++ b/internal/processor/markdown_test.go
@@ -0,0 +1,90 @@
+package processor
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestFindLocalImages(t *testing.T) {
+ t.Parallel()
+
+ t.Run("remote skipped", func(t *testing.T) {
+ t.Parallel()
+ dir := t.TempDir()
+ got := findLocalImages(`![](https://cdn.example/p.png) ![](http://x/y.jpg)`, dir)
+ if len(got) != 0 {
+ t.Fatalf("expected no locals, got %v", got)
+ }
+ })
+
+ t.Run("missing file ignored", func(t *testing.T) {
+ t.Parallel()
+ dir := t.TempDir()
+ got := findLocalImages(`![](nope.png)`, dir)
+ if len(got) != 0 {
+ t.Fatalf("expected no locals, got %v", got)
+ }
+ })
+
+ t.Run("picks existing basename", func(t *testing.T) {
+ t.Parallel()
+ dir := t.TempDir()
+ if err := os.WriteFile(filepath.Join(dir, "shot.png"), []byte("x"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ got := findLocalImages(`![alt](shot.png)`, dir)
+ if len(got) != 1 || got[0] != "shot.png" {
+ t.Fatalf("got %v; want [shot.png]", got)
+ }
+ })
+
+ tests := []struct {
+ name string
+ md string
+ files []string
+ want []string
+ wantLen int
+ }{
+ {
+ name: "multiple locals order",
+ md: `![](a.png) ![](b.png)`,
+ files: []string{"a.png", "b.png"},
+ wantLen: 2,
+ },
+ {
+ name: "alt with spaces",
+ md: `![my photo](z.gif)`,
+ files: []string{"z.gif"},
+ want: []string{"z.gif"},
+ wantLen: 1,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ dir := t.TempDir()
+ for _, f := range tt.files {
+ if err := os.WriteFile(filepath.Join(dir, f), []byte("x"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ got := findLocalImages(tt.md, dir)
+ if tt.want != nil {
+ if len(got) != len(tt.want) {
+ t.Fatalf("got %v; want %v", got, tt.want)
+ }
+ for i := range tt.want {
+ if got[i] != tt.want[i] {
+ t.Fatalf("got %v; want %v", got, tt.want)
+ }
+ }
+ return
+ }
+ if len(got) != tt.wantLen {
+ t.Fatalf("len(got)=%d; want %d (%v)", len(got), tt.wantLen, got)
+ }
+ })
+ }
+}
diff --git a/internal/processor/txt_test.go b/internal/processor/txt_test.go
new file mode 100644
index 0000000..bb44c28
--- /dev/null
+++ b/internal/processor/txt_test.go
@@ -0,0 +1,120 @@
+package processor
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestStripURLTrailing(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ in string
+ want string
+ }{
+ {name: "plain", in: "https://example.com", want: "https://example.com"},
+ {name: "trailing period", in: "https://example.com.", want: "https://example.com"},
+ {name: "multiple punctuation", in: "https://a.b/c).", want: "https://a.b/c"},
+ {name: "empty", in: "", want: ""},
+ {name: "only punctuation", in: "...", want: ""},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ got := stripURLTrailing(tt.in)
+ if got != tt.want {
+ t.Fatalf("stripURLTrailing(%q) = %q; want %q", tt.in, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestAutolinkLine(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ in string
+ want string
+ }{
+ {
+ name: "no url escapes",
+ in: `hello <world>`,
+ want: `hello &lt;world&gt;`,
+ },
+ {
+ name: "single url",
+ in: "see https://foo.test ok",
+ want: `see <a href="https://foo.test" target="_blank" rel="noopener noreferrer">https://foo.test</a> ok`,
+ },
+ {
+ name: "url with trailing period in prose",
+ in: "Visit https://foo.test.",
+ want: `Visit <a href="https://foo.test" target="_blank" rel="noopener noreferrer">https://foo.test</a>.`,
+ },
+ {
+ name: "two urls",
+ in: "a http://a.com b https://b.org c",
+ want: `a <a href="http://a.com" target="_blank" rel="noopener noreferrer">http://a.com</a> b <a href="https://b.org" target="_blank" rel="noopener noreferrer">https://b.org</a> c`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ got := autolinkLine(tt.in)
+ if got != tt.want {
+ t.Fatalf("autolinkLine(%q) = %q; want %q", tt.in, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestFormatParagraph(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ in string
+ want string
+ }{
+ {
+ name: "single line",
+ in: "hello",
+ want: "hello",
+ },
+ {
+ name: "line break",
+ in: "line one\nline two",
+ want: "line one<br>\nline two",
+ },
+ {
+ name: "skips blank lines inside para",
+ in: "a\n\nb",
+ want: "a<br>\nb",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ got := formatParagraph(tt.in)
+ if got != tt.want {
+ t.Fatalf("formatParagraph(%q) = %q; want %q", tt.in, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestFormatParagraph_autolinkMultiline(t *testing.T) {
+ t.Parallel()
+ got := formatParagraph("u https://x.y\nv")
+ if !strings.Contains(got, `<a href="https://x.y"`) {
+ t.Fatalf("expected autolink in multiline paragraph, got %q", got)
+ }
+ if !strings.Contains(got, "<br>") {
+ t.Fatalf("expected br between lines, got %q", got)
+ }
+}