| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "amadan.net/oficina/equipo/internal/wire" |
| 7 | ) |
| 8 | |
| 9 | func reachOf(logins ...wire.ReachLogin) wire.Hello { |
| 10 | return wire.Hello{Capabilities: &wire.Capabilities{Reach: &wire.Reach{Logins: logins}}} |
| 11 | } |
| 12 | |
| 13 | // siteCandidates' fixed rules (places spec §2): the flagship account named |
| 14 | // like the repository's namespace first (amadan.net/paulca/nearys: |
| 15 | // Carloku, account paulca; oficina projects: account oficina); else the |
| 16 | // only owned account; then flagship, then console and name; at most six; |
| 17 | // never an account the reach does not own. |
| 18 | func TestSiteCandidatesRank(t *testing.T) { |
| 19 | carloku := wire.ReachLogin{Console: wire.FlagshipConsole, Accounts: []wire.ReachAccount{{Name: "oficina", Sqid: "bcdfg"}, {Name: "paulca", Sqid: "kmnpq"}}} |
| 20 | tito := wire.ReachLogin{Console: wire.TitoConsole, Accounts: []wire.ReachAccount{{Name: "paulca", Sqid: "rstuv"}}} |
| 21 | names := func(cs []wire.PlaceCandidate) []string { |
| 22 | var out []string |
| 23 | for _, c := range cs { |
| 24 | out = append(out, c.ConsoleLabel+"/"+c.Account) |
| 25 | } |
| 26 | return out |
| 27 | } |
| 28 | for name, c := range map[string]struct { |
| 29 | h wire.Hello |
| 30 | repo string |
| 31 | want []string |
| 32 | }{ |
| 33 | "nearys": {reachOf(tito, carloku), "amadan.net/paulca/nearys", []string{"Carloku/paulca", "Carloku/oficina", "Tito/paulca"}}, |
| 34 | "oficina": {reachOf(carloku, tito), "amadan.net/oficina/equipo", []string{"Carloku/oficina", "Carloku/paulca", "Tito/paulca"}}, |
| 35 | "no match": {reachOf(tito, carloku), "amadan.net/someone/site", []string{"Carloku/oficina", "Carloku/paulca", "Tito/paulca"}}, |
| 36 | "only account": {reachOf(tito), "amadan.net/someone/site", []string{"Tito/paulca"}}, |
| 37 | "tito match?": {reachOf(tito), "amadan.net/paulca/nearys", []string{"Tito/paulca"}}, |
| 38 | "none": {wire.Hello{}, "amadan.net/paulca/nearys", nil}, |
| 39 | } { |
| 40 | got := siteCandidates(c.h, c.repo) |
| 41 | if len(got) != len(c.want) { |
| 42 | t.Fatalf("%s: %v", name, names(got)) |
| 43 | } |
| 44 | for i := range got { |
| 45 | if names(got)[i] != c.want[i] || got[i].ID != wire.CandidateID(got[i].Console, got[i].Sqid) { |
| 46 | t.Fatalf("%s: %v", name, names(got)) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | var many []wire.ReachAccount |
| 51 | for _, s := range []string{"bcd", "bce", "bcf", "bcg", "bch", "bcj", "bck", "bcm"} { |
| 52 | many = append(many, wire.ReachAccount{Name: "a" + s, Sqid: s}) |
| 53 | } |
| 54 | if got := siteCandidates(reachOf(wire.ReachLogin{Console: wire.FlagshipConsole, Accounts: many}), "amadan.net/x/y"); len(got) != wire.MaxPlaceOptions { |
| 55 | t.Fatalf("%d candidates", len(got)) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // A snapshot's target is everything but the key name, minted afresh for |
| 60 | // every card. |
| 61 | func TestPublishSnapshotTarget(t *testing.T) { |
| 62 | a := publishSnapshot{SHA: "e", Place: "pl_1", PlaceRevision: 1, Binding: "pp_1", BindingRevision: 1, Console: wire.FlagshipConsole, Sqid: "kmnpq", Name: "paulca", App: "site-nearys-abcdef", Mode: wire.SiteSetup, DeployName: "eq-nearys-abcdef-aaaa", Destination: "d"} |
| 63 | b := a |
| 64 | b.DeployName = "eq-nearys-abcdef-bbbb" |
| 65 | if !a.sameTarget(b) { |
| 66 | t.Fatal("the key name moved the target") |
| 67 | } |
| 68 | for _, f := range []func(s *publishSnapshot){ |
| 69 | func(s *publishSnapshot) { s.PlaceRevision++ }, |
| 70 | func(s *publishSnapshot) { s.BindingRevision++ }, |
| 71 | func(s *publishSnapshot) { s.Mode = wire.SiteMint }, |
| 72 | func(s *publishSnapshot) { s.Sqid = "bcdfg" }, |
| 73 | func(s *publishSnapshot) { s.SHA = "f" }, |
| 74 | } { |
| 75 | c := a |
| 76 | f(&c) |
| 77 | if a.sameTarget(c) { |
| 78 | t.Fatalf("%+v counted the same", c) |
| 79 | } |
| 80 | } |
| 81 | if n := deployName("site-" + "abcdefghijklmnopqrstuvwxyz0"); len(n) > 32 || !wire.ValidDeployName(n) { |
| 82 | t.Fatalf("deploy name %q", n) |
| 83 | } |
| 84 | } |
| 85 | |