using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SVSim.Database;
using SVSim.Database.Enums;
using SVSim.Database.Models;
using SVSim.Database.Repositories.Viewer;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Repositories;
///
/// Direct tests against . The owned-type lookup in
/// previously used
/// _dbContext.Set<SocialAccountConnection>() which EF couldn't translate (owned
/// types aren't queryable as a root). This test would have caught the regression.
///
public class ViewerRepositoryTests
{
[Test]
public async Task GetViewerBySocialConnection_returns_viewer_when_steam_id_matches()
{
using var factory = new SVSimTestFactory();
const ulong steamId = 76_561_198_111_222_333UL;
long expectedViewerId = await factory.SeedViewerAsync(steamId: steamId, displayName: "Owner");
using var scope = factory.Services.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService();
Viewer? found = await repo.GetViewerBySocialConnection(SocialAccountType.Steam, steamId);
Assert.That(found, Is.Not.Null, "Expected to find the seeded viewer by Steam social connection.");
Assert.That(found!.Id, Is.EqualTo(expectedViewerId));
Assert.That(found.DisplayName, Is.EqualTo("Owner"));
}
[Test]
public async Task GetViewerBySocialConnection_returns_null_when_steam_id_does_not_match()
{
using var factory = new SVSimTestFactory();
await factory.SeedViewerAsync(steamId: 76_561_198_111_222_333UL);
using var scope = factory.Services.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService();
Viewer? found = await repo.GetViewerBySocialConnection(SocialAccountType.Steam, 76_561_198_999_999_999UL);
Assert.That(found, Is.Null);
}
[Test]
public async Task RegisterViewer_grants_default_leader_skins_to_classes()
{
// Guards the just-fixed nav-graph NRE — RegisterViewer iterates ClassEntry.LeaderSkins
// and needs the .Include to populate them. If the include is lost, this throws inside
// SeedViewerAsync rather than reaching the assertion.
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService();
Viewer viewer = await db.Viewers
.Include(v => v.Classes).ThenInclude(c => c.LeaderSkin)
.Include(v => v.LeaderSkins)
.FirstAsync(v => v.Id == viewerId);
Assert.That(viewer.Classes, Is.Not.Empty, "RegisterViewer should populate Classes from seed data.");
Assert.That(viewer.Classes.Select(c => c.LeaderSkin).All(s => s is not null), Is.True,
"Every class should have a LeaderSkin assigned (placeholder or real).");
Assert.That(viewer.LeaderSkins, Is.Not.Empty,
"Viewer should own at least one leader skin from class defaults.");
}
}