|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createMessages } from "./messages"; |
| 3 | +import { createOutbox, type OutboxStorage } from "./outbox"; |
| 4 | +import type { EventData } from "./events"; |
| 5 | +import { foldMessages } from "./fold"; |
| 6 | +import { keypair, message, signed } from "./testing"; |
| 7 | + |
| 8 | +const relay = keypair(), |
| 9 | + alice = keypair(); |
| 10 | +const channel = "compat"; |
| 11 | +const imageUrl = "https://relay.test/media/photo.png"; |
| 12 | +const fileUrl = "https://relay.test/media/report.pdf"; |
| 13 | +const imageIMeta = () => [ |
| 14 | + "imeta", |
| 15 | + `url ${imageUrl}`, |
| 16 | + "m image/png", |
| 17 | + "dim 640x480", |
| 18 | +]; |
| 19 | +const fileIMeta = () => [ |
| 20 | + "imeta", |
| 21 | + `url ${fileUrl}`, |
| 22 | + "m application/pdf", |
| 23 | + "size 1234", |
| 24 | +]; |
| 25 | + |
| 26 | +function memoryStorage(): OutboxStorage { |
| 27 | + return { load: () => [], save: () => {} }; |
| 28 | +} |
| 29 | + |
| 30 | +function localOutbox(viewer: string) { |
| 31 | + const writer = { |
| 32 | + sign: vi.fn(async (event) => signed(alice, event)), |
| 33 | + publish: vi.fn(async () => {}), |
| 34 | + kinds: [9, 40003], |
| 35 | + }; |
| 36 | + return createOutbox(viewer, writer, memoryStorage()).outbox; |
| 37 | +} |
| 38 | + |
| 39 | +function fold(events: EventData | readonly EventData[]) { |
| 40 | + return foldMessages( |
| 41 | + channel, |
| 42 | + relay.pubkey, |
| 43 | + Array.isArray(events) ? events : [events], |
| 44 | + )[0]; |
| 45 | +} |
| 46 | + |
| 47 | +describe("cross-client rich content compatibility", () => { |
| 48 | + it.each([ |
| 49 | + { |
| 50 | + name: "old Markdown image", |
| 51 | + content: `Before  after`, |
| 52 | + tags: [], |
| 53 | + expectedContent: "Before after", |
| 54 | + expectedAttachments: [{ url: imageUrl, kind: "image" }], |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "new imeta image", |
| 58 | + content: "Before attachment after", |
| 59 | + tags: [imageIMeta()], |
| 60 | + expectedContent: "Before attachment after", |
| 61 | + expectedAttachments: [ |
| 62 | + { |
| 63 | + url: imageUrl, |
| 64 | + kind: "image", |
| 65 | + mime: "image/png", |
| 66 | + name: "photo.png", |
| 67 | + dimensions: { width: 640, height: 480 }, |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "new imeta file with attachment link label", |
| 73 | + content: `Before [Quarterly Report](${fileUrl}) after`, |
| 74 | + tags: [fileIMeta()], |
| 75 | + expectedContent: "Before after", |
| 76 | + expectedAttachments: [ |
| 77 | + { |
| 78 | + url: fileUrl, |
| 79 | + kind: "file", |
| 80 | + mime: "application/pdf", |
| 81 | + size: 1234, |
| 82 | + name: "Quarterly Report", |
| 83 | + }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + ])( |
| 87 | + "folds representative $name content", |
| 88 | + ({ content, tags, expectedContent, expectedAttachments }) => { |
| 89 | + const row = fold(message(alice, channel, content, 10, tags)); |
| 90 | + expect(row?.content).toBe(expectedContent); |
| 91 | + expect(row?.attachments).toEqual(expectedAttachments); |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + it("uses the latest authorized edit imeta as the attachment metadata source", () => { |
| 96 | + const original = message( |
| 97 | + alice, |
| 98 | + channel, |
| 99 | + `Original [Report](${fileUrl})`, |
| 100 | + 10, |
| 101 | + [fileIMeta()], |
| 102 | + ); |
| 103 | + const edit = signed(alice, { |
| 104 | + kind: 40003, |
| 105 | + content: `Edited `, |
| 106 | + created_at: 11, |
| 107 | + tags: [["h", channel], ["e", original.id], imageIMeta()], |
| 108 | + }); |
| 109 | + const spoofedLaterEdit = signed(keypair(), { |
| 110 | + kind: 40003, |
| 111 | + content: "Spoofed", |
| 112 | + created_at: 12, |
| 113 | + tags: [["h", channel], ["e", original.id], fileIMeta()], |
| 114 | + }); |
| 115 | + |
| 116 | + expect(fold([original, edit, spoofedLaterEdit])).toMatchObject({ |
| 117 | + content: "Edited", |
| 118 | + edited: true, |
| 119 | + attachments: [ |
| 120 | + { |
| 121 | + url: imageUrl, |
| 122 | + kind: "image", |
| 123 | + mime: "image/png", |
| 124 | + name: "photo.png", |
| 125 | + dimensions: { width: 640, height: 480 }, |
| 126 | + }, |
| 127 | + ], |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it("preserves original imeta attachments on text-only edits without imeta", () => { |
| 132 | + const original = message(alice, channel, "Original attachment", 10, [ |
| 133 | + imageIMeta(), |
| 134 | + ]); |
| 135 | + const edit = signed(alice, { |
| 136 | + kind: 40003, |
| 137 | + content: "Edited text", |
| 138 | + created_at: 11, |
| 139 | + tags: [ |
| 140 | + ["h", channel], |
| 141 | + ["e", original.id], |
| 142 | + ], |
| 143 | + }); |
| 144 | + |
| 145 | + expect(fold([original, edit])).toMatchObject({ |
| 146 | + content: "Edited text", |
| 147 | + edited: true, |
| 148 | + attachments: [ |
| 149 | + { |
| 150 | + url: imageUrl, |
| 151 | + kind: "image", |
| 152 | + mime: "image/png", |
| 153 | + name: "photo.png", |
| 154 | + dimensions: { width: 640, height: 480 }, |
| 155 | + }, |
| 156 | + ], |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it("does not admit kind 40008 diff rows into the channel fold", () => { |
| 161 | + const diff = signed(alice, { |
| 162 | + kind: 40008, |
| 163 | + content: JSON.stringify({ patches: [] }), |
| 164 | + created_at: 10, |
| 165 | + tags: [["h", channel]], |
| 166 | + }); |
| 167 | + |
| 168 | + expect(foldMessages(channel, relay.pubkey, [diff])).toEqual([]); |
| 169 | + }); |
| 170 | + |
| 171 | + it("renders spoiler syntax as ordinary literal text", () => { |
| 172 | + const row = fold(message(alice, channel, "Before ||secret|| after", 10)); |
| 173 | + |
| 174 | + expect(row?.content).toBe("Before ||secret|| after"); |
| 175 | + expect(row?.attachments).toEqual([]); |
| 176 | + }); |
| 177 | + |
| 178 | + it("emits original imeta tags on text edits so old clients retain attachments", () => { |
| 179 | + const original = message(alice, channel, `Original ${imageUrl}`, 10, [ |
| 180 | + imageIMeta(), |
| 181 | + ]); |
| 182 | + const outbox = localOutbox(alice.pubkey); |
| 183 | + const messages = createMessages( |
| 184 | + outbox, |
| 185 | + alice.pubkey, |
| 186 | + (id) => (id === original.id ? original : undefined), |
| 187 | + () => [], |
| 188 | + () => {}, |
| 189 | + ); |
| 190 | + |
| 191 | + const editId = messages.edit(original.id, "Edited text"); |
| 192 | + const edit = outbox |
| 193 | + .snapshot() |
| 194 | + .find((item) => item.event.id === editId)?.event; |
| 195 | + |
| 196 | + expect(edit?.tags).toEqual( |
| 197 | + expect.arrayContaining([ |
| 198 | + ["h", channel], |
| 199 | + ["e", original.id], |
| 200 | + imageIMeta(), |
| 201 | + ]), |
| 202 | + ); |
| 203 | + }); |
| 204 | +}); |
0 commit comments