From 7a643d5f75e0549ba5057869443c43d7df8a7cd6 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 19:57:20 +0100 Subject: [PATCH] feat(plugin-sdk): let the mock host switch Documents off like the real host With documentsAddonEnabled: false, files.* works only on attached files and files.create needs a link target, so a plugin's tests see what the host does on a trip with Documents off. The plugin guide says the same. --- plugin-sdk/src/mock-host.ts | 19 ++++++++++++++++++- plugin-sdk/test/sdk.test.ts | 25 +++++++++++++++++++++++++ wiki/Plugin-Development.md | 4 ++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/plugin-sdk/src/mock-host.ts b/plugin-sdk/src/mock-host.ts index 4888fba53..9ca5fd580 100644 --- a/plugin-sdk/src/mock-host.ts +++ b/plugin-sdk/src/mock-host.ts @@ -73,6 +73,10 @@ export interface MockHostOptions { vacayAddonEnabled?: boolean; collectionsAddonEnabled?: boolean; collabAddonEnabled?: boolean; + /** Whether the trip has Documents on (default true). Off, files.* works only on + * files attached to a booking, place, assignment or expense, and files.create + * needs such a target, as on the real host. */ + documentsAddonEnabled?: boolean; /** Exports of the plugins this one depends on, keyed by plugin id then fn name. * `ctx.plugins.call(id, fn, args)` invokes the matching function; a missing entry * throws RESOURCE_FORBIDDEN (models "not a satisfied dependency / not exported"). */ @@ -346,6 +350,13 @@ export function createMockHost(opts: MockHostOptions = {}): MockHost { const requireAddon = (enabled: boolean | undefined, noun: string) => { if (enabled === false) throw new Error(`RESOURCE_FORBIDDEN: the ${noun} addon is disabled`); }; + // With Documents off only files that belong to something stay reachable. + const LINK_KEYS = ['reservation_id', 'place_id', 'assignment_id', 'budget_item_id'] as const; + const attaches = (fields: Record) => LINK_KEYS.some((k) => fields[k] != null); + const requireReachable = (file: Record, targets: Record = {}) => { + if (opts.documentsAddonEnabled !== false || attaches(file) || attaches(targets)) return; + throw new Error('RESOURCE_FORBIDDEN: the documents addon is disabled'); + }; const assertEdit = ( t: { canEditPlaces?: boolean; canEditDays?: boolean; canEditTrip?: boolean }, flag: 'canEditPlaces' | 'canEditDays' | 'canEditTrip', @@ -727,13 +738,15 @@ export function createMockHost(opts: MockHostOptions = {}): MockHost { files: { async list(tripId) { need('db:read:files', 'files.list'); - return (assertMember(tripId, requireActingUser()).files ?? []) as TripFile[]; + const files = rows(assertMember(tripId, requireActingUser()).files); + return (opts.documentsAddonEnabled === false ? files.filter((f) => attaches(f)) : files) as TripFile[]; }, async getContent(tripId, fileId) { need('db:read:files:content', 'files.getContent'); const t = assertMember(tripId, requireActingUser()); const file = rows(t.files).find((x) => x.id === fileId); if (!file) throw new Error(`RESOURCE_FORBIDDEN: no file ${fileId} on trip ${tripId}`); + requireReachable(file); const content = typeof file.content_base64 === 'string' ? file.content_base64 : ''; return { name: String(file.name ?? `file-${fileId}`), @@ -752,6 +765,8 @@ export function createMockHost(opts: MockHostOptions = {}): MockHost { if (content_base64.length > 14 * 1024 * 1024) throw new Error('file exceeds the 10MB plugin upload cap'); const t = assertMember(tripId, uid); assertRight(t, 'file_upload', tripId); + if (opts.documentsAddonEnabled === false && !attaches(rest)) + throw new Error('RESOURCE_FORBIDDEN: the documents addon is disabled: pass reservation_id or place_id to attach the uploaded file'); const file = { id: (t.files?.length ?? 0) + 1, trip_id: tripId, size: Buffer.from(content_base64, 'base64').length, ...rest }; (t.files ??= []).push(file); return file; @@ -762,6 +777,7 @@ export function createMockHost(opts: MockHostOptions = {}): MockHost { assertRight(t, 'file_edit', tripId); const file = rows(t.files).find((x) => x.id === fileId); if (!file) throw new Error(`RESOURCE_FORBIDDEN: no file ${fileId} on trip ${tripId}`); + requireReachable(file, linkOpts as Record); Object.assign(file, linkOpts); return file; }, @@ -771,6 +787,7 @@ export function createMockHost(opts: MockHostOptions = {}): MockHost { assertRight(t, 'file_edit', tripId); const file = rows(t.files).find((x) => x.id === fileId); if (!file) throw new Error(`RESOURCE_FORBIDDEN: no file ${fileId} on trip ${tripId}`); + requireReachable(file, { place_id: input.place_id, reservation_id: input.reservation_id }); Object.assign(file, input); return file as TripFile; }, diff --git a/plugin-sdk/test/sdk.test.ts b/plugin-sdk/test/sdk.test.ts index fda25523e..7cfe7a42a 100644 --- a/plugin-sdk/test/sdk.test.ts +++ b/plugin-sdk/test/sdk.test.ts @@ -378,6 +378,31 @@ describe('createMockHost', () => { await expect(ctx.files.getContent(1, 99)).rejects.toThrow(/RESOURCE_FORBIDDEN/); }); + it('with Documents off works only on attached files and needs a target to create one, like the real host', async () => { + const content = Buffer.from('x').toString('base64'); + const { ctx } = createMockHost({ + grants: ['db:read:files', 'db:read:files:content', 'db:write:files'], + actingUserId: 42, + documentsAddonEnabled: false, + trips: { + 1: { + members: [42], + files: [ + { id: 1, name: 'loose.pdf', content_base64: content }, + { id: 2, name: 'ticket.pdf', reservation_id: 7, content_base64: content }, + ], + }, + }, + }); + expect((await ctx.files.list(1)).map((f) => f.id)).toEqual([2]); + await expect(ctx.files.getContent(1, 1)).rejects.toThrow(/documents addon is disabled/); + expect((await ctx.files.getContent(1, 2)).name).toBe('ticket.pdf'); + await expect(ctx.files.create(1, { name: 'a.pdf', content_base64: content })).rejects.toThrow(/pass reservation_id or place_id/); + expect(await ctx.files.create(1, { name: 'b.pdf', content_base64: content, place_id: 3 })).toMatchObject({ place_id: 3 }); + await expect(ctx.files.update(1, 1, { description: 'x' })).rejects.toThrow(/documents addon is disabled/); + expect(await ctx.files.update(1, 1, { reservation_id: 7 })).toMatchObject({ reservation_id: 7 }); + }); + it('refuses costs when the permission is missing or the addon is disabled', async () => { const ungranted = createMockHost({ grants: [], actingUserId: 42, trips: { 1: { members: [42] } } }); await expect(ungranted.ctx.costs.getByTrip(1)).rejects.toThrow(/PERMISSION_DENIED/); diff --git a/wiki/Plugin-Development.md b/wiki/Plugin-Development.md index 3c3f95481..ad111fc70 100644 --- a/wiki/Plugin-Development.md +++ b/wiki/Plugin-Development.md @@ -289,7 +289,7 @@ exports the list per method as `PLUGIN_METHOD_RESULT`: | `ctx.itinerary` | `assign(tripId, dayId, placeId, notes?)` / `unassign(tripId, assignmentId)` — place↔day | `db:write:itinerary` | | `ctx.meta` | `get` / `set` / `list` / `delete` your **own** namespaced data on a `trip`/`place`/`day`/`reservation`/`accommodation` (enrich core entities without forking the schema) | `db:meta` | | `ctx.packing` | `list(tripId)` — a trip's packing items (membership-checked, respects private-item visibility) | `db:read:packing` | -| `ctx.files` | `list(tripId)` — a trip's files, trash excluded (membership-checked) | `db:read:files` | +| `ctx.files` | `list(tripId)` — a trip's files, trash excluded (membership-checked); with the Documents addon off for the trip only the attached ones | `db:read:files` | | `ctx.files.getContent` | `getContent(tripId, fileId)` → `{ name, mimetype, size, content_base64 }` — a file's bytes as base64 (10MB cap; trashed files refused) | `db:read:files:content` | | `ctx.journal` | `listMine()` — the acting user's own travel journals; `getEntries(journeyId)` — one journey's entries (photos/story/checkins, access-checked) | `db:read:journal` (+ Journey addon) | | `ctx.atlas` | `visited()` — the acting user's visited countries + regions; `bucketList()` — their bucket-list items | `db:read:atlas` (+ Atlas addon) | @@ -299,7 +299,7 @@ exports the list per method as `PLUGIN_METHOD_RESULT`: | `ctx.atlas` (write) | `markCountry(code)` / `unmarkCountry` / `markRegion(regionCode, countryCode, regionName?)` / `unmarkRegion` / `createBucketItem(input)` / `deleteBucketItem(itemId)` — the acting user's own rows | `db:write:atlas` (+ Atlas addon) | | `ctx.vacay` (write) | `toggleEntry(date)` — the acting user's own PTO day on their active plan; `toggleCompanyHoliday(date, note?)` | `db:write:vacay` (+ Vacay addon) | | `ctx.journal` (write) | `createEntry(journeyId, {entry_date, ...})` / `updateEntry(entryId, fields)` / `deleteEntry(entryId)` — owner/contributor-gated; `createJourney({title, subtitle?, trip_ids?})` / `deleteJourney(journeyId)` — a new journal owned by the acting user (importers bootstrap the journal they then fill) | `db:write:journal` (+ Journey addon) | -| `ctx.files` (write) | `create(tripId, {name, content_base64, mimetype?, ...})` (10MB cap, blocked extensions refused) / `createLink(tripId, fileId, opts)` / `update` / `softDelete` — broadcasts `file:*` | `db:write:files` (+ the acting user's `file_upload`/`file_edit`/`file_delete`) | +| `ctx.files` (write) | `create(tripId, {name, content_base64, mimetype?, ...})` (10MB cap, blocked extensions refused) / `createLink(tripId, fileId, opts)` / `update` / `softDelete` — broadcasts `file:*` | `db:write:files` (+ the acting user's `file_upload`/`file_edit`/`file_delete`; with the Documents addon off for the trip, only files attached to a booking, place, assignment or expense, and `create` needs such a target. `createMockHost({ documentsAddonEnabled: false })` behaves the same) | | `ctx.collab` | `listNotes(tripId)` / `listPolls(tripId)` / `listMessages(tripId, before?)` — a trip's notes, polls (with options + voters) and chat (newest 100, oldest first; `before` = a message id to page back), membership-checked | `db:read:collab` (+ Collab addon) | | `ctx.collab` (write) | `createNote(tripId, {title, ...})` / `createPoll(tripId, {question, options})` / `votePoll(tripId, pollId, optionIndex)` / `createMessage(tripId, text, replyTo?)` — broadcasts `collab:*` | `db:write:collab` (+ `collab_edit`, Collab addon) | | `ctx.trips.addMember` / `.removeMember` | `addMember(tripId, userId)` — **grants trip access**; `removeMember(tripId, userId)` — revokes it (never the owner), so a directory-sync integration can reconcile departures too | `db:write:members` (+ `member_manage`) |