|
15 | 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>.
|
16 | 16 | import { MemoryKvStore } from "@fedify/fedify/federation";
|
17 | 17 | import {
|
| 18 | + Announce, |
| 19 | + Create, |
| 20 | + Delete, |
18 | 21 | Hashtag,
|
19 | 22 | Mention,
|
20 | 23 | Note,
|
| 24 | + Person, |
21 | 25 | PUBLIC_COLLECTION,
|
| 26 | + Tombstone, |
| 27 | + Undo, |
22 | 28 | } from "@fedify/fedify/vocab";
|
| 29 | +import { assert } from "@std/assert/assert"; |
23 | 30 | import { assertEquals } from "@std/assert/equals";
|
| 31 | +import { assertInstanceOf } from "@std/assert/instance-of"; |
24 | 32 | import { assertRejects } from "@std/assert/rejects";
|
25 | 33 | import { BotImpl } from "./bot-impl.ts";
|
26 | 34 | import { createMessage } from "./message-impl.ts";
|
| 35 | +import { createMockContext } from "./session-impl.test.ts"; |
| 36 | +import { SessionImpl } from "./session-impl.ts"; |
| 37 | +import { text } from "./text.ts"; |
27 | 38 |
|
28 | 39 | Deno.test("createMessage()", async () => {
|
29 | 40 | const bot = new BotImpl<void>({ kv: new MemoryKvStore(), username: "bot" });
|
@@ -126,3 +137,173 @@ Deno.test("createMessage()", async () => {
|
126 | 137 | const unknownMessage = await createMessage<Note, void>(unknown, session);
|
127 | 138 | assertEquals(unknownMessage.visibility, "unknown");
|
128 | 139 | });
|
| 140 | + |
| 141 | +Deno.test("MessageImpl.delete()", async () => { |
| 142 | + const kv = new MemoryKvStore(); |
| 143 | + const bot = new BotImpl<void>({ kv, username: "bot" }); |
| 144 | + const ctx = createMockContext(bot, "https://example.com"); |
| 145 | + const session = new SessionImpl(bot, ctx); |
| 146 | + const note = new Note({ |
| 147 | + id: new URL( |
| 148 | + "https://example.com/ap/note/c1c792ce-a0be-4685-b396-e59e5ef8c788", |
| 149 | + ), |
| 150 | + content: "<p>Hello, world!</p>", |
| 151 | + attribution: new URL("https://example.com/ap/actor/bot"), |
| 152 | + to: PUBLIC_COLLECTION, |
| 153 | + cc: new URL("https://example.com/ap/actor/bot/followers"), |
| 154 | + }); |
| 155 | + const msg = await createMessage<Note, void>(note, session); |
| 156 | + await kv.set( |
| 157 | + bot.kvPrefixes.messages, |
| 158 | + ["c1c792ce-a0be-4685-b396-e59e5ef8c788"], |
| 159 | + ); |
| 160 | + await kv.set( |
| 161 | + [...bot.kvPrefixes.messages, "c1c792ce-a0be-4685-b396-e59e5ef8c788"], |
| 162 | + { |
| 163 | + "@context": "https://www.w3.org/ns/activitystreams", |
| 164 | + "type": "Create", |
| 165 | + "id": |
| 166 | + "https://example.com/ap/create/c1c792ce-a0be-4685-b396-e59e5ef8c788", |
| 167 | + "actor": "https://example.com/ap/actor/bot", |
| 168 | + "to": "https://www.w3.org/ns/activitystreams#Public", |
| 169 | + "cc": "https://example.com/ap/actor/bot/followers", |
| 170 | + object: await note.toJsonLd(), |
| 171 | + }, |
| 172 | + ); |
| 173 | + await msg.delete(); |
| 174 | + assertEquals(await kv.get(bot.kvPrefixes.messages), []); |
| 175 | + assertEquals( |
| 176 | + await kv.get([ |
| 177 | + ...bot.kvPrefixes.messages, |
| 178 | + "c1c792ce-a0be-4685-b396-e59e5ef8c788", |
| 179 | + ]), |
| 180 | + undefined, |
| 181 | + ); |
| 182 | + assertEquals(ctx.sentActivities.length, 1); |
| 183 | + const { recipients, activity } = ctx.sentActivities[0]; |
| 184 | + assertEquals(recipients, "followers"); |
| 185 | + assertInstanceOf(activity, Delete); |
| 186 | + assertEquals(activity.actorId, ctx.getActorUri(bot.identifier)); |
| 187 | + assertEquals(activity.toIds, [PUBLIC_COLLECTION]); |
| 188 | + assertEquals(activity.ccIds, [ctx.getFollowersUri(bot.identifier)]); |
| 189 | + const tombstone = await activity.getObject(); |
| 190 | + assertInstanceOf(tombstone, Tombstone); |
| 191 | + assertEquals(tombstone.id, note.id); |
| 192 | +}); |
| 193 | + |
| 194 | +Deno.test("MessageImpl.reply()", async () => { |
| 195 | + const kv = new MemoryKvStore(); |
| 196 | + const bot = new BotImpl<void>({ kv, username: "bot" }); |
| 197 | + const ctx = createMockContext(bot, "https://example.com"); |
| 198 | + const session = new SessionImpl(bot, ctx); |
| 199 | + const originalPost = new Note({ |
| 200 | + id: new URL( |
| 201 | + "https://example.com/ap/note/c1c792ce-a0be-4685-b396-e59e5ef8c788", |
| 202 | + ), |
| 203 | + content: "<p>Hello, world!</p>", |
| 204 | + attribution: new Person({ |
| 205 | + id: new URL("https://example.com/ap/actor/john"), |
| 206 | + preferredUsername: "john", |
| 207 | + }), |
| 208 | + to: new URL("https://example.com/ap/actor/john/followers"), |
| 209 | + cc: PUBLIC_COLLECTION, |
| 210 | + }); |
| 211 | + const originalMsg = await createMessage<Note, void>(originalPost, session); |
| 212 | + const reply = await originalMsg.reply(text`Hello, John!`); |
| 213 | + const msgIds = await kv.get<string[]>(bot.kvPrefixes.messages); |
| 214 | + assert(msgIds != null); |
| 215 | + assertEquals(msgIds.length, 1); |
| 216 | + const [msgId] = msgIds; |
| 217 | + const activityJson = await kv.get([...bot.kvPrefixes.messages, msgId]); |
| 218 | + assert(activityJson != null); |
| 219 | + const create = await Create.fromJsonLd(activityJson); |
| 220 | + assertEquals(ctx.sentActivities.length, 1); |
| 221 | + const { recipients, activity } = ctx.sentActivities[0]; |
| 222 | + assertEquals(recipients, "followers"); |
| 223 | + assertInstanceOf(activity, Create); |
| 224 | + assertEquals( |
| 225 | + await activity.toJsonLd({ format: "compact" }), |
| 226 | + await create.toJsonLd({ format: "compact" }), |
| 227 | + ); |
| 228 | + assertEquals( |
| 229 | + await reply.raw.toJsonLd({ format: "compact" }), |
| 230 | + await (await create.getObject())?.toJsonLd({ format: "compact" }), |
| 231 | + ); |
| 232 | + assertEquals(reply.actor, await session.getActor()); |
| 233 | + assertEquals(reply.replyTarget, originalMsg); |
| 234 | + assertEquals(reply.visibility, "unlisted"); |
| 235 | +}); |
| 236 | + |
| 237 | +Deno.test("MessageImpl.share()", async (t) => { |
| 238 | + const kv = new MemoryKvStore(); |
| 239 | + const bot = new BotImpl<void>({ kv, username: "bot" }); |
| 240 | + const ctx = createMockContext(bot, "https://example.com"); |
| 241 | + const session = new SessionImpl(bot, ctx); |
| 242 | + const originalPost = new Note({ |
| 243 | + id: new URL( |
| 244 | + "https://example.com/ap/note/c1c792ce-a0be-4685-b396-e59e5ef8c788", |
| 245 | + ), |
| 246 | + content: "<p>Hello, world!</p>", |
| 247 | + attribution: new Person({ |
| 248 | + id: new URL("https://example.com/ap/actor/john"), |
| 249 | + preferredUsername: "john", |
| 250 | + }), |
| 251 | + to: new URL("https://example.com/ap/actor/john/followers"), |
| 252 | + cc: PUBLIC_COLLECTION, |
| 253 | + }); |
| 254 | + const originalMsg = await createMessage<Note, void>(originalPost, session); |
| 255 | + const sharedMsg = await originalMsg.share(); |
| 256 | + let msgId: string; |
| 257 | + |
| 258 | + await t.step("share()", async () => { |
| 259 | + const msgIds = await kv.get<string[]>(bot.kvPrefixes.messages); |
| 260 | + assert(msgIds != null); |
| 261 | + assertEquals(msgIds.length, 1); |
| 262 | + [msgId] = msgIds; |
| 263 | + const activityJson = await kv.get([...bot.kvPrefixes.messages, msgId]); |
| 264 | + assert(activityJson != null); |
| 265 | + const announce = await Announce.fromJsonLd(activityJson); |
| 266 | + assertEquals(ctx.sentActivities.length, 1); |
| 267 | + const { recipients, activity } = ctx.sentActivities[0]; |
| 268 | + assertEquals(recipients, "followers"); |
| 269 | + assertInstanceOf(activity, Announce); |
| 270 | + assertEquals(activity.toIds, [ctx.getFollowersUri(bot.identifier)]); |
| 271 | + assertEquals(activity.ccIds, [ |
| 272 | + PUBLIC_COLLECTION, |
| 273 | + originalPost.attributionId, |
| 274 | + ]); |
| 275 | + assertEquals( |
| 276 | + await activity.toJsonLd({ format: "compact" }), |
| 277 | + await announce.toJsonLd({ format: "compact" }), |
| 278 | + ); |
| 279 | + assertEquals( |
| 280 | + await sharedMsg.raw.toJsonLd({ format: "compact" }), |
| 281 | + await announce.toJsonLd({ format: "compact" }), |
| 282 | + ); |
| 283 | + assertEquals(sharedMsg.actor, await session.getActor()); |
| 284 | + assertEquals(sharedMsg.visibility, "unlisted"); |
| 285 | + assertEquals(sharedMsg.original, originalMsg); |
| 286 | + }); |
| 287 | + |
| 288 | + ctx.sentActivities = []; |
| 289 | + |
| 290 | + await t.step("unshare()", async () => { |
| 291 | + await sharedMsg.unshare(); |
| 292 | + assertEquals(await kv.get(bot.kvPrefixes.messages), []); |
| 293 | + assertEquals( |
| 294 | + await kv.get([...bot.kvPrefixes.messages, msgId]), |
| 295 | + undefined, |
| 296 | + ); |
| 297 | + assertEquals(ctx.sentActivities.length, 1); |
| 298 | + const { recipients, activity } = ctx.sentActivities[0]; |
| 299 | + assertEquals(recipients, "followers"); |
| 300 | + assertInstanceOf(activity, Undo); |
| 301 | + assertEquals(activity.actorId, ctx.getActorUri(bot.identifier)); |
| 302 | + assertEquals(activity.toIds, [ctx.getFollowersUri(bot.identifier)]); |
| 303 | + assertEquals(activity.ccIds, [ |
| 304 | + PUBLIC_COLLECTION, |
| 305 | + originalPost.attributionId, |
| 306 | + ]); |
| 307 | + assertEquals(activity.objectId, sharedMsg.id); |
| 308 | + }); |
| 309 | +}); |
0 commit comments