package console import "testing" func TestMatchAccounts(t *testing.T) { accts := []Account{ {ID: 1, Sqid: "bab", Name: "ops"}, {ID: 2, Sqid: "bah", Name: "amadan"}, {ID: 3, Sqid: "bad", Name: "amadan"}, {ID: 4, Sqid: "baf", Name: "bah"}, // name colliding with #2's sqid {ID: 5, Sqid: "bag", Name: "Mixed"}, } cases := []struct { seg string want []int64 // matched account IDs, in accts order }{ {"bab", []int64{1}}, // sqid only {"ops", []int64{1}}, // name only {"amadan", []int64{2, 3}}, // two accounts share the name {"bah", []int64{2, 4}}, // sqid of one, name of another {"mixed", []int64{5}}, // names match case-insensitively {"zzz", nil}, // nothing } for _, c := range cases { got := matchAccounts(accts, c.seg) var ids []int64 for _, a := range got { ids = append(ids, a.ID) } if len(ids) != len(c.want) { t.Errorf("matchAccounts(%q) = %v, want %v", c.seg, ids, c.want) continue } for i := range ids { if ids[i] != c.want[i] { t.Errorf("matchAccounts(%q) = %v, want %v", c.seg, ids, c.want) break } } } } // An account whose NAME is its own sqid is one account, not two matches — // otherwise the picker would ask a viewer to disambiguate between an account // and itself. func TestMatchAccountsCountsSelfCollisionOnce(t *testing.T) { accts := []Account{{ID: 1, Sqid: "bab", Name: "bab"}} if got := matchAccounts(accts, "bab"); len(got) != 1 || got[0].ID != 1 { t.Errorf("matchAccounts(self-collision) = %+v, want the one account", got) } } // The returned pointers address the CALLER's slice, so a match carries the // Role AccountsFor filled in — s.account returns it as the viewer's role. func TestMatchAccountsKeepsRole(t *testing.T) { accts := []Account{{ID: 7, Sqid: "bab", Name: "ops", Role: RoleOwner}} got := matchAccounts(accts, "ops") if len(got) != 1 || got[0].Role != RoleOwner { t.Errorf("matchAccounts dropped the role: %+v", got) } } func TestPreferredSeg(t *testing.T) { accts := []Account{ {ID: 1, Sqid: "bab", Name: "ops"}, {ID: 2, Sqid: "bah", Name: "amadan"}, {ID: 3, Sqid: "bad", Name: "amadan"}, {ID: 4, Sqid: "baf", Name: "paul@keymail.dev"}, {ID: 5, Sqid: "bag", Name: "bah"}, // collides with #2's sqid } cases := []struct { id int64 want string }{ {1, "ops"}, // unique slug-shaped name → the name {2, "bah"}, // name shared with #3 → sqid {3, "bad"}, // same {4, "baf"}, // address-named (auto-mint) → sqid {5, "bag"}, // name equals #2's sqid → sqid } for _, c := range cases { var a *Account for i := range accts { if accts[i].ID == c.id { a = &accts[i] } } if got := preferredSeg(accts, a); got != c.want { t.Errorf("preferredSeg(#%d) = %q, want %q", c.id, got, c.want) } } }