Files
CommandForge/tests/CommandForge.Tests/ExecutionHistoryRoundTripTests.cs
poli0981andClaude Opus 4.8 ad91973a3f feat(history): persistent execution history + re-run (v-next Đợt 2)
Adds a local execution history (C1 from docs/Extra.md Part 2).

- Domain ExecutionRecord; Application IExecutionHistoryService port +
  pure ExecutionHistory helper (newest-first, cap 200, no de-dupe).
- Infrastructure JsonExecutionHistoryService -> %AppData%\CommandForge\
  history.json (source-gen context, schema version, corrupt-file
  fallback). No telemetry.
- ExecutionViewModel exposes LastResult; MainViewModel records each
  finished, non-cancelled run via IClock + the history service.
- New History shell section (sidebar button + View menu) with
  HistoryViewModel/HistoryView: per-run time/status/duration, "Run
  again" (re-runs through the normal confirm/elevation flow) and
  "Clear history". Runs of commands removed from the catalog show
  their id and cannot be re-run.
- EN/VI .resx keys; tests for the cap helper and JSON round-trip /
  fallbacks.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 14:12:55 +07:00

83 lines
2.4 KiB
C#

using CommandForge.Domain;
using CommandForge.Infrastructure.History;
namespace CommandForge.Tests;
/// <summary>Verifies execution history persists to / loads from history.json, with graceful fallbacks.</summary>
public sealed class ExecutionHistoryRoundTripTests
{
private static ExecutionRecord Rec(string id, bool success = true) => new()
{
CommandId = id,
Timestamp = new DateTimeOffset(2026, 6, 19, 10, 30, 0, TimeSpan.Zero),
ExitCode = success ? 0 : 1,
Success = success,
DurationMs = 1234,
RequiresRestart = false,
};
[Fact]
public void Record_Persists_And_Reloads_NewestFirst()
{
var path = Path.Combine(Path.GetTempPath(), $"cf-history-{Guid.NewGuid():N}.json");
try
{
var writer = new JsonExecutionHistoryService(path);
writer.Record(Rec("a"));
writer.Record(Rec("b", success: false));
var records = new JsonExecutionHistoryService(path).GetRecent();
Assert.Equal(["b", "a"], records.Select(r => r.CommandId));
Assert.False(records[0].Success);
Assert.Equal(1, records[0].ExitCode);
Assert.Equal(1234, records[0].DurationMs);
}
finally
{
File.Delete(path);
}
}
[Fact]
public void Clear_EmptiesHistory()
{
var path = Path.Combine(Path.GetTempPath(), $"cf-history-{Guid.NewGuid():N}.json");
try
{
var svc = new JsonExecutionHistoryService(path);
svc.Record(Rec("a"));
svc.Clear();
Assert.Empty(svc.GetRecent());
Assert.Empty(new JsonExecutionHistoryService(path).GetRecent());
}
finally
{
File.Delete(path);
}
}
[Fact]
public void CorruptFile_FallsBackToEmpty()
{
var path = Path.Combine(Path.GetTempPath(), $"cf-history-{Guid.NewGuid():N}.json");
try
{
File.WriteAllText(path, "{ not valid json ");
Assert.Empty(new JsonExecutionHistoryService(path).GetRecent());
}
finally
{
File.Delete(path);
}
}
[Fact]
public void MissingFile_IsEmpty()
=> Assert.Empty(new JsonExecutionHistoryService(
Path.Combine(Path.GetTempPath(), $"cf-history-{Guid.NewGuid():N}.json")).GetRecent());
}