diff options
Diffstat (limited to 'tests/SomhairlesDream.Modeling.Tests')
3 files changed, 262 insertions, 0 deletions
diff --git a/tests/SomhairlesDream.Modeling.Tests/BridgeTests.fs b/tests/SomhairlesDream.Modeling.Tests/BridgeTests.fs new file mode 100644 index 0000000..d9c7347 --- /dev/null +++ b/tests/SomhairlesDream.Modeling.Tests/BridgeTests.fs @@ -0,0 +1,72 @@ +module SomhairlesDream.Modeling.Tests.BridgeTests + +open System +open System.IO +open Xunit +open SomhairlesDream.Modeling + +let private withTempDirectory action = + let path = Path.Combine(Path.GetTempPath(), "somhairles-dream-bridge-" + Guid.NewGuid().ToString("N")) + Directory.CreateDirectory(path) |> ignore + + try + action path + finally + if Directory.Exists(path) then + Directory.Delete(path, true) + +let private fixtureScript = + [| "import argparse" + "import json" + "import os" + "" + "parser = argparse.ArgumentParser()" + "parser.add_argument('--step-id', required=True)" + "parser.add_argument('--output', required=True)" + "parser.add_argument('--render-output')" + "parser.add_argument('--result-output')" + "args = parser.parse_args()" + "os.makedirs(os.path.dirname(args.output), exist_ok=True)" + "with open(args.output, 'wb') as stream:" + " stream.write(b'glTF')" + "result_path = args.result_output or args.output + '.result.json'" + "with open(result_path, 'w', encoding='utf-8') as stream:" + " json.dump({" + " 'ok': True," + " 'blenderVersion': 'test'," + " 'exportedAt': '2026-09-20T12:00:05Z'," + " 'objectIds': ['heritage.foundation', 'heritage.deck']," + " 'verifiedObjectIds': ['heritage.foundation', 'heritage.deck']," + " 'glbBytes': 4" + " }, stream)" + "with open(args.output + '.log', 'w', encoding='utf-8') as stream:" + " stream.write('fixture log retained separately\\n')" + "print('Blender informational output')" |] + |> String.concat Environment.NewLine + +[<Fact>] +let ``bridge reads result file instead of polluted stdout and retains process log`` () = + withTempDirectory (fun root -> + let blenderRoot = Path.Combine(root, "stage", "blender") + Directory.CreateDirectory(blenderRoot) |> ignore + File.WriteAllText(Path.Combine(blenderRoot, "fixture.py"), fixtureScript) + + let output = Path.Combine(root, "run", "step.glb") + let request = + { Step = Design.steps[0] + GlbPath = output + RenderPath = None + ResultPath = output + ".result.json" + LogPath = output + ".log" } + + let result = + (BlenderProcessBridge("python3", Path.Combine(root, "stage")) :> IArtifactBridge) + .Export(request, ignore) + + match result with + | Error message -> failwith message + | Ok value -> + Assert.Equal("test", value.BlenderVersion) + Assert.Equal(4L, value.GlbBytes) + Assert.True(File.Exists(output + ".log")) + Assert.Contains("Blender informational output", File.ReadAllText(output + ".log"))) diff --git a/tests/SomhairlesDream.Modeling.Tests/PipelineTests.fs b/tests/SomhairlesDream.Modeling.Tests/PipelineTests.fs new file mode 100644 index 0000000..7a5e3e1 --- /dev/null +++ b/tests/SomhairlesDream.Modeling.Tests/PipelineTests.fs @@ -0,0 +1,167 @@ +module SomhairlesDream.Modeling.Tests.PipelineTests + +open System +open System.IO +open Xunit +open SomhairlesDream.Modeling +open SomhairlesDream.Shared + +let private ids : RunIds = + { ProjectId = "heritage-001" + RunId = "run-20260920-001" + BaseId = "heritage" + TargetId = "heritage-001" } + +let private expectOk = function + | Ok value -> value + | Error message -> failwith message + +let private expectError = function + | Error message -> message + | Ok _ -> failwith "expected an error" + +let private withTempDirectory action = + let path = Path.Combine(Path.GetTempPath(), "somhairles-dream-" + Guid.NewGuid().ToString("N")) + Directory.CreateDirectory(path) |> ignore + + try + action path + finally + if Directory.Exists(path) then + Directory.Delete(path, true) + +type private TestBridge(?wrongObjectIds: bool) = + interface IArtifactBridge with + member _.Export(request, _) = + File.WriteAllBytes(request.GlbPath, [| 0x67uy; 0x6Cuy; 0x54uy; byte request.Step.Index |]) + + Directory.CreateDirectory(Path.GetDirectoryName(request.LogPath)) |> ignore + File.WriteAllText(request.LogPath, $"bridge log for {request.Step.StepId}") + + match request.RenderPath with + | Some path -> File.WriteAllBytes(path, [| 0x89uy; 0x50uy; 0x4Euy; 0x47uy |]) + | None -> () + + let objectIds = + if defaultArg wrongObjectIds false then + [| "unexpected.object" |] + else + request.Step.ObjectIds + + Ok + { BlenderVersion = "5.0.1" + ExportedAt = DateTimeOffset.Parse("2026-09-20T12:00:05Z") + ObjectIds = objectIds + VerifiedObjectIds = objectIds + GlbBytes = 4L + RenderPath = request.RenderPath + RenderedAt = request.RenderPath |> Option.map (fun _ -> DateTimeOffset.Parse("2026-09-20T12:00:06Z")) } + +let private options root bridge = + { PipelineOptions.ArtifactRoot = root + Ids = ids + Render = true + Bridge = bridge + Clock = fun () -> DateTimeOffset.Parse("2026-09-20T12:00:00Z") + OnEvent = ignore } + +[<Fact>] +let ``design steps expose the fixed ordered object contract`` () = + let steps = Design.steps + + Assert.Equal(3, steps.Length) + Assert.True([| "01-foundation"; "02-frame"; "03-cabin" |] = (steps |> Array.map _.StepId)) + Assert.True([| 0; 1; 2 |] = (steps |> Array.map _.Index)) + Assert.True([| "heritage.foundation"; "heritage.deck" |] = steps[0].ObjectIds) + Assert.True( + [| "heritage.foundation" + "heritage.deck" + "heritage.frame.left" + "heritage.frame.right" + "heritage.spine" |] = steps[1].ObjectIds) + Assert.True( + [| "heritage.foundation" + "heritage.deck" + "heritage.frame.left" + "heritage.frame.right" + "heritage.spine" + "heritage.cabin" + "heritage.crossbeam" |] = steps[2].ObjectIds) + +[<Fact>] +let ``pipeline commits each step and writes manifest after complete event`` () = + withTempDirectory (fun root -> + let manifest = Pipeline.run (options root (TestBridge() :> IArtifactBridge)) |> expectOk + let runDirectory = Path.Combine(root, ids.ProjectId, ids.RunId) + let eventsPath = Path.Combine(runDirectory, "events.ndjson") + let manifestPath = Path.Combine(runDirectory, "manifest.json") + + Assert.Equal("complete", manifest.Status) + Assert.Equal(3, manifest.Steps.Length) + Assert.True(File.Exists(eventsPath)) + Assert.True(File.Exists(manifestPath)) + let verifiedManifest: ArtifactManifest = ArtifactVerifier.verify manifestPath |> expectOk + Assert.Equal(manifest.RunId, verifiedManifest.RunId) + Assert.Equal(manifest.Steps.Length, verifiedManifest.Steps.Length) + Assert.False(Directory.Exists(Path.Combine(runDirectory, ".staging"))) + + for step in manifest.Steps do + Assert.True(File.Exists(Path.Combine(runDirectory, step.ArtifactPath.Replace('/', Path.DirectorySeparatorChar)))) + Assert.Equal($"logs/{step.StepId}.log", step.LogPath) + Assert.True(File.Exists(Path.Combine(runDirectory, step.LogPath.Replace('/', Path.DirectorySeparatorChar)))) + Assert.True(step.Render |> Option.exists (fun render -> File.Exists(Path.Combine(runDirectory, render.ArtifactPath.Replace('/', Path.DirectorySeparatorChar))))) + Assert.True(step.ObjectIds = step.VerifiedObjectIds) + + let events = File.ReadAllLines(eventsPath) + Assert.Contains("\"kind\":\"start\"", events[0]) + Assert.Contains("\"kind\":\"complete\"", events[events.Length - 1])) + +[<Fact>] +let ``pipeline rejects a pre-existing run directory without overwriting it`` () = + withTempDirectory (fun root -> + let runDirectory = Path.Combine(root, ids.ProjectId, ids.RunId) + Directory.CreateDirectory(runDirectory) |> ignore + let marker = Path.Combine(runDirectory, "marker.txt") + File.WriteAllText(marker, "keep") + + let result = Pipeline.run (options root (TestBridge() :> IArtifactBridge)) + + Assert.True(Result.isError result) + Assert.Equal("keep", File.ReadAllText(marker))) + +[<Fact>] +let ``pipeline fails before manifest commit when bridge object ids differ`` () = + withTempDirectory (fun root -> + let result = Pipeline.run (options root (TestBridge(true) :> IArtifactBridge)) + let runDirectory = Path.Combine(root, ids.ProjectId, ids.RunId) + + Assert.True(Result.isError result) + Assert.False(File.Exists(Path.Combine(runDirectory, "manifest.json"))) + Assert.Contains("object ids", result |> expectError)) + +[<Fact>] +let ``manifest verifier rejects a tampered artifact hash`` () = + withTempDirectory (fun root -> + Pipeline.run (options root (TestBridge() :> IArtifactBridge)) |> expectOk |> ignore + let runDirectory = Path.Combine(root, ids.ProjectId, ids.RunId) + let artifactPath = Path.Combine(runDirectory, "steps", "01-foundation.glb") + let bytes = File.ReadAllBytes(artifactPath) + bytes[0] <- bytes[0] ^^^ 1uy + File.WriteAllBytes(artifactPath, bytes) + + let result = ArtifactVerifier.verify (Path.Combine(runDirectory, "manifest.json")) + + Assert.True(Result.isError result) + Assert.Contains("hash", result |> expectError)) + +[<Fact>] +let ``manifest verifier rejects a missing retained process log`` () = + withTempDirectory (fun root -> + Pipeline.run (options root (TestBridge() :> IArtifactBridge)) |> expectOk |> ignore + let runDirectory = Path.Combine(root, ids.ProjectId, ids.RunId) + File.Delete(Path.Combine(runDirectory, "logs", "01-foundation.log")) + + let result = ArtifactVerifier.verify (Path.Combine(runDirectory, "manifest.json")) + + Assert.True(Result.isError result) + Assert.Contains("log", result |> expectError)) diff --git a/tests/SomhairlesDream.Modeling.Tests/SomhairlesDream.Modeling.Tests.fsproj b/tests/SomhairlesDream.Modeling.Tests/SomhairlesDream.Modeling.Tests.fsproj new file mode 100644 index 0000000..f7175b7 --- /dev/null +++ b/tests/SomhairlesDream.Modeling.Tests/SomhairlesDream.Modeling.Tests.fsproj @@ -0,0 +1,23 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <TargetFramework>net8.0</TargetFramework> + <IsPackable>false</IsPackable> + <IsTestProject>true</IsTestProject> + </PropertyGroup> + <ItemGroup> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> + <PackageReference Include="xunit" Version="2.9.2" /> + <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"> + <PrivateAssets>all</PrivateAssets> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + </PackageReference> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="../../src/SomhairlesDream.Shared/SomhairlesDream.Shared.fsproj" /> + <ProjectReference Include="../../src/SomhairlesDream.Modeling/SomhairlesDream.Modeling.fsproj" /> + </ItemGroup> + <ItemGroup> + <Compile Include="PipelineTests.fs" /> + <Compile Include="BridgeTests.fs" /> + </ItemGroup> +</Project> |
