|
| 1 | +// ----------------------------------------------------------------------- |
| 2 | +// <copyright file="SearchConfigEditorPageTests.cs" company="Petabridge, LLC"> |
| 3 | +// Copyright (C) 2026 - 2026 Petabridge, LLC <https://petabridge.com> |
| 4 | +// </copyright> |
| 5 | +// ----------------------------------------------------------------------- |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Netclaw.Cli.Tui.Config; |
| 8 | +using Netclaw.Configuration; |
| 9 | +using Netclaw.Tests.Utilities; |
| 10 | +using Termina; |
| 11 | +using Termina.Hosting; |
| 12 | +using Termina.Input; |
| 13 | +using Termina.Terminal; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace Netclaw.Cli.Tests.Tui.Config; |
| 17 | + |
| 18 | +public sealed class SearchConfigEditorPageTests : IDisposable |
| 19 | +{ |
| 20 | + private readonly DisposableTempDir _dir = new(); |
| 21 | + private readonly NetclawPaths _paths; |
| 22 | + |
| 23 | + public SearchConfigEditorPageTests() |
| 24 | + { |
| 25 | + _paths = new NetclawPaths(_dir.Path); |
| 26 | + _paths.EnsureDirectoriesExist(); |
| 27 | + File.WriteAllText(_paths.NetclawConfigPath, """ |
| 28 | + { |
| 29 | + "configVersion": 1, |
| 30 | + "Search": { |
| 31 | + "Backend": "duckduckgo" |
| 32 | + } |
| 33 | + } |
| 34 | + """); |
| 35 | + } |
| 36 | + |
| 37 | + public void Dispose() => _dir.Dispose(); |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task ProviderSelection_RendersActiveAndConfiguredLegend() |
| 41 | + { |
| 42 | + var (terminal, app, _) = CreateHeadlessApp(out var input); |
| 43 | + input.EnqueueKey(ConsoleKey.Q, false, false, true); |
| 44 | + |
| 45 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 46 | + await app.RunAsync(cts.Token); |
| 47 | + |
| 48 | + Assert.True(terminal.Contains("(*) active backend"), |
| 49 | + $"Expected active-backend legend in terminal output. Screen:\n{terminal}"); |
| 50 | + Assert.True(terminal.Contains("backend has saved setup"), |
| 51 | + $"Expected configured-backend legend in terminal output. Screen:\n{terminal}"); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task SavedScreen_EscapeReturnsToProviderSelection() |
| 56 | + { |
| 57 | + var (terminal, app, vm) = CreateHeadlessApp(out var input); |
| 58 | + |
| 59 | + vm.SaveWithoutProbeOverride(); |
| 60 | + |
| 61 | + input.EnqueueKey(ConsoleKey.Escape); |
| 62 | + input.EnqueueKey(ConsoleKey.Q, false, false, true); |
| 63 | + |
| 64 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); |
| 65 | + await app.RunAsync(cts.Token); |
| 66 | + |
| 67 | + Assert.Equal(SearchConfigEditorScreen.ProviderSelection, vm.CurrentScreen.Value); |
| 68 | + Assert.True(terminal.Contains("Choose the backend Netclaw uses for web search."), |
| 69 | + $"Expected provider selection screen after Esc from saved state. Screen:\n{terminal}"); |
| 70 | + } |
| 71 | + |
| 72 | + private (VirtualTerminal Terminal, TerminaApplication App, SearchConfigEditorViewModel Vm) |
| 73 | + CreateHeadlessApp(out VirtualInputSource input) |
| 74 | + { |
| 75 | + var terminal = new VirtualTerminal(120, 40); |
| 76 | + var virtualInput = new VirtualInputSource(); |
| 77 | + input = virtualInput; |
| 78 | + |
| 79 | + SearchConfigEditorViewModel? capturedVm = null; |
| 80 | + |
| 81 | + var services = new ServiceCollection(); |
| 82 | + services.AddSingleton<IAnsiTerminal>(terminal); |
| 83 | + services.AddTerminaVirtualInput(virtualInput); |
| 84 | + services.AddTermina("/search", builder => |
| 85 | + { |
| 86 | + builder.RegisterRoute<SearchConfigEditorPage, SearchConfigEditorViewModel>( |
| 87 | + "/search", |
| 88 | + _ => new SearchConfigEditorPage(), |
| 89 | + _ => |
| 90 | + { |
| 91 | + capturedVm = new SearchConfigEditorViewModel(_paths); |
| 92 | + return capturedVm; |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + var sp = services.BuildServiceProvider(); |
| 97 | + var app = sp.GetRequiredService<TerminaApplication>(); |
| 98 | + |
| 99 | + return (terminal, app, capturedVm!); |
| 100 | + } |
| 101 | +} |
0 commit comments