From b448cfec3ca891d3ae415f84e811ad587da67729 Mon Sep 17 00:00:00 2001
From: Heavy Bob <ferrettclay@gmail.com>
Date: Thu, 10 Apr 2025 03:16:42 +1000
Subject: [PATCH] Point streamlink test to /test

Streamlink tests now throw at the /test api and expect a streamer variable, if nothing then it will succeed. Think the streamer link stuff is mostly done, just need to do the changes on the api.
---
 AutoTrackR2/ConfigPage.xaml.cs | 55 +++++++++++++++++++++++++++++-----
 AutoTrackR2/WebHandler.cs      |  2 +-
 2 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/AutoTrackR2/ConfigPage.xaml.cs b/AutoTrackR2/ConfigPage.xaml.cs
index 284f835..e4c1772 100644
--- a/AutoTrackR2/ConfigPage.xaml.cs
+++ b/AutoTrackR2/ConfigPage.xaml.cs
@@ -477,20 +477,61 @@ public partial class ConfigPage : UserControl
 
     private async void TestStreamlinkButton_Click(object sender, RoutedEventArgs e)
     {
+        Debug.WriteLine("TestStreamlinkButton_Click: Starting streamlink test");
+
+        if (ConfigManager.StreamlinkEnabled != 1)
+        {
+            Debug.WriteLine("TestStreamlinkButton_Click: Streamlink is not enabled");
+            MessageBox.Show("Streamlink is not enabled. Please enable Streamlink to test.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
+            return;
+        }
+
         try
         {
-            // Get the duration from the slider
-            int duration = (int)StreamlinkDurationSlider.Value;
+            Debug.WriteLine("TestStreamlinkButton_Click: Configuring HTTP client");
+            // Configure HttpClient with TLS 1.2
+            var handler = new HttpClientHandler
+            {
+                SslProtocols = System.Security.Authentication.SslProtocols.Tls12
+            };
 
-            // Calculate the total duration (30 seconds before + configured duration)
-            int totalDuration = 30 + duration;
+            using (var client = new HttpClient(handler))
+            {
+                // Set headers
+                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ConfigManager.ApiKey);
+                client.DefaultRequestHeaders.UserAgent.ParseAdd("AutoTrackR2");
 
-            // Test recording functionality, we're using spacecutlet cause need a good tester.
-            TrackREventDispatcher.OnStreamlinkRecordEvent("spacecutlet");
+                // Create JSON body with version
+                var jsonBody = new { version = "2.10" };
+                var content = new StringContent(JsonSerializer.Serialize(jsonBody), Encoding.UTF8, "application/json");
+
+                // Send POST to test endpoint
+                string baseUrl = Regex.Replace(ConfigManager.ApiUrl ?? "", @"(https?://[^/]+)/?.*", "$1");
+                string endpoint = "test";
+                string fullUrl = $"{baseUrl}/{endpoint}";
+                Debug.WriteLine($"TestStreamlinkButton_Click: Sending request to {fullUrl}");
+
+                var response = await client.PostAsync(fullUrl, content);
+                Debug.WriteLine($"TestStreamlinkButton_Click: Response status code: {response.StatusCode}");
+
+                if (response.IsSuccessStatusCode)
+                {
+                    var responseContent = await response.Content.ReadAsStringAsync();
+                    Debug.WriteLine($"TestStreamlinkButton_Click: Response content: {responseContent}");
+                    WebHandler.ProcessStreamerResponse(responseContent);
+                    MessageBox.Show("Streamlink Test Success.");
+                }
+                else
+                {
+                    Debug.WriteLine($"TestStreamlinkButton_Click: Error response: {response.StatusCode} - {response.ReasonPhrase}");
+                    MessageBox.Show($"Error: {response.StatusCode} - {response.ReasonPhrase}");
+                }
+            }
         }
         catch (Exception ex)
         {
-            MessageBox.Show($"Error testing Streamlink: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
+            Debug.WriteLine($"TestStreamlinkButton_Click: Exception: {ex.Message}");
+            MessageBox.Show($"Streamlink Test Failure. {ex.Message}");
         }
     }
 
diff --git a/AutoTrackR2/WebHandler.cs b/AutoTrackR2/WebHandler.cs
index b05a1a8..7015282 100644
--- a/AutoTrackR2/WebHandler.cs
+++ b/AutoTrackR2/WebHandler.cs
@@ -183,7 +183,7 @@ public static class WebHandler
         }
     }
 
-    private static void ProcessStreamerResponse(string responseContent)
+    public static void ProcessStreamerResponse(string responseContent)
     {
         try
         {