phronCare/phronCare.Test/PdfGeneratorServiceTests.cs

143 lines
5.5 KiB
C#

using Transversal.Services;
using Transversal.Models;
namespace phronCare.Test
{
[TestFixture]
public class PdfGeneratorServiceTests
{
private PuppeteerPdfGeneratorService _pdfService;
[OneTimeSetUp]
public void Setup()
{
// Instancia real del servicio (recomendado para test manual/local)
_pdfService = new PuppeteerPdfGeneratorService();
}
[OneTimeTearDown]
public async Task TearDown()
{
// Liberar Chromium al finalizar los tests
if (_pdfService != null)
await _pdfService.DisposeAsync();
}
[Test]
public async Task GeneratePdfFromHtml_ShouldCreatePdfFile()
{
// Arrange
string html = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; font-size: 12px; margin: 20px; }
.header { text-align: center; border-bottom: 2px solid #4CAF50; padding-bottom: 10px; margin-bottom: 20px; }
.header h1 { color: #4CAF50; }
.info { margin-bottom: 20px; }
.info table { width: 100%; }
.info td { padding: 5px; }
.details table { width: 100%; border-collapse: collapse; }
.details th, .details td { border: 1px solid #dddddd; text-align: center; padding: 8px; }
.details th { background-color: #4CAF50; color: white; }
.totals { margin-top: 20px; float: right; width: 300px; }
.totals table { width: 100%; border-collapse: collapse; }
.totals th, .totals td { border: 1px solid #dddddd; text-align: right; padding: 8px; }
.totals th { background-color: #4CAF50; color: white; }
.footer { position: fixed; bottom: 20px; left: 0; right: 0; text-align: center; font-size: 10px; color: gray; }
</style>
</head>
<body>
<div class='header'>
<h1>PhronCare Ortopedia</h1>
<p>Presupuesto Médico</p>
</div>
<div class='info'>
<table>
<tr>
<td><strong>Cliente:</strong> Juan Pérez</td>
<td><strong>Presupuesto N°:</strong> 000123</td>
</tr>
<tr>
<td><strong>Fecha:</strong> 13/05/2025</td>
<td><strong>Profesional:</strong> Dr. Carlos López</td>
</tr>
</table>
</div>
<div class='details'>
<table>
<tr>
<th>Cantidad</th>
<th>Producto</th>
<th>Precio Unitario</th>
<th>Subtotal</th>
</tr>
<tr>
<td>2</td>
<td>Rodillera ortopédica</td>
<td>$5.000</td>
<td>$10.000</td>
</tr>
<tr>
<td>1</td>
<td>Férula de inmovilización</td>
<td>$8.000</td>
<td>$8.000</td>
</tr>
</table>
</div>
<div class='totals'>
<table>
<tr>
<th>Subtotal</th>
<td>$18.000</td>
</tr>
<tr>
<th>IVA (21%)</th>
<td>$3.780</td>
</tr>
<tr>
<th>Total</th>
<td><strong>$21.780</strong></td>
</tr>
</table>
</div>
<div class='footer'>
Presupuesto generado automáticamente por PhronCare - No válido como factura.
</div>
</body>
</html>";
string outputFolder = @"C:\temp";
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder);
string outputPath = Path.Combine(outputFolder, "DemoTest_Puppeteer.pdf");
// Opcional: podés probar pasando o no opciones
var options = new PdfGenerationOptions
{
Format = PuppeteerSharp.Media.PaperFormat.A4,
Landscape = false,
PrintBackground = true,
Scale = 1.0m,
HeaderTemplate = "<div style='font-size:10px; text-align:center;'>Presupuesto</div>",
FooterTemplate = "<div style='font-size:10px; text-align:center;'>Página <span class='pageNumber'></span> de <span class='totalPages'></span></div>"
};
// Act
byte[] pdfBytes = await _pdfService.GeneratePdfFromHtmlAsync(html, options);
await File.WriteAllBytesAsync(outputPath, pdfBytes);
// Assert
Assert.IsTrue(File.Exists(outputPath));
Assert.IsTrue(new FileInfo(outputPath).Length > 0);
TestContext.WriteLine($"PDF generado correctamente en: {outputPath}");
}
}
}