diff --git a/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs b/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs index e5fdd14e..e296a7a2 100644 --- a/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs +++ b/runtime/promptycs/Prompty.Core.Tests/LoadTests.cs @@ -1,3 +1,4 @@ +using System.Reflection; using System.Transactions; namespace Prompty.Core.Tests; @@ -36,6 +37,76 @@ public void LoadRawWithConfig(string path) Assert.Equal("FAKE_TYPE", prompty.Model?.Configuration.Type); } + /// + /// Test the Loading from a Stream + /// + [Fact] + public void LoadStream() + { + var path = "prompty/basic.prompty"; + using var stream = File.OpenRead(path); + var prompty = Prompty.Load(stream); + + Assert.NotNull(prompty); + Assert.NotNull(prompty.Content); + } + + /// + /// Test the Loading from an embedded resource + /// + [Fact] + public void LoadEmbeddedResource() + { + // Get the fully qualified name of the embedded resource + var assembly = Assembly.GetExecutingAssembly(); + var resourceNames = assembly.GetManifestResourceNames(); + var resourceName = resourceNames.FirstOrDefault(r => r.EndsWith("basic.prompty")); + + Assert.NotNull(resourceName); // Ensure we found the resource + + using var stream = assembly.GetManifestResourceStream(resourceName); + Assert.NotNull(stream); + var prompty = Prompty.Load(stream!); + + Assert.NotNull(prompty); + Assert.NotNull(prompty.Content); + } + + /// + /// Test the Loading from an embedded resource with config + /// + [Fact] + public void LoadEmbeddedResourceWithConfig() + { + // Get the fully qualified name of the embedded resource + var assembly = Assembly.GetExecutingAssembly(); + var resourceNames = assembly.GetManifestResourceNames(); + var resourceName = resourceNames.FirstOrDefault(r => r.EndsWith("basic.prompty")); + + Assert.NotNull(resourceName); // Ensure we found the resource + + using var stream = assembly.GetManifestResourceStream(resourceName); + Assert.NotNull(stream); + var prompty = Prompty.Load(stream!, "fake"); + + Assert.Equal("FAKE_TYPE", prompty.Model?.Configuration.Type); + } + + + /// + /// Test the Loading from a Stream Async + /// + [Fact] + public async Task LoadStreamAsync() + { + var path = "prompty/basic.prompty"; + using var stream = File.OpenRead(path); + var prompty = await Prompty.LoadAsync(stream); + + Assert.NotNull(prompty); + Assert.NotNull(prompty.Content); + } + [Fact] public void BasicSampleParameters() { diff --git a/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj b/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj index bb5a5b8b..e6a0505d 100644 --- a/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj +++ b/runtime/promptycs/Prompty.Core.Tests/Prompty.Core.Tests.csproj @@ -69,6 +69,7 @@ Always + Always diff --git a/runtime/promptycs/Prompty.Core/Prompty.cs b/runtime/promptycs/Prompty.Core/Prompty.cs index eccd0912..7170f375 100644 --- a/runtime/promptycs/Prompty.Core/Prompty.cs +++ b/runtime/promptycs/Prompty.Core/Prompty.cs @@ -181,6 +181,44 @@ public static async Task LoadAsync(string path, string configuration = return prompty; } + /// + /// Load a prompty file from a Stream + /// + /// Stream to read the prompty file from. + /// Id of the configuration to use. + public static Prompty Load(Stream stream, string configuration = "default") + { + using var reader = new StreamReader(stream); + string text = reader.ReadToEnd(); + + var global_config = GlobalConfig.Load(System.IO.Path.GetDirectoryName(stream.ToString()) ?? string.Empty, configuration) ?? []; + var streamPath = stream.ToString() ?? string.Empty; + global_config = Normalizer.Normalize(global_config, streamPath); + + var frontmatter = LoadRaw(text, global_config, stream.ToString()); + var prompty = Convert(frontmatter, stream.ToString()); + return prompty; + } + + /// + /// Load a prompty file from a Stream Asynchronously + /// + /// Stream to read the prompty file from. + /// Id of the configuration to use. + public static async Task LoadAsync(Stream stream, string configuration = "default") + { + using var reader = new StreamReader(stream); + string text = await reader.ReadToEndAsync(); + + var global_config = await GlobalConfig.LoadAsync(System.IO.Path.GetDirectoryName(stream.ToString()) ?? string.Empty, configuration) ?? []; + var streamPath = stream.ToString() ?? string.Empty; + global_config = Normalizer.Normalize(global_config, streamPath); + + var frontmatter = LoadRaw(text, global_config, stream.ToString()); + var prompty = Convert(frontmatter, stream.ToString()); + return prompty; + } + /// /// Load a prompty file using the provided text content. ///