-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathLanguageListCommandTests.cs
More file actions
204 lines (183 loc) · 8.43 KB
/
Copy pathLanguageListCommandTests.cs
File metadata and controls
204 lines (183 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Net;
using Azure.Mcp.Tools.Functions.Commands;
using Azure.Mcp.Tools.Functions.Commands.Language;
using Azure.Mcp.Tools.Functions.Models;
using Azure.Mcp.Tools.Functions.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Services.Caching;
using Microsoft.Mcp.Tests.Client;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
namespace Azure.Mcp.Tools.Functions.Tests.Language;
public sealed class LanguageListCommandTests : CommandUnitTestsBase<LanguageListCommand, IFunctionsService>
{
[Fact]
public void Constructor_InitializesCommandCorrectly()
{
Assert.Equal("list", Command.Name);
Assert.NotEmpty(Command.Description);
Assert.Equal("List Supported Languages", Command.Title);
}
[Fact]
public void Command_HasCorrectMetadata()
{
Assert.False(Command.Metadata.Destructive);
Assert.True(Command.Metadata.Idempotent);
Assert.False(Command.Metadata.OpenWorld);
Assert.True(Command.Metadata.ReadOnly);
Assert.False(Command.Metadata.LocalRequired);
Assert.False(Command.Metadata.Secret);
}
[Fact]
public async Task ExecuteAsync_ReturnsLanguageList()
{
// Arrange
var expectedResult = new LanguageListResult
{
FunctionsRuntimeVersion = "4.x",
ExtensionBundleVersion = "[4.*, 5.0.0)",
Languages =
[
new()
{
Language = "python",
Info = new()
{
Name = "Python",
Runtime = "python",
ProgrammingModel = "v2 (Decorator-based)",
Prerequisites = ["Python 3.10+", "Azure Functions Core Tools v4"],
DevelopmentTools = ["VS Code with Azure Functions extension", "Azure Functions Core Tools"],
InitCommand = "func init --worker-runtime python --model V2",
RunCommand = "func start",
BuildCommand = null,
ProjectFiles = ["requirements.txt"],
RuntimeVersions = new()
{
Supported = ["3.10", "3.11", "3.12", "3.13"],
Preview = ["3.14"],
Default = "3.11"
},
InitInstructions = "Test instructions",
ProjectStructure = ["function_app.py"]
},
RuntimeVersions = new()
{
Supported = ["3.10", "3.11", "3.12", "3.13"],
Preview = ["3.14"],
Default = "3.11"
}
},
new()
{
Language = "csharp",
Info = new()
{
Name = "C#",
Runtime = "dotnet",
ProgrammingModel = "Isolated worker process",
Prerequisites = [".NET 8 SDK or later", "Azure Functions Core Tools v4"],
DevelopmentTools = ["Visual Studio 2022", "VS Code with C# + Azure Functions extensions", "Azure Functions Core Tools"],
InitCommand = "func init --worker-runtime dotnet-isolated",
RunCommand = "func start",
BuildCommand = "dotnet build",
ProjectFiles = [],
RuntimeVersions = new()
{
Supported = ["8", "9", "10"],
Deprecated = ["6", "7"],
Default = "8",
FrameworkSupported = ["4.8.1"]
},
InitInstructions = "Test instructions",
ProjectStructure = ["*.csproj"]
},
RuntimeVersions = new()
{
Supported = ["8", "9", "10"],
Deprecated = ["6", "7"],
Default = "8",
FrameworkSupported = ["4.8.1"]
}
}
]
};
Service.GetLanguageListAsync(Arg.Any<CancellationToken>()).Returns(expectedResult);
// Act
var response = await ExecuteCommandAsync();
// Assert
var results = ValidateAndDeserializeResponse(response, FunctionsJsonContext.Default.ListLanguageListResult);
Assert.Single(results);
var result = results[0];
Assert.Equal("4.x", result.FunctionsRuntimeVersion);
Assert.Equal("[4.*, 5.0.0)", result.ExtensionBundleVersion);
Assert.Equal(2, result.Languages.Count);
Assert.Equal("python", result.Languages[0].Language);
Assert.Equal("Python", result.Languages[0].Info.Name);
Assert.Equal("3.11", result.Languages[0].RuntimeVersions.Default);
Assert.Equal("csharp", result.Languages[1].Language);
Assert.Equal("C#", result.Languages[1].Info.Name);
}
[Fact]
public async Task ExecuteAsync_HandlesServiceErrors()
{
// Arrange
Service.GetLanguageListAsync(Arg.Any<CancellationToken>()).ThrowsAsync(new InvalidOperationException("Service error"));
// Act
var response = await ExecuteCommandAsync();
// Assert
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.Status);
Assert.Contains("Service error", response.Message);
}
[Fact]
public async Task ExecuteAsync_DeserializationValidation()
{
// Arrange - use the real service to verify actual data shape
// GetLanguageListAsync now fetches manifest for runtime versions
var mockManifestService = Substitute.For<IManifestService>();
// Set up manifest with runtime versions
var manifest = new TemplateManifest
{
RuntimeVersions = new Dictionary<string, RuntimeVersionInfo>
{
["Python"] = new RuntimeVersionInfo { Supported = ["3.10", "3.11", "3.12", "3.13"], Preview = ["3.14"], Default = "3.11" },
["TypeScript"] = new RuntimeVersionInfo { Supported = ["20", "22"], Preview = ["24"], Default = "22" },
["JavaScript"] = new RuntimeVersionInfo { Supported = ["20", "22"], Preview = ["24"], Default = "22" },
["Java"] = new RuntimeVersionInfo { Supported = ["8", "11", "17", "21"], Preview = ["25"], Default = "21" },
["CSharp"] = new RuntimeVersionInfo { Supported = ["8", "9", "10"], FrameworkSupported = ["4.8.1"], Default = "8" },
["PowerShell"] = new RuntimeVersionInfo { Supported = ["7.4"], Default = "7.4" },
["Go"] = new RuntimeVersionInfo { Supported = [], Preview = ["1.0"], Default = "1.0" }
}
};
mockManifestService.FetchManifestAsync(Arg.Any<CancellationToken>()).Returns(manifest);
var realService = new FunctionsService(
Substitute.For<IHttpClientFactory>(),
new LanguageMetadataProvider(),
mockManifestService,
Substitute.For<ICacheService>(),
Substitute.For<ILogger<FunctionsService>>());
var realResult = await realService.GetLanguageListAsync(TestContext.Current.CancellationToken);
Service.GetLanguageListAsync(Arg.Any<CancellationToken>()).Returns(realResult);
// Act
var response = await ExecuteCommandAsync();
// Assert
var results = ValidateAndDeserializeResponse(response, FunctionsJsonContext.Default.ListLanguageListResult);
Assert.Single(results);
var result = results[0];
Assert.Equal("4.x", result.FunctionsRuntimeVersion);
Assert.Equal(7, result.Languages.Count);
// Verify all expected languages are present
var languageNames = result.Languages.Select(l => l.Language).ToList();
Assert.Contains("python", languageNames);
Assert.Contains("typescript", languageNames);
Assert.Contains("javascript", languageNames);
Assert.Contains("java", languageNames);
Assert.Contains("csharp", languageNames);
Assert.Contains("powershell", languageNames);
Assert.Contains("go", languageNames);
}
}