|
| 1 | +using System.Text.Json.Nodes; |
| 2 | +using kapacitor.Commands; |
| 3 | + |
| 4 | +namespace kapacitor.Tests.Unit; |
| 5 | + |
| 6 | +public class SetupCommandTests { |
| 7 | + [Test] |
| 8 | + public async Task InstallPlugin_CreatesNewSettingsFile() { |
| 9 | + using var tmp = new TempDir(); |
| 10 | + var settingsPath = Path.Combine(tmp.Path, "settings.json"); |
| 11 | + var marketplace = "/opt/kapacitor"; |
| 12 | + |
| 13 | + var result = SetupCommand.InstallPlugin(settingsPath, marketplace); |
| 14 | + |
| 15 | + await Assert.That(result).IsTrue(); |
| 16 | + |
| 17 | + var root = JsonNode.Parse(File.ReadAllText(settingsPath))!.AsObject(); |
| 18 | + |
| 19 | + await Assert.That(root["extraKnownMarketplaces"]?["kurrent"]?["source"]?["path"]?.GetValue<string>()) |
| 20 | + .IsEqualTo(marketplace); |
| 21 | + |
| 22 | + await Assert.That(root["enabledPlugins"]?["kapacitor@kurrent"]?.GetValue<bool>()) |
| 23 | + .IsEqualTo(true); |
| 24 | + } |
| 25 | + |
| 26 | + [Test] |
| 27 | + public async Task InstallPlugin_PreservesExistingSettings() { |
| 28 | + using var tmp = new TempDir(); |
| 29 | + var settingsPath = Path.Combine(tmp.Path, "settings.json"); |
| 30 | + var marketplace = "/opt/kapacitor"; |
| 31 | + |
| 32 | + // Pre-populate with existing settings |
| 33 | + File.WriteAllText(settingsPath, """ |
| 34 | + { |
| 35 | + "permissions": { "allow": ["Bash"] }, |
| 36 | + "enabledPlugins": { "other-plugin@foo": true } |
| 37 | + } |
| 38 | + """); |
| 39 | + |
| 40 | + var result = SetupCommand.InstallPlugin(settingsPath, marketplace); |
| 41 | + |
| 42 | + await Assert.That(result).IsTrue(); |
| 43 | + |
| 44 | + var root = JsonNode.Parse(File.ReadAllText(settingsPath))!.AsObject(); |
| 45 | + |
| 46 | + // Original settings preserved |
| 47 | + await Assert.That(root["permissions"]?["allow"]?[0]?.GetValue<string>()) |
| 48 | + .IsEqualTo("Bash"); |
| 49 | + |
| 50 | + await Assert.That(root["enabledPlugins"]?["other-plugin@foo"]?.GetValue<bool>()) |
| 51 | + .IsEqualTo(true); |
| 52 | + |
| 53 | + // Plugin added |
| 54 | + await Assert.That(root["enabledPlugins"]?["kapacitor@kurrent"]?.GetValue<bool>()) |
| 55 | + .IsEqualTo(true); |
| 56 | + |
| 57 | + await Assert.That(root["extraKnownMarketplaces"]?["kurrent"]?["source"]?["path"]?.GetValue<string>()) |
| 58 | + .IsEqualTo(marketplace); |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public async Task InstallPlugin_UpdatesExistingMarketplacePath() { |
| 63 | + using var tmp = new TempDir(); |
| 64 | + var settingsPath = Path.Combine(tmp.Path, "settings.json"); |
| 65 | + var newPath = "/new/path"; |
| 66 | + |
| 67 | + // Pre-populate with old marketplace path |
| 68 | + File.WriteAllText(settingsPath, """ |
| 69 | + { |
| 70 | + "extraKnownMarketplaces": { |
| 71 | + "kurrent": { "source": { "source": "directory", "path": "/old/path" } } |
| 72 | + }, |
| 73 | + "enabledPlugins": { "kapacitor@kurrent": true } |
| 74 | + } |
| 75 | + """); |
| 76 | + |
| 77 | + var result = SetupCommand.InstallPlugin(settingsPath, newPath); |
| 78 | + |
| 79 | + await Assert.That(result).IsTrue(); |
| 80 | + |
| 81 | + var root = JsonNode.Parse(File.ReadAllText(settingsPath))!.AsObject(); |
| 82 | + |
| 83 | + await Assert.That(root["extraKnownMarketplaces"]?["kurrent"]?["source"]?["path"]?.GetValue<string>()) |
| 84 | + .IsEqualTo(newPath); |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public async Task InstallPlugin_CreatesIntermediateDirectories() { |
| 89 | + using var tmp = new TempDir(); |
| 90 | + var settingsPath = Path.Combine(tmp.Path, ".claude", "nested", "settings.json"); |
| 91 | + var marketplace = "/opt/kapacitor"; |
| 92 | + |
| 93 | + var result = SetupCommand.InstallPlugin(settingsPath, marketplace); |
| 94 | + |
| 95 | + await Assert.That(result).IsTrue(); |
| 96 | + await Assert.That(File.Exists(settingsPath)).IsTrue(); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public async Task InstallPlugin_MalformedJson_StartsFromScratch() { |
| 101 | + using var tmp = new TempDir(); |
| 102 | + var settingsPath = Path.Combine(tmp.Path, "settings.json"); |
| 103 | + var marketplace = "/opt/kapacitor"; |
| 104 | + |
| 105 | + File.WriteAllText(settingsPath, "not json {{{"); |
| 106 | + |
| 107 | + var result = SetupCommand.InstallPlugin(settingsPath, marketplace); |
| 108 | + |
| 109 | + await Assert.That(result).IsTrue(); |
| 110 | + |
| 111 | + var root = JsonNode.Parse(File.ReadAllText(settingsPath))!.AsObject(); |
| 112 | + |
| 113 | + await Assert.That(root["enabledPlugins"]?["kapacitor@kurrent"]?.GetValue<bool>()) |
| 114 | + .IsEqualTo(true); |
| 115 | + } |
| 116 | + |
| 117 | + sealed class TempDir : IDisposable { |
| 118 | + public string Path { get; } = System.IO.Path.Combine( |
| 119 | + System.IO.Path.GetTempPath(), |
| 120 | + "kapacitor-test-" + Guid.NewGuid().ToString("N")[..8] |
| 121 | + ); |
| 122 | + |
| 123 | + public TempDir() => Directory.CreateDirectory(Path); |
| 124 | + |
| 125 | + public void Dispose() { |
| 126 | + try { Directory.Delete(Path, true); } catch { /* best effort */ } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments