Skip to content
Repos

mologram

C++ 5.8 KB142 lines Opens in site

Directory Raw Download

// Stories beyond viewing (FR-51, FR-60): who sees a story, how long it stays, views, stealth, react, link, keep, delete. The 2 day duration,
// the view list and stealth mode are Premium extras, tested three ways: with Premium, without it, and with Premium lost midway.
#include "rig.hpp"
#include "mologram/ui/StoryTools.hpp"

using namespace uitest;

namespace {

ttgui::KeyEventData ch(const char* c) {
  ttgui::KeyEventData k;
  k.code = ttgui::KeyCode::Char;
  k.text = c;
  k.ch = (char32_t)c[0];
  return k;
}

media::ViewerItem story() { return {"story:user:100/7", "caption", false, "user:100", 7}; }

std::string makePhoto(Rig& r) {
  std::string path = r.tmp.path + "/pic.jpg";
  std::ofstream(path) << "not really a jpeg, the fake does not look";
  return path;
}

}  // namespace

TEST_CASE("story audience and duration choices never widen by accident", "[ui][stories][unit]") {
  CHECK_EQ(StoryTools::privacyValue(0), std::string("everyone"));
  CHECK_EQ(StoryTools::privacyValue(1), std::string("contacts"));
  CHECK_EQ(StoryTools::privacyValue(2), std::string("close_friends"));
  CHECK_EQ(StoryTools::privacyValue(3), std::string("self"));
  CHECK_EQ(StoryTools::privacyLabels().size(), (size_t)4);
  CHECK_EQ(StoryTools::periodSeconds(0), 21600);
  CHECK_EQ(StoryTools::periodSeconds(2), 86400);
  CHECK_EQ(StoryTools::periodSeconds(3), 172800);
  CHECK(StoryTools::periodNeedsPremium(172800));
  CHECK(!StoryTools::periodNeedsPremium(86400));
  PremiumModel none;
  CHECK(StoryTools::periodLabels(none, false)[3].find("\xF0\x9F\x94\x92") != std::string::npos);  // locked without Premium
  none.set(PremiumState::Premium);
  CHECK_EQ(StoryTools::periodLabels(none, false)[3], std::string("2 days"));
}

TEST_CASE("posting a story sends the chosen audience; the default duration is left to the server", "[ui][stories][integration]") {
  Rig r;
  std::string photo = makePhoto(r);
  r.action("story_post");
  ttgui::Window* w = r.window("PostStoryDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w, 0), photo);
  r.type(r.edit(w, 1), "hello");
  auto combos = r.widgets<ttgui::ComboBox>(w);
  REQUIRE(combos.size() == 2);
  combos[0]->selected = 3;  // Only me
  r.click(w, "Post");
  REQUIRE(r.rec.count("stories.send") == 1);
  js::Value p = r.rec.last("stories.send");
  CHECK_EQ(js::getStr(p, "privacy"), std::string("self"));
  CHECK_EQ(js::getStr(p, "caption"), std::string("hello"));
  CHECK(p.find("period") == nullptr);
  CHECK(p.find("pinned") == nullptr);
}

TEST_CASE("a 2 day story needs Premium: locked and explained without it, sent with it", "[ui][stories][premium][integration]") {
  Rig r;
  std::string photo = makePhoto(r);
  r.action("story_post");
  ttgui::Window* w = r.window("PostStoryDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w, 0), photo);
  auto combos = r.widgets<ttgui::ComboBox>(w);
  combos[1]->selected = 3;  // 2 days
  r.click(w, "Post");
  CHECK(r.hasWindow("PremiumPanel"));
  CHECK_EQ(r.rec.count("stories.send"), 0);
  r.click(r.window("PremiumPanel"), "OK");
  // with Premium the same choice goes through, together with "keep on my profile"
  r.svc.setPremium(PremiumState::Premium);
  r.settle();
  w = r.window("PostStoryDialog");
  REQUIRE(w != nullptr);
  r.widgets<ttgui::CheckBox>(w)[0]->checked = true;
  r.click(w, "Post");
  REQUIRE(r.rec.count("stories.send") == 1);
  js::Value p = r.rec.last("stories.send");
  CHECK_EQ(js::getInt(p, "period"), 172800);
  CHECK(js::getBool(p, "pinned"));
}

TEST_CASE("story keys: stealth and views are Premium extras, react link keep delete are for everybody", "[ui][stories][premium][integration]") {
  Rig r;
  auto press = [&](const char* k) { REQUIRE(StoryTools::handleKey(r.app.context(), ch(k), story())); r.settle(); };
  press("z");  // stealth without Premium
  CHECK(r.hasWindow("PremiumPanel"));
  CHECK_EQ(r.rec.count("stories.stealth"), 0);
  r.click(r.window("PremiumPanel"), "OK");
  press("v");  // view list without Premium
  CHECK(r.hasWindow("PremiumPanel"));
  CHECK_EQ(r.rec.count("stories.view_list"), 0);
  r.click(r.window("PremiumPanel"), "OK");
  press("r");
  CHECK_EQ(js::getStr(*r.rec.last("stories.react").find("reaction"), "emoji"), std::string("\xE2\x9D\xA4"));
  press("l");
  CHECK_EQ(r.rec.count("stories.link"), 1);
  press("k");
  CHECK(js::getBool(r.rec.last("stories.pin"), "pinned"));
  CHECK_EQ((int)js::getArr(r.rec.last("stories.pin"), "ids")->size(), 1);
  press("x");
  REQUIRE(r.hasWindow("StoryDeleteConfirm"));
  CHECK_EQ(r.rec.count("stories.delete"), 0);  // nothing is deleted before the explicit button
  r.click(r.window("StoryDeleteConfirm"), "Cancel");
  CHECK_EQ(r.rec.count("stories.delete"), 0);
  press("x");
  r.click(r.window("StoryDeleteConfirm"), "Delete");
  CHECK_EQ(r.rec.count("stories.delete"), 1);
  CHECK(!StoryTools::handleKey(r.app.context(), ch("q"), story()));  // not a story key
  media::ViewerItem photo{"tok", "t", false, "", 0};
  CHECK(!StoryTools::handleKey(r.app.context(), ch("z"), photo));    // a plain photo has no story keys
}

TEST_CASE("story extras with Premium, and with Premium lost midway", "[ui][stories][premium][integration]") {
  Rig r;
  r.svc.setPremium(PremiumState::Premium);
  r.settle();
  REQUIRE(StoryTools::handleKey(r.app.context(), ch("z"), story()));
  r.settle();
  CHECK_EQ(r.rec.count("stories.stealth"), 1);
  CHECK(!r.hasWindow("PremiumPanel"));
  REQUIRE(StoryTools::handleKey(r.app.context(), ch("v"), story()));
  r.settle();
  CHECK_EQ(r.rec.count("stories.view_list"), 1);
  CHECK(r.hasWindow("StoryViews"));
  r.click(r.window("StoryViews"), "Close");
  // the server stops honouring Premium while the UI still believes it: stealth is refused, the state flips and the panel explains
  r.svc.revokePremiumSilently();
  REQUIRE(r.app.premium().has());
  REQUIRE(StoryTools::handleKey(r.app.context(), ch("z"), story()));
  r.settle();
  CHECK(!r.app.premium().has());
  CHECK(r.hasWindow("PremiumPanel"));
}
51.5ms