feat: migrar backend unificado a Go 1.27 + SPA Vite embebido
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func floatPtr(v float64) *float64 { return &v }
|
||||
func strPtr(v string) *string { return &v }
|
||||
|
||||
func TestCalculatorsParity(t *testing.T) {
|
||||
if IVARate != 0.16 {
|
||||
t.Fatalf("expected IVARate 0.16, got %v", IVARate)
|
||||
}
|
||||
if len(EstadosCotizacion) != 6 {
|
||||
t.Fatalf("expected 6 estados, got %d", len(EstadosCotizacion))
|
||||
}
|
||||
|
||||
// Financing plans (3, 6, 9, 12 months)
|
||||
cases := []struct {
|
||||
meses int
|
||||
tasa float64
|
||||
monto float64
|
||||
wantComision float64
|
||||
wantPagoMes float64
|
||||
wantTotalMes float64
|
||||
}{
|
||||
{3, 0.077, 10000, 250, 3679.75, 4268.51},
|
||||
{6, 0.107, 10000, 250, 1891.13, 2193.71},
|
||||
{9, 0.137, 10000, 250, 1294.92, 1502.1},
|
||||
{12, 0.167, 10000, 250, 996.81, 1156.3},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
res := CalcularFinanciamiento(FinanciamientoInput{
|
||||
Monto: tc.monto, Meses: tc.meses, Tasa: tc.tasa, Comision: 2.5, IVA: 0.16,
|
||||
})
|
||||
if res.ComisionTotal != tc.wantComision {
|
||||
t.Errorf("%dm comisionTotal=%v want %v", tc.meses, res.ComisionTotal, tc.wantComision)
|
||||
}
|
||||
if res.PagoMensual != tc.wantPagoMes {
|
||||
t.Errorf("%dm pagoMensual=%v want %v", tc.meses, res.PagoMensual, tc.wantPagoMes)
|
||||
}
|
||||
if res.TotalMensual != tc.wantTotalMes {
|
||||
t.Errorf("%dm totalMensual=%v want %v", tc.meses, res.TotalMensual, tc.wantTotalMes)
|
||||
}
|
||||
}
|
||||
|
||||
// Totales cotizacion with mixed models & unselected item
|
||||
svcs := []ServicioCalc{
|
||||
{TipoPago: "unico", Precio: 15000, Seleccionado: true, ModeloCobro: "fijo"},
|
||||
{TipoPago: "mensual", Precio: 5000, Seleccionado: true, ModeloCobro: "retainer"},
|
||||
{TipoPago: "mensual", Precio: 0, Seleccionado: true, ModeloCobro: "demanda", TarifaHora: floatPtr(700)},
|
||||
{TipoPago: "unico", Precio: 99999, Seleccionado: false},
|
||||
}
|
||||
totCon := CalcularTotalesCotizacion(svcs, true)
|
||||
if totCon.SubtotalUnico != 15000 || totCon.SubtotalMensual != 5000 || totCon.TotalUnico != 17400 || totCon.TotalMensual != 5800 || totCon.TotalPrimerAnio != 87000 {
|
||||
t.Fatalf("unexpected totCon: %+v", totCon)
|
||||
}
|
||||
totSin := CalcularTotalesCotizacion(svcs, false)
|
||||
if totSin.IVAUnico != 0 || totSin.TotalUnico != 15000 || totSin.TotalPrimerAnio != 75000 {
|
||||
t.Fatalf("unexpected totSin: %+v", totSin)
|
||||
}
|
||||
|
||||
// Doble propuesta option totals
|
||||
doble := []ServicioCalc{
|
||||
{Opcion: strPtr("1"), TipoPago: "unico", Precio: 10000, Horas: floatPtr(10)},
|
||||
{Opcion: strPtr("2"), TipoPago: "unico", Precio: 20000, Horas: floatPtr(20)},
|
||||
{Opcion: strPtr("ambas"), TipoPago: "mensual", Precio: 3000, Horas: floatPtr(5)},
|
||||
}
|
||||
op1 := CalcularTotalesOpcion(doble, "1")
|
||||
op2 := CalcularTotalesOpcion(doble, "2")
|
||||
if op1.TotalUnico != 10000 || op1.TotalMensual != 3000 || op1.Horas != 15 {
|
||||
t.Fatalf("unexpected op1: %+v", op1)
|
||||
}
|
||||
if op2.TotalUnico != 20000 || op2.TotalMensual != 3000 || op2.Horas != 25 {
|
||||
t.Fatalf("unexpected op2: %+v", op2)
|
||||
}
|
||||
|
||||
// 15 business days vigencia: Mon 2026-06-01 -> Mon 2026-06-22
|
||||
start := time.Date(2026, 6, 1, 12, 0, 0, 0, time.UTC)
|
||||
vig := CalcularVigencia(start)
|
||||
if vig.Format("2006-01-02") != "2026-06-22" {
|
||||
t.Fatalf("expected vigencia 2026-06-22, got %s", vig.Format("2006-01-02"))
|
||||
}
|
||||
|
||||
// Hours range & summary
|
||||
if h := CalcularHorasRango("09:00", "13:30"); h != 4.5 {
|
||||
t.Fatalf("expected 4.5 hours, got %v", h)
|
||||
}
|
||||
resH := CalcularResumenPagoHoras([]RegistroHorasCalc{
|
||||
{Horas: 2, TarifaHora: 700, EstadoPago: "por_pagar"},
|
||||
{Horas: 3, TarifaHora: 700, EstadoPago: "pagada"},
|
||||
})
|
||||
if resH.PorPagar.Importe != 1400 || resH.Pagada.Importe != 2100 || resH.Total.Importe != 3500 {
|
||||
t.Fatalf("unexpected resumen horas: %+v", resH)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApprovalGuard(t *testing.T) {
|
||||
now := time.Now()
|
||||
agent := ActorInfo{Type: "agent", Scopes: []string{"cotizaciones:borrador", "cotizaciones:enviar"}}
|
||||
asesor := ActorInfo{Type: "user", Role: "asesor"}
|
||||
admin := ActorInfo{Type: "user", Role: "admin"}
|
||||
enlace := ActorInfo{Type: "enlace_firmado"}
|
||||
|
||||
// Agent cannot approve
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "borrador", Nuevo: "aprobada", Actor: agent, ViaEndpoint: "aprobar"}); r.OK || r.Status != 403 {
|
||||
t.Fatalf("agent approval should be 403, got %+v", r)
|
||||
}
|
||||
// Asesor cannot approve
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "borrador", Nuevo: "aprobada", Actor: asesor, ViaEndpoint: "aprobar"}); r.OK || r.Status != 403 {
|
||||
t.Fatalf("asesor approval should be 403, got %+v", r)
|
||||
}
|
||||
// Admin can approve borrador
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "borrador", Nuevo: "aprobada", Actor: admin, ViaEndpoint: "aprobar"}); !r.OK {
|
||||
t.Fatalf("admin approval should succeed, got %+v", r)
|
||||
}
|
||||
// Signed link can approve borrador
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "borrador", Nuevo: "aprobada", Actor: enlace, ViaEndpoint: "enlace_firmado"}); !r.OK {
|
||||
t.Fatalf("signed link approval should succeed, got %+v", r)
|
||||
}
|
||||
// Cannot send borrador
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "borrador", Nuevo: "enviada", Actor: agent, ViaEndpoint: "enviar"}); r.OK || r.Status != 409 {
|
||||
t.Fatalf("sending draft should be 409, got %+v", r)
|
||||
}
|
||||
// Agent cannot set enviada via PUT
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "aprobada", Nuevo: "enviada", Actor: agent, ViaEndpoint: "put", AprobadaAt: &now}); r.OK || r.Status != 403 {
|
||||
t.Fatalf("agent PUT enviada should be 403, got %+v", r)
|
||||
}
|
||||
// Agent can send approved quote via enviar
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "aprobada", Nuevo: "enviada", Actor: agent, ViaEndpoint: "enviar", AprobadaAt: &now}); !r.OK {
|
||||
t.Fatalf("agent enviar approved should succeed, got %+v", r)
|
||||
}
|
||||
// Agent cannot mark aceptada
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "enviada", Nuevo: "aceptada", Actor: agent, ViaEndpoint: "estado"}); r.OK || r.Status != 403 {
|
||||
t.Fatalf("agent aceptada should be 403, got %+v", r)
|
||||
}
|
||||
// Staff can mark aceptada
|
||||
if r := ValidarTransicionEstado(ValidarTransicionInput{Actual: "enviada", Nuevo: "aceptada", Actor: asesor, ViaEndpoint: "estado"}); !r.OK {
|
||||
t.Fatalf("asesor aceptada should succeed, got %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSoloAdminRestriction(t *testing.T) {
|
||||
soloIDs := map[string]struct{}{"plan-mensual-crm-bucefalo-18": {}}
|
||||
|
||||
// Human admin allowed everything
|
||||
r1 := EvaluarRestriccionSoloAdmin(EvaluarSoloAdminInput{
|
||||
EsAgente: false,
|
||||
HasPlanBucefalo: true,
|
||||
Servicios: []ServicioDraftInput{{CatalogoID: "plan-mensual-crm-bucefalo-18", Nombre: "CRM Bucéfalo"}},
|
||||
SoloAdminCatalogIDs: soloIDs,
|
||||
})
|
||||
if !r1.OK {
|
||||
t.Fatalf("human admin should not be blocked: %+v", r1)
|
||||
}
|
||||
|
||||
// Agent blocked on planBucefalo, soloAdmin ID, bucefalo-* prefix, and custom name
|
||||
r2 := EvaluarRestriccionSoloAdmin(EvaluarSoloAdminInput{
|
||||
EsAgente: true,
|
||||
HasPlanBucefalo: true,
|
||||
Servicios: []ServicioDraftInput{
|
||||
{CatalogoID: "plan-mensual-crm-bucefalo-18"},
|
||||
{CatalogoID: "bucefalo-pro"},
|
||||
{Nombre: "Implementacion Bucéfalo Custom"},
|
||||
},
|
||||
SoloAdminCatalogIDs: soloIDs,
|
||||
})
|
||||
if r2.OK || len(r2.ItemsBloqueados) != 4 {
|
||||
t.Fatalf("expected 4 blocked items, got %+v", r2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFechasPeriodo(t *testing.T) {
|
||||
ini, _ := ParseFechaCalendario("2026-07-01")
|
||||
fin, _ := ParseFechaCalendario("2026-07-31")
|
||||
sab := 6
|
||||
fechas := FechasPeriodo(ProgramaCalendario{
|
||||
Frecuencia: "semanal",
|
||||
DiaSemana: &sab,
|
||||
FechaInicio: ini,
|
||||
FechaFin: &fin,
|
||||
}, fin)
|
||||
if len(fechas) != 4 || FormatFechaCorta(fechas[0]) != "04/07/2026" {
|
||||
t.Fatalf("expected 4 saturdays starting 04/07/2026, got %d (%v)", len(fechas), fechas)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user