package pipeline import ( "context" "path/filepath" "testing" "git.a-wan.net/alan/pipelines/internal/summarise" ) func TestOutputFormatPersistenceAndIdempotency(t *testing.T) { ctx := context.Background() s, err := Open(filepath.Join(t.TempDir(), "format.db")) if err != nil { t.Fatal(err) } defer s.Close() s.Submit(ctx, "format", Template([]string{"one"}, true)) items, _ := s.List(ctx) d, _ := s.Get(ctx, items[0].ID, 1) input := map[string]any{"prompt": "task"} create := func(format string) (int64, bool, error) { return s.CreateRunTitled(ctx, d, "same-key", "workspace", false, nil, input, RunOverrides{OutputFormat: format}, summarise.Title{}) } id, created, err := create("html") if err != nil || !created { t.Fatalf("%d %v %v", id, created, err) } again, created, err := create("html") if err != nil || created || again != id { t.Fatalf("%d %v %v", again, created, err) } for _, format := range []string{"", "markdown", "HTML", "../../report.html"} { if _, _, err := create(format); err == nil { t.Fatalf("accepted changed/invalid format %q", format) } } got, err := s.GetRun(ctx, id) if err != nil || got.OutputFormat != "html" { t.Fatalf("%+v %v", got, err) } runs, err := s.ListRuns(ctx) if err != nil || len(runs) != 1 || runs[0].OutputFormat != "html" { t.Fatalf("%+v %v", runs, err) } } func TestOutputFormatMigrationPreservesHistory(t *testing.T) { ctx := context.Background() path := filepath.Join(t.TempDir(), "legacy.db") s, err := Open(path) if err != nil { t.Fatal(err) } s.Submit(ctx, "old", Template([]string{"one"}, true)) items, _ := s.List(ctx) d, _ := s.Get(ctx, items[0].ID, 1) id, _, err := s.CreateRun(ctx, d, "old-key", "workspace", false, nil, map[string]any{"prompt": "old task"}) if err != nil { t.Fatal(err) } if _, err := s.db.Exec("ALTER TABLE pipeline_runs DROP COLUMN output_format"); err != nil { t.Fatal(err) } if _, err := s.db.Exec("DROP TABLE session_export_repairs"); err != nil { t.Fatal(err) } s.Close() for range 2 { s, err = Open(path) if err != nil { t.Fatal(err) } got, err := s.GetRun(ctx, id) if err != nil || got.OutputFormat != "" || got.Input["prompt"] != "old task" { t.Fatalf("%+v %v", got, err) } if _, err := s.db.Exec("SELECT export_json FROM session_export_repairs LIMIT 1"); err != nil { t.Fatalf("repair schema not migrated: %v", err) } s.Close() } }