112 lines
3.5 KiB
C#
112 lines
3.5 KiB
C#
using ClaudeDo.Data;
|
|
using ClaudeDo.Ui.Services;
|
|
using ClaudeDo.Ui.ViewModels.Islands;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ClaudeDo.Ui.Tests.ViewModels;
|
|
|
|
public class DetailsIslandPrepModeTests : IDisposable
|
|
{
|
|
private readonly string _dbPath;
|
|
|
|
public DetailsIslandPrepModeTests()
|
|
{
|
|
_dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_prep_test_{Guid.NewGuid():N}.db");
|
|
using var ctx = NewContext();
|
|
ctx.Database.EnsureCreated();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
try { File.Delete(_dbPath); } catch { }
|
|
try { File.Delete(_dbPath + "-wal"); } catch { }
|
|
try { File.Delete(_dbPath + "-shm"); } catch { }
|
|
}
|
|
|
|
private ClaudeDoDbContext NewContext()
|
|
{
|
|
var opts = new DbContextOptionsBuilder<ClaudeDoDbContext>()
|
|
.UseSqlite($"Data Source={_dbPath}")
|
|
.Options;
|
|
return new ClaudeDoDbContext(opts);
|
|
}
|
|
|
|
private sealed class TestDbFactory : IDbContextFactory<ClaudeDoDbContext>
|
|
{
|
|
private readonly Func<ClaudeDoDbContext> _create;
|
|
public TestDbFactory(Func<ClaudeDoDbContext> create) => _create = create;
|
|
public ClaudeDoDbContext CreateDbContext() => _create();
|
|
}
|
|
|
|
private sealed class DefaultStub : StubWorkerClient { }
|
|
|
|
private sealed class StubNotesApi : ClaudeDo.Ui.Services.Interfaces.INotesApi
|
|
{
|
|
public Task<List<DailyNoteDto>> ListAsync(DateOnly day) => Task.FromResult(new List<DailyNoteDto>());
|
|
public Task<DailyNoteDto?> AddAsync(DateOnly day, string text) => Task.FromResult<DailyNoteDto?>(null);
|
|
public Task UpdateAsync(string id, string text) => Task.CompletedTask;
|
|
public Task DeleteAsync(string id) => Task.CompletedTask;
|
|
}
|
|
|
|
private DetailsIslandViewModel NewDetailsVm(StubWorkerClient stub)
|
|
{
|
|
var factory = new TestDbFactory(NewContext);
|
|
return new DetailsIslandViewModel(factory, stub, new NullServiceProvider(), new StubNotesApi());
|
|
}
|
|
|
|
private sealed class NullServiceProvider : IServiceProvider
|
|
{
|
|
public object? GetService(Type serviceType) => null;
|
|
}
|
|
|
|
[Fact]
|
|
public void PrepLine_event_appends_to_PrepLog()
|
|
{
|
|
var stub = new DefaultStub();
|
|
var vm = NewDetailsVm(stub);
|
|
|
|
stub.RaisePrepLine("{\"type\":\"assistant\",\"message\":{\"content\":[{\"type\":\"text\",\"text\":\"hi\"}]}}");
|
|
|
|
Assert.NotEmpty(vm.PrepLog);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShowPrep_sets_prep_mode_and_clears_notes_mode()
|
|
{
|
|
var vm = NewDetailsVm(new DefaultStub());
|
|
vm.ShowPrep();
|
|
|
|
Assert.True(vm.IsPrepMode);
|
|
Assert.False(vm.IsNotesMode);
|
|
Assert.False(vm.IsTaskDetailVisible);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ShowPrep_loads_persisted_log_when_empty()
|
|
{
|
|
var stub = new DefaultStub { LastPrepLog = "{\"type\":\"assistant\",\"message\":{\"content\":[{\"type\":\"text\",\"text\":\"restored\"}]}}" };
|
|
var vm = NewDetailsVm(stub);
|
|
|
|
vm.ShowPrep();
|
|
await vm.LoadLastPrepLogIfEmptyAsync();
|
|
|
|
Assert.NotEmpty(vm.PrepLog);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PlanDayCommand_calls_worker()
|
|
{
|
|
var stub = new DefaultStub();
|
|
var vm = NewDetailsVm(stub);
|
|
await vm.PlanDayCommand.ExecuteAsync(null);
|
|
Assert.Equal(1, stub.RunDailyPrepNowCalls);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShowPrepEmptyState_true_when_empty_and_not_running()
|
|
{
|
|
var vm = NewDetailsVm(new DefaultStub());
|
|
Assert.True(vm.ShowPrepEmptyState);
|
|
}
|
|
}
|