import argparse
from contextlib import ExitStack
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
import re
import threading
from urllib.parse import urlsplit, parse_qs
from playwright.sync_api import expect, sync_playwright
class Quiet(SimpleHTTPRequestHandler):
def log_message(self, *_):
pass
def host_warning(page, click, *, accept=False):
with page.expect_event('dialog') as warning:
page.once('dialog', lambda d: d.accept() if accept else d.dismiss())
click()
assert warning.value.type == 'confirm' and 'ホスト' in warning.value.message
def run(base):
with ExitStack() as stack:
if not base:
site = Path(__file__).resolve().parents[1] / 'target/invite-site'
if not (site / 'pkg/engawa_browser_bg.wasm').exists():
raise SystemExit('Build the site with python browser/build.py first')
server = stack.enter_context(ThreadingHTTPServer(('127.0.0.1', 0), partial(Quiet, directory=str(site))))
threading.Thread(target=server.serve_forever, daemon=True).start()
stack.callback(server.shutdown)
base = f'http://127.0.0.1:{server.server_port}/'
pw = stack.enter_context(sync_playwright())
browser = pw.webkit.launch()
stack.callback(browser.close)
def phone():
page = browser.new_page(viewport={'width': 390, 'height': 844}, is_mobile=True, has_touch=True)
page.set_default_timeout(60000)
return page
def row(page, name):
return page.locator('#verandas li').filter(has_text=name)
def select(page, name):
row(page, name).locator('[data-action=open]').click()
expect(page.locator('#directory')).to_be_visible(timeout=60000)
def enter(page, name):
page.locator('#rooms button').filter(has_text=name).click()
expect(page.locator('#room-name')).to_contain_text(name)
expect(page.locator('#chat')).to_be_visible()
def post(page, text):
page.locator('#draft').fill(text)
page.locator('#send').click()
def open_picker(page):
page.locator('#show-picker').click()
expect(page.locator('#picker-dialog')).to_be_visible()
def permanent_public(page):
built_in = page.locator('#verandas > li[data-id=public]')
expect(built_in).to_have_count(1)
expect(page.locator('#verandas > li').first).to_have_attribute('data-id', 'public')
expect(built_in).to_contain_text('みんなの縁側')
expect(built_in.locator('[data-action=open]')).to_be_enabled()
expect(built_in.locator('[data-action=forget], [data-action=rename]')).to_have_count(0)
home, friend = phone(), phone()
home.goto(base)
expect(home.locator('#picker')).to_be_visible()
permanent_public(home)
home.locator('#new-veranda-name').fill('海の縁側')
create = home.locator('#new-veranda button[type=submit]')
create.click()
expect(home.locator('#veranda-name')).to_contain_text('海の縁側', timeout=60000)
expect(home.locator('#directory')).to_be_visible(timeout=60000)
open_picker(home)
home.locator('#new-veranda-name').fill('庭の縁側')
host_warning(home, create.click, accept=True)
expect(home.locator('#veranda-name')).to_contain_text('庭の縁側', timeout=60000)
expect(home.locator('#directory')).to_be_visible(timeout=60000)
assert not urlsplit(home.url).fragment
home.locator('#share-veranda').click()
invite = home.locator('#share-link').input_value()
fields = parse_qs(urlsplit(invite).fragment)
assert set(fields) == {'v', 'kind', 'peer', 'relay', 'veranda', 'key'} and fields['v'] == ['2']
enter(home, 'もくもく')
draft, incoming = 'まだ送らない下書き', 'スマホから庭へ'
home.locator('#draft').fill(draft)
open_picker(home)
current = home.locator('#verandas li[aria-current=true]')
expect(current).to_contain_text('庭の縁側')
expect(current).to_contain_text('この端末がホスト')
friend.goto(invite)
expect(friend.locator('#connection')).to_be_visible()
expect(friend.locator('#invite-target')).to_contain_text('庭の縁側')
expect(friend.locator('#visibility')).to_contain_text('招待制')
expect(friend.locator('#directory')).to_be_hidden()
expect(friend.locator('#chat')).to_be_hidden()
friend.locator('#join').click()
expect(friend.locator('#directory')).to_be_visible(timeout=60000)
open_picker(friend)
unsaved = friend.locator('#verandas li[aria-current=true]')
expect(unsaved).to_contain_text('庭の縁側')
expect(unsaved).to_contain_text('未保存')
friend.go_back()
expect(friend.locator('#picker-dialog')).to_be_hidden()
expect(friend.locator('#directory')).to_be_visible()
assert friend.url == invite
friend.locator('#save-veranda').click()
expect(friend.locator('#save-veranda')).to_be_disabled()
enter(friend, 'もくもく')
expect(friend.locator('#status')).to_contain_text('部屋に接続しました', timeout=60000)
post(friend, incoming)
home.bring_to_front()
expect(home.locator('#messages')).to_contain_text(incoming, timeout=30000)
expect(home.locator('#picker-dialog')).to_be_visible()
def still_hosting():
expect(home.locator('#picker-dialog')).to_be_hidden()
expect(home.locator('#chat')).to_be_visible()
expect(home.locator('#room-name')).to_contain_text('もくもく')
expect(home.locator('#messages').get_by_text(incoming, exact=True)).to_be_visible()
expect(home.locator('#draft')).to_have_value(draft)
assert home.locator('#share-link').input_value() == invite
home.keyboard.press('Escape')
still_hosting()
open_picker(home)
current.locator('[data-action=open]').click()
still_hosting()
open_picker(home)
home.locator('#new-veranda-name').fill('庭' * 33) create.click()
expect(home.locator('#picker-error')).to_be_visible()
home.locator('#close-picker').click()
still_hosting()
open_picker(home)
host_warning(home, row(home, '海の縁側').locator('[data-action=open]').click)
if home.locator('#picker-dialog').is_visible():
home.locator('#close-picker').click()
still_hosting()
expect(home.locator('#leave')).to_be_hidden()
home.locator('#session-menu > summary').click()
host_warning(home, home.locator('#leave').click)
still_hosting()
open_picker(home)
home.once('dialog', lambda d: d.accept('うちの庭'))
current.locator('[data-action=rename]').click()
expect(current).to_contain_text('うちの庭')
home.locator('#close-picker').click()
expect(home.locator('#veranda-name')).to_contain_text('うちの庭')
still_hosting()
back = home.locator('#back-to-lobby')
expect(back).to_have_text('退室して部屋一覧へ')
expect(home.locator('#live-exit-note')).to_be_visible()
expect(home.locator('#live-exit-note')).to_contain_text('読み返せません')
back.click()
expect(home.locator('#directory')).to_be_visible()
enter(home, 'もくもく')
expect(home.locator('#empty')).to_be_visible()
expect(home.locator('#messages li')).to_have_count(0)
back.click()
enter(home, '雑談')
home.locator('#identity-settings > summary').click()
home.locator('#handle').fill('すずめ')
home.locator('#trip').check()
home.locator('#save-identity').click()
retained = 'この庭だけに残る話'
post(home, retained)
expect(home.locator('#messages')).to_contain_text(retained)
trip = re.compile(r'^すずめ ◆[0-9a-f]{16}$')
author = home.locator('#messages li').filter(has_text=retained).locator('small')
expect(author).to_have_text(trip)
garden_mark = author.inner_text()
expect(back).to_have_text('← 部屋一覧')
expect(home.locator('#live-exit-note')).to_be_hidden()
back.click()
expect(home.locator('#directory')).to_be_visible()
enter(home, '雑談')
expect(home.locator('#messages')).to_contain_text(retained)
friend.goto('about:blank')
open_picker(home)
host_warning(home, row(home, '海の縁側').locator('[data-action=open]').click, accept=True)
expect(home.locator('#directory')).to_be_visible(timeout=60000)
enter(home, '雑談')
expect(home.locator('#messages li')).to_have_count(0)
post(home, '別の縁側での署名')
sea_author = home.locator('#messages li').filter(has_text='別の縁側での署名').locator('small')
expect(sea_author).to_have_text(trip)
assert sea_author.inner_text() != garden_mark
home.goto('about:blank'); home.goto(base)
select(home, 'うちの庭')
home.locator('#share-veranda').click()
assert parse_qs(urlsplit(home.locator('#share-link').input_value()).fragment) == fields
enter(home, '雑談')
friend.goto(base)
select(friend, '庭の縁側')
enter(friend, '雑談')
post(home, '同じ招待先で再開')
expect(friend.locator('#messages')).to_contain_text('同じ招待先で再開', timeout=30000)
verified = friend.locator('#messages li').filter(has_text='同じ招待先で再開').locator('small')
expect(verified).to_have_text(garden_mark)
home.locator('#identity-settings > summary').click()
home.locator('#handle').fill('')
expect(home.locator('#trip')).to_be_disabled()
home.locator('#save-identity').click()
post(home, '名乗らずに投稿')
anon = friend.locator('#messages li').filter(has_text='名乗らずに投稿').locator('small')
expect(anon).to_have_text('名無しさん', timeout=30000)
expect(verified).to_have_text(garden_mark)
friend.bring_to_front()
open_picker(friend)
friend.once('dialog', lambda d: d.accept())
row(friend, '庭の縁側').locator('[data-action=forget]').click()
expect(unsaved).to_contain_text('未保存')
friend.locator('#close-picker').click()
expect(friend.locator('#picker-dialog')).to_be_hidden()
expect(friend.locator('#room-name')).to_have_text('#雑談')
post(friend, '忘れても退室しない')
expect(home.locator('#messages')).to_contain_text('忘れても退室しない', timeout=30000)
friend.locator('#session-menu > summary').click()
friend.locator('#leave').click()
expect(friend.locator('#no-verandas')).to_be_visible()
permanent_public(friend)
friend.goto('about:blank'); friend.goto(base)
expect(friend.locator('#no-verandas')).to_be_visible()
permanent_public(friend)
friend.goto(invite)
friend.locator('#join').click()
expect(friend.locator('#directory')).to_be_visible(timeout=60000)
home.locator('#session-menu > summary').click()
host_warning(home, home.locator('#leave').click, accept=True)
expect(home.locator('#picker')).to_be_visible()
expect(home.locator('#chat')).to_be_hidden()
print('PASS: two phones, live picker, room policies, host warnings, isolation, rename, stable rehost, forget and rejoin')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--site')
run(parser.parse_args().site)