277 lines
8.9 KiB
Go
277 lines
8.9 KiB
Go
package excel
|
|
|
|
import (
|
|
"bytes"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
|
"cotizador/internal/domain"
|
|
)
|
|
|
|
func f64(v float64) *float64 { return &v }
|
|
func str(v string) *string { return &v }
|
|
|
|
func TestCotizacionExcelDataHasNoObservacionesInternas(t *testing.T) {
|
|
rt := reflect.TypeOf(CotizacionExcelData{})
|
|
for i := 0; i < rt.NumField(); i++ {
|
|
name := strings.ToLower(rt.Field(i).Name)
|
|
if strings.Contains(name, "interna") {
|
|
t.Fatalf("CotizacionExcelData must never declare internal observations field, found %q", rt.Field(i).Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func sheetContains(t *testing.T, f *excelize.File, sheet, substr string) bool {
|
|
t.Helper()
|
|
rows, err := f.GetRows(sheet)
|
|
if err != nil {
|
|
t.Fatalf("GetRows(%s): %v", sheet, err)
|
|
}
|
|
for _, row := range rows {
|
|
for _, cell := range row {
|
|
if strings.Contains(cell, substr) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestBuildCotizacionExcelSingle(t *testing.T) {
|
|
fecha := time.Date(2026, 5, 10, 0, 0, 0, 0, time.UTC)
|
|
vigencia := domain.CalcularVigencia(fecha)
|
|
obs := "Nota visible para el cliente con acentos: diseño y año."
|
|
|
|
raw, err := BuildCotizacionExcel(CotizacionExcelData{
|
|
RazonSocial: "URIEL JARETH CONSULTING",
|
|
DomicilioFiscal: "DOMICILIO_SECRETO_NO_IMPRIMIR 123",
|
|
Numero: "UJ2605UR001",
|
|
ClienteNombre: "Sofía Ramírez",
|
|
ClienteEmpresa: "Ramírez Diseño S.A.",
|
|
AsesorNombre: "Uriel Jareth",
|
|
Fecha: fecha,
|
|
Vigencia: vigencia,
|
|
Moneda: "MXN",
|
|
TipoCambio: "N/A",
|
|
Proyecto: "Sitio Web y Campañas",
|
|
EsquemaPago: "50/50",
|
|
Servicios: []ServicioExcel{
|
|
{
|
|
Nombre: "Auditoría SEO / Técnica: [2026]*?",
|
|
Fase: 0,
|
|
TipoPago: "unico",
|
|
Precio: 6000,
|
|
TiempoEntrega: "7 días",
|
|
Entregables: []string{"Diagnóstico técnico", "Roadmap SEO"},
|
|
ModeloCobro: "fijo",
|
|
},
|
|
{
|
|
Nombre: "Desarrollo Custom",
|
|
Fase: 1,
|
|
TipoPago: "unico",
|
|
Precio: 14000,
|
|
TiempoEntrega: "15 días",
|
|
Entregables: []string{"Frontend", "Backend"},
|
|
ModeloCobro: "horas",
|
|
EsPersonalizado: true,
|
|
Horas: f64(20),
|
|
TarifaHora: f64(700),
|
|
},
|
|
{
|
|
Nombre: "Retainer Mensual",
|
|
Fase: 2,
|
|
TipoPago: "mensual",
|
|
Precio: 7500,
|
|
TiempoEntrega: "Mensual",
|
|
Entregables: []string{"Soporte", "Reportes"},
|
|
ModeloCobro: "retainer",
|
|
MontoMinimo: f64(7500),
|
|
HorasIncluidas: f64(10),
|
|
TarifaHora: f64(700),
|
|
},
|
|
{
|
|
Nombre: "Desarrollo Custom", // duplicate tab name test
|
|
Fase: 3,
|
|
TipoPago: "mensual",
|
|
Precio: 0,
|
|
TiempoEntrega: "Bajo demanda",
|
|
ModeloCobro: "demanda",
|
|
TarifaHora: f64(700),
|
|
},
|
|
},
|
|
PlanBucefaloNivel: str("premium"), // nil price -> falls back to domain.BucefaloPrecio (4500)
|
|
ColorPrimario: "#0400C4",
|
|
ColorSecundario: "#0F172A",
|
|
IncluirIVA: true,
|
|
Observaciones: &obs,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("BuildCotizacionExcel error: %v", err)
|
|
}
|
|
|
|
f, err := excelize.OpenReader(bytes.NewReader(raw))
|
|
if err != nil {
|
|
t.Fatalf("excelize.OpenReader error: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
sheets := f.GetSheetList()
|
|
if len(sheets) != 5 {
|
|
t.Fatalf("expected 5 sheets (1 resumen + 4 services), got %d: %v", len(sheets), sheets)
|
|
}
|
|
if sheets[0] != "HOJA RESUMEN" {
|
|
t.Fatalf("expected first sheet HOJA RESUMEN, got %q", sheets[0])
|
|
}
|
|
|
|
// Verify key values in HOJA RESUMEN
|
|
if v, _ := f.GetCellValue("HOJA RESUMEN", "B2"); v != "URIEL JARETH CONSULTING" {
|
|
t.Errorf("B2 = %q, want URIEL JARETH CONSULTING", v)
|
|
}
|
|
if v, _ := f.GetCellValue("HOJA RESUMEN", "B3"); v != "UJ2605UR001" {
|
|
t.Errorf("B3 = %q, want UJ2605UR001", v)
|
|
}
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "20 h x $700/hr") {
|
|
t.Errorf("HOJA RESUMEN missing horas DetalleModelo")
|
|
}
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "segun consumo") {
|
|
t.Errorf("HOJA RESUMEN missing demanda DetalleModelo")
|
|
}
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "CRM Bucéfalo - Premium") {
|
|
t.Errorf("HOJA RESUMEN missing Plan Bucéfalo row")
|
|
}
|
|
if !sheetContains(t, f, "HOJA RESUMEN", obs) {
|
|
t.Errorf("HOJA RESUMEN missing Observaciones")
|
|
}
|
|
|
|
// Verify DomicilioFiscal is NEVER printed on any sheet.
|
|
for _, s := range sheets {
|
|
if sheetContains(t, f, s, "DOMICILIO_SECRETO_NO_IMPRIMIR") {
|
|
t.Fatalf("sheet %q leaked DomicilioFiscal", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildCotizacionExcelDoblePropuesta(t *testing.T) {
|
|
fecha := time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)
|
|
raw, err := BuildCotizacionExcel(CotizacionExcelData{
|
|
RazonSocial: "URIEL JARETH CONSULTING",
|
|
Numero: "UJ2606UR002",
|
|
ClienteNombre: "Luis Torres",
|
|
ClienteEmpresa: "Torres Corp",
|
|
AsesorNombre: "Uriel Jareth",
|
|
Fecha: fecha,
|
|
Vigencia: domain.CalcularVigencia(fecha),
|
|
Moneda: "MXN",
|
|
Proyecto: "Escalamiento",
|
|
EsquemaPago: "Mensual",
|
|
EsDoble: true,
|
|
IncluirIVA: true,
|
|
OpcionesMetadata: map[string]domain.MetaOpcion{
|
|
"1": {Titulo: "Básico", Descripcion: "Plan inicial", NoIncluye: "Video"},
|
|
"2": {Titulo: "Pro", Descripcion: "Plan avanzado", NoIncluye: "Pauta"},
|
|
},
|
|
Servicios: []ServicioExcel{
|
|
{Nombre: "HOJA RESUMEN", Fase: 0, TipoPago: "unico", Precio: 5000, TiempoEntrega: "5 días", Opcion: str("ambas"), Horas: f64(5)},
|
|
{Nombre: "Landing", Fase: 1, TipoPago: "unico", Precio: 10000, TiempoEntrega: "10 días", Opcion: str("1"), Horas: f64(10)},
|
|
{Nombre: "Web Pro", Fase: 1, TipoPago: "unico", Precio: 20000, TiempoEntrega: "20 días", Opcion: str("2"), Horas: f64(25)},
|
|
{Nombre: "Ads", Fase: 2, TipoPago: "mensual", Precio: 8000, TiempoEntrega: "Mensual", Opcion: str("ambas")},
|
|
},
|
|
PlanBucefaloNivel: str("basico"),
|
|
PlanBucefaloPrecio: f64(1200),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("BuildCotizacionExcel doble error: %v", err)
|
|
}
|
|
|
|
f, err := excelize.OpenReader(bytes.NewReader(raw))
|
|
if err != nil {
|
|
t.Fatalf("OpenReader error: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "OPCIÓN 1: Básico") {
|
|
t.Errorf("missing OPCIÓN 1 banner")
|
|
}
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "OPCIÓN 2: Pro") {
|
|
t.Errorf("missing OPCIÓN 2 banner")
|
|
}
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "COMPARATIVA DE OPCIONES") {
|
|
t.Errorf("missing COMPARATIVA DE OPCIONES")
|
|
}
|
|
// Opción 1 único = (5000+10000)*1.16 = $17,400; Opción 2 único = (5000+20000)*1.16 = $29,000
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "$17,400") || !sheetContains(t, f, "HOJA RESUMEN", "$29,000") {
|
|
t.Errorf("expected IVA-adjusted comparative totals $17,400 and $29,000")
|
|
}
|
|
if !sheetContains(t, f, "HOJA RESUMEN", "15 h") || !sheetContains(t, f, "HOJA RESUMEN", "30 h") {
|
|
t.Errorf("expected comparative hours 15 h and 30 h")
|
|
}
|
|
}
|
|
|
|
func TestCatalogoCSVExportPlantillaAndParse(t *testing.T) {
|
|
// 1. Plantilla parses cleanly into 6 rows.
|
|
tpl := PlantillaCatalogoCSV()
|
|
parsedTpl, err := ParseCatalogoCSV(tpl)
|
|
if err != nil {
|
|
t.Fatalf("ParseCatalogoCSV(PlantillaCatalogoCSV()) error: %v", err)
|
|
}
|
|
if len(parsedTpl) != 6 {
|
|
t.Fatalf("expected 6 rows from plantilla, got %d", len(parsedTpl))
|
|
}
|
|
if parsedTpl[0].Nombre != "SEO On-Page Basico" || parsedTpl[0].Fase != 1 || parsedTpl[0].TipoPago != "unico" || parsedTpl[0].PrecioBase != 5000 {
|
|
t.Errorf("unexpected first plantilla row: %+v", parsedTpl[0])
|
|
}
|
|
if len(parsedTpl[0].Entregables) != 3 {
|
|
t.Errorf("expected 3 entregables in first row, got %v", parsedTpl[0].Entregables)
|
|
}
|
|
|
|
// 2. Round-trip ExportCatalogoCSV -> ParseCatalogoCSV (including commas, quotes, BOM).
|
|
orig := []CatalogoCSVRow{
|
|
{
|
|
Nombre: `Auditoría "Pro", Web & SEO`,
|
|
Categoria: "SEO",
|
|
Fase: 0,
|
|
TipoPago: "unico",
|
|
PrecioBase: 4500.5,
|
|
TiempoEntrega: "5 - 7 días",
|
|
Entregables: []string{"Informe técnico", "Sesión de entrega"},
|
|
Variante: "Premium",
|
|
},
|
|
{
|
|
Nombre: "Gestión de Redes",
|
|
Categoria: "Marketing",
|
|
Fase: 2,
|
|
TipoPago: "mensual",
|
|
PrecioBase: 8000,
|
|
TiempoEntrega: "Mensual",
|
|
Entregables: []string{"Calendario editorial", "Reporte mensual"},
|
|
Variante: "",
|
|
},
|
|
}
|
|
exported, err := ExportCatalogoCSV(orig)
|
|
if err != nil {
|
|
t.Fatalf("ExportCatalogoCSV error: %v", err)
|
|
}
|
|
|
|
// Prepend UTF-8 BOM to verify BOM stripping too.
|
|
withBOM := append([]byte{0xEF, 0xBB, 0xBF}, exported...)
|
|
roundTrip, err := ParseCatalogoCSV(withBOM)
|
|
if err != nil {
|
|
t.Fatalf("ParseCatalogoCSV round-trip error: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(roundTrip, orig) {
|
|
t.Fatalf("round-trip mismatch:\ngot: %+v\nwant: %+v", roundTrip, orig)
|
|
}
|
|
|
|
// 3. Error cases.
|
|
if _, err := ParseCatalogoCSV([]byte("Nombre,Categoria,Fase,Tipo de Pago,Precio Base\n")); err == nil {
|
|
t.Errorf("expected error for header-only CSV")
|
|
}
|
|
if _, err := ParseCatalogoCSV([]byte("Nombre,Categoria,Fase,Tipo de Pago,Precio Base\nFoo,Bar,Invalida,unico,-5\n")); err == nil {
|
|
t.Errorf("expected error when no valid rows exist")
|
|
}
|
|
}
|