//go:build integration package integration import ( "context" "testing" "time" "github.com/google/uuid" "github.com/missionworkforce/missionmatch/internal/modules/commandcenter" "github.com/missionworkforce/missionmatch/internal/platform/audit" "github.com/missionworkforce/missionmatch/internal/platform/tenant" ) // TestCommandCenterUpcomingIncumbentAdopt proves the Workstream D additions against a real database: the // Upcoming Opportunities panel surfaces the global forecast catalog (planning labeled), the Incumbent // Landscape surfaces the contract award feed, neither alters the current readiness math, and adopting a // forecast creates a real engine scorable requirement that is audited. func TestCommandCenterUpcomingIncumbentAdopt(t *testing.T) { org := seedOrg(t) // A real user so the adopt audit actor satisfies any reference. var userID uuid.UUID if err := migrator.QueryRow(context.Background(), `INSERT INTO users (organization_id, email, full_name, role, status) VALUES ($1, $2, 'Adopt Tester', 'owner', 'active') RETURNING id`, org, "adopt-"+org.String()[:8]+"@example.com").Scan(&userID); err != nil { t.Fatalf("seed user: %v", err) } ctx := tenant.WithUser(tenant.WithOrg(context.Background(), org.String()), tenant.Principal{UserID: userID.String(), Role: "owner"}) svc := commandcenter.NewServiceWithAudit(appDB, audit.New(appDB)) v, err := svc.CommandCenter(ctx, time.Now()) if err != nil { t.Fatalf("CommandCenter: %v", err) } // Forecasts are global reference data, so they appear for any org and are always planning labeled. if len(v.UpcomingOpportunities) == 0 { t.Fatal("expected forecast records in upcoming opportunities (the catalog is global)") } for _, u := range v.UpcomingOpportunities { if u.PlanningNote != "Planning data subject to change" { t.Fatalf("forecast not labeled as planning data: %+v", u) } if u.AvailableClearedInState < 0 { t.Fatalf("negative bench count: %+v", u) } if u.FitNote == "" { t.Fatal("missing fit note") } if !u.Adoptable { t.Fatal("forecast should be adoptable") } } // Incumbent landscape draws from the contract award feed (real values with provenance). if len(v.IncumbentLandscape) == 0 { t.Fatal("expected incumbent landscape from the contract award feed") } for _, inc := range v.IncumbentLandscape { if inc.Incumbent == "" { t.Fatalf("incumbent row missing a name: %+v", inc) } } // Adopt a forecast into a real requirement (the adopt to score path). var forecastID string if err := appDB.Pool.QueryRow(context.Background(), `SELECT id::text FROM opportunity_records WHERE record_kind='forecast' LIMIT 1`).Scan(&forecastID); err != nil { t.Fatalf("pick a forecast: %v", err) } res, err := svc.AdoptForecast(ctx, forecastID, commandcenter.AdoptForecastInput{ RequiredLevel: "secret", RequiredYears: 5, Skills: []string{"Cybersecurity", "Systems Engineering"}, }) if err != nil { t.Fatalf("AdoptForecast: %v", err) } if res.RequirementID == "" { t.Fatal("adopt returned no requirement id") } // The new requirement is real, scoped to the org, with the supplied clearance and mandatory skills. var level string var mandSkills int if err := migrator.QueryRow(context.Background(), `SELECT required_level, (SELECT count(*) FROM required_skills WHERE requirement_id=$1 AND mandatory) FROM requirements WHERE id=$1 AND organization_id=$2`, res.RequirementID, org). Scan(&level, &mandSkills); err != nil { t.Fatalf("read adopted requirement: %v", err) } if level != "secret" { t.Fatalf("required_level = %q, want secret", level) } if mandSkills != 2 { t.Fatalf("mandatory skills = %d, want 2", mandSkills) } // The adopt was recorded to the append only audit trail. var audits int if err := migrator.QueryRow(context.Background(), `SELECT count(*) FROM audit_logs WHERE action='requirement.adopted_from_forecast' AND entity_id=$1`, res.RequirementID).Scan(&audits); err != nil { t.Fatalf("audit read: %v", err) } if audits == 0 { t.Fatal("adopt as requirement was not audited") } // Adopting a non forecast (a contract award) must be refused. var awardID string if err := appDB.Pool.QueryRow(context.Background(), `SELECT id::text FROM opportunity_records WHERE record_kind='contract_award' LIMIT 1`).Scan(&awardID); err == nil { if _, aerr := svc.AdoptForecast(ctx, awardID, commandcenter.AdoptForecastInput{}); aerr == nil { t.Fatal("adopting a contract award should be refused") } } }