// The logo for PDFs: an uploaded one is used as it is; the church's logo from Accounts (an address // on another site) is copied into uploads/accounts once, when it's a PNG, JPEG or SVG; drawLogo // draws either kind into a PDF. import fs from 'fs'; import path from 'path'; /* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */ const { pdfLogoPath, drawLogo, resetLogoCopies } = require('../src/utils/logoCopy') as { pdfLogoPath(u: string): Promise; drawLogo(doc: unknown, file: string | null, x: number, y: number, o?: { fit?: [number, number]; align?: string }): boolean; resetLogoCopies(): void; }; const PDFDocument = require('pdfkit'); const { UPLOADS_DIR } = require('../src/utils/uploads') as { UPLOADS_DIR: string }; /* eslint-enable @typescript-eslint/no-require-imports */ const PNG = Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), Buffer.alloc(32)]); const SVG = Buffer.from('\n'); const WEBP = Buffer.concat([Buffer.from('RIFF'), Buffer.alloc(4), Buffer.from('WEBPVP8 ')]); const COPIES = path.join(UPLOADS_DIR, 'accounts'); const realFetch = global.fetch; let served: Record = {}; let fetched: string[] = []; beforeAll(() => { global.fetch = (async (url: string) => { fetched.push(url); const body = served[url]; if (body === undefined || typeof body === 'number') return new Response('', { status: typeof body === 'number' ? body : 404 }); return new Response(new Uint8Array(body), { status: 200 }); }) as typeof fetch; }); afterAll(() => { global.fetch = realFetch; fs.rmSync(COPIES, { recursive: true, force: true }); }); beforeEach(() => { fs.rmSync(COPIES, { recursive: true, force: true }); resetLogoCopies(); served = {}; fetched = []; jest.spyOn(console, 'warn').mockImplementation(() => {}); }); afterEach(() => { jest.restoreAllMocks(); }); function pdfOf(draw: (doc: unknown) => void): Promise { return new Promise((resolve) => { const doc = new PDFDocument({ compress: false }); const parts: Buffer[] = []; doc.on('data', (b: Buffer) => parts.push(b)); doc.on('end', () => resolve(Buffer.concat(parts).toString('latin1'))); draw(doc); doc.end(); }); } test("the church's PNG logo from Accounts is copied once, and used from the copy after", async () => { served['https://accounts.hope.test/logo.png'] = PNG; const p = await pdfLogoPath('https://accounts.hope.test/logo.png'); expect(p && path.dirname(p)).toBe(COPIES); expect(fs.readFileSync(p!)).toEqual(PNG); expect(await pdfLogoPath('https://accounts.hope.test/logo.png')).toBe(p); expect(fetched).toHaveLength(1); }); test('a new logo replaces the copy of the old one', async () => { served['https://accounts.hope.test/old.png'] = PNG; served['https://accounts.hope.test/new.png'] = PNG; const old = await pdfLogoPath('https://accounts.hope.test/old.png'); const next = await pdfLogoPath('https://accounts.hope.test/new.png'); expect(next).not.toBe(old); expect(fs.existsSync(old!)).toBe(false); expect(fs.readdirSync(COPIES)).toHaveLength(1); }); test("an SVG logo (the demo churches') is copied too", async () => { served['https://accounts.hope.test/logo.svg'] = SVG; const p = await pdfLogoPath('https://accounts.hope.test/logo.svg'); expect(p?.endsWith('.svg')).toBe(true); }); test("a logo a PDF can't draw, or one that can't be fetched, gives none (the PDF shows the name), and isn't fetched again at once", async () => { served['https://accounts.hope.test/logo.webp'] = WEBP; served['https://accounts.hope.test/gone.png'] = 404; expect(await pdfLogoPath('https://accounts.hope.test/logo.webp')).toBeNull(); expect(await pdfLogoPath('https://accounts.hope.test/gone.png')).toBeNull(); expect(await pdfLogoPath('https://accounts.hope.test/logo.webp')).toBeNull(); expect(fetched).toHaveLength(2); }); test('drawLogo draws an SVG logo as shapes in its colours, and never opens a file the SVG names', async () => { fs.mkdirSync(COPIES, { recursive: true }); const file = path.join(COPIES, 'logo-test.svg'); fs.writeFileSync(file, SVG); let drawn = false; const pdf = await pdfOf((doc) => { drawn = drawLogo(doc, file, 40, 40, { fit: [60, 60], align: 'center' }); }); expect(drawn).toBe(true); expect(pdf).toMatch(/0\.12156\d* 0\.30588\d* 0\.47450\d* scn/); // #1f4e79 const picture = path.join(COPIES, 'logo-test.png'); fs.writeFileSync(picture, PNG); const sneaky = path.join(COPIES, 'logo-sneaky.svg'); const href = picture.split(path.sep).join('/'); fs.writeFileSync(sneaky, ``); const out = await pdfOf((doc) => { drawLogo(doc, sneaky, 0, 0, { fit: [10, 10] }); }); expect(out).not.toContain('/Subtype /Image'); }); test("drawLogo gives false, without throwing, for a file it can't draw", async () => { fs.mkdirSync(COPIES, { recursive: true }); const bad = path.join(COPIES, 'logo-bad.png'); fs.writeFileSync(bad, 'not a picture'); let drawn = true; await pdfOf((doc) => { drawn = drawLogo(doc, bad, 0, 0, { fit: [10, 10] }); }); expect(drawn).toBe(false); expect(drawLogo({}, null, 0, 0)).toBe(false); }); test('a logo uploaded here is used as it is; nothing for no logo', async () => { fs.mkdirSync(COPIES, { recursive: true }); fs.writeFileSync(path.join(COPIES, 'own.png'), PNG); expect(await pdfLogoPath('/uploads/accounts/own.png')).toBe(path.join(COPIES, 'own.png')); expect(await pdfLogoPath('')).toBeNull(); expect(await pdfLogoPath('/uploads/accounts/missing.png')).toBeNull(); expect(fetched).toHaveLength(0); });