# Path: asciinema/asciicast/v2.py
# class LoadError(Exception):
# class Asciicast:
# class open_from_file:
# class writer(file_writer):
#     def __init__(
#         self, f: Union[TextIO, StreamReader], header: Dict[str, Any]
#     ) -> None:
#     def events(self) -> Generator[Any, None, None]:
#     def stdout_events(self) -> Generator[List[Any], None, None]:
# def build_from_header_and_file(
#     header: Dict[str, Any], f: Union[StreamReader, TextIO]
# ) -> Asciicast:
#     def __init__(
#         self, first_line: str, file: Union[StreamReader, TextIO]
#     ) -> None:
#     def __enter__(self) -> Asciicast:
#     def __exit__(
#         self, exc_type: str, exc_value: str, exc_traceback: str
#     ) -> None:
# def get_duration(path_: str) -> Any:
# def build_header(
#     width: Optional[int], height: Optional[int], metadata: Any
# ) -> Dict[str, Any]:
#     def __init__(  # pylint: disable=too-many-arguments
#         self,
#         path_: str,
#         metadata: Any = None,
#         append: bool = False,
#         buffering: int = 1,
#         width: Optional[int] = None,
#         height: Optional[int] = None,
#         on_error: Optional[Callable[[str], None]] = None,
#     ) -> None:
#     def __enter__(self) -> Any:
#     def write_stdout(self, ts: float, data: Union[str, bytes]) -> None:
#     def write_stdin(self, ts: float, data: Union[str, bytes]) -> None:
#     def _open_file(self) -> None:
#     def __write_event(self, ts: float, etype: str, data: str) -> None:
#     def __write_line(self, obj: Any) -> None:
#     FORMAT_ERROR = "only asciicast v2 format can be opened"
# 
# Path: tests/test_helper.py
# class Test:
#     def setUp(self) -> None:
#         global stdout  # pylint: disable=global-statement
#         self.real_stdout = sys.stdout
#         sys.stdout = stdout = StringIO()
# 
#     def tearDown(self) -> None:
#         sys.stdout = self.real_stdout


import json
import tempfile
from asciinema.asciicast import v2
from ..test_helper import Test




class TestWriter(Test):
    @staticmethod
    def test_writing() -> None:
        _file, path = tempfile.mkstemp()

        with v2.writer(path, width=80, height=24) as w:
            w.write_stdout(1, "x")  # ensure it supports both str and bytes
            w.write_stdout(2, bytes.fromhex("78 c5 bc c3 b3 c5"))
            w.write_stdout(3, bytes.fromhex("82 c4 87"))
            w.write_stdout(4, bytes.fromhex("78 78"))


# Path: asciinema/asciicast/v2.py
# class LoadError(Exception):
# class Asciicast:
# class open_from_file:
# class writer(file_writer):
#     def __init__(
#         self, f: Union[TextIO, StreamReader], header: Dict[str, Any]
#     ) -> None:
#     def events(self) -> Generator[Any, None, None]:
#     def stdout_events(self) -> Generator[List[Any], None, None]:
# def build_from_header_and_file(
#     header: Dict[str, Any], f: Union[StreamReader, TextIO]
# ) -> Asciicast:
#     def __init__(
#         self, first_line: str, file: Union[StreamReader, TextIO]
#     ) -> None:
#     def __enter__(self) -> Asciicast:
#     def __exit__(
#         self, exc_type: str, exc_value: str, exc_traceback: str
#     ) -> None:
# def get_duration(path_: str) -> Any:
# def build_header(
#     width: Optional[int], height: Optional[int], metadata: Any
# ) -> Dict[str, Any]:
#     def __init__(  # pylint: disable=too-many-arguments
#         self,
#         path_: str,
#         metadata: Any = None,
#         append: bool = False,
#         buffering: int = 1,
#         width: Optional[int] = None,
#         height: Optional[int] = None,
#         on_error: Optional[Callable[[str], None]] = None,
#     ) -> None:
#     def __enter__(self) -> Any:
#     def write_stdout(self, ts: float, data: Union[str, bytes]) -> None:
#     def write_stdin(self, ts: float, data: Union[str, bytes]) -> None:
#     def _open_file(self) -> None:
#     def __write_event(self, ts: float, etype: str, data: str) -> None:
#     def __write_line(self, obj: Any) -> None:
#     FORMAT_ERROR = "only asciicast v2 format can be opened"
# 
# Path: tests/test_helper.py
# class Test:
#     def setUp(self) -> None:
#         global stdout  # pylint: disable=global-statement
#         self.real_stdout = sys.stdout
#         sys.stdout = stdout = StringIO()
# 
#     def tearDown(self) -> None:
#         sys.stdout = self.real_stdout


# Path: tests/asciicast/v2_test.py
import json
import tempfile
from asciinema.asciicast import v2
from ..test_helper import Test





class TestWriter(Test):
    @staticmethod
    def test_writing() -> None:
        _file, path = tempfile.mkstemp()

        with v2.writer(path, width=80, height=24) as w:
            w.write_stdout(1, "x")  # ensure it supports both str and bytes
            w.write_stdout(2, bytes.fromhex("78 c5 bc c3 b3 c5"))
            w.write_stdout(3, bytes.fromhex("82 c4 87"))
            w.write_stdout(4, bytes.fromhex("78 78"))


        with open(path, "rt", encoding="utf_8") as f: