"""Tests for kiosk.py.

Author: CS149 Faculty
Version: 11/10/2026
"""

import subprocess
import sys
from pathlib import Path

KIOSK = Path(__file__).with_name("kiosk.py")


def run_kiosk(typed):
    """Run kiosk.py as if a visitor typed the given lines.

    Each run starts fresh from shelter_data.py, so adoptions don't carry over.

    Args:
        typed: Everything the visitor types, with a newline after each answer.

    Returns:
        Everything the kiosk printed.
    """
    result = subprocess.run(
        [sys.executable, KIOSK], input=typed, capture_output=True, text=True, cwd=KIOSK.parent
    )
    return result.stdout + result.stderr


def test_list_pets():
    out = run_kiosk("1\n0\n")
    assert "Welcome to Blue Ridge Paws!" in out
    assert "Biscuit   dog     age 3   playful, good with kids" in out
    assert "Noodle    rabbit  age 2   curious" in out
    # adopted pets are not listed
    assert "Pepper" not in out


def test_find_match():
    out = run_kiosk("2\ndog\n5\ngood with kids\n0\n")
    assert "2 match(es): Biscuit, Juniper" in out
    out = run_kiosk("2\nrabbit\n1\ncalm\n0\n")
    assert "No matches yet. Check back soon!" in out


def test_adopt():
    out = run_kiosk("3\nmochi\nRiley\n3\nPepper\nRiley\n1\n0\n")
    assert "Congratulations, Riley! Mochi is going home with you." in out
    assert "Sorry, Pepper isn't available." in out
    # Mochi has a home now, so she's no longer listed
    assert "Mochi     cat" not in out
    assert "Goodbye! 5 pets are still waiting for a home." in out


def test_invalid_choice():
    out = run_kiosk("7\n0\n")
    assert "7 isn't an option. Try again." in out
