package app import ( "context" "os" "path/filepath" "reflect" "testing" "time" "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" ) func suppliedCompletionFixture(t *testing.T) (*manifest.Manifest, *Env) { t.Helper() cfg := testConfig(t) paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID) path := filepath.Join(paths.Root, filepath.FromSlash(artifacts.TranscriptPathFinalTrimmed)) if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { t.Fatal(err) } body := []byte(`{"segments":[]}`) if err := os.WriteFile(path, body, 0600); err != nil { t.Fatal(err) } checksum, err := artifacts.SHA256File(path) if err != nil { t.Fatal(err) } m := manifest.New(cfg.Session.SessionID, time.Now().UTC()) m.Campaign = cfg.Session.Campaign m.SuppliedProducts = &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{artifacts.ArtifactTranscriptFinalTrimmed: {Path: artifacts.TranscriptPathFinalTrimmed, Checksum: checksum, Size: int64(len(body))}}} saveBoundedManifest(t, cfg, m) return m, &Env{Config: cfg} } func TestSuppliedTranscriptPlanAndBoundedExecution(t *testing.T) { m, env := suppliedCompletionFixture(t) cfg := env.Config plan := mustBoundedPlan(t, "transcribe", "trim") counts := 0 plan.stages = []stage.Stage{countingStage{name: "transcribe", runs: &counts}, countingStage{name: "merge", runs: &counts}, countingStage{name: "polish", runs: &counts}, countingStage{name: "normalize", runs: &counts}, countingStage{name: "trim", runs: &counts}} before, err := cloneManifestForPlan(m, cfg) if err != nil { t.Fatal(err) } projection, err := projectSelectedWork(context.Background(), cfg, plan, false, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil, m) if err != nil { t.Fatal(err) } for _, entry := range projection.Stages { if entry.Decision.action != stageActionSkip { t.Fatalf("projection %+v", entry) } } if !reflect.DeepEqual(before, m) { t.Fatal("projection changed manifest") } result, err := executePlan(context.Background(), cfg, plan, RunOptions{}) if err != nil { t.Fatal(err) } if counts != 0 || len(result.Executed) != 0 { t.Fatalf("calls %d, executed %v", counts, result.Executed) } if err := validateBoundedPrerequisites(mustBoundedPlan(t, "analyze", "analyze"), m, cfg); err != nil { t.Fatal(err) } } func TestSuppliedCoverageRejectsTamperingAndForceRetiresAuthority(t *testing.T) { m, env := suppliedCompletionFixture(t) cfg := env.Config if !suppliedCompletion(cfg, m)["trim"] { t.Fatal("missing coverage") } path := filepath.Join(artifacts.SessionWorkDirForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID), filepath.FromSlash(artifacts.TranscriptPathFinalTrimmed)) if err := os.WriteFile(path, []byte("tampered"), 0600); err != nil { t.Fatal(err) } if suppliedCompletion(cfg, m)["trim"] { t.Fatal("tampered coverage") } retireSuppliedForAttempt(cfg, m, "transcribe") if len(m.SuppliedProducts.Products) != 0 { t.Fatal("force retained superseded supplied record") } } func TestSuppliedTranscriptForcedRunMatchesPlan(t *testing.T) { m, env := suppliedCompletionFixture(t) cfg := env.Config plan := mustBoundedPlan(t, "transcribe", "transcribe") calls := 0 plan.stages = []stage.Stage{countingStage{name: "transcribe", runs: &calls}} projected, err := projectSelectedWork(context.Background(), cfg, plan, true, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil, m) if err != nil { t.Fatal(err) } if projected.Stages[0].Decision.action != stageActionRun { t.Fatalf("projection %+v", projected.Stages) } if len(projected.Model.SuppliedProducts.Products) != 0 || len(m.SuppliedProducts.Products) != 1 { t.Fatal("force did not retire authority on the detached projection") } result, err := executePlan(context.Background(), cfg, plan, RunOptions{Force: true}) if err != nil { t.Fatal(err) } if calls != 1 || !reflect.DeepEqual(result.Executed, []string{"transcribe"}) { t.Fatalf("calls %d result %+v", calls, result) } saved, err := (&manifest.LocalStore{}).Load(context.Background(), result.ManifestPath) if err != nil { t.Fatal(err) } if len(saved.SuppliedProducts.Products) != 0 { t.Fatal("force retained supplied authority") } } func TestSuppliedTranscriptRealStagesReuseWithoutAdapters(t *testing.T) { m, env := suppliedCompletionFixture(t) cfg := env.Config cfg.Pipeline.Audita.ProposalProfile = "changed-historical-profile" plan := mustBoundedPlan(t, "transcribe", "trim") projected, err := projectSelectedWork(context.Background(), cfg, plan, false, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil, m) if err != nil { t.Fatal(err) } for _, decision := range projected.Stages { if decision.Decision.action != stageActionSkip { t.Fatalf("projected %+v", decision) } } result, err := executePlan(context.Background(), cfg, plan, RunOptions{}) if err != nil { t.Fatal(err) } if len(result.Executed) != 0 { t.Fatalf("unexpected execution %v", result.Executed) } } func TestSuppliedTranscriptFullPrefixPlanMatchesExecution(t *testing.T) { m, env := suppliedCompletionFixture(t) cfg := env.Config plan := mustBoundedPlan(t, "", "trim") prepareRuns := 0 plan.stages[0] = countingStage{name: "prepare", runs: &prepareRuns} projected, err := projectSelectedWork(context.Background(), cfg, plan, false, nil, artifacts.EffectiveArtifactSet{}, artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), nil, m) if err != nil { t.Fatal(err) } if projected.Stages[0].Decision.action != stageActionRun { t.Fatalf("prepare decision %+v", projected.Stages[0]) } for _, decision := range projected.Stages[1:] { if decision.Decision.action != stageActionSkip { t.Fatalf("decision %+v", decision) } } result, err := executePlan(context.Background(), cfg, plan, RunOptions{}) if err != nil { t.Fatal(err) } if prepareRuns != 1 || !reflect.DeepEqual(result.Executed, []string{"prepare"}) { t.Fatalf("runs %d execution %v", prepareRuns, result.Executed) } } func TestSuppliedRenderCompletionFollowsSelection(t *testing.T) { m, env := suppliedCompletionFixture(t) cfg := env.Config paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID) path := filepath.Join(paths.Root, filepath.FromSlash(artifacts.TranscriptPathFinalTrimmedMarkdown)) body := []byte("trimmed markdown\n") if err := os.WriteFile(path, body, 0600); err != nil { t.Fatal(err) } checksum, err := artifacts.SHA256File(path) if err != nil { t.Fatal(err) } m.SuppliedProducts.Products[artifacts.ArtifactTranscriptFinalTrimmedMarkdown] = manifest.SuppliedProduct{Path: artifacts.TranscriptPathFinalTrimmedMarkdown, Checksum: checksum, Size: int64(len(body))} cfg.Pipeline.Render = &config.RenderConfig{Variants: []string{"trimmed"}} if !suppliedCompletion(cfg, m)["render"] { t.Fatal("selected output not recognized") } cfg.Pipeline.Render.Variants = []string{"normalized", "trimmed"} if suppliedCompletion(cfg, m)["render"] { t.Fatal("missing selected output treated as complete") } }