The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
test(collab): cover the image upload path and the spool cleanup
Maurice2a38e7943156
diff --git a/server/tests/unit/nest/collab.controller.test.ts b/server/tests/unit/nest/collab.controller.test.tsindex 4ff039a74..596eae402 100644--- a/server/tests/unit/nest/collab.controller.test.ts+++ b/server/tests/unit/nest/collab.controller.test.ts@@ -4,7 +4,7 @@ import fs from 'fs';import os from 'os';import path from 'path';-import { CollabController, collabChatImageFilter } from '../../../src/nest/collab/collab.controller';+import { CollabController, collabChatImageFilter, collabNoteFileFilter } from '../../../src/nest/collab/collab.controller';import { TripAccessGuard, TRIP_PERMISSION_KEY } from '../../../src/nest/permissions/trip-access.guard';import { JwtAuthGuard } from '../../../src/nest/auth/jwt-auth.guard';import type { CollabService } from '../../../src/nest/collab/collab.service';@@ -202,6 +202,73 @@ describe('CollabController (parity with the legacy /api/trips/:tripId/collab rou// The decorators, not the handler body. Constructing the controller directly,// which every test above does, runs no guard at all — so removing the trip check// again would leave this file green. It shipped without one once.+ describe('chat images on a message', () => {+ const img = (name: string) => ({ filename: `stored-${name}`, originalname: name, size: 10, mimetype: 'image/png', path: `/tmp/${name}` }) as never;++ it('commits every image to storage and hands the service what it needs to row them', async () => {+ const put = vi.fn().mockResolvedValue(undefined);+ const createMessage = vi.fn().mockReturnValue({ message: { id: 3 } });+ const s = svc({ createMessage, broadcast: vi.fn(), notifyCollab: vi.fn() } as Partial<CollabService>);+ await new CollabController(s, { put, delete: vi.fn() } as never).createMessage(user, '5', { text: 'look' }, [img('a.png'), img('b.png')]);++ expect(put.mock.calls.map(c => c[1])).toEqual(['stored-a.png', 'stored-b.png']);+ expect(createMessage.mock.calls[0][4]).toEqual([+ { filename: 'stored-a.png', originalname: 'a.png', size: 10, mimetype: 'image/png' },+ { filename: 'stored-b.png', originalname: 'b.png', size: 10, mimetype: 'image/png' },+ ]);+ });++ it('takes back what it already committed when a later image fails to store', async () => {+ const del = vi.fn().mockResolvedValue(undefined);+ const put = vi.fn().mockResolvedValueOnce(undefined).mockRejectedValueOnce(new Error('disk full'));+ const createMessage = vi.fn();+ const s = svc({ createMessage } as Partial<CollabService>);++ await expect(new CollabController(s, { put, delete: del } as never).createMessage(user, '5', { text: 'x' }, [img('a.png'), img('b.png')]))+ .rejects.toThrow('disk full');+ // The first one is already in storage and has to come back out; the+ // message row was never written, so nothing would point at it.+ expect(del.mock.calls.map(c => c[1])).toEqual(['stored-a.png']);+ expect(createMessage).not.toHaveBeenCalled();+ });++ it('drops the stored images again when the reply target turns out to be gone', async () => {+ const del = vi.fn().mockResolvedValue(undefined);+ const s = svc({ createMessage: vi.fn().mockReturnValue({ error: 'reply_not_found' }) } as Partial<CollabService>);++ expect(await thrownAsync(() => new CollabController(s, { put: vi.fn().mockResolvedValue(undefined), delete: del } as never)+ .createMessage(user, '5', { text: 'x', reply_to: 99 }, [img('a.png')])))+ .toEqual({ status: 400, body: { error: 'Reply target message not found' } });+ expect(del.mock.calls.map(c => c[1])).toEqual(['stored-a.png']);+ });++ it('refuses an image from someone who may write but may not upload', async () => {+ const put = vi.fn();+ const s = svc({ canUploadFiles: vi.fn().mockReturnValue(false) } as Partial<CollabService>);+ expect(await thrownAsync(() => new CollabController(s, { put, delete: vi.fn() } as never).createMessage(user, '5', { text: 'x' }, [img('a.png')])))+ .toEqual({ status: 403, body: { error: 'No permission to upload files' } });+ expect(put).not.toHaveBeenCalled();+ });+ });++ describe('note file filter', () => {+ const run = (originalname: string, mimetype: string) => {+ let outcome: { err: Error | null; ok?: boolean } = { err: null };+ collabNoteFileFilter!({} as never, { originalname, mimetype } as never, ((err: Error | null, ok?: boolean) => { outcome = { err, ok }; }) as never);+ return outcome;+ };++ it('takes an ordinary attachment', () => {+ expect(run('itinerary.pdf', 'application/pdf').ok).toBe(true);+ });++ it('refuses the spellings the download route would serve as a document', () => {+ expect(run('pwn.html', 'text/html').err).toBeInstanceOf(Error);+ expect(run('pwn.txt', 'image/svg+xml').err).toBeInstanceOf(Error);+ expect(run('pwn.txt', 'application/javascript').err).toBeInstanceOf(Error);+ });+ });+describe('chat image filter', () => {const run = (originalname: string, mimetype: string) => {let outcome: { err: Error | null; ok?: boolean } = { err: null };diff --git a/server/tests/unit/nest/spool-cleanup.interceptor.test.ts b/server/tests/unit/nest/spool-cleanup.interceptor.test.tsnew file mode 100644index 000000000..4a37bf90d--- /dev/null+++ b/server/tests/unit/nest/spool-cleanup.interceptor.test.ts@@ -0,0 +1,60 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';+import { firstValueFrom, of, throwError } from 'rxjs';+import type { CallHandler, ExecutionContext } from '@nestjs/common';++const fsMock = vi.hoisted(() => ({ unlinkSync: vi.fn() }));+vi.mock('fs', () => ({ ...fsMock, default: fsMock }));++import { SpoolCleanupInterceptor } from '../../../src/nest/common/spool-cleanup.interceptor';++function context(req: Record<string, unknown>): ExecutionContext {+ return { switchToHttp: () => ({ getRequest: () => req }) } as unknown as ExecutionContext;+}++function handler(observable: ReturnType<typeof of>): CallHandler {+ return { handle: () => observable } as CallHandler;+}++const interceptor = new SpoolCleanupInterceptor();++beforeEach(() => {+ fsMock.unlinkSync.mockReset();+});++describe('SpoolCleanupInterceptor', () => {+ it('SPOOL-001: leaves the spool alone when the handler succeeds', async () => {+ const req = { files: [{ path: '/tmp/a.png' }] };+ await firstValueFrom(interceptor.intercept(context(req), handler(of('ok'))));+ // A handler that returned owns its files and moves them itself.+ expect(fsMock.unlinkSync).not.toHaveBeenCalled();+ });++ it('SPOOL-002: removes every spooled part when the request dies before the handler', async () => {+ const req = { files: [{ path: '/tmp/a.png' }, { path: '/tmp/b.png' }] };+ const boom = new Error('body rejected');++ await expect(firstValueFrom(interceptor.intercept(context(req), handler(throwError(() => boom)))))+ .rejects.toBe(boom);+ expect(fsMock.unlinkSync.mock.calls.map(c => c[0])).toEqual(['/tmp/a.png', '/tmp/b.png']);+ });++ it('SPOOL-003: covers the single-file shape and skips a part with no path', async () => {+ const req = { file: { path: '/tmp/one.png' }, files: [{ path: undefined }] };+ await expect(firstValueFrom(interceptor.intercept(context(req), handler(throwError(() => new Error('x'))))))+ .rejects.toThrow('x');+ expect(fsMock.unlinkSync.mock.calls.map(c => c[0])).toEqual(['/tmp/one.png']);+ });++ it('SPOOL-004: a file the handler already moved does not turn into a second failure', async () => {+ fsMock.unlinkSync.mockImplementation(() => { throw new Error('ENOENT'); });+ const req = { files: [{ path: '/tmp/gone.png' }] };+ await expect(firstValueFrom(interceptor.intercept(context(req), handler(throwError(() => new Error('original'))))))+ .rejects.toThrow('original');+ });++ it('SPOOL-005: a request that carried no files at all is a no-op', async () => {+ await expect(firstValueFrom(interceptor.intercept(context({}), handler(throwError(() => new Error('x'))))))+ .rejects.toThrow('x');+ expect(fsMock.unlinkSync).not.toHaveBeenCalled();+ });+});