package plan3test import ( "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/library" "errors" "gorm.io/gorm" "testing" ) func TestSpecialistLibraryCatalogIsolation(t *testing.T) { f := newFixture(t) copyValue := "Shipped version one" templates := []library.Template{{Ref: library.TemplateRef{ID: "qa", Version: 1}, NameKey: "name", InstructionsKey: "instructions", Icon: "check-circle"}} 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 }, Catalog: 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) a, err := s.Install(owner, templates[0].Ref, equipo.Mutation{RequestID: "one"}) if err != nil { return err } b, err := s.Install(owner, templates[0].Ref, equipo.Mutation{RequestID: "two"}) if err != nil { return err } templates = append(templates, templates[0]) templates[1].Ref.Version = 2 copyValue = "Shipped version two" after, err := s.Get(owner, a.Key) if err != nil { return err } if after.Name != a.Name { t.Fatal("catalog changed installed copy") } after, err = s.Get(owner, b.Key) if err != nil { return err } if after.Name != b.Name { t.Fatal("catalog changed second copy") } diff, err := s.Diff(owner, a.Key, templates[1].Ref) if err != nil { return err } copyValue = "Changed after preview" if _, err = s.Apply(owner, a.Key, diff, []string{"name"}, equipo.Mutation{RequestID: "changed", Revision: a.Revision}); !errors.Is(err, equipo.ErrConflict) { t.Fatal("later catalog silently changed accepted diff", err) } copyValue = "Shipped version two" after, err = s.Apply(owner, a.Key, diff, []string{"name"}, equipo.Mutation{RequestID: "accept", Revision: a.Revision}) if err != nil { return err } if after.Name != copyValue || after.StandingInstructions != a.StandingInstructions || after.TemplateVersion != 2 { t.Fatal("explicit update lost provenance or changed unselected field", after) } copyValue = "Later release" replay, err := s.Apply(owner, a.Key, diff, []string{"name"}, equipo.Mutation{RequestID: "accept", Revision: a.Revision}) if err != nil { return err } if replay.Name != after.Name || replay.Revision != after.Revision { t.Fatal("retry changed accepted diff") } replay, err = s.Install(owner, templates[0].Ref, equipo.Mutation{RequestID: "one"}) if err != nil { return err } if replay.Name != a.Name { t.Fatal("catalog change altered install retry") } return nil })) }