711cc11805
- Creato progetto `Tests/AutoBidder.Tests` con test su servizi, modelli e utility principali - Aggiornato `.dockerignore` per escludere test e risultati - Aggiornati workflow GitHub Actions per .NET 10, salvataggio e upload dei risultati test come artifact - Aggiornata soluzione e csproj per includere/escludere i test correttamente - I test coprono casi standard ed edge case, senza modifiche funzionali al codice di produzione
286 lines
7.4 KiB
C#
286 lines
7.4 KiB
C#
using AutoBidder.Models;
|
|
using AutoBidder.Services;
|
|
using Xunit;
|
|
|
|
namespace AutoBidder.Tests;
|
|
|
|
public class ApplicationStateServiceTests
|
|
{
|
|
// === Auctions ===
|
|
|
|
[Fact]
|
|
public void Auctions_Initially_Empty()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
Assert.Empty(service.Auctions);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuction_AddsToList()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddAuction(new AuctionInfo { AuctionId = "1", Name = "Test" });
|
|
|
|
Assert.Single(service.Auctions);
|
|
Assert.Equal("1", service.Auctions[0].AuctionId);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddAuction_DuplicateId_DoesNotAdd()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddAuction(new AuctionInfo { AuctionId = "1", Name = "First" });
|
|
service.AddAuction(new AuctionInfo { AuctionId = "1", Name = "Second" });
|
|
|
|
Assert.Single(service.Auctions);
|
|
Assert.Equal("First", service.Auctions[0].Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void RemoveAuction_ExistingAuction_RemovesFromList()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
var auction = new AuctionInfo { AuctionId = "1" };
|
|
service.AddAuction(auction);
|
|
|
|
service.RemoveAuction(auction);
|
|
|
|
Assert.Empty(service.Auctions);
|
|
}
|
|
|
|
[Fact]
|
|
public void RemoveAuction_SelectedAuction_ClearsSelection()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
var auction = new AuctionInfo { AuctionId = "1" };
|
|
service.AddAuction(auction);
|
|
service.SetSelectedAuctionDirect(auction);
|
|
|
|
service.RemoveAuction(auction);
|
|
|
|
Assert.Null(service.SelectedAuction);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateAuction_ExistingAuction_ReplacesInList()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
var original = new AuctionInfo { AuctionId = "1", Name = "Original" };
|
|
service.AddAuction(original);
|
|
|
|
var updated = new AuctionInfo { AuctionId = "1", Name = "Updated" };
|
|
service.UpdateAuction(updated);
|
|
|
|
Assert.Single(service.Auctions);
|
|
Assert.Equal("Updated", service.Auctions[0].Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateAuction_SelectedAuction_UpdatesSelection()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
var auction = new AuctionInfo { AuctionId = "1", Name = "Original" };
|
|
service.AddAuction(auction);
|
|
service.SetSelectedAuctionDirect(auction);
|
|
|
|
var updated = new AuctionInfo { AuctionId = "1", Name = "Updated" };
|
|
service.UpdateAuction(updated);
|
|
|
|
Assert.Equal("Updated", service.SelectedAuction!.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetAuctions_ReplacesEntireList()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddAuction(new AuctionInfo { AuctionId = "old" });
|
|
|
|
var newList = new List<AuctionInfo>
|
|
{
|
|
new() { AuctionId = "new1" },
|
|
new() { AuctionId = "new2" }
|
|
};
|
|
service.SetAuctions(newList);
|
|
|
|
Assert.Equal(2, service.Auctions.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAuctionById_ExistingId_ReturnsAuction()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddAuction(new AuctionInfo { AuctionId = "42", Name = "Found" });
|
|
|
|
var result = service.GetAuctionById("42");
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal("Found", result!.Name);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAuctionById_NonExistent_ReturnsNull()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
Assert.Null(service.GetAuctionById("missing"));
|
|
}
|
|
|
|
// === SelectedAuction ===
|
|
|
|
[Fact]
|
|
public void SetSelectedAuctionDirect_SetsWithoutEvent()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
var auction = new AuctionInfo { AuctionId = "1" };
|
|
|
|
service.SetSelectedAuctionDirect(auction);
|
|
|
|
Assert.Equal("1", service.SelectedAuction!.AuctionId);
|
|
}
|
|
|
|
// === Log ===
|
|
|
|
[Fact]
|
|
public void AddLog_AddsEntry()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddLog("Test message");
|
|
|
|
Assert.Single(service.GlobalLog);
|
|
Assert.Equal("Test message", service.GlobalLog[0].Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddLog_TruncatesAtLimit()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
for (int i = 0; i < 600; i++)
|
|
{
|
|
service.AddLog($"Message {i}");
|
|
}
|
|
|
|
Assert.True(service.GlobalLog.Count <= 500);
|
|
}
|
|
|
|
[Fact]
|
|
public void ClearLog_EmptiesLog()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddLog("Test");
|
|
service.AddLog("Test2");
|
|
|
|
service.ClearLog();
|
|
|
|
Assert.Empty(service.GlobalLog);
|
|
}
|
|
|
|
// === Manual Bidding ===
|
|
|
|
[Fact]
|
|
public void IsManualBidding_Default_ReturnsFalse()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
Assert.False(service.IsManualBidding("123"));
|
|
}
|
|
|
|
[Fact]
|
|
public void StartManualBidding_SetsFlag()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.StartManualBidding("123");
|
|
|
|
Assert.True(service.IsManualBidding("123"));
|
|
}
|
|
|
|
[Fact]
|
|
public void StopManualBidding_ClearsFlag()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.StartManualBidding("123");
|
|
service.StopManualBidding("123");
|
|
|
|
Assert.False(service.IsManualBidding("123"));
|
|
}
|
|
|
|
// === Monitoring State ===
|
|
|
|
[Fact]
|
|
public void IsMonitoringActive_Default_False()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
Assert.False(service.IsMonitoringActive);
|
|
}
|
|
|
|
[Fact]
|
|
public void IsMonitoringActive_SetTrue_ReturnsTrue()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.IsMonitoringActive = true;
|
|
Assert.True(service.IsMonitoringActive);
|
|
}
|
|
|
|
// === Browser State ===
|
|
|
|
[Fact]
|
|
public void BrowserCategoryIndex_DefaultZero()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
Assert.Equal(0, service.BrowserCategoryIndex);
|
|
}
|
|
|
|
[Fact]
|
|
public void BrowserSearchQuery_DefaultEmpty()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
Assert.Equal("", service.BrowserSearchQuery);
|
|
}
|
|
|
|
[Fact]
|
|
public void BrowserCategoryIndex_SetAndGet()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.BrowserCategoryIndex = 5;
|
|
Assert.Equal(5, service.BrowserCategoryIndex);
|
|
}
|
|
|
|
[Fact]
|
|
public void BrowserSearchQuery_SetAndGet()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.BrowserSearchQuery = "iphone";
|
|
Assert.Equal("iphone", service.BrowserSearchQuery);
|
|
}
|
|
|
|
// === Direct References ===
|
|
|
|
[Fact]
|
|
public void GetAuctionsDirectRef_ReturnsSameList()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddAuction(new AuctionInfo { AuctionId = "1" });
|
|
|
|
var direct = service.GetAuctionsDirectRef();
|
|
|
|
Assert.Single(direct);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetLogDirectRef_ReturnsSameList()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
service.AddLog("test");
|
|
|
|
var direct = service.GetLogDirectRef();
|
|
|
|
Assert.Single(direct);
|
|
}
|
|
|
|
// === SessionStart ===
|
|
|
|
[Fact]
|
|
public void SessionStart_ReturnsNonDefault()
|
|
{
|
|
var service = new ApplicationStateService();
|
|
Assert.True(service.SessionStart > DateTime.MinValue);
|
|
}
|
|
}
|