File size: 5,695 Bytes
ad474c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import unittest

from hdc.boot import AUTOEXEC, BootSequence, demo, render
from hdc.os import Desktop

D = 2000


class FakeAutonomy:
    """Enough of AutonomousMode for the AUTOEXEC line to be exercised."""

    def __init__(self):
        self.enabled = False
        self.ticks = 0
        self.intents = {"status": 1, "help": 2}

    def tick(self):
        self.ticks += 1
        return {"tick": self.ticks, "learned": 0, "acted": [], "readahead": []}


class TestBootSequence(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.desktop = Desktop(d=D)
        cls.sequence = BootSequence(cls.desktop, d=D)
        cls.result = cls.sequence.run()

    # ------------------------------------------------------------- structure
    def test_phases_run_in_order(self):
        self.assertEqual(self.result["phases"], ["post", "splash", "autoexec", "desktop"])

    def test_every_frame_fits_the_grid(self):
        for frame in self.result["frames"]:
            self.assertEqual(len(frame["lines"]), self.desktop.rows)
            for line in frame["lines"]:
                self.assertLessEqual(len(line), self.desktop.cols)
            self.assertGreater(frame["delay"], 0)
            self.assertTrue(frame["status"])

    def test_post_frame_is_the_real_bios_output(self):
        # the lines on screen are the ones hdc.x86 produced, not a decoration
        post = self.result["frames"][0]
        text = "\n".join(post["lines"])
        self.assertIn("HDCSTORAGE BIOS", text)
        self.assertIn("POST:", text)
        self.assertIn("Kernel handoff", text)

    def test_splash_frames_contain_the_title_and_animate(self):
        splash = [f for f in self.result["frames"] if f["phase"] == "splash"]
        self.assertGreater(len(splash), 2)
        for frame in splash:
            self.assertIn("H D C S T O R A G E", "\n".join(frame["lines"]))
        # the sweep is an exact brighten, so consecutive frames must differ
        self.assertNotEqual(splash[0]["lines"], splash[-1]["lines"])

    def test_splash_progress_increases(self):
        percents = []
        for frame in self.result["frames"]:
            if frame["phase"] != "splash":
                continue
            for token in frame["status"].split():
                if token.endswith("%"):
                    percents.append(int(token.rstrip("%")))
        self.assertEqual(percents, sorted(percents))
        self.assertEqual(percents[-1], 100)

    # -------------------------------------------------------------- autoexec
    def test_autoexec_mounts_the_filesystem(self):
        # MOUNT is a real chdir, not a printed string
        self.assertEqual(self.desktop.fs.cwd, "/home/user")

    def test_autoexec_launches_the_windows(self):
        titles = [window.title for window in self.desktop.windows]
        for expected in ("Terminal", "Monitor", "Files"):
            self.assertIn(expected, titles)
        # tiled: the terminal is at the top left, not cascaded onto it
        terminal = self.desktop.find("terminal")
        self.assertEqual((terminal.x, terminal.y), (1, 1))

    def test_autoexec_search_reports_a_real_hit(self):
        hits = [entry for entry in self.result["log"]
                if entry["phase"] == "autoexec" and "FS.SEARCH" in entry["command"]]
        self.assertTrue(hits)
        self.assertIn("notes-shader.txt", hits[0]["result"])
        self.assertIn("+0.", hits[0]["result"])

    def test_gfx_init_verifies_the_surface(self):
        gfx = [entry for entry in self.result["log"]
               if entry["phase"] == "autoexec" and "GFX.INIT" in entry["command"]]
        self.assertTrue(gfx)
        self.assertIn("verify ok", gfx[0]["result"])

    def test_unknown_boot_command_is_reported_not_raised(self):
        outcome = self.sequence._run_autoexec("FROBNICATE now")
        self.assertIn("unknown boot command", outcome)

    def test_autonomy_line_enables_the_mode(self):
        desktop = Desktop(d=D)
        desktop.autonomy = FakeAutonomy()
        sequence = BootSequence(desktop, d=D)
        outcome = sequence._run_autoexec("AUTONOMY enable readahead")
        self.assertTrue(desktop.autonomy.enabled)
        self.assertGreaterEqual(desktop.autonomy.ticks, 1)
        self.assertIn("intents", outcome)

    def test_autonomy_without_an_assistant_says_so(self):
        desktop = Desktop(d=D)
        self.assertIsNone(desktop.autonomy)
        outcome = BootSequence(desktop, d=D)._run_autoexec("AUTONOMY enable")
        self.assertIn("no assistant", outcome)

    def test_autoexec_script_is_declared(self):
        verbs = {line.split()[0] for line in AUTOEXEC}
        for expected in ("SET", "GFX.INIT", "MOUNT", "FS.SEARCH", "START", "AUTONOMY"):
            self.assertIn(expected, verbs)


class TestRendering(unittest.TestCase):
    def test_render_marks_each_frame(self):
        desktop = Desktop(d=D)
        sequence = BootSequence(desktop, d=D)
        sequence.run()
        text = render(sequence.frames)
        self.assertIn("--- frame 1/", text)
        self.assertIn("[splash]", text)
        self.assertNotIn("--- frame", render(sequence.frames, marks=False))

    def test_report_lists_phases_and_commands(self):
        desktop = Desktop(d=D)
        sequence = BootSequence(desktop, d=D)
        sequence.run()
        report = sequence.report()
        self.assertIn("post -> splash -> autoexec -> desktop", report)
        self.assertIn("POST", report)
        self.assertIn("GFX.INIT", report)

    def test_demo_prints_a_timeline(self):
        text = demo(D)
        self.assertIn("HDCSTORAGE BIOS", text)
        self.assertIn("boot:", text)


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