Repository navigation
Expand file tree
/
Copy pathClaudeCliMetadataReaderTests.cs
More file actions
520 lines (467 loc) · 25.9 KB
/
Copy pathClaudeCliMetadataReaderTests.cs
File metadata and controls
520 lines (467 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using System.Diagnostics;
using System.Text.Json;
using ManagedCode.ClaudeCodeSharpSDK.Client;
using ManagedCode.ClaudeCodeSharpSDK.Configuration;
using ManagedCode.ClaudeCodeSharpSDK.Execution;
using ManagedCode.ClaudeCodeSharpSDK.Internal;
using ManagedCode.ClaudeCodeSharpSDK.Models;
using ManagedCode.ClaudeCodeSharpSDK.Tests.Shared;
namespace ManagedCode.ClaudeCodeSharpSDK.Tests.Unit;
public class ClaudeCliMetadataReaderTests
{
private const string MetadataSandboxPrefix = "ClaudeCliMetadataReaderTests-";
private const string NpmPackageDirectory = "claude-code";
private const string NodeModulesDirectory = "node_modules";
private const string DotBinDirectory = ".bin";
private const string NpmPackageManifestName = "package.json";
private const string NpmShimName = "claude.cmd";
private const string NpmEntrypointRelativePath = "bundle/claude-fixture.js";
private const string NpmPackageManifest = "{\"name\":\"@anthropic-ai/claude-code\",\"bin\":{\"claude\":\"bundle/claude-fixture.js\"}}";
private const string NodeCliFixture = "if(process.argv.includes('--version')){console.log('2.0.75 (Claude Code)');}else{process.stdin.setEncoding('utf8');let input='';process.stdin.on('data',chunk=>input+=chunk);process.stdin.on('end',()=>console.log(JSON.stringify({args:process.argv.slice(2),input})));}";
private const string PromptFixture = "--approval-mode=yolo ! & | ^ % ' space\nsecond line";
private const string NodeWindowsExecutableName = "node.exe";
private const string NodeExecutableName = "node";
private const string NpmPackageScopeSegment = "@anthropic-ai";
private const string WindowsShimTestSkipReason = "The npm command-shim launch contract is Windows-specific.";
private const string UnsupportedShimErrorFragment = "configured Claude Code command shim is unsupported";
private const string SystemRootRequiredMessage = "SystemRoot is required for Windows child processes.";
private const string NodeRequiredMessage = "Node.js is required for the Windows npm shim test.";
private const string QuoteCharacter = "\"";
private const string JsonInputPropertyName = "input";
private const string JsonArgumentsPropertyName = "args";
private const string PrintFlag = "--print";
private const string PathEnvironmentVariable = "PATH";
private const string SystemRootEnvironmentVariable = "SystemRoot";
private const string ClaudeConfigDirectoryEnvironmentVariable = "CLAUDE_CONFIG_DIR";
private const string SettingsFileName = "settings.json";
private const int SmallMetadataFileLimit = 256;
private const string MetadataFileLimitMessage = "CLI metadata file exceeded the configured character limit.";
private const string ClaudeSettingsFixture = "{ \"model\": \"" + ClaudeModels.Sonnet + "\" }";
private const string CommandFlagUnix = "-c";
private const string CommandFlagWindows = "/c";
private const string ConcurrentOutputCommandUnix =
"i=0; while [ $i -lt 5000 ]; do printf 'stdout-line-%s\\n' \"$i\"; printf 'stderr-line-%s\\n' \"$i\" >&2; i=$((i+1)); done";
private const string ConcurrentOutputCommandWindows =
"for /L %i in (0,1,4999) do @(echo stdout-line-%i & echo stderr-line-%i 1>&2)";
private const string DefaultModelJsonTemplate = "{\"model\":\"__MODEL__\",\"statusLine\":{\"enabled\":true}}";
private const string FirstStandardErrorLine = "stderr-line-0";
private const string FirstStandardOutputLine = "stdout-line-0";
private const string HighestStableGitOutput = "deadbeef\trefs/tags/v2.0.74\nfeedface\trefs/tags/v2.0.75-beta.1\ncafebabe\trefs/tags/v2.0.75";
private const string InstalledVersionOutput = "2.0.75 (Claude Code)";
private const string InvalidVersionText = "not-a-version";
private const string LastStandardErrorLine = "stderr-line-4999";
private const string LastStandardOutputLine = "stdout-line-4999";
private const string ModelPlaceholder = "__MODEL__";
private const string NumericPrereleaseGitOutput = "deadbeef\trefs/tags/v2.0.75-beta.2\nfeedface\trefs/tags/v2.0.75-beta.10";
private const string ProcessReadTimedOutMessage = "Concurrent process stream read timed out.";
private const string ShellExecutableUnix = "/bin/sh";
private const string ShellExecutableWindows = "cmd.exe";
private const string StartProcessFailedMessage = "Failed to start metadata reader test process.";
private const string PrereleaseTenVersion = "2.0.75-beta.10";
private const string PrereleaseTwoVersion = "2.0.75-beta.2";
private const string StableVersion = "2.0.75";
private const string StableVsPrereleaseVersion = "2.0.75-beta.1";
private static readonly TimeSpan ProcessReadTimeout = TimeSpan.FromSeconds(90);
[Test]
public async Task ParseInstalledVersion_ReturnsFirstTokenForClaudeCodeOutput()
{
var parsed = ClaudeCliMetadataReader.ParseInstalledVersion(InstalledVersionOutput);
await Assert.That(parsed).IsEqualTo(StableVersion);
}
[Test]
public async Task ParseLatestPublishedVersion_PicksHighestGitTag()
{
var parsed = ClaudeCliMetadataReader.ParseLatestPublishedVersion(HighestStableGitOutput);
await Assert.That(parsed).IsEqualTo(StableVersion);
}
[Test]
public async Task IsNewerVersion_TreatsStableAsNotOlderThanMatchingPrerelease()
{
var isNewer = ClaudeCliMetadataReader.IsNewerVersion(StableVsPrereleaseVersion, StableVersion);
await Assert.That(isNewer).IsFalse();
}
[Test]
public async Task IsNewerVersion_UsesNumericSemVerComparisonForPrereleaseIdentifiers()
{
var isNewer = ClaudeCliMetadataReader.IsNewerVersion(PrereleaseTenVersion, PrereleaseTwoVersion);
await Assert.That(isNewer).IsTrue();
}
[Test]
public async Task ParseLatestPublishedVersion_UsesNumericSemVerComparisonForPrereleaseIdentifiers()
{
var parsed = ClaudeCliMetadataReader.ParseLatestPublishedVersion(NumericPrereleaseGitOutput);
await Assert.That(parsed).IsEqualTo(PrereleaseTenVersion);
}
[Test]
public async Task ParseDefaultModelFromJson_ReadsModelProperty()
{
var parsed = ClaudeCliMetadataReader.ParseDefaultModelFromJson(
DefaultModelJsonTemplate.Replace(ModelPlaceholder, ClaudeModels.ClaudeOpus45, StringComparison.Ordinal));
await Assert.That(parsed).IsEqualTo(ClaudeModels.ClaudeOpus45);
}
[Test]
public async Task TryParseSemanticVersion_RejectsInvalidText()
{
var parsed = ClaudeCliMetadataReader.TryParseSemanticVersion(InvalidVersionText, out _);
await Assert.That(parsed).IsFalse();
}
[Test]
public async Task ReadStandardStreamsAndWaitForExit_CapturesLargeStandardOutputAndError()
{
using var process = Process.Start(CreateConcurrentOutputProcessStartInfo())
?? throw new InvalidOperationException(StartProcessFailedMessage);
process.StandardInput.Close();
var readTask = Task.Run(() => ClaudeCliMetadataReader.ReadStandardStreamsAndWaitForExit(process));
var completedTask = await Task.WhenAny(readTask, Task.Delay(ProcessReadTimeout));
if (!ReferenceEquals(completedTask, readTask))
{
TryKillProcess(process);
throw new TimeoutException(ProcessReadTimedOutMessage);
}
var (standardOutput, standardError) = await readTask;
await Assert.That(process.ExitCode).IsEqualTo(0);
await Assert.That(standardOutput).Contains(FirstStandardOutputLine);
await Assert.That(standardOutput).Contains(LastStandardOutputLine);
await Assert.That(standardError).Contains(FirstStandardErrorLine);
await Assert.That(standardError).Contains(LastStandardErrorLine);
}
[Test]
public async Task ClaudeClient_GetCliMetadata_UsesConfiguredSettingsAndEnvironmentAllowlist()
{
var configDirectory = Path.Combine(Environment.CurrentDirectory, "tests", ".sandbox",
$"{MetadataSandboxPrefix}{Guid.NewGuid():N}");
Directory.CreateDirectory(configDirectory);
try
{
File.WriteAllText(Path.Combine(configDirectory, SettingsFileName), ClaudeSettingsFixture);
using var client = new ClaudeClient(new ClaudeOptions
{
ClaudeExecutablePath = ClaudeCliLocator.FindClaudePath(null),
EnvironmentVariables = new Dictionary<string, string>(StringComparer.Ordinal)
{
[PathEnvironmentVariable] = Environment.GetEnvironmentVariable(PathEnvironmentVariable) ?? string.Empty,
[SystemRootEnvironmentVariable] = Environment.GetEnvironmentVariable(SystemRootEnvironmentVariable) ?? string.Empty,
[ClaudeConfigDirectoryEnvironmentVariable] = configDirectory,
},
InheritEnvironmentVariables = false,
});
var metadata = client.GetCliMetadata();
await Assert.That(metadata.DefaultModel).IsEqualTo(ClaudeModels.Sonnet);
await Assert.That(string.IsNullOrWhiteSpace(metadata.InstalledVersion)).IsFalse();
}
finally
{
Directory.Delete(configDirectory, recursive: true);
}
}
[Test]
public async Task ClaudeClient_GetCliMetadata_ResolvesOfficialNpmShimThroughNodeOnWindows()
{
if (!OperatingSystem.IsWindows())
{
Skip.Test(WindowsShimTestSkipReason);
}
var sandboxDirectory = Path.Combine(Environment.CurrentDirectory, "tests", ".sandbox",
$"{MetadataSandboxPrefix}{Guid.NewGuid():N}");
var modulesDirectory = Path.Combine(sandboxDirectory, NodeModulesDirectory);
var packageDirectory = Path.Combine(modulesDirectory, NpmPackageScopeSegment, NpmPackageDirectory);
var shimDirectory = Path.Combine(modulesDirectory, DotBinDirectory);
Directory.CreateDirectory(packageDirectory);
Directory.CreateDirectory(shimDirectory);
try
{
await File.WriteAllTextAsync(Path.Combine(packageDirectory, NpmPackageManifestName), NpmPackageManifest);
var entrypointPath = Path.Combine(packageDirectory, NpmEntrypointRelativePath.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(Path.GetDirectoryName(entrypointPath)!);
await File.WriteAllTextAsync(entrypointPath, NodeCliFixture);
var shimPath = Path.Combine(shimDirectory, NpmShimName);
await File.WriteAllTextAsync(shimPath, TestConstants.EchoOffScript);
var nodePath = FindNodeExecutablePath(Environment.GetEnvironmentVariable(PathEnvironmentVariable), isWindows: true);
var effectivePath = string.Join(Path.PathSeparator,
Path.GetDirectoryName(nodePath)!, Environment.GetEnvironmentVariable(PathEnvironmentVariable) ?? string.Empty);
using var client = new ClaudeClient(new ClaudeOptions
{
ClaudeExecutablePath = shimPath,
EnvironmentVariables = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
[PathEnvironmentVariable] = effectivePath,
[SystemRootEnvironmentVariable] = Environment.GetEnvironmentVariable(SystemRootEnvironmentVariable)
?? throw new InvalidOperationException(SystemRootRequiredMessage),
},
InheritEnvironmentVariables = false,
});
var launch = client.GetCliLaunchCommand();
var metadata = client.GetCliMetadata();
await Assert.That(launch.ExecutablePath).IsEqualTo(nodePath);
await Assert.That(launch.PrefixArguments).IsEquivalentTo([Path.GetFullPath(entrypointPath)]);
await Assert.That(metadata.InstalledVersion).IsEqualTo("2.0.75");
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
[Test]
public async Task ClaudeClient_GetCliLaunchCommand_DoesNotReplaceAnExplicitUnknownShimWithTheDefaultCli()
{
if (!OperatingSystem.IsWindows())
{
Skip.Test(WindowsShimTestSkipReason);
}
var sandboxDirectory = Path.Combine(Environment.CurrentDirectory, "tests", ".sandbox",
$"{MetadataSandboxPrefix}{Guid.NewGuid():N}");
var explicitCommandDirectory = Path.Combine(sandboxDirectory, "custom-bin");
var defaultShimDirectory = Path.Combine(sandboxDirectory, NodeModulesDirectory, DotBinDirectory);
var decoyPackageDirectory = Path.Combine(sandboxDirectory, NodeModulesDirectory,
NpmPackageScopeSegment, NpmPackageDirectory);
Directory.CreateDirectory(explicitCommandDirectory);
Directory.CreateDirectory(defaultShimDirectory);
Directory.CreateDirectory(decoyPackageDirectory);
try
{
var explicitShim = Path.Combine(explicitCommandDirectory, "custom-claude.cmd");
await File.WriteAllTextAsync(explicitShim, TestConstants.EchoOffScript);
await File.WriteAllTextAsync(Path.Combine(defaultShimDirectory, NpmShimName), TestConstants.EchoOffScript);
await File.WriteAllTextAsync(Path.Combine(decoyPackageDirectory, NpmPackageManifestName), NpmPackageManifest);
var decoyEntrypoint = Path.Combine(decoyPackageDirectory, NpmEntrypointRelativePath.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(Path.GetDirectoryName(decoyEntrypoint)!);
await File.WriteAllTextAsync(decoyEntrypoint, NodeCliFixture);
var nodePath = FindNodeExecutablePath(Environment.GetEnvironmentVariable(PathEnvironmentVariable), isWindows: true);
var effectivePath = string.Join(Path.PathSeparator, explicitCommandDirectory, defaultShimDirectory,
Path.GetDirectoryName(nodePath)!);
using var client = new ClaudeClient(new ClaudeOptions
{
ClaudeExecutablePath = explicitShim,
EnvironmentVariables = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
[PathEnvironmentVariable] = effectivePath,
[SystemRootEnvironmentVariable] = Environment.GetEnvironmentVariable(SystemRootEnvironmentVariable)
?? throw new InvalidOperationException(SystemRootRequiredMessage),
},
InheritEnvironmentVariables = false,
});
var action = () => client.GetCliLaunchCommand();
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<InvalidOperationException>();
await Assert.That(exception!.Message).Contains(UnsupportedShimErrorFragment);
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
[Test]
public async Task ClaudeClient_GetCliLaunchCommand_DoesNotFallBackFromMissingExplicitPathToPathEntry()
{
var sandboxDirectory = Path.Combine(Environment.CurrentDirectory, "tests", ".sandbox",
$"{MetadataSandboxPrefix}{Guid.NewGuid():N}");
var pathDirectory = Path.Combine(sandboxDirectory, "bin");
Directory.CreateDirectory(pathDirectory);
var markerPath = Path.Combine(sandboxDirectory, "launched.marker");
var pathExecutable = Path.Combine(pathDirectory,
OperatingSystem.IsWindows() ? ClaudeCliLocator.ClaudeWindowsExecutableName : ClaudeCliLocator.ClaudeExecutableName);
var missingExecutable = Path.Combine(sandboxDirectory, "missing", Path.GetFileName(pathExecutable));
try
{
if (OperatingSystem.IsWindows())
{
var nodePath = FindNodeExecutablePath(Environment.GetEnvironmentVariable(PathEnvironmentVariable), isWindows: true);
File.Copy(nodePath, pathExecutable);
}
else
{
await File.WriteAllTextAsync(pathExecutable, $"#!/bin/sh\nprintf started > '{markerPath}'\n");
File.SetUnixFileMode(pathExecutable, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
}
var options = new ClaudeOptions
{
ClaudeExecutablePath = missingExecutable,
EnvironmentVariables = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
[PathEnvironmentVariable] = pathDirectory,
},
InheritEnvironmentVariables = false,
};
using var client = new ClaudeClient(options);
var exception = await Assert.That(() => client.GetCliLaunchCommand()).ThrowsException();
await Assert.That(exception).IsTypeOf<FileNotFoundException>();
await Assert.That(File.Exists(markerPath)).IsFalse();
var bareNameOptions = options with { ClaudeExecutablePath = Path.GetFileName(pathExecutable) };
await Assert.That(bareNameOptions.GetCliLaunchCommand().ExecutablePath).IsEqualTo(Path.GetFullPath(pathExecutable));
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
[Test]
public async Task ClaudeClient_GetCliLaunchCommand_RejectsMissingExplicitScriptBeforeRuntimeOrPackageResolution()
{
var sandboxDirectory = Path.Combine(Environment.CurrentDirectory, "tests", ".sandbox",
$"{MetadataSandboxPrefix}{Guid.NewGuid():N}");
var modulesDirectory = Path.Combine(sandboxDirectory, NodeModulesDirectory);
var packageDirectory = Path.Combine(modulesDirectory, NpmPackageScopeSegment, NpmPackageDirectory);
var shimDirectory = Path.Combine(modulesDirectory, DotBinDirectory);
Directory.CreateDirectory(packageDirectory);
Directory.CreateDirectory(shimDirectory);
try
{
var entrypointPath = Path.Combine(packageDirectory, NpmEntrypointRelativePath.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(Path.GetDirectoryName(entrypointPath)!);
await File.WriteAllTextAsync(Path.Combine(packageDirectory, NpmPackageManifestName), NpmPackageManifest);
await File.WriteAllTextAsync(entrypointPath, NodeCliFixture);
var nodePath = FindNodeExecutablePath(Environment.GetEnvironmentVariable(PathEnvironmentVariable), OperatingSystem.IsWindows());
var options = new ClaudeOptions
{
EnvironmentVariables = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
[PathEnvironmentVariable] = string.Join(Path.PathSeparator, Path.GetDirectoryName(nodePath)!,
Environment.GetEnvironmentVariable(PathEnvironmentVariable) ?? string.Empty),
[SystemRootEnvironmentVariable] = Environment.GetEnvironmentVariable(SystemRootEnvironmentVariable)
?? string.Empty,
},
InheritEnvironmentVariables = false,
};
await AssertMissingSelectionAsync(options, Path.Combine(sandboxDirectory, "missing", "claude.js"));
await AssertMissingSelectionAsync(options, Path.Combine("missing", "claude"));
if (OperatingSystem.IsWindows())
{
await AssertMissingSelectionAsync(options, Path.Combine(shimDirectory, NpmShimName));
await AssertMissingSelectionAsync(options, Path.Combine(shimDirectory, ClaudeCliLocator.ClaudeWindowsBatchName));
}
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
private static async Task AssertMissingSelectionAsync(ClaudeOptions options, string selectedPath)
{
using var client = new ClaudeClient(options with { ClaudeExecutablePath = selectedPath });
var exception = await Assert.That(() => client.GetCliLaunchCommand()).ThrowsException();
await Assert.That(exception).IsTypeOf<FileNotFoundException>();
}
[Test]
public async Task ClaudeExec_ResolvesJavaScriptEntryAndPassesPromptThroughStdin()
{
var sandboxDirectory = Path.Combine(Environment.CurrentDirectory, "tests", ".sandbox",
$"{MetadataSandboxPrefix}{Guid.NewGuid():N}");
Directory.CreateDirectory(sandboxDirectory);
try
{
var entrypointPath = Path.Combine(sandboxDirectory, NpmEntrypointRelativePath.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(Path.GetDirectoryName(entrypointPath)!);
await File.WriteAllTextAsync(entrypointPath, NodeCliFixture);
var nodePath = FindNodeExecutablePath(Environment.GetEnvironmentVariable(PathEnvironmentVariable), OperatingSystem.IsWindows());
var environment = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
[PathEnvironmentVariable] = string.Join(Path.PathSeparator, Path.GetDirectoryName(nodePath)!,
Environment.GetEnvironmentVariable(PathEnvironmentVariable) ?? string.Empty),
};
if (OperatingSystem.IsWindows())
{
environment[SystemRootEnvironmentVariable] = Environment.GetEnvironmentVariable(SystemRootEnvironmentVariable)
?? throw new InvalidOperationException(SystemRootRequiredMessage);
}
var input = string.Concat(PromptFixture, new string('x', 256 * 1024));
var exec = new ClaudeExec(inheritEnvironmentVariables: false, executablePath: entrypointPath,
environmentOverride: environment);
var output = new List<string>();
await foreach (var line in exec.RunAsync(new ClaudeExecArgs { Input = input }))
{
output.Add(line);
}
await Assert.That(output.Count).IsEqualTo(1);
using var execution = JsonDocument.Parse(output[0]);
await Assert.That(execution.RootElement.GetProperty(JsonInputPropertyName).GetString()).IsEqualTo(input);
await Assert.That(execution.RootElement.GetProperty(JsonArgumentsPropertyName).EnumerateArray()
.Any(argument => string.Equals(argument.GetString(), PrintFlag, StringComparison.Ordinal))).IsTrue();
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
[Test]
public async Task ClaudeClient_GetCliMetadata_RejectsOversizedSettingsFile()
{
var configDirectory = Path.Combine(Environment.CurrentDirectory, "tests", ".sandbox",
$"{MetadataSandboxPrefix}{Guid.NewGuid():N}");
Directory.CreateDirectory(configDirectory);
try
{
File.WriteAllText(Path.Combine(configDirectory, SettingsFileName),
string.Concat(ClaudeSettingsFixture, new string('x', SmallMetadataFileLimit + 1)));
using var client = new ClaudeClient(new ClaudeOptions
{
ClaudeExecutablePath = ClaudeCliLocator.FindClaudePath(null),
EnvironmentVariables = new Dictionary<string, string>(StringComparer.Ordinal)
{
[PathEnvironmentVariable] = Environment.GetEnvironmentVariable(PathEnvironmentVariable) ?? string.Empty,
[SystemRootEnvironmentVariable] = Environment.GetEnvironmentVariable(SystemRootEnvironmentVariable) ?? string.Empty,
[ClaudeConfigDirectoryEnvironmentVariable] = configDirectory,
},
InheritEnvironmentVariables = false,
CliMetadataMaximumFileCharacters = SmallMetadataFileLimit,
});
var action = () => client.GetCliMetadata();
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<InvalidOperationException>();
await Assert.That(exception!.Message).Contains(MetadataFileLimitMessage);
}
finally
{
Directory.Delete(configDirectory, recursive: true);
}
}
[Test]
public async Task ClaudeClient_GetCliMetadata_RejectsNonPositiveProbeLeaseTimeout()
{
foreach (var timeout in new[] { TimeSpan.Zero, TimeSpan.FromMilliseconds(-1) })
{
using var client = new ClaudeClient(new ClaudeOptions { CliMetadataProbeLeaseTimeout = timeout });
var action = () => client.GetCliMetadata();
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<ArgumentOutOfRangeException>();
}
}
private static ProcessStartInfo CreateConcurrentOutputProcessStartInfo()
{
var startInfo = new ProcessStartInfo(
OperatingSystem.IsWindows() ? ShellExecutableWindows : ShellExecutableUnix)
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
startInfo.ArgumentList.Add(OperatingSystem.IsWindows() ? CommandFlagWindows : CommandFlagUnix);
startInfo.ArgumentList.Add(OperatingSystem.IsWindows() ? ConcurrentOutputCommandWindows : ConcurrentOutputCommandUnix);
return startInfo;
}
private static string FindNodeExecutablePath(string? pathVariable, bool isWindows)
{
if (!string.IsNullOrWhiteSpace(pathVariable))
{
foreach (var pathEntry in pathVariable.Split(Path.PathSeparator,
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
var candidate = Path.Combine(pathEntry.Trim(QuoteCharacter[0]),
isWindows ? NodeWindowsExecutableName : NodeExecutableName);
if (File.Exists(candidate))
{
return candidate;
}
}
}
throw new InvalidOperationException(NodeRequiredMessage);
}
private static void TryKillProcess(Process process)
{
if (!process.HasExited)
{
process.Kill(entireProcessTree: true);
}
}
}