import { DomainError } from '../../../../src/nest/common/domain-error'; import { PlaceVotesListController, PlaceVotesTripController, } from '../../../../src/nest/places-library/place-votes.controller'; import type { PlaceVotesService, VoteEvents } from '../../../../src/nest/places-library/place-votes.service'; import type { PlacesService } from '../../../../src/nest/places/places.service'; import type { User } from '../../../../src/types'; import { describe, expect, it, vi } from 'vitest'; const user = { id: 7, role: 'user' } as User; const place = { id: 3, trip_id: 9, name: 'Café', ratings: [], rating_avg: null, rating_count: 0 }; function build(access: unknown = { id: 9, user_id: 7 }) { const votes = { voteOnSavedPlace: vi.fn(async () => ({ id: 4, collection_id: 2, name: 'Café' })), voteOnEntry: vi.fn(async (_userId: number, key: string) => ({ key, rating: { average: 5, count: 1, mine: 5 } })), voteOnTripPlace: vi.fn(async (): Promise => place), }; const places = { verifyTripAccess: vi.fn(async () => access), broadcast: vi.fn() }; const asVotes = votes as unknown as PlaceVotesService; const asPlaces = places as unknown as PlacesService; return { votes, places, list: new PlaceVotesListController(asVotes, asPlaces), trip: new PlaceVotesTripController(asVotes, asPlaces), }; } /** The events a call handed the service. */ function eventsOf(mock: { mock: { calls: unknown[][] } }, at: number): VoteEvents { return mock.mock.calls[0]?.[at] as VoteEvents; } describe('PlaceVotesListController', () => { it('forwards a vote and its removal on a saved place with the caller socket', async () => { const t = build(); expect(await t.list.setRating(user, 4, { rating: 3 }, 's1')).toEqual({ id: 4, collection_id: 2, name: 'Café' }); expect(t.votes.voteOnSavedPlace).toHaveBeenCalledWith(7, 4, 3, expect.objectContaining({ socketId: 's1' })); await t.list.clearRating(user, 4, undefined); expect(t.votes.voteOnSavedPlace).toHaveBeenLastCalledWith( 7, 4, null, expect.objectContaining({ socketId: undefined }), ); }); it('sends a trip place of the vote to its room, skipping the socket only on the copy voted on', async () => { const t = build(); await t.list.setRating(user, 4, { rating: 3 }, 's1'); const events = eventsOf(t.votes.voteOnSavedPlace, 3); events.trip(9, place as never, false); expect(t.places.broadcast).toHaveBeenCalledWith('9', 'place:updated', { place }, undefined); events.trip(9, place as never, true); expect(t.places.broadcast).toHaveBeenLastCalledWith('9', 'place:updated', { place }, 's1'); }); it('forwards the vote by entry key and its removal from the query', async () => { const t = build(); expect(await t.list.rateEntry(user, { key: 'trip:3', rating: 5 }, 's2')).toEqual({ key: 'trip:3', rating: { average: 5, count: 1, mine: 5 }, }); await t.list.unrateEntry(user, { key: 'list:4' }, undefined); expect(t.votes.voteOnEntry).toHaveBeenLastCalledWith(7, 'list:4', null, expect.anything()); }); }); describe('PlaceVotesTripController', () => { it('answers the place of the vote', async () => { const t = build(); expect(await t.trip.rate(user, '9', '3', { rating: 4 }, 's3')).toEqual({ place }); expect(t.votes.voteOnTripPlace).toHaveBeenCalledWith(7, '9', '3', 4, expect.objectContaining({ socketId: 's3' })); expect(await t.trip.unrate(user, '9', '3', undefined)).toEqual({ place }); expect(t.votes.voteOnTripPlace).toHaveBeenLastCalledWith(7, '9', '3', null, expect.anything()); }); it("answers 404 { error: 'Trip not found' } for a trip out of reach, before any vote", async () => { const t = build(null); const err = await t.trip.rate(user, '9', '3', { rating: 4 }).catch((e: unknown) => e); expect(err).toBeInstanceOf(DomainError); expect((err as DomainError).getStatus()).toBe(404); expect((err as DomainError).toBody()).toEqual({ error: 'Trip not found' }); expect(t.votes.voteOnTripPlace).not.toHaveBeenCalled(); }); it("answers 404 { error: 'Place not found' } for a place the trip does not hold", async () => { const t = build(); t.votes.voteOnTripPlace.mockResolvedValueOnce(null); const err = await t.trip.unrate(user, '9', '99').catch((e: unknown) => e); expect((err as DomainError).getStatus()).toBe(404); expect((err as DomainError).toBody()).toEqual({ error: 'Place not found' }); }); });