package plan3test import ( "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/library" "errors" "gorm.io/gorm" "testing" ) func TestPlaybookTemplatesVersionAcceptance(t *testing.T) { f := newFixture(t) templates := library.PlaybookCatalog() copyValue := "Version one" deps := library.Dependencies{ Authorize: func(_ *gorm.DB, _ equipo.Actor) (library.Access, error) { return library.Access{TeamID: f.TeamID, Owner: true}, nil }, Audit: func(_ *gorm.DB, _ equipo.Actor, _, _ string) error { return nil }, Copy: func(string) string { return copyValue }, PlaybookCatalog: func() []library.Template { return templates }, Invalid: equipo.ErrInvalid, Forbidden: equipo.ErrForbidden, Conflict: equipo.ErrConflict, NotFound: equipo.ErrNotFound, } must(t, f.DB.G.Transaction(func(tx *gorm.DB) error { s := library.New(tx, deps) row, err := s.InstallPlaybookTemplate(owner, templates[0].Ref, equipo.Mutation{RequestID: "install"}) if err != nil { return err } templates = append(templates, templates[0]) templates[len(templates)-1].Ref.Version = 2 copyValue = "Version two" current, err := s.Playbook(owner, row.ID) if err != nil { return err } if current.Name != row.Name { t.Fatal("catalog changed copy") } diff, err := s.PlaybookTemplateDiff(owner, row.ID, templates[len(templates)-1].Ref) if err != nil { return err } copyValue = "Changed after preview" if _, err = s.ApplyPlaybookTemplate(owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: "changed", Revision: 1}); !errors.Is(err, equipo.ErrConflict) { t.Fatal("changed offered update accepted", err) } copyValue = "Version two" updated, err := s.ApplyPlaybookTemplate(owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: "apply", Revision: 1}) if err != nil { return err } current, err = s.Playbook(owner, row.ID) if err != nil { return err } if current.Name != "Version two" || current.Template.Version != 2 || current.Steps[0].Instructions != row.Steps[0].Instructions { t.Fatal("update changed unselected fields or lost version", current) } copyValue = "Later release" retry, err := s.ApplyPlaybookTemplate(owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: "apply", Revision: 1}) if err != nil { return err } if retry.Revision != updated.Revision || retry.Name != updated.Name { t.Fatal("retry changed accepted update") } return nil })) }