mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +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,6 +5,12 @@
|
||||
* fail-safe + server-side normalization — row count cap, label/value length caps,
|
||||
* a URL-scheme allowlist (no click-XSS).
|
||||
*/
|
||||
import type { JourneyEntriesRepository } from '../../../src/db/repositories/JourneyEntries.repository';
|
||||
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
import type { JourneyDomainService } from '../../../src/nest/journey/journey-domain.service';
|
||||
import { JournalEntryRowsController } from '../../../src/nest/plugins/contributions/journal-entry-rows.controller';
|
||||
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { entryJourney, canAccessJourney, isAddonEnabled, pluginsEnabled } = vi.hoisted(() => ({
|
||||
@@ -13,19 +19,16 @@ const { entryJourney, canAccessJourney, isAddonEnabled, pluginsEnabled } = vi.ho
|
||||
isAddonEnabled: vi.fn(() => true),
|
||||
pluginsEnabled: vi.fn(() => true),
|
||||
}));
|
||||
import type { JourneyDomainService } from '../../../src/nest/journey/journey-domain.service';
|
||||
import type { JourneyEntriesRepository } from '../../../src/db/repositories/JourneyEntries.repository';
|
||||
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
|
||||
|
||||
import { JournalEntryRowsController } from '../../../src/nest/plugins/contributions/journal-entry-rows.controller';
|
||||
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
|
||||
import type { AddonsService } from '../../../src/nest/addons/addons.service';
|
||||
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
|
||||
|
||||
const addonsStub = { isAddonEnabled } as unknown as AddonsService;
|
||||
// JEC1 (Plan 3g Task 4) — `JournalEntryRowsController` now injects
|
||||
// `JourneyEntriesRepository` instead of the raw `DatabaseService`/`db.prepare`
|
||||
// stub this test used to mock; `findById` stands in for the converted read.
|
||||
const journeyEntriesStub = { findById: vi.fn((entryId: number) => entryJourney(entryId)) } as unknown as JourneyEntriesRepository;
|
||||
const journeyEntriesStub = {
|
||||
findById: vi.fn((entryId: number) => entryJourney(entryId)),
|
||||
} as unknown as JourneyEntriesRepository;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const req = (id?: number) => ({ user: id === undefined ? undefined : { id } }) as any;
|
||||
@@ -34,7 +37,12 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
|
||||
providersOf: vi.fn(() => providers),
|
||||
journalRows: vi.fn(async (id: string) => invoke(id)),
|
||||
} as unknown as PluginHooks;
|
||||
return { c: new JournalEntryRowsController(runtime, journeyEntriesStub, addonsStub, { canAccessJourney } as unknown as JourneyDomainService), runtime };
|
||||
return {
|
||||
c: new JournalEntryRowsController(runtime, journeyEntriesStub, addonsStub, {
|
||||
canAccessJourney,
|
||||
} as unknown as JourneyDomainService),
|
||||
runtime,
|
||||
};
|
||||
}
|
||||
const row = (over: Record<string, unknown> = {}) => ({ label: 'Distance', value: '12 km', ...over });
|
||||
|
||||
@@ -63,19 +71,24 @@ describe('JournalEntryRowsController', () => {
|
||||
expect((await controller(() => [row()]).c.get('7', req(undefined))).providers).toEqual([]);
|
||||
expect((await controller(() => [row()]).c.get('999', req(5))).providers).toEqual([]); // entry not found
|
||||
canAccessJourney.mockReturnValue(null as never);
|
||||
expect((await controller(() => [row()]).c.get('7', req(5))).providers).toEqual([]); // no access
|
||||
expect((await controller(() => [row()]).c.get('7', req(5))).providers).toEqual([]); // no access
|
||||
});
|
||||
|
||||
it('keeps valid rows, groups them per plugin, passes the entry + acting user to the hook', async () => {
|
||||
const { c, runtime } = controller(() => [row({ url: 'https://ok.example' }), row({ label: 'Steps', value: '9000', url: 'mailto:x@y.z' })]);
|
||||
const { c, runtime } = controller(() => [
|
||||
row({ url: 'https://ok.example' }),
|
||||
row({ label: 'Steps', value: '9000', url: 'mailto:x@y.z' }),
|
||||
]);
|
||||
const res = await c.get('7', req(5));
|
||||
expect(res.providers).toEqual([{
|
||||
pluginId: 'p1',
|
||||
items: [
|
||||
{ label: 'Distance', value: '12 km', url: 'https://ok.example' },
|
||||
{ label: 'Steps', value: '9000', url: 'mailto:x@y.z' },
|
||||
],
|
||||
}]);
|
||||
expect(res.providers).toEqual([
|
||||
{
|
||||
pluginId: 'p1',
|
||||
items: [
|
||||
{ label: 'Distance', value: '12 km', url: 'https://ok.example' },
|
||||
{ label: 'Steps', value: '9000', url: 'mailto:x@y.z' },
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(runtime.journalRows).toHaveBeenCalledWith('p1', 7, 5);
|
||||
});
|
||||
|
||||
@@ -84,15 +97,15 @@ describe('JournalEntryRowsController', () => {
|
||||
row({ label: 'L'.repeat(200), value: 'V'.repeat(500), url: 'javascript:alert(1)' }),
|
||||
row({ url: '' }),
|
||||
row({ url: 'not a url' }),
|
||||
{ label: '', value: 'orphan' }, // no label
|
||||
{ value: 42 }, // still no label
|
||||
null, // non-object
|
||||
{ label: '', value: 'orphan' }, // no label
|
||||
{ value: 42 }, // still no label
|
||||
null, // non-object
|
||||
]);
|
||||
const items = (await c.get('7', req(5))).providers[0].items;
|
||||
expect(items).toHaveLength(3);
|
||||
expect(items[0].label.length).toBe(60);
|
||||
expect(items[0].value!.length).toBe(200);
|
||||
expect(items[0].url).toBeUndefined(); // click-XSS scheme dropped
|
||||
expect(items[0].url).toBeUndefined(); // click-XSS scheme dropped
|
||||
expect(items[1].url).toBeUndefined();
|
||||
expect(items[2].url).toBeUndefined();
|
||||
expect(items[2].value).toBe('12 km');
|
||||
@@ -100,14 +113,17 @@ describe('JournalEntryRowsController', () => {
|
||||
|
||||
it('caps at 12 rows per provider, omits empty providers, skips a failing one, tolerates non-arrays', async () => {
|
||||
const many = Array.from({ length: 15 }, (_, i) => row({ label: `r${i}` }));
|
||||
const { c } = controller((id) => {
|
||||
if (id === 'bad') throw new Error('boom');
|
||||
if (id === 'empty') return [{ value: 'no label' }]; // normalizes to nothing
|
||||
if (id === 'garbage') return 'not an array';
|
||||
return many;
|
||||
}, ['good', 'bad', 'empty', 'garbage']);
|
||||
const { c } = controller(
|
||||
(id) => {
|
||||
if (id === 'bad') throw new Error('boom');
|
||||
if (id === 'empty') return [{ value: 'no label' }]; // normalizes to nothing
|
||||
if (id === 'garbage') return 'not an array';
|
||||
return many;
|
||||
},
|
||||
['good', 'bad', 'empty', 'garbage'],
|
||||
);
|
||||
const res = await c.get('7', req(5));
|
||||
expect(res.providers).toHaveLength(1); // only 'good' survives
|
||||
expect(res.providers).toHaveLength(1); // only 'good' survives
|
||||
expect(res.providers[0].items).toHaveLength(12);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user