Skip to content

Commit

Permalink
test: Add Get Friends and Mutuals tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet committed Jan 13, 2025
1 parent 6266e4c commit 6b993b1
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 2 deletions.
14 changes: 14 additions & 0 deletions test/mocks/components/db.ts
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()
}
2 changes: 2 additions & 0 deletions test/mocks/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './logs'
export * from './db'
11 changes: 11 additions & 0 deletions test/mocks/components/logs.ts
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()
})
}
3 changes: 3 additions & 0 deletions test/setupTests.ts
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 test/unit/logic/adapters/rpc-server/services/get-friends.spec.ts
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()
})
})
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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
parseEmittedUpdateToFriendshipUpdate,
parseUpsertFriendshipRequest,
validateNewFriendshipAction
} from '../../src/logic/friendships'
import { Action, FriendshipStatus } from '../../src/types'
} from '../../../src/logic/friendships'
import { Action, FriendshipStatus } from '../../../src/types'

describe('isFriendshipActionValid()', () => {
test('it should be valid if from is null and to is REQUEST ', () => {
Expand Down

0 comments on commit 6b993b1

Please sign in to comment.