|
| 1 | +using System.Reflection; |
| 2 | +using StabilityMatrix.Core.Models; |
| 3 | +using StabilityMatrix.Core.Models.Api; |
| 4 | +using StabilityMatrix.Core.Models.Database; |
| 5 | +using StabilityMatrix.Core.Services; |
| 6 | + |
| 7 | +namespace StabilityMatrix.Tests.Core; |
| 8 | + |
| 9 | +[TestClass] |
| 10 | +public class ModelIndexServiceTests |
| 11 | +{ |
| 12 | + [TestMethod] |
| 13 | + public void GetHasEarlyAccessUpdateOnly_ReturnsTrue_WhenAllNewerVersionsAreEarlyAccess() |
| 14 | + { |
| 15 | + var model = CreateLocalModel(installedVersionId: 100, hasUpdate: true); |
| 16 | + var remoteModel = CreateRemoteModel( |
| 17 | + CreateVersion(id: 300, isEarlyAccess: true), |
| 18 | + CreateVersion(id: 200, isEarlyAccess: true), |
| 19 | + CreateVersion(id: 100, isEarlyAccess: false) |
| 20 | + ); |
| 21 | + |
| 22 | + var result = InvokeGetHasEarlyAccessUpdateOnly(model, remoteModel); |
| 23 | + |
| 24 | + Assert.IsTrue(result); |
| 25 | + } |
| 26 | + |
| 27 | + [TestMethod] |
| 28 | + public void GetHasEarlyAccessUpdateOnly_ReturnsFalse_WhenAnyNewerVersionIsPublic() |
| 29 | + { |
| 30 | + var model = CreateLocalModel(installedVersionId: 100, hasUpdate: true); |
| 31 | + var remoteModel = CreateRemoteModel( |
| 32 | + CreateVersion(id: 300, isEarlyAccess: true), |
| 33 | + CreateVersion(id: 200, isEarlyAccess: false), |
| 34 | + CreateVersion(id: 100, isEarlyAccess: false) |
| 35 | + ); |
| 36 | + |
| 37 | + var result = InvokeGetHasEarlyAccessUpdateOnly(model, remoteModel); |
| 38 | + |
| 39 | + Assert.IsFalse(result); |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public void GetHasEarlyAccessUpdateOnly_ReturnsFalse_WhenInstalledVersionIsLatest() |
| 44 | + { |
| 45 | + var model = CreateLocalModel(installedVersionId: 100, hasUpdate: true); |
| 46 | + var remoteModel = CreateRemoteModel( |
| 47 | + CreateVersion(id: 100, isEarlyAccess: false), |
| 48 | + CreateVersion(id: 90, isEarlyAccess: true) |
| 49 | + ); |
| 50 | + |
| 51 | + var result = InvokeGetHasEarlyAccessUpdateOnly(model, remoteModel); |
| 52 | + |
| 53 | + Assert.IsFalse(result); |
| 54 | + } |
| 55 | + |
| 56 | + [TestMethod] |
| 57 | + public void GetHasEarlyAccessUpdateOnly_ReturnsFalse_WhenModelHasNoUpdate() |
| 58 | + { |
| 59 | + var model = CreateLocalModel(installedVersionId: 100, hasUpdate: false); |
| 60 | + var remoteModel = CreateRemoteModel( |
| 61 | + CreateVersion(id: 300, isEarlyAccess: true), |
| 62 | + CreateVersion(id: 200, isEarlyAccess: true), |
| 63 | + CreateVersion(id: 100, isEarlyAccess: false) |
| 64 | + ); |
| 65 | + |
| 66 | + var result = InvokeGetHasEarlyAccessUpdateOnly(model, remoteModel); |
| 67 | + |
| 68 | + Assert.IsFalse(result); |
| 69 | + } |
| 70 | + |
| 71 | + private static bool InvokeGetHasEarlyAccessUpdateOnly(LocalModelFile model, CivitModel? remoteModel) |
| 72 | + { |
| 73 | + var method = typeof(ModelIndexService).GetMethod( |
| 74 | + "GetHasEarlyAccessUpdateOnly", |
| 75 | + BindingFlags.NonPublic | BindingFlags.Static |
| 76 | + ); |
| 77 | + |
| 78 | + Assert.IsNotNull(method); |
| 79 | + |
| 80 | + var result = method.Invoke(null, [model, remoteModel]); |
| 81 | + |
| 82 | + Assert.IsNotNull(result); |
| 83 | + |
| 84 | + return (bool)result; |
| 85 | + } |
| 86 | + |
| 87 | + private static LocalModelFile CreateLocalModel(int installedVersionId, bool hasUpdate) |
| 88 | + { |
| 89 | + return new LocalModelFile |
| 90 | + { |
| 91 | + RelativePath = "StableDiffusion/test-model.safetensors", |
| 92 | + SharedFolderType = SharedFolderType.StableDiffusion, |
| 93 | + HasUpdate = hasUpdate, |
| 94 | + ConnectedModelInfo = new ConnectedModelInfo |
| 95 | + { |
| 96 | + ModelId = 123, |
| 97 | + VersionId = installedVersionId, |
| 98 | + Source = ConnectedModelSource.Civitai, |
| 99 | + ModelName = "Test Model", |
| 100 | + ModelDescription = string.Empty, |
| 101 | + VersionName = $"v{installedVersionId}", |
| 102 | + Tags = [], |
| 103 | + Hashes = new CivitFileHashes(), |
| 104 | + }, |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + private static CivitModel CreateRemoteModel(params CivitModelVersion[] versions) |
| 109 | + { |
| 110 | + return new CivitModel |
| 111 | + { |
| 112 | + Id = 123, |
| 113 | + Name = "Test Model", |
| 114 | + Description = string.Empty, |
| 115 | + Type = CivitModelType.Unknown, |
| 116 | + Tags = [], |
| 117 | + Stats = new CivitModelStats(), |
| 118 | + ModelVersions = versions.ToList(), |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + private static CivitModelVersion CreateVersion(int id, bool isEarlyAccess) |
| 123 | + { |
| 124 | + return new CivitModelVersion |
| 125 | + { |
| 126 | + Id = id, |
| 127 | + Name = $"v{id}", |
| 128 | + Description = string.Empty, |
| 129 | + DownloadUrl = string.Empty, |
| 130 | + TrainedWords = [], |
| 131 | + Availability = isEarlyAccess ? "EarlyAccess" : "Public", |
| 132 | + Stats = new CivitModelStats(), |
| 133 | + }; |
| 134 | + } |
| 135 | +} |
0 commit comments