Skip to content
Repos

mologram

C++ 4.1 KB110 lines Opens in site

Directory Raw Download

// SPEC.md drift test (SPEC 0.1): machine-checked tables equal the code registries; IDs unique; changelog ascending.
#include <map>
#include <regex>
#include <set>

#include "tttest.hpp"
#include "mologram/ActionRegistry.hpp"
#include "mologram/ConfigSchema.hpp"
#include "mologram/PremiumFeatures.hpp"
#include "mologram/ProtocolMethods.hpp"
#include "mologram/SpecDocument.hpp"
#include "mologram/WidgetNames.hpp"

using mologram::ProtocolMethods;
using mologram::SpecDocument;

static SpecDocument& spec() {
  static SpecDocument d(std::string(MOLOGRAM_SOURCE_DIR) + "/SPEC.md");
  return d;
}

TEST_CASE("spec file loads and has every machine-checked table", "[spec][unit]") {
  REQUIRE(spec().error().empty());
  for (const char* t : {"config", "actions", "methods", "premium", "widgets"}) CHECK(spec().hasTable(t));
}

TEST_CASE("spec config table equals ConfigSchema", "[spec][unit]") {
  const auto& rows = spec().table("config");
  const auto& keys = mologram::ConfigSchema::keys();
  REQUIRE(rows.size() == keys.size());
  for (size_t i = 0; i < keys.size(); i++) {
    CHECK_EQ(rows[i][0], keys[i].name);
    CHECK_EQ(rows[i][1], mologram::ConfigSchema::typeName(keys[i].type));
    CHECK_EQ(rows[i][2], keys[i].defaultText);
    CHECK_EQ(rows[i][3], std::string(keys[i].secret ? "yes" : "no"));
  }
}

TEST_CASE("spec actions table equals ActionRegistry", "[spec][unit]") {
  const auto& rows = spec().table("actions");
  const auto& acts = mologram::ActionRegistry::all();
  REQUIRE(rows.size() == acts.size());
  for (size_t i = 0; i < acts.size(); i++) {
    CHECK_EQ(rows[i][0], acts[i].id);
    CHECK_EQ(rows[i][1], acts[i].defaultKey);
    CHECK_EQ(rows[i][2], acts[i].menu);
  }
}

TEST_CASE("spec methods table equals ProtocolMethods", "[spec][unit]") {
  const auto& rows = spec().table("methods");
  const auto& m = mologram::ProtocolMethods::all();
  REQUIRE(rows.size() == m.size());
  for (size_t i = 0; i < m.size(); i++) {
    CHECK_EQ(rows[i][0], m[i].name);
    CHECK_EQ(rows[i][1], m[i].params);
    CHECK_EQ(rows[i][2], m[i].result);
  }
}

TEST_CASE("spec premium table equals PremiumFeatures", "[spec][unit]") {
  const auto& rows = spec().table("premium");
  const auto& f = mologram::PremiumFeatures::all();
  REQUIRE(rows.size() == f.size());
  for (size_t i = 0; i < f.size(); i++) {
    CHECK_EQ(rows[i][0], f[i].id);
    CHECK_EQ(rows[i][1], f[i].gatedCall);
    CHECK_EQ(rows[i][2], f[i].fallback);
    CHECK_EQ(rows[i][3], f[i].method);
    CHECK(ProtocolMethods::known(f[i].method));  // every Premium feature is reachable through a declared protocol method
  }
}

TEST_CASE("spec widgets table equals WidgetNames", "[spec][unit]") {
  const auto& rows = spec().table("widgets");
  const auto& w = mologram::WidgetNames::all();
  REQUIRE(rows.size() == w.size());
  for (size_t i = 0; i < w.size(); i++) {
    CHECK_EQ(rows[i][0], w[i].name);
    CHECK_EQ(rows[i][1], w[i].role);
  }
}

TEST_CASE("spec action ids are unique and default keys are not duplicated except by decision D-08", "[spec][unit]") {
  std::set<std::string> ids;
  std::map<std::string, int> keys;
  for (const auto& a : mologram::ActionRegistry::all()) {
    CHECK(ids.insert(a.id).second);
    keys[a.defaultKey]++;
  }
  for (const auto& kv : keys) {
    // Ctrl+K is shared by search and fmt_link (D-08); the alt navigation keys are distinct from the ctrl ones.
    if (kv.second > 1) CHECK_EQ(kv.first, std::string("Ctrl+K"));
  }
}

TEST_CASE("spec definition IDs are unique and the changelog ascends", "[spec][unit]") {
  const std::string& t = spec().text();
  std::regex def(R"((?:^|\n)- ((?:G|NG|FR|NFR|UX|SEC|OPS|DOC|TRK|D|PIN|A|R)-[0-9]+):)");
  std::set<std::string> seen;
  for (std::sregex_iterator it(t.begin(), t.end(), def), end; it != end; ++it) CHECK(seen.insert((*it)[1]).second);
  std::regex ver(R"(\n\| ([0-9]+)\.([0-9]+)\.([0-9]+) \| [0-9-]+ \|)");
  long last = -1;
  for (std::sregex_iterator it(t.begin(), t.end(), ver), end; it != end; ++it) {
    long v = std::stol((*it)[1]) * 1000000 + std::stol((*it)[2]) * 1000 + std::stol((*it)[3]);
    CHECK(v > last);
    last = v;
  }
  CHECK(t.find("TBD") == std::string::npos);
}
36.0ms