121 lines
3.5 KiB
Go
121 lines
3.5 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||
|
|
"go.maximumdirect.net/commandkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Invoke the same installed tree as the process, with optional external seams.
|
||
|
|
func invokeNativeCommand(ctx context.Context, args []string, out io.Writer) error {
|
||
|
|
return invokeNativeCommandWith(ctx, args, out, executePlan, commandDependencies{})
|
||
|
|
}
|
||
|
|
func invokeNativeCommandWith(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor, deps commandDependencies) error {
|
||
|
|
application, err := newApplication(deps, executor)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
outcome := application.Execute(ctx, args, commandkit.Streams{Out: out, Err: io.Discard})
|
||
|
|
if outcome.Status == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
if outcome.Cause != nil {
|
||
|
|
return outcome.Cause
|
||
|
|
}
|
||
|
|
return fmt.Errorf("command failed with status %d: %s", outcome.Status, outcome.Explanation.Message)
|
||
|
|
}
|
||
|
|
|
||
|
|
func decodeNativeReport[T any](t *testing.T, body []byte) T {
|
||
|
|
t.Helper()
|
||
|
|
value, err := commandkit.DecodeYAMLReport(bytes.NewReader(body))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
data, err := json.Marshal(value)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
var result T
|
||
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return result
|
||
|
|
}
|
||
|
|
func runCountsMatch(t *testing.T, body []byte, executed, skipped int, stageName string) bool {
|
||
|
|
t.Helper()
|
||
|
|
report := decodeNativeReport[runReport](t, body)
|
||
|
|
return report.Complete && len(report.Executed) == executed && len(report.Skipped) == skipped && (stageName == "" || (len(report.Stages) == 1 && report.Stages[0] == stageName))
|
||
|
|
}
|
||
|
|
func markFixturePrerequisites(t *testing.T, m *manifest.Manifest, selected string) {
|
||
|
|
t.Helper()
|
||
|
|
plan, err := BuildBoundedPlan(selected, selected)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, name := range plan.PrefixNames() {
|
||
|
|
if m.Stages[name] == nil {
|
||
|
|
m.MarkStageSucceeded(name, time.Now().UTC(), nil)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
func seedInvocationPrerequisites(t *testing.T, root, selected string) {
|
||
|
|
t.Helper()
|
||
|
|
store := &manifest.LocalStore{}
|
||
|
|
path := artifacts.SessionManifestPathForCampaign(root, "sample-campaign", "2026-05-03")
|
||
|
|
m, _, err := loadManifestAtPathIfPresent(context.Background(), store, path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if m == nil {
|
||
|
|
m = manifest.New("2026-05-03", time.Now().UTC())
|
||
|
|
m.Campaign = "sample-campaign"
|
||
|
|
}
|
||
|
|
markFixturePrerequisites(t, m, selected)
|
||
|
|
if err := store.Save(context.Background(), path, m); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func findingMatches(findings []finding, severity, category, message string) bool {
|
||
|
|
for _, item := range findings {
|
||
|
|
if (severity == "" || item.Severity == severity) && item.Category == category && strings.Contains(item.Message, message) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
func publishedReportOutput(t *testing.T, body []byte, source string) sessionPublishedOutput {
|
||
|
|
t.Helper()
|
||
|
|
report := decodeNativeReport[sessionStateReport](t, body)
|
||
|
|
if report.Remote != nil {
|
||
|
|
for _, item := range report.Remote.Outputs {
|
||
|
|
if item.Source == source {
|
||
|
|
return item
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
t.Fatalf("published source %q missing from report: %#v", source, report)
|
||
|
|
return sessionPublishedOutput{}
|
||
|
|
}
|
||
|
|
func localReportArtifact(t *testing.T, body []byte, source string) sessionArtifactState {
|
||
|
|
t.Helper()
|
||
|
|
report := decodeNativeReport[sessionStateReport](t, body)
|
||
|
|
for _, item := range report.Artifacts {
|
||
|
|
if item.Source == source {
|
||
|
|
return item
|
||
|
|
}
|
||
|
|
}
|
||
|
|
t.Fatalf("artifact %q missing from report: %#v", source, report)
|
||
|
|
return sessionArtifactState{}
|
||
|
|
}
|