Files
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

58 lines
1.6 KiB
C#

using CommandForge.Application.History;
using CommandForge.Domain;
namespace CommandForge.Tests;
/// <summary>Tests the pure execution-history helper (newest-first, cap, no de-dupe, no mutation).</summary>
public sealed class ExecutionHistoryTests
{
private static ExecutionRecord Rec(string id) => new()
{
CommandId = id,
Timestamp = DateTimeOffset.UnixEpoch,
ExitCode = 0,
Success = true,
DurationMs = 10,
};
[Fact]
public void Add_PutsNewestFirst()
{
var result = ExecutionHistory.Add([Rec("a")], Rec("b"));
Assert.Equal(["b", "a"], result.Select(r => r.CommandId));
}
[Fact]
public void Add_KeepsDuplicateCommandIds()
{
// Unlike recent commands, every run is a distinct event — no de-duplication.
var result = ExecutionHistory.Add([Rec("a")], Rec("a"));
Assert.Equal(2, result.Count);
}
[Fact]
public void Add_CapsAtMaxItems()
{
IReadOnlyList<ExecutionRecord> current =
Enumerable.Range(0, ExecutionHistory.MaxItems).Select(i => Rec($"c{i}")).ToList();
var result = ExecutionHistory.Add(current, Rec("newest"));
Assert.Equal(ExecutionHistory.MaxItems, result.Count);
Assert.Equal("newest", result[0].CommandId);
Assert.DoesNotContain($"c{ExecutionHistory.MaxItems - 1}", result.Select(r => r.CommandId)); // oldest dropped
}
[Fact]
public void Add_DoesNotMutateInput()
{
var current = new List<ExecutionRecord> { Rec("a") };
ExecutionHistory.Add(current, Rec("b"));
Assert.Single(current);
}
}