| 1 | package slidestest |
| 2 | |
| 3 | // What a session with a position says on the wire beside the position |
| 4 | // itself (spec §6): the talk's timer, so every window shows the same |
| 5 | // one whoever pressed; and the empty talk, which is a position too. |
| 6 | |
| 7 | import ( |
| 8 | "bufio" |
| 9 | "encoding/json" |
| 10 | "math" |
| 11 | "net/http" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | // rawAnswer is a press's JSON answer, every key as sent. |
| 18 | func rawAnswer(t *testing.T, a *testApp, session, token, name string) map[string]any { |
| 19 | t.Helper() |
| 20 | rec := pressRequest(t, a, session, token, op(t, name), nil) |
| 21 | if rec.Code != http.StatusOK { |
| 22 | t.Fatalf("press %s: status %d\n%s", name, rec.Code, rec.Body) |
| 23 | } |
| 24 | var out map[string]any |
| 25 | if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { |
| 26 | t.Fatalf("press %s: %v\n%s", name, err, rec.Body) |
| 27 | } |
| 28 | return out |
| 29 | } |
| 30 | |
| 31 | // rawEvent is the stream's next slide event, every key as sent. |
| 32 | func rawEvent(t *testing.T, r *bufio.Reader) map[string]any { |
| 33 | t.Helper() |
| 34 | data := nextSlideEvent(t, r) |
| 35 | var out map[string]any |
| 36 | if err := json.Unmarshal([]byte(data), &out); err != nil { |
| 37 | t.Fatalf("the stream's event %q: %v", data, err) |
| 38 | } |
| 39 | return out |
| 40 | } |
| 41 | |
| 42 | // timerOf is the timerAt key: present, and a number of ms or null. |
| 43 | func timerOf(t *testing.T, what string, m map[string]any) (ms float64, running bool) { |
| 44 | t.Helper() |
| 45 | v, ok := m["timerAt"] |
| 46 | if !ok { |
| 47 | t.Fatalf("%s carries no timerAt: %v", what, m) |
| 48 | } |
| 49 | if v == nil { |
| 50 | return 0, false |
| 51 | } |
| 52 | n, ok := v.(float64) |
| 53 | if !ok { |
| 54 | t.Fatalf("%s: timerAt %v is neither a number nor null", what, v) |
| 55 | } |
| 56 | return n, true |
| 57 | } |
| 58 | |
| 59 | // TestTheTimerTravelsWithThePosition: every answer and every event of a |
| 60 | // session with a position says when its timer started, in Unix ms, or |
| 61 | // null while it waits for the first press; a reset says null again. A |
| 62 | // session begun before positions says nothing about a timer. |
| 63 | func TestTheTimerTravelsWithThePosition(t *testing.T) { |
| 64 | a := newApp(t) |
| 65 | cookies := signedIn(t, a, allowedAddress) |
| 66 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 67 | events := openStream(t, a, "/p/"+session+"/stream") |
| 68 | |
| 69 | if _, running := timerOf(t, "the first event", rawEvent(t, events)); running { |
| 70 | t.Error("the timer runs before the first press") |
| 71 | } |
| 72 | |
| 73 | before := time.Now() |
| 74 | answer := rawAnswer(t, a, session, token, "next") |
| 75 | after := time.Now() |
| 76 | at, running := timerOf(t, "next's answer", answer) |
| 77 | if !running || at < float64(before.Add(-time.Second).UnixMilli()) || at > float64(after.Add(time.Second).UnixMilli()) { |
| 78 | t.Fatalf("next's answer says the timer started at %v (running %v), want about %d", at, running, before.UnixMilli()) |
| 79 | } |
| 80 | if got, _ := timerOf(t, "next's event", rawEvent(t, events)); math.Abs(got-at) > 0.5 { |
| 81 | t.Errorf("next's event says %v, its answer %v", got, at) |
| 82 | } |
| 83 | |
| 84 | // A second press leaves it where it started. |
| 85 | if got, _ := timerOf(t, "the second answer", rawAnswer(t, a, session, token, "next")); math.Abs(got-at) > 0.5 { |
| 86 | t.Errorf("a second press moved the timer to %v from %v", got, at) |
| 87 | } |
| 88 | rawEvent(t, events) |
| 89 | |
| 90 | if _, running := timerOf(t, "reset's answer", rawAnswer(t, a, session, token, "timer-reset")); running { |
| 91 | t.Error("the timer runs after a reset") |
| 92 | } |
| 93 | if _, running := timerOf(t, "reset's event", rawEvent(t, events)); running { |
| 94 | t.Error("the stream says the timer runs after a reset") |
| 95 | } |
| 96 | |
| 97 | // Before positions: three fields, as always. |
| 98 | preDeploy(t, a, session) |
| 99 | old := rawEvent(t, openStream(t, a, "/p/"+session+"/stream")) |
| 100 | for _, key := range []string{"timerAt", "slideId", "seq"} { |
| 101 | if _, ok := old[key]; ok { |
| 102 | t.Errorf("a pre-deploy session's event carries %s: %v", key, old) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // TestAnEmptyTalkIsAPosition: once every slide is parked the talk is |
| 108 | // empty, and that is a position like any other: the answer and the |
| 109 | // event name no slide (an empty slideId, present), still numbered, and |
| 110 | // a press there answers the same. |
| 111 | func TestAnEmptyTalkIsAPosition(t *testing.T) { |
| 112 | a := newApp(t) |
| 113 | cookies := signedIn(t, a, allowedAddress) |
| 114 | path := stepsDeck(t, a, cookies) |
| 115 | session, token := startPresenting(t, a, path, cookies) |
| 116 | events := openStream(t, a, "/p/"+session+"/stream") |
| 117 | rawEvent(t, events) |
| 118 | |
| 119 | agent := mintAgent(t, a, cookies, "parker") |
| 120 | sqid := strings.TrimPrefix(path, "/d/") |
| 121 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, agent, "", http.StatusOK) |
| 122 | var ops []string |
| 123 | for _, s := range view["slides"].([]any) { |
| 124 | ops = append(ops, `{"op":"set_slide","id":`+quote(s.(map[string]any)["id"].(string))+`,"talk":"parked"}`) |
| 125 | } |
| 126 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 127 | |
| 128 | answer := rawAnswer(t, a, session, token, "next") |
| 129 | if slide, ok := answer["slide"]; !ok || slide != "" { |
| 130 | t.Errorf("a press on an empty talk answers slide %v, want \"\"", slide) |
| 131 | } |
| 132 | e := rawEvent(t, events) |
| 133 | if slide, ok := e["slideId"]; !ok || slide != "" { |
| 134 | t.Errorf("the event of an empty talk has slideId %v (present %v), want \"\"", slide, ok) |
| 135 | } |
| 136 | if seq, ok := e["seq"].(float64); !ok || int64(seq) != int64(answer["seq"].(float64)) { |
| 137 | t.Errorf("the event of an empty talk has seq %v, its answer %v", e["seq"], answer["seq"]) |
| 138 | } |
| 139 | if _, ok := e["timerAt"]; !ok { |
| 140 | t.Errorf("the event of an empty talk carries no timerAt: %v", e) |
| 141 | } |
| 142 | } |
| 143 | |