eleven / messenger Public

Clone
git clone https://amadan.net/eleven/messenger

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestReleaseAlertText(t *testing.T) {
9 e := releaseEntry{Version: "abc1234", Semver: "0.12.0"}
10
11 // stable + auto-update on: the safety-hold variant, semver-labelled.
12 title, body, ok := releaseAlertText("Familia", "stable", e, true, "")
13 if !ok {
14 t.Fatal("stable sighting should alert")
15 }
16 if !strings.Contains(title, "Familia") {
17 t.Fatalf("title misses the space name: %q", title)
18 }
19 if !strings.Contains(body, "v0.12.0 (abc1234)") || !strings.Contains(body, "72 hours") {
20 t.Fatalf("hold body = %q", body)
21 }
22
23 // Automatic updates off: no "installs automatically" promise.
24 _, body, ok = releaseAlertText("Familia", "stable", e, false, "")
25 if !ok || strings.Contains(body, "installs automatically") || !strings.Contains(body, "Automatic updates are off") {
26 t.Fatalf("updates-off body = %q (ok=%v)", body, ok)
27 }
28
29 // A major hold wins over the bake copy — the admin's action is different.
30 held := releaseEntry{Version: "abc1234", Semver: "1.0.0"}
31 _, body, ok = releaseAlertText("Familia", "stable", held, true, "0")
32 if !ok || !strings.Contains(body, "major version") {
33 t.Fatalf("major-held body = %q (ok=%v)", body, ok)
34 }
35
36 // edge never alerts: its riders are the operators doing the publishing.
37 if _, _, ok = releaseAlertText("Familia", "edge", e, true, ""); ok {
38 t.Fatal("edge sighting alerted")
39 }
40 // ...unless a major pin holds even edge — that waits on the admin.
41 if _, _, ok = releaseAlertText("Familia", "edge", held, true, "0"); !ok {
42 t.Fatal("major-held edge sighting should alert")
43 }
44}
45
46func TestNoteReleaseSightings(t *testing.T) {
47 _, s := newTestServer(t)
48 stable := func(ver, sv string) *releaseManifest {
49 return &releaseManifest{Channels: map[string]releaseEntry{"stable": {Version: ver, Semver: sv}}}
50 }
51
52 // First sighting on the channel baselines silently.
53 if alerted, _ := s.noteReleaseSightings(stable("aaa1111", "")); alerted {
54 t.Fatal("first sighting should baseline, not alert")
55 }
56 // The same head again is quiet.
57 if alerted, _ := s.noteReleaseSightings(stable("aaa1111", "")); alerted {
58 t.Fatal("unchanged head alerted")
59 }
60 // The head moving is the alert (sent stays 0 — no push subscriptions here).
61 alerted, sent := s.noteReleaseSightings(stable("bbb2222", "0.12.0"))
62 if !alerted || sent != 0 {
63 t.Fatalf("moved head: alerted=%v sent=%d", alerted, sent)
64 }
65 // At-most-once: the watermark advanced, so the same move doesn't replay.
66 if alerted, _ := s.noteReleaseSightings(stable("bbb2222", "0.12.0")); alerted {
67 t.Fatal("alert replayed for the same head")
68 }
69 // A head we're already running (a ship landed, or a pull-back onto us) is
70 // not "on the way" — buildVersion is "dev" under test.
71 if alerted, _ := s.noteReleaseSightings(stable(buildVersion, "")); alerted {
72 t.Fatal("alerted for the build we already run")
73 }
74 // A corrupt watermark re-baselines rather than announcing a backlog.
75 if err := s.db.setConfig(releaseSeenKey, "not-json"); err != nil {
76 t.Fatal(err)
77 }
78 if alerted, _ := s.noteReleaseSightings(stable("ccc3333", "")); alerted {
79 t.Fatal("corrupt watermark should re-baseline silently")
80 }
81 if alerted, _ := s.noteReleaseSightings(stable("ddd4444", "")); !alerted {
82 t.Fatal("post-re-baseline move should alert")
83 }
84
85 // An edge-riding space stays quiet through head moves.
86 if err := s.db.setUpdateChannel("edge"); err != nil {
87 t.Fatal(err)
88 }
89 edge := func(ver string) *releaseManifest {
90 return &releaseManifest{Channels: map[string]releaseEntry{"edge": {Version: ver}}}
91 }
92 s.noteReleaseSightings(edge("eee5555")) // baseline the edge channel
93 if alerted, _ := s.noteReleaseSightings(edge("fff6666")); alerted {
94 t.Fatal("edge head move alerted")
95 }
96}
97