Files
TREK/server/tests/e2e/place-votes-join.e2e.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

145 lines
6.0 KiB
TypeScript

/**
* Late joiners bring their votes, end to end on the whole app (buildApp, the
* real guards and a migrated temp database; in the Postgres lane on Postgres
* too): who accepts an invitation to a list or is added to a trip gets their
* latest vote on the copies there of a place they voted on elsewhere, where
* they have none, as an insert that never overwrites, and the list or the
* trip's room hears of it.
*/
import { buildApp } from '../../src/bootstrap';
import { CollectionPlaceRatings } from '../../src/db/entities/CollectionPlaceRatings.entity';
import { PlaceRatings } from '../../src/db/entities/PlaceRatings.entity';
import { addCollectionMember, makeCollection, makeCollectionPlace } from '../helpers/factories/collections';
import { makePlace } from '../helpers/factories/places';
import { findRows, insertRow } from '../helpers/factories/rows';
import { setAddonEnabled } from '../helpers/factories/settings';
import { makeTrip } from '../helpers/factories/trips';
import { makeUser } from '../helpers/factories/users';
import { spyOnRealtime, type RealtimeSpies } from '../helpers/fake-realtime';
import { resetRateLimits } from '../helpers/test-db';
import { sessionCookie } from './harness';
import { MikroORM } from '@mikro-orm/core';
import type { INestApplication } from '@nestjs/common';
import type { Server } from 'http';
import request from 'supertest';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
let app: INestApplication;
let server: Server;
let orm: MikroORM;
let realtime: RealtimeSpies;
const COLLECTIONS = '/api/addons/collections';
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
async function tripVotes(placeId: number): Promise<Record<number, number>> {
const rows = await findRows(orm, PlaceRatings, { place: placeId });
return Object.fromEntries(rows.map((row) => [row.user_id, row.rating]));
}
async function listVotes(collectionPlaceId: number): Promise<Record<number, number>> {
const rows = await findRows(orm, CollectionPlaceRatings, { collectionPlace: collectionPlaceId });
return Object.fromEntries(rows.map((row) => [row.user_id, row.rating]));
}
/**
* `owner` has the trip Alps with Café and the list Food with a copy of it
* that came from the trip place. `joiner` has a list Mine with a copy of the
* same Café and voted on it; once on Food or Alps, those copies are the same
* place for them.
*/
async function seed() {
const { user: owner } = await makeUser(orm);
const { user: joiner } = await makeUser(orm);
const alps = await makeTrip(orm, owner.id, { title: 'Alps' });
const cafe = await makePlace(orm, alps.id, { name: 'Café', lat: 48.2, lng: 16.3, google_place_id: 'g-cafe' });
const copyOf = {
name: 'Café',
lat: 48.2,
lng: 16.3,
google_place_id: 'g-cafe',
source_trip_id: alps.id,
source_place_id: cafe.id,
};
const food = await makeCollection(orm, owner.id, { name: 'Food' });
const shared = await makeCollectionPlace(orm, food.id, owner.id, copyOf);
const mine = await makeCollection(orm, joiner.id, { name: 'Mine' });
const own = await makeCollectionPlace(orm, mine.id, joiner.id, copyOf);
await insertRow(orm, CollectionPlaceRatings, { collectionPlace: own.id, user: joiner.id, rating: 4 });
return { owner, joiner, alps, cafe, food, shared, mine, own };
}
beforeAll(async () => {
app = await buildApp();
server = app.getHttpServer() as Server;
orm = app.get(MikroORM);
await setAddonEnabled(orm, 'collections', true);
}, 60_000);
beforeEach(async () => {
await resetRateLimits(app);
realtime = spyOnRealtime(app);
});
afterAll(async () => {
await app?.close();
});
describe('who joins brings their votes', () => {
it('accepting a list invitation carries the vote onto its copy and the list hears of it', async () => {
const s = await seed();
// The joiner's own trip place, voted on, that the shared list's copy came from.
const home = await makeTrip(orm, s.joiner.id, { title: 'Home' });
const visited = await makePlace(orm, home.id, { name: 'Bar', lat: 47.1, lng: 15.4, google_place_id: 'g-bar' });
await insertRow(orm, PlaceRatings, { place: visited.id, user: s.joiner.id, rating: 4 });
const barCopy = await makeCollectionPlace(orm, s.food.id, s.owner.id, {
name: 'Bar',
lat: 47.1,
lng: 15.4,
google_place_id: 'g-bar',
source_trip_id: home.id,
source_place_id: visited.id,
});
await addCollectionMember(orm, s.food.id, s.joiner.id, { status: 'pending', role: 'viewer' });
const res = await request(server)
.post(`${COLLECTIONS}/invite/accept`)
.set(as(s.joiner.id))
.send({ collection_id: s.food.id });
expect(res.status).toBe(200);
expect(await listVotes(barCopy.id)).toEqual({ [s.joiner.id]: 4 });
// Only into the list joined: the trip they are not on keeps none.
expect(await tripVotes(s.cafe.id)).toEqual({});
expect(realtime.broadcastToUser).toHaveBeenCalledWith(
s.owner.id,
expect.objectContaining({ type: 'collections:accepted', collectionId: s.food.id }),
undefined,
);
});
it('being added to a trip carries the vote onto its copy and tells the trip room', async () => {
const s = await seed();
const res = await request(server)
.post(`/api/trips/${s.alps.id}/members`)
.set(as(s.owner.id))
.send({ identifier: s.joiner.email });
expect(res.status).toBe(201);
expect(await tripVotes(s.cafe.id)).toEqual({ [s.joiner.id]: 4 });
expect(realtime.broadcast).toHaveBeenCalledWith(
String(s.alps.id),
'place:updated',
{ place: expect.objectContaining({ id: s.cafe.id }) },
undefined,
);
});
it('never overwrites a vote already on the copy', async () => {
const s = await seed();
await insertRow(orm, PlaceRatings, { place: s.cafe.id, user: s.joiner.id, rating: 1 });
await request(server)
.post(`/api/trips/${s.alps.id}/members`)
.set(as(s.owner.id))
.send({ identifier: s.joiner.email });
expect(await tripVotes(s.cafe.id)).toEqual({ [s.joiner.id]: 1 });
});
});