|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { GistsService } from './gists.service'; |
| 3 | +import { GistRepository } from './gist.repository'; |
| 4 | +import { GeoService } from '../geo/geo.service'; |
| 5 | +import { IpfsService } from '../ipfs/ipfs.service'; |
| 6 | +import { SorobanService } from '../soroban/soroban.service'; |
| 7 | +import { CacheService } from '../cache/cache.service'; |
| 8 | +import { Gist } from './entities/gist.entity'; |
| 9 | +import { CreateGistDto } from './dto/create-gist.dto'; |
| 10 | +import { QueryGistsDto } from './dto/query-gists.dto'; |
| 11 | + |
| 12 | +jest.mock('../common/utils/sanitize', () => ({ |
| 13 | + stripHtml: jest.fn((text: string) => text), |
| 14 | +})); |
| 15 | + |
| 16 | +const mockGist = (): Gist => ({ |
| 17 | + id: 'uuid-1', |
| 18 | + content: 'Test gist', |
| 19 | + location_cell: 's1t7d8c', |
| 20 | + content_hash: 'mock_Qmabc123', |
| 21 | + stellar_gist_id: '1000', |
| 22 | + tx_hash: 'mock_tx_abc', |
| 23 | + location: null, |
| 24 | + created_at: new Date('2026-01-01T00:00:00.000Z'), |
| 25 | +}); |
| 26 | + |
| 27 | +describe('GistsService', () => { |
| 28 | + let service: GistsService; |
| 29 | + let gistRepository: jest.Mocked<GistRepository>; |
| 30 | + let geoService: jest.Mocked<GeoService>; |
| 31 | + let ipfsService: jest.Mocked<IpfsService>; |
| 32 | + let sorobanService: jest.Mocked<SorobanService>; |
| 33 | + let cacheService: jest.Mocked<CacheService>; |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + const module: TestingModule = await Test.createTestingModule({ |
| 37 | + providers: [ |
| 38 | + GistsService, |
| 39 | + { |
| 40 | + provide: GistRepository, |
| 41 | + useValue: { |
| 42 | + create: jest.fn(), |
| 43 | + findNearby: jest.fn(), |
| 44 | + findByGistId: jest.fn(), |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + provide: GeoService, |
| 49 | + useValue: { encode: jest.fn() }, |
| 50 | + }, |
| 51 | + { |
| 52 | + provide: IpfsService, |
| 53 | + useValue: { pinJson: jest.fn() }, |
| 54 | + }, |
| 55 | + { |
| 56 | + provide: SorobanService, |
| 57 | + useValue: { postGist: jest.fn() }, |
| 58 | + }, |
| 59 | + { |
| 60 | + provide: CacheService, |
| 61 | + useValue: { |
| 62 | + get: jest.fn(), |
| 63 | + set: jest.fn(), |
| 64 | + delPattern: jest.fn(), |
| 65 | + }, |
| 66 | + }, |
| 67 | + ], |
| 68 | + }).compile(); |
| 69 | + |
| 70 | + service = module.get<GistsService>(GistsService); |
| 71 | + gistRepository = module.get(GistRepository); |
| 72 | + geoService = module.get(GeoService); |
| 73 | + ipfsService = module.get(IpfsService); |
| 74 | + sorobanService = module.get(SorobanService); |
| 75 | + cacheService = module.get(CacheService); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('create()', () => { |
| 79 | + it('calls GeoService.encode with lat/lon', async () => { |
| 80 | + const dto: CreateGistDto = { content: 'Test', lat: 9.0579, lon: 7.4951 }; |
| 81 | + geoService.encode.mockReturnValue('s1t7d8c'); |
| 82 | + ipfsService.pinJson.mockResolvedValue({ cid: 'mock_Qmabc', mock: true }); |
| 83 | + sorobanService.postGist.mockResolvedValue({ gistId: '1', txHash: 'tx1', mock: true }); |
| 84 | + gistRepository.create.mockResolvedValue(mockGist()); |
| 85 | + cacheService.delPattern.mockResolvedValue(); |
| 86 | + |
| 87 | + await service.create(dto); |
| 88 | + |
| 89 | + expect(geoService.encode).toHaveBeenCalledWith(9.0579, 7.4951); |
| 90 | + }); |
| 91 | + |
| 92 | + it('calls IpfsService.pinJson with content and location metadata', async () => { |
| 93 | + const dto: CreateGistDto = { content: 'Test', lat: 9.0579, lon: 7.4951 }; |
| 94 | + geoService.encode.mockReturnValue('s1t7d8c'); |
| 95 | + ipfsService.pinJson.mockResolvedValue({ cid: 'mock_Qmabc', mock: true }); |
| 96 | + sorobanService.postGist.mockResolvedValue({ gistId: '1', txHash: 'tx1', mock: true }); |
| 97 | + gistRepository.create.mockResolvedValue(mockGist()); |
| 98 | + cacheService.delPattern.mockResolvedValue(); |
| 99 | + |
| 100 | + await service.create(dto); |
| 101 | + |
| 102 | + expect(ipfsService.pinJson).toHaveBeenCalledWith( |
| 103 | + expect.objectContaining({ |
| 104 | + content: 'Test', |
| 105 | + lat: 9.0579, |
| 106 | + lon: 7.4951, |
| 107 | + location_cell: 's1t7d8c', |
| 108 | + }), |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('calls SorobanService.postGist with locationCell, cid, and author', async () => { |
| 113 | + const dto: CreateGistDto = { content: 'Test', lat: 9.0579, lon: 7.4951, author: 'GABC' }; |
| 114 | + geoService.encode.mockReturnValue('s1t7d8c'); |
| 115 | + ipfsService.pinJson.mockResolvedValue({ cid: 'mock_Qmabc', mock: true }); |
| 116 | + sorobanService.postGist.mockResolvedValue({ gistId: '1', txHash: 'tx1', mock: true }); |
| 117 | + gistRepository.create.mockResolvedValue(mockGist()); |
| 118 | + cacheService.delPattern.mockResolvedValue(); |
| 119 | + |
| 120 | + await service.create(dto); |
| 121 | + |
| 122 | + expect(sorobanService.postGist).toHaveBeenCalledWith('s1t7d8c', 'mock_Qmabc', 'GABC'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('calls GistRepository.create with all required fields', async () => { |
| 126 | + const dto: CreateGistDto = { content: 'Test', lat: 9.0579, lon: 7.4951 }; |
| 127 | + geoService.encode.mockReturnValue('s1t7d8c'); |
| 128 | + ipfsService.pinJson.mockResolvedValue({ cid: 'mock_Qmabc', mock: true }); |
| 129 | + sorobanService.postGist.mockResolvedValue({ gistId: '42', txHash: 'tx42', mock: true }); |
| 130 | + gistRepository.create.mockResolvedValue(mockGist()); |
| 131 | + cacheService.delPattern.mockResolvedValue(); |
| 132 | + |
| 133 | + await service.create(dto); |
| 134 | + |
| 135 | + expect(gistRepository.create).toHaveBeenCalledWith({ |
| 136 | + content: 'Test', |
| 137 | + lat: 9.0579, |
| 138 | + lon: 7.4951, |
| 139 | + location_cell: 's1t7d8c', |
| 140 | + content_hash: 'mock_Qmabc', |
| 141 | + stellar_gist_id: '42', |
| 142 | + tx_hash: 'tx42', |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('returns the gist created by the repository', async () => { |
| 147 | + const dto: CreateGistDto = { content: 'Test', lat: 9.0579, lon: 7.4951 }; |
| 148 | + const gist = mockGist(); |
| 149 | + geoService.encode.mockReturnValue('s1t7d8c'); |
| 150 | + ipfsService.pinJson.mockResolvedValue({ cid: 'cid1', mock: true }); |
| 151 | + sorobanService.postGist.mockResolvedValue({ gistId: '1', txHash: 'tx', mock: true }); |
| 152 | + gistRepository.create.mockResolvedValue(gist); |
| 153 | + cacheService.delPattern.mockResolvedValue(); |
| 154 | + |
| 155 | + const result = await service.create(dto); |
| 156 | + |
| 157 | + expect(result).toBe(gist); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('findNearby()', () => { |
| 162 | + const query: QueryGistsDto = { lat: 9.0579, lon: 7.4951, radius: 500, limit: 20 }; |
| 163 | + const paginatedResult = { |
| 164 | + data: [mockGist()], |
| 165 | + pagination: { count: 1, cursor: null, hasMore: false }, |
| 166 | + }; |
| 167 | + |
| 168 | + it('returns cached result when cache hit occurs', async () => { |
| 169 | + cacheService.get.mockResolvedValue(paginatedResult); |
| 170 | + |
| 171 | + const result = await service.findNearby(query); |
| 172 | + |
| 173 | + expect(result).toBe(paginatedResult); |
| 174 | + expect(gistRepository.findNearby).not.toHaveBeenCalled(); |
| 175 | + }); |
| 176 | + |
| 177 | + it('calls GistRepository.findNearby on cache miss', async () => { |
| 178 | + cacheService.get.mockResolvedValue(null); |
| 179 | + cacheService.set.mockResolvedValue(); |
| 180 | + gistRepository.findNearby.mockResolvedValue(paginatedResult); |
| 181 | + |
| 182 | + await service.findNearby(query); |
| 183 | + |
| 184 | + expect(gistRepository.findNearby).toHaveBeenCalledWith({ |
| 185 | + lat: 9.0579, |
| 186 | + lon: 7.4951, |
| 187 | + radiusMeters: 500, |
| 188 | + limit: 20, |
| 189 | + cursor: undefined, |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + it('skips cache and calls repository directly when cursor is present', async () => { |
| 194 | + const queryWithCursor = { ...query, cursor: '2026-01-01T00:00:00.000Z' }; |
| 195 | + gistRepository.findNearby.mockResolvedValue(paginatedResult); |
| 196 | + |
| 197 | + await service.findNearby(queryWithCursor); |
| 198 | + |
| 199 | + expect(cacheService.get).not.toHaveBeenCalled(); |
| 200 | + expect(gistRepository.findNearby).toHaveBeenCalledWith( |
| 201 | + expect.objectContaining({ cursor: '2026-01-01T00:00:00.000Z' }), |
| 202 | + ); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + describe('findOne()', () => { |
| 207 | + it('returns cached gist on cache hit', async () => { |
| 208 | + const gist = mockGist(); |
| 209 | + cacheService.get.mockResolvedValue(gist); |
| 210 | + |
| 211 | + const result = await service.findOne('uuid-1'); |
| 212 | + |
| 213 | + expect(result).toBe(gist); |
| 214 | + expect(gistRepository.findByGistId).not.toHaveBeenCalled(); |
| 215 | + }); |
| 216 | + |
| 217 | + it('calls GistRepository.findByGistId on cache miss', async () => { |
| 218 | + const gist = mockGist(); |
| 219 | + cacheService.get.mockResolvedValue(null); |
| 220 | + cacheService.set.mockResolvedValue(); |
| 221 | + gistRepository.findByGistId.mockResolvedValue(gist); |
| 222 | + |
| 223 | + const result = await service.findOne('uuid-1'); |
| 224 | + |
| 225 | + expect(gistRepository.findByGistId).toHaveBeenCalledWith('uuid-1'); |
| 226 | + expect(result).toBe(gist); |
| 227 | + }); |
| 228 | + |
| 229 | + it('returns null when gist not found', async () => { |
| 230 | + cacheService.get.mockResolvedValue(null); |
| 231 | + cacheService.set.mockResolvedValue(); |
| 232 | + gistRepository.findByGistId.mockResolvedValue(null); |
| 233 | + |
| 234 | + const result = await service.findOne('nonexistent'); |
| 235 | + |
| 236 | + expect(result).toBeNull(); |
| 237 | + }); |
| 238 | + }); |
| 239 | +}); |
0 commit comments