Merge claudedo/1b599d6711914658b9857b3df996f6e0

This commit is contained in:
mika kuns
2026-08-05 16:03:14 +02:00
11 changed files with 483 additions and 27 deletions
@@ -0,0 +1,151 @@
using System.Text.Json;
using ClaudeDo.Data;
namespace ClaudeDo.Data.Tests;
public class PromptFilesClassifyTests : IDisposable
{
private readonly string _root = Path.Combine(Path.GetTempPath(), "claudedo-prompt-tests-" + Guid.NewGuid());
public void Dispose()
{
if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true);
}
[Fact]
public void Classify_missing_file_returns_Missing()
{
Assert.Equal(PromptFileState.Missing, PromptFiles.Classify(PromptKind.System, _root));
}
[Fact]
public void Classify_file_matching_current_default_returns_MatchesCurrentDefault()
{
Directory.CreateDirectory(_root);
File.WriteAllText(PromptFiles.PathFor(PromptKind.Retry, _root), PromptFiles.DefaultFor(PromptKind.Retry));
Assert.Equal(PromptFileState.MatchesCurrentDefault, PromptFiles.Classify(PromptKind.Retry, _root));
}
[Fact]
public void Classify_file_matching_a_recorded_past_default_returns_MatchesKnownPastDefault()
{
Directory.CreateDirectory(_root);
const string oldDefaultText = "This was the bundled default a while ago.";
File.WriteAllText(PromptFiles.PathFor(PromptKind.Retry, _root), oldDefaultText);
// Simulate what Save() would have written back when oldDefaultText WAS the current default.
var hashes = new Dictionary<string, string>
{
[PromptKind.Retry.ToString()] = PromptFiles.HashOf(PromptFiles.Normalize(oldDefaultText))
};
File.WriteAllText(Path.Combine(_root, ".defaults.json"), JsonSerializer.Serialize(hashes));
Assert.Equal(PromptFileState.MatchesKnownPastDefault, PromptFiles.Classify(PromptKind.Retry, _root));
}
[Fact]
public void Classify_file_that_diverges_with_no_recorded_hash_returns_Edited()
{
Directory.CreateDirectory(_root);
File.WriteAllText(PromptFiles.PathFor(PromptKind.Retry, _root), "My own custom retry instructions.");
Assert.Equal(PromptFileState.Edited, PromptFiles.Classify(PromptKind.Retry, _root));
}
[Fact]
public void Save_with_content_equal_to_default_records_hash_so_it_classifies_as_current_default()
{
PromptFiles.Save(PromptKind.Retry, PromptFiles.DefaultFor(PromptKind.Retry), _root);
Assert.Equal(PromptFileState.MatchesCurrentDefault, PromptFiles.Classify(PromptKind.Retry, _root));
Assert.True(File.Exists(Path.Combine(_root, ".defaults.json")));
}
[Fact]
public void Save_with_edited_content_does_not_record_a_hash()
{
PromptFiles.Save(PromptKind.Retry, "Custom retry text.", _root);
Assert.Equal(PromptFileState.Edited, PromptFiles.Classify(PromptKind.Retry, _root));
}
[Fact]
public void ResetToDefault_deletes_the_override_file_and_its_hash_entry()
{
PromptFiles.Save(PromptKind.Retry, PromptFiles.DefaultFor(PromptKind.Retry), _root);
Assert.True(File.Exists(PromptFiles.PathFor(PromptKind.Retry, _root)));
PromptFiles.ResetToDefault(PromptKind.Retry, _root);
Assert.False(File.Exists(PromptFiles.PathFor(PromptKind.Retry, _root)));
Assert.Equal(PromptFileState.Missing, PromptFiles.Classify(PromptKind.Retry, _root));
}
[Fact]
public void ReconcileStaleDefaults_removes_a_file_that_only_matched_a_past_default()
{
Directory.CreateDirectory(_root);
const string oldDefaultText = "Old bundled default text.";
File.WriteAllText(PromptFiles.PathFor(PromptKind.Retry, _root), oldDefaultText);
var hashes = new Dictionary<string, string>
{
[PromptKind.Retry.ToString()] = PromptFiles.HashOf(PromptFiles.Normalize(oldDefaultText))
};
File.WriteAllText(Path.Combine(_root, ".defaults.json"), JsonSerializer.Serialize(hashes));
PromptFiles.ReconcileStaleDefaults(_root);
Assert.False(File.Exists(PromptFiles.PathFor(PromptKind.Retry, _root)));
}
[Fact]
public void ReconcileStaleDefaults_leaves_a_real_edit_untouched()
{
PromptFiles.Save(PromptKind.Retry, "Genuinely customized retry text.", _root);
PromptFiles.ReconcileStaleDefaults(_root);
Assert.True(File.Exists(PromptFiles.PathFor(PromptKind.Retry, _root)));
Assert.Equal(PromptFileState.Edited, PromptFiles.Classify(PromptKind.Retry, _root));
}
[Fact]
public void QuarantineOrphans_moves_unknown_md_files_into_orphans_subfolder()
{
Directory.CreateDirectory(_root);
var orphanPath = Path.Combine(_root, "agent.md");
File.WriteAllText(orphanPath, "leftover from an old naming scheme");
var moved = PromptFiles.QuarantineOrphans(_root);
Assert.False(File.Exists(orphanPath));
var dest = Assert.Single(moved);
Assert.True(File.Exists(dest));
Assert.Equal("leftover from an old naming scheme", File.ReadAllText(dest));
}
[Fact]
public void QuarantineOrphans_leaves_known_prompt_files_in_place()
{
PromptFiles.Save(PromptKind.Retry, "Custom retry text.", _root);
var moved = PromptFiles.QuarantineOrphans(_root);
Assert.Empty(moved);
Assert.True(File.Exists(PromptFiles.PathFor(PromptKind.Retry, _root)));
}
[Fact]
public void DiffAgainstDefault_shows_only_the_changed_lines()
{
var lines = PromptFiles.DefaultFor(PromptKind.PlanningInitial).Replace("\r\n", "\n").Split('\n');
var edited = string.Join('\n', lines) + "\nExtra custom line.";
PromptFiles.Save(PromptKind.PlanningInitial, edited, _root);
var diff = PromptFiles.DiffAgainstDefault(PromptKind.PlanningInitial, _root);
Assert.Contains("+ Extra custom line.", diff);
Assert.DoesNotContain("- {title}", diff);
}
}
@@ -0,0 +1,49 @@
using System.Text.Json;
using ClaudeDo.Data;
using ClaudeDo.Worker.Lifecycle;
using Microsoft.Extensions.Logging.Abstractions;
namespace ClaudeDo.Worker.Tests.Lifecycle;
public sealed class PromptFileRecoveryTests
{
[Fact]
public async Task StartAsync_ReconcilesStaleDefaultAndQuarantinesOrphan_WithoutTouchingRealEdit()
{
var root = Path.Combine(Path.GetTempPath(), $"claudedo_prompts_{Guid.NewGuid():N}");
try
{
// A file that only ever matched a now-superseded default should get reconciled away.
Directory.CreateDirectory(root);
const string oldDefaultText = "This used to be the bundled retry default.";
File.WriteAllText(PromptFiles.PathFor(PromptKind.Retry, root), oldDefaultText);
var hashes = new Dictionary<string, string>
{
[PromptKind.Retry.ToString()] = PromptFiles.HashOf(PromptFiles.Normalize(oldDefaultText))
};
File.WriteAllText(Path.Combine(root, ".defaults.json"), JsonSerializer.Serialize(hashes));
// A genuinely edited file should survive untouched.
PromptFiles.Save(PromptKind.System, "My real customization.", root);
// A leftover file from a retired naming scheme should be quarantined, not deleted.
var orphanPath = Path.Combine(root, "agent.md");
File.WriteAllText(orphanPath, "leftover");
var sut = new PromptFileRecovery(NullLogger<PromptFileRecovery>.Instance, root);
await sut.StartAsync(CancellationToken.None);
Assert.False(File.Exists(PromptFiles.PathFor(PromptKind.Retry, root)), "Stale unedited default must be reconciled away");
Assert.True(File.Exists(PromptFiles.PathFor(PromptKind.System, root)), "Real edit must survive");
Assert.False(File.Exists(orphanPath), "Orphan must be moved out of the prompts root");
Assert.True(File.Exists(Path.Combine(root, "_orphans", "agent.md")), "Orphan must be quarantined, not deleted");
await sut.StopAsync(CancellationToken.None); // must not throw
}
finally
{
if (Directory.Exists(root)) Directory.Delete(root, recursive: true);
}
}
}