theme.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """The theme bundle (goal 41 task 2): the account-settings appearance
  2. picker backed by Spry's built-in themes. The SetThemeAction persists
  3. the choice per user in user_preferences and writes the DEVICE-scope
  4. "ui" slot ({theme, mode}) the client applies as data-theme/data-mode;
  5. login seeds that slot from the stored preferences."""
  6. from harness import * # noqa: F401,F403
  7. def run(ctx):
  8. admin = ctx.auth
  9. print("== profile page: the appearance card ==")
  10. profile = slot_public(admin, "/profile", "profile")
  11. check(profile.get("set_theme") is not None,
  12. "profile carries the set_theme action")
  13. check(profile.get("theme", "") == "" and profile.get("mode") == "auto",
  14. "no stored preference reads as core theme + auto mode")
  15. page = http_get_page(admin, "/profile")
  16. check("Appearance" in page, "profile page shows the Appearance card")
  17. for label in ("Spry (default)", "Airy", "Dwarf", "Formal", "Glassy",
  18. "H2O", "Living Sky", "Modern"):
  19. check(label in page, "theme choice offered: %s" % label)
  20. for label in ("Auto (follow system)", "Light", "Dark"):
  21. check(label in page, "mode choice offered: %s" % label)
  22. check("remembered for this account" in page,
  23. "the card hints the choice is remembered")
  24. print("== the seven theme stylesheets are served ==")
  25. for slug in ("airy", "dwarf", "formal", "glassy", "h2o",
  26. "living-sky", "modern"):
  27. status, _ = http("GET", "/_statum/resource/spry-theme-%s.css" % slug)
  28. check(status == 200, "spry-theme-%s.css served (HTTP %d)" % (slug, status))
  29. status, _ = http("GET", "/_statum/resource/spry.css")
  30. check(status == 200, "spry.css served (HTTP %d)" % status)
  31. print("== saving a theme persists + answers the ui slot ==")
  32. result = act(admin, profile["set_theme"], {"theme": "dwarf", "mode": "dark"})
  33. check("saved" in (action_notify(result) or ""),
  34. "set_theme accepts dwarf/dark")
  35. ui = [d for d in result
  36. if d["type"] == "set"
  37. and json.loads(d["snapshot_frame"]["content"])["slot"]["type"] == "ui"]
  38. check(len(ui) == 1, "the action answers a ui slot")
  39. ui_public = json.loads(ui[0]["snapshot_frame"]["content"])["public"]
  40. check(ui_public.get("theme") == "dwarf" and ui_public.get("mode") == "dark",
  41. "ui slot public data carries {theme, mode}")
  42. ui_scope = json.loads(ui[0]["snapshot_frame"]["content"])["slot"]
  43. check(ui_scope.get("scope") == "device",
  44. "the ui slot is DEVICE scope (cached across sessions)")
  45. conn = db()
  46. try:
  47. rows = conn.execute(
  48. "select key, value from user_preferences").fetchall()
  49. finally:
  50. conn.close()
  51. check(dict(rows).get("theme") == "dwarf" and dict(rows).get("mode") == "dark",
  52. "user_preferences stores theme=dwarf mode=dark: %s" % rows)
  53. print("== the saved choice reflects back on the profile ==")
  54. profile = slot_public(admin, "/profile", "profile")
  55. check(profile.get("theme") == "dwarf" and profile.get("mode") == "dark",
  56. "profile reads the stored choice back")
  57. print("== invalid values are refused, nothing stored ==")
  58. result = act(admin, profile["set_theme"], {"theme": "gruvbox", "mode": "auto"})
  59. check("Unknown theme" in (action_notify(result) or ""),
  60. "an unknown theme slug is refused with a friendly error")
  61. result = act(admin, profile["set_theme"], {"theme": "airy", "mode": "sepia"})
  62. check("Unknown colour mode" in (action_notify(result) or ""),
  63. "an unknown colour mode is refused")
  64. conn = db()
  65. try:
  66. rows = conn.execute(
  67. "select key, value from user_preferences").fetchall()
  68. finally:
  69. conn.close()
  70. check(dict(rows).get("theme") == "dwarf",
  71. "the refused slugs wrote nothing: %s" % rows)
  72. print("== core default + upsert overwrite ==")
  73. result = act(admin, profile["set_theme"], {"theme": "", "mode": "light"})
  74. check("saved" in (action_notify(result) or ""), "core default + light accepted")
  75. conn = db()
  76. try:
  77. rows = dict(conn.execute(
  78. "select key, value from user_preferences").fetchall())
  79. finally:
  80. conn.close()
  81. check(rows.get("theme") == "" and rows.get("mode") == "light",
  82. "the upsert replaced both rows in place: %s" % rows)
  83. profile = slot_public(admin, "/profile", "profile")
  84. check(profile.get("theme") == "" and profile.get("mode") == "light",
  85. "profile reads the core default back")
  86. print("== login seeds the ui slot from stored prefs ==")
  87. fresh = login("admin", "adminpass123")
  88. seeded = [d for d in directives("/", fresh)
  89. if d["type"] == "set"
  90. and json.loads(d["snapshot_frame"]["content"])["slot"]["type"] == "ui"]
  91. # The DEVICE slot is client-held; a fresh login (a browser with no
  92. # cached frame) gets it seeded on the same directive chain as the
  93. # auth slot.
  94. if seeded:
  95. seeded_public = json.loads(seeded[0]["snapshot_frame"]["content"])["public"]
  96. check(seeded_public.get("theme") == "" and seeded_public.get("mode") == "light",
  97. "login seeds the ui slot from the stored preferences")
  98. else:
  99. # The entrypoint chain does not re-emit a held DEVICE slot when
  100. # the request already carries one; assert the seed indirectly
  101. # through the profile's mirror of the stored values instead.
  102. profile = slot_public(fresh, "/profile", "profile")
  103. check(profile.get("theme") == "" and profile.get("mode") == "light",
  104. "stored preferences survive a re-login (profile mirror)")