Skip to content

Commit

Permalink
end2end tests: fix test_multiple_page_updates
Browse files Browse the repository at this point in the history
was broken because seems like github diff something with project links

replaced with using a custom made html page which should hopefully be more reliable anyway
  • Loading branch information
karlicoss committed Sep 26, 2024
1 parent 97a1a90 commit bee3cb5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
26 changes: 18 additions & 8 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from functools import wraps
from __future__ import annotations

import os
from pathlib import Path
import sys
import time
from typing import Iterator, Optional, TypeVar
from contextlib import contextmanager
from functools import wraps
from pathlib import Path
from typing import Iterator, TypeVar

import pytest
import requests

from promnesia.tests.common import free_port


def has_x() -> bool:
# meh, not very portable, but good enough for now
Expand All @@ -27,13 +32,14 @@ def uses_x(f):
@wraps(f)
def ff(*args, **kwargs):
return f(*args, **kwargs)

return ff


from contextlib import contextmanager
@contextmanager
def tmp_popen(*args, **kwargs):
import psutil

with psutil.Popen(*args, **kwargs) as p:
try:
yield p
Expand All @@ -45,13 +51,16 @@ def tmp_popen(*args, **kwargs):


@contextmanager
def local_http_server(path: Path, *, port: int) -> Iterator[str]:
def local_http_server(path: Path) -> Iterator[str]:
address = '127.0.0.1'
with tmp_popen([sys.executable, '-m', 'http.server', '--directory', path, '--bind', address, str(port)]) as popen:
with (
free_port() as port,
tmp_popen([sys.executable, '-m', 'http.server', '--directory', path, '--bind', address, str(port)]) as popen,
):
endpoint = f'http://{address}:{port}'

# meh.. but not sure if there is a better way to find out whether it's ready to serve requests
for attempt in range(50):
for _attempt in range(50):
try:
requests.get(endpoint)
except:
Expand All @@ -64,6 +73,7 @@ def local_http_server(path: Path, *, port: int) -> Iterator[str]:

T = TypeVar('T')

def notnone(x: Optional[T]) -> T:

def notnone(x: T | None) -> T:
assert x is not None
return x
29 changes: 19 additions & 10 deletions tests/end2end_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
from contextlib import ExitStack
from dataclasses import dataclass
from datetime import datetime
import os
from pathlib import Path
import socket
from time import sleep
from typing import Iterator, TypeVar, Callable
import os

import pytest

Expand All @@ -18,6 +17,7 @@
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions as EC

from promnesia.tests.common import get_testdata
from promnesia.tests.utils import index_urls
from promnesia.tests.server_helper import run_server as wserver
from promnesia.logging import LazyLogger
Expand Down Expand Up @@ -560,7 +560,7 @@ def test_sidebar_navigation(base_url: str, addon: Addon, driver: Driver, backend
# also need to extract a scenario for manual testing I guess

if base_url == 'LOCAL':
local_addr = exit_stack.enter_context(local_http_server(PYTHON_DOC_PATH, port=15454))
local_addr = exit_stack.enter_context(local_http_server(PYTHON_DOC_PATH))
base_url = local_addr

tutorial = f'{base_url}/tutorial/index.html'
Expand Down Expand Up @@ -793,39 +793,48 @@ def test_showvisits_popup(addon: Addon, driver: Driver, backend: Backend) -> Non


@browsers()
def test_multiple_page_updates(addon: Addon, driver: Driver, backend: Backend) -> None:
def test_multiple_page_updates(tmp_path: Path, addon: Addon, driver: Driver, backend: Backend, exit_stack: ExitStack) -> None:
# on some pages, onUpdated is triggered multiple times (because of iframes or perhaps something else??)
# which previously resulted in flickering sidebar/performance degradation etc, so it's a regression test against this
# TODO would be nice to hook to the backend and check how many requests it had...
url = 'https://github.com/karlicoss/promnesia/projects/1'

http_server_root = get_testdata('test_multiple_page_updates')

base_url = exit_stack.enter_context(local_http_server(http_server_root))

indexer = index_urls(
[
('https://github.com/karlicoss/promnesia', 'some comment'),
('https://github.com/karlicoss/promnesia/projects/1', 'just a note for the sidebar'),
(f'{base_url}', 'this url is for the toast'),
('https://github.com/karlicoss/promnesia', 'this url is to be marked as visited on the page'),
(f'{base_url}/projects/1', 'just a note for the sidebar'),
]
)
indexer(backend.backend_dir)

addon.configure(notify_contexts=True, show_dots=True)

driver.get(url)
driver.get(base_url)

had_toast = False
# TODO need a better way to check this...
seen_titles = set()
for _ in range(50):
toasts = driver.find_elements(By.CLASS_NAME, 'toastify')
if len(toasts) == 1:
had_toast = True
assert len(toasts) <= 1
sleep(0.1)
seen_titles.add(driver.title)
assert had_toast

assert len(seen_titles) > 10, seen_titles # precondition to make sure page update events would be triggered

addon.sidebar.open()
addon.sidebar.close()

xpath = '//a[@href = "/karlicoss/promnesia"]'
xpath = '//a[@href = "https://github.com/karlicoss/promnesia"]'
links_to_mark = driver.find_elements(By.XPATH, xpath)
assert len(links_to_mark) > 2 # sanity check
assert len(links_to_mark) >= 2 # sanity check
for l in links_to_mark:
assert 'promnesia-visited' in notnone(l.get_attribute('class'))
# TODO would be nice to test clicking on them...
Expand Down
32 changes: 32 additions & 0 deletions tests/testdata/test_multiple_page_updates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<head>

<script type="text/javascript" defer>
let index = 0
const update_title = () => {
document.title = `title ${index}`
index += 1
setTimeout(update_title, 50)
}
update_title()

</script>

</head>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

<a href="https://github.com/karlicoss/promnesia">link to promnesia</a>

<a href="https://github.com/karlicoss/promnesia/issues">link to promnesia issues</a>

<a href="https://news.ycombinator.com/newest">link to HN</a>

<a href="https://github.com/karlicoss/promnesia">another link to promnesia</a>


</body>
</html>

0 comments on commit bee3cb5

Please sign in to comment.