fix(places): the second CI run, split the library reads and test the rating order

The reads of what an entry of All places keeps beside its facts (tags,
notes, votes, planned days, bookings, list roles) move from
PlacesLibraryRepository into PlacesLibraryDetailsRepository, so neither
file passes the size limit. New end to end tests cover the rating filter,
the tab counts under it, the rating order and its cursor, and
list_all_places with both. The test types follow the vote shapes, the edit
target says it may be null, the bookings pair names the booking card that
now holds the booking model, and the size baselines drop to the measured
lengths.
This commit is contained in:
Maurice
2026-11-30 00:06:47 +00:00
parent 7104fa6dd4
commit 1c9ac0b159
16 changed files with 531 additions and 316 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ const LIBRARY_RATING = `${COLLECTIONS}/library/places/rating`;
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
/** A voter as the entry rating names them. */
const voter = (user: { id: number; username: string }, rating: number) => ({
const voter = (user: { id: number; username?: string | null }, rating: number) => ({
user_id: user.id,
username: user.username,
avatar_url: null,
@@ -0,0 +1,161 @@
/**
* The rating filter and the rating order of All places, end to end on the
* whole app (buildApp, the real guards and a migrated temp database; in the
* Postgres lane on Postgres too): `min_rating` keeps the entries whose
* average vote (every person once, over the copies the reader reaches)
* reaches it and leaves out the unrated ones, the tab counts follow it, the
* order `rating` puts the best rated first and the unrated last, page by
* page through its cursor, and list_all_places answers the same.
*/
import { buildApp } from '../../src/bootstrap';
import { CollectionPlaceRatings } from '../../src/db/entities/CollectionPlaceRatings.entity';
import { PlaceRatings } from '../../src/db/entities/PlaceRatings.entity';
import type { McpContext } from '../../src/nest-mcp';
import { withRequestContext } from '../../src/nest/database/request-context';
import { PlacesLibraryMcp } from '../../src/nest/places-library/places-library.mcp';
import { makeCollection, makeCollectionPlace } from '../helpers/factories/collections';
import { makePlace } from '../helpers/factories/places';
import { insertRow } from '../helpers/factories/rows';
import { setAddonEnabled } from '../helpers/factories/settings';
import { addTripMember, makeTrip } from '../helpers/factories/trips';
import { makeUser } from '../helpers/factories/users';
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;
const LIBRARY = '/api/addons/collections/library/places';
const as = (userId: number) => ({ Cookie: sessionCookie(userId) });
interface Page {
entries: { key: string; name: string; rating?: { average: number | null } }[];
total: number;
counts: { all: number; trip: number; list: number };
next_cursor: string | null;
}
/**
* `me` has the trip Alps (with `friend` on it) holding Alpha (me 5, friend 3:
* 4 on average), Beta (me 5) and Gamma (no vote), and the list Food holding
* Delta (me 2). Each place stands alone, far from the others.
*/
async function seed() {
const { user: me } = await makeUser(orm);
const { user: friend } = await makeUser(orm);
const alps = await makeTrip(orm, me.id, { title: 'Alps' });
await addTripMember(orm, alps.id, friend.id);
const alpha = await makePlace(orm, alps.id, { name: 'Alpha', lat: 10, lng: 10 });
const beta = await makePlace(orm, alps.id, { name: 'Beta', lat: 20, lng: 20 });
await makePlace(orm, alps.id, { name: 'Gamma', lat: 30, lng: 30 });
const food = await makeCollection(orm, me.id, { name: 'Food' });
const delta = await makeCollectionPlace(orm, food.id, me.id, { name: 'Delta', lat: 40, lng: 40 });
await insertRow(orm, PlaceRatings, { place: alpha.id, user: me.id, rating: 5 });
await insertRow(orm, PlaceRatings, { place: alpha.id, user: friend.id, rating: 3 });
await insertRow(orm, PlaceRatings, { place: beta.id, user: me.id, rating: 5 });
await insertRow(orm, CollectionPlaceRatings, { collectionPlace: delta.id, user: me.id, rating: 2 });
return { me, friend, alps, food };
}
async function page(userId: number, query: Record<string, string | number>): Promise<Page> {
const res = await request(server).get(LIBRARY).query(query).set(as(userId));
expect(res.status).toBe(200);
return res.body as Page;
}
const names = (p: Page) => p.entries.map((entry) => entry.name);
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);
});
afterAll(async () => {
await app?.close();
});
describe('the rating filter of All places', () => {
it('keeps the entries whose average reaches the floor, and the counts follow it', async () => {
const s = await seed();
const four = await page(s.me.id, { min_rating: 4 });
expect(names(four).sort()).toEqual(['Alpha', 'Beta']);
expect(four.total).toBe(2);
expect(four.counts).toEqual({ all: 2, trip: 2, list: 0 });
const two = await page(s.me.id, { min_rating: '2' });
expect(names(two).sort()).toEqual(['Alpha', 'Beta', 'Delta']);
expect(two.counts).toEqual({ all: 3, trip: 2, list: 1 });
// Without the filter the unrated place is there too.
expect((await page(s.me.id, {})).counts.all).toBe(4);
});
it('narrows a kind as well, and refuses a floor outside 1 to 5', async () => {
const s = await seed();
expect(names(await page(s.me.id, { min_rating: 2, kind: 'list' }))).toEqual(['Delta']);
for (const bad of ['0', '6', '4.5', 'x']) {
const res = await request(server).get(LIBRARY).query({ min_rating: bad }).set(as(s.me.id));
expect(res.status).toBe(400);
}
});
it('averages over the votes the reader can see: the friend reaches the trip only', async () => {
const s = await seed();
const theirs = await page(s.friend.id, { min_rating: 3 });
expect(names(theirs).sort()).toEqual(['Alpha', 'Beta']);
expect(theirs.entries.find((entry) => entry.name === 'Alpha')?.rating?.average).toBe(4);
});
});
describe('the rating order of All places', () => {
it('puts the best rated first, a tie by name, and the unrated last', async () => {
const s = await seed();
expect(names(await page(s.me.id, { sort: 'rating' }))).toEqual(['Beta', 'Alpha', 'Delta', 'Gamma']);
});
it('pages through its cursor without a gap or a double', async () => {
const s = await seed();
const seen: string[] = [];
let cursor: string | null = null;
for (let i = 0; i < 5; i++) {
const query: Record<string, string | number> = { sort: 'rating', limit: 1 };
if (cursor) query.cursor = cursor;
const p = await page(s.me.id, query);
seen.push(...names(p));
cursor = p.next_cursor;
if (!cursor) break;
}
expect(seen).toEqual(['Beta', 'Alpha', 'Delta', 'Gamma']);
// A cursor of the name order is no cursor of the rating order.
const byName = await page(s.me.id, { limit: 1 });
const res = await request(server)
.get(LIBRARY)
.query({ sort: 'rating', cursor: byName.next_cursor ?? '' })
.set(as(s.me.id));
expect(res.status).toBe(400);
});
it('combines with the floor, and list_all_places answers the same', async () => {
const s = await seed();
expect(names(await page(s.me.id, { sort: 'rating', min_rating: 3 }))).toEqual(['Beta', 'Alpha']);
const tools = app.get(PlacesLibraryMcp);
const ctx: McpContext = { userId: s.me.id, scopes: null, isStaticToken: false };
const answer = await withRequestContext(orm, () =>
tools.listAllPlaces({ sort: 'rating', min_rating: 3 } as never, ctx),
);
const text = (answer as unknown as { content: { text: string }[] }).content[0]?.text ?? '';
expect((JSON.parse(text) as Page).entries.map((entry) => entry.name)).toEqual(['Beta', 'Alpha']);
});
});
@@ -1,9 +1,9 @@
import type {
LibraryCollectionPlaceRow,
LibraryOwnVoteRow,
LibraryTripPlaceRow,
LibraryUseRow,
} from '../../../../src/db/repositories/PlacesLibrary.repository';
import type { LibraryOwnVoteRow } from '../../../../src/db/repositories/PlacesLibraryDetails.repository';
import { votesToCarry, voteTargetsOf } from '../../../../src/nest/places-library/place-votes.helpers';
import { castAfter, unifiedRatingOf, type EntryRows } from '../../../../src/nest/places-library/places-library.helpers';
@@ -7,7 +7,7 @@
* the vote by entry key and the carry-over's inserts. The SQL behind it and
* the whole path run in tests/e2e/place-votes.e2e.test.ts.
*/
import { PlacesLibraryRepository } from '../../../../src/db/repositories/PlacesLibrary.repository';
import { PlacesLibraryDetailsRepository } from '../../../../src/db/repositories/PlacesLibraryDetails.repository';
import { AddonsService } from '../../../../src/nest/addons/addons.service';
import { CollectionsService } from '../../../../src/nest/collections/collections.service';
import { notFound } from '../../../../src/nest/common/domain-error';
@@ -62,7 +62,7 @@ async function build(copies: Copies, options: { addonOn?: boolean; uses?: unknow
{ provide: CollectionsService, useValue: collections },
{ provide: PlacesService, useValue: places },
{ provide: UnitOfWork, useValue: uow },
{ provide: PlacesLibraryRepository, useValue: repo },
{ provide: PlacesLibraryDetailsRepository, useValue: repo },
{ provide: AddonsService, useValue: addons },
{ provide: TripMembershipService, useValue: membership },
],
@@ -14,7 +14,7 @@ const BEFORE = { website: null, phone: null, email: null, opening_hours: null, d
const places = {
create: vi.fn(async (_trip: string, input: Record<string, unknown>) => ({ id: 21, ...input })),
update: vi.fn(async (_trip: string, id: string) => ({ id: Number(id), name: 'Updated' })),
update: vi.fn(async (_trip: string, id: string, ..._rest: unknown[]) => ({ id: Number(id), name: 'Updated' })),
setImageFromFile: vi.fn(async (_trip: string, id: string) => ({ id: Number(id) })),
updateMany: vi.fn(async (_trip: string, ids: number[]) => ids.map((id) => ({ id }))),
importGpx: vi.fn(async () => ({ places: [{ id: 31 }, { id: 32 }], count: 2, skipped: 0 })),