using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using RadicadeServer.Admin; using RadicadeServer.DB; using RadicadeServer.RecNet.Rooms; using Xunit; namespace RadicadeServer.Tests; [Collection(DbFactoryCollection.Name)] public class AdminBundleBindingTests { [Fact] public async Task BindingRequiresAdminExistingBundleAndMatchingRoomAndPreservesRoomSave() { var root = Directory.CreateTempSubdirectory("radicade-bundle-binding-").FullName; var oldStorage = DBConfig.CDNStorage; var oldKey = DBConfig.AdminApiKey; DBConfig.CDNStorage = root; DBConfig.AdminApiKey = "binding-test-key"; try { await using var db = new RecDBContext(TestDbOptions.Create() .UseInMemoryDatabase(Guid.NewGuid().ToString()).Options); var sub = new SubRoom { RoomId = 228, SubRoomId = 280, DataBlob = "original.room" }; db.SubRooms.Add(sub); await db.SaveChangesAsync(); var api = new AdminRoomAPI(db); var id = new string('a', 40); Assert.IsType(await api.BindAssetBundle("wrong", 228, 280, id)); Assert.IsType(await api.BindAssetBundle("binding-test-key", 228, 280, "../bad")); Assert.IsType(await api.BindAssetBundle("binding-test-key", 228, 280, id)); await File.WriteAllTextAsync(Path.Join(root, id + ".assetbundle"), "test bundle"); Assert.IsType(await api.BindAssetBundle("binding-test-key", 229, 280, id)); Assert.Equal("", sub.UnityAssetId); Assert.IsType(await api.BindAssetBundle("binding-test-key", 228, 280, id)); db.ChangeTracker.Clear(); var stored = await db.SubRooms.SingleAsync(); Assert.Equal(id, stored.UnityAssetId); Assert.Equal("original.room", stored.DataBlob); Assert.IsType(await api.BindAssetBundle("binding-test-key", 228, 280, id)); await File.WriteAllTextAsync(Path.Join(root, id), "shadowing manifest"); Assert.IsType(await api.BindAssetBundle("binding-test-key", 228, 280, id)); } finally { DBConfig.CDNStorage = oldStorage; DBConfig.AdminApiKey = oldKey; Directory.Delete(root, recursive: true); } } }