| 1 | package fichas |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | // TestEveryObjectKindIsNamedInSync is LU12's feed half: every object |
| 6 | // kind Fichas writes is named in Sync's switch, so none falls through |
| 7 | // to the full rebuild, and a view, file or draft event reloads nothing. |
| 8 | // A kind nobody names still rebuilds, the safe default. |
| 9 | // |
| 10 | // Mutation, 2026-11-17: deleting the ObjectField case (so a field event |
| 11 | // fell through) failed this test at the first fullLoads check. |
| 12 | func TestEveryObjectKindIsNamedInSync(t *testing.T) { |
| 13 | g, s := seeded(t) |
| 14 | ix := NewIndex(g, s) |
| 15 | card, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), map[string]string{FieldName: "Cnoc"}) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | mustQuery(t, ix, Query{}) |
| 20 | |
| 21 | ct, err := CreateType(ctx(), g, s, Person(1), "Project", "Projects") |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | fd, err := AddField(ctx(), g, s, TypeCustomer, Person(1), FieldSpec{Label: "Budget", Kind: KindNumber, Tier: TierSealed}) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if err := UpdateField(ctx(), g, s, fd.Key, Person(1), "Budget (EUR)", nil); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | for _, kind := range []string{ObjectView, ObjectFile, ObjectDraft, ObjectMember} { |
| 33 | if err := g.Create(&Event{Kind: EventUpdated, ObjectKind: kind, ObjectID: ct.ID, ChangedFields: "[]", Nonce: 1}).Error; err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | } |
| 37 | UpdateCard(ctx(), g, s, card.ID, Person(1), map[string]string{FieldName: "Cnoc Teoranta"}) |
| 38 | mustQuery(t, ix, Query{}) |
| 39 | if ix.fullLoads != 0 { |
| 40 | t.Fatalf("a named object kind rebuilt the whole index (%d rebuilds)", ix.fullLoads) |
| 41 | } |
| 42 | fields, _ := ix.Fields(TypeCustomer) |
| 43 | found := false |
| 44 | for _, f := range fields { |
| 45 | found = found || f.Label == "Budget (EUR)" |
| 46 | } |
| 47 | if !found { |
| 48 | t.Fatal("a field's rename did not reach the index") |
| 49 | } |
| 50 | if _, err := ix.Fields(ct.Key); err != nil { |
| 51 | t.Fatalf("a new type did not reach the index: %v", err) |
| 52 | } |
| 53 | if got := names(mustQuery(t, ix, Query{})); len(got) != 1 || got[0] != "Cnoc Teoranta" { |
| 54 | t.Fatalf("a card edit after other kinds: %v", got) |
| 55 | } |
| 56 | |
| 57 | g.Create(&Event{Kind: EventUpdated, ObjectKind: "something-new", ChangedFields: "[]", Nonce: 1}) |
| 58 | mustQuery(t, ix, Query{}) |
| 59 | if ix.fullLoads != 1 { |
| 60 | t.Fatalf("an unnamed kind did not rebuild (%d rebuilds)", ix.fullLoads) |
| 61 | } |
| 62 | } |
| 63 | |