| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // TestSiteSlug: the slug comes from the repository's name alone: |
| 13 | // lower-case letters and digits, every other run one hyphen, at most 20 |
| 14 | // characters. Whatever the name holds (flags, paths, spaces, newlines, |
| 15 | // another account, other scripts), a minted app from it is valid. |
| 16 | func TestSiteSlug(t *testing.T) { |
| 17 | for repo, want := range map[string]string{ |
| 18 | "amadan.net/oficina/sample": "sample", |
| 19 | "amadan.net/home/Quick.Site_2": "quick-site-2", |
| 20 | "amadan.net/oficina/--help": "help", |
| 21 | "amadan.net/oficina/x --account other": "x-account-other", |
| 22 | "amadan.net/oficina/..": "", |
| 23 | "amadan.net/oficina/café-menu": "caf-menu", |
| 24 | "amadan.net/oficina/a\nb": "a-b", |
| 25 | "amadan.net/oficina/" + strings.Repeat("abc-", 20): "abc-abc-abc-abc-abc", |
| 26 | "amadan.net/oficina/日本": "", |
| 27 | "": "", |
| 28 | } { |
| 29 | if got := siteSlug(repo); got != want { |
| 30 | t.Fatalf("%q gave %q, want %q", repo, got, want) |
| 31 | } |
| 32 | if app := mintSiteApp(repo); !wire.ValidSiteApp(app) { |
| 33 | t.Fatalf("%q minted %q", repo, app) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // TestProgressSkipsPublishes: a publish step never drives the task. |
| 39 | func TestProgressSkipsPublishes(t *testing.T) { |
| 40 | cand := strings.Repeat("2", 40) |
| 41 | steps := []stepRecord{ |
| 42 | {ID: "e", Specialist: wire.SpecialistEngineering, State: wire.StepDone, Pushed: true, CandidateSHA: cand}, |
| 43 | {ID: "r", Specialist: wire.SpecialistReview, State: wire.StepDone, Verdict: wire.VerdictApprove, CandidateSHA: cand}, |
| 44 | {ID: "m", Specialist: wire.SpecialistMerge, State: wire.StepDone, CandidateSHA: cand}, |
| 45 | {ID: "p", Specialist: wire.SpecialistPublish, State: wire.StepFailed, Reason: "publish_failed", Terminated: true, CandidateSHA: strings.Repeat("e", 40)}, |
| 46 | } |
| 47 | if p := progressOf(steps); p.last == nil || p.last.ID != "m" { |
| 48 | t.Fatalf("progress %+v", p.last) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // TestSiteAppMinted: a project's app is minted once, at its first publish |
| 53 | // card, as site-<slug>-<six random lower-case base32 characters> within |
| 54 | // Carloku's rules, and stored on the project; two projects whose names |
| 55 | // give the same slug get different apps; a later publish reuses the |
| 56 | // stored name; no two projects can hold one app. |
| 57 | func TestSiteAppMinted(t *testing.T) { |
| 58 | r := newHKRig(t) |
| 59 | r.project("p1", "amadan.net/oficina/sample") |
| 60 | r.project("p2", "amadan.net/home/Sample") |
| 61 | r.project("p3", "amadan.net/home/"+strings.Repeat("long-name-", 6)) |
| 62 | minted := map[string]string{} |
| 63 | mint := func(id string) string { |
| 64 | var app string |
| 65 | r.must(r.tx(func(t *txn) error { |
| 66 | var pr projectRecord |
| 67 | if err := t.db.Where("id = ?", id).First(&pr).Error; err != nil { |
| 68 | return err |
| 69 | } |
| 70 | got, ok, err := t.projectSite(pr) |
| 71 | if err != nil || !ok { |
| 72 | return fmt.Errorf("no app for %s: %v", id, err) |
| 73 | } |
| 74 | app = got |
| 75 | return nil |
| 76 | })) |
| 77 | return app |
| 78 | } |
| 79 | shape := regexp.MustCompile(`^site-[a-z0-9-]+-[a-z2-7]{6}$`) |
| 80 | for _, id := range []string{"p1", "p2", "p3"} { |
| 81 | app := mint(id) |
| 82 | if !shape.MatchString(app) || !wire.ValidSiteApp(app) { |
| 83 | t.Fatalf("%s minted %q", id, app) |
| 84 | } |
| 85 | minted[id] = app |
| 86 | } |
| 87 | if !strings.HasPrefix(minted["p1"], "site-sample-") || !strings.HasPrefix(minted["p2"], "site-sample-") || minted["p1"] == minted["p2"] { |
| 88 | t.Fatalf("same slug, apps %q %q", minted["p1"], minted["p2"]) |
| 89 | } |
| 90 | if !strings.HasPrefix(minted["p3"], "site-long-name-long-name-") { |
| 91 | t.Fatalf("long name %q", minted["p3"]) |
| 92 | } |
| 93 | for id, app := range minted { |
| 94 | if again := mint(id); again != app { |
| 95 | t.Fatalf("%s: %q then %q", id, app, again) |
| 96 | } |
| 97 | var stored string |
| 98 | r.must(r.tx(func(t *txn) error { |
| 99 | return t.db.Raw("SELECT site_app FROM chat_projects WHERE id = ?", id).Scan(&stored).Error |
| 100 | })) |
| 101 | if stored != app { |
| 102 | t.Fatalf("%s stored %q, minted %q", id, stored, app) |
| 103 | } |
| 104 | } |
| 105 | err := r.tx(func(t *txn) error { |
| 106 | return t.db.Exec("UPDATE chat_projects SET site_app = ? WHERE id = ?", minted["p1"], "p2").Error |
| 107 | }) |
| 108 | if err == nil { |
| 109 | t.Fatal("two projects hold one app") |
| 110 | } |
| 111 | } |
| 112 | |