package app import ( "bytes" "context" "encoding/json" "fmt" "io" "path/filepath" "strings" "testing" "time" "gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/manifest" "gitea.maximumdirect.net/eric/narratio/internal/stage" "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 selected == "polish" { seedGlossaryRecords(t, root, m) } if err := store.Save(context.Background(), path, m); err != nil { t.Fatal(err) } } func seedGlossaryRecords(t *testing.T, root string, m *manifest.Manifest) { t.Helper() store := artifacts.NewLocalStore(root) paths, err := store.EnsureLayoutFor("sample-campaign", "2026-05-03") if err != nil { t.Fatal(err) } for _, descriptor := range artifactpolicy.GlossaryInputSources() { found := false for _, input := range m.Inputs { if input.Kind == descriptor.ManifestKind { found = true break } } if found { continue } path := filepath.Join(paths.InputsDir, descriptor.Filename) mustWriteTestFile(t, path, "synthetic: {entries: []}\n") checksum, err := store.Checksum(path) if err != nil { t.Fatal(err) } m.Inputs = append(m.Inputs, manifest.InputRecord{Kind: descriptor.ManifestKind, Path: path, Checksum: checksum}) } } func seedPolishGlossaryEvidence(t *testing.T, root string, m *manifest.Manifest) { t.Helper() paths := artifacts.NewLocalStore(root).SessionPathsFor("sample-campaign", "2026-05-03") _, evidence, err := stage.BuildPolishRequest(paths, m, "", filepath.Join(paths.Root, "scratch", "polished.json"), filepath.Join(paths.Root, "scratch", "logs"), filepath.Join(paths.Root, "scratch", "debug")) if err != nil { t.Fatal(err) } m.Stages["polish"].Metadata = map[string]any{"glossary_evidence": evidence} } func seedGlossaryManifest(t *testing.T, cfg *config.Config) { t.Helper() m := manifest.New(cfg.Session.SessionID, time.Now().UTC()) m.Campaign = cfg.Session.Campaign seedGlossaryRecords(t, cfg.Pipeline.Workspace.Root, m) if err := (&manifest.LocalStore{}).Save(context.Background(), manifestPathFor(cfg), 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{} }