#!/usr/bin/env bats # tests/gen-keybinds.bats — black-box tests for scripts/gen-keybinds.sh # # Contract: parse a keybinds.kdl into a markdown cheat sheet on stdout, # or into a file with --write OUT. Observable surface: markdown rows, # section headers, exit status, written file. setup() { GEN="$BATS_TEST_DIRNAME/../scripts/gen-keybinds.sh" TMP="$(mktemp -d)" cat > "$TMP/keybinds.kdl" <<'EOF' binds { // ─── Applications ─── Mod+Return hotkey-overlay-title="Open Terminal: Ghostty" { spawn "ghostty"; } Mod+D hotkey-overlay-title="Open App Launcher: noctalia launcher" { spawn-sh "noctalia msg panel-toggle launcher"; } Mod+E { spawn "nautilus"; } // ─── Screenshots ─── Print hotkey-overlay-title="Fullscreen screenshot" { spawn-sh "noctalia msg screenshot-fullscreen"; } // a plain comment, not a section Mod+Shift+ESCAPE { show-hotkey-overlay; } } EOF } teardown() { rm -rf "$TMP" } @test "renders a row per bind with title preferred over action" { run "$GEN" "$TMP/keybinds.kdl" [ "$status" -eq 0 ] [[ "$output" == *'| `Mod+D` | Open App Launcher: noctalia launcher |'* ]] [[ "$output" == *'| `Mod+Return` | Open Terminal: Ghostty |'* ]] } @test "binds without a title render their action" { run "$GEN" "$TMP/keybinds.kdl" [[ "$output" == *'| `Mod+E` | spawn "nautilus" |'* ]] [[ "$output" == *'| `Mod+Shift+ESCAPE` | show-hotkey-overlay |'* ]] } @test "section comments become headers; plain comments are ignored" { run "$GEN" "$TMP/keybinds.kdl" [[ "$output" == *"## Applications"* ]] [[ "$output" == *"## Screenshots"* ]] [[ "$output" != *"a plain comment"* ]] } @test "row count matches bind count" { run "$GEN" "$TMP/keybinds.kdl" local rows rows="$(grep -c '^| `' <<<"$output")" [ "$rows" -eq 5 ] } @test "--write writes the markdown to the given path" { run "$GEN" "$TMP/keybinds.kdl" --write "$TMP/out/keybindings.md" [ "$status" -eq 0 ] [ -f "$TMP/out/keybindings.md" ] grep -q '| `Mod+D` | Open App Launcher: noctalia launcher |' "$TMP/out/keybindings.md" grep -q 'GENERATED' "$TMP/out/keybindings.md" } @test "output begins with OKF frontmatter so the artifact passes kb-lint" { run "$GEN" "$TMP/keybinds.kdl" [ "${lines[0]}" = '---' ] [[ "${lines[1]}" == 'type: reference' ]] grep -q '^generated:' <<<"$output" } @test "missing input file fails loudly" { run "$GEN" "$TMP/nope.kdl" [ "$status" -ne 0 ] [[ "$output" == *"no such file"* ]] }