import { I18nService } from '@ghostfolio/api/services/i18n/i18n.service'; import { personalFinanceTools } from '@ghostfolio/common/personal-finance-tools'; import { Logger } from '@nestjs/common'; import { NextFunction, Request, Response } from 'express'; import { readFileSync, readdirSync } from 'node:fs'; import { HtmlTemplateMiddleware } from './html-template.middleware'; jest.mock('@ghostfolio/api/environments/environment', () => { return { environment: { production: true, rootUrl: 'https://ghostfol.io' } }; }); jest.mock('node:fs', () => { return { ...jest.requireActual('node:fs'), readFileSync: jest.fn(), readdirSync: jest.fn() }; }); const genericDescription = 'Generic description'; const indexHtml = ''; const productPath = '/en/resources/personal-finance-tools/open-source-alternative-to-8figures'; function createLocale({ pricingDescription = 'Pricing description', productDescription }: { pricingDescription?: string; productDescription: string; }) { return ` ${genericDescription} ${pricingDescription} ${productDescription} pricing resources personal-finance-tools open-source-alternative-to `; } function getResponseBody({ locale, originalUrl }: { locale: string; originalUrl: string; }) { (readdirSync as jest.Mock).mockReturnValue(['messages.xlf']); (readFileSync as jest.Mock).mockImplementation((path: string) => { return path.endsWith('index.html') ? indexHtml : locale; }); const i18nService = new I18nService(); i18nService.onModuleInit(); const send = jest.fn(); new HtmlTemplateMiddleware(i18nService).use( { originalUrl } as Request, { send } as unknown as Response, jest.fn() as NextFunction ); return send.mock.calls[0][0]; } describe('HtmlTemplateMiddleware', () => { beforeEach(() => { jest.spyOn(Logger.prototype, 'warn').mockImplementation(); }); afterEach(() => { jest.restoreAllMocks(); }); it('serves the description of a public route', () => { expect( getResponseBody({ locale: createLocale({ productDescription: 'Compare ${name}' }), originalUrl: '/en/pricing' }) ).toContain('content="Pricing description"'); }); it('interpolates the name into the description of a personal finance tool', () => { expect( getResponseBody({ locale: createLocale({ productDescription: 'Compare ${name}' }), originalUrl: productPath }) ).toContain('content="Compare 8FIGURES"'); }); it.each( personalFinanceTools.filter(({ alias, key }) => { return (alias ?? key).includes('.'); }) )( 'serves the template of a personal finance tool with a dot in the key ($key)', ({ alias, key, name }) => { expect( getResponseBody({ locale: createLocale({ productDescription: 'Compare ${name}' }), originalUrl: `/en/resources/personal-finance-tools/open-source-alternative-to-${alias ?? key}` }) ).toContain(`content="Compare ${name}"`); } ); it('falls back to the generic description if the name token is not in the description of a personal finance tool', () => { expect( getResponseBody({ locale: createLocale({ productDescription: 'Compare ${product}' }), originalUrl: productPath }) ).toContain(`content="${genericDescription}"`); }); it('falls back to the generic description if the description of a personal finance tool contains another token than the name token', () => { expect( getResponseBody({ locale: createLocale({ productDescription: 'Compare ${name} (${product})' }), originalUrl: productPath }) ).toContain(`content="${genericDescription}"`); }); it('escapes the description for the use in an attribute', () => { expect( getResponseBody({ locale: createLocale({ pricingDescription: 'Compare "plans" & prices', productDescription: 'Compare ${name}' }), originalUrl: '/en/pricing' }) ).toContain('content="Compare "plans" & prices"'); }); });