feat(backend): PolicyRunner ONNX inference wrapper

This commit is contained in:
meelstorm
2026-07-17 17:40:56 +00:00
committed by EugeneTes
parent 9f34314c2f
commit 0102f5f748
2 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
using System.IO;
using Backend;
using Xunit;
namespace Backend.Tests;
public class PolicyRunnerTests
{
private static string LocateOnnxModel()
{
var dir = AppContext.BaseDirectory;
while (dir is not null)
{
var candidate = Path.Combine(dir, "models", "ppo_lander.onnx");
if (File.Exists(candidate)) return candidate;
var parent = Directory.GetParent(dir);
if (parent is null) break;
dir = parent.FullName;
}
throw new FileNotFoundException(
"models/ppo_lander.onnx not found — run `python Training/export_onnx.py ...`");
}
[Fact]
public void LoadsWithoutError()
{
using var runner = new PolicyRunner(LocateOnnxModel());
}
[Fact]
public void SelectAction_ReturnsValidActionForZeroObs()
{
using var runner = new PolicyRunner(LocateOnnxModel());
var obs = new float[7]; // all zeros
int action = runner.SelectAction(obs);
Assert.InRange(action, 0, 3);
}
[Fact]
public void SelectAction_IsDeterministicForSameInput()
{
using var runner = new PolicyRunner(LocateOnnxModel());
var obs = new float[] { 0.1f, -0.2f, 0.05f, 0.0f, 0.0f, 1.0f, 0.0f };
int a1 = runner.SelectAction(obs);
int a2 = runner.SelectAction(obs);
Assert.Equal(a1, a2);
}
[Fact]
public void SelectAction_ThrowsIfObsLengthWrong()
{
using var runner = new PolicyRunner(LocateOnnxModel());
Assert.Throws<ArgumentException>(() => runner.SelectAction(new float[3]));
}
}