import types

import pytest

import skilleter_modules.docker as docker


class DummyResult(types.SimpleNamespace):
    """Simple stand-in for subprocess.run results."""

    pass


def test_instances_parses_lines(monkeypatch):
    """Ensure docker.instances parses output lines into a list.

    Args:
        monkeypatch: Pytest monkeypatch fixture.

    Returns:
        None.
    """
    def fake_run(cmd, capture_output, check, text):
        """Return a dummy result with two container IDs.

        Args:
            cmd: Command list passed to subprocess.run.
            capture_output: Whether output is captured.
            check: Whether to raise on error.
            text: Whether output is text.

        Returns:
            DummyResult with stdout set.
        """
        assert cmd[:2] == ['docker', 'ps']
        return DummyResult(stdout='id1\nid2\n')

    monkeypatch.setattr('subprocess.run', fake_run)
    assert docker.instances() == ['id1', 'id2']


def test_stop_force_passes_flag(monkeypatch):
    """Ensure docker.stop passes --force when requested.

    Args:
        monkeypatch: Pytest monkeypatch fixture.

    Returns:
        None.
    """
    seen = {}

    def fake_run(cmd, check, capture_output):
        """Record the command passed to subprocess.run.

        Args:
            cmd: Command list passed to subprocess.run.
            check: Whether to raise on error.
            capture_output: Whether output is captured.

        Returns:
            DummyResult instance.
        """
        seen['cmd'] = cmd
        return DummyResult()

    monkeypatch.setattr('subprocess.run', fake_run)
    docker.stop('abc', force=True)
    assert seen['cmd'] == ['docker', 'stop', '--force', 'abc']
