test_relation_manager.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Task 1 — RelationManager: the declaration registry for referential
  2. // integrity relations. No enforcement, no LMDB index yet (later tasks).
  3. //
  4. // Modeled on tests/test_view_manager_paging.cpp: relations are keyed by
  5. // their project-qualified name in a `_relations` system collection, and
  6. // loadFromStore() must page explicitly since Query::limit defaults to 100
  7. // and limit=0 returns nothing (not everything) — the same trap that has
  8. // already shipped as a bug in ViewManager, PolicyManager and
  9. // CollectionConfigManager.
  10. #include <iostream>
  11. #include <string>
  12. #include <nlohmann/json.hpp>
  13. #include "document.hpp"
  14. #include "memory_store.hpp"
  15. #include "relations/relation_manager.hpp"
  16. using namespace smartbotic::database;
  17. namespace {
  18. int g_pass = 0;
  19. int g_fail = 0;
  20. void check(bool cond, const std::string& msg) {
  21. if (cond) { ++g_pass; }
  22. else { ++g_fail; std::cerr << "FAIL: " << msg << "\n"; }
  23. }
  24. struct Fixture {
  25. MemoryStore store;
  26. Fixture() : store(MemoryStore::Config{}) {
  27. store.start();
  28. }
  29. ~Fixture() { store.stop(); }
  30. };
  31. void test_relations_are_project_scoped_and_survive_reload() {
  32. Fixture f; // MemoryStore, started
  33. RelationManager rm(f.store);
  34. rm.loadFromStore();
  35. RelationInfo a;
  36. a.name = "default:exec_wf";
  37. a.child = "default:executions";
  38. a.childField = "workflowId";
  39. a.parent = "default:workflows";
  40. std::string err;
  41. check(rm.createRelation(a, err), "created in default");
  42. RelationInfo b = a; // SAME bare name, different project
  43. b.name = "acme:exec_wf";
  44. b.child = "acme:executions";
  45. b.parent = "acme:workflows";
  46. check(rm.createRelation(b, err), "the same name in another project is allowed");
  47. check(rm.listRelations("default").size() == 1, "listing is project-filtered");
  48. check(rm.listRelations().size() == 2, "empty project lists everything");
  49. RelationManager fresh(f.store); // restart
  50. fresh.loadFromStore();
  51. check(fresh.getRelation("default:exec_wf").has_value(), "survives reload");
  52. check(fresh.getRelation("acme:exec_wf").has_value(), "both survive");
  53. }
  54. void test_cross_project_relation_is_refused() {
  55. Fixture f;
  56. RelationManager rm(f.store);
  57. RelationInfo r;
  58. r.name = "default:bad";
  59. r.child = "default:executions";
  60. r.childField = "workflowId";
  61. r.parent = "acme:workflows"; // different env - no txn spans two
  62. std::string err;
  63. check(!rm.createRelation(r, err), "a cross-project relation is refused");
  64. check(err.find("project") != std::string::npos, "and says why");
  65. }
  66. void test_more_than_one_page_of_relations_loads() {
  67. Fixture f;
  68. RelationManager rm(f.store);
  69. for (int i = 0; i < 250; ++i) { // Query::limit defaults to 100
  70. RelationInfo r;
  71. char buf[32];
  72. std::snprintf(buf, sizeof(buf), "default:r%03d", i);
  73. r.name = buf;
  74. r.child = "default:c";
  75. r.childField = "p";
  76. r.parent = "default:p";
  77. std::string err;
  78. rm.createRelation(r, err);
  79. }
  80. RelationManager fresh(f.store);
  81. fresh.loadFromStore();
  82. check(fresh.listRelations().size() == 250,
  83. "all 250 load - a bare Query would stop at 100, as it did for views, "
  84. "policies and collection configs");
  85. }
  86. // v2.11.0 T13 round 2 (review finding 3) — createRelation() refuses a
  87. // cross-project declaration (test_cross_project_relation_is_refused,
  88. // above), but loadFromStore() is the OTHER way a RelationInfo enters the
  89. // cache and did not re-check it. A legacy or hand-written `_relations`
  90. // document naming a cross-project parent must be skipped at load time too -
  91. // arming it would resolve the bare parent name inside the CHILD's own
  92. // project env (validate_on_write/RelationRef only ever resolve `parent`
  93. // against the child's project), silently checking the wrong collection.
  94. void test_load_skips_a_cross_project_relation_written_by_hand() {
  95. Fixture f;
  96. // Bypass createRelation()'s own guard entirely - write the raw document
  97. // straight into `_relations`, the way a legacy record or a hand-edited
  98. // one would exist on disk. `parent` names a DIFFERENT project than
  99. // `child`, which createRelation() would refuse today.
  100. nlohmann::json bad = {
  101. {"name", "default:bad_cross"},
  102. {"child", "default:executions"},
  103. {"child_field", "workflowId"},
  104. {"parent", "acme:workflows"},
  105. {"on_delete", "restrict"},
  106. {"validate_on_write", true},
  107. {"created_at", 0},
  108. {"updated_at", 0},
  109. };
  110. Document d;
  111. d.id = "default:bad_cross";
  112. d.collection = RelationManager::SYSTEM_COLLECTION;
  113. d.set_data(bad);
  114. f.store.insert(RelationManager::SYSTEM_COLLECTION, d);
  115. // A well-formed, same-project relation alongside it, to confirm one bad
  116. // record does not stop the rest of the load.
  117. RelationInfo good;
  118. good.name = "default:exec_wf";
  119. good.child = "default:executions";
  120. good.childField = "ownerId";
  121. good.parent = "default:users";
  122. {
  123. RelationManager rm(f.store);
  124. rm.loadFromStore();
  125. std::string err;
  126. check(rm.createRelation(good, err), "the well-formed sibling declares fine");
  127. }
  128. RelationManager fresh(f.store);
  129. fresh.loadFromStore();
  130. check(!fresh.getRelation("default:bad_cross").has_value(),
  131. "the cross-project relation was skipped, not armed with the wrong parent");
  132. check(fresh.getRelation("default:exec_wf").has_value(),
  133. "the well-formed sibling still loaded - one bad record did not stop the rest");
  134. }
  135. } // namespace
  136. int main() {
  137. std::cout << "=== test_relation_manager ===\n";
  138. test_relations_are_project_scoped_and_survive_reload();
  139. test_cross_project_relation_is_refused();
  140. test_more_than_one_page_of_relations_loads();
  141. test_load_skips_a_cross_project_relation_written_by_hand();
  142. std::cout << "passed: " << g_pass << ", failed: " << g_fail << "\n";
  143. return g_fail == 0 ? 0 : 1;
  144. }