this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add more tests

Signed-off-by: Marius Kimmina <mar.kimmina@gmail.com>

+232
+232
internal/converter/markdown_test.go
··· 1 + package converter 2 + 3 + import ( 4 + "encoding/json" 5 + "strings" 6 + "testing" 7 + 8 + "github.com/marius/leaflet-hugo-sync/internal/atproto" 9 + ) 10 + 11 + func TestConvertLeaflet_TextBlock(t *testing.T) { 12 + doc := &atproto.LeafletDocument{ 13 + Title: "Test Post", 14 + Pages: []atproto.Page{ 15 + { 16 + Blocks: []atproto.BlockWrapper{ 17 + { 18 + Block: mustMarshal(atproto.TextBlock{ 19 + Type: "pub.leaflet.blocks.text", 20 + Plaintext: "Hello world", 21 + }), 22 + }, 23 + }, 24 + }, 25 + }, 26 + } 27 + 28 + conv := NewConverter() 29 + result, err := conv.ConvertLeaflet(doc) 30 + if err != nil { 31 + t.Fatalf("ConvertLeaflet failed: %v", err) 32 + } 33 + 34 + expected := "Hello world\n\n" 35 + if result.Markdown != expected { 36 + t.Errorf("expected %q, got %q", expected, result.Markdown) 37 + } 38 + } 39 + 40 + func TestConvertLeaflet_CodeBlock(t *testing.T) { 41 + doc := &atproto.LeafletDocument{ 42 + Pages: []atproto.Page{ 43 + { 44 + Blocks: []atproto.BlockWrapper{ 45 + { 46 + Block: mustMarshal(atproto.CodeBlock{ 47 + Type: "pub.leaflet.blocks.code", 48 + Language: "go", 49 + Plaintext: "fmt.Println(\"hello\")", 50 + }), 51 + }, 52 + }, 53 + }, 54 + }, 55 + } 56 + 57 + conv := NewConverter() 58 + result, err := conv.ConvertLeaflet(doc) 59 + if err != nil { 60 + t.Fatalf("ConvertLeaflet failed: %v", err) 61 + } 62 + 63 + if !strings.Contains(result.Markdown, "```go") { 64 + t.Errorf("expected code block with go language, got %q", result.Markdown) 65 + } 66 + if !strings.Contains(result.Markdown, "fmt.Println") { 67 + t.Errorf("expected code content, got %q", result.Markdown) 68 + } 69 + } 70 + 71 + func TestConvertLeaflet_ImageBlock(t *testing.T) { 72 + doc := &atproto.LeafletDocument{ 73 + Pages: []atproto.Page{ 74 + { 75 + Blocks: []atproto.BlockWrapper{ 76 + { 77 + Block: mustMarshal(atproto.ImageBlock{ 78 + Type: "pub.leaflet.blocks.image", 79 + Alt: "Test Image", 80 + Image: atproto.Blob{ 81 + Ref: atproto.BlobRef{ 82 + Link: "bafytest123", 83 + }, 84 + }, 85 + }), 86 + }, 87 + }, 88 + }, 89 + }, 90 + } 91 + 92 + conv := NewConverter() 93 + result, err := conv.ConvertLeaflet(doc) 94 + if err != nil { 95 + t.Fatalf("ConvertLeaflet failed: %v", err) 96 + } 97 + 98 + if !strings.Contains(result.Markdown, "![Test Image](bafytest123)") { 99 + t.Errorf("expected image markdown, got %q", result.Markdown) 100 + } 101 + if len(result.Images) != 1 { 102 + t.Errorf("expected 1 image reference, got %d", len(result.Images)) 103 + } 104 + if result.Images[0].Alt != "Test Image" { 105 + t.Errorf("expected alt 'Test Image', got %q", result.Images[0].Alt) 106 + } 107 + } 108 + 109 + func TestRenderText_WithLinkFacet(t *testing.T) { 110 + block := &atproto.TextBlock{ 111 + Plaintext: "Check out example.com for more info", 112 + Facets: []atproto.Facet{ 113 + { 114 + Index: atproto.Features{ 115 + ByteStart: 10, 116 + ByteEnd: 21, 117 + }, 118 + Features: []atproto.Feature{ 119 + { 120 + Type: "pub.leaflet.richtext.facet#link", 121 + URI: "https://example.com", 122 + }, 123 + }, 124 + }, 125 + }, 126 + } 127 + 128 + conv := NewConverter() 129 + result := conv.renderText(block) 130 + 131 + expected := "Check out [example.com](https://example.com) for more info" 132 + if result != expected { 133 + t.Errorf("expected %q, got %q", expected, result) 134 + } 135 + } 136 + 137 + func TestRenderText_WithInlineCode(t *testing.T) { 138 + block := &atproto.TextBlock{ 139 + Plaintext: "Use the fmt.Println function", 140 + Facets: []atproto.Facet{ 141 + { 142 + Index: atproto.Features{ 143 + ByteStart: 8, 144 + ByteEnd: 19, 145 + }, 146 + Features: []atproto.Feature{ 147 + { 148 + Type: "pub.leaflet.richtext.facet#code", 149 + }, 150 + }, 151 + }, 152 + }, 153 + } 154 + 155 + conv := NewConverter() 156 + result := conv.renderText(block) 157 + 158 + expected := "Use the `fmt.Println` function" 159 + if result != expected { 160 + t.Errorf("expected %q, got %q", expected, result) 161 + } 162 + } 163 + 164 + func TestRenderText_WithMention(t *testing.T) { 165 + block := &atproto.TextBlock{ 166 + Plaintext: "Thanks @alice for the help", 167 + Facets: []atproto.Facet{ 168 + { 169 + Index: atproto.Features{ 170 + ByteStart: 7, 171 + ByteEnd: 13, 172 + }, 173 + Features: []atproto.Feature{ 174 + { 175 + Type: "pub.leaflet.richtext.facet#didMention", 176 + Did: "did:plc:alice123", 177 + }, 178 + }, 179 + }, 180 + }, 181 + } 182 + 183 + conv := NewConverter() 184 + result := conv.renderText(block) 185 + 186 + expected := "Thanks [@alice](https://bsky.app/profile/did:plc:alice123) for the help" 187 + if result != expected { 188 + t.Errorf("expected %q, got %q", expected, result) 189 + } 190 + } 191 + 192 + func TestConvertLeaflet_UnorderedList(t *testing.T) { 193 + listItem := atproto.ListItem{ 194 + Content: mustMarshal(atproto.TextBlock{ 195 + Type: "pub.leaflet.blocks.text", 196 + Plaintext: "First item", 197 + }), 198 + } 199 + 200 + doc := &atproto.LeafletDocument{ 201 + Pages: []atproto.Page{ 202 + { 203 + Blocks: []atproto.BlockWrapper{ 204 + { 205 + Block: mustMarshal(atproto.UnorderedListBlock{ 206 + Type: "pub.leaflet.blocks.unorderedList", 207 + Children: []atproto.ListItem{listItem}, 208 + }), 209 + }, 210 + }, 211 + }, 212 + }, 213 + } 214 + 215 + conv := NewConverter() 216 + result, err := conv.ConvertLeaflet(doc) 217 + if err != nil { 218 + t.Fatalf("ConvertLeaflet failed: %v", err) 219 + } 220 + 221 + if !strings.Contains(result.Markdown, "- First item") { 222 + t.Errorf("expected list item, got %q", result.Markdown) 223 + } 224 + } 225 + 226 + func mustMarshal(v interface{}) json.RawMessage { 227 + data, err := json.Marshal(v) 228 + if err != nil { 229 + panic(err) 230 + } 231 + return data 232 + }