|
| 1 | +using LogExpert.Classes; |
| 2 | +using LogExpert.Core.Classes.IPC; |
| 3 | +using LogExpert.Core.Interface; |
| 4 | + |
| 5 | +using Moq; |
| 6 | + |
| 7 | +using Newtonsoft.Json; |
| 8 | + |
| 9 | +using NUnit.Framework; |
| 10 | + |
| 11 | +namespace LogExpert.Tests.IPC; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Unit tests for One Instance Only feature - IPC logic tests |
| 15 | +/// Tests the IPC message serialization and handling logic |
| 16 | +/// </summary> |
| 17 | +[TestFixture] |
| 18 | +public class OneInstanceIpcTests |
| 19 | +{ |
| 20 | + #region IPC Message Type Tests |
| 21 | + |
| 22 | + [Test] |
| 23 | + public void SerializeCommand_WhenAllowOnlyOneInstance_UsesNewWindowOrLockedWindowType () |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + string[] files = ["test.log"]; |
| 27 | + bool allowOnlyOne = true; |
| 28 | + |
| 29 | + // Act |
| 30 | + // Note: We can't call the private method directly, so we test the integration |
| 31 | + // through the public API. This test verifies the expected behavior. |
| 32 | + // For unit testing, we'd need to make SerializeCommandIntoNonFormattedJSON internal |
| 33 | + // or use InternalsVisibleTo attribute. |
| 34 | + |
| 35 | + // For now, we test the IpcMessage structure directly |
| 36 | + var message = new IpcMessage |
| 37 | + { |
| 38 | + Type = allowOnlyOne ? IpcMessageType.NewWindowOrLockedWindow : IpcMessageType.NewWindow, |
| 39 | + Payload = Newtonsoft.Json.Linq.JObject.FromObject(new LoadPayload { Files = [.. files] }) |
| 40 | + }; |
| 41 | + |
| 42 | + var json = JsonConvert.SerializeObject(message, Formatting.None); |
| 43 | + var deserialized = JsonConvert.DeserializeObject<IpcMessage>(json); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.That(deserialized.Type, Is.EqualTo(IpcMessageType.NewWindowOrLockedWindow)); |
| 47 | + var payload = deserialized.Payload.ToObject<LoadPayload>(); |
| 48 | + Assert.That(payload, Is.Not.Null); |
| 49 | + Assert.That(payload.Files.Count, Is.EqualTo(1)); |
| 50 | + Assert.That(payload.Files[0], Is.EqualTo("test.log")); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void SerializeCommand_WhenMultipleInstancesAllowed_UsesNewWindowType () |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + string[] files = ["test.log"]; |
| 58 | + bool allowOnlyOne = false; |
| 59 | + |
| 60 | + // Act |
| 61 | + var message = new IpcMessage |
| 62 | + { |
| 63 | + Type = allowOnlyOne ? IpcMessageType.NewWindowOrLockedWindow : IpcMessageType.NewWindow, |
| 64 | + Payload = Newtonsoft.Json.Linq.JObject.FromObject(new LoadPayload { Files = [.. files] }) |
| 65 | + }; |
| 66 | + |
| 67 | + var json = JsonConvert.SerializeObject(message, Formatting.None); |
| 68 | + var deserialized = JsonConvert.DeserializeObject<IpcMessage>(json); |
| 69 | + |
| 70 | + // Assert |
| 71 | + Assert.That(deserialized.Type, Is.EqualTo(IpcMessageType.NewWindow)); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public void IpcMessage_SerializesAndDeserializesCorrectly () |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + var originalMessage = new IpcMessage |
| 79 | + { |
| 80 | + Type = IpcMessageType.Load, |
| 81 | + Payload = Newtonsoft.Json.Linq.JObject.FromObject(new LoadPayload |
| 82 | + { |
| 83 | + Files = ["file1.log", "file2.log", "file3.log"] |
| 84 | + }) |
| 85 | + }; |
| 86 | + |
| 87 | + // Act |
| 88 | + var json = JsonConvert.SerializeObject(originalMessage, Formatting.None); |
| 89 | + var deserializedMessage = JsonConvert.DeserializeObject<IpcMessage>(json); |
| 90 | + |
| 91 | + // Assert |
| 92 | + Assert.That(deserializedMessage, Is.Not.Null); |
| 93 | + Assert.That(deserializedMessage.Type, Is.EqualTo(originalMessage.Type)); |
| 94 | + |
| 95 | + var originalPayload = originalMessage.Payload.ToObject<LoadPayload>(); |
| 96 | + var deserializedPayload = deserializedMessage.Payload.ToObject<LoadPayload>(); |
| 97 | + |
| 98 | + Assert.That(deserializedPayload.Files.Count, Is.EqualTo(originalPayload.Files.Count)); |
| 99 | + for (int i = 0; i < originalPayload.Files.Count; i++) |
| 100 | + { |
| 101 | + Assert.That(deserializedPayload.Files[i], Is.EqualTo(originalPayload.Files[i])); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + [Test] |
| 106 | + public void IpcMessage_MultipleFiles_SerializesCorrectly () |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + string[] multipleFiles = ["log1.txt", "log2.txt", "log3.txt", "log4.txt"]; |
| 110 | + var message = new IpcMessage |
| 111 | + { |
| 112 | + Type = IpcMessageType.NewWindowOrLockedWindow, |
| 113 | + Payload = Newtonsoft.Json.Linq.JObject.FromObject(new LoadPayload { Files = [.. multipleFiles] }) |
| 114 | + }; |
| 115 | + |
| 116 | + // Act |
| 117 | + var json = JsonConvert.SerializeObject(message, Formatting.None); |
| 118 | + var deserialized = JsonConvert.DeserializeObject<IpcMessage>(json); |
| 119 | + |
| 120 | + // Assert |
| 121 | + var payload = deserialized.Payload.ToObject<LoadPayload>(); |
| 122 | + Assert.That(payload.Files.Count, Is.EqualTo(4)); |
| 123 | + Assert.That(payload.Files, Is.EquivalentTo(multipleFiles)); |
| 124 | + } |
| 125 | + |
| 126 | + [Test] |
| 127 | + public void IpcMessage_EmptyFileList_SerializesCorrectly () |
| 128 | + { |
| 129 | + // Arrange |
| 130 | + var message = new IpcMessage |
| 131 | + { |
| 132 | + Type = IpcMessageType.NewWindow, |
| 133 | + Payload = Newtonsoft.Json.Linq.JObject.FromObject(new LoadPayload { Files = [] }) |
| 134 | + }; |
| 135 | + |
| 136 | + // Act |
| 137 | + var json = JsonConvert.SerializeObject(message, Formatting.None); |
| 138 | + var deserialized = JsonConvert.DeserializeObject<IpcMessage>(json); |
| 139 | + |
| 140 | + // Assert |
| 141 | + var payload = deserialized.Payload.ToObject<LoadPayload>(); |
| 142 | + Assert.That(payload.Files, Is.Empty); |
| 143 | + } |
| 144 | + |
| 145 | + #endregion |
| 146 | + |
| 147 | + #region IPC Message Type Enum Tests |
| 148 | + |
| 149 | + [Test] |
| 150 | + public void IpcMessageType_HasCorrectValues () |
| 151 | + { |
| 152 | + // Assert - verify the enum values exist |
| 153 | + Assert.That(IpcMessageType.Load, Is.Not.Null); |
| 154 | + Assert.That(IpcMessageType.NewWindow, Is.Not.Null); |
| 155 | + Assert.That(IpcMessageType.NewWindowOrLockedWindow, Is.Not.Null); |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + public void IpcMessageType_Load_IsDistinctFromNewWindow () |
| 160 | + { |
| 161 | + // Assert |
| 162 | + Assert.That(IpcMessageType.Load, Is.Not.EqualTo(IpcMessageType.NewWindow)); |
| 163 | + } |
| 164 | + |
| 165 | + [Test] |
| 166 | + public void IpcMessageType_NewWindowOrLockedWindow_IsDistinctFromOthers () |
| 167 | + { |
| 168 | + // Assert |
| 169 | + Assert.That(IpcMessageType.NewWindowOrLockedWindow, Is.Not.EqualTo(IpcMessageType.Load)); |
| 170 | + Assert.That(IpcMessageType.NewWindowOrLockedWindow, Is.Not.EqualTo(IpcMessageType.NewWindow)); |
| 171 | + } |
| 172 | + |
| 173 | + #endregion |
| 174 | + |
| 175 | + #region LoadPayload Tests |
| 176 | + |
| 177 | + [Test] |
| 178 | + public void LoadPayload_CanBeCreatedEmpty () |
| 179 | + { |
| 180 | + // Arrange & Act |
| 181 | + var payload = new LoadPayload(); |
| 182 | + |
| 183 | + // Assert |
| 184 | + Assert.That(payload, Is.Not.Null); |
| 185 | + Assert.That(payload.Files, Is.Not.Null); |
| 186 | + } |
| 187 | + |
| 188 | + [Test] |
| 189 | + public void LoadPayload_CanBeCreatedWithFiles () |
| 190 | + { |
| 191 | + // Arrange & Act |
| 192 | + var payload = new LoadPayload |
| 193 | + { |
| 194 | + Files = ["test1.log", "test2.log"] |
| 195 | + }; |
| 196 | + |
| 197 | + // Assert |
| 198 | + Assert.That(payload.Files, Has.Count.EqualTo(2)); |
| 199 | + Assert.That(payload.Files[0], Is.EqualTo("test1.log")); |
| 200 | + Assert.That(payload.Files[1], Is.EqualTo("test2.log")); |
| 201 | + } |
| 202 | + |
| 203 | + #endregion |
| 204 | +} |
0 commit comments