|
| 1 | +using Mailist.Utilities; |
| 2 | +using MailKit; |
| 3 | +using MailKit.Net.Imap; |
| 4 | +using Moq; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Mailist.Tests; |
| 12 | + |
| 13 | +public class ImapClientTests |
| 14 | +{ |
| 15 | + [Fact] |
| 16 | + public async Task IdleFetch_ReturnsExistingMessages() |
| 17 | + { |
| 18 | + var cancel = new CancellationTokenSource(); |
| 19 | + var client = new Mock<IImapClient>(); |
| 20 | + var folder = new Mock<IMailFolder>(); |
| 21 | + var existingMessage = new Mock<IMessageSummary>(); |
| 22 | + client.Setup(c => c.Capabilities).Returns(ImapCapabilities.Idle); |
| 23 | + client.Setup(c => c.IdleAsync(It.IsAny<CancellationToken>(), It.IsAny<CancellationToken>())) |
| 24 | + .Returns(async (CancellationToken doneToken, CancellationToken cancellationToken) => |
| 25 | + { |
| 26 | + try |
| 27 | + { |
| 28 | + using var cts = CancellationTokenSource.CreateLinkedTokenSource(doneToken, cancellationToken); |
| 29 | + await Task.Delay(-1, cts.Token); |
| 30 | + } |
| 31 | + catch (OperationCanceledException) when (doneToken.IsCancellationRequested) |
| 32 | + { |
| 33 | + } |
| 34 | + }); |
| 35 | + folder.Setup(f => f.IsOpen).Returns(true); |
| 36 | + folder.Setup(f => f.Count).Returns(1); |
| 37 | + folder.Setup(f => f.FetchAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<IFetchRequest>(), It.IsAny<CancellationToken>()).Result) |
| 38 | + .Returns([existingMessage.Object]); |
| 39 | + existingMessage.Setup(m => m.UniqueId).Returns(new UniqueId(1)); |
| 40 | + |
| 41 | + IAsyncEnumerator<IMessageSummary> enumerator = client.Object |
| 42 | + .IdleFetchAsync(folder.Object, MessageSummaryItems.UniqueId, cancel.Token) |
| 43 | + .GetAsyncEnumerator(CancellationToken.None); |
| 44 | + |
| 45 | + Assert.True(await enumerator.MoveNextAsync()); |
| 46 | + Assert.Same(existingMessage.Object, enumerator.Current); |
| 47 | + |
| 48 | + cancel.CancelAfter(100); |
| 49 | + await Assert.ThrowsAnyAsync<OperationCanceledException>(() => enumerator.MoveNextAsync().AsTask()); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task IdleFetch_ReturnsNewlyArrivedMessages() |
| 54 | + { |
| 55 | + var cancel = new CancellationTokenSource(); |
| 56 | + var client = new Mock<IImapClient>(); |
| 57 | + var folder = new Mock<IMailFolder>(); |
| 58 | + var newMessage = new Mock<IMessageSummary>(); |
| 59 | + client.Setup(c => c.Capabilities).Returns(ImapCapabilities.Idle); |
| 60 | + client.Setup(c => c.IdleAsync(It.IsAny<CancellationToken>(), It.IsAny<CancellationToken>())) |
| 61 | + .Returns(async (CancellationToken doneToken, CancellationToken cancellationToken) => |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + using var cts = CancellationTokenSource.CreateLinkedTokenSource(doneToken, cancellationToken); |
| 66 | + await Task.Delay(-1, cts.Token); |
| 67 | + } |
| 68 | + catch (OperationCanceledException) when (doneToken.IsCancellationRequested) |
| 69 | + { |
| 70 | + } |
| 71 | + }); |
| 72 | + folder.Setup(f => f.IsOpen).Returns(true); |
| 73 | + folder.Setup(f => f.Count).Returns(0); |
| 74 | + |
| 75 | + IAsyncEnumerator<IMessageSummary> enumerator = client.Object |
| 76 | + .IdleFetchAsync(folder.Object, MessageSummaryItems.UniqueId, cancel.Token) |
| 77 | + .GetAsyncEnumerator(CancellationToken.None); |
| 78 | + |
| 79 | + ValueTask<bool> task = enumerator.MoveNextAsync(); |
| 80 | + await Task.Delay(100, TestContext.Current.CancellationToken); |
| 81 | + folder.Setup(f => f.FetchAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<IFetchRequest>(), It.IsAny<CancellationToken>()).Result) |
| 82 | + .Returns([newMessage.Object]); |
| 83 | + folder.Setup(f => f.Count).Returns(1); |
| 84 | + folder.Raise(f => f.CountChanged += null, EventArgs.Empty); |
| 85 | + |
| 86 | + Assert.True(await task); |
| 87 | + Assert.Same(newMessage.Object, enumerator.Current); |
| 88 | + |
| 89 | + cancel.CancelAfter(100); |
| 90 | + await Assert.ThrowsAnyAsync<OperationCanceledException>(() => enumerator.MoveNextAsync().AsTask()); |
| 91 | + } |
| 92 | +} |
0 commit comments