109 lines
4.4 KiB
Go
109 lines
4.4 KiB
Go
package notarius
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess"
|
|
)
|
|
|
|
func TestNativeCompletionAndInvocation(t *testing.T) {
|
|
req := validRunRequest(t)
|
|
req.DebugDirectory = filepath.Join(filepath.Dir(req.LogPath), "debug")
|
|
mode, overlay := "refresh", ""
|
|
req.Cache, req.ConfigOverlay, req.Debug = &mode, &overlay, true
|
|
bundle := filepath.Join(req.OutputRoot, "published")
|
|
if err := os.Mkdir(bundle, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var args []string
|
|
runner := &SubprocessRunner{run: func(_ context.Context, request subprocess.RunRequest) (subprocess.RunResult, error) {
|
|
args = request.Args
|
|
document, _ := json.Marshal(map[string]any{"pipeline": req.PipelineID, "artifact_count": 12, "output_directory": bundle, "fallback_count": 3})
|
|
if err := os.WriteFile(req.ReportPath, document, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return subprocess.RunResult{}, nil
|
|
}}
|
|
result, err := runner.Run(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{"run", req.PipelineID, "--config", req.ConfigPath, "--input-file", req.InputPath, "--output-dir", req.OutputRoot, "--cache", mode, "--debug", "--debug-dir", req.DebugDirectory, "--overlay", "", "--format", "json"}
|
|
if !reflect.DeepEqual(args, want) {
|
|
t.Fatalf("args = %#v, want %#v", args, want)
|
|
}
|
|
if result.Report.FallbackCount != 3 || result.Report.ArtifactCount != 12 || result.BundleRoot != bundle {
|
|
t.Fatalf("result = %#v", result)
|
|
}
|
|
}
|
|
|
|
func TestNativeCompletionRequiresSuccessfulExit(t *testing.T) {
|
|
req := validRunRequest(t)
|
|
req.DebugDirectory = filepath.Join(filepath.Dir(req.LogPath), "debug")
|
|
runner := &SubprocessRunner{run: func(_ context.Context, _ subprocess.RunRequest) (subprocess.RunResult, error) {
|
|
if err := os.WriteFile(req.ReportPath, []byte(`{"pipeline":"dnd-session","artifact_count":0,"output_directory":"/tmp"}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return subprocess.RunResult{ExitCode: 1}, errors.New("process failed")
|
|
}}
|
|
result, err := runner.Run(context.Background(), req)
|
|
if err == nil || result.Report.Pipeline != "" {
|
|
t.Fatalf("result = %#v, error = %v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestNativeCompletionRejectsInvalidReports(t *testing.T) {
|
|
for _, test := range []struct{ name, body string }{
|
|
{"malformed", `{`},
|
|
{"wrong pipeline", `{"pipeline":"other","artifact_count":1,"output_directory":"/tmp"}`},
|
|
{"missing count", `{"pipeline":"dnd-session","output_directory":"/tmp"}`},
|
|
{"relative directory", `{"pipeline":"dnd-session","artifact_count":0,"output_directory":"relative"}`},
|
|
{"zero fallback", `{"pipeline":"dnd-session","artifact_count":0,"output_directory":"/tmp","fallback_count":0}`},
|
|
{"outside staging", `{"pipeline":"dnd-session","artifact_count":0,"output_directory":"/tmp"}`},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
req := validRunRequest(t)
|
|
req.DebugDirectory = filepath.Join(filepath.Dir(req.LogPath), "debug")
|
|
runner := &SubprocessRunner{run: func(_ context.Context, _ subprocess.RunRequest) (subprocess.RunResult, error) {
|
|
if err := os.WriteFile(req.ReportPath, []byte(test.body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return subprocess.RunResult{}, nil
|
|
}}
|
|
if _, err := runner.Run(context.Background(), req); err == nil {
|
|
t.Fatal("invalid report accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNativeInvocationOmitsOptionalControls(t *testing.T) {
|
|
req := validRunRequest(t)
|
|
req.DebugDirectory = filepath.Join(filepath.Dir(req.LogPath), "debug")
|
|
req.References = []ReferenceBinding{{Selector: "party", Path: req.InputPath}}
|
|
bundle := filepath.Join(req.OutputRoot, "bundle")
|
|
if err := os.Mkdir(bundle, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runner := &SubprocessRunner{run: func(_ context.Context, request subprocess.RunRequest) (subprocess.RunResult, error) {
|
|
want := []string{"run", req.PipelineID, "--config", req.ConfigPath, "--input-file", req.InputPath, "--output-dir", req.OutputRoot, "--debug-dir", req.DebugDirectory, "--reference", "party=" + req.InputPath, "--format", "json"}
|
|
if !reflect.DeepEqual(request.Args, want) {
|
|
t.Fatalf("args = %#v, want %#v", request.Args, want)
|
|
}
|
|
document, _ := json.Marshal(map[string]any{"pipeline": req.PipelineID, "artifact_count": 0, "output_directory": bundle})
|
|
if err := os.WriteFile(req.ReportPath, document, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return subprocess.RunResult{}, nil
|
|
}}
|
|
if _, err := runner.Run(context.Background(), req); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|