| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 6 | "errors" |
| 7 | "gorm.io/gorm" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestSpecialistLibraryCatalogIsolation(t *testing.T) { |
| 12 | f := newFixture(t) |
| 13 | copyValue := "Shipped version one" |
| 14 | templates := []library.Template{{Ref: library.TemplateRef{ID: "qa", Version: 1}, NameKey: "name", InstructionsKey: "instructions", Icon: "check-circle"}} |
| 15 | deps := library.Dependencies{ |
| 16 | Authorize: func(_ *gorm.DB, _ equipo.Actor) (library.Access, error) { |
| 17 | return library.Access{TeamID: f.TeamID, Owner: true}, nil |
| 18 | }, |
| 19 | Audit: func(_ *gorm.DB, _ equipo.Actor, _, _ string) error { return nil }, |
| 20 | Copy: func(string) string { return copyValue }, Catalog: func() []library.Template { return templates }, |
| 21 | Invalid: equipo.ErrInvalid, Forbidden: equipo.ErrForbidden, Conflict: equipo.ErrConflict, NotFound: equipo.ErrNotFound, |
| 22 | } |
| 23 | must(t, f.DB.G.Transaction(func(tx *gorm.DB) error { |
| 24 | s := library.New(tx, deps) |
| 25 | a, err := s.Install(owner, templates[0].Ref, equipo.Mutation{RequestID: "one"}) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | b, err := s.Install(owner, templates[0].Ref, equipo.Mutation{RequestID: "two"}) |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | templates = append(templates, templates[0]) |
| 34 | templates[1].Ref.Version = 2 |
| 35 | copyValue = "Shipped version two" |
| 36 | after, err := s.Get(owner, a.Key) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | if after.Name != a.Name { |
| 41 | t.Fatal("catalog changed installed copy") |
| 42 | } |
| 43 | after, err = s.Get(owner, b.Key) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | if after.Name != b.Name { |
| 48 | t.Fatal("catalog changed second copy") |
| 49 | } |
| 50 | diff, err := s.Diff(owner, a.Key, templates[1].Ref) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | copyValue = "Changed after preview" |
| 55 | if _, err = s.Apply(owner, a.Key, diff, []string{"name"}, equipo.Mutation{RequestID: "changed", Revision: a.Revision}); !errors.Is(err, equipo.ErrConflict) { |
| 56 | t.Fatal("later catalog silently changed accepted diff", err) |
| 57 | } |
| 58 | copyValue = "Shipped version two" |
| 59 | after, err = s.Apply(owner, a.Key, diff, []string{"name"}, equipo.Mutation{RequestID: "accept", Revision: a.Revision}) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | if after.Name != copyValue || after.StandingInstructions != a.StandingInstructions || after.TemplateVersion != 2 { |
| 64 | t.Fatal("explicit update lost provenance or changed unselected field", after) |
| 65 | } |
| 66 | copyValue = "Later release" |
| 67 | replay, err := s.Apply(owner, a.Key, diff, []string{"name"}, equipo.Mutation{RequestID: "accept", Revision: a.Revision}) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if replay.Name != after.Name || replay.Revision != after.Revision { |
| 72 | t.Fatal("retry changed accepted diff") |
| 73 | } |
| 74 | replay, err = s.Install(owner, templates[0].Ref, equipo.Mutation{RequestID: "one"}) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | if replay.Name != a.Name { |
| 79 | t.Fatal("catalog change altered install retry") |
| 80 | } |
| 81 | return nil |
| 82 | })) |
| 83 | } |
| 84 | |