using Avalonia.Controls;
using Avalonia.Headless.XUnit;
using Avalonia.VisualTree;
using Microsoft.Extensions.DependencyInjection;
using PostIt.Services;
using PostIt.ViewModels;
using PostIt.Views;
using Yavsc.Api.Client;
using Yavsc.Blogspot;
namespace PostIt.Tests;
///
/// Headless UI tests for the "Save" flow in .
/// Uses the shared (a real
/// with the production DI graph attached
/// to ) plus a local
/// that swaps
/// for the recording fake.
///
/// The bug we are pinning: the title TextBox is
/// currently {Binding SelectedPost.Title, Mode=TwoWay}.
/// When SelectedPost is null (i.e. the user has not yet
/// clicked an item in the posts list — which is the only state
/// in which a brand-new post can be created), the binding has
/// no target and the user's keystrokes are silently dropped.
/// Clicking "Save" then routes to the VM branch
/// if (SelectedPost is null) { new BlogPostDto { Title = string.Empty, ... } }
/// which the controller rejects with 400 "The Title field is
/// required." This test fails on that branch today and will
/// pass once the VM owns a dedicated Title/Article
/// buffer that the XAML binds to and the Save command consumes.
///
[Collection("PostIt Headless")]
public sealed class MainPageSaveTests
{
private readonly PostItHeadlessCollection _host;
public MainPageSaveTests(PostItHeadlessCollection host)
{
_host = host;
}
[AvaloniaFact]
public void Typing_a_title_then_clicking_Save_sends_that_title_in_the_post_body()
{
// Arrange: VM with a recording API client, mounted on
// the shared MainWindow's nav stack.
var recorder = new CallRecorder();
var blog = _host.Services.GetRequiredService();
var viewModel = new MainPageViewModel(blog);
var page = new MainPage { DataContext = viewModel };
_host.PushAsync(page);
// Act: type a title into the editor's TextBox without
// first selecting a post in the list — the only state
// in which a new post can be created. Then click Save.
var titleBox = _host.Window.GetVisualDescendants()
.OfType()
.First(t => t.PlaceholderText == "Title");
const string typed = "Mon premier billet";
titleBox.Text = typed;
var saveButton = _host.Window.GetVisualDescendants()
.OfType