Files
ClaudeDo/tests/ClaudeDo.Worker.Tests/Repositories/ListRepositoryTests.cs
Mika Kuns 9f51ff0b17 feat(data,worker): add repositories, stale-task recovery, and test foundation
Data: TagRepository, ListRepository, TaskRepository (incl. queue selection via
effective agent tag + scheduled_for filter), WorktreeRepository. All CRUD via
parameterized SqliteCommand; enums roundtrip as lowercase strings matching the
schema CHECK constraints.

Worker: StaleTaskRecovery IHostedService flips running -> failed on startup and
marks the result column with a [stale] reason. All four repositories registered
as singletons.

Tests: DbFixture with temp-file SQLite + schema bootstrap, covering
TaskRepository (queue pick via list-tag and task-tag, schedule filter,
transitions, stale flip), ListRepository CRUD + junctions, and
StaleTaskRecovery. 14 tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 12:08:06 +02:00

108 lines
3.2 KiB
C#

using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Tests.Infrastructure;
namespace ClaudeDo.Worker.Tests.Repositories;
public sealed class ListRepositoryTests : IDisposable
{
private readonly DbFixture _db = new();
private readonly ListRepository _lists;
private readonly TagRepository _tags;
public ListRepositoryTests()
{
_lists = new ListRepository(_db.Factory);
_tags = new TagRepository(_db.Factory);
}
public void Dispose() => _db.Dispose();
[Fact]
public async Task AddAsync_And_GetByIdAsync_Roundtrips()
{
var entity = new ListEntity
{
Id = Guid.NewGuid().ToString(),
Name = "Shopping",
CreatedAt = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc),
WorkingDir = @"C:\Repos\Test",
DefaultCommitType = "feat",
};
await _lists.AddAsync(entity);
var loaded = await _lists.GetByIdAsync(entity.Id);
Assert.NotNull(loaded);
Assert.Equal(entity.Id, loaded.Id);
Assert.Equal(entity.Name, loaded.Name);
Assert.Equal(entity.WorkingDir, loaded.WorkingDir);
Assert.Equal(entity.DefaultCommitType, loaded.DefaultCommitType);
}
[Fact]
public async Task UpdateAsync_Changes_Fields()
{
var entity = new ListEntity
{
Id = Guid.NewGuid().ToString(),
Name = "Original",
CreatedAt = DateTime.UtcNow,
};
await _lists.AddAsync(entity);
entity.Name = "Updated";
entity.WorkingDir = @"C:\New";
await _lists.UpdateAsync(entity);
var loaded = await _lists.GetByIdAsync(entity.Id);
Assert.Equal("Updated", loaded!.Name);
Assert.Equal(@"C:\New", loaded.WorkingDir);
}
[Fact]
public async Task DeleteAsync_Removes_List()
{
var entity = new ListEntity
{
Id = Guid.NewGuid().ToString(),
Name = "ToDelete",
CreatedAt = DateTime.UtcNow,
};
await _lists.AddAsync(entity);
await _lists.DeleteAsync(entity.Id);
var loaded = await _lists.GetByIdAsync(entity.Id);
Assert.Null(loaded);
}
[Fact]
public async Task GetAllAsync_Returns_All_Lists()
{
var a = new ListEntity { Id = Guid.NewGuid().ToString(), Name = "A", CreatedAt = DateTime.UtcNow.AddMinutes(-1) };
var b = new ListEntity { Id = Guid.NewGuid().ToString(), Name = "B", CreatedAt = DateTime.UtcNow };
await _lists.AddAsync(a);
await _lists.AddAsync(b);
var all = await _lists.GetAllAsync();
Assert.True(all.Count >= 2);
}
[Fact]
public async Task TagJunction_AddAndRemove()
{
var listId = Guid.NewGuid().ToString();
await _lists.AddAsync(new ListEntity { Id = listId, Name = "Tagged", CreatedAt = DateTime.UtcNow });
var tagId = await _tags.GetOrCreateAsync("agent");
await _lists.AddTagAsync(listId, tagId);
var tags = await _lists.GetTagsAsync(listId);
Assert.Single(tags);
Assert.Equal("agent", tags[0].Name);
await _lists.RemoveTagAsync(listId, tagId);
tags = await _lists.GetTagsAsync(listId);
Assert.Empty(tags);
}
}