Repository navigation
Expand file tree
/
Copy pathClaudeCliSmokeTests.cs
More file actions
335 lines (296 loc) · 12.7 KB
/
Copy pathClaudeCliSmokeTests.cs
File metadata and controls
335 lines (296 loc) · 12.7 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
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using System.Text.Json;
using ManagedCode.ClaudeCodeSharpSDK.Client;
using ManagedCode.ClaudeCodeSharpSDK.Configuration;
using ManagedCode.ClaudeCodeSharpSDK.Models;
using ManagedCode.ClaudeCodeSharpSDK.Tests.Shared;
namespace ManagedCode.ClaudeCodeSharpSDK.Tests.Integration;
[NotInParallel("CliProcess")]
public class ClaudeCliSmokeTests
{
private const string SandboxPrefix = "ClaudeCliSmokeTests-";
private const string HomeEnvironmentVariable = "HOME";
private const string UserProfileEnvironmentVariable = "USERPROFILE";
private const string XdgConfigHomeEnvironmentVariable = "XDG_CONFIG_HOME";
private const string AppDataEnvironmentVariable = "APPDATA";
private const string LocalAppDataEnvironmentVariable = "LOCALAPPDATA";
private const string ClaudeConfigDirEnvironmentVariable = "CLAUDE_CONFIG_DIR";
private const string AnthropicApiKeyEnvironmentVariable = "ANTHROPIC_API_KEY";
private const string AnthropicBaseUrlEnvironmentVariable = "ANTHROPIC_BASE_URL";
private const string VersionFlag = "--version";
private const string HelpFlag = "--help";
private const string PrintFlag = "-p";
private const string OutputFormatFlag = "--output-format";
private const string StreamJsonFormat = "stream-json";
private const string VerboseFlag = "--verbose";
private const string ReplyWithOkPrompt = "Reply with ok";
private const string ClaudeCodeFragment = "Claude Code";
private const string LoginGuidanceFragment = "Please run /login";
private const string ErrorJsonFragment = "\"is_error\":true";
private const string TypePropertyName = "type";
private const string SystemType = "system";
private const string ResultType = "result";
private const string ClaudeConfigDirectoryName = ".claude";
private const string ConfigDirectoryName = ".config";
private const string NewLine = "\n";
private const string CarriageReturn = "\r";
private const string CouldNotLocateRepositoryRootMessage = "Could not locate repository root from test execution directory.";
private const string StartProcessFailedMessagePrefix = "Failed to start Claude Code CLI at";
private const string SmokeCleanupFailedMessage = "Claude CLI smoke process cleanup could not be fully confirmed.";
private const string ClaudeCodeNestingEnvironmentVariable = "CLAUDECODE";
private const string Space = " ";
private const string MessageQuote = "'";
private const string MessageSuffix = ".";
private static readonly string[] StandardLineSeparators = [Environment.NewLine, NewLine, CarriageReturn];
private static readonly TimeSpan TestTimeout = TimeSpan.FromMinutes(2);
private static readonly TimeSpan TestCleanupTimeout = TimeSpan.FromSeconds(10);
[Test]
public async Task ClaudeCli_Smoke_FindExecutablePath_ResolvesExistingBinary()
{
var command = ResolveLaunchCommand();
await Assert.That(File.Exists(command.ExecutablePath)).IsTrue();
}
[Test]
public async Task ClaudeCli_Smoke_VersionCommand_ReturnsClaudeCodeVersion()
{
using var timeoutCts = new CancellationTokenSource(TestTimeout);
var result = await RunClaudeAsync(ResolveLaunchCommand(), null, timeoutCts.Token, VersionFlag);
await Assert.That(result.ExitCode).IsEqualTo(0);
await Assert.That(string.Concat(result.StandardOutput, result.StandardError))
.Contains(ClaudeCodeFragment);
}
[Test]
public async Task ClaudeCli_Smoke_HelpCommand_DescribesStreamJsonOutput()
{
using var timeoutCts = new CancellationTokenSource(TestTimeout);
var result = await RunClaudeAsync(ResolveLaunchCommand(), null, timeoutCts.Token, HelpFlag);
await Assert.That(result.ExitCode).IsEqualTo(0);
await Assert.That(string.Concat(result.StandardOutput, result.StandardError))
.Contains(OutputFormatFlag);
}
[Test]
public async Task ClaudeCli_Smoke_PrintModeWithoutAuth_EmitsInitAndLoginGuidance()
{
var sandboxDirectory = CreateSandboxDirectory();
using var timeoutCts = new CancellationTokenSource(TestTimeout);
try
{
var result = await RunClaudeAsync(
ResolveLaunchCommand(),
CreateUnauthenticatedEnvironmentOverrides(sandboxDirectory),
timeoutCts.Token,
PrintFlag,
OutputFormatFlag,
StreamJsonFormat,
VerboseFlag,
ReplyWithOkPrompt);
var stdoutLines = result.StandardOutput
.Split(StandardLineSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
await Assert.That(stdoutLines.Length).IsGreaterThanOrEqualTo(2);
await Assert.That(result.StandardOutput).Contains(LoginGuidanceFragment);
await Assert.That(result.StandardOutput).Contains(ErrorJsonFragment);
using var initDocument = JsonDocument.Parse(stdoutLines[0]);
using var finalDocument = JsonDocument.Parse(stdoutLines[^1]);
await Assert.That(initDocument.RootElement.GetProperty(TypePropertyName).GetString()).IsEqualTo(SystemType);
await Assert.That(finalDocument.RootElement.GetProperty(TypePropertyName).GetString()).IsEqualTo(ResultType);
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
private static CliLaunchCommand ResolveLaunchCommand()
{
using var client = new ClaudeClient(new ClaudeOptions());
return client.GetCliLaunchCommand();
}
private static string CreateSandboxDirectory()
{
var sandboxDirectory = Path.Combine(
ResolveRepositoryRootPath(),
TestConstants.TestsDirectoryName,
TestConstants.SandboxDirectoryName,
string.Concat(SandboxPrefix, Guid.NewGuid().ToString(TestConstants.NumericGuidFormat)));
Directory.CreateDirectory(sandboxDirectory);
return sandboxDirectory;
}
private static Dictionary<string, string> CreateUnauthenticatedEnvironmentOverrides(string sandboxDirectory)
{
var claudeConfigDirectory = Path.Combine(sandboxDirectory, ClaudeConfigDirectoryName);
var configHome = Path.Combine(sandboxDirectory, ConfigDirectoryName);
var appData = Path.Combine(sandboxDirectory, TestConstants.AppDataDirectoryName, TestConstants.RoamingDirectoryName);
var localAppData = Path.Combine(sandboxDirectory, TestConstants.AppDataDirectoryName, TestConstants.LocalDirectoryName);
Directory.CreateDirectory(claudeConfigDirectory);
Directory.CreateDirectory(configHome);
Directory.CreateDirectory(appData);
Directory.CreateDirectory(localAppData);
return new Dictionary<string, string>(StringComparer.Ordinal)
{
[HomeEnvironmentVariable] = sandboxDirectory,
[UserProfileEnvironmentVariable] = sandboxDirectory,
[XdgConfigHomeEnvironmentVariable] = configHome,
[AppDataEnvironmentVariable] = appData,
[LocalAppDataEnvironmentVariable] = localAppData,
[ClaudeConfigDirEnvironmentVariable] = claudeConfigDirectory,
[AnthropicApiKeyEnvironmentVariable] = TestConstants.EmptyString,
[AnthropicBaseUrlEnvironmentVariable] = TestConstants.EmptyString,
};
}
private static string ResolveRepositoryRootPath()
{
var current = new DirectoryInfo(AppContext.BaseDirectory);
while (current is not null)
{
if (File.Exists(Path.Combine(current.FullName, TestConstants.SolutionFileName)))
{
return current.FullName;
}
current = current.Parent;
}
throw new InvalidOperationException(CouldNotLocateRepositoryRootMessage);
}
private static async Task<ClaudeProcessResult> RunClaudeAsync(
CliLaunchCommand command,
IReadOnlyDictionary<string, string>? environmentOverrides,
CancellationToken cancellationToken = default,
params string[] arguments)
{
var startInfo = new ProcessStartInfo(command.ExecutablePath)
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
foreach (var prefixArgument in command.PrefixArguments)
{
startInfo.ArgumentList.Add(prefixArgument);
}
foreach (var argument in arguments)
{
startInfo.ArgumentList.Add(argument);
}
startInfo.Environment.Remove(ClaudeCodeNestingEnvironmentVariable);
if (environmentOverrides is not null)
{
foreach (var (key, value) in environmentOverrides)
{
startInfo.Environment[key] = value;
}
}
using var process = new Process { StartInfo = startInfo };
if (!process.Start())
{
throw new InvalidOperationException(
string.Concat(
StartProcessFailedMessagePrefix,
Space,
MessageQuote,
command.ExecutablePath,
MessageQuote,
MessageSuffix));
}
Task<string>? standardOutputTask = null;
Task<string>? standardErrorTask = null;
ClaudeProcessResult? result = null;
Exception? invocationFailure = null;
try
{
process.StandardInput.Close();
standardOutputTask = process.StandardOutput.ReadToEndAsync(cancellationToken);
standardErrorTask = process.StandardError.ReadToEndAsync(cancellationToken);
await process.WaitForExitAsync(cancellationToken);
var output = await Task.WhenAll(standardOutputTask, standardErrorTask)
.WaitAsync(TestCleanupTimeout, cancellationToken);
result = new ClaudeProcessResult(process.ExitCode, output[0], output[1]);
}
catch (Exception exception)
{
invocationFailure = exception;
}
var cleanupFailures = new List<Exception>();
if (!process.HasExited)
{
try
{
process.Kill(entireProcessTree: true);
}
catch (InvalidOperationException) when (process.HasExited)
{
// The CLI exited between the state check and the kill request.
}
catch (Exception exception)
{
cleanupFailures.Add(exception);
}
}
if (!process.HasExited)
{
try
{
await process.WaitForExitAsync().WaitAsync(TestCleanupTimeout);
}
catch (Exception exception)
{
cleanupFailures.Add(exception);
}
}
try
{
process.StandardOutput.Dispose();
}
catch (Exception exception)
{
cleanupFailures.Add(exception);
}
try
{
process.StandardError.Dispose();
}
catch (Exception exception)
{
cleanupFailures.Add(exception);
}
if (standardOutputTask is not null && standardErrorTask is not null)
{
try
{
await ObserveReadersAsync(standardOutputTask, standardErrorTask, cancellationToken);
}
catch (Exception exception)
{
cleanupFailures.Add(exception);
}
}
if (cleanupFailures.Count != 0)
{
if (invocationFailure is not null)
{
cleanupFailures.Insert(0, invocationFailure);
}
throw new AggregateException(SmokeCleanupFailedMessage, cleanupFailures);
}
if (invocationFailure is not null)
{
ExceptionDispatchInfo.Capture(invocationFailure).Throw();
}
return result!;
}
private static async Task ObserveReadersAsync(
Task<string> standardOutputTask,
Task<string> standardErrorTask,
CancellationToken cancellationToken)
{
try
{
await Task.WhenAll(standardOutputTask, standardErrorTask).WaitAsync(TestCleanupTimeout, CancellationToken.None);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// A timed-out smoke process has had its owned output reads canceled and observed.
}
}
private sealed record ClaudeProcessResult(int ExitCode, string StandardOutput, string StandardError);
}