neon-sprawl/server/NeonSprawl.Server.Tests/Game/Quests/PlayerQuestProgressPersiste...

91 lines
3.7 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NeonSprawl.Server.Game.PositionState;
using NeonSprawl.Server.Game.Quests;
using NeonSprawl.Server.Tests.Game.PositionState;
using Npgsql;
using Xunit;
namespace NeonSprawl.Server.Tests.Game.Quests;
[Collection("Postgres integration")]
public sealed class PlayerQuestProgressPersistenceIntegrationTests(PostgresIntegrationHarness harness)
{
private PostgresWebApplicationFactory Factory => harness.Factory;
private const string PlayerId = "dev-local-1";
private const string GatherQuestId = "prototype_quest_gather_intro";
private const string GatherObjectiveId = "gather_intro_obj_scrap";
private static readonly DateTimeOffset CompletedAt = new(2026, 6, 3, 14, 30, 0, TimeSpan.Zero);
[RequirePostgresFact]
public async Task ActivateCounterComplete_ShouldPersistAcrossNewFactory()
{
// Arrange
await ResetQuestProgressTableAsync();
using (var scope = Factory.Services.CreateScope())
{
var store = scope.ServiceProvider.GetRequiredService<IPlayerQuestStateStore>();
// Act — write on first host
Assert.True(store.TryActivate(PlayerId, GatherQuestId, out _));
Assert.True(store.TryUpdateObjectiveCounter(PlayerId, GatherQuestId, GatherObjectiveId, 3, out _));
Assert.True(store.TryMarkComplete(PlayerId, GatherQuestId, CompletedAt, out _));
}
QuestStepState readBack;
await using (var secondFactory = new PostgresWebApplicationFactory())
{
using var scope = secondFactory.Services.CreateScope();
var store = scope.ServiceProvider.GetRequiredService<IPlayerQuestStateStore>();
readBack = store.TryGetProgress(PlayerId, GatherQuestId, out var snapshot) ? snapshot : null!;
}
// Assert
Assert.NotNull(readBack);
Assert.Equal(QuestProgressStatus.Completed, readBack.Status);
Assert.Equal(0, readBack.CurrentStepIndex);
Assert.Equal(3, readBack.ObjectiveCounters[GatherObjectiveId]);
Assert.Equal(CompletedAt, readBack.CompletedAt);
}
private async Task ResetQuestProgressTableAsync()
{
var cs = Environment.GetEnvironmentVariable("ConnectionStrings__NeonSprawl");
if (string.IsNullOrWhiteSpace(cs))
{
throw new InvalidOperationException("ConnectionStrings__NeonSprawl is not set.");
}
_ = Factory.Services;
var options = Factory.Services.GetRequiredService<IOptions<GamePositionOptions>>().Value;
var positionDdlPath = Path.Combine(AppContext.BaseDirectory, "db", "migrations", "V001__player_position.sql");
var questDdlPath = Path.Combine(AppContext.BaseDirectory, "db", "migrations", "V008__player_quest_progress.sql");
if (!File.Exists(positionDdlPath) || !File.Exists(questDdlPath))
{
throw new FileNotFoundException("Test DDL for quest progress persistence not found.");
}
var positionDdl = await File.ReadAllTextAsync(positionDdlPath);
var questDdl = await File.ReadAllTextAsync(questDdlPath);
await using var conn = new NpgsqlConnection(cs);
await conn.OpenAsync();
await using (var applyPosition = new NpgsqlCommand(positionDdl, conn))
{
await applyPosition.ExecuteNonQueryAsync();
}
await using (var truncate = new NpgsqlCommand("TRUNCATE player_position CASCADE;", conn))
{
await truncate.ExecuteNonQueryAsync();
}
PostgresPositionBootstrap.SeedDevPlayer(conn, options);
await using (var applyQuest = new NpgsqlCommand(questDdl, conn))
{
await applyQuest.ExecuteNonQueryAsync();
}
}
}