Path: redb-route/tests/redb.Route.Tests.Mail/MailSearchTermTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Mail/MailSearchTermTests.cs ---- using MailKit.Search; using MimeKit; using redb.Route.Core; using redb.Route.Mail; namespace redb.Route.Tests.Mail; /// /// Apache Camel's mail searchTerm as an option group: the options bind in Camel's names, dates in Camel's format /// with now and offsets, the terms are ANDed, and a set search term replaces fetchFilter. /// public sealed class MailSearchTermTests { private static MailEndpointOptions Imap(string query) => ((ImapEndpoint)new ImapComponent().CreateEndpoint(EndpointUriParser.Parse($"imap://mail.example.com?password=p&{query}"))).EndpointOptions; [Fact] public void The_options_bind_in_camels_names() { var options = Imap("searchTerm.unseen=false&searchTerm.subjectOrBody=a&searchTerm.subject=b&searchTerm.body=c" + "&searchTerm.from=d&searchTerm.to=e&searchTerm.fromSentDate=now-24h&searchTerm.toSentDate=now" + "&searchTerm.fromReceivedDate=2026-11-01%2000:00:00&searchTerm.toReceivedDate=now%2B1h"); var term = options.SearchTerm!; term.Unseen.Should().BeFalse(); (term.SubjectOrBody, term.Subject, term.Body, term.From, term.To).Should().Be(("a", "b", "c", "d", "e")); (term.FromSentDate, term.ToSentDate, term.FromReceivedDate, term.ToReceivedDate) .Should().Be(("now-24h", "now", "2026-11-01 00:00:00", "now+1h")); } [Fact] public void SearchQuery_is_refused_naming_searchTerm() { var act = () => Imap("searchQuery=UNSEEN"); act.Should().Throw().WithMessage("*'searchQuery' is gone*searchTerm.subject=*"); } [Fact] public void An_unknown_member_is_refused_naming_the_members() { var act = () => Imap("searchTerm.sender=x"); act.Should().Throw().WithMessage("*the group searchTerm takes searchTerm, searchTerm.unseen*"); } [Fact] public void SearchTerm_beside_an_explicit_fetchFilter_is_refused() { var act = () => Imap("fetchFilter=All&searchTerm.subject=x"); act.Should().Throw().WithMessage("searchTerm selects the mails, as fetchFilter does*"); } [Theory] [InlineData("yesterday")] [InlineData("2026-11-19")] [InlineData("now-24x")] [InlineData("now-")] public void A_date_the_format_does_not_take_is_refused_naming_the_option(string value) { var act = () => Imap($"searchTerm.fromSentDate={Uri.EscapeDataString(value)}"); act.Should().Throw().WithMessage("'searchTerm.fromSentDate=*' is not a valid value*"); } [Theory] [InlineData("now", 0)] [InlineData("now-24h", -24 * 3600)] [InlineData("now - 24h", -24 * 3600)] [InlineData("now+1d12h", 36 * 3600)] [InlineData("now-5h30m", -(5 * 3600 + 1800))] [InlineData("now-90s", -90)] [InlineData("now-1500", -1.5)] public void Now_takes_camels_offsets(string value, double seconds) { var now = new DateTimeOffset(2026, 11, 19, 12, 0, 0, TimeSpan.Zero); MailSearchTerm.SearchDate.Parse("o", value).At(now).Should().Be(now + TimeSpan.FromSeconds(seconds)); } [Fact] public void A_fixed_date_is_read_in_the_hosts_time_zone() { var date = MailSearchTerm.SearchDate.Parse("o", "2026-11-19 10:30:15").At(DateTimeOffset.UtcNow); date.Should().Be(new DateTimeOffset(new DateTime(2026, 11, 19, 10, 30, 15, DateTimeKind.Local))); } [Fact] public void The_imap_query_ands_the_terms_and_widens_the_dates_to_whole_days() { var term = new MailSearchTerm { Subject = "invoice", From = "acme", FromSentDate = "2026-11-19 10:30:00", ToSentDate = "2026-11-20 08:00:00" }; var leaves = Leaves(term.ToImapQuery(DateTimeOffset.Now)).ToList(); leaves.Should().Contain(SearchQuery.NotSeen, "searchTerm.unseen defaults to true, as in Camel"); leaves.OfType().Select(t => (t.Term, t.Text)) .Should().BeEquivalentTo([(SearchTerm.SubjectContains, "invoice"), (SearchTerm.FromContains, "acme")]); leaves.OfType().Select(d => (d.Term, d.Date)) .Should().BeEquivalentTo([(SearchTerm.SentSince, new DateTime(2026, 11, 19)), (SearchTerm.SentBefore, new DateTime(2026, 11, 21))], "IMAP compares days: since the bound's day, before the day after the upper bound"); Ands(term.ToImapQuery(DateTimeOffset.Now)).Should().BeTrue("the terms are ANDed"); } private static IEnumerable Leaves(SearchQuery query) => query is BinarySearchQuery binary ? Leaves(binary.Left).Concat(Leaves(binary.Right)) : [query]; private static bool Ands(SearchQuery query) => query is not BinarySearchQuery binary || (binary.Term == SearchTerm.And && Ands(binary.Left) && Ands(binary.Right)); [Fact] public void Without_terms_and_unseen_false_the_query_takes_everything() => new MailSearchTerm { Unseen = false }.ToImapQuery(DateTimeOffset.Now).Should().BeSameAs(SearchQuery.All); [Fact] public void The_date_bounds_are_exact_and_a_mail_without_the_date_does_not_match() { var now = new DateTimeOffset(2026, 11, 19, 12, 0, 0, TimeSpan.Zero); var term = new MailSearchTerm { FromSentDate = "now-1h", ToReceivedDate = "now" }; term.MatchesDates(now.AddMinutes(-30), now.AddMinutes(-1), now).Should().BeTrue(); term.MatchesDates(now.AddMinutes(-61), now.AddMinutes(-1), now).Should().BeFalse("sent before the bound"); term.MatchesDates(now.AddMinutes(-30), now.AddMinutes(1), now).Should().BeFalse("received after the bound"); term.MatchesDates(null, now, now).Should().BeFalse("no sent date"); } [Fact] public void Pop3_checks_the_text_terms_on_the_mail() { var mime = new MimeMessage { Subject = "Invoice 42", Date = DateTimeOffset.Now }; mime.From.Add(new MailboxAddress("Acme", "billing@acme.example")); mime.To.Add(new MailboxAddress("Us", "in@us.example")); mime.Body = new TextPart("plain") { Text = "Please pay." }; new MailSearchTerm { Subject = "invoice", From = "acme", To = "us.example", Body = "PAY" } .Matches(mime, DateTimeOffset.Now).Should().BeTrue(); new MailSearchTerm { SubjectOrBody = "please" }.Matches(mime, DateTimeOffset.Now).Should().BeTrue(); new MailSearchTerm { Subject = "receipt" }.Matches(mime, DateTimeOffset.Now).Should().BeFalse(); new MailSearchTerm { FromSentDate = "now+1h" }.Matches(mime, DateTimeOffset.Now).Should().BeFalse(); } [Fact] public void The_builder_writes_the_group_in_camels_names() { var uri = redb.Route.Mail.Imap.Read("mail.example.com") .SearchTerm(t => t.Subject("invoice").FromSentDate("now-24h").Unseen(false)) .Build(); uri.Should().Contain("searchTerm.subject=invoice").And.Contain("searchTerm.fromSentDate=now-24h") .And.Contain("searchTerm.unseen=false"); redb.Route.Mail.Imap.Read("mail.example.com").SearchTerm("#q").Build().Should().Contain("searchTerm=%23q"); } }