feat(data): session skills entity, repository, and migration
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Models;
|
||||
using ClaudeDo.Data.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ClaudeDo.Data.Tests;
|
||||
|
||||
public sealed class SessionSkillRepositoryTests : IDisposable
|
||||
{
|
||||
private readonly string _dbPath;
|
||||
private readonly ClaudeDoDbContext _ctx;
|
||||
private readonly SessionSkillRepository _repo;
|
||||
|
||||
public SessionSkillRepositoryTests()
|
||||
{
|
||||
_dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_skills_{Guid.NewGuid():N}.db");
|
||||
var options = new DbContextOptionsBuilder<ClaudeDoDbContext>()
|
||||
.UseSqlite($"Data Source={_dbPath}")
|
||||
.Options;
|
||||
_ctx = new ClaudeDoDbContext(options);
|
||||
_ctx.Database.EnsureCreated();
|
||||
_repo = new SessionSkillRepository(_ctx);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_ctx.Dispose();
|
||||
foreach (var suffix in new[] { "", "-wal", "-shm" })
|
||||
try { File.Delete(_dbPath + suffix); } catch { }
|
||||
}
|
||||
|
||||
private static SessionSkillEntity MakeSkill(string name, string sourceUrl = "https://example.com/repo") => new()
|
||||
{
|
||||
Name = name,
|
||||
SourceUrl = sourceUrl,
|
||||
PinnedRef = "main",
|
||||
Subpath = "skills/" + name,
|
||||
Description = "desc for " + name,
|
||||
AddedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task UpsertAsync_then_GetAsync_roundtrips()
|
||||
{
|
||||
var skill = MakeSkill("brainstorming");
|
||||
|
||||
await _repo.UpsertAsync(skill);
|
||||
var found = await _repo.GetAsync("brainstorming");
|
||||
|
||||
Assert.NotNull(found);
|
||||
Assert.Equal(skill.SourceUrl, found!.SourceUrl);
|
||||
Assert.Equal(skill.PinnedRef, found.PinnedRef);
|
||||
Assert.Equal(skill.Subpath, found.Subpath);
|
||||
Assert.Equal(skill.Description, found.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpsertAsync_updates_existing_row()
|
||||
{
|
||||
await _repo.UpsertAsync(MakeSkill("brainstorming", "https://example.com/old"));
|
||||
await _repo.UpsertAsync(MakeSkill("brainstorming", "https://example.com/new"));
|
||||
|
||||
var found = await _repo.GetAsync("brainstorming");
|
||||
|
||||
Assert.NotNull(found);
|
||||
Assert.Equal("https://example.com/new", found!.SourceUrl);
|
||||
|
||||
var all = await _repo.ListAsync();
|
||||
Assert.Single(all);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAsync_returns_null_when_missing()
|
||||
{
|
||||
var found = await _repo.GetAsync("nope");
|
||||
Assert.Null(found);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListAsync_returns_all_ordered_by_name()
|
||||
{
|
||||
await _repo.UpsertAsync(MakeSkill("zeta"));
|
||||
await _repo.UpsertAsync(MakeSkill("alpha"));
|
||||
|
||||
var all = await _repo.ListAsync();
|
||||
|
||||
Assert.Equal(2, all.Count);
|
||||
Assert.Equal("alpha", all[0].Name);
|
||||
Assert.Equal("zeta", all[1].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteAsync_removes_only_matching_row()
|
||||
{
|
||||
await _repo.UpsertAsync(MakeSkill("keep"));
|
||||
await _repo.UpsertAsync(MakeSkill("remove"));
|
||||
|
||||
await _repo.DeleteAsync("remove");
|
||||
|
||||
var all = await _repo.ListAsync();
|
||||
Assert.Single(all);
|
||||
Assert.Equal("keep", all[0].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteBySourceAsync_removes_multiple_rows_same_source()
|
||||
{
|
||||
const string source = "https://example.com/shared-repo";
|
||||
await _repo.UpsertAsync(MakeSkill("skill-a", source));
|
||||
await _repo.UpsertAsync(MakeSkill("skill-b", source));
|
||||
await _repo.UpsertAsync(MakeSkill("skill-c", "https://example.com/other"));
|
||||
|
||||
await _repo.DeleteBySourceAsync(source);
|
||||
|
||||
var all = await _repo.ListAsync();
|
||||
Assert.Single(all);
|
||||
Assert.Equal("skill-c", all[0].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListBySourceAsync_returns_only_matching_rows()
|
||||
{
|
||||
const string source = "https://example.com/shared-repo";
|
||||
await _repo.UpsertAsync(MakeSkill("skill-a", source));
|
||||
await _repo.UpsertAsync(MakeSkill("skill-b", source));
|
||||
await _repo.UpsertAsync(MakeSkill("skill-c", "https://example.com/other"));
|
||||
|
||||
var result = await _repo.ListBySourceAsync(source);
|
||||
|
||||
Assert.Equal(2, result.Count);
|
||||
Assert.All(result, s => Assert.Equal(source, s.SourceUrl));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System.Text.Json;
|
||||
using ClaudeDo.Data;
|
||||
using ClaudeDo.Data.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
||||
|
||||
namespace ClaudeDo.Data.Tests;
|
||||
|
||||
public sealed class SessionSkillsColumnRoundtripTests : IDisposable
|
||||
{
|
||||
private readonly string _dbPath;
|
||||
private readonly ClaudeDoDbContext _ctx;
|
||||
|
||||
public SessionSkillsColumnRoundtripTests()
|
||||
{
|
||||
_dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_skillscol_{Guid.NewGuid():N}.db");
|
||||
var options = new DbContextOptionsBuilder<ClaudeDoDbContext>()
|
||||
.UseSqlite($"Data Source={_dbPath}")
|
||||
.Options;
|
||||
_ctx = new ClaudeDoDbContext(options);
|
||||
_ctx.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_ctx.Dispose();
|
||||
foreach (var suffix in new[] { "", "-wal", "-shm" })
|
||||
try { File.Delete(_dbPath + suffix); } catch { }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Task_SessionSkills_json_array_roundtrips()
|
||||
{
|
||||
var names = new List<string> { "a", "b" };
|
||||
var list = new ListEntity { Id = "l1", Name = "Test", CreatedAt = DateTime.UtcNow };
|
||||
var task = new TaskEntity
|
||||
{
|
||||
Id = "t1",
|
||||
ListId = "l1",
|
||||
Title = "T",
|
||||
Status = TaskStatus.Idle,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
SessionSkills = JsonSerializer.Serialize(names),
|
||||
};
|
||||
_ctx.Lists.Add(list);
|
||||
_ctx.Tasks.Add(task);
|
||||
await _ctx.SaveChangesAsync();
|
||||
|
||||
await using var freshCtx = new ClaudeDoDbContext(
|
||||
new DbContextOptionsBuilder<ClaudeDoDbContext>().UseSqlite($"Data Source={_dbPath}").Options);
|
||||
var reloaded = await freshCtx.Tasks.AsNoTracking().FirstAsync(t => t.Id == "t1");
|
||||
|
||||
var roundtripped = JsonSerializer.Deserialize<List<string>>(reloaded.SessionSkills!);
|
||||
Assert.Equal(names, roundtripped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListConfig_SessionSkills_json_array_roundtrips()
|
||||
{
|
||||
var names = new List<string> { "a", "b" };
|
||||
var list = new ListEntity { Id = "l1", Name = "Test", CreatedAt = DateTime.UtcNow };
|
||||
var config = new ListConfigEntity { ListId = "l1", SessionSkills = JsonSerializer.Serialize(names) };
|
||||
_ctx.Lists.Add(list);
|
||||
_ctx.ListConfigs.Add(config);
|
||||
await _ctx.SaveChangesAsync();
|
||||
|
||||
await using var freshCtx = new ClaudeDoDbContext(
|
||||
new DbContextOptionsBuilder<ClaudeDoDbContext>().UseSqlite($"Data Source={_dbPath}").Options);
|
||||
var reloaded = await freshCtx.ListConfigs.AsNoTracking().FirstAsync(c => c.ListId == "l1");
|
||||
|
||||
var roundtripped = JsonSerializer.Deserialize<List<string>>(reloaded.SessionSkills!);
|
||||
Assert.Equal(names, roundtripped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AppSettings_SessionSkills_json_array_roundtrips()
|
||||
{
|
||||
var names = new List<string> { "a", "b" };
|
||||
var settings = await _ctx.AppSettings.FirstAsync(s => s.Id == AppSettingsEntity.SingletonId);
|
||||
settings.SessionSkills = JsonSerializer.Serialize(names);
|
||||
await _ctx.SaveChangesAsync();
|
||||
|
||||
await using var freshCtx = new ClaudeDoDbContext(
|
||||
new DbContextOptionsBuilder<ClaudeDoDbContext>().UseSqlite($"Data Source={_dbPath}").Options);
|
||||
var reloaded = await freshCtx.AppSettings.AsNoTracking().FirstAsync(s => s.Id == AppSettingsEntity.SingletonId);
|
||||
|
||||
var roundtripped = JsonSerializer.Deserialize<List<string>>(reloaded.SessionSkills!);
|
||||
Assert.Equal(names, roundtripped);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user