2026-03-18 20:42:54 -04:00
|
|
|
import itertools
|
2026-01-14 13:23:31 -05:00
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
|
2026-02-11 15:12:04 -05:00
|
|
|
from datetime import datetime
|
2026-02-17 21:19:24 -05:00
|
|
|
from django.conf import settings
|
2026-01-14 13:23:31 -05:00
|
|
|
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
|
Django Channels role-select sprint: turn_changed, roles_revealed, role_select_start consumer handlers; WS URL changed from room_slug to room_id UUID; TableSeat model - room, gamer, slot_number, role, role_revealed, seat_position fields; Room.table_status field with ROLE_SELECT, SIG_SELECT, IN_GAME choices; migration 0006_table_status_and_table_seat; pick_roles and select_role views; _role_select_context helper; _notify_turn_changed, _notify_roles_revealed, _notify_role_select_start notifiers; all gate-mutation views now call _notify_gate_update; ChannelsFunctionalTest base class with serve_static, screenshot, dump helpers; SQLite TEST NAME set to file path for ChannelsLiveServerTestCase; InMemoryChannelLayer added to test CHANNEL_LAYERS settings; FT 5 and FT 6 now passing - active seat arc and turn advance via WS, no page refresh; room.js, gatekeeper.js, role-select.js added to apps/epic/static; applets.js, game-kit.js, dashboard.js, wallet.js relocated to app-scoped static dirs; room.html: hex table, table-seat arcs, card-stack, inventory panel, role-card hand, WS scripts; _room.scss: room-shell flex layout, .table-hex polygon clip-path, .table-seat and .seat-card-arc, .card-stack eligible/ineligible states, .card flip animation, .inv-role-card stacked hand, .role-select-backdrop; gear btn and room menu always position: fixed; 375 tests, 0 skipped
2026-03-17 00:24:23 -04:00
|
|
|
from channels.testing import ChannelsLiveServerTestCase
|
2026-02-11 15:12:04 -05:00
|
|
|
from pathlib import Path
|
2026-01-14 13:23:31 -05:00
|
|
|
from selenium import webdriver
|
|
|
|
|
from selenium.common.exceptions import WebDriverException
|
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
|
|
2026-02-17 21:19:24 -05:00
|
|
|
from .container_commands import create_session_on_server, reset_database
|
|
|
|
|
from .management.commands.create_session import create_pre_authenticated_session
|
2026-03-09 16:08:28 -04:00
|
|
|
from apps.applets.models import Applet
|
2026-02-17 21:19:24 -05:00
|
|
|
|
2026-02-03 22:14:55 -05:00
|
|
|
|
2026-02-11 15:12:04 -05:00
|
|
|
|
2026-02-11 14:42:38 -05:00
|
|
|
MAX_WAIT = 10
|
2026-02-11 15:12:04 -05:00
|
|
|
SCREEN_DUMP_LOCATION = Path(__file__).absolute().parent / "screendumps"
|
2026-01-14 13:23:31 -05:00
|
|
|
|
2026-02-01 20:38:26 -05:00
|
|
|
|
|
|
|
|
# Decorator fns
|
|
|
|
|
def wait(fn):
|
|
|
|
|
def modified_fn(*args, **kwargs):
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
return fn(*args, **kwargs)
|
|
|
|
|
except (AssertionError, WebDriverException) as e:
|
|
|
|
|
if time.time() - start_time > MAX_WAIT:
|
|
|
|
|
raise e
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
return modified_fn
|
|
|
|
|
|
2026-02-11 15:12:04 -05:00
|
|
|
# Functional Tests
|
2026-01-14 13:23:31 -05:00
|
|
|
class FunctionalTest(StaticLiveServerTestCase):
|
|
|
|
|
# Helper methods
|
|
|
|
|
def setUp(self):
|
2026-02-10 22:42:45 -05:00
|
|
|
options = webdriver.FirefoxOptions()
|
2026-03-09 22:42:30 -04:00
|
|
|
headless = os.environ.get("HEADLESS")
|
|
|
|
|
if headless:
|
2026-02-10 22:42:45 -05:00
|
|
|
options.add_argument("--headless")
|
|
|
|
|
self.browser = webdriver.Firefox(options=options)
|
2026-03-09 22:42:30 -04:00
|
|
|
if headless:
|
|
|
|
|
self.browser.set_window_size(1366, 900)
|
2026-02-03 14:54:37 -05:00
|
|
|
self.test_server = os.environ.get("TEST_SERVER")
|
|
|
|
|
if self.test_server:
|
|
|
|
|
self.live_server_url = 'http://' + self.test_server
|
2026-02-03 22:14:55 -05:00
|
|
|
reset_database(self.test_server)
|
2026-03-11 13:59:43 -04:00
|
|
|
Applet.objects.get_or_create(slug="new-note", defaults={"name": "New Note"})
|
2026-01-14 13:23:31 -05:00
|
|
|
|
|
|
|
|
def tearDown(self):
|
2026-02-11 15:12:04 -05:00
|
|
|
if self._test_has_failed():
|
|
|
|
|
if not SCREEN_DUMP_LOCATION.exists():
|
|
|
|
|
SCREEN_DUMP_LOCATION.mkdir(parents=True)
|
|
|
|
|
self.take_screenshot()
|
|
|
|
|
self.dump_html()
|
2026-01-14 13:23:31 -05:00
|
|
|
self.browser.quit()
|
2026-02-11 15:12:04 -05:00
|
|
|
super().tearDown()
|
|
|
|
|
|
|
|
|
|
def _test_has_failed(self):
|
2026-03-18 20:49:44 -04:00
|
|
|
try:
|
|
|
|
|
return any(
|
|
|
|
|
failure[0] == self
|
|
|
|
|
for failure in itertools.chain(
|
|
|
|
|
self._outcome.result.failures, self._outcome.result.errors
|
|
|
|
|
)
|
2026-03-18 20:42:54 -04:00
|
|
|
)
|
2026-03-18 20:49:44 -04:00
|
|
|
except TypeError:
|
|
|
|
|
return False
|
2026-02-11 15:12:04 -05:00
|
|
|
|
|
|
|
|
def take_screenshot(self):
|
|
|
|
|
path = SCREEN_DUMP_LOCATION / self._get_filename("png")
|
|
|
|
|
print("screendumping to", path)
|
|
|
|
|
self.browser.get_screenshot_as_file(str(path))
|
|
|
|
|
|
|
|
|
|
def dump_html(self):
|
|
|
|
|
path = SCREEN_DUMP_LOCATION / self._get_filename("html")
|
|
|
|
|
print("dumping page html to", path)
|
2026-03-04 00:07:10 -05:00
|
|
|
path.write_text(self.browser.page_source, encoding="utf-8")
|
2026-02-11 15:12:04 -05:00
|
|
|
|
|
|
|
|
def _get_filename(self, extension):
|
|
|
|
|
timestamp = datetime.now().isoformat().replace(":", ".")
|
|
|
|
|
return (
|
|
|
|
|
f"{self.__class__.__name__}.{self._testMethodName}-{timestamp}.{extension}"
|
|
|
|
|
)
|
2026-01-14 13:23:31 -05:00
|
|
|
|
2026-02-17 21:19:24 -05:00
|
|
|
|
2026-02-01 20:38:26 -05:00
|
|
|
@wait
|
2026-01-19 16:35:00 -05:00
|
|
|
def wait_for(self, fn):
|
2026-02-01 20:38:26 -05:00
|
|
|
return fn()
|
2026-03-11 00:58:24 -04:00
|
|
|
|
|
|
|
|
def wait_for_slow(self, fn, timeout=30):
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
return fn()
|
|
|
|
|
except (AssertionError, WebDriverException) as e:
|
|
|
|
|
if time.time() - start_time > timeout:
|
|
|
|
|
raise e
|
|
|
|
|
time.sleep(0.5)
|
2026-02-01 20:38:26 -05:00
|
|
|
|
2026-02-17 21:19:24 -05:00
|
|
|
def create_pre_authenticated_session(self, email):
|
|
|
|
|
if self.test_server:
|
|
|
|
|
session_key = create_session_on_server(self.test_server, email)
|
|
|
|
|
else:
|
|
|
|
|
session_key = create_pre_authenticated_session(email)
|
|
|
|
|
## to set a cookie we need to first visit the domain
|
|
|
|
|
## 404 pages load the quickest!
|
|
|
|
|
self.browser.get(self.live_server_url + "/404_no_such_url/")
|
|
|
|
|
self.browser.add_cookie(
|
|
|
|
|
dict(
|
|
|
|
|
name=settings.SESSION_COOKIE_NAME,
|
|
|
|
|
value=session_key,
|
|
|
|
|
path="/",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-23 19:31:57 -04:00
|
|
|
def confirm_guard(self, browser=None):
|
|
|
|
|
b = browser or self.browser
|
|
|
|
|
def _click():
|
|
|
|
|
btn = b.find_element(By.CSS_SELECTOR, "#id_guard_portal.active .guard-yes")
|
|
|
|
|
b.execute_script("arguments[0].click()", btn)
|
|
|
|
|
self.wait_for(_click)
|
|
|
|
|
|
2026-02-01 20:38:26 -05:00
|
|
|
@wait
|
2026-02-01 20:06:01 -05:00
|
|
|
def wait_to_be_logged_in(self, email):
|
2026-02-01 20:38:26 -05:00
|
|
|
self.browser.find_element(By.CSS_SELECTOR, "#id_logout"),
|
2026-02-01 20:06:01 -05:00
|
|
|
navbar = self.browser.find_element(By.CSS_SELECTOR, ".navbar")
|
|
|
|
|
self.assertIn(email, navbar.text)
|
|
|
|
|
|
2026-02-01 20:38:26 -05:00
|
|
|
@wait
|
2026-02-01 20:06:01 -05:00
|
|
|
def wait_to_be_logged_out(self, email):
|
2026-02-01 20:38:26 -05:00
|
|
|
lambda: self.browser.find_element(By.CSS_SELECTOR, "input[name=email]"),
|
2026-02-01 20:06:01 -05:00
|
|
|
navbar = self.browser.find_element(By.CSS_SELECTOR, ".navbar")
|
|
|
|
|
self.assertNotIn(email, navbar.text)
|
Django Channels role-select sprint: turn_changed, roles_revealed, role_select_start consumer handlers; WS URL changed from room_slug to room_id UUID; TableSeat model - room, gamer, slot_number, role, role_revealed, seat_position fields; Room.table_status field with ROLE_SELECT, SIG_SELECT, IN_GAME choices; migration 0006_table_status_and_table_seat; pick_roles and select_role views; _role_select_context helper; _notify_turn_changed, _notify_roles_revealed, _notify_role_select_start notifiers; all gate-mutation views now call _notify_gate_update; ChannelsFunctionalTest base class with serve_static, screenshot, dump helpers; SQLite TEST NAME set to file path for ChannelsLiveServerTestCase; InMemoryChannelLayer added to test CHANNEL_LAYERS settings; FT 5 and FT 6 now passing - active seat arc and turn advance via WS, no page refresh; room.js, gatekeeper.js, role-select.js added to apps/epic/static; applets.js, game-kit.js, dashboard.js, wallet.js relocated to app-scoped static dirs; room.html: hex table, table-seat arcs, card-stack, inventory panel, role-card hand, WS scripts; _room.scss: room-shell flex layout, .table-hex polygon clip-path, .table-seat and .seat-card-arc, .card-stack eligible/ineligible states, .card flip animation, .inv-role-card stacked hand, .role-select-backdrop; gear btn and room menu always position: fixed; 375 tests, 0 skipped
2026-03-17 00:24:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChannelsFunctionalTest(ChannelsLiveServerTestCase):
|
|
|
|
|
"""Like FunctionalTest but backed by daphne so WebSocket connections work."""
|
|
|
|
|
serve_static = True
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
options = webdriver.FirefoxOptions()
|
|
|
|
|
headless = os.environ.get("HEADLESS")
|
|
|
|
|
if headless:
|
|
|
|
|
options.add_argument("--headless")
|
|
|
|
|
self.browser = webdriver.Firefox(options=options)
|
|
|
|
|
if headless:
|
|
|
|
|
self.browser.set_window_size(1366, 900)
|
|
|
|
|
self.test_server = os.environ.get("TEST_SERVER")
|
|
|
|
|
if self.test_server:
|
|
|
|
|
self.live_server_url = 'http://' + self.test_server
|
|
|
|
|
reset_database(self.test_server)
|
|
|
|
|
Applet.objects.get_or_create(slug="new-note", defaults={"name": "New Note"})
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
if self._test_has_failed():
|
|
|
|
|
if not SCREEN_DUMP_LOCATION.exists():
|
|
|
|
|
SCREEN_DUMP_LOCATION.mkdir(parents=True)
|
|
|
|
|
self.take_screenshot()
|
|
|
|
|
self.dump_html()
|
|
|
|
|
self.browser.quit()
|
|
|
|
|
super().tearDown()
|
|
|
|
|
|
|
|
|
|
def _test_has_failed(self):
|
2026-03-18 20:49:44 -04:00
|
|
|
try:
|
|
|
|
|
return any(
|
|
|
|
|
failure[0] == self
|
|
|
|
|
for failure in itertools.chain(
|
|
|
|
|
self._outcome.result.failures, self._outcome.result.errors
|
|
|
|
|
)
|
2026-03-18 20:42:54 -04:00
|
|
|
)
|
2026-03-18 20:49:44 -04:00
|
|
|
except TypeError:
|
|
|
|
|
return False
|
Django Channels role-select sprint: turn_changed, roles_revealed, role_select_start consumer handlers; WS URL changed from room_slug to room_id UUID; TableSeat model - room, gamer, slot_number, role, role_revealed, seat_position fields; Room.table_status field with ROLE_SELECT, SIG_SELECT, IN_GAME choices; migration 0006_table_status_and_table_seat; pick_roles and select_role views; _role_select_context helper; _notify_turn_changed, _notify_roles_revealed, _notify_role_select_start notifiers; all gate-mutation views now call _notify_gate_update; ChannelsFunctionalTest base class with serve_static, screenshot, dump helpers; SQLite TEST NAME set to file path for ChannelsLiveServerTestCase; InMemoryChannelLayer added to test CHANNEL_LAYERS settings; FT 5 and FT 6 now passing - active seat arc and turn advance via WS, no page refresh; room.js, gatekeeper.js, role-select.js added to apps/epic/static; applets.js, game-kit.js, dashboard.js, wallet.js relocated to app-scoped static dirs; room.html: hex table, table-seat arcs, card-stack, inventory panel, role-card hand, WS scripts; _room.scss: room-shell flex layout, .table-hex polygon clip-path, .table-seat and .seat-card-arc, .card-stack eligible/ineligible states, .card flip animation, .inv-role-card stacked hand, .role-select-backdrop; gear btn and room menu always position: fixed; 375 tests, 0 skipped
2026-03-17 00:24:23 -04:00
|
|
|
|
|
|
|
|
def take_screenshot(self):
|
|
|
|
|
path = SCREEN_DUMP_LOCATION / self._get_filename("png")
|
|
|
|
|
print("screendumping to", path)
|
|
|
|
|
self.browser.get_screenshot_as_file(str(path))
|
|
|
|
|
|
|
|
|
|
def dump_html(self):
|
|
|
|
|
path = SCREEN_DUMP_LOCATION / self._get_filename("html")
|
|
|
|
|
print("dumping page html to", path)
|
|
|
|
|
path.write_text(self.browser.page_source, encoding="utf-8")
|
|
|
|
|
|
|
|
|
|
def _get_filename(self, extension):
|
|
|
|
|
timestamp = datetime.now().isoformat().replace(":", ".")
|
|
|
|
|
return (
|
|
|
|
|
f"{self.__class__.__name__}.{self._testMethodName}-{timestamp}.{extension}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@wait
|
|
|
|
|
def wait_for(self, fn):
|
|
|
|
|
return fn()
|
|
|
|
|
|
|
|
|
|
def wait_for_slow(self, fn, timeout=30):
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
return fn()
|
|
|
|
|
except (AssertionError, WebDriverException) as e:
|
|
|
|
|
if time.time() - start_time > timeout:
|
|
|
|
|
raise e
|
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
2026-03-23 19:31:57 -04:00
|
|
|
def confirm_guard(self, browser=None):
|
|
|
|
|
b = browser or self.browser
|
|
|
|
|
def _click():
|
|
|
|
|
btn = b.find_element(By.CSS_SELECTOR, "#id_guard_portal.active .guard-yes")
|
|
|
|
|
b.execute_script("arguments[0].click()", btn)
|
|
|
|
|
self.wait_for(_click)
|
|
|
|
|
|
Django Channels role-select sprint: turn_changed, roles_revealed, role_select_start consumer handlers; WS URL changed from room_slug to room_id UUID; TableSeat model - room, gamer, slot_number, role, role_revealed, seat_position fields; Room.table_status field with ROLE_SELECT, SIG_SELECT, IN_GAME choices; migration 0006_table_status_and_table_seat; pick_roles and select_role views; _role_select_context helper; _notify_turn_changed, _notify_roles_revealed, _notify_role_select_start notifiers; all gate-mutation views now call _notify_gate_update; ChannelsFunctionalTest base class with serve_static, screenshot, dump helpers; SQLite TEST NAME set to file path for ChannelsLiveServerTestCase; InMemoryChannelLayer added to test CHANNEL_LAYERS settings; FT 5 and FT 6 now passing - active seat arc and turn advance via WS, no page refresh; room.js, gatekeeper.js, role-select.js added to apps/epic/static; applets.js, game-kit.js, dashboard.js, wallet.js relocated to app-scoped static dirs; room.html: hex table, table-seat arcs, card-stack, inventory panel, role-card hand, WS scripts; _room.scss: room-shell flex layout, .table-hex polygon clip-path, .table-seat and .seat-card-arc, .card-stack eligible/ineligible states, .card flip animation, .inv-role-card stacked hand, .role-select-backdrop; gear btn and room menu always position: fixed; 375 tests, 0 skipped
2026-03-17 00:24:23 -04:00
|
|
|
def create_pre_authenticated_session(self, email):
|
|
|
|
|
if self.test_server:
|
|
|
|
|
session_key = create_session_on_server(self.test_server, email)
|
|
|
|
|
else:
|
|
|
|
|
session_key = create_pre_authenticated_session(email)
|
|
|
|
|
self.browser.get(self.live_server_url + "/404_no_such_url/")
|
|
|
|
|
self.browser.add_cookie(
|
|
|
|
|
dict(
|
|
|
|
|
name=settings.SESSION_COOKIE_NAME,
|
|
|
|
|
value=session_key,
|
|
|
|
|
path="/",
|
|
|
|
|
)
|
|
|
|
|
)
|