Browse Source

Ease flow

master
Roderic Day 5 years ago
parent
commit
0a47f7ddb7
4 changed files with 22 additions and 21 deletions
  1. +1
    -1
      makefile
  2. +4
    -5
      pico.js
  3. +6
    -4
      pico.py
  4. +11
    -11
      test.py

+ 1
- 1
makefile View File

@@ -1,4 +1,4 @@
default: deploy
default: test deploy

test: venv/ libs/
venv/bin/python -u test.py

+ 4
- 5
pico.js View File

@@ -104,9 +104,8 @@ const connect = (username) => {
*/
const Base = {
oncreate: () => {
if(localStorage.username) {
connect(localStorage.username)
}
const randomName = ('' + Math.random()).substring(2)
connect(localStorage.username || randomName)
},
sendLogin: (e) => {
e.preventDefault()
@@ -145,10 +144,10 @@ const Base = {
State.isConnected ? null : m('form.login',
{onsubmit: Base.sendLogin},
m('input', attrs),
m('button', 'Login'),
m('button', 're-join'),
),
State.isConnected ? [
m('button', {onclick: Base.sendLogout}, 'logout'),
m('button', {onclick: Base.sendLogout}, 'pick name'),
m('button', {onclick: () => navigator.clipboard.writeText(location)}, 'copy url'),
] : null,
State.isConnected ? m(VideoConfig) : null,

+ 6
- 4
pico.py View File

@@ -38,17 +38,19 @@ class PicoProtocol(websockets.WebSocketServerProtocol):
document.read_bytes(),
)

def random_redirect(self):
new_path = str(uuid.uuid4()).split('-')[1]
def redirect_to(self, path):
return (
http.HTTPStatus.FOUND,
[('Location', new_path)],
[('Location', path)],
b'',
)

async def process_request(self, path, request_headers):
if path == '/':
return self.random_redirect()
random_path = str(uuid.uuid4()).split('-')[1]
return self.redirect_to(random_path)
elif path.lower() != path:
return self.redirect_to(path.lower()[1:])
if request_headers.get('Upgrade') != 'websocket':
return self.serve_file(path)
return await super().process_request(path, request_headers)

+ 11
- 11
test.py View File

@@ -69,7 +69,7 @@ async def _test(*clients):


def test_happy_path():
client = _make_client('ws://localhost:8642/A', 0.1, Script()
client = _make_client('ws://localhost:8642/x', 0.1, Script()
+ {'kind': 'login', 'value': 'TestUser'}
- {'kind': 'state', 'username': 'TestUser'}
- {'kind': 'state', 'online': ['TestUser']}
@@ -80,17 +80,17 @@ def test_happy_path():


def test_name_taken():
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
client1 = _make_client('ws://localhost:8642/x', 0.10, Script()
+ {'kind': 'login', 'value': 'A'}
- {'kind': 'state', 'username': 'A'}
- {'kind': 'state', 'online': ['A']}
- {'kind': 'state', 'online': ['A', 'B']}
)
client2 = _make_client('ws://localhost:8642/A', 0.11, Script()
client2 = _make_client('ws://localhost:8642/x', 0.11, Script()
+ {'kind': 'login', 'value': 'A'}
- {'kind': 'error', 'value': 'Username taken'}
)
client3 = _make_client('ws://localhost:8642/A', 0.12, Script()
client3 = _make_client('ws://localhost:8642/x', 0.12, Script()
+ {'kind': 'login', 'value': 'B'}
- {'kind': 'state', 'username': 'B'}
- {'kind': 'state', 'online': ['A', 'B']}
@@ -100,7 +100,7 @@ def test_name_taken():


def test_interaction():
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
client1 = _make_client('ws://localhost:8642/x', 0.10, Script()
+ {'kind': 'login', 'value': 'Alice'}
- {'kind': 'state', 'username': 'Alice'}
- {'kind': 'state', 'online': ['Alice']}
@@ -109,7 +109,7 @@ def test_interaction():
- {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'}
- {'kind': 'post', 'value': 'Howdy!', 'source': 'Bob'}
)
client2 = _make_client('ws://localhost:8642/A', 0.11, Script()
client2 = _make_client('ws://localhost:8642/x', 0.11, Script()
+ {'kind': 'login', 'value': 'Bob'}
- {'kind': 'state', 'username': 'Bob'}
- {'kind': 'state', 'online': ['Alice', 'Bob']}
@@ -122,14 +122,14 @@ def test_interaction():


def test_rooms():
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
client1 = _make_client('ws://localhost:8642/x', 0.10, Script()
+ {'kind': 'login', 'value': 'Dandy'}
- {'kind': 'state', 'username': 'Dandy'}
- {'kind': 'state', 'online': ['Dandy']}
+ {'kind': 'post', 'value': 'Hi', 'source': 'Dandy'}
- {'kind': 'post', 'value': 'Hi', 'source': 'Dandy'}
)
client2 = _make_client('ws://localhost:8642/B', 0.10, Script()
client2 = _make_client('ws://localhost:8642/y', 0.10, Script()
+ {'kind': 'login', 'value': 'Dandy'}
- {'kind': 'state', 'username': 'Dandy'}
- {'kind': 'state', 'online': ['Dandy']}
@@ -140,7 +140,7 @@ def test_rooms():


def test_private_message():
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
client1 = _make_client('ws://localhost:8642/x', 0.10, Script()
+ {'kind': 'login', 'value': 'Norman'}
- {'kind': 'state', 'username': 'Norman'}
- {'kind': 'state', 'online': ['Norman']}
@@ -150,7 +150,7 @@ def test_private_message():
- {'kind': 'post', 'value': '1', 'source': 'Norman'}
- {'kind': 'post', 'value': '3', 'source': 'Emma'}
)
client2 = _make_client('ws://localhost:8642/A', 0.11, Script()
client2 = _make_client('ws://localhost:8642/x', 0.11, Script()
+ {'kind': 'login', 'value': 'Ray'}
- {'kind': 'state', 'username': 'Ray'}
- {'kind': 'state', 'online': ['Norman', 'Ray']}
@@ -160,7 +160,7 @@ def test_private_message():
- {'kind': 'post', 'value': '2', 'source': 'Ray'}
- {'kind': 'state', 'online': ['Ray', 'Emma']}
)
client3 = _make_client('ws://localhost:8642/A', 0.12, Script()
client3 = _make_client('ws://localhost:8642/x', 0.12, Script()
+ {'kind': 'login', 'value': 'Emma'}
- {'kind': 'state', 'username': 'Emma'}
- {'kind': 'state', 'online': ['Norman', 'Ray', 'Emma']}

Loading…
Cancel
Save