浏览代码

Ease flow

master
Roderic Day 5 年前
父节点
当前提交
0a47f7ddb7
共有 4 个文件被更改,包括 22 次插入21 次删除
  1. +1
    -1
      makefile
  2. +4
    -5
      pico.js
  3. +6
    -4
      pico.py
  4. +11
    -11
      test.py

+ 1
- 1
makefile 查看文件

default: deploy
default: test deploy


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

+ 4
- 5
pico.js 查看文件

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

+ 6
- 4
pico.py 查看文件

document.read_bytes(), document.read_bytes(),
) )


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


async def process_request(self, path, request_headers): async def process_request(self, path, request_headers):
if path == '/': 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': if request_headers.get('Upgrade') != 'websocket':
return self.serve_file(path) return self.serve_file(path)
return await super().process_request(path, request_headers) return await super().process_request(path, request_headers)

+ 11
- 11
test.py 查看文件





def test_happy_path(): 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': 'login', 'value': 'TestUser'}
- {'kind': 'state', 'username': 'TestUser'} - {'kind': 'state', 'username': 'TestUser'}
- {'kind': 'state', 'online': ['TestUser']} - {'kind': 'state', 'online': ['TestUser']}




def test_name_taken(): 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': 'login', 'value': 'A'}
- {'kind': 'state', 'username': 'A'} - {'kind': 'state', 'username': 'A'}
- {'kind': 'state', 'online': ['A']} - {'kind': 'state', 'online': ['A']}
- {'kind': 'state', 'online': ['A', 'B']} - {'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': 'login', 'value': 'A'}
- {'kind': 'error', 'value': 'Username taken'} - {'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': 'login', 'value': 'B'}
- {'kind': 'state', 'username': 'B'} - {'kind': 'state', 'username': 'B'}
- {'kind': 'state', 'online': ['A', 'B']} - {'kind': 'state', 'online': ['A', 'B']}




def test_interaction(): 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': 'login', 'value': 'Alice'}
- {'kind': 'state', 'username': 'Alice'} - {'kind': 'state', 'username': 'Alice'}
- {'kind': 'state', 'online': ['Alice']} - {'kind': 'state', 'online': ['Alice']}
- {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'} - {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'}
- {'kind': 'post', 'value': 'Howdy!', 'source': 'Bob'} - {'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': 'login', 'value': 'Bob'}
- {'kind': 'state', 'username': 'Bob'} - {'kind': 'state', 'username': 'Bob'}
- {'kind': 'state', 'online': ['Alice', 'Bob']} - {'kind': 'state', 'online': ['Alice', 'Bob']}




def test_rooms(): 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': 'login', 'value': 'Dandy'}
- {'kind': 'state', 'username': 'Dandy'} - {'kind': 'state', 'username': 'Dandy'}
- {'kind': 'state', 'online': ['Dandy']} - {'kind': 'state', 'online': ['Dandy']}
+ {'kind': 'post', 'value': 'Hi', 'source': 'Dandy'} + {'kind': 'post', 'value': 'Hi', 'source': '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': 'login', 'value': 'Dandy'}
- {'kind': 'state', 'username': 'Dandy'} - {'kind': 'state', 'username': 'Dandy'}
- {'kind': 'state', 'online': ['Dandy']} - {'kind': 'state', 'online': ['Dandy']}




def test_private_message(): 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': 'login', 'value': 'Norman'}
- {'kind': 'state', 'username': 'Norman'} - {'kind': 'state', 'username': 'Norman'}
- {'kind': 'state', 'online': ['Norman']} - {'kind': 'state', 'online': ['Norman']}
- {'kind': 'post', 'value': '1', 'source': 'Norman'} - {'kind': 'post', 'value': '1', 'source': 'Norman'}
- {'kind': 'post', 'value': '3', 'source': 'Emma'} - {'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': 'login', 'value': 'Ray'}
- {'kind': 'state', 'username': 'Ray'} - {'kind': 'state', 'username': 'Ray'}
- {'kind': 'state', 'online': ['Norman', 'Ray']} - {'kind': 'state', 'online': ['Norman', 'Ray']}
- {'kind': 'post', 'value': '2', 'source': 'Ray'} - {'kind': 'post', 'value': '2', 'source': 'Ray'}
- {'kind': 'state', 'online': ['Ray', 'Emma']} - {'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': 'login', 'value': 'Emma'}
- {'kind': 'state', 'username': 'Emma'} - {'kind': 'state', 'username': 'Emma'}
- {'kind': 'state', 'online': ['Norman', 'Ray', 'Emma']} - {'kind': 'state', 'online': ['Norman', 'Ray', 'Emma']}

正在加载...
取消
保存