package oltp
import (
"path/filepath"
"testing"
)
// A terminal run records when it finished; the migration leaves old
// rows readable with an empty stamp rather than failing the scan.
func TestCompleteCIRunStampsOnce(t *testing.T) {
db, err := Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
if err := db.AddCIRun(CIRun{RunID: "r1", Repo: "alpha", Checkin: "c1",
Workflow: "w", Pipeline: "ci/p.json", Status: "running"}); err != nil {
t.Fatal(err)
}
rows, _ := db.CIRuns("alpha", 10)
if len(rows) != 1 || rows[0].Completed != "" {
t.Fatalf("a running run has a completion stamp: %+v", rows)
}
if err := db.CompleteCIRun("r1", "failed", "2026-08-29T12:00:00Z"); err != nil {
t.Fatal(err)
}
rows, _ = db.CIRuns("alpha", 10)
if rows[0].Status != "failed" || rows[0].Completed != "2026-08-29T12:00:00Z" {
t.Fatalf("terminal transition not recorded: %+v", rows[0])
}
if err := db.CompleteCIRun("r2", "completed", ""); err != nil {
t.Fatal(err) // unknown run: harmless
}
}