|
| 1 | +using ManagedCode.CodexSharpSDK.Client; |
| 2 | +using ManagedCode.CodexSharpSDK.Configuration; |
| 3 | +using ManagedCode.CodexSharpSDK.Internal; |
| 4 | + |
| 5 | +namespace ManagedCode.CodexSharpSDK.Tests.Shared; |
| 6 | + |
| 7 | +internal static class RealCodexTestSupport |
| 8 | +{ |
| 9 | + private const string ModelEnvVar = "CODEX_TEST_MODEL"; |
| 10 | + |
| 11 | + public static RealCodexTestSettings GetRequiredSettings() |
| 12 | + { |
| 13 | + if (!IsCodexAvailable()) |
| 14 | + { |
| 15 | + throw new InvalidOperationException( |
| 16 | + "Real Codex tests require the codex CLI. Install it first and ensure it is available in PATH."); |
| 17 | + } |
| 18 | + |
| 19 | + return new RealCodexTestSettings(ResolveModel()); |
| 20 | + } |
| 21 | + |
| 22 | + public static CodexClient CreateClient() |
| 23 | + { |
| 24 | + return new CodexClient(new CodexOptions()); |
| 25 | + } |
| 26 | + |
| 27 | + private static string ResolveModel() |
| 28 | + { |
| 29 | + var fromEnvironment = Environment.GetEnvironmentVariable(ModelEnvVar); |
| 30 | + if (!string.IsNullOrWhiteSpace(fromEnvironment)) |
| 31 | + { |
| 32 | + return fromEnvironment; |
| 33 | + } |
| 34 | + |
| 35 | + var fromConfig = TryReadModelFromCodexConfig(); |
| 36 | + if (!string.IsNullOrWhiteSpace(fromConfig)) |
| 37 | + { |
| 38 | + return fromConfig; |
| 39 | + } |
| 40 | + |
| 41 | + throw new InvalidOperationException( |
| 42 | + $"Real Codex tests require a model. Set {ModelEnvVar} or define model in '~/.codex/config.toml'."); |
| 43 | + } |
| 44 | + |
| 45 | + private static string? TryReadModelFromCodexConfig() |
| 46 | + { |
| 47 | + var configPath = GetCodexConfigPath(); |
| 48 | + if (configPath is null || !File.Exists(configPath)) |
| 49 | + { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + try |
| 54 | + { |
| 55 | + foreach (var line in File.ReadLines(configPath)) |
| 56 | + { |
| 57 | + var trimmed = line.Trim(); |
| 58 | + if (!trimmed.StartsWith("model", StringComparison.Ordinal)) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + var separatorIndex = trimmed.IndexOf('='); |
| 64 | + if (separatorIndex <= 0) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + var key = trimmed[..separatorIndex].Trim(); |
| 70 | + if (!string.Equals(key, "model", StringComparison.Ordinal)) |
| 71 | + { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + var rawValue = trimmed[(separatorIndex + 1)..].Trim(); |
| 76 | + if (rawValue.Length >= 2 |
| 77 | + && rawValue.StartsWith('"') |
| 78 | + && rawValue.EndsWith('"')) |
| 79 | + { |
| 80 | + rawValue = rawValue[1..^1]; |
| 81 | + } |
| 82 | + |
| 83 | + return string.IsNullOrWhiteSpace(rawValue) |
| 84 | + ? null |
| 85 | + : rawValue; |
| 86 | + } |
| 87 | + } |
| 88 | + catch (IOException exception) |
| 89 | + { |
| 90 | + throw new InvalidOperationException($"Failed to read Codex config at '{configPath}'.", exception); |
| 91 | + } |
| 92 | + catch (UnauthorizedAccessException exception) |
| 93 | + { |
| 94 | + throw new InvalidOperationException($"Failed to read Codex config at '{configPath}'.", exception); |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + private static string? GetCodexConfigPath() |
| 101 | + { |
| 102 | + var homeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| 103 | + if (string.IsNullOrWhiteSpace(homeDirectory)) |
| 104 | + { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + return Path.Combine(homeDirectory, ".codex", "config.toml"); |
| 109 | + } |
| 110 | + |
| 111 | + private static bool IsCodexAvailable() |
| 112 | + { |
| 113 | + var resolvedPath = CodexCliLocator.FindCodexPath(null); |
| 114 | + if (Path.IsPathRooted(resolvedPath)) |
| 115 | + { |
| 116 | + return File.Exists(resolvedPath); |
| 117 | + } |
| 118 | + |
| 119 | + return CodexCliLocator.TryResolvePathExecutable( |
| 120 | + Environment.GetEnvironmentVariable("PATH"), |
| 121 | + OperatingSystem.IsWindows(), |
| 122 | + out _); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +internal sealed record RealCodexTestSettings(string Model); |
0 commit comments