using Microsoft.EntityFrameworkCore; using RadicadeServer.DB; using RadicadeServer.RecNet.Rooms; using RadicadeServer.Utils; using Xunit; namespace RadicadeServer.Tests; /// /// A dorm is the one room every player is certain to see, and it had no picture of its own: the /// row carried RoomData's "default.png", which is not a file on disk, so the client drew its own /// placeholder and every dorm looked the same in the panel. images.txt ships a Dorm picture, so a /// dorm with nothing set gets it - while a dorm whose picture was set on purpose keeps it. /// public class DormRoomImageTests { private static RecDBContext NewDb() => new(TestDbOptions.Create() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); private static RoomData Room(long id, string name, bool isDorm, string image, long creator = 1) => new() { RoomId = id, Name = name, Description = name, ImageName = image, CreatorAccountId = creator, IsDorm = isDorm, MaxPlayers = 10, CreatedAt = DateTime.UtcNow, }; [Fact] public async Task ANewDormRoomGetsTheDormPicture() { var db = NewDb(); await db.AddDormRoom(); Assert.Equal("Dorm", (await db.Rooms.SingleAsync(room => room.IsDorm)).ImageName); } [Fact] public async Task DormsWithNoPictureOfTheirOwnGetTheDormPictureAndSetOnesAreLeftAlone() { var db = NewDb(); db.Rooms.AddRange( Room(1, "DormRoom", isDorm: true, image: "default.png"), Room(2, "DormRoom", isDorm: true, image: " "), Room(3, "DormRoom", isDorm: true, image: DBConfig.DefaultRoomImage), Room(4, "DormRoom", isDorm: true, image: "MyCustomDormPicture"), Room(5, "MyCoolRoom", isDorm: false, image: "default.png", creator: 2)); db.SaveChanges(); await db.NormalizeDormRoomImagesAsync(); Assert.Equal("Dorm", (await db.Rooms.SingleAsync(room => room.RoomId == 1)).ImageName); Assert.Equal("Dorm", (await db.Rooms.SingleAsync(room => room.RoomId == 2)).ImageName); Assert.Equal("Dorm", (await db.Rooms.SingleAsync(room => room.RoomId == 3)).ImageName); Assert.Equal("MyCustomDormPicture", (await db.Rooms.SingleAsync(room => room.RoomId == 4)).ImageName); // Not a dorm: the room default there is somebody else's business. Assert.Equal("default.png", (await db.Rooms.SingleAsync(room => room.RoomId == 5)).ImageName); // Runs on every boot, so a second pass must be a no-op. await db.NormalizeDormRoomImagesAsync(); Assert.Equal("MyCustomDormPicture", (await db.Rooms.SingleAsync(room => room.RoomId == 4)).ImageName); } [Fact] public void TheImagesSetShipsBothPictures() { var lines = File.ReadAllLines(Path.Combine(FindServerDirectory(), "images.txt")); Assert.Contains("Dorm=https://cdn.radicade.app/Dorm.png", lines); Assert.Contains("Showdown=https://cdn.radicade.app/Showdown.png", lines); } /// /// images.txt lives next to the project file and is copied to the test output too, so walk up /// from the test binary until the Server project's copy shows up. /// private static string FindServerDirectory() { var directory = new DirectoryInfo(AppContext.BaseDirectory); while (directory != null) { var candidate = Path.Combine(directory.FullName, "Server", "images.txt"); if (File.Exists(candidate)) return Path.Combine(directory.FullName, "Server"); directory = directory.Parent; } throw new InvalidOperationException("could not find Server/images.txt from the test output directory"); } }