| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestRoomAttachmentRenderingIntegration(t *testing.T) { |
| 9 | url := "https://cdn.example.test/report.pdf" |
| 10 | row := map[string]any{"content" : "See [report](" + url + ") and keep this prose." , "tags" : [][]string{{"imeta" , "url " + url, "m application/pdf" , "filename report.pdf" }}} |
| 11 | items := roomAttachments(row) |
| 12 | if len(items) != 1 { |
| 13 | t.Fatalf("items = %#v" , items) |
| 14 | } |
| 15 | if got := roomAttachmentContent(row["content" ].(string), items); got != row["content" ].(string) { |
| 16 | t.Fatalf("generic markdown changed: %q" , got) |
| 17 | } |
| 18 | markup := string(roomAttachmentMarkup(items)) |
| 19 | if strings.Contains(markup, "[report]()" ) || !strings.Contains(markup, "report.pdf" ) { |
| 20 | t.Fatalf("generic attachment markup: %s" , markup) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestRoomAttachmentVisibleURLMatchingIsExactAndSafe(t *testing.T) { |
| 25 | url := "https://cdn.example.test/photo.png" |
| 26 | base := [][]string{{"imeta" , "url " + url, "m image/png" }} |
| 27 | for name, content := range map[string]string{ |
| 28 | "prefix" : url + "-backup" , |
| 29 | "fenced" : "```\n" + url + "\n```" , |
| 30 | "inline" : "`" + url + "`" , |
| 31 | } { |
| 32 | if got := roomAttachments(map[string]any{"content" : content, "tags" : base}); len(got) != 0 { |
| 33 | t.Errorf("%s matched %#v" , name, got) |
| 34 | } |
| 35 | } |
| 36 | for _, unsafe := range []string{"javascript:alert(1)" , "data:image/png,abc" , "//cdn.example/photo.png" , "/media/" + strings.Repeat("a" , 64)} { |
| 37 | if got := roomAttachments(map[string]any{"content" : unsafe, "tags" : [][]string{{"imeta" , "url " + unsafe, "m image/png" }}}); len(got) != 0 { |
| 38 | t.Errorf("unsafe URL %q accepted" , unsafe) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestRoomAttachmentEditAndThreadRootReplacement(t *testing.T) { |
| 44 | rootID := strings.Repeat("1" , 64) |
| 45 | oldURL := "https://cdn.example.test/old.png" |
| 46 | newURL := "https://cdn.example.test/new.mp4" |
| 47 | root := map[string]any{"id" : rootID, "pubkey" : strings.Repeat("a" , 64), "kind" : 11, "created_at" : int64(10), "content" : oldURL, "tags" : [][]string{{"imeta" , "url " + oldURL, "m image/png" }}} |
| 48 | edit := map[string]any{"id" : strings.Repeat("2" , 64), "pubkey" : root["pubkey" ], "kind" : 40003, "created_at" : int64(11), "content" : newURL, "tags" : [][]string{{"e" , rootID}, {"imeta" , "url " + newURL, "m video/mp4" }}} |
| 49 | value := map[string]any{"room" : map[string]any{"id" : "general" }, "root" : root, "replies" : []any{edit}} |
| 50 | item := roomRoot(value) |
| 51 | if !item.Edited || item.Content != newURL || len(item.Attachments) != 1 || item.Attachments[0].URL != newURL { |
| 52 | t.Fatalf("root edit = %#v" , item) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestRoomAttachmentMediaMarkupHasFallbackAndBounds(t *testing.T) { |
| 57 | items := []roomAttachment{{URL: "https://cdn.example.test/a.mp4" , MIME: "video/mp4" , Name: "clip.mp4" }, {URL: "https://cdn.example.test/a.mp3" , MIME: "audio/mpeg" , Name: "audio.mp3" }} |
| 58 | markup := string(roomAttachmentMarkup(items)) |
| 59 | if !strings.Contains(markup, "clip.mp4" ) || !strings.Contains(markup, "audio.mp3" ) { |
| 60 | t.Fatalf("media fallback missing: %s" , markup) |
| 61 | } |
| 62 | } |