|
| 1 | +import { execFile } from 'node:child_process'; |
| 2 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import { join, resolve } from 'node:path'; |
| 4 | +import { promisify } from 'node:util'; |
| 5 | + |
| 6 | +import { PDFDocument } from 'pdf-lib'; |
| 7 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 8 | + |
| 9 | +import { createServer, type ServerHandle } from '../src/server/preview.js'; |
| 10 | + |
| 11 | +const execFileAsync = promisify(execFile); |
| 12 | +const cliRoot = resolve(import.meta.dirname, '..'); |
| 13 | +const repositoryRoot = resolve(cliRoot, '..'); |
| 14 | +const scratchRoot = resolve(cliRoot, '.tmp'); |
| 15 | + |
| 16 | +describe('remote CLI rendering', () => { |
| 17 | + let server: ServerHandle; |
| 18 | + let workspace: string; |
| 19 | + let template: string; |
| 20 | + let data: string; |
| 21 | + let outputDirectory: string; |
| 22 | + |
| 23 | + beforeAll(async () => { |
| 24 | + process.env['FACET_PACKAGE_PATH'] = repositoryRoot; |
| 25 | + process.env['FACET_LOW_PRIORITY'] = '0'; |
| 26 | + await mkdir(scratchRoot, { recursive: true }); |
| 27 | + workspace = await mkdtemp(join(scratchRoot, 'remote-rendering-')); |
| 28 | + const project = join(workspace, 'project'); |
| 29 | + outputDirectory = join(workspace, 'output'); |
| 30 | + template = join(project, 'RemoteReport.tsx'); |
| 31 | + data = join(project, 'data.json'); |
| 32 | + |
| 33 | + await mkdir(project, { recursive: true }); |
| 34 | + await writeFile(join(project, 'package.json'), JSON.stringify({ |
| 35 | + name: 'remote-rendering-fixture', |
| 36 | + private: true, |
| 37 | + })); |
| 38 | + await writeFile(template, ` |
| 39 | +import React from 'react'; |
| 40 | +
|
| 41 | +export default function RemoteReport({ data }: { data: { title: string } }) { |
| 42 | + return <html><body><h1>{data.title}</h1></body></html>; |
| 43 | +} |
| 44 | +`); |
| 45 | + await writeFile(data, JSON.stringify({ title: 'Rendered by remote Facet' })); |
| 46 | + |
| 47 | + server = await createServer({ |
| 48 | + port: 0, |
| 49 | + templatesDir: join(cliRoot, 'examples'), |
| 50 | + workers: 1, |
| 51 | + renderTimeout: 180000, |
| 52 | + maxUploadSize: 52428800, |
| 53 | + cacheMaxSize: 104857600, |
| 54 | + cacheDir: join(workspace, 'cache'), |
| 55 | + verbose: false, |
| 56 | + }); |
| 57 | + }, 30000); |
| 58 | + |
| 59 | + afterAll(async () => { |
| 60 | + await server?.stop(); |
| 61 | + await rm(workspace, { recursive: true, force: true }); |
| 62 | + }, 15000); |
| 63 | + |
| 64 | + it('renders HTML through the server selected by FACET_URL', async () => { |
| 65 | + const output = join(outputDirectory, 'remote.html'); |
| 66 | + |
| 67 | + await execFileAsync(process.execPath, [ |
| 68 | + '--import', 'tsx', |
| 69 | + 'src/cli.ts', |
| 70 | + 'html', template, |
| 71 | + '--data', data, |
| 72 | + '--output', output, |
| 73 | + ], { |
| 74 | + cwd: cliRoot, |
| 75 | + env: { ...process.env, FACET_URL: server.url }, |
| 76 | + timeout: 180000, |
| 77 | + }); |
| 78 | + |
| 79 | + await expect(readFile(output, 'utf-8')).resolves.toContain('Rendered by remote Facet'); |
| 80 | + }, 180000); |
| 81 | + |
| 82 | + it('renders PDF through --facet-url when FACET_URL points elsewhere', async () => { |
| 83 | + const output = join(outputDirectory, 'remote.pdf'); |
| 84 | + |
| 85 | + await execFileAsync(process.execPath, [ |
| 86 | + '--import', 'tsx', |
| 87 | + 'src/cli.ts', |
| 88 | + '--facet-url', server.url, |
| 89 | + 'pdf', template, |
| 90 | + '--data', data, |
| 91 | + '--output', output, |
| 92 | + ], { |
| 93 | + cwd: cliRoot, |
| 94 | + env: { ...process.env, FACET_URL: 'http://127.0.0.1:1' }, |
| 95 | + timeout: 180000, |
| 96 | + }); |
| 97 | + |
| 98 | + const pdf = await PDFDocument.load(await readFile(output)); |
| 99 | + expect(pdf.getPageCount()).toBeGreaterThanOrEqual(1); |
| 100 | + }, 180000); |
| 101 | +}); |
0 commit comments