| 1 | package console |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestMatchAccounts(t *testing.T) { |
| 6 | accts := []Account{ |
| 7 | {ID: 1, Sqid: "bab", Name: "ops"}, |
| 8 | {ID: 2, Sqid: "bah", Name: "amadan"}, |
| 9 | {ID: 3, Sqid: "bad", Name: "amadan"}, |
| 10 | {ID: 4, Sqid: "baf", Name: "bah"}, // name colliding with #2's sqid |
| 11 | {ID: 5, Sqid: "bag", Name: "Mixed"}, |
| 12 | } |
| 13 | cases := []struct { |
| 14 | seg string |
| 15 | want []int64 // matched account IDs, in accts order |
| 16 | }{ |
| 17 | {"bab", []int64{1}}, // sqid only |
| 18 | {"ops", []int64{1}}, // name only |
| 19 | {"amadan", []int64{2, 3}}, // two accounts share the name |
| 20 | {"bah", []int64{2, 4}}, // sqid of one, name of another |
| 21 | {"mixed", []int64{5}}, // names match case-insensitively |
| 22 | {"zzz", nil}, // nothing |
| 23 | } |
| 24 | for _, c := range cases { |
| 25 | got := matchAccounts(accts, c.seg) |
| 26 | var ids []int64 |
| 27 | for _, a := range got { |
| 28 | ids = append(ids, a.ID) |
| 29 | } |
| 30 | if len(ids) != len(c.want) { |
| 31 | t.Errorf("matchAccounts(%q) = %v, want %v", c.seg, ids, c.want) |
| 32 | continue |
| 33 | } |
| 34 | for i := range ids { |
| 35 | if ids[i] != c.want[i] { |
| 36 | t.Errorf("matchAccounts(%q) = %v, want %v", c.seg, ids, c.want) |
| 37 | break |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // An account whose NAME is its own sqid is one account, not two matches — |
| 44 | // otherwise the picker would ask a viewer to disambiguate between an account |
| 45 | // and itself. |
| 46 | func TestMatchAccountsCountsSelfCollisionOnce(t *testing.T) { |
| 47 | accts := []Account{{ID: 1, Sqid: "bab", Name: "bab"}} |
| 48 | if got := matchAccounts(accts, "bab"); len(got) != 1 || got[0].ID != 1 { |
| 49 | t.Errorf("matchAccounts(self-collision) = %+v, want the one account", got) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // The returned pointers address the CALLER's slice, so a match carries the |
| 54 | // Role AccountsFor filled in — s.account returns it as the viewer's role. |
| 55 | func TestMatchAccountsKeepsRole(t *testing.T) { |
| 56 | accts := []Account{{ID: 7, Sqid: "bab", Name: "ops", Role: RoleOwner}} |
| 57 | got := matchAccounts(accts, "ops") |
| 58 | if len(got) != 1 || got[0].Role != RoleOwner { |
| 59 | t.Errorf("matchAccounts dropped the role: %+v", got) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestPreferredSeg(t *testing.T) { |
| 64 | accts := []Account{ |
| 65 | {ID: 1, Sqid: "bab", Name: "ops"}, |
| 66 | {ID: 2, Sqid: "bah", Name: "amadan"}, |
| 67 | {ID: 3, Sqid: "bad", Name: "amadan"}, |
| 68 | {ID: 4, Sqid: "baf", Name: "paul@keymail.dev"}, |
| 69 | {ID: 5, Sqid: "bag", Name: "bah"}, // collides with #2's sqid |
| 70 | } |
| 71 | cases := []struct { |
| 72 | id int64 |
| 73 | want string |
| 74 | }{ |
| 75 | {1, "ops"}, // unique slug-shaped name → the name |
| 76 | {2, "bah"}, // name shared with #3 → sqid |
| 77 | {3, "bad"}, // same |
| 78 | {4, "baf"}, // address-named (auto-mint) → sqid |
| 79 | {5, "bag"}, // name equals #2's sqid → sqid |
| 80 | } |
| 81 | for _, c := range cases { |
| 82 | var a *Account |
| 83 | for i := range accts { |
| 84 | if accts[i].ID == c.id { |
| 85 | a = &accts[i] |
| 86 | } |
| 87 | } |
| 88 | if got := preferredSeg(accts, a); got != c.want { |
| 89 | t.Errorf("preferredSeg(#%d) = %q, want %q", c.id, got, c.want) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |