aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_frontend.py
blob: cdd31fc1b5363f27ece9bea9f1629374a3b22f88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import http.client
import os
import socket
import tempfile
import threading
import unittest

import deckit.module as mod
import deckit.server as srv


class UnixConnection(http.client.HTTPConnection):
    def __init__(self, path):
        super().__init__("localhost")
        self.path = path

    def connect(self):
        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.sock.connect(self.path)


class Frontend(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.dir = tempfile.TemporaryDirectory()
        cls.sock = os.path.join(cls.dir.name, "http.sock")
        srv.TOKEN = "t"
        srv.REGISTRY = mod.Registry([])
        cls.server = srv.UnixHTTPServer(cls.sock, srv.Handler)
        threading.Thread(target=cls.server.serve_forever, daemon=True).start()

    @classmethod
    def tearDownClass(cls):
        cls.server.shutdown()
        cls.server.server_close()
        cls.dir.cleanup()

    def get(self, path):
        conn = UnixConnection(self.sock)
        conn.request("GET", path, headers={"Host": "127.0.0.1:8765"})
        r = conn.getresponse()
        data = r.read()
        conn.close()
        return r.status, data

    def test_page_references_assets(self):
        status, body = self.get("/")
        self.assertEqual(status, 200)
        text = body.decode()
        self.assertIn("app.js", text)
        self.assertIn("deck.css", text)

    def test_css_uses_udt_vars(self):
        status, body = self.get("/deck.css")
        self.assertEqual(status, 200)
        self.assertIn(b"--udt-", body)

    def assert_panel(self, name):
        status, body = self.get(f"/modules/{name}.js")
        self.assertEqual(status, 200, name)
        text = body.decode()
        for part in ("export default", "mount", "update"):
            self.assertIn(part, text, name)

    def test_panel_for_every_module(self):
        from deckit.modules import build
        names = [m["name"] for m in build({}).names()]
        self.assertIn("stats", names)
        for name in names:
            self.assert_panel(name)


if __name__ == "__main__":
    unittest.main()