|
| 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 CodexModelsTests |
| 8 | +{ |
| 9 | + private const string SolutionFileName = "ManagedCode.CodexSharpSDK.slnx"; |
| 10 | + private const string BundledModelsFileName = "models.json"; |
| 11 | + |
| 12 | + [Test] |
| 13 | + public async Task CodexModels_ContainAllBundledUpstreamModelSlugs() |
| 14 | + { |
| 15 | + var bundledModelSlugs = await ReadBundledModelSlugsAsync(); |
| 16 | + var sdkModelSlugs = GetSdkModelSlugs(); |
| 17 | + var missingBundledSlugs = bundledModelSlugs |
| 18 | + .Except(sdkModelSlugs, StringComparer.Ordinal) |
| 19 | + .ToArray(); |
| 20 | + |
| 21 | + await Assert.That(missingBundledSlugs).IsEmpty(); |
| 22 | + } |
| 23 | + |
| 24 | + private static string[] GetSdkModelSlugs() |
| 25 | + { |
| 26 | + return typeof(CodexModels) |
| 27 | + .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly) |
| 28 | + .Where(field => field is { IsLiteral: true, IsInitOnly: false, FieldType: not null } && field.FieldType == typeof(string)) |
| 29 | + .Select(field => (string)field.GetRawConstantValue()!) |
| 30 | + .ToArray(); |
| 31 | + } |
| 32 | + |
| 33 | + private static async Task<string[]> ReadBundledModelSlugsAsync() |
| 34 | + { |
| 35 | + var modelsPath = ResolveBundledModelsFilePath(); |
| 36 | + using var stream = File.OpenRead(modelsPath); |
| 37 | + using var document = await JsonDocument.ParseAsync(stream); |
| 38 | + |
| 39 | + return document.RootElement |
| 40 | + .GetProperty("models") |
| 41 | + .EnumerateArray() |
| 42 | + .Select(model => model.GetProperty("slug").GetString()) |
| 43 | + .OfType<string>() |
| 44 | + .ToArray(); |
| 45 | + } |
| 46 | + |
| 47 | + private static string ResolveBundledModelsFilePath() |
| 48 | + { |
| 49 | + return Path.Combine( |
| 50 | + ResolveRepositoryRootPath(), |
| 51 | + "submodules", |
| 52 | + "openai-codex", |
| 53 | + "codex-rs", |
| 54 | + "core", |
| 55 | + BundledModelsFileName); |
| 56 | + } |
| 57 | + |
| 58 | + private static string ResolveRepositoryRootPath() |
| 59 | + { |
| 60 | + var current = new DirectoryInfo(AppContext.BaseDirectory); |
| 61 | + while (current is not null) |
| 62 | + { |
| 63 | + if (File.Exists(Path.Combine(current.FullName, SolutionFileName))) |
| 64 | + { |
| 65 | + return current.FullName; |
| 66 | + } |
| 67 | + |
| 68 | + current = current.Parent; |
| 69 | + } |
| 70 | + |
| 71 | + throw new InvalidOperationException("Could not locate repository root from test execution directory."); |
| 72 | + } |
| 73 | +} |
0 commit comments