|
| 1 | +using PapersCli.Cli.Commands; |
| 2 | +using PapersCli.Cli.Config; |
| 3 | +using PapersCli.Cli.Data; |
| 4 | +using PapersCli.Cli.Models; |
| 5 | +using PapersCli.Cli.Sources; |
| 6 | + |
| 7 | +namespace PapersCli.Cli.Tests.Commands; |
| 8 | + |
| 9 | +public class SearchCommandTests |
| 10 | +{ |
| 11 | + [Test] |
| 12 | + [NotInParallel("Console")] |
| 13 | + public async Task Search_UsesConfigDefaultSourceAndPassesPagingOptions() |
| 14 | + { |
| 15 | + var source = new FakeSource("arxiv"); |
| 16 | + var command = CreateCommand(new AppConfig { DefaultSource = "arxiv" }, source); |
| 17 | + |
| 18 | + var (_, output, _) = await CaptureConsoleAsync(() => |
| 19 | + command.Search("attention", limit: 10, page: 2, json: true)); |
| 20 | + |
| 21 | + await Assert.That(source.Calls).IsEqualTo(1); |
| 22 | + await Assert.That(source.LastQuery).IsEqualTo("attention"); |
| 23 | + await Assert.That(source.LastLimit).IsEqualTo(10); |
| 24 | + await Assert.That(source.LastPage).IsEqualTo(2); |
| 25 | + await Assert.That(output.Contains("\"total_results\": 30")).IsTrue(); |
| 26 | + await Assert.That(output.Contains("\"total_pages\": 3")).IsTrue(); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + [NotInParallel("Console")] |
| 31 | + public async Task Search_RejectsMultipleSources() |
| 32 | + { |
| 33 | + var source = new FakeSource("arxiv"); |
| 34 | + var command = CreateCommand(new AppConfig { DefaultSource = "arxiv" }, source); |
| 35 | + |
| 36 | + var (error, _, exitCode) = await CaptureConsoleAsync(() => |
| 37 | + command.Search("attention", source: "arxiv,jstage", json: true)); |
| 38 | + |
| 39 | + await Assert.That(exitCode).IsEqualTo(1); |
| 40 | + await Assert.That(source.Calls).IsEqualTo(0); |
| 41 | + await Assert.That(error.Contains("single source")).IsTrue(); |
| 42 | + } |
| 43 | + |
| 44 | + [Test] |
| 45 | + [NotInParallel("Console")] |
| 46 | + public async Task Search_RejectsUnknownSource() |
| 47 | + { |
| 48 | + var source = new FakeSource("arxiv"); |
| 49 | + var command = CreateCommand(new AppConfig { DefaultSource = "arxiv" }, source); |
| 50 | + |
| 51 | + var (error, _, exitCode) = await CaptureConsoleAsync(() => |
| 52 | + command.Search("attention", source: "jstage", json: true)); |
| 53 | + |
| 54 | + await Assert.That(exitCode).IsEqualTo(1); |
| 55 | + await Assert.That(source.Calls).IsEqualTo(0); |
| 56 | + await Assert.That(error.Contains("Unknown source: jstage")).IsTrue(); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + [NotInParallel("Console")] |
| 61 | + public async Task Search_RejectsUnsupportedSort() |
| 62 | + { |
| 63 | + var source = new FakeSource("arxiv"); |
| 64 | + var command = CreateCommand(new AppConfig { DefaultSource = "arxiv" }, source); |
| 65 | + |
| 66 | + var (error, _, exitCode) = await CaptureConsoleAsync(() => |
| 67 | + command.Search("attention", sort: "author", json: true)); |
| 68 | + |
| 69 | + await Assert.That(exitCode).IsEqualTo(1); |
| 70 | + await Assert.That(source.Calls).IsEqualTo(0); |
| 71 | + await Assert.That(error.Contains("Sort 'author' is not supported by arxiv.")).IsTrue(); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + [NotInParallel("Console")] |
| 76 | + public async Task Search_RejectsInvalidLimit() |
| 77 | + { |
| 78 | + var source = new FakeSource("arxiv"); |
| 79 | + var command = CreateCommand(new AppConfig { DefaultSource = "arxiv" }, source); |
| 80 | + |
| 81 | + var (error, _, exitCode) = await CaptureConsoleAsync(() => |
| 82 | + command.Search("attention", limit: 0, json: true)); |
| 83 | + |
| 84 | + await Assert.That(exitCode).IsEqualTo(1); |
| 85 | + await Assert.That(source.Calls).IsEqualTo(0); |
| 86 | + await Assert.That(error.Contains("--limit must be greater than 0.")).IsTrue(); |
| 87 | + } |
| 88 | + |
| 89 | + [Test] |
| 90 | + [NotInParallel("Console")] |
| 91 | + public async Task Search_RejectsInvalidPage() |
| 92 | + { |
| 93 | + var source = new FakeSource("arxiv"); |
| 94 | + var command = CreateCommand(new AppConfig { DefaultSource = "arxiv" }, source); |
| 95 | + |
| 96 | + var (error, _, exitCode) = await CaptureConsoleAsync(() => |
| 97 | + command.Search("attention", page: 0, json: true)); |
| 98 | + |
| 99 | + await Assert.That(exitCode).IsEqualTo(1); |
| 100 | + await Assert.That(source.Calls).IsEqualTo(0); |
| 101 | + await Assert.That(error.Contains("--page must be greater than 0.")).IsTrue(); |
| 102 | + } |
| 103 | + |
| 104 | + private static PaperCommands CreateCommand(AppConfig config, params IPaperSource[] sources) |
| 105 | + { |
| 106 | + var dbPath = Path.Combine(Path.GetTempPath(), $"papers-command-test-{Guid.NewGuid()}.db"); |
| 107 | + var repository = new PaperRepository($"Data Source={dbPath}"); |
| 108 | + return new PaperCommands(repository, sources, config, new HttpClient()); |
| 109 | + } |
| 110 | + |
| 111 | + private static async Task<(string Error, string Output, int ExitCode)> CaptureConsoleAsync(Func<Task> action) |
| 112 | + { |
| 113 | + Environment.ExitCode = 0; |
| 114 | + var previousError = Console.Error; |
| 115 | + var previousOutput = Console.Out; |
| 116 | + using var error = new StringWriter(); |
| 117 | + using var output = new StringWriter(); |
| 118 | +#pragma warning disable TUnit0055 |
| 119 | + Console.SetError(error); |
| 120 | + Console.SetOut(output); |
| 121 | +#pragma warning restore TUnit0055 |
| 122 | + try |
| 123 | + { |
| 124 | + await action(); |
| 125 | + return (error.ToString(), output.ToString(), Environment.ExitCode); |
| 126 | + } |
| 127 | + finally |
| 128 | + { |
| 129 | +#pragma warning disable TUnit0055 |
| 130 | + Console.SetError(previousError); |
| 131 | + Console.SetOut(previousOutput); |
| 132 | +#pragma warning restore TUnit0055 |
| 133 | + Environment.ExitCode = 0; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private sealed class FakeSource(string name) : IPaperSource |
| 138 | + { |
| 139 | + public string Name => name; |
| 140 | + public IReadOnlyList<string> SupportedFormats => ["pdf"]; |
| 141 | + public int Calls { get; private set; } |
| 142 | + public string? LastQuery { get; private set; } |
| 143 | + public int? LastLimit { get; private set; } |
| 144 | + public int? LastPage { get; private set; } |
| 145 | + |
| 146 | + public Task<SearchResultsPage> SearchAsync( |
| 147 | + string query, |
| 148 | + string? author = null, |
| 149 | + int? fromYear = null, |
| 150 | + int? toYear = null, |
| 151 | + string? category = null, |
| 152 | + string sort = "relevance", |
| 153 | + int limit = 20, |
| 154 | + int page = 1, |
| 155 | + CancellationToken cancellationToken = default) |
| 156 | + { |
| 157 | + Calls++; |
| 158 | + LastQuery = query; |
| 159 | + LastLimit = limit; |
| 160 | + LastPage = page; |
| 161 | + |
| 162 | + return Task.FromResult(new SearchResultsPage |
| 163 | + { |
| 164 | + Source = name, |
| 165 | + Query = query, |
| 166 | + Page = page, |
| 167 | + Limit = limit, |
| 168 | + TotalResults = 30, |
| 169 | + Results = |
| 170 | + [ |
| 171 | + new SearchResult |
| 172 | + { |
| 173 | + Source = name, |
| 174 | + SourceId = "id-1", |
| 175 | + Title = "Paper", |
| 176 | + Authors = "[]", |
| 177 | + Url = "https://example.com/paper", |
| 178 | + }, |
| 179 | + ], |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + public Task<SearchResult?> GetMetadataAsync(string sourceId, CancellationToken cancellationToken = default) |
| 184 | + => throw new NotImplementedException(); |
| 185 | + |
| 186 | + public Task<Dictionary<string, string>> GetDownloadUrlsAsync(string sourceId, CancellationToken cancellationToken = default) |
| 187 | + => throw new NotImplementedException(); |
| 188 | + |
| 189 | + public string? ParseUrl(string url) => null; |
| 190 | + } |
| 191 | +} |
0 commit comments