mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
Move the tags plugin surface onto the decorators
Wires the registry into the host and takes the first domain across, so the kit lands with a worked example rather than as scaffolding nobody uses yet. The registry is an additive, optional fourth constructor parameter on PluginRpcHost, so all 107 existing construction sites keep compiling untouched; that is what makes the rest of the rollout a per-domain move instead of a big-bang one. bindInto runs LAST in the constructor, so a method still owned by one of the has() blocks collides loudly instead of being silently overridden. tags is the right pilot: four methods, a service that is already DI-native, and it exercises the read/write permission split, the userless refusal and a BadParams write. Handler bodies and messages are verbatim; only deps.foo(...) became this.tags... and uid became ctx.actingUserId. dispatch now strips the supervisor's _inv marker. The supervisor resolves the acting user from it before dispatch and then passes the request on untouched, so it has been visible to every handler and is in effect a reserved param name. Nothing reads it: auditResource takes named fields only, and the one handler that touches the params object as a whole (packing.setBagMembers) reads a named field off it. Dropping it keeps a future strict parse from rejecting calls every shipped plugin makes. Two tests carry the rollout from here. rpc-coverage.test.ts derives both owner sets from the code and proves they partition the method vocabulary exactly, so a PR that moves a method and forgets to delete the legacy registration fails in CI. And because a decorated class that no module registers is invisible to discovery, with every call to it coming back PERMISSION_DENIED and no other symptom, tags.rpc.test.ts asserts TagsRpc is actually listed in providers.
This commit is contained in:
@@ -800,15 +800,8 @@ describe('host-deps factory — Wave 1 wiring (weather/categories/tags/todos/ros
|
||||
expect((await call(host('db:read:categories'), 'categories.list', {}, undefined)).ok).toBe(true)
|
||||
})
|
||||
|
||||
it('tags: list/create/update/delete are the acting user\'s; a missing tag is RESOURCE_FORBIDDEN', async () => {
|
||||
const h = host('db:read:tags', 'db:write:tags')
|
||||
expect((await call(h, 'tags.list', {}, 5)).ok).toBe(true)
|
||||
expect((await call(h, 'tags.create', { input: { name: 'x' } }, 5)).ok).toBe(true)
|
||||
expect((await call(h, 'tags.update', { tagId: 1, input: { name: 'y' } }, 5)).ok).toBe(true)
|
||||
expect((await call(h, 'tags.delete', { tagId: 1 }, 5)).ok).toBe(true)
|
||||
expect(((await call(h, 'tags.update', { tagId: 404, input: {} }, 5)) as { error: { code: string } }).error.code).toBe('RESOURCE_FORBIDDEN')
|
||||
expect(((await call(h, 'tags.delete', { tagId: 404 }, 5)) as { error: { code: string } }).error.code).toBe('RESOURCE_FORBIDDEN')
|
||||
})
|
||||
// tags.* left this factory with the decorator migration. The same cases now run in
|
||||
// tests/unit/tags/tags.rpc.test.ts against TagsRpc.
|
||||
|
||||
it('trips.members returns the roster', async () => {
|
||||
const r = await call(host('db:read:trips'), 'trips.members', { tripId: 1 }, 5)
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* The coverage ledger: the anchor for the whole decorator rollout.
|
||||
*
|
||||
* Every wire method is owned exactly once, either by the legacy router blocks in
|
||||
* rpc-host.ts or by a decorated *.rpc.ts class in the registry. Both sets are derived
|
||||
* by construction, so no list is maintained by hand and the test cannot drift.
|
||||
*
|
||||
* It fails the moment a migration PR moves a method and forgets to delete the legacy
|
||||
* registration, deletes one without adding it, or forgets to list a new *.rpc.ts class
|
||||
* in allRpcControllers().
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { PluginRpcHost } from '../../../src/nest/plugins/host/rpc-host';
|
||||
import { createTestPluginRegistry } from '../../../src/nest/plugins/host/rpc-kit/testing';
|
||||
import {
|
||||
KNOWN_METHODS,
|
||||
KNOWN_PERMISSIONS,
|
||||
UNCONDITIONAL_METHODS,
|
||||
} from '../../../src/nest/plugins/protocol/envelope';
|
||||
import { allRpcControllers, makeDeps } from '../../helpers/rpc-host-deps';
|
||||
|
||||
/** Reads the router's private dispatch map. That map IS the thing under test. */
|
||||
const boundMethods = (host: PluginRpcHost): Set<string> =>
|
||||
new Set((host as unknown as { methods: Map<string, unknown> }).methods.keys());
|
||||
|
||||
describe('plugin RPC coverage ledger', () => {
|
||||
const registry = createTestPluginRegistry(allRpcControllers());
|
||||
// Every permission granted, so the router registers everything it can.
|
||||
const host = new PluginRpcHost('probe', new Set<string>(KNOWN_PERMISSIONS), makeDeps(), registry);
|
||||
const all = boundMethods(host);
|
||||
const fromRegistry = registry.methodNames();
|
||||
const legacy = new Set([...all].filter((m) => !fromRegistry.has(m)));
|
||||
|
||||
it('RPCLEDGER-001 no method is owned by both the registry and the legacy router', () => {
|
||||
expect([...fromRegistry].filter((m) => legacy.has(m))).toEqual([]);
|
||||
});
|
||||
|
||||
it('RPCLEDGER-002 every registered method is one the wire protocol knows', () => {
|
||||
const vocabulary = new Set<string>([...KNOWN_METHODS, ...UNCONDITIONAL_METHODS]);
|
||||
expect([...all].filter((m) => !vocabulary.has(m))).toEqual([]);
|
||||
});
|
||||
|
||||
it('RPCLEDGER-003 no method in the vocabulary is orphaned', () => {
|
||||
const vocabulary = new Set<string>([...KNOWN_METHODS, ...UNCONDITIONAL_METHODS]);
|
||||
expect([...vocabulary].filter((m) => !all.has(m))).toEqual([]);
|
||||
});
|
||||
|
||||
it('RPCLEDGER-004 the two owner sets partition the vocabulary exactly', () => {
|
||||
expect(all).toEqual(new Set<string>([...KNOWN_METHODS, ...UNCONDITIONAL_METHODS]));
|
||||
expect(fromRegistry.size + legacy.size).toBe(all.size);
|
||||
});
|
||||
|
||||
it('RPCLEDGER-005 the migration ledger reads as expected for this PR', () => {
|
||||
// Update these two numbers in every rollout PR. They make the diff state the size
|
||||
// of the move out loud, which is the number a reviewer wants to see.
|
||||
const total = KNOWN_METHODS.length + UNCONDITIONAL_METHODS.length;
|
||||
expect(fromRegistry.size).toBe(4); // tags.list / create / update / delete
|
||||
expect(legacy.size).toBe(total - 4);
|
||||
});
|
||||
|
||||
it('RPCLEDGER-006 the migrated methods are exactly the ones this PR claims', () => {
|
||||
expect([...fromRegistry].sort()).toEqual(['tags.create', 'tags.delete', 'tags.list', 'tags.update']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* The supervisor tags every host-bound request with `_inv`, resolves the acting user
|
||||
* from it BEFORE dispatch, and then passes the request on untouched. That made `_inv`
|
||||
* visible to every handler and, in effect, a reserved param name.
|
||||
*
|
||||
* dispatch() now strips it. These tests prove the strip is invisible: the same result,
|
||||
* the same audit row, and the acting user still resolved from the host side.
|
||||
*/
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { PluginRpcHost } from '../../../src/nest/plugins/host/rpc-host';
|
||||
import type { RpcRequest, RpcResponse } from '../../../src/nest/plugins/protocol/envelope';
|
||||
import { makeDeps } from '../../helpers/rpc-host-deps';
|
||||
|
||||
const req = (method: string, params: Record<string, unknown>): RpcRequest => ({ k: 'req', id: 'x', method, params });
|
||||
|
||||
describe('dispatch strips the supervisor _inv marker', () => {
|
||||
it('RPCINV-001 the handler never sees _inv', async () => {
|
||||
const deps = makeDeps();
|
||||
const host = new PluginRpcHost('p', new Set(['db:own']), deps);
|
||||
await host.dispatch(req('db.query', { sql: 'SELECT 1', args: [], _inv: 'req-7' }));
|
||||
expect(deps.data.query).toHaveBeenCalledWith('SELECT 1', []);
|
||||
});
|
||||
|
||||
it('RPCINV-002 the result is identical with and without the marker', async () => {
|
||||
const host = new PluginRpcHost('p', new Set(['db:own']), makeDeps());
|
||||
const withMarker = (await host.dispatch(req('db.query', { sql: 'SELECT 1', _inv: 'req-7' }))) as RpcResponse;
|
||||
const without = (await host.dispatch(req('db.query', { sql: 'SELECT 1' }))) as RpcResponse;
|
||||
expect(withMarker.ok).toBe(true);
|
||||
expect(withMarker.result).toEqual(without.result);
|
||||
});
|
||||
|
||||
it('RPCINV-003 the audit row is unchanged, because auditResource reads named fields only', async () => {
|
||||
const deps = makeDeps();
|
||||
const audit = vi.fn();
|
||||
const host = new PluginRpcHost('p', new Set(['db:read:trips']), { ...deps, audit });
|
||||
await host.dispatch(req('trips.getById', { tripId: 1, _inv: 'req-7' }), 42);
|
||||
expect(audit).toHaveBeenCalledWith({
|
||||
pluginId: 'p',
|
||||
actingUserId: 42,
|
||||
method: 'trips.getById',
|
||||
resource: 'trip:1',
|
||||
code: 'ok',
|
||||
});
|
||||
});
|
||||
|
||||
it('RPCINV-004 the acting user still comes from the host, not from the params', async () => {
|
||||
const deps = makeDeps();
|
||||
const host = new PluginRpcHost('p', new Set(['db:read:trips']), deps);
|
||||
// No acting user bound: the trip read is refused even though _inv is present.
|
||||
const res = await host.dispatch(req('trips.getById', { tripId: 1, _inv: 'req-7' }));
|
||||
expect(res.ok).toBe(false);
|
||||
expect((res as { error: { code: string } }).error.code).toBe('RESOURCE_FORBIDDEN');
|
||||
});
|
||||
|
||||
it('RPCINV-005 params without the marker are passed through untouched', async () => {
|
||||
const deps = makeDeps();
|
||||
const host = new PluginRpcHost('p', new Set(['db:own']), deps);
|
||||
await host.dispatch(req('db.exec', { sql: 'INSERT INTO t VALUES (1)', args: [1] }));
|
||||
expect(deps.data.exec).toHaveBeenCalledWith('INSERT INTO t VALUES (1)', [1]);
|
||||
});
|
||||
|
||||
it('RPCINV-006 a handler reading the whole payload object no longer finds _inv in it', async () => {
|
||||
const deps = makeDeps();
|
||||
const host = new PluginRpcHost('p', new Set(['db:write:packing']), deps);
|
||||
// packing.setBagMembers is the one handler that reads the params object as a
|
||||
// whole (asPayload(p).userIds) rather than a named field off it.
|
||||
await host.dispatch(req('packing.setBagMembers', { tripId: 1, bagId: 80, userIds: [3], _inv: 'req-7' }), 42);
|
||||
expect(deps.setPackingBagMembers).toHaveBeenCalledWith(1, 80, [3]);
|
||||
});
|
||||
});
|
||||
@@ -10,162 +10,8 @@ import Database from 'better-sqlite3';
|
||||
import { createTables } from '../../../src/db/schema';
|
||||
import { PluginRpcHost, type HostDeps } from '../../../src/nest/plugins/host/rpc-host';
|
||||
import type { RpcRequest, RpcResponse, RpcError } from '../../../src/nest/plugins/protocol/envelope';
|
||||
import { makeDeps } from '../../helpers/rpc-host-deps';
|
||||
|
||||
function makeDeps(): HostDeps {
|
||||
return {
|
||||
data: {
|
||||
query: vi.fn(() => [{ n: 1 }]),
|
||||
exec: vi.fn(() => ({ changes: 1 })),
|
||||
migrate: vi.fn(() => ({ applied: true })),
|
||||
close: vi.fn(),
|
||||
} as unknown as HostDeps['data'],
|
||||
db: {
|
||||
prepare: vi.fn((sql: string) => ({
|
||||
all: () => [{ id: 7, name: 'Place' }],
|
||||
get: () =>
|
||||
sql.includes('FROM trips')
|
||||
? { id: 1, title: 'Japan', start_date: '2027-01-01' }
|
||||
: { id: 3, username: 'ada', display_name: 'Ada', avatar: null },
|
||||
})),
|
||||
},
|
||||
// trip 1 is accessible to user 42; everything else is not
|
||||
canAccessTrip: vi.fn((tripId: number, userId: number) => (tripId === 1 && userId === 42 ? { id: 1 } : undefined)),
|
||||
// user 42 may see user 3 (they share a trip); nobody else
|
||||
canSeeUser: vi.fn((actingUserId: number, targetUserId: number) => actingUserId === 42 && targetUserId === 3),
|
||||
broadcastToTrip: vi.fn(),
|
||||
broadcastToUser: vi.fn(),
|
||||
// Costs (budget) — addon on; user 42 may edit trip 1's costs.
|
||||
budgetAddonEnabled: vi.fn(() => true),
|
||||
canEditCosts: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
listPackingItems: vi.fn((tripId: number, _userId: number) => [{ id: 1, trip_id: tripId, name: 'Socks' }]),
|
||||
listTripFiles: vi.fn((tripId: number) => [{ id: 2, trip_id: tripId, filename: 'visa.pdf' }]),
|
||||
getTripFileContent: vi.fn((tripId: number, fileId: number) => ({ name: 'visa.pdf', mimetype: 'application/pdf', size: 3, content_base64: 'aGk=', _t: tripId, _f: fileId })),
|
||||
listCollabNotes: vi.fn((tripId: number) => [{ id: 1, trip_id: tripId, title: 'Note' }]),
|
||||
listCollabPolls: vi.fn((tripId: number) => [{ id: 2, trip_id: tripId, question: 'Q?' }]),
|
||||
listCollabMessages: vi.fn((tripId: number, before: number | undefined) => [{ id: 3, trip_id: tripId, text: 'hi', _before: before ?? null }]),
|
||||
journalEntriesForUser: vi.fn((uid: number, journeyId: number) => [{ id: 10, journey_id: journeyId, author_id: uid }]),
|
||||
atlasBucketForUser: vi.fn((uid: number) => [{ id: 5, user_id: uid, name: 'Kyoto' }]),
|
||||
canCreateTrip: vi.fn((userId: number) => userId === 42),
|
||||
createTripForUser: vi.fn((userId: number, input: unknown) => ({ id: 99, user_id: userId, ...(input as object) })),
|
||||
getRates: vi.fn(async (base: string) => ({ [base]: 1, USD: 1.08 })),
|
||||
listCostsForTrip: vi.fn((tripId: number) => [{ id: 5, trip_id: tripId, name: 'Hotel', total_price: 100 }]),
|
||||
listCostsForUser: vi.fn(() => [
|
||||
{ id: 5, trip_id: 1, name: 'Hotel' },
|
||||
{ id: 6, trip_id: 2, name: 'Food' },
|
||||
]),
|
||||
createCost: vi.fn((tripId: number, input: unknown) => ({ id: 9, trip_id: tripId, ...(input as object) })),
|
||||
updateCost: vi.fn((tripId: number, itemId: number, input: unknown) => ({ id: itemId, trip_id: tripId, ...(input as object) })),
|
||||
deleteCost: vi.fn(() => ({ deleted: true })),
|
||||
// Planner writes — user 42 may edit trip 1 only (mirrors canAccessTrip).
|
||||
canEditPlaces: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
createPlace: vi.fn((tripId: number, input: unknown) => ({ id: 10, trip_id: tripId, ...(input as object) })),
|
||||
updatePlace: vi.fn((tripId: number, placeId: number, input: unknown) => ({ id: placeId, trip_id: tripId, ...(input as object) })),
|
||||
deletePlace: vi.fn(() => ({ deleted: true })),
|
||||
canEditDays: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
createDay: vi.fn((tripId: number, input: unknown) => ({ id: 20, trip_id: tripId, ...(input as object) })),
|
||||
updateDay: vi.fn((tripId: number, dayId: number, input: unknown) => ({ id: dayId, trip_id: tripId, ...(input as object) })),
|
||||
deleteDay: vi.fn(() => ({ deleted: true })),
|
||||
assignPlaceToDay: vi.fn((tripId: number, dayId: number, placeId: number, notes: string | null) => ({ id: 30, day_id: dayId, place_id: placeId, notes })),
|
||||
unassignPlace: vi.fn(() => ({ deleted: true })),
|
||||
canEditTrip: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
updateTrip: vi.fn((tripId: number, _userId: number, input: unknown) => ({ id: tripId, ...(input as object) })),
|
||||
// Cross-trip reads + reservations (bookings) — user 42 may edit trip 1 only.
|
||||
listTripsForUser: vi.fn(() => [{ id: 1, title: 'Japan' }, { id: 2, title: 'Peru' }]),
|
||||
listReservationsForUser: vi.fn(() => [{ id: 5, trip_id: 1, title: 'Hotel' }, { id: 6, trip_id: 2, title: 'Flight' }]),
|
||||
canEditReservations: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
createReservation: vi.fn((tripId: number, input: unknown) => ({ id: 40, trip_id: tripId, ...(input as object) })),
|
||||
updateReservation: vi.fn((tripId: number, reservationId: number, input: unknown) => ({ id: reservationId, trip_id: tripId, ...(input as object) })),
|
||||
deleteReservation: vi.fn(() => ({ deleted: true })),
|
||||
// Trip-scoped hydrated reads + accommodations (lodging blocks, day_edit-gated).
|
||||
listTripDays: vi.fn((tripId: number) => [{ id: 3, trip_id: tripId, day_number: 1, assignments: [], notes_items: [] }]),
|
||||
listTripReservations: vi.fn((tripId: number) => [{ id: 5, trip_id: tripId, title: 'Hotel', endpoints: [], day_positions: null }]),
|
||||
listTripAccommodations: vi.fn((tripId: number) => [{ id: 11, trip_id: tripId, place_name: 'Ryokan' }]),
|
||||
createAccommodation: vi.fn((tripId: number, input: unknown) => ({ id: 60, trip_id: tripId, ...(input as object) })),
|
||||
updateAccommodation: vi.fn((tripId: number, accommodationId: number, input: unknown) => ({ id: accommodationId, trip_id: tripId, ...(input as object) })),
|
||||
deleteAccommodation: vi.fn(() => ({ deleted: true })),
|
||||
// User-scoped addon reads + day notes.
|
||||
listJournalsForUser: vi.fn(() => [{ id: 1, title: 'Japan 2027' }]),
|
||||
atlasVisitedForUser: vi.fn(() => ({ countries: [{ country_code: 'JP' }], regions: [] })),
|
||||
vacayForUser: vi.fn(() => ({ plan: { id: 1 }, entries: [] })),
|
||||
listCollectionsForUser: vi.fn(() => ({ collections: [{ id: 1, name: 'Tokyo eats' }] })),
|
||||
getCollectionForUser: vi.fn((_userId: number, id: number) => ({ id, name: 'Tokyo eats', places: [] })),
|
||||
listDayNotes: vi.fn((tripId: number, dayId: number) => [{ id: 1, day_id: dayId, trip_id: tripId, text: 'note' }]),
|
||||
createDayNote: vi.fn((_tripId: number, dayId: number, input: unknown) => ({ id: 50, day_id: dayId, ...(input as object) })),
|
||||
updateDayNote: vi.fn((_tripId: number, dayId: number, noteId: number, input: unknown) => ({ id: noteId, day_id: dayId, ...(input as object) })),
|
||||
deleteDayNote: vi.fn(() => ({ deleted: true })),
|
||||
canEditPacking: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
createPackingItem: vi.fn((tripId: number, input: unknown) => ({ id: 70, trip_id: tripId, ...(input as object) })),
|
||||
updatePackingItem: vi.fn((tripId: number, itemId: number, input: unknown) => ({ id: itemId, trip_id: tripId, ...(input as object) })),
|
||||
deletePackingItem: vi.fn(() => ({ deleted: true })),
|
||||
listPackingBags: vi.fn(() => [{ id: 80, name: 'Backpack' }]),
|
||||
createPackingBag: vi.fn((tripId: number, input: unknown) => ({ id: 80, trip_id: tripId, ...(input as object) })),
|
||||
updatePackingBag: vi.fn((_tripId: number, bagId: number, input: unknown) => ({ id: bagId, ...(input as object) })),
|
||||
deletePackingBag: vi.fn(() => ({ deleted: true })),
|
||||
setPackingBagMembers: vi.fn((_tripId: number, bagId: number, userIds: number[]) => ({ bagId, members: userIds })),
|
||||
getWeather: vi.fn(() => ({ temp: 20 })),
|
||||
listCategories: vi.fn(() => [{ id: 1, name: 'Food' }]),
|
||||
tripMembers: vi.fn(() => [{ id: 5, username: 'ada' }, { id: 6, username: 'bob' }]),
|
||||
listTagsForUser: vi.fn((uid: number) => [{ id: 1, user_id: uid, name: 'work' }]),
|
||||
createTagForUser: vi.fn((uid: number, name: string) => ({ id: 9, user_id: uid, name })),
|
||||
updateTagForUser: vi.fn((_uid: number, tagId: number, name?: string) => ({ id: tagId, name })),
|
||||
deleteTagForUser: vi.fn(() => ({ deleted: true })),
|
||||
canUploadFiles: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
canEditFiles: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
canDeleteFiles: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
createTripFile: vi.fn((tripId: number, input: unknown, uid: number) => ({ id: 130, trip_id: tripId, uploaded_by: uid, ...(input as object) })),
|
||||
createTripFileLink: vi.fn(() => [{ file_id: 130 }]),
|
||||
updateTripFile: vi.fn((_tripId: number, fileId: number, input: unknown) => ({ id: fileId, ...(input as object) })),
|
||||
softDeleteTripFile: vi.fn(() => ({ deleted: true })),
|
||||
canEditCollab: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
createCollabNote: vi.fn((tripId: number, input: unknown, uid: number) => ({ id: 140, trip_id: tripId, created_by: uid, ...(input as object) })),
|
||||
createCollabPoll: vi.fn((tripId: number, input: unknown) => ({ id: 141, trip_id: tripId, ...(input as object) })),
|
||||
voteCollabPoll: vi.fn((_tripId: number, pollId: number) => ({ id: pollId, votes: 1 })),
|
||||
createCollabMessage: vi.fn((tripId: number, text: string) => ({ id: 142, trip_id: tripId, text })),
|
||||
canManageMembers: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
addTripMember: vi.fn((tripId: number, targetUserId: number, invitedBy: number) => ({ joined: true, tripId, targetUserId, invitedBy })),
|
||||
removeTripMember: vi.fn((tripId: number, targetUserId: number) => ({ removed: true, tripId, targetUserId })),
|
||||
canAccessTripForNotify: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
sendPluginNotification: vi.fn(async (pluginId: string, input: unknown) => ({ sent: true, pluginId, ...(input as object) })),
|
||||
aiConfigured: vi.fn((userId: number) => userId === 42),
|
||||
aiComplete: vi.fn(async (_uid: number, prompt: string) => ({ text: `echo:${prompt}` })),
|
||||
aiExtract: vi.fn(async () => ({ results: [{ ok: true }] })),
|
||||
getUserSetting: vi.fn((pluginId: string, userId: number, key: string) => (userId === 42 && key === 'apiKey' ? `secret-of-${pluginId}` : undefined)),
|
||||
getOAuthToken: vi.fn(async (_pluginId: string, userId: number) => (userId === 42 ? 'access-token-xyz' : null)),
|
||||
schedulerSet: vi.fn((name: string, dueAt: number, everyMs: number | undefined) => ({ scheduled: true, name, dueAt, everyMs })),
|
||||
schedulerCancel: vi.fn((name: string) => ({ cancelled: name !== 'ghost' })),
|
||||
createCollectionForUser: vi.fn((uid: number, input: unknown) => ({ id: 100, owner_id: uid, ...(input as object) })),
|
||||
updateCollectionForUser: vi.fn((_uid: number, id: number, input: unknown) => ({ id, ...(input as object) })),
|
||||
saveCollectionPlace: vi.fn((uid: number, input: unknown) => ({ id: 101, saved_by: uid, ...(input as object) })),
|
||||
copyCollectionToTrip: vi.fn(() => ({ copied: 2, skipped: [] })),
|
||||
deleteCollectionPlace: vi.fn(() => ({ deleted: true })),
|
||||
markCountryVisited: vi.fn(() => ({ visited: true })),
|
||||
unmarkCountryVisited: vi.fn(() => ({ visited: false })),
|
||||
markRegionVisited: vi.fn(() => ({ visited: true })),
|
||||
unmarkRegionVisited: vi.fn(() => ({ visited: false })),
|
||||
createBucketItem: vi.fn((uid: number, input: unknown) => ({ id: 110, user_id: uid, ...(input as object) })),
|
||||
deleteBucketItem: vi.fn(() => ({ deleted: true })),
|
||||
vacayToggleEntry: vi.fn(() => ({ action: 'added' })),
|
||||
vacayToggleCompanyHoliday: vi.fn(() => ({ action: 'added' })),
|
||||
createJournalEntry: vi.fn((uid: number, journeyId: number, input: unknown) => ({ id: 120, journey_id: journeyId, created_by: uid, ...(input as object) })),
|
||||
updateJournalEntry: vi.fn((_uid: number, entryId: number, input: unknown) => ({ id: entryId, ...(input as object) })),
|
||||
deleteJournalEntry: vi.fn(() => ({ deleted: true })),
|
||||
createJournal: vi.fn((uid: number, input: unknown) => ({ id: 130, user_id: uid, ...(input as object) })),
|
||||
deleteJournal: vi.fn(() => ({ deleted: true })),
|
||||
canEditTodos: vi.fn((tripId: number, userId: number) => tripId === 1 && userId === 42),
|
||||
listTodos: vi.fn(() => [{ id: 1, name: 'Pack' }]),
|
||||
createTodo: vi.fn((tripId: number, input: unknown) => ({ id: 90, trip_id: tripId, ...(input as object) })),
|
||||
updateTodo: vi.fn((_tripId: number, todoId: number, input: unknown) => ({ id: todoId, ...(input as object) })),
|
||||
deleteTodo: vi.fn(() => ({ deleted: true })),
|
||||
// Metadata — trip 1 and place 7 resolve to trip 1 (accessible to 42); else undefined.
|
||||
metaEntityTrip: vi.fn((entityType: string, entityId: number) =>
|
||||
(entityType === 'trip' && entityId === 1) || (entityType === 'place' && entityId === 7) || (entityType === 'day' && entityId === 3)
|
||||
|| (entityType === 'reservation' && entityId === 40) || (entityType === 'accommodation' && entityId === 11) ? 1 : undefined),
|
||||
metaGet: vi.fn(() => ({ hello: 'world' })),
|
||||
metaSet: vi.fn((_et: string, _eid: number, key: string, value: unknown) => ({ key, value })),
|
||||
metaList: vi.fn(() => ({ a: 1 })),
|
||||
metaDelete: vi.fn(() => ({ deleted: true })),
|
||||
};
|
||||
}
|
||||
|
||||
const req = (method: string, params: Record<string, unknown> = {}): RpcRequest => ({ k: 'req', id: 'x', method, params });
|
||||
const ok = (r: RpcResponse | RpcError): r is RpcResponse => r.ok === true;
|
||||
@@ -545,14 +391,7 @@ describe('PluginRpcHost — capability enforcement', () => {
|
||||
expect(ok(await c.dispatch(req('categories.list', {}), undefined))).toBe(true);
|
||||
});
|
||||
|
||||
it('tags are the acting user\'s own; a userless context + empty name are refused', async () => {
|
||||
const host = new PluginRpcHost('p', new Set(['db:read:tags', 'db:write:tags']), deps);
|
||||
expect(ok(await host.dispatch(req('tags.list'), 42))).toBe(true);
|
||||
expect(deps.listTagsForUser).toHaveBeenCalledWith(42);
|
||||
expect(ok(await host.dispatch(req('tags.create', { input: { name: 'work' } }), 42))).toBe(true);
|
||||
expect((await host.dispatch(req('tags.list'), undefined)).ok).toBe(false);
|
||||
expect((await host.dispatch(req('tags.create', { input: { name: '' } }), 42)).ok).toBe(false);
|
||||
});
|
||||
// The tags cases moved to tests/unit/tags/tags.rpc.test.ts with the handlers.
|
||||
|
||||
it('trips.members + todos.list are trip-membership-gated', async () => {
|
||||
const host = new PluginRpcHost('p', new Set(['db:read:trips', 'db:read:todos']), deps);
|
||||
|
||||
Reference in New Issue
Block a user