Files
TREK/server/tests/unit/nest/places-library/place-votes.controller.test.ts
T
Maurice 783c98f41e feat(places): one vote per person across copies, copies stay in step
A vote belongs to its person: cast or taken back on one copy of a place, it
lands on every copy of that place in the lists and trips the person may vote
on, in one transaction. A pass on the first start carries the existing votes
over, and whoever joins a list or a trip brings their latest vote onto its
copies of the places they voted on elsewhere, as inserts that never
overwrite.

An edit of the general facts of a place (name, description, address,
position, category, picture, website, phone) on a trip place or a saved
place is made on every other copy of it the editor may edit, with a history
entry per copy and an announcement per trip and list. Notes, tags, status
and votes stay with each copy, and a copy the editor may not edit is
skipped. REST and MCP take the same path; a token reaches only the kinds of
copies it holds write scope for.

The entry rating names its voters, the library list filters by a lowest
average vote and sorts by rating, and the tab counts honour the filter.
2026-11-29 23:36:43 +00:00

98 lines
4.4 KiB
TypeScript

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<typeof place | null> => 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' });
});
});