refactor(trips): trip access through TripsRepository; demo seed in a request context; dialect helpers for the trips cluster

This commit is contained in:
jubnl
2026-09-23 04:46:18 +02:00
parent 769b9577ec
commit d2e0d7e320
97 changed files with 1595 additions and 406 deletions
@@ -38,7 +38,7 @@ function build(overrides: {
collections?: Record<string, unknown>;
} = {}) {
const db = {
canAccessTrip: vi.fn(() => ({ id: 1, user_id: 42 })),
canAccessTrip: vi.fn(async () => ({ id: 1, user_id: 42 })),
prepare: vi.fn(() => ({ get: () => ({ role: 'user' }), all: () => [] })),
} as unknown as DatabaseService;
const guards = new PluginGuards(
@@ -56,7 +56,7 @@ function spyService(calls: string[], name: string) {
function build(addonOn: boolean) {
const calls: string[] = [];
const db = {
canAccessTrip: vi.fn(() => ({ id: 1, user_id: 42 })),
canAccessTrip: vi.fn(async () => ({ id: 1, user_id: 42 })),
prepare: vi.fn(() => ({ get: () => ({ role: 'user' }), all: () => [] })),
} as unknown as DatabaseService;
const guards = new PluginGuards(
@@ -8,7 +8,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled, tripDays } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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),
tripDays: { value: [{ id: 10 }, { id: 11 }] as Array<{ id: number }> },
}));
@@ -16,7 +16,6 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ all: () => tripDays.value }) },
canAccessTrip,
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
@@ -30,12 +29,12 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
providersOf: vi.fn(() => providers),
daySchedule: vi.fn(async (id: string) => invoke(id)),
} as unknown as PluginHooks;
return { c: new DayScheduleController(runtime, new DatabaseService(dbConn)), runtime };
return { c: new DayScheduleController(runtime, { canAccessTrip, connection: { prepare: () => ({ all: () => tripDays.value }) } } as unknown as DatabaseService), runtime };
}
const item = (over: Record<string, unknown> = {}) => ({ id: 's1', dayId: 10, label: 'Charging', ...over });
describe('DayScheduleController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
it('gates: disabled / no user / non-member all return [] (no plugin calls on the first)', async () => {
pluginsEnabled.mockReturnValue(false);
@@ -45,7 +44,7 @@ describe('DayScheduleController', () => {
pluginsEnabled.mockReturnValue(true);
expect((await controller(() => [item()]).c.get('1', req(undefined))).items).toEqual([]);
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect((await controller(() => [item()]).c.get('1', req(5))).items).toEqual([]);
});
@@ -8,7 +8,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled, tripDays } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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),
tripDays: { value: [{ id: 10 }, { id: 11 }] as Array<{ id: number }> },
}));
@@ -16,7 +16,6 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ all: () => tripDays.value }) },
canAccessTrip,
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
@@ -30,12 +29,12 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
providersOf: vi.fn(() => providers),
dayTints: vi.fn(async (id: string) => invoke(id)),
} as unknown as PluginHooks;
return { c: new DayTintsController(runtime, new DatabaseService(dbConn)), runtime };
return { c: new DayTintsController(runtime, { canAccessTrip, connection: { prepare: () => ({ all: () => tripDays.value }) } } as unknown as DatabaseService), runtime };
}
const tint = (over: Record<string, unknown> = {}) => ({ dayId: 10, tone: 'success', ...over });
describe('DayTintsController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
it('gates: disabled / no user / non-member all return [] (no plugin calls on the first)', async () => {
pluginsEnabled.mockReturnValue(false);
@@ -45,7 +44,7 @@ describe('DayTintsController', () => {
pluginsEnabled.mockReturnValue(true);
expect((await controller(() => [tint()]).c.get('1', req(undefined))).tints).toEqual([]);
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect((await controller(() => [tint()]).c.get('1', req(5))).tints).toEqual([]);
});
@@ -96,6 +96,16 @@ const userSettings = {
} as unknown as PluginUserSettingsService;
const dbs = new DatabaseService(mockDb);
// Plan 3c Task 0b: `canAccessTrip` is `TripsRepository.findAccessible` now,
// which needs a real EntityManager over the full migrated schema — this
// file's `mockDb` is a hand-trimmed `:memory:` table set (no `currency`
// column on `trips`, among others), so a real repository read isn't
// possible against it. Spied directly with the exact access rule the old
// `db/database` mock factory above encoded (trip 1 belongs to user 5, user 6
// is a member), which is now dead code for this purpose.
vi.spyOn(dbs, 'canAccessTrip').mockImplementation(async (tripId, userId) =>
Number(tripId) === 1 && (userId === 5 || userId === 6) ? { id: 1, user_id: 5, currency: null } : undefined,
);
const guards = new PluginGuards(dbs, permissions, addons);
const registry = createTestPluginRegistry([
new DbRpc(userSettings),
@@ -8,7 +8,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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 }));
@@ -35,7 +35,7 @@ const line = (n = 3, over: Record<string, unknown> = {}) => ({
const layer = (features: unknown[], over: Record<string, unknown> = {}) => ({ id: 'l1', features, ...over });
describe('MapLayersController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
it('gates: disabled / no user / non-member all return [] (no plugin calls on the first)', async () => {
pluginsEnabled.mockReturnValue(false);
@@ -45,7 +45,7 @@ describe('MapLayersController', () => {
pluginsEnabled.mockReturnValue(true);
expect((await controller(() => [layer([line()])]).c.get('1', req(undefined))).layers).toEqual([]);
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect((await controller(() => [layer([line()])]).c.get('1', req(5))).layers).toEqual([]);
});
@@ -7,7 +7,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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 }));
@@ -29,7 +29,7 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
const mk = (over: Record<string, unknown> = {}) => ({ id: 'm1', lat: 48.85, lng: 2.35, ...over });
describe('MapMarkersController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
it('gates: disabled / no user / non-member all return [] (no plugin calls on the first)', async () => {
pluginsEnabled.mockReturnValue(false);
@@ -39,7 +39,7 @@ describe('MapMarkersController', () => {
pluginsEnabled.mockReturnValue(true);
expect((await controller(() => [mk()]).c.get('1', req(undefined))).markers).toEqual([]);
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect((await controller(() => [mk()]).c.get('1', req(5))).markers).toEqual([]);
});
@@ -8,7 +8,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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 }));
@@ -30,7 +30,7 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
const sec = (over: Record<string, unknown> = {}) => ({ title: 'Weather', ...over });
describe('PdfSectionsController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ 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);
@@ -41,7 +41,7 @@ describe('PdfSectionsController', () => {
expect((await controller(() => [sec()]).c.get('abc', req(5))).sections).toEqual([]);
expect((await controller(() => [sec()]).c.get('1', req(undefined))).sections).toEqual([]);
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect((await controller(() => [sec()]).c.get('1', req(5))).sections).toEqual([]);
});
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, placeTrip, pluginsEnabled } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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)),
placeTrip: vi.fn((placeId: number) => (placeId === 7 ? { trip_id: 1 } : undefined)),
pluginsEnabled: vi.fn(() => true),
}));
@@ -9,7 +9,6 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ get: (placeId: number) => placeTrip(placeId) }) },
canAccessTrip,
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
@@ -24,11 +23,11 @@ function controller(over: Partial<PluginHooks> = {}) {
placeDetails: vi.fn(async (id: string) => (id === 'p2' ? [{ label: 'Rating', value: '4.5' }] : [{ label: 'Reviews', value: '12', url: 'https://x' }])),
...over,
} as unknown as PluginHooks;
return { c: new PlaceDetailsController(runtime, new DatabaseService(dbConn)), runtime };
return { c: new PlaceDetailsController(runtime, { canAccessTrip, connection: { prepare: () => ({ get: (placeId: number) => placeTrip(placeId) }) } } as unknown as DatabaseService), runtime };
}
describe('PlaceDetailsController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
it('returns [] when the runtime is disabled (no plugin calls)', async () => {
pluginsEnabled.mockReturnValue(false);
@@ -40,7 +39,7 @@ describe('PlaceDetailsController', () => {
it('returns [] for an unknown place or a place the caller cannot access', async () => {
const { c } = controller();
expect(await c.get('999', req(5))).toEqual({ providers: [] }); // place not found
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect(await c.get('7', req(5))).toEqual({ providers: [] }); // no access
});
@@ -30,7 +30,7 @@ const ctx = (actingUserId: number | undefined): PluginRpcContext => ({
/** Trip 1 belongs to user 42 and only user 42 may reach it, mirroring rpc-host.test.ts. */
function build(overrides: { role?: string | undefined; allow?: boolean; addonOn?: boolean } = {}) {
const db = {
canAccessTrip: vi.fn((tripId: number, userId: number) =>
canAccessTrip: vi.fn(async (tripId: number, userId: number) =>
tripId === 1 && userId === 42 ? { id: 1, user_id: 42 } : undefined,
),
prepare: vi.fn(() => ({
@@ -128,7 +128,7 @@ describe('PluginGuards — requireTripEdit and canEditAs', () => {
it('PGUARD-014 a non-owner member is flagged as shared', async () => {
const db = {
canAccessTrip: vi.fn(() => ({ id: 1, user_id: 7 })),
canAccessTrip: vi.fn(async () => ({ id: 1, user_id: 7 })),
prepare: vi.fn(() => ({ get: () => ({ role: 'user' }) })),
} as unknown as DatabaseService;
const permissions = { checkPermission: vi.fn(() => true) } as unknown as PermissionsService;
@@ -8,7 +8,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled, capabilitiesRow } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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),
capabilitiesRow: { value: JSON.stringify({ routeProfiles: [{ id: 'ev', label: 'EV' }] }) as string | undefined },
}));
@@ -16,7 +16,6 @@ vi.mock('../../../src/db/database', () => ({
db: { prepare: () => ({ get: () => (capabilitiesRow.value === undefined ? undefined : { capabilities: capabilitiesRow.value }) }) },
canAccessTrip,
}));
import { db as dbConn } from '../../../src/db/database';
import { DatabaseService } from '../../../src/nest/database/database.service';
vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
@@ -30,7 +29,7 @@ function controller(invoke: () => unknown, providers = ['ev-plug']) {
providersOf: vi.fn(() => providers),
route: vi.fn(async () => invoke()),
} as unknown as PluginHooks;
return { c: new PluginRoutesController(runtime, new DatabaseService(dbConn)), runtime };
return { c: new PluginRoutesController(runtime, { canAccessTrip, connection: { prepare: () => ({ get: () => (capabilitiesRow.value === undefined ? undefined : { capabilities: capabilitiesRow.value }) }) } } as unknown as DatabaseService), runtime };
}
const wp = (n = 3) => Array.from({ length: n }, (_, i) => ({ lat: 48 + i * 0.1, lng: 2 + i * 0.1 }));
const goodRoute = (n = 3, over: Record<string, unknown> = {}) => ({
@@ -45,7 +44,7 @@ const body = (over: Record<string, unknown> = {}) => ({ tripId: 1, dayId: 7, way
describe('PluginRoutesController', () => {
beforeEach(() => {
pluginsEnabled.mockReturnValue(true);
canAccessTrip.mockReturnValue({ id: 1 } as never);
canAccessTrip.mockResolvedValue({ id: 1 } as never);
capabilitiesRow.value = JSON.stringify({ routeProfiles: [{ id: 'ev', label: 'EV' }] });
});
@@ -57,9 +56,9 @@ describe('PluginRoutesController', () => {
pluginsEnabled.mockReturnValue(true);
expect((await controller(() => goodRoute()).c.route('ev-plug', 'ev', body(), req(undefined))).route).toBeNull();
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect((await controller(() => goodRoute()).c.route('ev-plug', 'ev', body(), req(5))).route).toBeNull();
canAccessTrip.mockReturnValue({ id: 1 } as never);
canAccessTrip.mockResolvedValue({ id: 1 } as never);
expect((await controller(() => goodRoute()).c.route('ev-plug', '../etc', body(), req(5))).route).toBeNull();
});
@@ -40,7 +40,7 @@ const ALL = new Set([
function guardsFor() {
const db = {
canAccessTrip: vi.fn(() => ({ id: 1, user_id: 42 })),
canAccessTrip: vi.fn(async () => ({ id: 1, user_id: 42 })),
prepare: vi.fn(() => ({ get: () => ({ role: 'user' }), all: () => [] })),
} as unknown as DatabaseService;
return {
@@ -9,7 +9,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
// trips 1 and 2 are accessible to user 5; everything else is not.
canAccessTrip: vi.fn((tripId: number, userId: number) => (userId === 5 && (tripId === 1 || tripId === 2) ? { id: tripId } : undefined)),
canAccessTrip: vi.fn(async (tripId: number, userId: number) => (userId === 5 && (tripId === 1 || tripId === 2) ? { id: tripId } : undefined)),
pluginsEnabled: vi.fn(() => true),
}));
vi.mock('../../../src/db/database', () => ({ db: { prepare: () => ({ get: () => undefined }) }, canAccessTrip }));
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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: {}, canAccessTrip }));
@@ -24,14 +24,14 @@ function controller(over: Partial<PluginHooks> = {}) {
}
describe('TripWarningsController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
it('returns [] when disabled / no user / no access', async () => {
pluginsEnabled.mockReturnValue(false);
expect(await controller().c.get('1', req(5))).toEqual({ warnings: [] });
pluginsEnabled.mockReturnValue(true);
expect(await controller().c.get('1', req(undefined))).toEqual({ warnings: [] });
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect(await controller().c.get('1', req(5))).toEqual({ warnings: [] });
});
@@ -8,7 +8,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
const { canAccessTrip, pluginsEnabled } = vi.hoisted(() => ({
canAccessTrip: vi.fn((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 }));
@@ -31,7 +31,7 @@ const col = (over: Record<string, unknown> = {}) => ({ kind: 'column', entityId:
const act = (over: Record<string, unknown> = {}) => ({ kind: 'action', entityId: 1, id: 'a1', label: 'Go', target: { kind: 'frame', sub: '/ui' }, ...over });
describe('ViewContributionsController', () => {
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockReturnValue({ id: 1 } as never); });
beforeEach(() => { pluginsEnabled.mockReturnValue(true); canAccessTrip.mockResolvedValue({ id: 1 } as never); });
it('gates: disabled / unknown view / no user / non-member all return [] (no plugin calls on the first two)', async () => {
pluginsEnabled.mockReturnValue(false);
@@ -52,7 +52,7 @@ describe('ViewContributionsController', () => {
}
expect((await controller(() => [col()]).c.get('places', '1', req(undefined))).contributions).toEqual([]);
canAccessTrip.mockReturnValue(undefined as never);
canAccessTrip.mockResolvedValue(undefined as never);
expect((await controller(() => [col()]).c.get('day', '1', req(5))).contributions).toEqual([]);
});