|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { type Context, type Create, PUBLIC_COLLECTION } from '@fedify/fedify'; |
| 4 | + |
| 5 | +import type { ContextData } from '@/app'; |
| 6 | +import { ok } from '@/core/result'; |
| 7 | +import { |
| 8 | + Audience, |
| 9 | + Post, |
| 10 | + PostSummary, |
| 11 | + PostTitle, |
| 12 | + PostType, |
| 13 | +} from '@/post/post.entity'; |
| 14 | +import type { PostService } from '@/post/post.service'; |
| 15 | +import { createTestExternalAccount } from '@/test/account-entity-test-helpers'; |
| 16 | +import { CreateHandler } from './create.handler'; |
| 17 | + |
| 18 | +describe('CreateHandler', () => { |
| 19 | + let handler: CreateHandler; |
| 20 | + let mockPostService: PostService; |
| 21 | + let mockContext: Context<ContextData>; |
| 22 | + let mockLogger: { |
| 23 | + info: ReturnType<typeof vi.fn>; |
| 24 | + error: ReturnType<typeof vi.fn>; |
| 25 | + }; |
| 26 | + let mockGlobalDb: { |
| 27 | + get: ReturnType<typeof vi.fn>; |
| 28 | + set: ReturnType<typeof vi.fn>; |
| 29 | + }; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + mockLogger = { |
| 33 | + info: vi.fn(), |
| 34 | + error: vi.fn(), |
| 35 | + }; |
| 36 | + |
| 37 | + mockGlobalDb = { |
| 38 | + get: vi.fn(), |
| 39 | + set: vi.fn(), |
| 40 | + }; |
| 41 | + |
| 42 | + mockPostService = { |
| 43 | + getByApId: vi.fn(), |
| 44 | + } as unknown as PostService; |
| 45 | + |
| 46 | + mockContext = { |
| 47 | + data: { |
| 48 | + logger: mockLogger, |
| 49 | + globaldb: mockGlobalDb, |
| 50 | + }, |
| 51 | + parseUri: vi.fn((url) => ({ type: 'object', id: url?.href })), |
| 52 | + } as unknown as Context<ContextData>; |
| 53 | + |
| 54 | + handler = new CreateHandler(mockPostService); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('handle', () => { |
| 58 | + it('should ignore Create activities with no id', async () => { |
| 59 | + const mockCreate = { |
| 60 | + id: null, |
| 61 | + objectId: new URL('https://example.com/post/123'), |
| 62 | + toIds: [PUBLIC_COLLECTION], |
| 63 | + ccIds: [], |
| 64 | + toJsonLd: vi.fn(), |
| 65 | + } as unknown as Create; |
| 66 | + |
| 67 | + await handler.handle(mockContext, mockCreate); |
| 68 | + |
| 69 | + expect(mockPostService.getByApId).not.toHaveBeenCalled(); |
| 70 | + expect(mockGlobalDb.set).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should ignore Create activities with no objectId', async () => { |
| 74 | + const mockCreate = { |
| 75 | + id: new URL('https://example.com/create/123'), |
| 76 | + objectId: null, |
| 77 | + toIds: [PUBLIC_COLLECTION], |
| 78 | + ccIds: [], |
| 79 | + toJsonLd: vi.fn(), |
| 80 | + } as unknown as Create; |
| 81 | + |
| 82 | + await handler.handle(mockContext, mockCreate); |
| 83 | + |
| 84 | + expect(mockPostService.getByApId).not.toHaveBeenCalled(); |
| 85 | + expect(mockGlobalDb.set).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should ignore private / unlisted Create activities', async () => { |
| 89 | + const mockCreate = { |
| 90 | + id: new URL('https://example.com/create/123'), |
| 91 | + objectId: new URL('https://example.com/post/123'), |
| 92 | + toIds: [new URL('https://example.com/users/specific-user')], // Not addressed to PUBLIC_COLLECTION |
| 93 | + ccIds: [], |
| 94 | + toJsonLd: vi.fn(), |
| 95 | + } as unknown as Create; |
| 96 | + |
| 97 | + await handler.handle(mockContext, mockCreate); |
| 98 | + |
| 99 | + expect(mockPostService.getByApId).not.toHaveBeenCalled(); |
| 100 | + expect(mockGlobalDb.set).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should process Create activity', async () => { |
| 104 | + const mockAccount = await createTestExternalAccount(1, { |
| 105 | + username: 'testuser', |
| 106 | + name: 'Test User', |
| 107 | + bio: null, |
| 108 | + url: null, |
| 109 | + avatarUrl: null, |
| 110 | + bannerImageUrl: null, |
| 111 | + customFields: null, |
| 112 | + apId: new URL('https://example.com/users/testuser'), |
| 113 | + apFollowers: null, |
| 114 | + apInbox: new URL('https://example.com/users/testuser/inbox'), |
| 115 | + }); |
| 116 | + |
| 117 | + const postApId = new URL('https://example.com/post/123'); |
| 118 | + const mockPost = new Post( |
| 119 | + 1, |
| 120 | + 'post-uuid', |
| 121 | + mockAccount, |
| 122 | + PostType.Article, |
| 123 | + Audience.Public, |
| 124 | + PostTitle.parse('Test Post'), |
| 125 | + PostSummary.parse('Test excerpt'), |
| 126 | + null, |
| 127 | + 'Test content', |
| 128 | + postApId, |
| 129 | + null, |
| 130 | + new Date(), |
| 131 | + { ghostAuthors: [] }, |
| 132 | + 0, |
| 133 | + 0, |
| 134 | + 0, |
| 135 | + null, // inReplyTo |
| 136 | + null, // threadRoot |
| 137 | + null, // _readingTimeMinutes |
| 138 | + [], // attachments |
| 139 | + postApId, // apId |
| 140 | + ); |
| 141 | + |
| 142 | + vi.mocked(mockPostService.getByApId).mockResolvedValue( |
| 143 | + ok(mockPost), |
| 144 | + ); |
| 145 | + |
| 146 | + const mockCreateJson = { |
| 147 | + '@context': 'https://www.w3.org/ns/activitystreams', |
| 148 | + type: 'Create', |
| 149 | + id: 'https://example.com/create/123', |
| 150 | + }; |
| 151 | + |
| 152 | + const mockCreate = { |
| 153 | + id: new URL('https://example.com/create/123'), |
| 154 | + objectId: new URL('https://example.com/post/123'), |
| 155 | + toIds: [PUBLIC_COLLECTION], |
| 156 | + ccIds: [], |
| 157 | + toJsonLd: vi.fn().mockResolvedValue(mockCreateJson), |
| 158 | + } as unknown as Create; |
| 159 | + |
| 160 | + await handler.handle(mockContext, mockCreate); |
| 161 | + |
| 162 | + expect(mockPostService.getByApId).toHaveBeenCalledWith( |
| 163 | + mockCreate.objectId, |
| 164 | + ); |
| 165 | + expect(mockGlobalDb.set).toHaveBeenCalledWith( |
| 166 | + [mockCreate.id?.href], |
| 167 | + mockCreateJson, |
| 168 | + ); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments