Files
ClaudeDo/tests/ClaudeDo.Installer.Tests/Checks/ClaudeCliLookupTests.cs
T
mika kuns d743a9d0e9 feat(installer): add Claude CLI preflight checks (found, version, login, auto-mode)
Four IEnvironmentCheck implementations in src/ClaudeDo.Installer/Checks/:
- ClaudeCliCheck (Error) — resolves ctx.ClaudeBin via ExecutableResolver, runs
  --version; failure message lists searched PATH entries + fallback dirs, flags
  shim resolution (.cmd/.ps1) in Detail.
- ClaudeVersionCheck (Error) — tolerant version parsing (ignores surrounding
  text), numeric System.Version comparison against a named floor constant
  (2.1.220, see docs/explore-notes/installer-preflight.md §3). Unparseable or
  missing CLI -> Unknown, never Failed.
- ClaudeAuthCheck (Error) — `claude auth status --json`, never sends a prompt.
- PermissionModeAutoCheck (Warning) — per the note's §2 conclusion, real
  auto-mode eligibility (org/model/plan) has no cheap static signal, so this
  only confirms `claude --help` still lists "auto" as a --permission-mode
  choice. Kept as its own check rather than folded into ClaudeVersionCheck,
  since the note explicitly separates "flag recognized" from "mode eligible"
  and only the former is checkable at all.

All four share a new ClaudeCliLookup that resolves the CLI and runs
--version exactly once per install run (memoized, semaphore-guarded), so
none of the three version-dependent checks repeats that call.

Foundation prerequisites this task depends on (docs/explore-notes/
installer-preflight.md, ExecutableResolver, the IEnvironmentCheck/CheckResult/
EnvironmentCheckService abstraction, ProcessCommand, IProcessRunner) existed
only on an unmerged sibling branch, not on main. Rather than merging that
whole unreviewed branch, the specific prerequisite files were copied in
as-is (git history shows their origin). GitCheck/GitIdentityCheck/PortCheck/
WriteAccessCheck from that branch were intentionally NOT brought in — out of
scope for this task.

Deviations/decisions worth flagging:
- Added ExecutableResolver.FallbackDirectories() (public) so ClaudeCliCheck
  can name the checked fallback locations in its failure message; the prior
  branch kept that list private.
- Paths.cs now qualifies System.Environment explicitly. Adding the
  ClaudeDo.Data.Environment namespace makes bare `Environment.X` inside any
  ClaudeDo.Data.* namespace resolve to the sibling namespace instead of
  System.Environment (C# prefers nested/enclosing namespace members over
  usings) — this broke the build until qualified.

Not done (explicitly out of scope): no DI wiring into the wizard UI, no
XAML — matches how the prerequisite Git/Port/WriteAccess checks were also
left unwired.
2026-08-05 19:43:34 +02:00

85 lines
3.0 KiB
C#

using ClaudeDo.Installer.Checks;
using ClaudeDo.Installer.Core;
namespace ClaudeDo.Installer.Tests.Checks;
public sealed class ClaudeCliLookupTests : IDisposable
{
private readonly string _dir;
public ClaudeCliLookupTests()
{
_dir = Path.Combine(Path.GetTempPath(), $"cdclaudelookup_{Guid.NewGuid():N}");
Directory.CreateDirectory(_dir);
File.WriteAllText(Path.Combine(_dir, "claude.exe"), "");
}
public void Dispose()
{
try { Directory.Delete(_dir, recursive: true); } catch { }
}
[Fact]
public async Task Version_is_executed_only_once_across_repeated_calls()
{
var runner = new FakeProcessRunner((0, "2.1.230 (Claude Code)"));
var lookup = new ClaudeCliLookup(runner, pathOverride: _dir, pathExtOverride: ".exe");
await lookup.ResolveAsync(new InstallContext(), CancellationToken.None);
await lookup.ResolveAsync(new InstallContext(), CancellationToken.None);
await lookup.ResolveAsync(new InstallContext(), CancellationToken.None);
Assert.Single(runner.Calls);
}
[Fact]
public async Task Concurrent_calls_still_run_version_only_once()
{
var runner = new FakeProcessRunner((0, "2.1.230 (Claude Code)"));
var lookup = new ClaudeCliLookup(runner, pathOverride: _dir, pathExtOverride: ".exe");
await Task.WhenAll(Enumerable.Range(0, 8)
.Select(_ => lookup.ResolveAsync(new InstallContext(), CancellationToken.None)));
Assert.Single(runner.Calls);
}
[Fact]
public async Task Version_is_executed_only_once_when_shared_across_all_four_checks()
{
var runner = new FakeProcessRunner((0, "2.1.230 (Claude Code)"));
var lookup = new ClaudeCliLookup(runner, pathOverride: _dir, pathExtOverride: ".exe");
var ctx = new InstallContext();
await new ClaudeCliCheck(lookup).RunAsync(ctx, CancellationToken.None);
await new ClaudeVersionCheck(lookup).RunAsync(ctx, CancellationToken.None);
await new ClaudeAuthCheck(lookup).RunAsync(ctx, CancellationToken.None);
await new PermissionModeAutoCheck(lookup).RunAsync(ctx, CancellationToken.None);
var versionCalls = runner.Calls.Count(c => c.Arguments.Contains("--version"));
Assert.Equal(1, versionCalls);
}
[Fact]
public async Task Cli_not_found_returns_null_resolved_without_running_a_process()
{
var emptyDir = Path.Combine(Path.GetTempPath(), $"cdclaudelookup_empty_{Guid.NewGuid():N}");
Directory.CreateDirectory(emptyDir);
try
{
var runner = new FakeProcessRunner();
var lookup = new ClaudeCliLookup(runner, pathOverride: emptyDir, pathExtOverride: ".exe");
var ctx = new InstallContext { ClaudeBin = "claudedo_totally_missing_cmd_9f3a1" };
var result = await lookup.ResolveAsync(ctx, CancellationToken.None);
Assert.Null(result.Resolved);
Assert.Empty(runner.Calls);
}
finally
{
Directory.Delete(emptyDir, recursive: true);
}
}
}