From 68c4dac6f1857550d96ed615ee09999d61ffc3a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Thu, 6 Apr 2023 17:05:39 +0200 Subject: [PATCH] Adding integration tests for service description parsing in code generator --- .../GlobalUsings.cs | 6 ++- .../CodegenIntegrationTests.cs | 43 +++++++++++++++++++ .../NetDaemon.Tests.Integration.csproj | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/NetDaemon.Tests.Integration/CodegenIntegrationTests.cs diff --git a/src/HassModel/NetDaemon.HassModel.CodeGenerator/GlobalUsings.cs b/src/HassModel/NetDaemon.HassModel.CodeGenerator/GlobalUsings.cs index 713ecf8b6..465069bed 100644 --- a/src/HassModel/NetDaemon.HassModel.CodeGenerator/GlobalUsings.cs +++ b/src/HassModel/NetDaemon.HassModel.CodeGenerator/GlobalUsings.cs @@ -10,4 +10,8 @@ global using NetDaemon.HassModel.Entities; global using Microsoft.CodeAnalysis; global using static NetDaemon.HassModel.CodeGenerator.Helpers.NamingHelper; -global using static NetDaemon.HassModel.CodeGenerator.Helpers.SyntaxFactoryHelper; \ No newline at end of file +global using static NetDaemon.HassModel.CodeGenerator.Helpers.SyntaxFactoryHelper; + +// This is needed to allow integration tests to run code generation and parsing without major refactoring +using System.Runtime.CompilerServices; +[assembly:InternalsVisibleTo("NetDaemon.Tests.Integration")] diff --git a/tests/Integration/NetDaemon.Tests.Integration/CodegenIntegrationTests.cs b/tests/Integration/NetDaemon.Tests.Integration/CodegenIntegrationTests.cs new file mode 100644 index 000000000..e14a7d042 --- /dev/null +++ b/tests/Integration/NetDaemon.Tests.Integration/CodegenIntegrationTests.cs @@ -0,0 +1,43 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using FluentAssertions; +using NetDaemon.Client; +using NetDaemon.Client.HomeAssistant.Extensions; +using NetDaemon.HassModel.CodeGenerator.Model; +using NetDaemon.Tests.Integration.Helpers; +using Xunit; + +namespace NetDaemon.Tests.Integration; + +public class CodegenIntegrationTests : IClassFixture +{ + private readonly IHomeAssistantConnection _haConnection; + + public CodegenIntegrationTests( + MakeSureNetDaemonIsRunningFixture _, + IHomeAssistantConnection haConnection + ) + { + _haConnection = haConnection; + } + + /// + /// Tests the code generator. We had som problems with websocket interface changing and this should at least allert us on changes when it reaches + /// beta stage + /// + [Fact] + public async Task Codegen_ShouldBeAbleToParseServiceDescriptions() + { + var element = await _haConnection.GetServicesAsync(new CancellationTokenSource(TimeSpan.FromSeconds(20)).Token).ConfigureAwait(false) ?? throw new InvalidOperationException("Failed to get services"); + var serviceMetadata = ServiceMetaDataParser.Parse(element); + serviceMetadata.Count.Should().NotBe(0); + + var lightDomain = serviceMetadata.FirstOrDefault(n => n.Domain == "switch") ?? throw new InvalidOperationException("Expected domain light to be present"); + + var turnOnService = lightDomain.Services.FirstOrDefault(n => n.Service == "turn_on") ?? throw new InvalidOperationException("Expected domain light to be present"); + + Assert.NotNull(turnOnService?.Target?.Entity); + } +} \ No newline at end of file diff --git a/tests/Integration/NetDaemon.Tests.Integration/NetDaemon.Tests.Integration.csproj b/tests/Integration/NetDaemon.Tests.Integration/NetDaemon.Tests.Integration.csproj index 42dcc28a3..70dbd4fff 100644 --- a/tests/Integration/NetDaemon.Tests.Integration/NetDaemon.Tests.Integration.csproj +++ b/tests/Integration/NetDaemon.Tests.Integration/NetDaemon.Tests.Integration.csproj @@ -25,6 +25,7 @@ +