generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add Get Friends and Mutuals tests
- Loading branch information
1 parent
6266e4c
commit 6b993b1
Showing
8 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { IDatabaseComponent } from '../../../src/adapters/db' | ||
|
||
export const mockDb: jest.Mocked<IDatabaseComponent> = { | ||
createFriendship: jest.fn(), | ||
updateFriendshipStatus: jest.fn(), | ||
getFriends: jest.fn(), | ||
getMutualFriends: jest.fn(), | ||
getFriendship: jest.fn(), | ||
getLastFriendshipAction: jest.fn(), | ||
recordFriendshipAction: jest.fn(), | ||
getReceivedFriendshipRequests: jest.fn(), | ||
getSentFriendshipRequests: jest.fn(), | ||
executeTx: jest.fn() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './logs' | ||
export * from './db' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ILoggerComponent } from '@well-known-components/interfaces/dist/components/logger' | ||
|
||
export const mockLogs: jest.Mocked<ILoggerComponent> = { | ||
getLogger: jest.fn().mockReturnValue({ | ||
log: jest.fn(), | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) |
Empty file.
79 changes: 79 additions & 0 deletions
79
test/unit/logic/adapters/rpc-server/services/get-friends.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { mockDb, mockLogs } from '../../../../../mocks/components' | ||
import { getFriendsService } from '../../../../../../src/adapters/rpc-server/services/get-friends' | ||
import { Empty } from '@dcl/protocol/out-js/google/protobuf/empty.gen' | ||
import { | ||
FRIENDSHIPS_COUNT_PAGE_STREAM, | ||
INTERNAL_SERVER_ERROR | ||
} from '../../../../../../src/adapters/rpc-server/constants' | ||
import { RpcServerContext, Friendship, AppComponents } from '../../../../../../src/types' | ||
|
||
describe('getFriendsService', () => { | ||
let components: jest.Mocked<Pick<AppComponents, 'db' | 'logs'>> | ||
let getFriends: ReturnType<typeof getFriendsService> | ||
|
||
const rpcContext: RpcServerContext = { | ||
address: '0x123', | ||
subscribers: undefined | ||
} | ||
|
||
const emptyRequest = {} as Empty | ||
|
||
beforeEach(() => { | ||
components = { db: mockDb, logs: mockLogs } | ||
getFriends = getFriendsService({ components }) | ||
}) | ||
|
||
it('should return the correct list of friends', async () => { | ||
const mockGetFriendsGenerator = async function* () { | ||
yield createMockFriendship('0x456', '0x123') | ||
yield createMockFriendship('0x789', '0x123') | ||
} | ||
mockDb.getFriends.mockReturnValueOnce(mockGetFriendsGenerator()) | ||
|
||
const generator = getFriends(emptyRequest, rpcContext) | ||
|
||
const result1 = await generator.next() | ||
expect(result1.value).toEqual({ users: [{ address: '0x456' }, { address: '0x789' }] }) | ||
|
||
const result2 = await generator.next() | ||
expect(result2.done).toBe(true) | ||
}) | ||
|
||
it('should respect the pagination limit', async () => { | ||
const mockFriendsGenerator = async function* () { | ||
for (let i = 0; i < FRIENDSHIPS_COUNT_PAGE_STREAM + 1; i++) { | ||
yield createMockFriendship(`0x${i + 1}`, '0x123') | ||
} | ||
} | ||
mockDb.getFriends.mockReturnValueOnce(mockFriendsGenerator()) | ||
|
||
const generator = getFriends(emptyRequest, rpcContext) | ||
|
||
const result1 = await generator.next() | ||
expect(result1.value.users).toHaveLength(FRIENDSHIPS_COUNT_PAGE_STREAM) | ||
|
||
const result2 = await generator.next() | ||
expect(result2.value.users).toHaveLength(1) | ||
expect(result2.done).toBe(false) // Generator still has values | ||
}) | ||
|
||
it('should handle errors from the database gracefully', async () => { | ||
mockDb.getFriends.mockImplementationOnce(() => { | ||
throw new Error('Database error') | ||
}) | ||
|
||
const generator = getFriends(emptyRequest, rpcContext) | ||
|
||
await expect(generator.next()).rejects.toThrow(INTERNAL_SERVER_ERROR) | ||
}) | ||
|
||
// Helper to create a mock friendship object | ||
const createMockFriendship = (requester: string, requested: string): Friendship => ({ | ||
address_requester: requester, | ||
address_requested: requested, | ||
id: 'mock-friendship-id', | ||
is_active: false, | ||
created_at: new Date().toISOString(), | ||
updated_at: new Date().toISOString() | ||
}) | ||
}) |
71 changes: 71 additions & 0 deletions
71
test/unit/logic/adapters/rpc-server/services/get-mutual-friends.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { mockDb, mockLogs } from '../../../../../mocks/components' | ||
import { getMutualFriendsService } from '../../../../../../src/adapters/rpc-server/services/get-mutual-friends' | ||
import { | ||
INTERNAL_SERVER_ERROR, | ||
FRIENDSHIPS_COUNT_PAGE_STREAM | ||
} from '../../../../../../src/adapters/rpc-server/constants' | ||
import { MutualFriendsPayload } from '@dcl/protocol/out-ts/decentraland/social_service_v2/social_service.gen' | ||
import { RpcServerContext, AppComponents } from '../../../../../../src/types' | ||
|
||
describe('getMutualFriendsService', () => { | ||
let components: jest.Mocked<Pick<AppComponents, 'db' | 'logs'>> | ||
let getMutualFriends: ReturnType<typeof getMutualFriendsService> | ||
|
||
const rpcContext: RpcServerContext = { | ||
address: '0x123', | ||
subscribers: undefined | ||
} | ||
|
||
const mutualFriendsRequest: MutualFriendsPayload = { | ||
user: { address: '0x456' } | ||
} | ||
|
||
beforeEach(() => { | ||
components = { db: mockDb, logs: mockLogs } | ||
getMutualFriends = getMutualFriendsService({ components }) | ||
}) | ||
|
||
it('should return the correct list of mutual friends', async () => { | ||
const mockMutualFriendsGenerator = async function* () { | ||
yield { address: '0x789' } | ||
yield { address: '0xabc' } | ||
} | ||
mockDb.getMutualFriends.mockReturnValueOnce(mockMutualFriendsGenerator()) | ||
|
||
const generator = getMutualFriends(mutualFriendsRequest, rpcContext) | ||
|
||
const result1 = await generator.next() | ||
expect(result1.value).toEqual({ users: [{ address: '0x789' }, { address: '0xabc' }] }) | ||
|
||
const result2 = await generator.next() | ||
expect(result2.done).toBe(true) | ||
}) | ||
|
||
it('should respect the pagination limit', async () => { | ||
const mockMutualFriendsGenerator = async function* () { | ||
for (let i = 0; i <= FRIENDSHIPS_COUNT_PAGE_STREAM; i++) { | ||
yield { address: `0x${i}` } | ||
} | ||
} | ||
mockDb.getMutualFriends.mockReturnValueOnce(mockMutualFriendsGenerator()) | ||
|
||
const generator = getMutualFriends(mutualFriendsRequest, rpcContext) | ||
|
||
const result1 = await generator.next() | ||
expect(result1.value.users).toHaveLength(FRIENDSHIPS_COUNT_PAGE_STREAM) | ||
|
||
const result2 = await generator.next() | ||
expect(result2.value.users).toHaveLength(1) | ||
expect(result2.done).toBe(false) | ||
}) | ||
|
||
it('should handle errors from the database gracefully', async () => { | ||
mockDb.getMutualFriends.mockImplementationOnce(() => { | ||
throw new Error('Database error') | ||
}) | ||
|
||
const generator = getMutualFriends(mutualFriendsRequest, rpcContext) | ||
|
||
await expect(generator.next()).rejects.toThrow(INTERNAL_SERVER_ERROR) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters