package slidestest // What a session with a position says on the wire beside the position // itself (spec ยง6): the talk's timer, so every window shows the same // one whoever pressed; and the empty talk, which is a position too. import ( "bufio" "encoding/json" "math" "net/http" "strings" "testing" "time" ) // rawAnswer is a press's JSON answer, every key as sent. func rawAnswer(t *testing.T, a *testApp, session, token, name string) map[string]any { t.Helper() rec := pressRequest(t, a, session, token, op(t, name), nil) if rec.Code != http.StatusOK { t.Fatalf("press %s: status %d\n%s", name, rec.Code, rec.Body) } var out map[string]any if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { t.Fatalf("press %s: %v\n%s", name, err, rec.Body) } return out } // rawEvent is the stream's next slide event, every key as sent. func rawEvent(t *testing.T, r *bufio.Reader) map[string]any { t.Helper() data := nextSlideEvent(t, r) var out map[string]any if err := json.Unmarshal([]byte(data), &out); err != nil { t.Fatalf("the stream's event %q: %v", data, err) } return out } // timerOf is the timerAt key: present, and a number of ms or null. func timerOf(t *testing.T, what string, m map[string]any) (ms float64, running bool) { t.Helper() v, ok := m["timerAt"] if !ok { t.Fatalf("%s carries no timerAt: %v", what, m) } if v == nil { return 0, false } n, ok := v.(float64) if !ok { t.Fatalf("%s: timerAt %v is neither a number nor null", what, v) } return n, true } // TestTheTimerTravelsWithThePosition: every answer and every event of a // session with a position says when its timer started, in Unix ms, or // null while it waits for the first press; a reset says null again. A // session begun before positions says nothing about a timer. func TestTheTimerTravelsWithThePosition(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) events := openStream(t, a, "/p/"+session+"/stream") if _, running := timerOf(t, "the first event", rawEvent(t, events)); running { t.Error("the timer runs before the first press") } before := time.Now() answer := rawAnswer(t, a, session, token, "next") after := time.Now() at, running := timerOf(t, "next's answer", answer) if !running || at < float64(before.Add(-time.Second).UnixMilli()) || at > float64(after.Add(time.Second).UnixMilli()) { t.Fatalf("next's answer says the timer started at %v (running %v), want about %d", at, running, before.UnixMilli()) } if got, _ := timerOf(t, "next's event", rawEvent(t, events)); math.Abs(got-at) > 0.5 { t.Errorf("next's event says %v, its answer %v", got, at) } // A second press leaves it where it started. if got, _ := timerOf(t, "the second answer", rawAnswer(t, a, session, token, "next")); math.Abs(got-at) > 0.5 { t.Errorf("a second press moved the timer to %v from %v", got, at) } rawEvent(t, events) if _, running := timerOf(t, "reset's answer", rawAnswer(t, a, session, token, "timer-reset")); running { t.Error("the timer runs after a reset") } if _, running := timerOf(t, "reset's event", rawEvent(t, events)); running { t.Error("the stream says the timer runs after a reset") } // Before positions: three fields, as always. preDeploy(t, a, session) old := rawEvent(t, openStream(t, a, "/p/"+session+"/stream")) for _, key := range []string{"timerAt", "slideId", "seq"} { if _, ok := old[key]; ok { t.Errorf("a pre-deploy session's event carries %s: %v", key, old) } } } // TestAnEmptyTalkIsAPosition: once every slide is parked the talk is // empty, and that is a position like any other: the answer and the // event name no slide (an empty slideId, present), still numbered, and // a press there answers the same. func TestAnEmptyTalkIsAPosition(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path := stepsDeck(t, a, cookies) session, token := startPresenting(t, a, path, cookies) events := openStream(t, a, "/p/"+session+"/stream") rawEvent(t, events) agent := mintAgent(t, a, cookies, "parker") sqid := strings.TrimPrefix(path, "/d/") view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, agent, "", http.StatusOK) var ops []string for _, s := range view["slides"].([]any) { ops = append(ops, `{"op":"set_slide","id":`+quote(s.(map[string]any)["id"].(string))+`,"talk":"parked"}`) } apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) answer := rawAnswer(t, a, session, token, "next") if slide, ok := answer["slide"]; !ok || slide != "" { t.Errorf("a press on an empty talk answers slide %v, want \"\"", slide) } e := rawEvent(t, events) if slide, ok := e["slideId"]; !ok || slide != "" { t.Errorf("the event of an empty talk has slideId %v (present %v), want \"\"", slide, ok) } if seq, ok := e["seq"].(float64); !ok || int64(seq) != int64(answer["seq"].(float64)) { t.Errorf("the event of an empty talk has seq %v, its answer %v", e["seq"], answer["seq"]) } if _, ok := e["timerAt"]; !ok { t.Errorf("the event of an empty talk carries no timerAt: %v", e) } }