|
| 1 | +using System.Text.Json; |
| 2 | + |
| 3 | +namespace OpenClaw.SetupEngine.Tests; |
| 4 | + |
| 5 | +public class WizardDefaultAnswerMatrixTests |
| 6 | +{ |
| 7 | + private static readonly string[] ProviderOptions = |
| 8 | + [ |
| 9 | + "skip", |
| 10 | + "github-copilot", |
| 11 | + "lmstudio", |
| 12 | + "lm-studio", |
| 13 | + "openai", |
| 14 | + "azure-openai", |
| 15 | + ]; |
| 16 | + |
| 17 | + private static readonly string[] ChannelOptions = |
| 18 | + [ |
| 19 | + "__skip__", |
| 20 | + "teams", |
| 21 | + "telegram", |
| 22 | + "discord", |
| 23 | + "whatsapp", |
| 24 | + ]; |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void DefaultConfig_CoversProviderChannelAndSearchWizardSteps() |
| 28 | + { |
| 29 | + var answers = LoadDefaultWizardAnswers(); |
| 30 | + |
| 31 | + AssertConfiguredSelect(answers, "model-auth-provider", ProviderOptions); |
| 32 | + AssertConfiguredSelect(answers, "default-model", ["__keep__", "gpt-5.5", "gpt-5.4", "claude-sonnet-4.6"]); |
| 33 | + AssertConfiguredMultiselect(answers, "select-channel-quickstart", ChannelOptions); |
| 34 | + AssertConfiguredSelect(answers, "search-provider", ["__skip__", "tavily", "brave", "bing"]); |
| 35 | + |
| 36 | + Assert.True(answers.TryGetValue("configure-skills-now-recommended", out var configureSkills)); |
| 37 | + Assert.False((bool)WizardAnswerBuilder.BuildWireValue("confirm", configureSkills, [])); |
| 38 | + } |
| 39 | + |
| 40 | + [Theory] |
| 41 | + [MemberData(nameof(RepresentativeProviderChannelMatrix))] |
| 42 | + public void WizardAnswerBuilder_ValidatesRepresentativeProviderChannelCombinations( |
| 43 | + string provider, |
| 44 | + string channels) |
| 45 | + { |
| 46 | + var providerOptions = Options(ProviderOptions); |
| 47 | + var channelOptions = Options(ChannelOptions); |
| 48 | + |
| 49 | + Assert.True(WizardAnswerBuilder.TryFindOption(providerOptions, provider, out _)); |
| 50 | + Assert.True(WizardAnswerBuilder.TryResolveOptions(channelOptions, channels, out var selectedChannels)); |
| 51 | + Assert.NotEmpty(selectedChannels); |
| 52 | + |
| 53 | + var providerWire = WizardAnswerBuilder.BuildWireValue("select", provider, providerOptions); |
| 54 | + var channelsWire = WizardAnswerBuilder.BuildWireValue("multiselect", channels, channelOptions); |
| 55 | + |
| 56 | + Assert.Equal(JsonSerializer.Serialize(provider), JsonSerializer.Serialize(providerWire)); |
| 57 | + Assert.StartsWith("[", JsonSerializer.Serialize(channelsWire), StringComparison.Ordinal); |
| 58 | + } |
| 59 | + |
| 60 | + public static IEnumerable<object[]> RepresentativeProviderChannelMatrix() |
| 61 | + { |
| 62 | + foreach (var provider in ProviderOptions) |
| 63 | + { |
| 64 | + yield return [provider, "__skip__"]; |
| 65 | + yield return [provider, "teams"]; |
| 66 | + yield return [provider, "telegram"]; |
| 67 | + yield return [provider, "discord,whatsapp"]; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static Dictionary<string, string> LoadDefaultWizardAnswers() |
| 72 | + { |
| 73 | + var configPath = Path.Combine(RepositoryRoot(), "src", "OpenClaw.SetupEngine", "default-config.json"); |
| 74 | + var config = SetupConfig.LoadFromFile(configPath); |
| 75 | + return config.WizardAnswers ?? new Dictionary<string, string>(); |
| 76 | + } |
| 77 | + |
| 78 | + private static void AssertConfiguredSelect( |
| 79 | + Dictionary<string, string> answers, |
| 80 | + string key, |
| 81 | + IReadOnlyList<string> values) |
| 82 | + { |
| 83 | + Assert.True(answers.TryGetValue(key, out var answer), $"Missing WizardAnswers['{key}']"); |
| 84 | + Assert.True(WizardAnswerBuilder.TryFindOption(Options(values), answer, out _), |
| 85 | + $"WizardAnswers['{key}'] value '{answer}' was not present in representative options."); |
| 86 | + } |
| 87 | + |
| 88 | + private static void AssertConfiguredMultiselect( |
| 89 | + Dictionary<string, string> answers, |
| 90 | + string key, |
| 91 | + IReadOnlyList<string> values) |
| 92 | + { |
| 93 | + Assert.True(answers.TryGetValue(key, out var answer), $"Missing WizardAnswers['{key}']"); |
| 94 | + Assert.True(WizardAnswerBuilder.TryResolveOptions(Options(values), answer, out var selected), |
| 95 | + $"WizardAnswers['{key}'] value '{answer}' could not be resolved from representative options."); |
| 96 | + Assert.NotEmpty(selected); |
| 97 | + } |
| 98 | + |
| 99 | + private static IReadOnlyList<WizardOptionValue> Options(IReadOnlyList<string> values) |
| 100 | + { |
| 101 | + var json = JsonSerializer.Serialize(new |
| 102 | + { |
| 103 | + options = values.Select(value => new { label = value, value }) |
| 104 | + }); |
| 105 | + using var document = JsonDocument.Parse(json); |
| 106 | + return WizardAnswerBuilder.ReadOptions(document.RootElement); |
| 107 | + } |
| 108 | + |
| 109 | + private static string RepositoryRoot() |
| 110 | + { |
| 111 | + if (Environment.GetEnvironmentVariable("OPENCLAW_REPO_ROOT") is { Length: > 0 } configured) |
| 112 | + return configured; |
| 113 | + |
| 114 | + var directory = AppContext.BaseDirectory; |
| 115 | + while (!string.IsNullOrWhiteSpace(directory)) |
| 116 | + { |
| 117 | + if (File.Exists(Path.Combine(directory, "src", "OpenClaw.SetupEngine", "default-config.json"))) |
| 118 | + return directory; |
| 119 | + |
| 120 | + directory = Directory.GetParent(directory)?.FullName; |
| 121 | + } |
| 122 | + |
| 123 | + throw new DirectoryNotFoundException("Could not locate repository root for default-config.json."); |
| 124 | + } |
| 125 | +} |
0 commit comments