package internal import ( "testing" "time" ) func TestCalculateHash_DeterministicoConFechaFija(t *testing.T) { svc := NewHashService() fechaGen, _ := time.Parse("2006-01-02T15:04:05-07:00", "2026-05-28T10:00:00+02:00") fecha, _ := time.Parse("02-01-2006", "28-05-2026") data := &InvoiceData{ EmisorNIF: "A12345678", NumSerie: "INV-001", Fecha: fecha, TipoFactura: "F1", CuotaTotal: 21.00, ImporteTotal: 121.00, FechaGen: fechaGen, } hash1 := svc.CalculateHash(data, "") hash2 := svc.CalculateHash(data, "") if hash1 != hash2 { t.Errorf("el hash no es determinístico: %s != %s", hash1, hash2) } if len(hash1) != 64 { t.Errorf("hash SHA-256 debe tener 64 caracteres hex, tiene %d", len(hash1)) } } func TestCalculateHash_CambiaConDiferenteHashPrevio(t *testing.T) { svc := NewHashService() fechaGen, _ := time.Parse("2006-01-02T15:04:05-07:00", "2026-05-28T10:00:00+02:00") fecha, _ := time.Parse("02-01-2006", "28-05-2026") data := &InvoiceData{ EmisorNIF: "A12345678", NumSerie: "INV-001", Fecha: fecha, TipoFactura: "F1", CuotaTotal: 21.00, ImporteTotal: 121.00, FechaGen: fechaGen, } hashSinPrev := svc.CalculateHash(data, "") hashConPrev := svc.CalculateHash(data, "ABCDEF1234567890") if hashSinPrev == hashConPrev { t.Error("el hash debería cambiar cuando cambia el hash previo") } } func TestCalculateHash_PrimerRegistroHashVacio(t *testing.T) { svc := NewHashService() if !svc.IsFirstRecord() { t.Error("un HashService nuevo debería indicar que es el primer registro") } if svc.GetPreviousHash() != "" { t.Error("el hash previo del primer registro debe ser cadena vacía") } } func TestCalculateHash_EncadenamientoHashPrevio(t *testing.T) { svc := NewHashService() fechaGen, _ := time.Parse("2006-01-02T15:04:05-07:00", "2026-05-28T10:00:00+02:00") fecha, _ := time.Parse("02-01-2006", "28-05-2026") data := &InvoiceData{ EmisorNIF: "A12345678", NumSerie: "INV-001", Fecha: fecha, TipoFactura: "F1", CuotaTotal: 21.00, ImporteTotal: 121.00, FechaGen: fechaGen, } hash1 := svc.CalculateHash(data, "") svc.SetLastRecord(&LastRecord{Huella: hash1}) if svc.IsFirstRecord() { t.Error("después de SetLastRecord no debería ser primer registro") } if svc.GetPreviousHash() != hash1 { t.Errorf("GetPreviousHash devolvió %q, esperaba %q", svc.GetPreviousHash(), hash1) } }