import os import time import unittest from tests.common import SCRATCH from das_shell import banner, widgets # noqa: E402 class GridTest(unittest.TestCase): def test_round_trip(self): g = banner.Grid(10, 3) g.box(0, 0, 9, 2, "double", g.style_key("cyan", None, True)) g.text(2, 1, "Дас", g.style_key("yellow")) d = g.to_design({"name": "x"}) g2 = banner.Grid.from_design(d) self.assertEqual(banner.grid_lines(g2, color=False), ["╔════════╗", "║ Дас ║", "╚════════╝"]) self.assertEqual(len(d["styles"]), 2) def test_stamp_merges_styles(self): a = banner.Grid(4, 1) a.text(0, 0, "ab", a.style_key("red")) b = banner.Grid(6, 1) b.text(0, 0, "x", b.style_key("blue")) b.stamp(2, 0, a) self.assertEqual(banner.grid_lines(b, color=False), ["x ab"]) self.assertEqual(b.style(b.get(2, 0)[1])[0], "red") def test_region_and_resize(self): g = banner.Grid(5, 5) g.fill(1, 1, 3, 3, "#", " ") r = g.region(1, 1, 2, 2) self.assertEqual((r.width, r.height), (2, 2)) g.resize(2, 2) self.assertEqual(banner.grid_lines(g, color=False), ["", " #"]) class RenderTest(unittest.TestCase): def test_fields_keep_the_frame(self): d = banner.load("das-classic") values = widgets.Values({"title": "T", "url": "https://example.org/"}) values["ip"] = "10.0.0.123" lines = banner.render(dict(d, widgets=[]), values, [], color=False).splitlines() self.assertEqual(len({len(line) for line in lines}), 1, lines) self.assertIn("10.0.0.123", lines[2]) def test_long_value_is_cut(self): self.assertEqual(banner.fit("abcdef", 4, "left"), "abc…") self.assertEqual(banner.fit("ab", 4, "center"), " ab ") def test_languages_and_empty_values(self): values = widgets.Values({"url": ""}) self.assertEqual(banner.expand("[[Сайт|Site]]: {url}", values), "Site: ") f = {"x": 0, "y": 0, "width": 20, "text": "{x} · {url}"} self.assertEqual(banner.field_lines(f, widgets.Values({"x": "host", "url": ""}))[0].rstrip(), "host") self.assertEqual(banner.expand("{unknown}", values), "{unknown}") def test_widgets_right(self): d = {"lines": ["AB", "CD"], "widgets_position": "right"} out = banner.render(d, widgets.Values(), [["w1", "w2", "w3"]], color=False) self.assertEqual(out.splitlines(), ["AB w1", "CD w2", " w3"]) def test_every_preset_renders(self): for name, _ in banner.list_presets(): d = banner.load(name) out = banner.render(d, widgets.Values(d["vars"]), [], color=True) self.assertTrue(out.strip(), name) def test_art_pictures(self): names = {n for n, _, _ in banner.list_art()} self.assertTrue({"das-logo", "das-logo-small", "trident", "trident-small", "trident-shield"} <= names) class WidgetTest(unittest.TestCase): def test_slow_widget_does_not_hold_the_login(self): d = {"widgets": [{"type": "command", "cmd": "sleep 10"}, {"type": "command", "cmd": "echo hi"}]} start = time.monotonic() blocks = widgets.blocks(d, widgets.Values()) self.assertLess(time.monotonic() - start, 4) self.assertEqual(blocks, [[], ["hi"]]) def test_cache(self): w = {"type": "command", "cmd": "echo cached", "mode": "cached", "interval": 1000} d = {"widgets": [w]} self.assertEqual(widgets.blocks(d, widgets.Values()), [[]]) widgets.refresh(d) self.assertEqual(widgets.blocks(d, widgets.Values()), [["cached"]]) def test_file_widget(self): path = os.path.join(SCRATCH, "out.txt") with open(path, "w") as f: f.write("a\nb\n") self.assertEqual(widgets.run({"type": "file", "path": path}, widgets.Values()), ["a", "b"]) def test_builtins(self): values = widgets.Values() for name in widgets.BUILTINS: widgets.run({"type": "builtin", "name": name}, values) self.assertTrue(values.get("hostname")) self.assertNotEqual(values.get("uptime"), "?") def test_vars_reach_scripts(self): d = {"vars": {"city": "Kyiv"}, "widgets": [{"type": "command", "cmd": "echo $DBANNER_CITY"}]} self.assertEqual(widgets.blocks(d, widgets.Values())[0], ["Kyiv"]) if __name__ == "__main__": unittest.main()