understanding.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. """The understanding bundle (goal 24 task 2): the project Understanding
  2. ledger — the /api/saddle/understanding insert/read callback pair, the
  3. immutable history rows, and the current document riding the assignment
  4. payload."""
  5. from harness import * # noqa: F401,F403 — the shared wire helpers
  6. def run(ctx):
  7. auth, fake, fake_id, fake_token = ctx.auth, ctx.fake, ctx.fake_id, ctx.fake_token
  8. def db_rows(sql, params=()):
  9. conn = db()
  10. try:
  11. return conn.execute(sql, params).fetchall()
  12. finally:
  13. conn.close()
  14. print("== understanding: the migration ==")
  15. # A fresh database: M0001..M0004 ran at startup, so the table
  16. # exists with the goal-24 shape.
  17. tables = [r[0] for r in db_rows(
  18. "select name from sqlite_master where type = 'table'")]
  19. check("project_understandings" in tables,
  20. "M0004 creates the project_understandings table on a fresh DB")
  21. columns = [r[1] for r in db_rows("pragma table_info(project_understandings)")]
  22. check(columns == ["id", "project_id", "body", "commit_hash", "task_id",
  23. "created_at"],
  24. "the table carries the goal-24 columns (%s)" % columns)
  25. print("== understanding: POST auth + validation ==")
  26. status, _ = saddle_api("POST", "/api/saddle/understanding", None,
  27. {"project": "demo", "body": "x"})
  28. check(status == 401, "POST rejects a missing token")
  29. status, _ = saddle_api("POST", "/api/saddle/understanding", "saddle-bogus",
  30. {"project": "demo", "body": "x"})
  31. check(status == 401, "POST rejects a wrong token")
  32. status, body = saddle_api("POST", "/api/saddle/understanding", fake_token,
  33. {"project": "no-such-project", "body": "x"})
  34. check(status == 400 and body.get("ok") is False,
  35. "POST rejects an unknown project")
  36. status, body = saddle_api("POST", "/api/saddle/understanding", fake_token,
  37. {"project": "demo", "body": " "})
  38. check(status == 400 and body.get("ok") is False,
  39. "POST rejects an empty body")
  40. print("== understanding: inserts append history ==")
  41. status, body = saddle_api("POST", "/api/saddle/understanding", fake_token,
  42. {"project": "demo",
  43. "body": "# Demo understanding\n\nFirst revision.",
  44. "commit_hash": "abc1000", "task_id": 11})
  45. check(status == 200 and body.get("ok") is True
  46. and int(body.get("understanding_id", 0)) > 0,
  47. "POST inserts and returns the understanding id")
  48. first_id = int(body["understanding_id"])
  49. status, body = saddle_api("POST", "/api/saddle/understanding", fake_token,
  50. {"project": "demo",
  51. "body": "# Demo understanding\n\nSecond revision.",
  52. "commit_hash": "def2000"})
  53. check(status == 200 and int(body["understanding_id"]) > first_id,
  54. "a refresh appends a LATER history row")
  55. second_id = int(body["understanding_id"])
  56. rows = db_rows("select id, body, commit_hash, task_id"
  57. " from project_understandings order by id")
  58. check([r[0] for r in rows] == [first_id, second_id],
  59. "history rows are immutable appends in insert order")
  60. check(rows[0][2] == "abc1000" and rows[0][3] == 11
  61. and rows[1][2] == "def2000" and rows[1][3] == 0,
  62. "rows keep the commit hash and the optional task id")
  63. print("== understanding: GET reads the current revision ==")
  64. status, _ = saddle_api("GET", "/api/saddle/understanding?project=demo", None)
  65. check(status == 401, "GET rejects a missing token")
  66. status, body = saddle_api(
  67. "GET", "/api/saddle/understanding?project=no-such-project", fake_token)
  68. check(status == 400, "GET rejects an unknown project")
  69. status, body = saddle_api("GET", "/api/saddle/understanding", fake_token)
  70. check(status == 400, "GET requires the project query parameter")
  71. status, body = saddle_api("GET", "/api/saddle/understanding?project=demo",
  72. fake_token)
  73. check(status == 200 and body.get("ok") is True,
  74. "GET answers with the current understanding")
  75. current = body.get("understanding") or {}
  76. check(current.get("body") == "# Demo understanding\n\nSecond revision.",
  77. "GET returns the NEWEST revision (%r)" % current.get("body", "")[:40])
  78. check(current.get("commit_hash") == "def2000"
  79. and (current.get("created") or "").endswith("Z"),
  80. "GET carries the commit hash and an ISO-8601 stamp")
  81. # A project without any understanding answers null, not an error.
  82. state = slot_public(auth, "/projects", "projects")
  83. act(auth, state["create"], {"name": "plain",
  84. "git_url": "https://example.com/plain.git"})
  85. status, body = saddle_api("GET", "/api/saddle/understanding?project=plain",
  86. fake_token)
  87. check(status == 200 and body.get("ok") is True
  88. and body.get("understanding") is None,
  89. "GET answers understanding null for a project with none")
  90. print("== understanding: rides the assignment payload ==")
  91. payload_count = len(fake.start_payloads)
  92. create_queued(auth, {
  93. "project_id": "1",
  94. "title": "Understanding dispatch",
  95. "description": "The current understanding rides the /start payload.",
  96. "priority": "2",
  97. })
  98. check(wait_for(lambda: len(fake.start_payloads) > payload_count or False,
  99. timeout=30, what="understanding /start push") is not None,
  100. "dispatcher pushes the understanding task")
  101. payload = fake.start_payloads[-1]
  102. check("understanding" in payload,
  103. "the assignment payload carries an understanding member")
  104. check(payload.get("understanding") == "# Demo understanding\n\nSecond revision.",
  105. "the payload's understanding is the project's CURRENT revision")
  106. # A giant revision is head-clamped at 16000 chars (the payload cap).
  107. conn = db()
  108. conn.execute("insert into project_understandings"
  109. " (project_id, body, commit_hash, task_id, created_at)"
  110. " values (1, ?, 'big0000', 0, strftime('%s','now'))",
  111. ("giant!" + "y" * 25000,))
  112. conn.commit()
  113. conn.close()
  114. payload_count = len(fake.start_payloads)
  115. create_queued(auth, {
  116. "project_id": "1",
  117. "title": "Understanding cap dispatch",
  118. "priority": "2",
  119. })
  120. check(wait_for(lambda: len(fake.start_payloads) > payload_count or False,
  121. timeout=30, what="cap /start push") is not None,
  122. "dispatcher pushes the cap task")
  123. shipped = fake.start_payloads[-1].get("understanding", "")
  124. check(len(shipped) == 16000 and shipped.startswith("giant!"),
  125. "the payload caps the understanding at 16000 chars, head kept (%d)"
  126. % len(shipped))
  127. # No understanding yet: the member is present but empty.
  128. plain_id = int([p["id"] for p in
  129. slot_public(auth, "/projects", "projects")["projects"]
  130. if p["name"] == "plain"][0])
  131. payload_count = len(fake.start_payloads)
  132. create_queued(auth, {
  133. "project_id": str(plain_id),
  134. "title": "No-understanding dispatch",
  135. "priority": "2",
  136. })
  137. check(wait_for(lambda: len(fake.start_payloads) > payload_count or False,
  138. timeout=30, what="empty /start push") is not None,
  139. "dispatcher pushes the no-understanding task")
  140. check(fake.start_payloads[-1].get("understanding") == "",
  141. "a project without an understanding ships an empty string")