using Mimante.Bidoo;
using Mimante.Bidoo.FreeBids;
using Xunit;
namespace Mimante.Tests;
/// Ricompense, cronologia crediti e campanella di Bidoo (#38): copie della struttura vera, valori inventati.
public class RewardsPagesTests
{
private static readonly TimeZoneInfo Rome = TimeZoneInfo.FindSystemTimeZoneById("Europe/Rome");
private static string Fixture(string name) => File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Fixtures", name));
[Fact]
public void I_forzieri_aperti_con_data_fonte_e_premio()
{
var read = RewardsPages.ParseChests(Fixture("ricompense-forzieri.html"), Rome);
Assert.Null(read.Error);
Assert.Equal(4, read.Items.Count); // quello senza premio non conta
var first = read.Items[0];
Assert.Equal(RewardsPages.KindChest, first.Kind);
Assert.Equal(new DateTime(2026, 11, 24, 8, 5, 0, DateTimeKind.Utc), first.AtUtc); // 09:05 a Roma
Assert.Equal(5, first.Bids);
Assert.True(first.Free);
Assert.Equal("link", first.Source);
Assert.Contains("+5 Puntate", first.Description);
// Due forzieri uguali nello stesso minuto restano due.
Assert.NotEqual(read.Items[0].Key, read.Items[1].Key);
Assert.Equal(read.Items.Count, read.Items.Select(i => i.Key).Distinct().Count());
Assert.Equal("aste di puntate confermate", read.Items[3].Source);
Assert.Equal(15, read.Items[3].Bids);
}
[Fact]
public void La_cronologia_crediti_prende_l_ora_dallo_script_della_pagina()
{
var read = RewardsPages.ParseCreditHistory(Fixture("cronologia-crediti.html"));
Assert.Null(read.Error);
Assert.Equal(3, read.Items.Count);
Assert.Equal(1, read.Skipped); // il movimento senza ora nello script
var free = read.Items[0];
Assert.Equal("accredito|1000000003", free.Key);
Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(1795511512).UtcDateTime, free.AtUtc);
Assert.Equal(5, free.Bids);
Assert.True(free.Free);
var paid = read.Items[2];
Assert.Equal(1000, paid.Bids);
Assert.False(paid.Free);
Assert.Contains("Pacchetto", paid.Description);
Assert.Contains("100,00 €", paid.Description);
}
[Fact]
public void La_campanella_da_solo_i_collegamenti_promozionali_e_passano_il_controllo()
{
var bell = RewardsPages.ParseNotifications(Fixture("campanella.json"));
Assert.Null(bell.Error);
Assert.Equal(3, bell.Notifications);
Assert.Equal(2, bell.Links.Count);
Assert.Equal("9000003", bell.Links[0].Id);
Assert.Equal("https://it.bidoo.com/?promocode=prova241126&sign=0123456789abcdef0123456789abcdef01234567", bell.Links[0].Href);
Assert.Equal("9000001", bell.Links[1].Id); // indirizzo scritto nel testo, senza href
var fb = BidooReferences.Default.FreeBids;
foreach (var (_, href) in bell.Links)
Assert.True(PromoLinkCheck.Check(href, BidooReferences.Default.Host, fb.PromoPaths, fb.PromoExtraParameters).Ok, href);
Assert.Empty(RewardsPages.ParseNotifications("""{"count":0,"elements":[],"t":1}""").Links);
}
[Fact]
public void Una_pagina_di_accesso_o_una_risposta_rotta_si_dicono_e_non_danno_ricompense()
{
const string login = "
";
Assert.NotNull(RewardsPages.ParseChests(login, Rome).Error);
Assert.NotNull(RewardsPages.ParseCreditHistory(login).Error);
Assert.NotNull(RewardsPages.ParseChests(null, Rome).Error);
Assert.NotNull(RewardsPages.ParseNotifications(login).Error);
Assert.NotNull(RewardsPages.ParseNotifications("""{"count":0}""").Error);
Assert.Empty(RewardsPages.ParseNotifications("rotto").Links);
}
[Fact]
public void Il_confronto_del_giorno_con_i_riscatti_del_bot()
{
var items = RewardsPages.ParseChests(Fixture("ricompense-forzieri.html"), Rome).Items;
var today = new DateOnly(2026, 11, 24);
Assert.EndsWith("tornano", RewardsPages.CompareToday(items, today, Rome, 2, 10));
Assert.Contains("2 ricompense da collegamento (+10 puntate)", RewardsPages.CompareToday(items, today, Rome, 2, 10));
Assert.Contains("fuori dal bot", RewardsPages.CompareToday(items, today, Rome, 1, 5));
Assert.Contains("non compare", RewardsPages.CompareToday(items, today, Rome, 3, 15));
Assert.Contains("1 ricompense da collegamento (+2 puntate)", RewardsPages.CompareToday(items, today.AddDays(-1), Rome, 1, 2));
}
}