|
| 1 | +using System.Reflection; |
| 2 | +using System.Text.Json; |
| 3 | +using ManagedCode.CodexSharpSDK.Models; |
| 4 | + |
| 5 | +namespace ManagedCode.CodexSharpSDK.Tests.Unit; |
| 6 | + |
| 7 | +public class CodexFeaturesTests |
| 8 | +{ |
| 9 | + private const string SolutionFileName = "ManagedCode.CodexSharpSDK.slnx"; |
| 10 | + private const string BundledConfigSchemaFileName = "config.schema.json"; |
| 11 | + |
| 12 | + [Test] |
| 13 | + public async Task CodexFeatures_NewUpstreamFlags_ArePresent() |
| 14 | + { |
| 15 | + // These two flags were added in upstream commit 3b5fe5c and are the primary |
| 16 | + // motivation for this sync. Verify the constants are present and map to the correct |
| 17 | + // upstream key strings by resolving them via reflection (avoiding a constant-vs-constant |
| 18 | + // comparison that the analyzer rightly flags as a no-op assertion). |
| 19 | + var sdkValues = GetSdkFeatureValues(); |
| 20 | + await Assert.That(sdkValues).Contains("guardian_approval"); |
| 21 | + await Assert.That(sdkValues).Contains("tool_call_mcp_elicitation"); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public async Task CodexFeatures_AllConstantsAreValidUpstreamFeatureKeys() |
| 26 | + { |
| 27 | + var schemaFeatureKeys = await ReadBundledSchemaFeatureKeysAsync(); |
| 28 | + var sdkFeatureValues = GetSdkFeatureValues(); |
| 29 | + var invalidKeys = sdkFeatureValues |
| 30 | + .Except(schemaFeatureKeys, StringComparer.Ordinal) |
| 31 | + .ToArray(); |
| 32 | + |
| 33 | + await Assert.That(invalidKeys).IsEmpty(); |
| 34 | + } |
| 35 | + |
| 36 | + [Test] |
| 37 | + public async Task CodexFeatures_CoversAllCanonicalUpstreamFeatureKeys() |
| 38 | + { |
| 39 | + // The canonical (non-alias) keys from features.rs must all have an SDK constant so |
| 40 | + // that callers can reference them without magic strings. |
| 41 | + var schemaFeatureKeys = await ReadBundledSchemaFeatureKeysAsync(); |
| 42 | + var sdkFeatureValues = GetSdkFeatureValues(); |
| 43 | + |
| 44 | + // Legacy alias keys that exist in config.schema.json but are NOT canonical feature |
| 45 | + // keys in features.rs; they are intentionally excluded from CodexFeatures. |
| 46 | + var knownAliases = new HashSet<string>(StringComparer.Ordinal) |
| 47 | + { |
| 48 | + "collab", |
| 49 | + "connectors", |
| 50 | + "enable_experimental_windows_sandbox", |
| 51 | + "experimental_use_freeform_apply_patch", |
| 52 | + "experimental_use_unified_exec_tool", |
| 53 | + "include_apply_patch_tool", |
| 54 | + "memory_tool", |
| 55 | + "web_search", |
| 56 | + }; |
| 57 | + |
| 58 | + var canonicalKeys = schemaFeatureKeys |
| 59 | + .Except(knownAliases, StringComparer.Ordinal) |
| 60 | + .ToArray(); |
| 61 | + |
| 62 | + var missingKeys = canonicalKeys |
| 63 | + .Except(sdkFeatureValues, StringComparer.Ordinal) |
| 64 | + .ToArray(); |
| 65 | + |
| 66 | + await Assert.That(missingKeys).IsEmpty(); |
| 67 | + } |
| 68 | + |
| 69 | + private static string[] GetSdkFeatureValues() |
| 70 | + { |
| 71 | + return typeof(CodexFeatures) |
| 72 | + .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly) |
| 73 | + .Where(field => field is { IsLiteral: true, IsInitOnly: false } && field.FieldType == typeof(string)) |
| 74 | + .Select(field => (string)field.GetRawConstantValue()!) |
| 75 | + .ToArray(); |
| 76 | + } |
| 77 | + |
| 78 | + private static async Task<string[]> ReadBundledSchemaFeatureKeysAsync() |
| 79 | + { |
| 80 | + var schemaPath = ResolveBundledConfigSchemaFilePath(); |
| 81 | + using var stream = File.OpenRead(schemaPath); |
| 82 | + using var document = await JsonDocument.ParseAsync(stream); |
| 83 | + |
| 84 | + return document.RootElement |
| 85 | + .GetProperty("properties") |
| 86 | + .GetProperty("features") |
| 87 | + .GetProperty("properties") |
| 88 | + .EnumerateObject() |
| 89 | + .Select(p => p.Name) |
| 90 | + .ToArray(); |
| 91 | + } |
| 92 | + |
| 93 | + private static string ResolveBundledConfigSchemaFilePath() |
| 94 | + { |
| 95 | + return Path.Combine( |
| 96 | + ResolveRepositoryRootPath(), |
| 97 | + "submodules", |
| 98 | + "openai-codex", |
| 99 | + "codex-rs", |
| 100 | + "core", |
| 101 | + BundledConfigSchemaFileName); |
| 102 | + } |
| 103 | + |
| 104 | + private static string ResolveRepositoryRootPath() |
| 105 | + { |
| 106 | + var current = new DirectoryInfo(AppContext.BaseDirectory); |
| 107 | + while (current is not null) |
| 108 | + { |
| 109 | + if (File.Exists(Path.Combine(current.FullName, SolutionFileName))) |
| 110 | + { |
| 111 | + return current.FullName; |
| 112 | + } |
| 113 | + |
| 114 | + current = current.Parent; |
| 115 | + } |
| 116 | + |
| 117 | + throw new InvalidOperationException("Could not locate repository root from test execution directory."); |
| 118 | + } |
| 119 | +} |
0 commit comments