Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions runtime/promptycs/Prompty.Core.Tests/LoadTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using System.Transactions;

namespace Prompty.Core.Tests;
Expand Down Expand Up @@ -36,6 +37,76 @@ public void LoadRawWithConfig(string path)
Assert.Equal("FAKE_TYPE", prompty.Model?.Configuration.Type);
}

/// <summary>
/// Test the Loading from a Stream
/// </summary>
[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);
}

/// <summary>
/// Test the Loading from an embedded resource
/// </summary>
[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);
}

/// <summary>
/// Test the Loading from an embedded resource with config
/// </summary>
[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);
}


/// <summary>
/// Test the Loading from a Stream Async
/// </summary>
[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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<None Update="prompty\basic.prompty">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<EmbeddedResource Include="prompty\basic.prompty" />
<None Update="prompty\basic_mustache.prompty">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
38 changes: 38 additions & 0 deletions runtime/promptycs/Prompty.Core/Prompty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,44 @@ public static async Task<Prompty> LoadAsync(string path, string configuration =
return prompty;
}

/// <summary>
/// Load a prompty file from a Stream
/// </summary>
/// <param name="stream">Stream to read the prompty file from.</param>
/// <param name="configuration">Id of the configuration to use.</param>
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;
}

/// <summary>
/// Load a prompty file from a Stream Asynchronously
/// </summary>
/// <param name="stream">Stream to read the prompty file from.</param>
/// <param name="configuration">Id of the configuration to use.</param>
public static async Task<Prompty> 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;
}

/// <summary>
/// Load a prompty file using the provided text content.
/// </summary>
Expand Down
Loading