# Path: skoolkit/snapshot.py
# def get_snapshot(fname, page=None):
#     """
#     Read a snapshot file and produce a 65536-element list of byte values.
# 
#     :param fname: The snapshot filename.
#     :param page: The page number to map to addresses 49152-65535 (C000-FFFF).
#                  This is relevant only when reading a 128K snapshot file.
#     :return: A 65536-element list of byte values.
#     """
#     if not can_read(fname):
#         raise SnapshotError("{}: Unknown file type".format(fname))
#     data = read_bin_file(fname)
#     ext = fname[-4:].lower()
#     if ext == '.sna':
#         ram = _read_sna(data, page)
#     elif ext == '.z80':
#         ram = _read_z80(data, page)
#     elif ext == '.szx':
#         ram = _read_szx(data, page)
#     if len(ram) != 49152:
#         raise SnapshotError("RAM size is {0}".format(len(ram)))
#     mem = [0] * 16384
#     mem.extend(ram)
#     return mem
# 
# def make_z80_ram_block(data, page):
#     block = []
#     prev_b = None
#     count = 0
#     for b in data:
#         if b == prev_b or prev_b is None:
#             prev_b = b
#             if count < 255:
#                 count += 1
#                 continue
#         if count > 4 or (count > 1 and prev_b == 237):
#             block.extend((237, 237, count, prev_b))
#         elif prev_b == 237:
#             block.extend((237, b))
#             prev_b = None
#             count = 0
#             continue
#         else:
#             block.extend((prev_b,) * count)
#         prev_b = b
#         count = 1
#     if count > 4 or (count > 1 and prev_b == 237):
#         block.extend((237, 237, count, prev_b))
#     else:
#         block.extend((prev_b,) * count)
#     length = len(block)
#     return [length % 256, length // 256, page] + block
# 
# class SnapshotError(SkoolKitError):
#     """Raised when an error occurs while reading a snapshot file."""


from skoolkittest import SkoolKitTestCase
from skoolkit.snapshot import get_snapshot, make_z80_ram_block, SnapshotError

    def test_szx_48k_bad_page_size(self):
        szx = self._get_szx_header()
        ram = [0, 0, 0] # Bad page size (3 != 16384)
        page = 5
        szx.extend(self._get_zxstrampage(page, False, ram))
        tmp_szx = self.write_bin_file(szx, suffix='.szx')
        try:
            get_snapshot(tmp_szx)
        except SnapshotError as e:
            self.assertEqual(e.args[0], "Page {0} is {1} bytes (should be 16384)".format(5, len(ram)))

    def test_szx_48k_missing_page(self):
        szx = self._get_szx_header()
        szx.extend(self._get_zxstrampage(5, True, [0] * 16384))
        tmp_szx = self.write_bin_file(szx, suffix='.szx')
        try:
            get_snapshot(tmp_szx)
        except SnapshotError as e:
            self.assertEqual(e.args[0], "Page 2 not found")

    def test_szx_128k(self):
        exp_ram = [(n + 73) & 255 for n in range(49152)]
        self._test_szx(exp_ram, True, 3)

    def test_szx_128k_no_specregs(self):
        szx = self._get_szx_header(2, specregs=False)
        tmp_szx = self.write_bin_file(szx, suffix='.szx')
        try:
            get_snapshot(tmp_szx)

# Path: skoolkit/snapshot.py
# def get_snapshot(fname, page=None):
#     """
#     Read a snapshot file and produce a 65536-element list of byte values.
# 
#     :param fname: The snapshot filename.
#     :param page: The page number to map to addresses 49152-65535 (C000-FFFF).
#                  This is relevant only when reading a 128K snapshot file.
#     :return: A 65536-element list of byte values.
#     """
#     if not can_read(fname):
#         raise SnapshotError("{}: Unknown file type".format(fname))
#     data = read_bin_file(fname)
#     ext = fname[-4:].lower()
#     if ext == '.sna':
#         ram = _read_sna(data, page)
#     elif ext == '.z80':
#         ram = _read_z80(data, page)
#     elif ext == '.szx':
#         ram = _read_szx(data, page)
#     if len(ram) != 49152:
#         raise SnapshotError("RAM size is {0}".format(len(ram)))
#     mem = [0] * 16384
#     mem.extend(ram)
#     return mem
# 
# def make_z80_ram_block(data, page):
#     block = []
#     prev_b = None
#     count = 0
#     for b in data:
#         if b == prev_b or prev_b is None:
#             prev_b = b
#             if count < 255:
#                 count += 1
#                 continue
#         if count > 4 or (count > 1 and prev_b == 237):
#             block.extend((237, 237, count, prev_b))
#         elif prev_b == 237:
#             block.extend((237, b))
#             prev_b = None
#             count = 0
#             continue
#         else:
#             block.extend((prev_b,) * count)
#         prev_b = b
#         count = 1
#     if count > 4 or (count > 1 and prev_b == 237):
#         block.extend((237, 237, count, prev_b))
#     else:
#         block.extend((prev_b,) * count)
#     length = len(block)
#     return [length % 256, length // 256, page] + block
# 
# class SnapshotError(SkoolKitError):
#     """Raised when an error occurs while reading a snapshot file."""


# Path: tests/test_snapshot.py
from skoolkittest import SkoolKitTestCase
from skoolkit.snapshot import get_snapshot, make_z80_ram_block, SnapshotError


    def test_szx_48k_bad_page_size(self):
        szx = self._get_szx_header()
        ram = [0, 0, 0] # Bad page size (3 != 16384)
        page = 5
        szx.extend(self._get_zxstrampage(page, False, ram))
        tmp_szx = self.write_bin_file(szx, suffix='.szx')
        try:
            get_snapshot(tmp_szx)
        except SnapshotError as e:
            self.assertEqual(e.args[0], "Page {0} is {1} bytes (should be 16384)".format(5, len(ram)))

    def test_szx_48k_missing_page(self):
        szx = self._get_szx_header()
        szx.extend(self._get_zxstrampage(5, True, [0] * 16384))
        tmp_szx = self.write_bin_file(szx, suffix='.szx')
        try:
            get_snapshot(tmp_szx)
        except SnapshotError as e:
            self.assertEqual(e.args[0], "Page 2 not found")

    def test_szx_128k(self):
        exp_ram = [(n + 73) & 255 for n in range(49152)]
        self._test_szx(exp_ram, True, 3)

    def test_szx_128k_no_specregs(self):
        szx = self._get_szx_header(2, specregs=False)
        tmp_szx = self.write_bin_file(szx, suffix='.szx')
        try:
            get_snapshot(tmp_szx)

        except SnapshotError as e: