mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-22 23:16:46 +00:00
style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
This commit is contained in:
@@ -5,20 +5,22 @@
|
||||
* title/paragraph/header/cell, count caps on sections/paragraphs/headers/rows,
|
||||
* rows clipped to the header width, headerless tables dropped.
|
||||
*/
|
||||
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
|
||||
import { PdfSectionsController } from '../../../src/nest/plugins/contributions/pdf-sections.controller';
|
||||
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
|
||||
import { TripAccessService } from '../../../src/nest/trip-membership/trip-access.service';
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
|
||||
canAccessTrip: vi.fn(async (tripId: number, userId: number) => (tripId === 1 && userId === 5 ? { id: 1 } : undefined)),
|
||||
canAccessTrip: vi.fn(async (tripId: number, userId: number) =>
|
||||
tripId === 1 && userId === 5 ? { id: 1 } : undefined,
|
||||
),
|
||||
pluginsEnabled: vi.fn(() => true),
|
||||
}));
|
||||
vi.mock('../../../src/db/database', () => ({ db: { prepare: () => ({ get: () => undefined }) }, canAccessTrip }));
|
||||
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
|
||||
|
||||
import { PdfSectionsController } from '../../../src/nest/plugins/contributions/pdf-sections.controller';
|
||||
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
|
||||
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
|
||||
import { TripAccessService } from '../../../src/nest/trip-membership/trip-access.service';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const req = (id?: number) => ({ user: id === undefined ? undefined : { id } }) as any;
|
||||
function controller(invoke: (id: string) => unknown, providers = ['p1']) {
|
||||
@@ -26,12 +28,21 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
|
||||
providersOf: vi.fn(() => providers),
|
||||
pdfSections: vi.fn(async (id: string) => invoke(id)),
|
||||
} as unknown as PluginHooks;
|
||||
return { c: new PdfSectionsController(runtime, new TripAccessService({ findAccessible: canAccessTrip } as unknown as TripsRepository)), runtime };
|
||||
return {
|
||||
c: new PdfSectionsController(
|
||||
runtime,
|
||||
new TripAccessService({ findAccessible: canAccessTrip } as unknown as TripsRepository),
|
||||
),
|
||||
runtime,
|
||||
};
|
||||
}
|
||||
const sec = (over: Record<string, unknown> = {}) => ({ title: 'Weather', ...over });
|
||||
|
||||
describe('PdfSectionsController', () => {
|
||||
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
|
||||
beforeEach(() => {
|
||||
pluginsEnabled.mockReturnValue(true);
|
||||
canAccessTrip.mockResolvedValue({ id: 1 } as never);
|
||||
});
|
||||
|
||||
it('gates: disabled / bad tripId / no user / non-member all return [] (no plugin calls on the first)', async () => {
|
||||
pluginsEnabled.mockReturnValue(false);
|
||||
@@ -51,21 +62,23 @@ describe('PdfSectionsController', () => {
|
||||
sec({ paragraphs: ['Sunny all week'], table: { headers: ['Day', 'Temp'], rows: [['Mon', '24°C']] } }),
|
||||
]);
|
||||
const out = (await c.get('1', req(5))).sections;
|
||||
expect(out).toEqual([{
|
||||
pluginId: 'p1',
|
||||
title: 'Weather',
|
||||
paragraphs: ['Sunny all week'],
|
||||
table: { headers: ['Day', 'Temp'], rows: [['Mon', '24°C']] },
|
||||
}]);
|
||||
expect(out).toEqual([
|
||||
{
|
||||
pluginId: 'p1',
|
||||
title: 'Weather',
|
||||
paragraphs: ['Sunny all week'],
|
||||
table: { headers: ['Day', 'Temp'], rows: [['Mon', '24°C']] },
|
||||
},
|
||||
]);
|
||||
expect(runtime.pdfSections).toHaveBeenCalledWith('p1', 1, 5);
|
||||
});
|
||||
|
||||
it('drops non-objects, untitled sections and a non-array result; coerces + caps the title', async () => {
|
||||
const { c } = controller(() => [
|
||||
null, // non-object
|
||||
sec({ title: '' }), // no heading
|
||||
sec({ title: undefined }), // still no heading
|
||||
sec({ title: 'T'.repeat(500) }), // survivor, capped
|
||||
null, // non-object
|
||||
sec({ title: '' }), // no heading
|
||||
sec({ title: undefined }), // still no heading
|
||||
sec({ title: 'T'.repeat(500) }), // survivor, capped
|
||||
]);
|
||||
const out = (await c.get('1', req(5))).sections;
|
||||
expect(out).toHaveLength(1);
|
||||
@@ -90,21 +103,18 @@ describe('PdfSectionsController', () => {
|
||||
sec({
|
||||
table: {
|
||||
headers: Array.from({ length: 10 }, () => 'H'.repeat(100)),
|
||||
rows: [
|
||||
...Array.from({ length: 55 }, () => Array.from({ length: 12 }, () => 'C'.repeat(300))),
|
||||
'not a row',
|
||||
],
|
||||
rows: [...Array.from({ length: 55 }, () => Array.from({ length: 12 }, () => 'C'.repeat(300))), 'not a row'],
|
||||
},
|
||||
}),
|
||||
sec({ table: { headers: [], rows: [['x']] } }), // no headers -> no width to clip to
|
||||
sec({ table: 'garbage' }), // not an object
|
||||
sec({ table: 'garbage' }), // not an object
|
||||
]);
|
||||
const out = (await c.get('1', req(5))).sections;
|
||||
const table = out[0].table!;
|
||||
expect(table.headers).toHaveLength(8);
|
||||
expect(table.headers[0].length).toBe(60);
|
||||
expect(table.rows).toHaveLength(50);
|
||||
expect(table.rows[0]).toHaveLength(8); // clipped to the header width
|
||||
expect(table.rows[0]).toHaveLength(8); // clipped to the header width
|
||||
expect(table.rows[0][0].length).toBe(200);
|
||||
expect(out[1].table).toBeUndefined();
|
||||
expect(out[2].table).toBeUndefined();
|
||||
@@ -112,7 +122,15 @@ describe('PdfSectionsController', () => {
|
||||
|
||||
it('caps the section count at 5 per provider and skips a failing provider', async () => {
|
||||
const many = Array.from({ length: 8 }, (_, i) => sec({ title: `S${i}` }));
|
||||
const { c } = controller((id) => (id === 'bad' ? (() => { throw new Error('boom'); })() : many), ['good', 'bad']);
|
||||
const { c } = controller(
|
||||
(id) =>
|
||||
id === 'bad'
|
||||
? (() => {
|
||||
throw new Error('boom');
|
||||
})()
|
||||
: many,
|
||||
['good', 'bad'],
|
||||
);
|
||||
const out = (await c.get('1', req(5))).sections;
|
||||
expect(out).toHaveLength(5); // good capped to 5; bad contributes nothing
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user