// 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, and only when it's a PNG or JPEG. import fs from 'fs'; import path from 'path'; /* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */ const { pdfLogoPath, resetLogoCopies } = require('../src/utils/logoCopy') as { pdfLogoPath(u: string): Promise; resetLogoCopies(): void }; 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(''); 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(); }); 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 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.svg'] = SVG; served['https://accounts.hope.test/gone.png'] = 404; expect(await pdfLogoPath('https://accounts.hope.test/logo.svg')).toBeNull(); expect(await pdfLogoPath('https://accounts.hope.test/gone.png')).toBeNull(); expect(await pdfLogoPath('https://accounts.hope.test/logo.svg')).toBeNull(); expect(fetched).toHaveLength(2); }); test('a logo uploaded here is used as it is; nothing for no logo', async () => { const dir = path.join(UPLOADS_DIR, 'accounts'); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, 'own.png'), PNG); expect(await pdfLogoPath('/uploads/accounts/own.png')).toBe(path.join(dir, 'own.png')); expect(await pdfLogoPath('')).toBeNull(); expect(await pdfLogoPath('/uploads/accounts/missing.png')).toBeNull(); expect(fetched).toHaveLength(0); });