<commit_before># Run with ./run-tests.sh

start_fake_lircd() {
    waitfor() {
        for i in {1..100}; do grep "$1" "$2" && return; sleep 0.1; done;
        return 1
    }

    $python "$testdir"/fake-lircd "$@" > fake-lircd.log &
    fake_lircd=$!
    trap "kill $fake_lircd" EXIT
    waitfor "^SOCKET=" fake-lircd.log || fail "fake-lircd failed to start"
    lircd_socket=$(awk -F= '/^SOCKET=/ { print $2 }' fake-lircd.log)
}

test_press_with_lirc() {
    start_fake_lircd

    cat > test.py <<-EOF &&
	press("menu")
	press("ok")
	EOF
    stbt run -v --control lirc:$lircd_socket:test test.py || return
    grep -Eq "fake-lircd: Received: b?'?SEND_ONCE test menu" fake-lircd.log &&
    grep -Eq "fake-lircd: Received: b?'?SEND_ONCE test ok" fake-lircd.log ||
        fail "fake-lircd didn't receive 2 SEND_ONCE messages"
}

test_that_press_fails_on_lircd_error() {
    start_fake_lircd

    cat > test.py <<-EOF &&
	press("button_that_causes_error")
	EOF
    ! stbt run -v --control lirc:$lircd_socket:test test.py ||
        fail "Expected 'press' to raise exception"
    cat log | grep 'RuntimeError' | grep 'fake-lircd error' ||
        fail "Expected to see RuntimeError('fake-lircd error')"
}

test_that_press_times_out_when_lircd_doesnt_reply() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_timeout")
	EOF
    timeout 10s stbt run -v --control lirc:$lircd_socket:test test.py
    ret=$?
    [ $ret -eq $timedout ] && fail "'stbt run' timed out"
    [ $ret -ne 0 ] || fail "Expected 'press' to raise exception"
}

test_that_press_ignores_lircd_broadcast_messages_on_success() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_sighup_and_broadcast_and_ack")
	EOF
    stbt run -v --control lirc:$lircd_socket:test test.py || return
}

test_that_press_ignores_lircd_broadcast_messages_on_error() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_sighup_and_broadcast_and_error")
	EOF
    ! stbt run -v --control lirc:$lircd_socket:test test.py ||
        fail "Expected 'press' to raise exception"
    cat log | grep 'RuntimeError' | grep 'fake-lircd error' ||
        fail "Expected to see RuntimeError('fake-lircd error')"
}

test_that_press_ignores_lircd_broadcast_messages_on_no_reply() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_sighup_and_broadcast_and_timeout")
	EOF
    timeout 10s stbt run -v --control lirc:$lircd_socket:test test.py
    ret=$?
    [ $ret -eq $timedout ] && fail "'stbt run' timed out"
    [ $ret -ne 0 ] || fail "Expected 'press' to raise exception"
}

test_press_hold_secs_with_lirc() {
    start_fake_lircd

    cat > test.py <<-EOF &&
	import stbt
	stbt.press("KEY_LEFT", hold_secs=0.1)
	with stbt.pressing("KEY_RIGHT"):
	    pass
	EOF

    stbt run -v --control lirc:$lircd_socket:test test.py ||
        fail "stbt run failed"

    grep -Eq "fake-lircd: Received: b?'?SEND_START test KEY_LEFT" fake-lircd.log &&
    grep -Eq "fake-lircd: Received: b?'?SEND_STOP test KEY_LEFT" fake-lircd.log ||
        fail "press: fake-lircd dind't see SEND_START and SEND_STOP"

    grep -Eq "fake-lircd: Received: b?'?SEND_START test KEY_RIGHT" fake-lircd.log &&
    grep -Eq "fake-lircd: Received: b?'?SEND_STOP test KEY_RIGHT" fake-lircd.log ||
        fail "pressing: fake-lircd didn't see SEND_START and SEND_STOP"
}
<commit_msg>test_that_press_times_out...: Increase timeout waiting for `stbt run`<commit_after># Run with ./run-tests.sh

start_fake_lircd() {
    waitfor() {
        for i in {1..100}; do grep "$1" "$2" && return; sleep 0.1; done;
        return 1
    }

    $python "$testdir"/fake-lircd "$@" > fake-lircd.log &
    fake_lircd=$!
    trap "kill $fake_lircd" EXIT
    waitfor "^SOCKET=" fake-lircd.log || fail "fake-lircd failed to start"
    lircd_socket=$(awk -F= '/^SOCKET=/ { print $2 }' fake-lircd.log)
}

test_press_with_lirc() {
    start_fake_lircd

    cat > test.py <<-EOF &&
	press("menu")
	press("ok")
	EOF
    stbt run -v --control lirc:$lircd_socket:test test.py || return
    grep -Eq "fake-lircd: Received: b?'?SEND_ONCE test menu" fake-lircd.log &&
    grep -Eq "fake-lircd: Received: b?'?SEND_ONCE test ok" fake-lircd.log ||
        fail "fake-lircd didn't receive 2 SEND_ONCE messages"
}

test_that_press_fails_on_lircd_error() {
    start_fake_lircd

    cat > test.py <<-EOF &&
	press("button_that_causes_error")
	EOF
    ! stbt run -v --control lirc:$lircd_socket:test test.py ||
        fail "Expected 'press' to raise exception"
    cat log | grep 'RuntimeError' | grep 'fake-lircd error' ||
        fail "Expected to see RuntimeError('fake-lircd error')"
}

test_that_press_times_out_when_lircd_doesnt_reply() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_timeout")
	EOF
    timeout 30s stbt run -v --control lirc:$lircd_socket:test test.py
    ret=$?
    [ $ret -eq $timedout ] && fail "'stbt run' timed out"
    [ $ret -ne 0 ] || fail "Expected 'press' to raise exception"
}

test_that_press_ignores_lircd_broadcast_messages_on_success() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_sighup_and_broadcast_and_ack")
	EOF
    stbt run -v --control lirc:$lircd_socket:test test.py || return
}

test_that_press_ignores_lircd_broadcast_messages_on_error() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_sighup_and_broadcast_and_error")
	EOF
    ! stbt run -v --control lirc:$lircd_socket:test test.py ||
        fail "Expected 'press' to raise exception"
    cat log | grep 'RuntimeError' | grep 'fake-lircd error' ||
        fail "Expected to see RuntimeError('fake-lircd error')"
}

test_that_press_ignores_lircd_broadcast_messages_on_no_reply() {
    start_fake_lircd

    cat > test.py <<-EOF
	press("button_that_causes_sighup_and_broadcast_and_timeout")
	EOF
    timeout 30s stbt run -v --control lirc:$lircd_socket:test test.py
    ret=$?
    [ $ret -eq $timedout ] && fail "'stbt run' timed out"
    [ $ret -ne 0 ] || fail "Expected 'press' to raise exception"
}

test_press_hold_secs_with_lirc() {
    start_fake_lircd

    cat > test.py <<-EOF &&
	import stbt
	stbt.press("KEY_LEFT", hold_secs=0.1)
	with stbt.pressing("KEY_RIGHT"):
	    pass
	EOF

    stbt run -v --control lirc:$lircd_socket:test test.py ||
        fail "stbt run failed"

    grep -Eq "fake-lircd: Received: b?'?SEND_START test KEY_LEFT" fake-lircd.log &&
    grep -Eq "fake-lircd: Received: b?'?SEND_STOP test KEY_LEFT" fake-lircd.log ||
        fail "press: fake-lircd dind't see SEND_START and SEND_STOP"

    grep -Eq "fake-lircd: Received: b?'?SEND_START test KEY_RIGHT" fake-lircd.log &&
    grep -Eq "fake-lircd: Received: b?'?SEND_STOP test KEY_RIGHT" fake-lircd.log ||
        fail "pressing: fake-lircd didn't see SEND_START and SEND_STOP"
}

{'fraction_non_alphanumeric': 0.12870996179841315, 'fraction_numerical': 0.005583308845136644, 'mean_word_length': 3.513925729442971, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4509829', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2633, 'n_words': 596}