| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/fichas/internal/fichas/seal" |
| 8 | ) |
| 9 | |
| 10 | func TestAddFieldRefusesWhatWouldLeakOrMislead(t *testing.T) { |
| 11 | g, s := seeded(t) |
| 12 | for name, spec := range map[string]FieldSpec{ |
| 13 | "no name": {Kind: KindText, Tier: TierSealed}, |
| 14 | "no kind": {Label: "x", Tier: TierSealed}, |
| 15 | "a link, not built": {Label: "x", Kind: KindRef, Tier: TierSealed}, |
| 16 | "plaintext": {Label: "x", Kind: KindText, Tier: TierPlain}, |
| 17 | "a findable choice": {Label: "x", Kind: KindEnum, Tier: TierPrint, Options: []string{"a", "b"}}, |
| 18 | "findable long text": {Label: "x", Kind: KindLongText, Tier: TierPrint}, |
| 19 | "a choice, no options": {Label: "x", Kind: KindEnum, Tier: TierSealed, Options: []string{" ", ""}}, |
| 20 | } { |
| 21 | _, err := AddField(ctx(), g, s, TypeCustomer, Person(1), spec) |
| 22 | if _, ok := IsFormError(err); !ok { |
| 23 | t.Errorf("%s: got %v, want a FormError", name, err) |
| 24 | } |
| 25 | } |
| 26 | var n int64 |
| 27 | g.Model(&FieldDef{}).Where("key NOT LIKE ?", "f\\_%").Count(&n) |
| 28 | var total int64 |
| 29 | g.Model(&FieldDef{}).Count(&total) |
| 30 | if total != int64(len(catalogue[0].Fields)+len(catalogue[1].Fields)) { |
| 31 | t.Errorf("a refused field was written anyway (%d field defs)", total) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestAddedFieldsAreOpaqueSealedAndLogged(t *testing.T) { |
| 36 | g, s := seeded(t) |
| 37 | fd, err := AddField(ctx(), g, s, TypeCustomer, Person(1), FieldSpec{ |
| 38 | Label: "Contract value", Kind: KindNumber, Tier: TierSealed}) |
| 39 | if err != nil { |
| 40 | t.Fatalf("AddField: %v", err) |
| 41 | } |
| 42 | if !strings.HasPrefix(fd.Key, "f_") || strings.Contains(strings.ToLower(fd.Key), "contract") { |
| 43 | t.Fatalf("the key %q is not opaque: it reaches the durable file in the clear", fd.Key) |
| 44 | } |
| 45 | label, _ := s.Open(seal.PurposeLabel, fd.SealedLabel) |
| 46 | if string(label) != "Contract value" { |
| 47 | t.Fatalf("label is %q", label) |
| 48 | } |
| 49 | if fd.Ordinal != len(catalogue[0].Fields) { |
| 50 | t.Fatalf("a new field is at ordinal %d, want it last (%d)", fd.Ordinal, len(catalogue[0].Fields)) |
| 51 | } |
| 52 | if ev := events(t, g, ObjectField, fd.ID); len(ev) != 1 || ev[0].Kind != EventCreated { |
| 53 | t.Fatalf("adding a field was not logged: %+v", ev) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // A field added in the designer must reach the Table view, which reads the |
| 58 | // index — and the index only learns from the event log. |
| 59 | func TestTheIndexSeesANewField(t *testing.T) { |
| 60 | g, s := seeded(t) |
| 61 | ix := NewIndex(g, s) |
| 62 | CreateCard(ctx(), g, s, TypeCustomer, Person(1), map[string]string{FieldName: "Cnoc Teoranta"}) |
| 63 | mustQuery(t, ix, Query{}) |
| 64 | |
| 65 | fd, _ := AddField(ctx(), g, s, TypeCustomer, Person(1), FieldSpec{Label: "Region", Kind: KindEnum, |
| 66 | Tier: TierSealed, Options: []string{"North", "South"}}) |
| 67 | fields, _ := func() ([]FieldView, error) { ix.Sync(ctx()); return ix.Fields(TypeCustomer) }() |
| 68 | found := false |
| 69 | for _, f := range fields { |
| 70 | if f.Def.Key == fd.Key && f.Label == "Region" && len(f.Options) == 2 { |
| 71 | found = true |
| 72 | } |
| 73 | } |
| 74 | if !found { |
| 75 | t.Fatal("the index did not pick up a field added after it loaded") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestUpdateFieldRenamesAndLogsOnlyRealChanges(t *testing.T) { |
| 80 | g, s := seeded(t) |
| 81 | fd, _ := AddField(ctx(), g, s, TypeCustomer, Person(1), FieldSpec{Label: "Region", Kind: KindEnum, |
| 82 | Tier: TierSealed, Options: []string{"North", "South"}}) |
| 83 | |
| 84 | if err := UpdateField(ctx(), g, s, fd.Key, Person(1), "Region", []string{"North", "South"}); err != nil { |
| 85 | t.Fatalf("no-op update: %v", err) |
| 86 | } |
| 87 | if n := len(events(t, g, ObjectField, fd.ID)); n != 1 { |
| 88 | t.Fatalf("a no-op update was logged (%d events)", n) |
| 89 | } |
| 90 | if err := UpdateField(ctx(), g, s, fd.Key, Person(2), "Territory", []string{"North", "South", "West"}); err != nil { |
| 91 | t.Fatalf("UpdateField: %v", err) |
| 92 | } |
| 93 | ev := events(t, g, ObjectField, fd.ID) |
| 94 | if len(ev) != 2 || ev[1].ActorID == nil || *ev[1].ActorID != 2 { |
| 95 | t.Fatalf("the rename was not logged with its actor: %+v", ev) |
| 96 | } |
| 97 | var after FieldDef |
| 98 | g.Where("key = ?", fd.Key).First(&after) |
| 99 | if l, _ := s.Open(seal.PurposeLabel, after.SealedLabel); string(l) != "Territory" { |
| 100 | t.Fatalf("label is %q after a rename", l) |
| 101 | } |
| 102 | if after.Kind != KindEnum || after.Tier != TierSealed { |
| 103 | t.Fatal("a rename changed kind or tier") |
| 104 | } |
| 105 | if _, ok := IsFormError(UpdateField(ctx(), g, s, fd.Key, Person(1), "Territory", nil)); !ok { |
| 106 | t.Fatal("a choice was left with no options") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestCreateTypeGivesItANameField(t *testing.T) { |
| 111 | g, s := seeded(t) |
| 112 | ct, err := CreateType(ctx(), g, s, Person(1), "Supplier", "") |
| 113 | if err != nil { |
| 114 | t.Fatalf("CreateType: %v", err) |
| 115 | } |
| 116 | if ct.Scope != ScopeCard || !strings.HasPrefix(ct.Key, "t_") || strings.Contains(ct.Key, "upplier") { |
| 117 | t.Fatalf("type is %+v", ct) |
| 118 | } |
| 119 | var defs []FieldDef |
| 120 | g.Where("card_type_id = ?", ct.ID).Find(&defs) |
| 121 | if len(defs) != 1 || defs[0].Kind != KindText || defs[0].Tier != TierPrint { |
| 122 | t.Fatalf("a new type's fields are %+v", defs) |
| 123 | } |
| 124 | card, err := CreateCard(ctx(), g, s, ct.Key, Person(1), map[string]string{defs[0].Key: "Harbour Timber"}) |
| 125 | if err != nil { |
| 126 | t.Fatalf("a card of the new type: %v", err) |
| 127 | } |
| 128 | ix := NewIndex(g, s) |
| 129 | ix.Sync(ctx()) |
| 130 | r, err := ix.Query(ct.Key, Query{}) |
| 131 | if err != nil || r.Total != 1 || r.Cards[0].Title() != "Harbour Timber" || r.Plural != "Suppliers" { |
| 132 | t.Fatalf("the new type's list: %v total=%d plural=%q", err, r.Total, r.Plural) |
| 133 | } |
| 134 | _ = card |
| 135 | if _, ok := IsFormError(func() error { _, e := CreateType(ctx(), g, s, Person(1), " ", ""); return e }()); !ok { |
| 136 | t.Fatal("a type with no name was created") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestSetFaceKeepsOnlyThisTypesFields(t *testing.T) { |
| 141 | g, s := seeded(t) |
| 142 | if err := SetFace(ctx(), g, s, TypeCustomer, Person(1), []string{FieldStage, FaceLatestCall, FieldStage}); err != nil { |
| 143 | t.Fatalf("SetFace: %v", err) |
| 144 | } |
| 145 | var ct CardType |
| 146 | g.Where("key = ?", TypeCustomer).First(&ct) |
| 147 | if ct.FaceKeys != `["`+FieldStage+`","`+FaceLatestCall+`"]` { |
| 148 | t.Fatalf("face is %s", ct.FaceKeys) |
| 149 | } |
| 150 | if _, ok := IsFormError(SetFace(ctx(), g, s, TypeCustomer, Person(1), []string{FieldBody})); !ok { |
| 151 | t.Fatal("a Call log field was accepted on the Customer card face") |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Values are checked by kind, and a bad one writes nothing — before this a |
| 156 | // mistyped number or date was a 500. |
| 157 | func TestValuesAreCheckedByKind(t *testing.T) { |
| 158 | g, s := seeded(t) |
| 159 | num, _ := AddField(ctx(), g, s, TypeCustomer, Person(1), FieldSpec{Label: "Seats", Kind: KindNumber, Tier: TierSealed}) |
| 160 | for name, v := range map[string]map[string]string{ |
| 161 | "a word in a number": {num.Key: "twelve"}, |
| 162 | "a bad date": {FieldNextAction: "next tuesday"}, |
| 163 | "an unknown stage": {FieldStage: "Maybe"}, |
| 164 | } { |
| 165 | _, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), v) |
| 166 | fe, ok := IsFormError(err) |
| 167 | if !ok { |
| 168 | t.Errorf("%s: got %v, want a FormError", name, err) |
| 169 | continue |
| 170 | } |
| 171 | for k := range v { |
| 172 | if fe[k] == "" { |
| 173 | t.Errorf("%s: no message for %s", name, k) |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | var cards int64 |
| 178 | g.Model(&Card{}).Count(&cards) |
| 179 | if cards != 0 { |
| 180 | t.Fatalf("%d card(s) written despite invalid values", cards) |
| 181 | } |
| 182 | if _, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), map[string]string{FieldName: "Cnoc Teoranta", num.Key: "1,200", FieldStage: "Won"}); err != nil { |
| 183 | t.Fatalf("valid values refused: %v", err) |
| 184 | } |
| 185 | } |
| 186 | |