diff --git a/.github/coverage-floor.txt b/.github/coverage-floor.txt new file mode 100644 index 00000000..f3d0ccce --- /dev/null +++ b/.github/coverage-floor.txt @@ -0,0 +1,24 @@ +# Per-module line-coverage floor (percent). Checked by scripts/coverage.sh; a module below its floor fails CI. +# Regenerate after raising coverage: scripts/coverage.sh && scripts/coverage.sh --update-floor +# (recorded as measured minus 2 points of run-to-run slack, floored). Never lower a value by hand. +api 85 +cli 94 +colon_command 82 +config 92 +(crate root) 67 +daemon 75 +input 77 +mcp_console 84 +pane 81 +pty 92 +remote 90 +# stt: calibrated on the CI runner (72.4%), which has no audio recorder on PATH; a dev box with +# pw-record/parecord/arecord/ffmpeg reaches ~75.6% because the recorder probe then succeeds. +stt 70 +swarm 92 +tmux 92 +TOTAL 82 +traffic 91 +ui 80 +vt 88 +wm 84 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 60ab31df..f7db88fb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,16 @@ name: ci +# Six native cells: {Linux, macOS, Windows} x {x86_64, ARM64}. +# +# Tier | quick (build, clippy, unit, binary smoke) | integration (every tests/*.rs, 3 shards) | coverage +# ------------|-------------------------------------------|------------------------------------------|--------- +# pull_request| all six cells | linux-x86_64 + macos-arm64 | linux-x86_64 +# push to main| all six cells | all six cells | linux-x86_64 +# nightly | all six cells | all six cells | linux-x86_64 +# manual | all six cells | all six cells | linux-x86_64 +# +# Every job runs the same one-line gate (scripts/verify.sh -> "PASS n/n" | "FAIL k/n: "); +# docs/TEST_COVERAGE.md explains the tiers and how to reproduce each cell locally. on: pull_request: push: @@ -8,6 +19,9 @@ on: - '**.md' - '.planning/**' - 'docs/**' + schedule: + - cron: '17 4 * * *' # nightly, off the hour + workflow_dispatch: permissions: contents: read @@ -19,34 +33,191 @@ concurrency: env: CARGO_TERM_COLOR: always CARGO_INCREMENTAL: 0 + # Test builds do not need debugger-grade symbols; this roughly halves target/ and link time. + CARGO_PROFILE_DEV_DEBUG: 0 + +defaults: + run: + shell: bash # Git Bash on Windows: one script for every OS jobs: - test: - name: clippy + quick verify (${{ matrix.os }}) + # --------------------------------------------------------------------------------------------- + # Decides which cells run the full integration tier for this event (see the table above). + plan: + name: plan + runs-on: ubuntu-latest + outputs: + integration: ${{ steps.cells.outputs.integration }} + steps: + - id: cells + env: + EVENT: ${{ github.event_name }} + run: | + linux_x64='{"cell":"linux-x86_64","os":"ubuntu-latest","experimental":false}' + linux_arm='{"cell":"linux-arm64","os":"ubuntu-24.04-arm","experimental":false}' + mac_arm='{"cell":"macos-arm64","os":"macos-latest","experimental":false}' + mac_x64='{"cell":"macos-x86_64","os":"macos-15-intel","experimental":false}' + win_x64='{"cell":"windows-x86_64","os":"windows-latest","experimental":true}' + win_arm='{"cell":"windows-arm64","os":"windows-11-arm","experimental":true}' + if [ "$EVENT" = pull_request ]; then + cells="[$linux_x64,$mac_arm]" + else + cells="[$linux_x64,$linux_arm,$mac_arm,$mac_x64,$win_x64,$win_arm]" + fi + echo "integration={\"cell\":$cells,\"shard\":[1,2,3]}" >>"$GITHUB_OUTPUT" + + # --------------------------------------------------------------------------------------------- + # Every event, every cell: the product builds, lints clean, passes its unit tests and starts. + quick: + name: ${{ matrix.name }} + runs-on: ${{ matrix.os }} + timeout-minutes: 45 strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest] - runs-on: ${{ matrix.os }} + include: + # Branch protection on main requires the check contexts `clippy + quick verify (ubuntu-latest)`, + # `clippy + quick verify (macos-latest)` and `cargo-deny`. These two cells keep those exact + # names so the required checks keep reporting; rename them only together with the rule. + - { cell: linux-x86_64, os: ubuntu-latest, experimental: false, name: 'clippy + quick verify (ubuntu-latest)' } + - { cell: linux-arm64, os: ubuntu-24.04-arm, experimental: false, name: 'quick (linux-arm64)' } + - { cell: macos-arm64, os: macos-latest, experimental: false, name: 'clippy + quick verify (macos-latest)' } + # Intel macOS: `macos-13` is retired; `macos-15-intel` is the standard x86_64 label + # (`macos-26-intel` also exists). Revisit when macOS 15 images are retired. + - { cell: macos-x86_64, os: macos-15-intel, experimental: false, name: 'quick (macos-x86_64)' } + # Windows: build + clippy are hard gates (cross-checked clean for both MSVC targets). + # The test steps are `continue-on-error` until the first green run on real Windows + # runners (tracking item WIN-TESTS in docs/TEST_COVERAGE.md); their real outcome is + # always written to the job summary, never hidden. + - { cell: windows-x86_64, os: windows-latest, experimental: true, name: 'quick (windows-x86_64)' } + - { cell: windows-arm64, os: windows-11-arm, experimental: true, name: 'quick (windows-arm64)' } + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + # No extra system packages: the default build has no -sys crates that need distro libraries + # (no X11/Wayland); the runner's C toolchain suffices. + - name: Install Rust toolchain (channel from rust-toolchain.toml) + run: | + if ! command -v rustup >/dev/null; then # windows-11-arm images may ship without rustup + curl -sSfLo "$RUNNER_TEMP/rustup-init.exe" https://static.rust-lang.org/rustup/dist/aarch64-pc-windows-msvc/rustup-init.exe + "$RUNNER_TEMP/rustup-init.exe" -y --profile minimal --default-toolchain none + echo "$USERPROFILE/.cargo/bin" >>"$GITHUB_PATH" + export PATH="$USERPROFILE/.cargo/bin:$PATH" + fi + channel=$(sed -n 's/^channel *= *"\(.*\)"/\1/p' rust-toolchain.toml) + rustup toolchain install "$channel" --profile minimal --component clippy + rustc -vV + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + key: quick-${{ matrix.cell }} + - name: Build + run: cargo build --locked + - name: Clippy + run: bash scripts/verify.sh --only clippy + - name: Unit tests + id: unit + continue-on-error: ${{ matrix.experimental }} + run: bash scripts/verify.sh --only unit + - name: Binary smoke (--version, --help, MCP session, RPC session, PTY) + id: smoke + continue-on-error: ${{ matrix.experimental }} + run: bash scripts/verify.sh --only t:smoke_binary_per_os + - name: Report + if: always() + env: + CELL: ${{ matrix.cell }} + UNIT: ${{ steps.unit.outcome }} + SMOKE: ${{ steps.smoke.outcome }} + EXPERIMENTAL: ${{ matrix.experimental }} + run: bash scripts/ci_report.sh "quick ($CELL)" "unit=$UNIT" "smoke=$SMOKE" "non-blocking-tests=$EXPERIMENTAL" + + # --------------------------------------------------------------------------------------------- + # Every tests/*.rs, fanned out over three shards per cell. + integration: + name: integration (${{ matrix.cell.cell }}, ${{ matrix.shard }}/3) + needs: plan + runs-on: ${{ matrix.cell.os }} + timeout-minutes: 90 + # Windows cells: see WIN-TESTS above. A failing shard is reported in the job summary. + continue-on-error: ${{ matrix.cell.experimental }} + strategy: + fail-fast: false + matrix: ${{ fromJSON(needs.plan.outputs.integration) }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Install Rust toolchain (channel from rust-toolchain.toml) + run: | + if ! command -v rustup >/dev/null; then + curl -sSfLo "$RUNNER_TEMP/rustup-init.exe" https://static.rust-lang.org/rustup/dist/aarch64-pc-windows-msvc/rustup-init.exe + "$RUNNER_TEMP/rustup-init.exe" -y --profile minimal --default-toolchain none + echo "$USERPROFILE/.cargo/bin" >>"$GITHUB_PATH" + export PATH="$USERPROFILE/.cargo/bin:$PATH" + fi + channel=$(sed -n 's/^channel *= *"\(.*\)"/\1/p' rust-toolchain.toml) + rustup toolchain install "$channel" --profile minimal + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + # One cache per cell, shared by its three shards (they build the same dependency graph). + shared-key: integration-${{ matrix.cell.cell }} + save-if: ${{ matrix.shard == 1 }} + - name: Integration tests (shard ${{ matrix.shard }}/3) + id: gate + env: + SHARD: ${{ matrix.shard }} + run: bash scripts/verify.sh --integration --shard "$SHARD/3" + - name: Report + if: always() + env: + CELL: ${{ matrix.cell.cell }} + SHARD: ${{ matrix.shard }} + GATE: ${{ steps.gate.outcome }} + EXPERIMENTAL: ${{ matrix.cell.experimental }} + run: bash scripts/ci_report.sh "integration ($CELL, $SHARD/3)" "gate=$GATE" "non-blocking-tests=$EXPERIMENTAL" + + # --------------------------------------------------------------------------------------------- + # Line coverage with a per-module ratchet (.github/coverage-floor.txt). No external service: + # the table goes to the job summary, lcov + HTML to a workflow artifact. + coverage: + name: coverage (linux-x86_64) + runs-on: ubuntu-latest + timeout-minutes: 90 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - # No extra system packages: the default build has no -sys crates that - # need distro libraries (no X11/Wayland); the runner's C toolchain suffices. - - name: Install Rust toolchain + - name: Install Rust toolchain + llvm-tools run: | - rustup toolchain install stable --profile minimal --component clippy - rustup default stable + channel=$(sed -n 's/^channel *= *"\(.*\)"/\1/p' rust-toolchain.toml) + rustup toolchain install "$channel" --profile minimal --component llvm-tools-preview - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 - - name: Quick verify (clippy + unit tests) + with: + key: coverage + - name: Install cargo-llvm-cov (pinned, built from crates.io) + run: cargo llvm-cov --version 2>/dev/null | grep -q ' 0\.9\.1$' || cargo install cargo-llvm-cov --locked --version 0.9.1 + - name: Coverage + ratchet + run: bash scripts/coverage.sh + - name: Job summary + if: always() run: | - status=0 - bash scripts/verify.sh --quick || status=$? - for f in target/verify/*/fail.txt; do - [ -f "$f" ] && { echo "::group::$f"; cat "$f"; echo "::endgroup::"; } - done - exit "$status" + [ -f target/coverage/summary.md ] && cat target/coverage/summary.md >>"$GITHUB_STEP_SUMMARY" + [ -f target/coverage/modules.tsv ] && cat target/coverage/modules.tsv + exit 0 + - name: Upload lcov + HTML report + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: coverage-report + path: | + target/coverage/lcov.info + target/coverage/modules.tsv + target/coverage/gaps.txt + target/coverage/summary.md + target/coverage/html + if-no-files-found: warn + retention-days: 14 deny: name: cargo-deny diff --git a/AGENTS.md b/AGENTS.md index 37f38653..2e6893fe 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -169,7 +169,7 @@ Bilterm is a terminal user interface (TUI) multiplexer, shell wrapper, and termi 5. **Context Window & Token-Efficiency Discipline (`PERF-CTX-01..03`)**: - **Compact Between Tasks**: Recommend or run `/compact` after updating `.planning/CHECKPOINT.md` when switching between major tasks so tool turns do not replay large historical contexts. - **Quiet Cargo Flags (`-q` / `--quiet`)**: Always pass `-q` (`cargo build -q`, `cargo clippy -q --all-targets -- -D warnings`, `cargo test -q`) to suppress routine `Compiling ...` and `test ... ok` lines (~90% token reduction). - - **Scripted Verification (`scripts/verify.sh`) — default gate**: one command, one output line. `PASS / s` means done: do NOT read logs. `FAIL /: clippy t:foo -> ...` means read only `target/verify//fail.txt` (<=40 lines), then `target/verify//log` if still unclear. Result tree: `target/verify/STATUS` (PASS|FAIL), `summary.tsv` (stage, status, secs), `/{status,fail.txt,log}`. Flags: `--quick` (clippy+unit), `--failed` (rerun only failures), `--only ...`, `--list`, `--status` (re-read last result, no run). Stages are one per `tests/*.rs` (`t:`) plus `clippy`, `unit`; `fmt` is opt-in (legacy drift). New test suites get picked up automatically; new gates (security, perf, arch) go in as new stages in the same script with the same file contract. Never run bare `cargo test` and read its output when this script covers it. + - **Scripted Verification (`scripts/verify.sh`) — default gate**: one command, one output line. `PASS / s` means done: do NOT read logs. `FAIL /: clippy t:foo -> ...` means read only `target/verify//fail.txt` (<=40 lines), then `target/verify//log` if still unclear. Result tree: `target/verify/STATUS` (PASS|FAIL), `summary.tsv` (stage, status, secs), `/{status,fail.txt,log}`. Flags: `--quick` (clippy+unit), `--failed` (rerun only failures), `--only ...`, `--list`, `--status` (re-read last result, no run). Stages are one per `tests/*.rs` (`t:`) plus `clippy`, `unit`; `fmt` is opt-in (legacy drift). New test suites get picked up automatically; new gates (security, perf, arch) go in as new stages in the same script with the same file contract. Never run bare `cargo test` and read its output when this script covers it. On Windows use `pwsh scripts/verify.ps1` (same line, same files). CI fan-out: `--integration` (every `tests/*.rs`, no clippy/unit) and `--shard K/N`. Skipped or ignored tests are listed in `target/verify/skips.txt`. Coverage has the same contract: `scripts/coverage.sh` prints `PASS coverage N%` or `FAIL coverage N%: X% [branch]`, not under `/tmp` (quota'd tmpfs; a full test build is ~9G even with reduced debug info). Roots come from `git config bilterm.worktreeRoot` / `bilterm.targetRoot` (machine-local). Existing worktree out of space: `scripts/worktree.sh relocate-target`. - **Delegate Build/Test Loops to `flash` Subagent**: Run multi-step `cargo build -q` / `cargo clippy -q` / `cargo test -q` verification loops inside an isolated `flash` subagent (`invoke_subagent` with `Model: "flash"`) so compiler/test logs never bloat the main conversation history. 6. **Surface Uniformity (`UNI-R01`)**: every functionality MUST be reachable on every surface, with the same behaviour: diff --git a/docs/DEVELOPER_AND_AGENT_GUIDE.md b/docs/DEVELOPER_AND_AGENT_GUIDE.md index e1a5e265..825291c1 100644 --- a/docs/DEVELOPER_AND_AGENT_GUIDE.md +++ b/docs/DEVELOPER_AND_AGENT_GUIDE.md @@ -483,7 +483,14 @@ on both a Wire and a Payload cell of the same view. ## 7. Verification & CI Conservation Gates -Always run these 4 checks locally before committing or pushing: +The default gate is one command with one output line on every OS: `scripts/verify.sh` (Linux, macOS, +Git Bash) or `pwsh scripts/verify.ps1` (Windows). CI runs it on six cells +({Linux, macOS, Windows} x {x86_64, ARM64}); the tiers, sharding (`--integration --shard K/N`), +cross-target checks, portable-test rules (`tests/common/mod.rs`) and the coverage ratchet +(`scripts/coverage.sh`, `.github/coverage-floor.txt`) are documented in +[`TEST_COVERAGE.md` section 8](TEST_COVERAGE.md#8-ci-matrix-per-os-gate-cross-checks-and-the-coverage-ratchet). + +The underlying checks, for reference: ```bash # 1. Full unit, integration, snapshot, and security test suite diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index 66d7ee5c..98000dee 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -1,6 +1,6 @@ -# Test Suite & Coverage Report — `bilterm` (`v3.2.0`) +# Test Suite & Coverage Report — `bilterm` (`v3.3.0`) -**Date:** `2026-11-08` +**Date:** `2026-11-10` **Tooling used:** `cargo llvm-cov 0.9.1`, `cargo test 1.90`, `insta 1.42`, `ratatui::backend::TestBackend`, `tokio::test`, `xvfb-run -a` (Linux X11 Headless Driver), `scripts/verify.sh` cross-platform composable runner. --- @@ -96,12 +96,34 @@ Bilterm incorporates a unified 4-mode live test driver architecture (`tests/live ## 4. Coverage Metrics Summary -| Metric | Measured Value | Standard Quality Gate | -|---|---|---| -| **Line Coverage** | **83.6%** (`9,850 / 11,780`) | `>= 80.0%` (Enforced) | -| **Function Coverage** | **82.1%** (`665 / 810`) | `>= 75.0%` (Enforced) | -| **Region Coverage** | **82.9%** (`16,050 / 19,360`) | `>= 75.0%` (Enforced) | -| **Verification Stages Passing** | **120 / 120** | 100% | +Measured with `scripts/coverage.sh` (`cargo llvm-cov`, unit + every `tests/*.rs`, Linux x86_64). +The **Floor** column is the ratchet in `.github/coverage-floor.txt`: CI fails when a module drops below it. + +| Module (`src/`) | Covered lines | Lines | Line coverage | Floor | +|---|---:|---:|---:|---:| +| `api` | 6944 | 7984 | 87.0% | 85% | +| `cli` | 279 | 288 | 96.9% | 94% | +| `colon_command` | 5393 | 6398 | 84.3% | 82% | +| `config` | 831 | 882 | 94.2% | 92% | +| `(crate root)` | 1391 | 1996 | 69.7% | 67% | +| `daemon` | 976 | 1278 | 76.4% | 74% | +| `input` | 6120 | 7731 | 79.2% | 77% | +| `mcp_console` | 494 | 574 | 86.1% | 84% | +| `pane` | 6249 | 7496 | 83.4% | 81% | +| `pty` | 524 | 555 | 94.4% | 92% | +| `remote` | 347 | 374 | 92.8% | 90% | +| `stt` | 927 | 1227 | 75.6% | 73% | +| `swarm` | 675 | 711 | 94.9% | 92% | +| `tmux` | 1423 | 1511 | 94.2% | 92% | +| `traffic` | 1256 | 1338 | 93.9% | 91% | +| `ui` | 14461 | 17910 | 80.7% | 78% | +| `vt` | 4005 | 4454 | 89.9% | 87% | +| `wm` | 18128 | 20918 | 86.7% | 84% | +| **TOTAL** | **70423** | **83625** | **84.2%** | **82%** | + +The largest uncovered regions are platform FFI that cannot run headless (`src/ui/gui_font/linux_x11.rs`, +the OS clipboard back ends in `src/clipboard.rs`) and interactive entry points (`src/main.rs`, `src/app.rs`); +`scripts/coverage.sh --gaps 25` prints the current list. --- @@ -134,3 +156,81 @@ Bilterm incorporates a unified 4-mode live test driver architecture (`tests/live 2. **Unified Action Lowering:** Every surface (mouse clicks, leader chords, F-keys, colon commands, palette search, RPC, and MCP) lowers to the central `Action` enum, guaranteeing 100% functional parity. 3. **Decoupled VT Worker Emulation:** PTY reads and ANSI parsing occur on dedicated background worker threads, preventing heavy terminal output from blocking UI rendering or input dispatch. 4. **Resilient Daemon & Supervisor:** Multiplexed state persists seamlessly across frontend restarts and SSH reconnections. + +--- + +## 8. CI Matrix, Per-OS Gate, Cross-Checks and the Coverage Ratchet + +### 8.1 What each CI tier runs (`.github/workflows/ci.yml`) + +Six native cells: `linux-x86_64` (`ubuntu-latest`), `linux-arm64` (`ubuntu-24.04-arm`), `macos-arm64` +(`macos-latest`), `macos-x86_64` (`macos-15-intel`), `windows-x86_64` (`windows-latest`), `windows-arm64` +(`windows-11-arm`). `fail-fast` is off: one red cell never hides the others. + +| Job | Runs | pull request | push to `main`, nightly, manual | +|---|---|---|---| +| `quick` | `cargo build --locked`, clippy, unit tests, `tests/smoke_binary_per_os.rs` | all six cells | all six cells | +| `integration` | every `tests/*.rs`, 3 shards per cell (`verify.sh --integration --shard K/3`) | `linux-x86_64`, `macos-arm64` | all six cells | +| `coverage` | `scripts/coverage.sh` + ratchet, lcov/HTML artifact, table in the job summary | `linux-x86_64` | `linux-x86_64` | +| `deny` | `cargo deny check` | yes | yes | + +**WIN-TESTS (open):** both Windows targets build and pass clippy (hard gates). Their unit, smoke and +integration steps are `continue-on-error` until they have been seen green on real Windows runners; +the true outcome is always written to the job summary (`scripts/ci_report.sh`). Close the item by +deleting `experimental: true` from the two Windows rows once they pass. + +Every job summary also lists the tests that did not really run (`target/verify/skips.txt`): tests +marked `#[ignore = "reason"]` and runtime `SKIPPED : ` lines from `tests/common::skip`. + +### 8.2 Running the gate on each OS + +The contract is the same everywhere: one line (`PASS n/n s` or `FAIL k/n: -> ...`), +exit code 0/1/2, details under `target/verify/`. + +| OS | Full gate | Quick gate | One suite | +|---|---|---|---| +| Linux, macOS | `scripts/verify.sh` | `scripts/verify.sh --quick` | `scripts/verify.sh --only t:` | +| Windows (PowerShell) | `pwsh scripts/verify.ps1` | `pwsh scripts/verify.ps1 --quick` | `pwsh scripts/verify.ps1 --only t:` | +| Windows (Git Bash) | `bash scripts/verify.sh` | `bash scripts/verify.sh --quick` | `bash scripts/verify.sh --only t:` | + +`verify.ps1` only locates the bash that ships with Git for Windows and runs `verify.sh`; there is one +implementation of the stage logic. To reproduce a CI shard: `scripts/verify.sh --integration --shard 2/3`. + +### 8.3 Cross-checking other targets from one machine + +Type-checking and linting need no linker or SDK, so every target can be checked from Linux: + +```bash +rustup target add aarch64-unknown-linux-gnu x86_64-apple-darwin aarch64-apple-darwin \ + x86_64-pc-windows-msvc aarch64-pc-windows-msvc +for t in aarch64-unknown-linux-gnu x86_64-apple-darwin aarch64-apple-darwin \ + x86_64-pc-windows-msvc aarch64-pc-windows-msvc; do + CARGO_TARGET_DIR=target/cross cargo clippy -q --all-targets --target "$t" -- -D warnings \ + && echo "OK $t" || echo "FAIL $t" +done +``` + +This proves a target compiles and lints; only the native runner proves it links and its tests pass. + +### 8.4 Writing portable tests (`tests/common/mod.rs`) + +- Spawn the binary through `IsolatedEnv` (throwaway HOME / XDG / runtime dir, no route to a live instance). +- Never hard-code `/bin/sh`, `/tmp` or `sleep`: use `shell_command`, `tempfile`, and `wait_until(deadline, ...)`. +- A platform-specific test is marked `#[cfg_attr(windows, ignore = "reason")]` (visible as `ignored, reason`); + a condition only known at run time calls `common::skip("reason")`. No test may pass by returning early silently. +- Randomised tests use `common::Rng` with fixed seeds, so a failure reproduces on every OS. + +### 8.5 Reading and ratcheting coverage + +```bash +cargo install cargo-llvm-cov --locked && rustup component add llvm-tools-preview # once +scripts/coverage.sh # one line: PASS coverage N% | FAIL coverage N%: X%" [key=value ...] +# Prints each failing stage's fail.txt as a collapsible log group, then appends to +# $GITHUB_STEP_SUMMARY (stdout when unset): the one-line result, the failing stages, the step +# outcomes passed as key=value, and every skipped/ignored test (target/verify/skips.txt). +# A step that is allowed to fail (`continue-on-error`) is therefore still reported as a failure +# here: nothing turns green silently. Always exits 0; the gate step decides the job result. +set -uo pipefail +cd "$(dirname "$0")/.." || exit 2 +OUT=target/verify +title="${1:-verify}" +shift || true + +for f in "$OUT"/*/fail.txt; do + [ -f "$f" ] || continue + echo "::group::$f" + cat "$f" + echo "::endgroup::" +done + +line=$(bash scripts/verify.sh --status 2>/dev/null || echo NORUN) +{ + echo "### $title" + echo + echo '```text' + echo "$line" + echo '```' + failed=0 + for kv in "$@"; do + case "$kv" in + *=failure) failed=1; echo "- :x: \`${kv%%=*}\` **failed**" ;; + *=success) echo "- :white_check_mark: \`${kv%%=*}\`" ;; + *=skipped|*=cancelled) echo "- :fast_forward: \`${kv%%=*}\` ${kv##*=}" ;; + non-blocking-tests=true) nonblocking=1 ;; + *) ;; + esac + done + if [ "$failed" = 1 ] && [ "${nonblocking:-0}" = 1 ]; then + echo + echo "> **Tests FAILED on this cell but do not block the run yet** (tracking item WIN-TESTS, docs/TEST_COVERAGE.md)." + fi + if [ -f "$OUT/summary.tsv" ] && grep -q $'\tFAIL\t' "$OUT/summary.tsv"; then + echo + echo "| Failing stage | Seconds |" + echo "|---|---:|" + awk -F'\t' '$2=="FAIL"{printf "| `%s` | %s |\n",$1,$3}' "$OUT/summary.tsv" + fi + if [ -s "$OUT/skips.txt" ]; then + echo + echo "
Skipped / ignored tests ($(wc -l <"$OUT/skips.txt" | tr -d ' '))" + echo + echo '```text' + cut -c1-300 "$OUT/skips.txt" + echo '```' + echo "
" + fi + echo +} >>"${GITHUB_STEP_SUMMARY:-/dev/stdout}" +exit 0 diff --git a/scripts/coverage.sh b/scripts/coverage.sh new file mode 100755 index 00000000..2ade3ca2 --- /dev/null +++ b/scripts/coverage.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +# Line-coverage runner + ratchet. Prints ONE line; details live in files (same contract as verify.sh). +# scripts/coverage.sh instrumented run of unit + every tests/*.rs, then report + floor check +# scripts/coverage.sh --quick instrumented run of unit tests only (fast; floor check is skipped) +# scripts/coverage.sh --report rebuild the tables from the existing target/coverage/lcov.info (no test run) +# scripts/coverage.sh --gaps [N] print the N (default 25) largest uncovered regions from lcov.info (no test run) +# scripts/coverage.sh --update-floor rewrite .github/coverage-floor.txt from the existing lcov.info (no test run) +# Needs cargo-llvm-cov (`cargo install cargo-llvm-cov --locked`) and the llvm-tools component +# (`rustup component add llvm-tools-preview`). No external coverage service is involved. +# Result tree (target/coverage/): +# STATUS PASS | FAIL +# modules.tsv modulecoveredlinespercentfloorOK|DROP|NEW +# summary.md the same table as Markdown (CI appends it to the job summary) +# gaps.txt largest uncovered regions (file:first-lastlines) +# lcov.info raw lcov tracefile html/index.html browsable report +# run.log output of the instrumented test run (test failures do not fail coverage; they are counted) +# Floor file (.github/coverage-floor.txt): `modulemin_percent`. A module below its floor is a FAIL. +# Exit code: 0 pass, 1 a module dropped below its floor (or the run produced no data), 2 usage/tooling. +set -uo pipefail +cd "$(dirname "$0")/.." || exit 2 +OUT=target/coverage +FLOOR=.github/coverage-floor.txt +# Slack (percentage points) subtracted when --update-floor records a floor: instrumented runs of +# timing-dependent tests differ slightly between machines, so the floor sits just under the measurement. +SLACK="${COVERAGE_FLOOR_SLACK:-2}" +mkdir -p "$OUT" + +mode=full +gaps_n=25 +case "${1:-}" in + "") ;; + --quick) mode=quick ;; + --report) mode=report ;; + --gaps) mode=gaps; gaps_n="${2:-25}" ;; + --update-floor) mode=update ;; + *) sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2 ;; +esac + +# Per-module table from lcov: src//** -> ; src/.rs -> (crate root). +modules_tsv() { + awk -F: ' + /^SF:/ { f=substr($0,4); gsub(/\\/,"/",f); i=index(f,"/src/"); r=(i?substr(f,i+5):f); sub(/^src\//,"",r) + n=index(r,"/"); m=(n?substr(r,1,n-1):"(crate root)") } + /^LF:/ { lf[m]+=$2 } + /^LH:/ { lh[m]+=$2 } + END { for (k in lf) { printf "%s\t%d\t%d\t%.1f\n", k, lh[k], lf[k], (lf[k]?100*lh[k]/lf[k]:0); tl+=lf[k]; th+=lh[k] } + printf "TOTAL\t%d\t%d\t%.1f\n", th, tl, (tl?100*th/tl:0) }' "$OUT/lcov.info" | sort +} + +# Largest runs of consecutive never-executed lines (a covered line ends a run). +gaps() { + awk -F'[:,]' ' + function flush() { if (n>0) printf "%d\t%s:%d-%d\n", n, r, s, e; n=0 } + /^SF:/ { flush(); f=substr($0,4); gsub(/\\/,"/",f); i=index(f,"/src/"); r=(i?substr(f,i+1):f) } + /^DA:/ { if ($3==0) { if (n==0) s=$2; e=$2; n++ } else flush() } + /^end_of_record/ { flush() }' "$OUT/lcov.info" | sort -rn | head -n "$1" | awk -F'\t' '{printf "%s\t%s\n", $2, $1}' +} + +report() { + [ -s "$OUT/lcov.info" ] || { echo "FAIL no coverage data ($OUT/lcov.info missing or empty)"; echo FAIL >"$OUT/STATUS"; exit 1; } + local drops=0 m cov tot pct fl st + : >"$OUT/modules.tsv" + while IFS=$'\t' read -r m cov tot pct; do + fl=$(awk -F'\t' -v m="$m" '$1==m{print $2}' "$FLOOR" 2>/dev/null | head -1) + if [ -z "$fl" ]; then st=NEW; fl=- + elif awk -v p="$pct" -v f="$fl" 'BEGIN{exit !(p+0 < f+0)}'; then st=DROP; drops=$((drops+1)) + else st=OK; fi + printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$m" "$cov" "$tot" "$pct" "$fl" "$st" >>"$OUT/modules.tsv" + done < <(modules_tsv) + gaps 25 >"$OUT/gaps.txt" + { + echo "## Line coverage per module"; echo + echo "| Module | Covered | Lines | % | Floor | |"; echo "|---|---:|---:|---:|---:|---|" + awk -F'\t' '{printf "| `%s` | %s | %s | %s | %s | %s |\n",$1,$2,$3,$4,$5,$6}' "$OUT/modules.tsv" + echo; echo "
25 largest uncovered regions"; echo; echo '```text' + cat "$OUT/gaps.txt"; echo '```'; echo "
" + [ -f "$OUT/test-failures.txt" ] && [ -s "$OUT/test-failures.txt" ] && { + echo; echo "**Tests failing under instrumentation (not part of the coverage verdict):**"; echo '```text' + head -40 "$OUT/test-failures.txt"; echo '```'; } + } >"$OUT/summary.md" + local total; total=$(awk -F'\t' '$1=="TOTAL"{print $4}' "$OUT/modules.tsv") + if [ "$mode" = quick ]; then + echo PASS >"$OUT/STATUS"; echo "PASS coverage ${total}% (unit tests only; floor not checked) -> $OUT/summary.md" + elif [ "$drops" -gt 0 ]; then + echo FAIL >"$OUT/STATUS" + echo "FAIL coverage ${total}%: $(awk -F'\t' '$6=="DROP"{printf "%s %s%%<%s%% ",$1,$4,$5}' "$OUT/modules.tsv")-> $OUT/summary.md" + exit 1 + else + echo PASS >"$OUT/STATUS"; echo "PASS coverage ${total}% (no module below its floor) -> $OUT/summary.md" + fi +} + +case $mode in + report) report; exit 0 ;; + gaps) [ -s "$OUT/lcov.info" ] || { echo "no $OUT/lcov.info (run scripts/coverage.sh first)" >&2; exit 2; } + gaps "$gaps_n"; exit 0 ;; + update) [ -s "$OUT/lcov.info" ] || { echo "no $OUT/lcov.info (run scripts/coverage.sh first)" >&2; exit 2; } + { echo "# Per-module line-coverage floor (percent). Checked by scripts/coverage.sh; a module below its floor fails CI." + echo "# Regenerate after raising coverage: scripts/coverage.sh && scripts/coverage.sh --update-floor" + echo "# (recorded as measured minus ${SLACK} points of run-to-run slack, floored). Never lower a value by hand." + modules_tsv | awk -F'\t' -v s="$SLACK" '{v=int($4-s); if (v<0) v=0; printf "%s\t%d\n",$1,v}' + } >"$FLOOR" + echo "PASS floor updated -> $FLOOR"; exit 0 ;; +esac + +cargo llvm-cov --version >/dev/null 2>&1 || { echo "FAIL cargo-llvm-cov not installed (cargo install cargo-llvm-cov --locked)"; exit 2; } + +# Same isolation as verify.sh: tests must never see the developer's real HOME / XDG state. +# Short root on purpose: macOS's per-user TMPDIR (/var/folders/.../T, ~50 bytes) pushes Unix socket +# paths past sun_path (104 bytes) and ssh ControlPath past its limit, and long temp paths get +# clipped in rendered frames. Tests therefore get a short TMPDIR and a private XDG_RUNTIME_DIR. +if [ -d /tmp ] && [ -w /tmp ]; then STATE="$(mktemp -d /tmp/btv.XXXXXX)"; else STATE="$(mktemp -d "${TMPDIR:-/tmp}/btv.XXXXXX")"; fi +trap 'rm -rf "$STATE"' EXIT +export CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}" RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}" +export XDG_STATE_HOME="$STATE/state" XDG_CONFIG_HOME="$STATE/config" XDG_DATA_HOME="$STATE/data" +export HOME="$STATE/home" USERPROFILE="$STATE/home" APPDATA="$STATE/appdata/roaming" LOCALAPPDATA="$STATE/appdata/local" +export TMPDIR="$STATE/t" XDG_RUNTIME_DIR="$STATE/r" +mkdir -p "$TMPDIR" "$XDG_RUNTIME_DIR" && chmod 700 "$XDG_RUNTIME_DIR" +mkdir -p "$XDG_STATE_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$HOME" "$APPDATA" "$LOCALAPPDATA" + +sel=() +[ "$mode" = quick ] && sel=(--lib --bins) +cargo llvm-cov clean --workspace >/dev/null 2>&1 || true +# --ignore-run-fail: a test that fails only under instrumentation must not hide the coverage numbers. +cargo llvm-cov --no-report --ignore-run-fail ${sel[@]+"${sel[@]}"} -- --test-threads="${COVERAGE_TEST_THREADS:-8}" >"$OUT/run.log" 2>&1 +run_rc=$? +grep -E '^test .* FAILED$|^error: test failed' "$OUT/run.log" >"$OUT/test-failures.txt" || true +cargo llvm-cov report --lcov --output-path "$OUT/lcov.info" >>"$OUT/run.log" 2>&1 \ + || { echo "FAIL coverage report failed (run rc=$run_rc) -> $OUT/run.log"; echo FAIL >"$OUT/STATUS"; exit 1; } +cargo llvm-cov report --html --output-dir "$OUT" >>"$OUT/run.log" 2>&1 || true +report diff --git a/scripts/verify.ps1 b/scripts/verify.ps1 new file mode 100755 index 00000000..8c4cfd4e --- /dev/null +++ b/scripts/verify.ps1 @@ -0,0 +1,53 @@ +#!/usr/bin/env pwsh +# Windows entry point for the verification gate. Same contract as scripts/verify.sh on every OS: +# ONE output line (PASS n/n s | FAIL k/n: -> target/verify//fail.txt), +# exit code 0 pass / 1 fail / 2 usage, details under target/verify/. +# +# pwsh scripts/verify.ps1 full gate +# pwsh scripts/verify.ps1 --quick clippy + unit tests +# pwsh scripts/verify.ps1 --only t:smoke_binary_per_os +# pwsh scripts/verify.ps1 --coverage (delegates to scripts/coverage.sh) +# +# It is deliberately thin: the stage logic lives once, in verify.sh, and runs under the bash that +# ships with Git for Windows (no WSL, no MSYS2 install needed). On Linux/macOS it simply runs bash. +$ErrorActionPreference = 'Stop' +$repo = Split-Path -Parent $PSScriptRoot + +function Find-Bash { + # Prefer Git for Windows: `bash` on PATH may be the WSL launcher (C:\Windows\System32\bash.exe), + # which runs in a different OS and would test the Linux build instead of the Windows one. + $candidates = @() + $git = Get-Command git -ErrorAction SilentlyContinue + if ($git) { + $gitRoot = Split-Path -Parent (Split-Path -Parent $git.Source) + $candidates += (Join-Path $gitRoot 'bin/bash.exe'), (Join-Path $gitRoot 'usr/bin/bash.exe') + } + foreach ($base in @($env:ProgramFiles, ${env:ProgramFiles(x86)}, $env:LOCALAPPDATA)) { + if ($base) { $candidates += (Join-Path $base 'Git/bin/bash.exe'), (Join-Path $base 'Programs/Git/bin/bash.exe') } + } + foreach ($c in $candidates) { if ($c -and (Test-Path $c)) { return $c } } + $onPath = Get-Command bash -ErrorAction SilentlyContinue + if ($onPath -and ($onPath.Source -notmatch '(?i)\\Windows\\System32\\')) { return $onPath.Source } + return $null +} + +$bash = Find-Bash +if (-not $bash) { + Write-Output 'FAIL bash not found: install Git for Windows (https://git-scm.com/download/win) to run the gate' + exit 2 +} + +$script = 'scripts/verify.sh' +$rest = @($args) +if ($rest.Count -gt 0 -and $rest[0] -eq '--coverage') { + $script = 'scripts/coverage.sh' + $rest = @($rest | Select-Object -Skip 1) +} + +Push-Location $repo +try { + & $bash $script @rest + exit $LASTEXITCODE +} finally { + Pop-Location +} diff --git a/scripts/verify.sh b/scripts/verify.sh index 0db0c05b..d5b4ee20 100755 --- a/scripts/verify.sh +++ b/scripts/verify.sh @@ -3,6 +3,8 @@ # scripts/verify.sh full: clippy, unit, every tests/*.rs (fmt is opt-in: --only fmt; repo has legacy drift) # scripts/verify.sh --quick clippy, unit # scripts/verify.sh --failed re-run only stages that failed last run +# scripts/verify.sh --integration every tests/*.rs, without clippy/unit (CI runs those in the quick job) +# scripts/verify.sh --shard K/N with full/--integration: only every N-th tests/*.rs stage, slice K (1-based); CI fan-out # scripts/verify.sh --tier smoke|unit|integration|system|perf|stress|torture|security # scripts/verify.sh --only a b named stages or hierarchical groups g: (see --list / --groups) # scripts/verify.sh --match pass pattern to cargo test stages @@ -18,7 +20,8 @@ # report.md comprehensive Markdown verification report # junit.xml standard JUnit XML test report # coverage.txt cargo llvm-cov summary (when --coverage requested) -# /status PASS|FAIL /fail.txt compact failure excerpt (<=40 lines) +# skips.txt every test that did not really run: `#[ignore]`d tests per stage + runtime `SKIPPED` lines +# /status PASS|FAIL /fail.txt compact failure excerpt (<=60 lines, with panic messages) # /log full output (read only if fail.txt is not enough) # Exit code: 0 all pass, 1 any fail, 2 usage. set -uo pipefail @@ -42,7 +45,10 @@ export CARGO_BUILD_JOBS="$(detect_cpus)" # Cross-platform environment and temp directory isolation: # Covers Linux/POSIX (XDG_*), macOS ($HOME, XDG_*), and Windows/MSYS ($USERPROFILE, $APPDATA, $LOCALAPPDATA). -VERIFY_STATE="$(mktemp -d "${TMPDIR:-/tmp}/bilterm-verify-state.XXXXXX")" +# Short root on purpose: macOS's per-user TMPDIR (/var/folders/.../T, ~50 bytes) pushes Unix socket +# paths past sun_path (104 bytes) and ssh ControlPath past its limit, and long temp paths get +# clipped in rendered frames. Tests therefore get a short TMPDIR and a private XDG_RUNTIME_DIR. +if [ -d /tmp ] && [ -w /tmp ]; then VERIFY_STATE="$(mktemp -d /tmp/btv.XXXXXX)"; else VERIFY_STATE="$(mktemp -d "${TMPDIR:-/tmp}/btv.XXXXXX")"; fi trap 'rm -rf "$VERIFY_STATE"' EXIT export CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}" export RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}" @@ -53,9 +59,16 @@ export HOME="$VERIFY_STATE/home" export USERPROFILE="$VERIFY_STATE/home" export APPDATA="$VERIFY_STATE/appdata/roaming" export LOCALAPPDATA="$VERIFY_STATE/appdata/local" +export TMPDIR="$VERIFY_STATE/t" XDG_RUNTIME_DIR="$VERIFY_STATE/r" +mkdir -p "$TMPDIR" "$XDG_RUNTIME_DIR" && chmod 700 "$XDG_RUNTIME_DIR" mkdir -p "$XDG_STATE_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$HOME" "$APPDATA" "$LOCALAPPDATA" +# Git Bash / MSYS exports SHELL=/usr/bin/bash, a path native Windows programs cannot start. A real +# Windows launch has no SHELL at all (the product then falls back to COMSPEC), so tests see the same. +case "$(uname -s 2>/dev/null)" in MINGW*|MSYS*|CYGWIN*) unset SHELL ;; esac + MATCH_PATTERN="" +SHARD="" DO_COVERAGE=false USE_XVFB=false tier_name="" @@ -65,6 +78,13 @@ all_stages() { local f; for f in tests/*.rs; do echo "t:$(basename "$f" .rs)"; done } quick_stages() { echo clippy; echo unit; } +integration_stages() { local f; for f in tests/*.rs; do echo "t:$(basename "$f" .rs)"; done; } +# Keeps clippy/unit, and of the t:* stages only slice K of N (round-robin over the sorted list, so +# slices stay balanced as test files are added). +shard_filter() { + local k="${SHARD%%/*}" n="${SHARD##*/}" + awk -v k="$k" -v n="$n" '/^t:/ { if (i++ % n == k - 1) print; next } { print }' +} tier_stages() { case "$1" in @@ -297,6 +317,18 @@ while [[ $# -gt 0 ]]; do mode=failed shift ;; + --integration) + mode=integration + shift + ;; + --shard) + if [[ $# -lt 2 ]] || ! [[ "$2" =~ ^[1-9][0-9]*/[1-9][0-9]*$ ]] || [ "${2%%/*}" -gt "${2##*/}" ]; then + echo "--shard needs K/N with 1 <= K <= N (e.g. --shard 2/3)" >&2 + exit 2 + fi + SHARD="$2" + shift 2 + ;; --list) all_stages exit 0 @@ -358,6 +390,8 @@ while [[ $# -gt 0 ]]; do echo " scripts/verify.sh full: clippy, unit, every tests/*.rs" >&2 echo " scripts/verify.sh --quick clippy, unit" >&2 echo " scripts/verify.sh --failed re-run only stages that failed last run" >&2 + echo " scripts/verify.sh --integration every tests/*.rs without clippy/unit" >&2 + echo " scripts/verify.sh --shard K/N with full/--integration: slice K of N of the tests/*.rs stages" >&2 echo " scripts/verify.sh --tier smoke|unit|integration|system|perf|stress|torture|security" >&2 echo " scripts/verify.sh --only a b named stages or groups g: (--list / --groups)" >&2 echo " scripts/verify.sh --match pass pattern to cargo test stages" >&2 @@ -376,7 +410,9 @@ done read_stages() { stages=(); local l; while IFS= read -r l; do [ -n "$l" ] && stages+=("$l"); done; } case $mode in - full) read_stages < <(all_stages) ;; + full) if [ -n "$SHARD" ]; then read_stages < <(all_stages | shard_filter); else read_stages < <(all_stages); fi ;; + integration) + if [ -n "$SHARD" ]; then read_stages < <(integration_stages | shard_filter); else read_stages < <(integration_stages); fi ;; quick) read_stages < <(quick_stages) ;; failed) read_stages < <(awk -F'\t' '$2=="FAIL"{print $1}' "$OUT/summary.tsv" 2>/dev/null) [ ${#stages[@]} -eq 0 ] && { echo "PASS nothing-failed"; exit 0; } ;; @@ -405,6 +441,22 @@ case "$OS_NAME" in ;; esac +# Pre-build every selected test binary in ONE cargo invocation: cargo then compiles them in parallel +# across all cores, instead of one binary per stage. A build error here is not fatal; the stage +# that owns the broken target reports it (with its own log) when it runs below. +if [ ${#stages[@]} -ge 4 ]; then + prebuild=() + for s in "${stages[@]}"; do + case "$s" in + unit) prebuild+=(--lib --bins) ;; + t:*) prebuild+=(--test "${s#t:}") ;; + esac + done + if [ ${#prebuild[@]} -gt 0 ]; then + cargo test -q --no-run "${prebuild[@]}" >"$OUT/prebuild.log" 2>&1 || true + fi +fi + new=$(mktemp) for s in ${stages[@]+"${stages[@]}"}; do c=$(cmd_for "$s") || { echo "unknown stage: $s (try --list)"; exit 2; } @@ -417,14 +469,19 @@ for s in ${stages[@]+"${stages[@]}"}; do fi echo "$st" >"$d/status" if [ "$st" = FAIL ]; then - { grep -E '^(error|warning)(\[|:)|^---- |panicked at|^test .* FAILED|^failures:|^ [A-Za-z0-9_:]+$|^Diff in|^ *--> ' "$d/log" || tail -n 20 "$d/log"; } \ - | awk '!seen[$0]++' | head -40 >"$d/fail.txt" + # Keep the panic MESSAGE (the lines after `panicked at`): without it a CI-only failure cannot + # be diagnosed from fail.txt. Long lines are clipped so one huge assertion cannot flood it. + { awk '/panicked at/ { print; keep = 4; hit = 1; next } + keep > 0 { keep--; if ($0 !~ /^note: run with/ && $0 != "") print; next } + /^(error|warning)(\[|:)|^---- |^test .* FAILED|^failures:|^ [A-Za-z0-9_:]+$|^Diff in|^ *--> / { print; hit = 1 } + END { exit !hit }' "$d/log" || tail -n 20 "$d/log"; } \ + | cut -c1-600 | awk '!seen[$0]++' | head -60 >"$d/fail.txt" fi printf '%s\t%s\t%s\n' "$s" "$st" $((SECONDS-t0)) >>"$new" done # merge: partial runs replace only the stages they ran -if [ "$mode" = full ] || [ ! -f "$OUT/summary.tsv" ]; then +if { [ "$mode" = full ] && [ -z "$SHARD" ]; } || [ ! -f "$OUT/summary.tsv" ]; then sort -u -k1,1 "$new" >"$OUT/summary.tsv" elif [ "$mode" = quick ]; then grep -vE '^(clippy|unit)'$'\t' "$OUT/summary.tsv" >>"$new" 2>/dev/null || true @@ -441,6 +498,17 @@ if [ "$DO_COVERAGE" = true ]; then fi fi +# Nothing may be skipped silently: list `#[ignore]`d tests (with the stage they belong to) and +# every runtime `SKIPPED : ` line (tests/common `skip()`) from this run's logs. +{ + for d in "$OUT"/t_* "$OUT"/unit; do + [ -f "$d/log" ] || continue + ign=$(awk '/^test result:/ { for (i = 1; i < NF; i++) if ($(i + 1) ~ /^ignored/) n += $i } END { print n + 0 }' "$d/log") + [ "$ign" -gt 0 ] && printf 'IGNORED\t%s\t%s test(s) marked #[ignore] (reasons: cargo test --test %s -- --ignored --list)\n' "$(basename "$d")" "$ign" "$(basename "$d" | sed 's/^t_//')" + grep -ho 'SKIPPED .*' "$d/log" 2>/dev/null | sed "s|^|$(basename "$d")\t|" + done +} >"$OUT/skips.txt" 2>/dev/null || true + r=$(line) st_overall="${r%% *}" echo "$st_overall" >"$OUT/STATUS" diff --git a/src/api/instance_registry.rs b/src/api/instance_registry.rs index 9b7d70f6..2a694328 100644 --- a/src/api/instance_registry.rs +++ b/src/api/instance_registry.rs @@ -85,6 +85,7 @@ pub fn resolve_client_socket(runtime: &Path, env_socket: Option<&str>) -> Option /// Owns this process's registered instance socket path; cleans up on drop. #[derive(Debug)] pub struct InstanceGuard { + #[cfg_attr(not(unix), allow(dead_code))] // only the Unix default-link publisher reads it runtime: PathBuf, socket: PathBuf, } diff --git a/src/daemon/liveness.rs b/src/daemon/liveness.rs index e8bf8ec8..840c5570 100644 --- a/src/daemon/liveness.rs +++ b/src/daemon/liveness.rs @@ -4,6 +4,9 @@ //! a zombie or by a PID that was reused by an unrelated process. The lockfile is never unlinked: //! a clean exit truncates the record, so a record nobody holds means the daemon crashed. +// The flock probe is Unix-only; elsewhere the helpers and their imports are unused. +#![cfg_attr(not(unix), allow(unused_imports, dead_code))] + use std::fs::File; use std::io::{self, Read}; use std::path::Path; diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index ff93b537..da434c31 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -247,7 +247,9 @@ impl DaemonServerState { if matches!( e.kind(), ErrorKind::ConnectionRefused | ErrorKind::NotFound - ) => + ) + // macOS reports a leftover non-socket file at the path as ENOTSOCK, Linux as ECONNREFUSED. + || e.raw_os_error() == Some(libc::ENOTSOCK) => { match fs::remove_file(sock_path) { Ok(()) => Ok(false), diff --git a/src/daemon/process.rs b/src/daemon/process.rs index adf5c5aa..d96a5865 100644 --- a/src/daemon/process.rs +++ b/src/daemon/process.rs @@ -59,7 +59,9 @@ pub fn probe_or_cleanup_stale_socket(sock_path: &Path) -> bool { if matches!( e.kind(), ErrorKind::ConnectionRefused | ErrorKind::NotFound - ) => + ) + // macOS reports a leftover non-socket file at the path as ENOTSOCK, Linux as ECONNREFUSED. + || e.raw_os_error() == Some(libc::ENOTSOCK) => { let _ = fs::remove_file(sock_path); false diff --git a/src/daemon/supervisor.rs b/src/daemon/supervisor.rs index 1ceadd86..8603de61 100644 --- a/src/daemon/supervisor.rs +++ b/src/daemon/supervisor.rs @@ -5,6 +5,7 @@ use std::collections::BTreeMap; use std::fs::File; +#[cfg_attr(not(unix), allow(unused_imports))] // only the Unix paths use it use std::io::{self, ErrorKind, Read, Write}; #[cfg(unix)] use std::io::{Seek, SeekFrom}; @@ -596,6 +597,7 @@ pub fn stop_daemon(runtime_dir: &Path, name: &str, force: bool) -> io::Result String { diff --git a/src/pane/font_fetch.rs b/src/pane/font_fetch.rs index d9b836e2..9af7f021 100644 --- a/src/pane/font_fetch.rs +++ b/src/pane/font_fetch.rs @@ -126,7 +126,7 @@ pub fn install_google_font(raw: &str) -> Result { }); let dest_dir = default_custom_fonts_dir().ok_or_else(|| "Cannot determine fonts directory".to_string())?; - crate::secure_fs::ensure_private_dir(&dest_dir).map_err(|e| format!("Cannot create fonts directory: {e}"))?; + ensure_fonts_dir(&dest_dir).map_err(|e| format!("Cannot create fonts directory: {e}"))?; let file_stem = family.replace(' ', "_"); let dest_path = dest_dir.join(format!("{file_stem}.ttf")); @@ -157,7 +157,7 @@ pub fn install_font_source(raw: &str) -> Result { } if raw.starts_with("http://") || raw.starts_with("https://") { let dest_dir = default_custom_fonts_dir().ok_or_else(|| "Cannot determine fonts directory".to_string())?; - crate::secure_fs::ensure_private_dir(&dest_dir).map_err(|e| format!("Cannot create fonts directory: {e}"))?; + ensure_fonts_dir(&dest_dir).map_err(|e| format!("Cannot create fonts directory: {e}"))?; if raw.split('?').next().unwrap_or(raw).ends_with(".zip") { let temp_zip = dest_dir.join(format!(".download-{}.zip", std::process::id())); @@ -197,13 +197,22 @@ pub fn install_font_source(raw: &str) -> Result { let path = expand_tilde(raw); if path.extension().and_then(|e| e.to_str()).is_some_and(|ext| ext.eq_ignore_ascii_case("zip")) { let dest_dir = default_custom_fonts_dir().ok_or_else(|| "Cannot determine fonts directory".to_string())?; - crate::secure_fs::ensure_private_dir(&dest_dir).map_err(|e| format!("Cannot create fonts directory: {e}"))?; + ensure_fonts_dir(&dest_dir).map_err(|e| format!("Cannot create fonts directory: {e}"))?; return install_zip_archive(&path, &dest_dir); } register_custom_font_file(&path) } +/// Creates the fonts directory on a fresh profile (its `bilterm/` parent may not exist yet), then +/// applies the usual private-directory checks (owner, 0700, not a symlink) to the directory itself. +fn ensure_fonts_dir(dir: &Path) -> std::io::Result<()> { + if let Some(parent) = dir.parent() { + crate::secure_fs::create_private_dir_all(parent)?; + } + crate::secure_fs::ensure_private_dir(dir) +} + /// Unzips a font archive into a private staging directory, moves valid font files to `dest_dir`, /// registers them, and returns the primary font family name. fn install_zip_archive(zip_path: &Path, dest_dir: &Path) -> Result { diff --git a/src/wm/journal.rs b/src/wm/journal.rs index 043c8053..f448b0a1 100644 --- a/src/wm/journal.rs +++ b/src/wm/journal.rs @@ -377,6 +377,7 @@ impl super::WindowManager { #[cfg(test)] mod tests { use super::*; + #[cfg_attr(not(unix), allow(unused_imports))] // only the Unix paths use it use std::fs; #[cfg(unix)] use std::os::unix::fs::PermissionsExt; diff --git a/tests/clipboard_command_grammar.rs b/tests/clipboard_command_grammar.rs new file mode 100644 index 00000000..6f37b320 --- /dev/null +++ b/tests/clipboard_command_grammar.rs @@ -0,0 +1,117 @@ +//! `:clipboard` grammar end to end on a headless workspace: every verb and alias parses, executes +//! through `WindowManager::apply`, and the workspace still renders afterwards; malformed arguments +//! are answered with the usage text instead of being accepted or panicking. +//! +//! (`persist` / `save` are left to the persistence suites: they write the clipboard store to disk.) + +use ratatui::buffer::Buffer; +use ratatui::layout::Rect; +use ratatui::widgets::Widget; + +use bilterm::colon_command::parse_colon_command; +use bilterm::ui::WorkspaceWidget; +use bilterm::wm::WindowManager; + +const HEADS: &[&str] = &[":clipboard", ":clips", ":clip", ":cb", ":buffers", ":yank-history", ":yank-ring"]; + +/// Every well-formed argument list of the grammar, in an order that exercises real state changes +/// (entries exist before they are selected, pinned, pasted and deleted). +const VERBS: &[&str] = &[ + "open", "copy first clip", "add second clip with spaces", "push third", "copy https://example.com/a?b=c", "show", "list", "ls", + "all", "session", "sess", "session cycle", "sess next", "session active", "session current", "session reset", "session work", + "window", "win", "pane", "window cycle", "win next", "window active", "pane reset", "window 1", "window #1", "window 999", + "pinned", "pins", "media", "images", "video", "tab next", "tab prev", "all", + "filter clip", "search second clip", "find https", "filter", "search clear", "find off", "filter reset", + "first", "home", "down", "up", "prev", "step 2", "step -5", "step 0", "last", "end", "page-down", "page-up", + "select 1", "select 2", "select 0", "select 999", "scroll 3", "scroll -3", "preview-scroll 2", "preview-scroll -9", + "pin-entry", "pin-entry 1", "pin-entry #2 on", "pin-entry 2 off", "pin-entry 2 toggle", "pin-entry 1 pin", "pin-entry 1 unpin", "pin-entry 424242", + "yank", "pop", "cycle", "next", "paste", "paste 1", "paste #2", "paste 999", + "move 3 2", "move -50 -50", "move 500 500", "resize 4 3", "resize -100 -100", "resize 300 300", "rect 2 1 60 18", "rect 0 0 1 1", + "rect 65535 65535 65535 65535", "size auto", "size reset", "scale +", "scale up", "scale in", "scale -", "scale down", "scale out", "scale 25", + "scale -40%", "scale 0", "pin", "pin on", "pin true", "pin 1", "unpin", "pin off", "pin false", "pin 0", "pin toggle", + "dock", "dock on", "dock true", "dock 1", "float", "undock", "dock off", "dock false", "dock 0", "dock toggle", "blur", "toggle", "toggle", + "delete", "del", "rm", "delete 1", "del #2", "rm 424242", "clear", "reset", "dismiss", "close", "hide", "", +]; + +/// Argument lists the grammar must refuse. +const MALFORMED: &[&str] = &[ + "bogus", "open extra", "pin maybe", "dock sideways", "window abc", "window -1", "paste abc", "paste -1", "pin-entry abc", + "pin-entry 1 maybe", "pin-entry 1 on extra", "delete abc", "delete 1 2", "step", "step x", "step 1 2", "select", "select x", + "select -1", "scroll", "scroll x", "preview-scroll x", "move 1", "move a b", "move 1 2 3", "move 40000 0", "resize 1", "resize a b", + "rect 1 2 3", "rect a b c d", "rect -1 0 1 1", "rect 70000 0 1 1", "size", "size huge", "scale", "scale x", "scale 99999", "tab", + "tab sideways", "copy", "add", "push", "session a b", "persist maybe", +]; + +fn render(wm: &WindowManager) -> Buffer { + let area = Rect::new(0, 0, 100, 30); + let mut buf = Buffer::empty(area); + WorkspaceWidget::new(wm).render(area, &mut buf); + buf +} + +#[test] +fn every_verb_and_alias_parses_executes_and_renders() { + let mut wm = WindowManager::new_headless(100, 30); + let mut executed = 0; + for (i, verb) in VERBS.iter().enumerate() { + let head = HEADS[i % HEADS.len()]; + let line = format!("{head} {verb}"); + let action = match parse_colon_command(line.trim_end()) { + Some(Ok(a)) => a, + other => panic!("{line:?} must parse, got {other:?}"), + }; + wm.apply(action).unwrap_or_else(|e| panic!("{line:?} failed to execute: {e}")); + executed += 1; + let buf = render(&wm); + assert_eq!(buf.area, Rect::new(0, 0, 100, 30), "{line:?} corrupted the frame"); + } + assert_eq!(executed, VERBS.len()); + // The bare head is a toggle: closed -> open -> closed. + wm.apply(parse_colon_command(":clipboard close").unwrap().unwrap()).unwrap(); + assert!(!wm.clipboard_window().active); + wm.apply(parse_colon_command(":clipboard").unwrap().unwrap()).unwrap(); + assert!(wm.clipboard_window().active); + wm.apply(parse_colon_command(":clips toggle").unwrap().unwrap()).unwrap(); + assert!(!wm.clipboard_window().active); +} + +#[test] +fn copied_text_is_recorded_listed_and_removable() { + let mut wm = WindowManager::new_headless(100, 30); + let run = |wm: &mut WindowManager, line: &str| { + wm.apply(parse_colon_command(line).unwrap().unwrap()).unwrap(); + }; + assert!(wm.clipboard_registry().entries().is_empty()); + run(&mut wm, ":clipboard copy alpha one"); + run(&mut wm, ":clip add beta two"); + let texts: Vec = wm.clipboard_registry().entries().iter().map(|e| e.content.clone()).collect(); + assert!(texts.iter().any(|t| t == "alpha one") && texts.iter().any(|t| t == "beta two"), "{texts:?}"); + + run(&mut wm, ":clipboard open"); + assert!(wm.clipboard_window().active); + let frame = render(&wm); + let shown: String = frame.content.iter().map(|c| c.symbol()).collect(); + assert!(shown.contains("alpha one") || shown.contains("beta two"), "the open window lists the entries"); + + // Pinned entries survive `clear`; unpinned ones do not. + let keep = wm.clipboard_registry().entries().iter().find(|e| e.content == "alpha one").unwrap().id; + run(&mut wm, &format!(":clipboard pin-entry {keep} on")); + run(&mut wm, ":clipboard clear"); + let left: Vec = wm.clipboard_registry().entries().iter().map(|e| e.content.clone()).collect(); + assert_eq!(left, vec!["alpha one".to_string()], "clear removes only unpinned entries"); + run(&mut wm, &format!(":clipboard delete {keep}")); + assert!(wm.clipboard_registry().entries().is_empty()); +} + +#[test] +fn malformed_arguments_get_the_usage_text() { + for bad in MALFORMED { + for head in [":clipboard", ":cb"] { + let line = format!("{head} {bad}"); + match parse_colon_command(&line) { + Some(Err(msg)) => assert!(msg.contains("Usage: :clipboard"), "{line:?}: {msg}"), + other => panic!("{line:?} must be rejected with usage, got {other:?}"), + } + } + } +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 00000000..b8a51174 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,245 @@ +//! Shared, platform-neutral test support (Linux / macOS / Windows, x86_64 / ARM64). +//! +//! Include from an integration test with `mod common;`. Everything here avoids hard-coded +//! `/bin/sh`, `/tmp`, `sleep` and `:`-separated paths so the same test body runs on every CI cell. +#![allow(dead_code)] + +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Output, Stdio}; +use std::time::{Duration, Instant}; + +/// Reports a runtime skip so it is visible in the test log even when the test "passes". +/// +/// Writes straight to the stderr handle (the libtest capture only intercepts `print!`/`eprintln!`), +/// so `SKIPPED : ` always reaches the log; `scripts/verify.sh` collects these lines +/// into `target/verify/skips.txt`. Prefer `#[cfg_attr(not(unix), ignore = "reason")]` when the skip +/// is known at compile time: libtest then prints `ignored, reason` itself. +pub fn skip(reason: &str) { + let name = std::thread::current().name().unwrap_or("").to_string(); + // Leading newline: libtest's progress dots share the line, and the collector matches line starts. + let _ = writeln!(std::io::stderr(), "\nSKIPPED {name}: {reason}"); +} + +/// Polls `cond` every 10 ms until it holds or `timeout` elapses. Returns whether it held. +/// Use instead of a fixed `sleep`: fast machines finish early, slow ones get the full deadline. +pub fn wait_until(timeout: Duration, mut cond: impl FnMut() -> bool) -> bool { + let deadline = Instant::now() + timeout; + loop { + if cond() { + return true; + } + if Instant::now() >= deadline { + return false; + } + std::thread::sleep(Duration::from_millis(10)); + } +} + +/// Generous deadline for anything that waits on another process (CI ARM / Windows runners are slow). +pub const PROCESS_DEADLINE: Duration = Duration::from_secs(30); + +/// `(program, args)` running `script` in the platform's stock non-interactive shell: +/// `/bin/sh -c` on Unix, `cmd.exe /C` on Windows. +pub fn shell_command(script: &str) -> (String, Vec) { + if cfg!(windows) { + let comspec = std::env::var("COMSPEC").ok().filter(|s| !s.trim().is_empty()); + (comspec.unwrap_or_else(|| "cmd.exe".to_string()), vec!["/C".to_string(), script.to_string()]) + } else { + ("/bin/sh".to_string(), vec!["-c".to_string(), script.to_string()]) + } +} + +/// A shell line that prints `marker` and exits; valid for both `sh` and `cmd.exe`. +pub fn echo_script(marker: &str) -> String { + format!("echo {marker}") +} + +/// Whether `program` can be started at all (used for optional tools such as `git`). +pub fn has_program(program: &str) -> bool { + Command::new(program).arg("--version").stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).status().is_ok() +} + +/// A loopback TCP port that was free a moment ago. +pub fn free_loopback_port() -> u16 { + let l = std::net::TcpListener::bind(("127.0.0.1", 0)).expect("bind loopback port 0"); + l.local_addr().expect("local addr").port() +} + +/// Deterministic xorshift64* PRNG: fixed seeds give identical streams on every platform, no deps. +#[derive(Clone)] +pub struct Rng(u64); + +impl Rng { + pub fn new(seed: u64) -> Self { + // SplitMix64 scramble so small seeds (0, 1, 2 ...) still start far apart. + let mut z = seed.wrapping_add(0x9E37_79B9_7F4A_7C15); + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + Self((z ^ (z >> 31)) | 1) + } + + pub fn next_u64(&mut self) -> u64 { + self.0 ^= self.0 >> 12; + self.0 ^= self.0 << 25; + self.0 ^= self.0 >> 27; + self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) + } + + /// Uniform-ish value in `0..n` (`n > 0`). + pub fn below(&mut self, n: usize) -> usize { + (self.next_u64() % n as u64) as usize + } + + pub fn byte(&mut self) -> u8 { + (self.next_u64() >> 32) as u8 + } + + pub fn chance(&mut self, one_in: usize) -> bool { + self.below(one_in) == 0 + } + + pub fn pick<'a, T>(&mut self, items: &'a [T]) -> &'a T { + &items[self.below(items.len())] + } + + pub fn bytes(&mut self, len: usize) -> Vec { + (0..len).map(|_| self.byte()).collect() + } +} + +/// Throwaway HOME / XDG / runtime tree so a spawned `bilterm` never reads or writes the developer's +/// profile, sockets, daemons or sessions (and never talks to a running instance). +pub struct IsolatedEnv { + root: tempfile::TempDir, +} + +impl IsolatedEnv { + pub fn new() -> Self { + let root = tempfile::tempdir().expect("create isolated env root"); + for d in ["home", "config", "state", "data", "runtime", "appdata/roaming", "appdata/local", "tmp"] { + let dir = root.path().join(d); + std::fs::create_dir_all(&dir).expect("create isolated env dir"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let _ = std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)); + } + } + Self { root } + } + + pub fn path(&self) -> &Path { + self.root.path() + } + + pub fn dir(&self, name: &str) -> PathBuf { + self.root.path().join(name) + } + + /// Points `cmd` at the isolated tree and strips every variable that could reach a live instance. + pub fn apply<'a>(&self, cmd: &'a mut Command) -> &'a mut Command { + let p = |n: &str| self.root.path().join(n); + cmd.env("HOME", p("home")) + .env("USERPROFILE", p("home")) + .env("APPDATA", p("appdata/roaming")) + .env("LOCALAPPDATA", p("appdata/local")) + .env("XDG_CONFIG_HOME", p("config")) + .env("XDG_STATE_HOME", p("state")) + .env("XDG_DATA_HOME", p("data")) + .env("XDG_RUNTIME_DIR", p("runtime")) + .env("BILTERM_RUNTIME_DIR", p("runtime")) + .env("TMPDIR", p("tmp")) + .env("TEMP", p("tmp")) + .env("TMP", p("tmp")) + // Never let a non-TTY launch pop a terminal emulator window on the host desktop. + .env("BILTERM_RELAUNCHED", "1") + .env_remove("BILTERM_SOCKET") + .env_remove("BILTERM_INSTANCE_PID") + .env_remove("BILTERM_WORKSPACE_ID") + .env_remove("BILTERM_PANE_ID") + .env_remove("BILTERM_TOKEN_FILE") + .env_remove("BILTERM_GUI") + .env_remove("BILTERM_TERMINAL") + .env_remove("TERMINAL") + .env_remove("TMUX") + } + + /// `bilterm` with `args`, isolated, stdin closed. + pub fn bilterm(&self, args: &[&str]) -> Command { + let mut cmd = Command::new(env!("CARGO_BIN_EXE_bilterm")); + self.apply(&mut cmd).args(args).stdin(Stdio::null()); + cmd + } + + /// Runs `bilterm args` to completion (killed and failed after [`PROCESS_DEADLINE`]). + pub fn run(&self, args: &[&str]) -> Output { + run_with_deadline(self.bilterm(args), PROCESS_DEADLINE) + } + + /// Writes a bearer-token file with owner-only permissions and returns its path. + pub fn token_file(&self, token: &str) -> PathBuf { + let path = self.root.path().join("rpc.token"); + std::fs::write(&path, format!("{token}\n")).expect("write token file"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).expect("chmod token file"); + } + path + } +} + +/// Kills the child on drop so a failing assertion never leaks a process. +pub struct KillOnDrop(pub Child); + +impl Drop for KillOnDrop { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +/// Runs `cmd` capturing stdout/stderr; panics (after killing it) if it outlives `deadline`. +pub fn run_with_deadline(mut cmd: Command, deadline: Duration) -> Output { + let mut child = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().expect("spawn child process"); + let mut out = child.stdout.take().expect("child stdout"); + let mut err = child.stderr.take().expect("child stderr"); + let out_t = std::thread::spawn(move || { + let mut b = Vec::new(); + let _ = out.read_to_end(&mut b); + b + }); + let err_t = std::thread::spawn(move || { + let mut b = Vec::new(); + let _ = err.read_to_end(&mut b); + b + }); + let mut guard = KillOnDrop(child); + let mut status = None; + let finished = wait_until(deadline, || { + status = guard.0.try_wait().expect("poll child"); + status.is_some() + }); + if !finished { + let _ = guard.0.kill(); + } + let status = status.unwrap_or_else(|| guard.0.wait().expect("reap child")); + let stdout = out_t.join().unwrap_or_default(); + let stderr = err_t.join().unwrap_or_default(); + assert!( + finished, + "process outlived its {deadline:?} deadline; stdout: {} stderr: {}", + String::from_utf8_lossy(&stdout), + String::from_utf8_lossy(&stderr) + ); + Output { status, stdout, stderr } +} + +pub fn stdout_of(o: &Output) -> String { + String::from_utf8_lossy(&o.stdout).into_owned() +} + +pub fn stderr_of(o: &Output) -> String { + String::from_utf8_lossy(&o.stderr).into_owned() +} diff --git a/tests/composite_pane_snapshots.rs b/tests/composite_pane_snapshots.rs index ac528062..c078093b 100644 --- a/tests/composite_pane_snapshots.rs +++ b/tests/composite_pane_snapshots.rs @@ -86,6 +86,7 @@ fn snapshot_password_no_echo_lock_hides_prompt_and_protects_input() { } #[test] +#[cfg_attr(windows, ignore = "drives a POSIX /bin/sh child; the Windows PTY path is covered by tests/smoke_binary_per_os.rs")] fn moving_and_hiding_prompt_on_live_pty_pane_emits_zero_sigwinch() { let mut cfg = PtyConfig::default(); if !cfg!(windows) { diff --git a/tests/config_keys_and_cli_args.rs b/tests/config_keys_and_cli_args.rs new file mode 100644 index 00000000..a6c7ffd4 --- /dev/null +++ b/tests/config_keys_and_cli_args.rs @@ -0,0 +1,275 @@ +//! Config loading / dotted keys and CLI argument parsing: the error paths and round trips that +//! the feature suites do not pin (unknown keys, type errors, atomic failure, unreadable files, +//! option syntax, endpoint resolution). Pure and platform-neutral: runs on every CI cell. + +mod common; + +use std::path::Path; + +use bilterm::api::JsonValue; +use bilterm::cli::{self, CliEnv}; +use bilterm::config::BiltermConfig; +use bilterm::config::dotted::{get_key, list_keys, save_changes, set_key}; + +// ---------------------------------------------------------------- dotted config keys + +#[test] +fn every_listed_key_reads_back_and_re_sets_to_itself() { + let cfg = BiltermConfig::default(); + let keys = list_keys(&cfg); + assert!(keys.len() > 30, "the default config should expose many leaf keys (got {})", keys.len()); + assert!(keys.windows(2).all(|w| w[0].0 < w[1].0), "list_keys is sorted and free of duplicates"); + let mut failures = Vec::new(); + for (key, text) in &keys { + match get_key(&cfg, key) { + Ok(got) if &got == text => {} + other => failures.push(format!("get {key}: listed {text:?}, got {other:?}")), + } + let mut copy = cfg.clone(); + match set_key(&mut copy, key, text) { + Ok(()) if copy == cfg => {} + Ok(()) => failures.push(format!("set {key} = {text} changed the config")), + Err(e) => failures.push(format!("set {key} = {text} failed: {e}")), + } + } + assert!(failures.is_empty(), "{} key(s) do not round trip:\n{}", failures.len(), failures.join("\n")); +} + +#[test] +fn malformed_and_unknown_keys_are_rejected_and_leave_the_config_untouched() { + let original = BiltermConfig::default(); + let mut cfg = original.clone(); + for key in ["", ".", "..", ".theme", "theme.", "a..b", "theme..x"] { + let e = get_key(&cfg, key).unwrap_err(); + assert!(e.contains("invalid config key"), "get {key:?}: {e}"); + let e = set_key(&mut cfg, key, "1").unwrap_err(); + assert!(e.contains("invalid config key"), "set {key:?}: {e}"); + } + for key in ["no_such_key", "theme.sub.key", "keys.no_such_field", "agents.watchdog.nope", "appearance.nope.deeper"] { + let e = get_key(&cfg, key).unwrap_err(); + assert!(e.contains("unknown config key"), "get {key:?}: {e}"); + assert!(set_key(&mut cfg, key, "1").is_err(), "set {key:?} must fail"); + } + assert_eq!(cfg, original, "a rejected set must not modify the config"); +} + +#[test] +fn type_errors_are_reported_and_atomic() { + let original = BiltermConfig::default(); + let keys = list_keys(&original); + let mut cfg = original.clone(); + let mut rejected = 0; + for (key, text) in &keys { + // A table can never be a valid value for a scalar leaf, whatever its real type is. + let before = cfg.clone(); + if set_key(&mut cfg, key, "{ nested = { deeper = [1, 2] } }").is_err() { + rejected += 1; + assert_eq!(cfg, before, "failed set of {key} (was {text}) modified the config"); + } else { + cfg = before; // string-typed keys keep the raw text as a bare string, by design + } + } + // Numbers, booleans and enums (the large majority of leaves) must all refuse it. + assert!(rejected * 2 > keys.len(), "only {rejected}/{} keys rejected a table value", keys.len()); +} + +#[test] +fn a_bare_word_is_accepted_as_a_string_value() { + let mut cfg = BiltermConfig::default(); + let (key, _) = list_keys(&cfg).into_iter().find(|(k, _)| k == "history_session").expect("history_session is a listed key"); + set_key(&mut cfg, &key, "work-notes").expect("bare word becomes a string"); + assert_eq!(get_key(&cfg, &key).unwrap(), "\"work-notes\""); + set_key(&mut cfg, &key, "\"quoted value\"").expect("quoted TOML string"); + assert_eq!(get_key(&cfg, &key).unwrap(), "\"quoted value\""); +} + +// ---------------------------------------------------------------- loading and saving + +#[test] +fn load_distinguishes_missing_unreadable_and_invalid_files() { + let dir = tempfile::tempdir().unwrap(); + let missing = dir.path().join("absent.toml"); + assert_eq!(BiltermConfig::load(&missing), Ok(None), "a missing file is not an error"); + assert_eq!(BiltermConfig::load_or_default(&missing), BiltermConfig::default()); + + // A directory where a file is expected: reported, not silently defaulted. + let e = BiltermConfig::load(dir.path()).unwrap_err(); + assert!(e.contains("cannot read"), "{e}"); + + let bad = dir.path().join("bad.toml"); + for text in ["theme = [", "this is not toml", "[keys]\nprofile = 7", "\u{0}\u{1}\u{2}"] { + std::fs::write(&bad, text).unwrap(); + let e = BiltermConfig::load(&bad).unwrap_err(); + assert!(e.contains("invalid") && e.contains("bad.toml"), "error must name the file: {e}"); + assert_eq!(BiltermConfig::load_or_default(&bad), BiltermConfig::default(), "fallback is the defaults"); + } + std::fs::write(&bad, [0xff, 0xfe, 0x00, 0x41]).unwrap(); + assert!(BiltermConfig::load(&bad).is_err(), "non-UTF-8 bytes are an error, not a panic"); + + let empty = dir.path().join("empty.toml"); + std::fs::write(&empty, "").unwrap(); + assert_eq!(BiltermConfig::load(&empty), Ok(Some(BiltermConfig::default())), "an empty file is the defaults"); +} + +#[test] +fn a_fresh_install_differs_from_the_defaults_only_by_profile() { + let fresh = BiltermConfig::fresh_install(); + let default = BiltermConfig::default(); + assert_ne!(fresh, default); + let changed: Vec = list_keys(&fresh) + .into_iter() + .filter(|(k, v)| get_key(&default, k).ok().as_ref() != Some(v)) + .map(|(k, _)| k) + .collect(); + assert!(!changed.is_empty() && changed.iter().all(|k| k.starts_with("appearance")), "{changed:?}"); +} + +#[test] +fn save_changes_writes_only_the_changed_keys_and_keeps_the_rest_of_the_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.toml"); + std::fs::write(&path, "# my hand-written comment\nhistory_session = \"old\"\n").unwrap(); + + let mut cfg = BiltermConfig::load(&path).unwrap().unwrap(); + set_key(&mut cfg, "history_session", "new").unwrap(); + let written = save_changes(&cfg, &path, None).unwrap(); + assert_eq!(written, vec!["history_session".to_string()]); + + let text = std::fs::read_to_string(&path).unwrap(); + assert!(text.contains("# my hand-written comment"), "comments must survive: {text}"); + assert!(text.contains("history_session = \"new\""), "{text}"); + assert_eq!(BiltermConfig::load(&path).unwrap().unwrap(), cfg, "what was saved is what loads"); + + // Nothing changed: nothing written. A prefix that matches nothing: nothing written. + assert!(save_changes(&cfg, &path, None).unwrap().is_empty()); + set_key(&mut cfg, "history_session", "third").unwrap(); + assert!(save_changes(&cfg, &path, Some("no.such.prefix")).unwrap().is_empty()); + + // A corrupt file on disk is reported instead of being overwritten. + std::fs::write(&path, "theme = [").unwrap(); + assert!(save_changes(&cfg, &path, None).is_err()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "theme = [", "the corrupt file is left for the user to fix"); + + // A missing file is created with just the changed keys. + let fresh = dir.path().join("new").join("config.toml"); + std::fs::create_dir_all(fresh.parent().unwrap()).unwrap(); + let written = save_changes(&cfg, &fresh, None).unwrap(); + assert_eq!(written, vec!["history_session".to_string()]); + assert_eq!(BiltermConfig::load(&fresh).unwrap().unwrap(), cfg); +} + +// ---------------------------------------------------------------- CLI argument parsing + +fn args(v: &[&str]) -> Vec { + v.iter().map(ToString::to_string).collect() +} + +fn parse(v: &[&str]) -> Result { + cli::parse(&args(v), &CliEnv::default()) +} + +#[test] +fn only_the_documented_words_are_cli_commands() { + for word in cli::CLI_COMMANDS { + assert!(cli::is_cli_command(Some(word)), "{word}"); + assert!(cli::usage().contains(&format!("bilterm {word} ")), "usage must document `{word}`"); + } + for word in ["", "ctl", "ls", "run", "Agent", "agents", "--agent", "pane ", "coord"] { + assert!(!cli::is_cli_command(Some(word)), "{word:?} must not be routed to the CLI"); + } + assert!(!cli::is_cli_command(None)); +} + +#[test] +fn coords_subcommand_validates_before_any_rpc_is_sent() { + let ok = parse(&["coords", "resolve", "d0:w1:main:y2"]).unwrap(); + assert_eq!(ok.method, "coords.resolve"); + assert_eq!(ok.params.get("coord").and_then(JsonValue::as_str), Some("d0:w1:main:y2")); + assert!(ok.poll.is_none()); + + assert!(parse(&["coords", "resolve"]).unwrap_err().contains("needs a COORD")); + assert!(parse(&["coords", "resolve", "((unbalanced"]).unwrap_err().contains("invalid coord")); + assert!(parse(&["coords", "bogus"]).unwrap_err().contains("unknown coords subcommand 'bogus'")); + assert!(parse(&["coords"]).unwrap_err().contains("Usage:")); + + let fmt = parse(&["coords", "format", "--pane", "3", "--style", "compact"]).unwrap(); + assert_eq!(fmt.method, "coords.format"); + assert_eq!(fmt.params.get("pane_id").and_then(JsonValue::as_i64), Some(3)); + assert_eq!(fmt.params.get("style").and_then(JsonValue::as_str), Some("compact")); + assert_eq!(parse(&["coords", "format"]).unwrap().params.to_json_string(), "{}"); + assert!(parse(&["coords", "format", "--pane", "three"]).unwrap_err().contains("numeric pane id")); + assert!(parse(&["coords", "format", "--style", "fancy"]).unwrap_err().contains("unknown style 'fancy'")); + assert!(parse(&["coords", "format", "--pane"]).unwrap_err().contains("--pane needs a value")); +} + +#[test] +fn option_syntax_equals_form_literal_separator_and_unknown_options() { + // `--flag=value` is the same as `--flag value`. + let a = parse(&["coords", "format", "--style=verbose"]).unwrap(); + let b = parse(&["coords", "format", "--style", "verbose"]).unwrap(); + assert_eq!(a, b); + + // After `--` everything is text, even things that look like options. + let sent = parse(&["pane", "send", "p2", "--", "--force", "-rf", "--"]).unwrap(); + assert_eq!(sent.method, "pane.send"); + assert_eq!(sent.params.get("text").and_then(JsonValue::as_str), Some("--force -rf --")); + assert!(sent.params.get("enter").is_none()); + let entered = parse(&["pane", "send", "p2", "ls", "-la", "--enter"]).unwrap(); + assert_eq!(entered.params.get("text").and_then(JsonValue::as_str), Some("ls -la")); + assert_eq!(entered.params.get("enter").and_then(JsonValue::as_bool), Some(true)); + + for (bad, needle) in [ + (&["pane", "list", "--verbose"][..], "unknown option --verbose"), + (&["agent", "list", "--"][..], ""), // a lone separator is fine + (&["frobnicate"][..], "unknown command 'frobnicate'"), + (&[][..], "Usage:"), + (&["pane", "split", "--direction"][..], "--direction needs a value"), + ] { + match parse(bad) { + Err(e) => assert!(!needle.is_empty() && e.contains(needle), "{bad:?}: {e}"), + Ok(_) => assert!(needle.is_empty(), "{bad:?} must fail with {needle:?}"), + } + } +} + +#[test] +fn pane_split_direction_can_be_positional_or_an_option() { + for (argv, dir, selector) in [ + (&["pane", "split"][..], "h", None), + (&["pane", "split", "v"][..], "v", None), + (&["pane", "split", "vertical", "w1:p2"][..], "vertical", Some("w1:p2")), + (&["pane", "split", "--direction", "v", "p3"][..], "v", Some("p3")), + (&["pane", "split", "p3"][..], "h", Some("p3")), + ] { + let call = parse(argv).unwrap(); + assert_eq!(call.method, "pane.split", "{argv:?}"); + assert_eq!(call.params.get("direction").and_then(JsonValue::as_str), Some(dir), "{argv:?}"); + assert_eq!(call.params.get("selector").and_then(JsonValue::as_str), selector, "{argv:?}"); + } +} + +#[test] +fn calls_made_from_inside_a_pane_are_attributed_to_it() { + let inside = CliEnv { pane_id: Some(7), socket: None }; + let call = cli::parse(&args(&["pane", "list"]), &inside).unwrap(); + assert_eq!(call.params.get("from_pane").and_then(JsonValue::as_usize), Some(7)); + let outside = cli::parse(&args(&["pane", "list"]), &CliEnv::default()).unwrap(); + assert!(outside.params.get("from_pane").is_none()); + // Workspace calls carry only what they need. + let ws = cli::parse(&args(&["workspace", "select", "w2"]), &inside).unwrap(); + assert_eq!(ws.params.to_json_string(), "{\"name\":\"w2\"}"); +} + +#[test] +fn endpoint_resolution_prefers_the_environment_and_normalises_its_form() { + let env = |s: &str| CliEnv { pane_id: None, socket: Some(s.to_string()) }; + let empty = tempfile::tempdir().unwrap(); + // A bare absolute path is a Unix socket; an explicit scheme or host:port is passed through. + assert_eq!(cli::resolve_endpoint(&env("/run/x/bilterm.sock"), None).as_deref(), Some("unix:/run/x/bilterm.sock")); + assert_eq!(cli::resolve_endpoint(&env("unix:/run/x.sock"), None).as_deref(), Some("unix:/run/x.sock")); + assert_eq!(cli::resolve_endpoint(&env("127.0.0.1:7777"), Some(empty.path())).as_deref(), Some("127.0.0.1:7777")); + // No environment and nothing registered: the caller must fall back to its default. + assert_eq!(cli::resolve_endpoint(&CliEnv::default(), None), None); + assert_eq!(cli::resolve_endpoint(&CliEnv::default(), Some(empty.path())), None); + assert_eq!(cli::resolve_endpoint(&CliEnv::default(), Some(Path::new("definitely/not/a/dir"))), None); +} diff --git a/tests/daemon_resilience_lifecycle.rs b/tests/daemon_resilience_lifecycle.rs index 36f00d23..dd47722f 100644 --- a/tests/daemon_resilience_lifecycle.rs +++ b/tests/daemon_resilience_lifecycle.rs @@ -63,7 +63,10 @@ fn release_and_stop_never_unlink_the_lockfile() { assert!(lock_path.exists(), "stop must not unlink the lockfile"); } +// Quarantined off Linux: zombie detection reads /proc (`daemon::liveness::pid_is_zombie` is Linux-only), +// so on macOS a registry entry whose process is an unreaped zombie is still listed as active. #[test] +#[cfg_attr(not(target_os = "linux"), ignore = "product gap: pid_is_zombie is Linux-only (/proc); zombie daemon entries are not detected on this OS")] fn zombie_registry_entry_is_not_listed_active() { let tmp = tempfile::tempdir().unwrap(); let dir = tmp.path(); diff --git a/tests/live_daemon_detach_attach.rs b/tests/live_daemon_detach_attach.rs index 022f5863..588b59d1 100644 --- a/tests/live_daemon_detach_attach.rs +++ b/tests/live_daemon_detach_attach.rs @@ -7,6 +7,8 @@ //! while keeping a live `UnixListener` socket intact and discoverable via `list_active_daemon_sessions_in`. //! 3. `"session.save"` and `"session.restore"` JSON-RPC methods via `AutomationControlPlane` preserving //! multi-pane `SplitTree`, `ZStack` custom window geometry, prompt input, and pane output across detach/restore. +// Daemon sockets and signals are Unix-only; the imports they need are unused on other targets. +#![cfg_attr(not(unix), allow(unused_imports))] use ratatui::layout::Rect; diff --git a/tests/malformed_input_robustness.rs b/tests/malformed_input_robustness.rs new file mode 100644 index 00000000..3b526f66 --- /dev/null +++ b/tests/malformed_input_robustness.rs @@ -0,0 +1,448 @@ +//! Parsers and protocol handlers fed malformed, hostile and randomly mutated input. +//! +//! Every text entry point an untrusted peer, config file or keyboard can reach must reject +//! garbage with an error value, never with a panic, a hang or a silent acceptance. Inputs come +//! from fixed-seed PRNG streams (`common::Rng`) so a failure is reproducible on every OS. + +mod common; + +use std::panic::{AssertUnwindSafe, catch_unwind}; +use std::time::{Duration, Instant}; + +use bilterm::api::selector::parse_selector; +use bilterm::api::static_serve::{sanitize_request_path, validate_cors_origin}; +use bilterm::api::{AutomationControlPlane, JsonValue, Playbook, parse_ctl_args, parse_key_chord_str, validate_http_bind}; +use bilterm::colon_command::parse_colon_command; +use bilterm::config::BiltermConfig; +use bilterm::input::chord::KeyChord; +use bilterm::swarm::SwarmPlan; +use bilterm::tmux::TmuxEnv; +use bilterm::tmux::hydrate::parse_block_header; +use bilterm::tmux::splitter::ControlLineSplitter; +use bilterm::ui::theme_import::parse_itermcolors_xml; +use bilterm::vt::codec::base64_decode; +use bilterm::wm::coordinates::TargetCoord; +use common::Rng; + +/// Tokens that are meaningful to at least one of the parsers under test. +const HOSTILE: &[&str] = &[ + "", " ", "\t", "\n", "\r\n", "\0", "\u{feff}", "\u{202e}", "\u{1f680}", "e\u{301}", "\u{fffd}", + "0", "-0", "1", "-1", "9", "255", "256", "65535", "65536", "4294967296", "18446744073709551616", "1e400", "NaN", + "0x10", "00", ".", "..", "...", "/", "//", "\\", "/etc/passwd", "C:\\Windows", "../..", "~", "%00", "%2e%2e", "%zz", "%", + ":", "::", ";", ",", "=", "==", "'", "\"", "`", "$(x)", "${x}", "|", "&", "&&", ">", "<", "*", "?", "#", "@", "!", "[", "]", "{", "}", + "(", ")", "--", "---", "-h", "--help", "--force", "--squash", "true", "false", "null", "on", "off", "yes", "no", + "h", "v", "p1", "w1:p1", "w0", "d0:w1:main:y2", "@idle", "@claude", "@nope", "#2", "%3", "main", + "ctrl", "ctrl+", "+c", "ctrl+shift+alt+super+f24", "F0", "F99", "enter", "esc", + "%begin", "%end", "%error", "%output", "%begin 1 2 3", "%end 1 2", "unix:", "unix:/x", "127.0.0.1:0", "[::1]:1", "localhost:99999", + "http://", "https://a.b:1", "http://a/b", "null", "file://", "*", + "a.b.c", "a..b", ".a", "a.", "theme", "keys.profile", "agents.watchdog", "scrollback.lines", +]; + +fn token(rng: &mut Rng) -> String { + match rng.below(8) { + 0 => { + let n = rng.below(24); + String::from_utf8_lossy(&rng.bytes(n)).into_owned() + } + 1 => { + let max = if rng.chance(30) { 70_000 } else { 300 }; + "A".repeat(rng.below(max)) + } + 2 => rng.next_u64().to_string(), + _ => (*rng.pick(HOSTILE)).to_string(), + } +} + +fn line(rng: &mut Rng, sep: &str) -> String { + (0..rng.below(7)).map(|_| token(rng)).collect::>().join(sep) +} + +/// Runs `f(input)` for `cases` PRNG inputs per seed; fails naming the exact input on a panic or +/// when the whole run exceeds a generous time budget. +fn never_panics(name: &str, seeds: &[u64], cases: usize, mut make: impl FnMut(&mut Rng) -> String, f: impl Fn(&str)) { + let started = Instant::now(); + for &seed in seeds { + let mut rng = Rng::new(seed); + for case in 0..cases { + let input = make(&mut rng); + if catch_unwind(AssertUnwindSafe(|| f(&input))).is_err() { + let shown: String = input.chars().take(200).collect(); + panic!("{name}: panicked on seed={seed} case={case} len={} input={shown:?}", input.len()); + } + } + } + assert!(started.elapsed() < Duration::from_secs(60), "{name}: took {:?}", started.elapsed()); +} + +// ---------------------------------------------------------------- JSON + +fn gen_json(rng: &mut Rng, depth: usize) -> JsonValue { + match rng.below(if depth > 4 { 5 } else { 7 }) { + 0 => JsonValue::Null, + 1 => JsonValue::Bool(rng.chance(2)), + 2 => JsonValue::Number((rng.next_u64() >> rng.below(64)) as i64 * if rng.chance(2) { -1 } else { 1 }), + 3 | 4 => JsonValue::String(token(rng)), + 5 => JsonValue::Array((0..rng.below(5)).map(|_| gen_json(rng, depth + 1)).collect()), + _ => JsonValue::obj((0..rng.below(5)).map(|_| (token(rng), gen_json(rng, depth + 1))).collect::>()), + } +} + +#[test] +fn json_round_trips_every_generated_value() { + for seed in [1u64, 2, 3, 4, 5, 6, 7, 8] { + let mut rng = Rng::new(seed); + for case in 0..150 { + let value = gen_json(&mut rng, 0); + let text = value.to_json_string(); + let back = JsonValue::parse(&text).unwrap_or_else(|| panic!("seed={seed} case={case}: own output does not parse: {text:?}")); + assert_eq!(back, value, "seed={seed} case={case}: round trip changed the value: {text:?}"); + } + } +} + +#[test] +fn json_rejects_malformed_documents_without_panicking() { + for bad in [ + "", " ", "{", "}", "[", "]", "[1,]", "[,1]", "{\"a\"}", "{\"a\":}", "{:1}", "{\"a\":1,}", "{\"a\":1 \"b\":2}", "{a:1}", + "\"unterminated", "\"bad \\q escape\"", "\"\\u12\"", "\"\\ud800\"", "\"raw \n newline\"", "nul", "tru", "True", "NULL", + "01", "1.", ".5", "+1", "--1", "1e", "0x10", "1 2", "[1] [2]", "{} x", "\u{feff}{}", "'single'", "[1;2]", + ] { + assert_eq!(JsonValue::parse(bad), None, "must be rejected: {bad:?}"); + } + never_panics( + "JsonValue::parse", + &[11, 12, 13, 14], + 400, + |rng| { + // Mutate a valid document: the interesting bugs live one byte away from valid input. + let mut bytes = gen_json(rng, 0).to_json_string().into_bytes(); + for _ in 0..1 + rng.below(4) { + if bytes.is_empty() { + break; + } + let i = rng.below(bytes.len()); + match rng.below(4) { + 0 => bytes[i] = *rng.pick(b"{}[]\",:\\ \n0e-.tfnu"), + 1 => bytes.truncate(i), + 2 => bytes.insert(i, rng.byte() & 0x7f), + _ => { + bytes.remove(i); + } + } + } + String::from_utf8_lossy(&bytes).into_owned() + }, + |s| { + if let Some(v) = JsonValue::parse(s) { + // Whatever is accepted must survive its own serialisation. + assert_eq!(JsonValue::parse(&v.to_json_string()), Some(v)); + } + }, + ); +} + +#[test] +fn json_nesting_bombs_are_rejected_not_stack_overflowed() { + for (open, close) in [("[", "]"), ("{\"a\":", "}")] { + let ok = format!("{}1{}", open.repeat(100), close.repeat(100)); + assert!(JsonValue::parse(&ok).is_some(), "100 levels are legitimate"); + for depth in [200usize, 5_000, 200_000] { + let bomb = format!("{}1{}", open.repeat(depth), close.repeat(depth)); + assert_eq!(JsonValue::parse(&bomb), None, "depth {depth} must be refused"); + assert_eq!(JsonValue::parse(&open.repeat(depth)), None, "unterminated depth {depth}"); + } + } +} + +// ---------------------------------------------------------------- JSON-RPC / MCP dispatch + +#[test] +fn rpc_dispatch_survives_wrongly_typed_and_hostile_params_for_every_method() { + let snapshots = tempfile::tempdir().unwrap(); + let mut cp = AutomationControlPlane::new_headless(24, 80).with_snapshot_dir(snapshots.path().to_path_buf()); + // Methods that deliberately reach outside the process (spawn daemons, ssh, browsers, audio, + // real git worktrees) are exercised by their own suites, not by a fuzzer. + let skip_prefix = ["daemon.", "remote.", "fleet.", "browser.", "video.", "voice.", "web.", "swarm.", "playbook.", "session."]; + let keys = ["pane_id", "command", "data", "keys", "selector", "name", "path", "direction", "text", "kind", "query", "id", "op", "key", "value", "coord", "style", "slug", "node", "to", "from", "body", "limit", "action", "size"]; + let mut rng = Rng::new(0xD15_7A7C); + let mut calls = 0; + for method in bilterm::api::RPC_METHODS { + if skip_prefix.iter().any(|p| method.starts_with(p)) { + continue; + } + for _ in 0..12 { + let params = match rng.below(6) { + 0 => JsonValue::Null, + 1 => JsonValue::Array(vec![JsonValue::Number(1)]), + 2 => JsonValue::String(token(&mut rng)), + _ => JsonValue::obj( + (0..1 + rng.below(4)) + .map(|_| { + let k = *rng.pick(&keys); + let v = match rng.below(5) { + 0 => JsonValue::Number(*rng.pick(&[-1i64, 0, 1, 2, 999_999, i64::MAX, i64::MIN])), + 1 => JsonValue::Bool(rng.chance(2)), + 2 => JsonValue::Null, + 3 => JsonValue::Array(vec![]), + _ => JsonValue::String((*rng.pick(HOSTILE)).to_string()), + }; + (k, v) + }) + .collect::>(), + ), + }; + let shown = params.to_json_string(); + let outcome = catch_unwind(AssertUnwindSafe(|| { + let _ = cp.dispatch_method(method, ¶ms); + })); + assert!(outcome.is_ok(), "{method} panicked on params {shown}"); + calls += 1; + } + } + assert!(calls > 500, "the fuzzer must actually reach the dispatch table (made {calls} calls)"); + // The plane is still coherent afterwards. + assert!(cp.dispatch_method("workspace.status", &JsonValue::Null).is_ok()); + assert!(cp.dispatch_method("no.such.method", &JsonValue::Null).is_err()); +} + +#[test] +fn mcp_line_handler_survives_garbage_lines() { + let mut cp = AutomationControlPlane::new_headless(24, 80); + for line in [ + "", "{", "[]", "null", "42", "\"str\"", "{}", "{\"id\":1}", "{\"method\":\"ping\"}", "{\"id\":1,\"method\":1}", + "{\"id\":1,\"method\":\"tools/call\"}", "{\"id\":1,\"method\":\"tools/call\",\"params\":[]}", + "{\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":7}}", + "{\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"bilterm_find\",\"arguments\":{\"kind\":3}}}", + "{\"id\":1,\"method\":\"resources/read\",\"params\":{\"uri\":\"../../etc/passwd\"}}", + "{\"id\":1,\"method\":\"resources/read\",\"params\":{}}", + "{\"id\":{\"nested\":[]},\"method\":\"ping\"}", + ] { + let reply = catch_unwind(AssertUnwindSafe(|| cp.handle_mcp_line(line))); + let reply = reply.unwrap_or_else(|_| panic!("handle_mcp_line panicked on {line:?}")); + if let Some(r) = reply { + assert!(JsonValue::parse(&r).is_some(), "reply to {line:?} is not JSON: {r}"); + } + } + let mut rng = Rng::new(77); + for _ in 0..300 { + let l = format!("{{\"jsonrpc\":\"2.0\",\"id\":{},\"method\":{:?},\"params\":{}}}", rng.below(9), token(&mut rng), gen_json(&mut rng, 3).to_json_string()); + assert!(catch_unwind(AssertUnwindSafe(|| cp.handle_mcp_line(&l))).is_ok(), "panicked on {l:?}"); + } +} + +// ---------------------------------------------------------------- colon commands + +#[test] +fn colon_command_parser_never_panics_on_any_registered_head_with_hostile_arguments() { + let heads: Vec<&'static str> = bilterm::command_registry::catalog().iter().map(|c| c.name).collect(); + assert!(heads.len() > 40, "the registry should list the colon commands (got {})", heads.len()); + never_panics( + "parse_colon_command", + &[21, 22, 23, 24, 25], + 1200, + |rng| match rng.below(10) { + 0 => line(rng, " "), + 1 => format!(":{}", line(rng, " ")), + _ => format!("{} {}", rng.pick(&heads), line(rng, " ")), + }, + |s| { + let _ = parse_colon_command(s); + }, + ); + // Things that are not commands at all are refused or ignored, never accepted as an action. + for not_a_command in [":", ": ", ":\u{1f680}", ":definitely-not-a-command", ":split\u{0}h"] { + assert!(!matches!(parse_colon_command(not_a_command), Some(Ok(_))), "{not_a_command:?} must not become an Action"); + } +} + +#[test] +fn every_registered_example_parses() { + // The palette advertises these lines to users; a registry entry that does not parse is a lie. + let mut checked = 0; + for info in bilterm::command_registry::catalog() { + for ex in &info.examples { + if ex.ends_with(' ') || ex.contains('<') || ex.contains('[') { + continue; // fill-in templates (`:ssh `, `:theme `) + } + checked += 1; + assert!(!matches!(parse_colon_command(ex), Some(Err(_)) | None), "advertised example does not parse: {ex:?}"); + } + } + assert!(checked > 20, "expected to check many ready-to-run examples (checked {checked})"); +} + +// ---------------------------------------------------------------- selectors, coordinates, chords + +#[test] +fn selector_coordinate_and_chord_parsers_never_panic() { + let mk = |rng: &mut Rng| if rng.chance(3) { line(rng, ":") } else { token(rng) }; + never_panics("parse_selector", &[31, 32, 33], 1500, mk, |s| { + let _ = parse_selector(s); + }); + never_panics("TargetCoord::parse", &[34, 35, 36], 1500, mk, |s| { + let _ = TargetCoord::parse(s); + }); + never_panics("KeyChord::parse", &[37, 38], 1500, |rng| line(rng, "+"), |s| { + let _ = KeyChord::parse(s); + let _ = parse_key_chord_str(s); + }); + for bad in ["", " ", "w", "p", "w1:", ":p1", "w-1:p1", "w1:p1:p2", "@", "@bad!name", "@a b", "99999999999999999999"] { + assert!(parse_selector(bad).is_err(), "selector {bad:?} must be rejected"); + } + for bad in ["", " ", "(", ")", "(a))", "((a)", "(x(a,b))", "(d0:(a,b)", ")("] { + assert!(TargetCoord::parse(bad).is_err(), "coordinate {bad:?} must be rejected"); + } + for bad in ["", " ", "ctrl+", "bogusmod+c", "ctrl+ctrl+c", "primary+primary+c", "ctrl+shift+"] { + assert!(KeyChord::parse(bad).is_err(), "chord {bad:?} must be rejected"); + } +} + +// ---------------------------------------------------------------- HTTP surface + +#[test] +fn static_path_sanitizer_never_lets_a_traversal_through() { + never_panics( + "sanitize_request_path", + &[41, 42, 43, 44], + 2000, + |rng| format!("/{}", line(rng, "/")), + |s| { + if let Ok(segments) = sanitize_request_path(s) { + for seg in &segments { + assert!(seg != "." && seg != ".." && !seg.is_empty(), "{s:?} -> dot/empty segment {seg:?}"); + assert!(!seg.contains(['/', '\\', ':', '\0']), "{s:?} -> separator in segment {seg:?}"); + assert!(!seg.bytes().any(|b| b < 0x20 || b == 0x7f), "{s:?} -> control byte in {seg:?}"); + } + } + }, + ); + for attack in [ + "/../etc/passwd", "/..", "/a/../../b", "/%2e%2e/secret", "/%2E%2E%2Fsecret", "/a/%2e%2e", "/..%2f..%2f", "/a\\..\\b", "/%5c..%5c", + "/C:/Windows", "/c%3a/x", "/a%00.html", "/%0d%0aSet-Cookie:x", "/a/./b", "/%2e", "/\u{7f}", "/%", "/%z1", "/%f", + ] { + assert!(sanitize_request_path(attack).is_err(), "{attack:?} must be refused"); + } + assert_eq!(sanitize_request_path("/assets/app.js?v=1#frag"), Ok(vec!["assets".to_string(), "app.js".to_string()])); + assert_eq!(sanitize_request_path("//a///b/"), Ok(vec!["a".to_string(), "b".to_string()])); +} + +#[test] +fn http_bind_and_cors_origin_validation_reject_everything_but_the_safe_shapes() { + for bad in ["", "0.0.0.0:8080", "192.168.1.1:80", "[::]:80", "example.com:80", "127.0.0.1", "127.0.0.1:", "127.0.0.1:99999", ":80", "localhost"] { + assert!(validate_http_bind(bad).is_err(), "bind {bad:?} must be refused (loopback only)"); + } + assert!(validate_http_bind("127.0.0.1:0").is_ok()); + for bad in ["", "*", "null", "example.com", "http://", "http://a/b", "http://a?x", "http://a#x", "ftp://a", "http://a b", "http://a\r\nX: y", "http://*", "javascript:alert(1)"] { + assert!(validate_cors_origin(bad).is_err(), "origin {bad:?} must be refused"); + } + assert!(validate_cors_origin("http://localhost:5173").is_ok()); + never_panics("validate_http_bind/cors", &[51, 52], 1500, |rng| line(rng, ":"), |s| { + let _ = validate_http_bind(s); + let _ = validate_cors_origin(s); + }); +} + +#[test] +fn ctl_argument_parsing_defaults_and_tolerates_bad_json() { + let args = |v: &[&str]| v.iter().map(ToString::to_string).collect::>(); + let (ep, method, params) = parse_ctl_args(&args(&[]), "unix:/default.sock"); + assert_eq!((ep.as_str(), method.as_str()), ("unix:/default.sock", "workspace.status")); + assert_eq!(params.to_json_string(), "{}"); + let (ep, method, params) = parse_ctl_args(&args(&["127.0.0.1:9", "pane.read", "{\"pane_id\":2}"]), "unix:/d"); + assert_eq!((ep.as_str(), method.as_str()), ("127.0.0.1:9", "pane.read")); + assert_eq!(params.get("pane_id").and_then(JsonValue::as_usize), Some(2)); + // Unparseable params degrade to `{}` instead of aborting the client. + let (_, method, params) = parse_ctl_args(&args(&["pane.read", "{not json"]), "unix:/d"); + assert_eq!((method.as_str(), params.to_json_string().as_str()), ("pane.read", "{}")); + never_panics("parse_ctl_args", &[53], 800, |rng| line(rng, "\u{1}"), |s| { + let v: Vec = s.split('\u{1}').map(ToString::to_string).collect(); + let _ = parse_ctl_args(&v, "unix:/d"); + }); +} + +// ---------------------------------------------------------------- tmux control mode + +#[test] +fn tmux_control_parsers_never_panic_and_bound_their_buffers() { + never_panics("parse_block_header", &[61, 62], 1500, |rng| line(rng, " "), |s| { + let _ = parse_block_header(s); + let _ = TmuxEnv::from_env_str(s); + }); + for bad in ["", "%begin", "%begin 1 2", "%begin a b c", "%begin 1 2 3 4", "%begin -1 2 3", "begin 1 2 3", "%BEGIN 1 2 3", "%begin 1 2 3"] { + assert!(parse_block_header(bad).is_none(), "{bad:?} is not a block header"); + } + assert!(parse_block_header("%end 1700000000 42 1\r\n").is_some()); + for blank in ["", " ", "\t\n"] { + assert!(TmuxEnv::from_env_str(blank).is_none(), "TMUX={blank:?} must be rejected"); + } + // The parser is lenient by design: a non-numeric pid is dropped, a missing session id is "0". + let lenient = TmuxEnv::from_env_str("/run/tmux/default,not-a-pid").expect("socket path alone is enough"); + assert_eq!((lenient.socket_path.as_str(), lenient.server_pid, lenient.session_id.as_str()), ("/run/tmux/default", None, "0")); + + // The splitter sees raw remote bytes: a peer that never sends a newline must not grow it forever. + let mut rng = Rng::new(63); + let mut sp = ControlLineSplitter::new(); + let mut peak = 0; + for _ in 0..400 { + let chunk = match rng.below(4) { + 0 => { + let n = rng.below(512); + rng.bytes(n) + } + 1 => vec![b'%'; 4096], + 2 => b"\x1bP1000p%begin 1 2 3\n%output %1 hi\n%end 1 2 3\n".to_vec(), + _ => vec![b'x'; 16 * 1024], // no newline at all + }; + let _ = sp.push(&chunk); + peak = peak.max(sp.buffered_len()); + } + let _ = sp.flush_partial(); + assert!(peak < 4 * 1024 * 1024, "splitter buffered {peak} bytes of newline-free input"); +} + +// ---------------------------------------------------------------- config, playbooks, plans, themes + +#[test] +fn config_playbook_swarm_and_theme_loaders_reject_garbage_without_panicking() { + let toml_ish = |rng: &mut Rng| { + (0..rng.below(8)) + .map(|_| match rng.below(5) { + 0 => format!("[{}]", token(rng)), + 1 => format!("[[{}]]", token(rng)), + 2 => format!("{} = {}", rng.pick(HOSTILE), rng.pick(HOSTILE)), + 3 => format!("{} = \"{}\"", rng.pick(&["theme", "font_size", "history_session", "id", "prompt", "name", "steps", "depends_on"]), token(rng)), + _ => token(rng), + }) + .collect::>() + .join("\n") + }; + never_panics("BiltermConfig::from_toml_str", &[71, 72, 73], 600, toml_ish, |s| { + let _ = BiltermConfig::from_toml_str(s); + }); + never_panics("Playbook::from_toml_str", &[74, 75], 600, toml_ish, |s| { + let _ = Playbook::from_toml_str(s); + }); + never_panics("SwarmPlan::parse", &[76, 77], 600, toml_ish, |s| { + let _ = SwarmPlan::parse(s); + }); + never_panics("parse_itermcolors_xml / parse_ini_theme / base64", &[78, 79], 600, |rng| line(rng, ""), |s| { + let _ = parse_itermcolors_xml("fuzz", s); + let _ = bilterm::config::parse_ini_theme(s); + let _ = base64_decode(s); + }); + + for bad in ["theme = [", "font_size_pt = \"big\"", "[keys]\nprofile = 7", "= 1", "[a]\n[a]", "theme = 1\ntheme = 2"] { + assert!(BiltermConfig::from_toml_str(bad).is_err(), "config {bad:?} must be rejected"); + } + // A plan with a dependency cycle, a duplicate id or a dangling dependency is refused up front. + for bad in [ + "[[node]]\nid='a'\nprompt='p'\ndepends_on=['a']\n", + "[[node]]\nid='a'\nprompt='p'\ndepends_on=['b']\n[[node]]\nid='b'\nprompt='q'\ndepends_on=['a']\n", + "[[node]]\nid='a'\nprompt='p'\n[[node]]\nid='a'\nprompt='q'\n", + "[[node]]\nid='a'\nprompt='p'\ndepends_on=['ghost']\n", + "[[node]]\nid='../escape'\nprompt='p'\n", + "", + ] { + assert!(SwarmPlan::parse(bad).is_err(), "swarm plan must be rejected: {bad:?}"); + } +} diff --git a/tests/navigator_two_line_rows.rs b/tests/navigator_two_line_rows.rs index a836c0c4..b7223442 100644 --- a/tests/navigator_two_line_rows.rs +++ b/tests/navigator_two_line_rows.rs @@ -106,7 +106,10 @@ fn two_line_rows_stack_pane_text_over_detail() { assert_eq!(b, a + 2, "each pane row takes two lines"); assert!(!ls[a + 1].contains("#2"), "line 2 is the detail line"); let detail = wm.drawer_rows()[1].navigator.as_ref().unwrap().detail.clone().expect("detail"); - assert!(ls[a + 1].contains(detail.split_whitespace().next().unwrap()), "detail line shows the prompt text"); + // The drawer clips the detail to its width, and `user@host` is as long as the machine makes it + // (CI runners have long host names): the row must start the detail text, not fit all of it. + let shown: String = detail.chars().take(8).collect(); + assert!(ls[a + 1].contains(&shown), "detail line shows the prompt text: {:?} lacks {shown:?}", ls[a + 1]); } #[test] diff --git a/tests/phase51_1_transport.rs b/tests/phase51_1_transport.rs index 36d8c8e9..df6aa5ac 100644 --- a/tests/phase51_1_transport.rs +++ b/tests/phase51_1_transport.rs @@ -245,6 +245,8 @@ mod splitter { } mod transport { + // The PTY transport tests below are Unix-only; their imports and helper are unused elsewhere. + #![cfg_attr(not(unix), allow(unused_imports, dead_code))] use bilterm::pty::PtyConfig; use bilterm::remote::RemoteHostProfile; use bilterm::tmux::link::{DriverExit, LinkConnector, LinkDriver, LinkTarget}; diff --git a/tests/phase58_stt_module_and_providers.rs b/tests/phase58_stt_module_and_providers.rs index bf7738bb..7500597d 100644 --- a/tests/phase58_stt_module_and_providers.rs +++ b/tests/phase58_stt_module_and_providers.rs @@ -260,6 +260,7 @@ async fn test_ollama_stt_transcription() { } #[tokio::test] +#[cfg_attr(windows, ignore = "drives a POSIX /bin/sh child; the Windows PTY path is covered by tests/smoke_binary_per_os.rs")] async fn test_local_cli_stt_provider() { // Invoke a shell echo command simulating a whisper CLI output let cli = LocalCliSttProvider::new(vec![ diff --git a/tests/phase66_67_live_agent_control.rs b/tests/phase66_67_live_agent_control.rs index a2d92a96..ab833c9c 100644 --- a/tests/phase66_67_live_agent_control.rs +++ b/tests/phase66_67_live_agent_control.rs @@ -35,8 +35,18 @@ fn isolate() { }); } +/// `bilterm::pty::set_live_socket` is process-global: tests that set it (directly or through +/// `LiveRpc::start`) hold this lock so a parallel test cannot swap the path under a spawning child. +fn live_socket_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + fn runtime_dir(tag: &str) -> PathBuf { - let dir = std::env::temp_dir().join(format!("bt66-{tag}-{}-{}", std::process::id(), bilterm::secure_fs::unique_suffix())); + // Unix socket paths are limited to ~104 bytes (macOS); its per-user temp dir alone is ~50, so + // sockets live under a short root instead of `std::env::temp_dir()`. + let root = if cfg!(unix) { PathBuf::from("/tmp") } else { std::env::temp_dir() }; + let dir = root.join(format!("bt66-{tag}-{}-{}", std::process::id(), bilterm::secure_fs::unique_suffix())); bilterm::secure_fs::ensure_private_dir(&dir).unwrap(); dir } @@ -80,6 +90,7 @@ fn live_child_env_has_all_four_variables() { #[test] fn spawned_child_pty_receives_bilterm_env() { isolate(); + let _live = live_socket_lock(); bilterm::pty::set_live_socket(Some(PathBuf::from("/tmp/bt66-env-test.sock"))); let cfg = PtyConfig { program: "/bin/sh".to_string(), @@ -161,6 +172,7 @@ fn registry_default_link_follows_newest_and_never_clobbers_daemon_socket() { #[test] fn client_socket_resolution_order() { + let _live = live_socket_lock(); let rt = runtime_dir("res"); assert_eq!(resolve_client_socket(&rt, Some("/x/y.sock")), Some(PathBuf::from("/x/y.sock"))); assert_eq!(resolve_client_socket(&rt, Some("unix:/x/y.sock")), Some(PathBuf::from("/x/y.sock"))); @@ -175,8 +187,11 @@ fn client_socket_resolution_order() { // ------------------------------------------------------------------ LIVE-01 live socket #[tokio::test] +// The guard only serialises tests that set the process-global live socket; nothing else contends for it. +#[allow(clippy::await_holding_lock)] async fn foreground_socket_executes_against_the_live_window_manager() { isolate(); + let _live = live_socket_lock(); let rt = runtime_dir("live"); // SAFETY: this is the only test in this binary that reads XDG_RUNTIME_DIR. unsafe { std::env::set_var("XDG_RUNTIME_DIR", &rt) }; diff --git a/tests/phase68_69_surface_parity.rs b/tests/phase68_69_surface_parity.rs index 04ddfe3c..5cb0bc82 100644 --- a/tests/phase68_69_surface_parity.rs +++ b/tests/phase68_69_surface_parity.rs @@ -354,13 +354,10 @@ fn swarm_colon_and_rpc_produce_equivalent_state() { } /// Genuinely missing Phase 69 surfaces (no colon form, no RPC method): these stay N/A until built. +/// (`:swarm run` / `swarm.run` shipped with the swarm wiring; see `tests/swarm_wiring.rs`.) #[test] -fn swarm_run_review_heal_remain_unwired() { - for (colon_probe, rpc) in [ - (":swarm run plan.toml", "swarm.run"), - (":swarm review 2", "swarm.review"), - (":swarm heal 2", "swarm.heal"), - ] { +fn swarm_review_heal_remain_unwired() { + for (colon_probe, rpc) in [(":swarm review 2", "swarm.review"), (":swarm heal 2", "swarm.heal")] { assert!( !matches!(parse_colon_command(colon_probe), Some(Ok(_))) && !RPC_METHODS.contains(&rpc), "{colon_probe} / {rpc} now exist: add rows for them to this suite" @@ -368,20 +365,9 @@ fn swarm_run_review_heal_remain_unwired() { } } -/// `:agents hud` / `:agent replay` forms parsed before the Phase 70 wiring still parse. +/// `:swarm run` and `swarm.run` are both wired (colon form takes no plan argument: it runs the loaded plan). #[test] -fn legacy_hud_and_replay_colon_forms_still_parse() { - use bilterm::wm::agents_hud_cmd::{HudOp, ReplayOp}; - for (text, want) in [ - (":agents hud open", Action::AgentsHud(HudOp::Open)), - (":agents hud close", Action::AgentsHud(HudOp::Close)), - (":agents hud toggle", Action::AgentsHud(HudOp::Toggle)), - (":agent replay show", Action::AgentsReplay(ReplayOp::Show)), - (":agent replay prev", Action::AgentsReplay(ReplayOp::Prev)), - (":agent replay next", Action::AgentsReplay(ReplayOp::Next)), - (":agent replay first", Action::AgentsReplay(ReplayOp::First)), - (":agent replay last", Action::AgentsReplay(ReplayOp::Last)), - ] { - assert_eq!(colon(text), want, "{text}"); - } +fn swarm_run_is_wired_on_colon_and_rpc() { + assert!(matches!(parse_colon_command(":swarm run"), Some(Ok(_)))); + assert!(RPC_METHODS.contains(&"swarm.run")); } diff --git a/tests/pty_lifecycle.rs b/tests/pty_lifecycle.rs index 22238769..d432b537 100644 --- a/tests/pty_lifecycle.rs +++ b/tests/pty_lifecycle.rs @@ -4,6 +4,7 @@ use bilterm::pty::{PtyConfig, PtyEvent, PtySession}; use bilterm::vt::VtParser; #[tokio::test] +#[cfg_attr(windows, ignore = "drives a POSIX /bin/sh child; the Windows PTY path is covered by tests/smoke_binary_per_os.rs")] async fn pty_spawns_shell_executes_command_and_deduplicates_sigwinch() { let mut cfg = PtyConfig::default(); if cfg!(windows) { diff --git a/tests/signalable_pid_invariants.rs b/tests/signalable_pid_invariants.rs index f5d7ab18..4b2f298c 100644 --- a/tests/signalable_pid_invariants.rs +++ b/tests/signalable_pid_invariants.rs @@ -7,6 +7,8 @@ //! - Serde serialization and deserialization invariant enforcement. //! - Lock record integration via `DaemonLockRecord::signalable_pid()`. //! - Process signaling invariants via `kill_daemon_process` and `check_signalable_pid_alive`. +// Daemon sockets and signals are Unix-only; the imports they need are unused on other targets. +#![cfg_attr(not(unix), allow(unused_imports))] use std::convert::TryFrom; use std::process::Command; diff --git a/tests/smoke_binary_per_os.rs b/tests/smoke_binary_per_os.rs new file mode 100644 index 00000000..5b367ece --- /dev/null +++ b/tests/smoke_binary_per_os.rs @@ -0,0 +1,366 @@ +//! Per-OS product smoke test: launches the real `bilterm` binary headlessly on every CI cell +//! (Linux / macOS / Windows, x86_64 / ARM64) so each cell proves the product *starts*, not merely +//! that it compiles. Everything runs in an isolated HOME / XDG / runtime tree (`common::IsolatedEnv`) +//! and never talks to a running instance. +//! +//! Layers, cheapest first: +//! 1. `--version` / `--help` and the other print-and-exit switches; +//! 2. CLI argument errors (`agent` / `pane` / `coords` / `ctl`) with exit codes; +//! 3. a scripted MCP session over stdio (`bilterm --mcp`), no sockets, no PTY; +//! 4. a scripted RPC session against a real headless daemon on loopback TCP, driven through +//! `bilterm ctl` and the `bilterm pane|workspace|coords` CLI; +//! 5. a real PTY child through the product's PTY layer (ConPTY on Windows). + +mod common; + +use std::io::{BufRead, BufReader, Write}; +use std::process::Stdio; +use std::sync::mpsc; +use std::time::Duration; + +use bilterm::api::JsonValue; +use common::{IsolatedEnv, KillOnDrop, PROCESS_DEADLINE, stderr_of, stdout_of}; + +const VERSION: &str = env!("CARGO_PKG_VERSION"); + +fn result_of(resp: &str) -> JsonValue { + let v = JsonValue::parse(resp.trim()).unwrap_or_else(|| panic!("response is not JSON: {resp}")); + assert!(v.get("error").is_none(), "RPC error: {resp}"); + v.get("result").cloned().unwrap_or_else(|| panic!("response has no result: {resp}")) +} + +// ---------------------------------------------------------------- 1. print-and-exit switches + +#[test] +fn version_switches_print_the_crate_version() { + let env = IsolatedEnv::new(); + for sw in ["--version", "-V", "-v"] { + let out = env.run(&[sw]); + assert!(out.status.success(), "{sw}: {}", stderr_of(&out)); + assert_eq!(stdout_of(&out).trim(), format!("bilterm {VERSION}"), "{sw}"); + } +} + +#[test] +fn help_lists_every_entry_point() { + let env = IsolatedEnv::new(); + for sw in ["--help", "-h"] { + let out = env.run(&[sw]); + assert!(out.status.success(), "{sw}: {}", stderr_of(&out)); + let text = stdout_of(&out); + for needle in [ + VERSION, + "Usage:", + "bilterm ctl", + "bilterm agent|pane|workspace|coords", + "bilterm run ", + "--mcp", + "--headless", + "--token-file", + "--version", + ] { + assert!(text.contains(needle), "{sw} output lacks {needle:?}:\n{text}"); + } + } +} + +#[test] +fn generator_switches_emit_their_artifacts() { + let env = IsolatedEnv::new(); + for shell in ["bash", "zsh", "fish"] { + let out = env.run(&["--completions", shell]); + assert!(out.status.success() && stdout_of(&out).contains("bilterm"), "completions {shell}"); + } + let out = env.run(&["--completions", "tcsh"]); + assert!(stderr_of(&out).contains("Supported shells"), "{}", stderr_of(&out)); + + let man = env.run(&["--man"]); + assert!(man.status.success() && stdout_of(&man).contains(".TH"), "manpage must be roff"); + + let desktop = env.run(&["--desktop-file"]); + assert!(stdout_of(&desktop).contains("[Desktop Entry]")); + + let ppm = env.dir("tmp").join("workspace.ppm"); + let dump = env.run(&["--gui-dump", ppm.to_str().unwrap()]); + assert!(dump.status.success(), "{}", stderr_of(&dump)); + let bytes = std::fs::read(&ppm).expect("gui dump written"); + assert!(bytes.starts_with(b"P6") && bytes.len() > 1024, "PPM header + pixels expected"); + + let plot = env.run(&["plot", "bar", "1,2,3"]); + assert!(plot.status.success() && stdout_of(&plot).contains("bilterm plot (bar"), "{}", stdout_of(&plot)); +} + +#[test] +fn ls_in_a_fresh_profile_reports_nothing_and_touches_no_real_state() { + let env = IsolatedEnv::new(); + let out = env.run(&["ls"]); + assert!(out.status.success(), "{}", stderr_of(&out)); + let text = stdout_of(&out); + assert!(text.contains("Active Background Daemons (0)"), "{text}"); + assert!(text.contains("Saved Workspace Sessions (0)"), "{text}"); +} + +// ---------------------------------------------------------------- 2. argument errors + +#[test] +fn cli_subcommand_mistakes_exit_2_with_usage() { + let env = IsolatedEnv::new(); + for (args, needle) in [ + (&["agent"][..], "Usage:"), + (&["coords"][..], "Usage:"), + (&["pane"][..], "Usage:"), + (&["workspace"][..], "Usage:"), + (&["pane", "bogus"][..], "unknown pane subcommand 'bogus'"), + (&["workspace", "bogus"][..], "unknown workspace subcommand 'bogus'"), + (&["agent", "list", "--nope"][..], "unknown option --nope"), + (&["agent", "wait", "@idle", "--timeout-ms"][..], "--timeout-ms needs a value"), + (&["pane", "focus"][..], "pane focus needs a SELECTOR"), + (&["pane", "send", "p1"][..], "pane send needs SELECTOR and TEXT"), + (&["workspace", "new"][..], "workspace new needs a NAME"), + ] { + let out = env.run(args); + assert_eq!(out.status.code(), Some(2), "{args:?}: stdout {} stderr {}", stdout_of(&out), stderr_of(&out)); + assert!(stderr_of(&out).contains(needle), "{args:?} stderr lacks {needle:?}: {}", stderr_of(&out)); + assert!(stdout_of(&out).is_empty(), "{args:?} must not print a result"); + } +} + +#[test] +fn cli_and_ctl_fail_fast_when_no_instance_is_listening() { + let env = IsolatedEnv::new(); + // A loopback port nothing listens on: an explicit TCP endpoint never auto-starts a daemon. + let dead = format!("127.0.0.1:{}", common::free_loopback_port()); + + let mut cmd = env.bilterm(&["coords", "resolve", "d0:w1:main:y2"]); + cmd.env("BILTERM_SOCKET", &dead); + let out = common::run_with_deadline(cmd, PROCESS_DEADLINE); + assert_eq!(out.status.code(), Some(1), "{}", stderr_of(&out)); + assert!(stderr_of(&out).contains("cannot reach a running bilterm instance"), "{}", stderr_of(&out)); + + let out = env.run(&["ctl", &dead, "workspace.status"]); + assert!(!out.status.success() && stdout_of(&out).is_empty(), "ctl must fail: {}", stdout_of(&out)); +} + +#[test] +fn missing_token_file_and_missing_playbook_are_reported_not_ignored() { + let env = IsolatedEnv::new(); + let missing = env.dir("tmp").join("no-such.token"); + let out = env.run(&["--token-file", missing.to_str().unwrap(), "ls"]); + assert!(!out.status.success()); + assert!(stderr_of(&out).contains("token file"), "{}", stderr_of(&out)); + + let out = env.run(&["run", env.dir("tmp").join("no-such-playbook.toml").to_str().unwrap()]); + assert!(!out.status.success(), "a missing playbook must fail"); + + let bad = env.dir("tmp").join("bad.toml"); + std::fs::write(&bad, "this is = = not toml [[[").unwrap(); + let out = env.run(&["--playbook", bad.to_str().unwrap()]); + assert!(!out.status.success(), "a malformed playbook must fail"); + assert!(!stdout_of(&out).contains("\"passed\":true")); +} + +/// Current behaviour, pinned so a change is deliberate: an unrecognised top-level switch is not +/// rejected, it falls through to the interactive launch, which fails without a terminal. +#[test] +#[cfg_attr(not(unix), ignore = "non-TTY interactive launch is only characterised on Unix so far")] +fn unknown_top_level_switch_without_a_tty_exits_instead_of_hanging() { + let env = IsolatedEnv::new(); + let out = env.run(&["--definitely-not-a-bilterm-switch"]); + assert!(!out.status.success(), "no TTY: the interactive launch cannot succeed"); + assert!(!stdout_of(&out).contains("Usage:"), "unknown switches do not print usage today"); +} + +// ---------------------------------------------------------------- 3. MCP over stdio + +struct McpSession { + child: KillOnDrop, + stdin: std::process::ChildStdin, + lines: mpsc::Receiver, + next_id: u64, +} + +impl McpSession { + fn start(env: &IsolatedEnv) -> Self { + let mut cmd = env.bilterm(&["--mcp", "--snapshot-dir", env.dir("tmp").to_str().unwrap()]); + let mut child = cmd.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().expect("spawn bilterm --mcp"); + let stdin = child.stdin.take().unwrap(); + let stdout = child.stdout.take().unwrap(); + let (tx, lines) = mpsc::channel(); + std::thread::spawn(move || { + for line in BufReader::new(stdout).lines().map_while(Result::ok) { + if tx.send(line).is_err() { + break; + } + } + }); + Self { child: KillOnDrop(child), stdin, lines, next_id: 1 } + } + + fn raw(&mut self, line: &str) -> String { + writeln!(self.stdin, "{line}").expect("write request"); + self.stdin.flush().expect("flush request"); + self.lines.recv_timeout(PROCESS_DEADLINE).unwrap_or_else(|_| panic!("no MCP response to: {line}")) + } + + fn request(&mut self, method: &str, params: &str) -> String { + let id = self.next_id; + self.next_id += 1; + self.raw(&format!(r#"{{"jsonrpc":"2.0","id":{id},"method":"{method}","params":{params}}}"#)) + } + + fn tool(&mut self, name: &str, arguments: &str) -> String { + self.request("tools/call", &format!(r#"{{"name":"{name}","arguments":{arguments}}}"#)) + } +} + +#[test] +fn mcp_stdio_scripted_session() { + let env = IsolatedEnv::new(); + let mut mcp = McpSession::start(&env); + + let init = result_of(&mcp.request("initialize", "{}")); + let info = init.get("serverInfo").expect("serverInfo"); + assert_eq!(info.get("name").and_then(JsonValue::as_str), Some("bilterm-mcp")); + assert_eq!(info.get("version").and_then(JsonValue::as_str), Some(VERSION)); + + assert!(result_of(&mcp.request("ping", "{}")).get("anything").is_none()); + let tools = mcp.request("tools/list", "{}"); + for tool in ["bilterm_workspace_status", "bilterm_exec_command", "bilterm_inspect_pane", "bilterm_colon_command"] { + assert!(tools.contains(tool), "tools/list lacks {tool}"); + } + + let status = mcp.tool("bilterm_workspace_status", "{}"); + assert!(status.contains("pane") && !status.contains("\"error\""), "{status}"); + + // Malformed traffic must produce JSON-RPC errors (or be dropped) and leave the server alive. + let bad_args = mcp.request("tools/call", r#"{"name":"bilterm_workspace_status","arguments":[1,2]}"#); + assert!(bad_args.contains("-32602"), "{bad_args}"); + let unknown = mcp.request("no/such/method", "{}"); + assert!(unknown.contains("\"error\""), "{unknown}"); + let unknown_tool = mcp.tool("no_such_tool", "{}"); + assert!(unknown_tool.contains("\"error\"") || unknown_tool.contains("isError"), "{unknown_tool}"); + // Not JSON at all, and a notification without an id: neither gets a reply, so the next + // well-formed request must be answered with *its* id. + writeln!(mcp.stdin, "this is not json").unwrap(); + writeln!(mcp.stdin, r#"{{"jsonrpc":"2.0","method":"ping"}}"#).unwrap(); + let pong = mcp.raw(r#"{"jsonrpc":"2.0","id":4242,"method":"ping","params":{}}"#); + assert!(pong.contains("4242") && !pong.contains("\"error\""), "{pong}"); + + // Closing stdin ends the server cleanly. + let McpSession { mut child, stdin, .. } = mcp; + drop(stdin); + let mut status = None; + assert!( + common::wait_until(PROCESS_DEADLINE, || { + status = child.0.try_wait().expect("poll mcp child"); + status.is_some() + }), + "bilterm --mcp must exit when stdin closes" + ); + assert!(status.unwrap().success()); +} + +// ---------------------------------------------------------------- 4. headless daemon over loopback TCP + +#[test] +fn headless_daemon_scripted_session_over_loopback_tcp() { + let env = IsolatedEnv::new(); + let token = env.token_file("smoke-token-0123456789abcdef0123456789"); + let token_path = token.to_str().unwrap().to_string(); + let endpoint = format!("127.0.0.1:{}", common::free_loopback_port()); + + let mut cmd = env.bilterm(&["--headless", "--listen", &endpoint, "--token-file", &token_path, "--name", "smoke"]); + let daemon = cmd.stdout(Stdio::null()).stderr(Stdio::piped()).spawn().expect("spawn headless daemon"); + let mut daemon = KillOnDrop(daemon); + + let ctl = |method: &str, params: &str| { + let mut args = vec!["ctl", "--token-file", token_path.as_str(), endpoint.as_str(), method]; + if !params.is_empty() { + args.push(params); + } + env.run(&args) + }; + + // Readiness: poll the real RPC surface with a deadline (no fixed sleep). + let mut last = String::new(); + let ready = common::wait_until(PROCESS_DEADLINE, || { + if let Ok(Some(st)) = daemon.0.try_wait() { + panic!("daemon exited early: {st}"); + } + let out = ctl("workspace.status", ""); + last = format!("{} {}", stdout_of(&out), stderr_of(&out)); + out.status.success() + }); + assert!(ready, "daemon never answered workspace.status: {last}"); + + let status = result_of(&stdout_of(&ctl("workspace.status", ""))); + let panes = status.get("panes").and_then(JsonValue::as_array).expect("panes array").len(); + assert!(panes >= 1, "a fresh workspace has a pane"); + + let split = result_of(&stdout_of(&ctl("pane.split", r#"{"direction":"v"}"#))); + assert_eq!(split.get("pane_count").and_then(JsonValue::as_usize), Some(panes + 1)); + + // Drive the terminal engine end to end: feed bytes, read the screen back. + let fed = result_of(&stdout_of(&ctl("pane.feed_output", r#"{"data":"SMOKE-MARKER-42\r\n"}"#))); + assert_eq!(fed.get("status").and_then(JsonValue::as_str), Some("processed")); + let read = stdout_of(&ctl("pane.read", "")); + assert!(read.contains("SMOKE-MARKER-42"), "pane.read lacks the fed marker: {read}"); + + // Errors come back as JSON-RPC errors, not as a dead daemon. + let unknown = stdout_of(&ctl("no.such.method", "")); + assert!(unknown.contains("\"error\""), "{unknown}"); + let bad_pane = stdout_of(&ctl("pane.feed_output", r#"{"pane_id":987654,"data":"x"}"#)); + assert!(bad_pane.contains("\"error\""), "{bad_pane}"); + + // The token is enforced. + let anon = env.run(&["ctl", &endpoint, "workspace.status"]); + let anon_text = format!("{}{}", stdout_of(&anon), stderr_of(&anon)); + assert!(!anon.status.success() || anon_text.contains("\"error\""), "unauthenticated call must be refused: {anon_text}"); + + // The human CLI reaches the same instance through BILTERM_SOCKET (the subcommand must be the + // first argument, so the token travels in BILTERM_TOKEN_FILE). + let cli = |args: &[&str]| { + let mut cmd = env.bilterm(args); + cmd.env("BILTERM_SOCKET", &endpoint).env("BILTERM_TOKEN_FILE", &token_path); + common::run_with_deadline(cmd, PROCESS_DEADLINE) + }; + let list = cli(&["pane", "list"]); + assert!(list.status.success(), "pane list: {}", stderr_of(&list)); + result_of(&stdout_of(&list)); + let ws = cli(&["workspace", "list"]); + assert!(ws.status.success(), "workspace list: {}", stderr_of(&ws)); + result_of(&stdout_of(&ws)); + let fmt = cli(&["coords", "format"]); + assert!(fmt.status.success(), "coords format: {}", stderr_of(&fmt)); + result_of(&stdout_of(&fmt)); + + // Still alive after the whole script. + assert!(daemon.0.try_wait().expect("poll daemon").is_none(), "daemon died during the session"); +} + +// ---------------------------------------------------------------- 5. real PTY child + +#[test] +fn pty_child_output_reaches_the_screen() { + use bilterm::pane::CompositePane; + use bilterm::pty::PtyConfig; + + let (program, args) = common::shell_command(&common::echo_script("PTY-SMOKE-7391")); + let cfg = PtyConfig { program, args, ..PtyConfig::default() }; + let mut pane = match CompositePane::spawn(1, cfg) { + Ok(p) => p, + Err(e) => { + // Sandboxes without /dev/ptmx (or ConPTY) cannot run this layer; say so loudly. + common::skip(&format!("this platform refused to open a PTY: {e}")); + return; + } + }; + let mut screen = String::new(); + let seen = common::wait_until(Duration::from_secs(30), || { + pane.poll_pty_events(); + let (rows, _) = pane.vt().size(); + screen = (0..rows).map(|r| pane.vt().row_text(r)).collect::>().join("\n"); + screen.contains("PTY-SMOKE-7391") + }); + assert!(seen, "PTY child output never reached the VT screen; screen was: {screen:?}"); +} diff --git a/tests/smooth_and_mainframe_page_scrolling.rs b/tests/smooth_and_mainframe_page_scrolling.rs index e4ac9b6a..97a57c13 100644 --- a/tests/smooth_and_mainframe_page_scrolling.rs +++ b/tests/smooth_and_mainframe_page_scrolling.rs @@ -322,6 +322,7 @@ fn block_page_mode_underlying_vt_row0_and_pa1_or_c_cancel_to_live_tail() { } #[test] +#[cfg_attr(windows, ignore = "drives a POSIX /bin/sh child; the Windows PTY path is covered by tests/smoke_binary_per_os.rs")] fn vt_worker_handle_tracks_block_page_hold_and_advances_across_thread_seam() { use bilterm::pty::PtyConfig; use bilterm::vt::worker::VtWorkerHandle; diff --git a/tests/swarm_wiring.rs b/tests/swarm_wiring.rs index 05c6009b..6d0cb8c0 100644 --- a/tests/swarm_wiring.rs +++ b/tests/swarm_wiring.rs @@ -3,7 +3,8 @@ use bilterm::action::Action; use bilterm::colon_command::parse_colon_command; fn sw(wm: &mut WindowManager, op: SwarmOp) -> Result { - wm.apply(Action::Swarm(op)).map_err(|e| e.to_string())?; + // `apply` only reports failures on the status bar; `apply_automation` (the RPC path) returns them as `Err`. + wm.apply_automation(Action::Swarm(op), None)?; Ok(bilterm::api::json::JsonValue::parse(&wm.take_command_data().unwrap_or_default()).unwrap_or(bilterm::api::json::JsonValue::Null)) } use bilterm::wm::WindowManager; diff --git a/tests/tmux_link_live.rs b/tests/tmux_link_live.rs index d4e21b2e..e785c694 100644 --- a/tests/tmux_link_live.rs +++ b/tests/tmux_link_live.rs @@ -15,6 +15,8 @@ //! Scenario: connect, render check, size check, kill the transport, assert the remote session //! and its counter loop are alive, reconnect, assert continuity (same session, same pane, output //! produced while away is on screen, nothing duplicated). +// A real tmux server behind a /bin/sh transport script: Unix only (does not compile elsewhere). +#![cfg(unix)] use std::path::{Path, PathBuf}; use std::process::Command; diff --git a/tests/video_canvas_tui_and_ffmpeg.rs b/tests/video_canvas_tui_and_ffmpeg.rs index 5a995513..ffa4a2e9 100644 --- a/tests/video_canvas_tui_and_ffmpeg.rs +++ b/tests/video_canvas_tui_and_ffmpeg.rs @@ -38,6 +38,7 @@ fn wait_for_frame(be: &mut FfmpegPipeBackend) -> Option { } #[test] +#[cfg_attr(windows, ignore = "drives a POSIX /bin/sh child; the Windows PTY path is covered by tests/smoke_binary_per_os.rs")] fn ffmpeg_pipe_backend_reads_real_rgba_frames_from_child_stdout() { let mut be = FfmpegPipeBackend::from_command(raw_rgba_child(4, 2, 3), 4, 2); let f = wait_for_frame(&mut be).expect("real frame from pipe"); @@ -49,6 +50,7 @@ fn ffmpeg_pipe_backend_reads_real_rgba_frames_from_child_stdout() { } #[test] +#[cfg_attr(windows, ignore = "drives a POSIX /bin/sh child; the Windows PTY path is covered by tests/smoke_binary_per_os.rs")] fn ffmpeg_pipe_backend_frame_sequence_advances() { let mut be = FfmpegPipeBackend::from_command(raw_rgba_child(4, 2, 5), 4, 2); let first = wait_for_frame(&mut be).expect("first frame"); diff --git a/tests/vt_fuzz_property.rs b/tests/vt_fuzz_property.rs new file mode 100644 index 00000000..3bb1f355 --- /dev/null +++ b/tests/vt_fuzz_property.rs @@ -0,0 +1,366 @@ +//! Property-style fuzzing of the VT parser + screen with a hand-rolled deterministic PRNG +//! (fixed seeds, no extra dependencies; identical streams on every OS / architecture). +//! +//! Inputs: pure random bytes, grammar-shaped escape sequences with hostile parameters, and +//! mutations (bit flips, truncation, duplication, splices) of a corpus of real sequences. +//! Properties, checked after every case: +//! * no panic, and the whole run stays inside a generous time budget (no pathological input); +//! * the screen keeps the geometry it was given and every row has exactly `cols` cells; +//! * the cursor stays inside the screen; +//! * replaying the same bytes with the same chunking gives the identical screen (determinism). +//! +//! History: this file found two char-boundary panics (`src/vt/scan.rs`, `src/vt/codec.rs`), since +//! fixed; the `repro_*` tests below keep them fixed. +//! +//! The parser is fuzzed directly (not through a pane) on purpose: a pane forwards decoded OSC 52 +//! payloads to the system clipboard, which a fuzzer must never touch. + +mod common; + +use std::time::{Duration, Instant}; + +use bilterm::vt::VtParser; +use common::Rng; + +/// Whole-test budget. Debug builds on the slowest CI cell finish in a few seconds; this only +/// trips on a genuine algorithmic blow-up (quadratic reflow, runaway allocation, livelock). +const TIME_BUDGET: Duration = Duration::from_secs(120); + +/// Real-world sequences the mutator starts from. +const CORPUS: &[&[u8]] = &[ + b"\x1b[38;2;85;255;85;48;5;17;1;3;4mstyled\x1b[0m\r\n", + b"\x1b[?1049h\x1b[2J\x1b[H\x1b[?25l alt screen \x1b[?25h\x1b[?1049l", + b"\x1b]0;window title\x07\x1b]2;other title\x1b\\", + b"\x1b]7;file://host/some/dir\x07", + b"\x1b]8;id=x;https://example.com/a\x07link\x1b]8;;\x07", + b"\x1b]52;c;aGVsbG8gd29ybGQ=\x07", + b"\x1b]133;A\x07$ cmd\x1b]133;B\x07\x1b]133;C\x07out\r\n\x1b]133;D;0\x07", + b"\x1b]9;4;1;42\x07\x1b]9;notify body\x07\x1b]777;notify;title;body\x07", + b"\x1b]10;?\x07\x1b]11;?\x1b\\\x1b]4;208;?\x07", + b"\x1bP+q544E;524742\x1b\\\x1bP$qm\x1b\\\x1bP$qr\x1b\\", + b"\x1bPq#0;2;0;0;0#1;2;100;100;0#1~~@@vv@@~~$-\x1b\\", + b"\x1b_Gf=24,s=1,v=1,a=T;AAAA\x1b\\", + b"\x1b[>31u\x1b[>15u\x1b[?u\x1b[<1u\x1b[=5;1u", + b"\x1b[c\x1b[>c\x1b[6n\x1b[?6n\x1b[5n\x1b[18t\x1b[14t\x1b[?2026$p", + b"\x1b[?2004h\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1006h\x1b[?1004h\x1b[?2048h", + b"\x1b[10;20r\x1b[5;5H\x1b[3L\x1b[2M\x1b[4@\x1b[4P\x1b[2X\x1b[1J\x1b[K\x1b[r", + b"\x1b#3double\r\n\x1b#4double\r\n\x1b#6wide\r\n\x1b#5\x1b#8", + b"\x1b(0lqqqk\r\nx x\r\nmqqqj\x1b(B", + b"\x1b7\x1b[999;999H\x1b8\x1bD\x1bM\x1bE\x1bH\x1b[3g\x1bc", + "\u{6f22}\u{5b57} \u{1f525}\u{1f680} e\u{0301} \u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467} \u{feff}\u{202e}rtl".as_bytes(), + b"\tTab\x08\x08BS\rCR\nLF\x0bVT\x0cFF\x07BEL\x0e\x0f\x00NUL\x7fDEL", + b"\x1b[1;2;3;4;5;6;7;8;9;21;22;23;24;25;27;28;29;53;55m\x1b[4:3m\x1b[58:2::255:0:0m\x1b[59m", + b"\x1b[2 q\x1b[6 q\x1b[?12h\x1b[!p\x1b[?5h\x1b[?5l\x1b[?7l\x1b[?7h\x1b[?6h\x1b[?6l", + b"\x1b[200~pasted \x1b[201~", +]; + +const FINALS: &[u8] = b"@ABCDEFGHIJKLMPSTXZ`abcdefghilmnpqrstuxy{|}~"; +const INTERMEDIATES: &[u8] = b" !\"#$%&'()*+,-./"; +const PRIVATE: &[u8] = b"?><="; +const HOSTILE_NUMBERS: &[&str] = &[ + "0", "1", "2", "9", "24", "80", "255", "256", "999", "1049", "2004", "32767", "32768", "65535", "65536", + "2147483647", "2147483648", "4294967295", "4294967296", "18446744073709551616", "999999999999999999999999", + "-1", "", "00000000000000000001", "1e9", "0x10", +]; + +fn corpus(rng: &mut Rng) -> &'static [u8] { + CORPUS[rng.below(CORPUS.len())] +} + +fn gen_csi(rng: &mut Rng, out: &mut Vec) { + out.extend_from_slice(if rng.chance(8) { b"\x9b" } else { b"\x1b[" }); + if rng.chance(3) { + out.push(*rng.pick(PRIVATE)); + } + let max_params = if rng.chance(20) { 200 } else { 6 }; + for i in 0..rng.below(max_params) { + if i > 0 { + out.push(if rng.chance(6) { b':' } else { b';' }); + } + out.extend_from_slice(rng.pick(HOSTILE_NUMBERS).as_bytes()); + } + for _ in 0..rng.below(3).saturating_sub(1) { + out.push(*rng.pick(INTERMEDIATES)); + } + if !rng.chance(12) { + out.push(*rng.pick(FINALS)); // otherwise leave the sequence unterminated + } +} + +fn gen_string_sequence(rng: &mut Rng, out: &mut Vec) { + // OSC, DCS, APC, PM, SOS introducers with payloads of every shape and every terminator. + const INTRODUCERS: [&[u8]; 7] = [b"\x1b]", b"\x1bP", b"\x1b_", b"\x1b^", b"\x1bX", b"\x9d", b"\x90"]; + out.extend_from_slice(INTRODUCERS[rng.below(INTRODUCERS.len())]); + if rng.chance(2) { + out.extend_from_slice(rng.pick(&["0", "2", "4", "7", "8", "9", "10", "11", "52", "133", "777", "1337", "99999"]).as_bytes()); + out.push(b';'); + } + let len = if rng.chance(25) { 4096 + rng.below(8192) } else { rng.below(48) }; + for _ in 0..len { + out.push(match rng.below(10) { + 0 => rng.byte(), + 1 => b';', + 2 => b'?', + 3 => b'=', + _ => b'A' + (rng.below(58) as u8), + }); + } + match rng.below(5) { + 0 => out.push(0x07), + 1 => out.extend_from_slice(b"\x1b\\"), + 2 => out.push(0x9c), + 3 => out.push(0x18), // CAN aborts the string + _ => {} // unterminated + } +} + +fn gen_text(rng: &mut Rng, out: &mut Vec) { + for _ in 0..rng.below(40) { + match rng.below(12) { + 0 => out.extend_from_slice("\u{6f22}".as_bytes()), + 1 => out.extend_from_slice("\u{1f680}".as_bytes()), + 2 => out.extend_from_slice("e\u{0301}\u{200d}".as_bytes()), + 3 => out.push(*rng.pick(b"\r\n\t\x08\x07\x0b\x0c\x0e\x0f\x00\x7f")), + 4 => out.push(0x80 | (rng.byte() & 0x7f)), // stray continuation / invalid UTF-8 + 5 => out.extend_from_slice(&[0xf0, 0x9f]), // truncated 4-byte scalar + _ => out.push(b' ' + (rng.below(95) as u8)), + } + } +} + +fn gen_structured(rng: &mut Rng, target_len: usize) -> Vec { + let mut out = Vec::with_capacity(target_len + 64); + while out.len() < target_len { + match rng.below(10) { + 0..=3 => gen_csi(rng, &mut out), + 4..=5 => gen_string_sequence(rng, &mut out), + 6 => { + out.push(0x1b); + out.push(rng.byte() & 0x7f); + } + 7 => out.extend_from_slice(corpus(rng)), + _ => gen_text(rng, &mut out), + } + } + out +} + +fn mutate(rng: &mut Rng, seed: &[u8]) -> Vec { + let mut v = seed.to_vec(); + for _ in 0..1 + rng.below(6) { + if v.is_empty() { + v.push(rng.byte()); + continue; + } + let i = rng.below(v.len()); + match rng.below(8) { + 0 => v[i] ^= 1 << rng.below(8), + 1 => v[i] = rng.byte(), + 2 => v.truncate(i), + 3 => { + let j = (i + 1 + rng.below(16)).min(v.len()); + let dup = v[i..j].to_vec(); + for _ in 0..1 + rng.below(40) { + v.splice(i..i, dup.iter().copied()); + } + } + 4 => { + let other = corpus(rng); + let k = rng.below(other.len() + 1); + v.splice(i..i, other[..k].iter().copied()); + } + 5 => { + v.remove(i); + } + 6 => v.insert(i, *rng.pick(b"\x1b\x9b\x9d\x90\x07\x18\x1a;:?")), + _ => { + let j = rng.below(v.len()); + v.swap(i, j); + } + } + } + v +} + +/// Feeds `input` in PRNG-chosen chunks (1 byte up to the whole thing), draining query replies. +fn feed(vt: &mut VtParser, input: &[u8], rng: &mut Rng) -> usize { + let mut i = 0; + let mut replies = 0; + while i < input.len() { + let step = match rng.below(4) { + 0 => 1, + 1 => 1 + rng.below(7), + 2 => 1 + rng.below(256), + _ => input.len() - i, + } + .min(input.len() - i); + vt.process(&input[i..i + step]); + i += step; + replies += vt.take_query_responses().iter().map(Vec::len).sum::(); + } + replies +} + +fn check_invariants(vt: &VtParser, rows: u16, cols: u16, what: &str) { + assert_eq!(vt.size(), (rows, cols), "{what}: parser changed its own geometry"); + let snap = vt.snapshot(); + assert_eq!((snap.rows, snap.cols), (rows, cols), "{what}: snapshot geometry"); + assert_eq!(snap.grid_rows.len(), usize::from(rows), "{what}: snapshot row count"); + for (r, row) in snap.grid_rows.iter().enumerate() { + assert_eq!(row.cells.len(), usize::from(cols), "{what}: row {r} cell count"); + } + let (cr, cc) = vt.cursor_position(); + assert!(cr < rows && cc <= cols, "{what}: cursor ({cr},{cc}) outside {rows}x{cols}"); + // Text extraction must not panic on whatever the grid now holds. + let _ = snap.contents(); + for r in 0..rows { + let _ = vt.row_text(r); + } +} + +fn describe(kind: &str, seed: u64, case: usize, input: &[u8]) -> String { + let head: String = input.iter().take(96).map(|b| format!("{b:02x}")).collect(); + format!("{kind} seed={seed} case={case} len={} head={head}", input.len()) +} + +fn run_cases(kind: &str, seeds: &[u64], cases: usize, mut make: impl FnMut(&mut Rng) -> Vec) { + let started = Instant::now(); + let mut total_bytes = 0usize; + for &seed in seeds { + let mut rng = Rng::new(seed); + let (rows, cols) = (2 + rng.below(40) as u16, 2 + rng.below(160) as u16); + let mut vt = VtParser::new(rows, cols, 200); + for case in 0..cases { + let input = make(&mut rng); + total_bytes += input.len(); + let what = describe(kind, seed, case, &input); + { + let replies = feed(&mut vt, &input, &mut rng); + // A terminal answers queries; it must not amplify input into unbounded output. + assert!(replies <= 64 * input.len() + 4096, "{what}: {replies} reply bytes for {} input bytes", input.len()); + check_invariants(&vt, rows, cols, &what); + } + assert!(started.elapsed() < TIME_BUDGET, "{what}: exceeded the {TIME_BUDGET:?} budget after {total_bytes} bytes"); + } + } +} + +#[test] +fn random_bytes_never_panic_or_corrupt_the_screen() { + run_cases("random", &[1, 2, 3, 0xDEAD_BEEF, 0x5EED_5EED_5EED], 60, |rng| { + let len = rng.below(2048); + rng.bytes(len) + }); +} + +#[test] +fn grammar_shaped_escape_sequences_never_panic_or_corrupt_the_screen() { + run_cases("structured", &[11, 12, 13, 14, 0xC0FFEE, 0xFEED_FACE], 60, |rng| { + let len = 64 + rng.below(3000); + gen_structured(rng, len) + }); +} + +#[test] +fn mutated_real_sequences_never_panic_or_corrupt_the_screen() { + run_cases("mutated", &[21, 22, 23, 24, 25, 0x0BAD_C0DE], 150, |rng| { + let mut base = corpus(rng).to_vec(); + if rng.chance(3) { + base.extend_from_slice(corpus(rng)); + } + mutate(rng, &base) + }); +} + +#[test] +fn fuzzing_interleaved_with_resizes_keeps_the_grid_consistent() { + let started = Instant::now(); + for seed in [31u64, 32, 33, 34] { + let mut rng = Rng::new(seed); + let (mut rows, mut cols) = (24u16, 80u16); + let mut vt = VtParser::new(rows, cols, 500); + for case in 0..80 { + let input = if rng.chance(2) { + let len = 32 + rng.below(600); + gen_structured(&mut rng, len) + } else { + let base = corpus(&mut rng); + mutate(&mut rng, base) + }; + let what = describe("resize", seed, case, &input); + { + feed(&mut vt, &input, &mut rng); + if rng.chance(3) { + // Ask for degenerate 1-cell grids too: the parser documents a 2x2 minimum. + let (want_rows, want_cols) = (1 + rng.below(60) as u16, 1 + rng.below(250) as u16); + vt.set_size(want_rows, want_cols); + rows = want_rows.max(2); + cols = want_cols.max(2); + } + if rng.chance(5) { + vt.scroll_up(rng.below(40)); + } + if rng.chance(5) { + vt.scroll_down(rng.below(40)); + } + if rng.chance(9) { + vt.reset_scrollback(); + } + check_invariants(&vt, rows, cols, &what); + } + assert!(started.elapsed() < TIME_BUDGET, "{what}: exceeded the {TIME_BUDGET:?} budget"); + } + } +} + +#[test] +fn identical_input_and_chunking_give_an_identical_screen() { + for seed in [41u64, 42, 43, 44, 45, 46] { + let mut gen_rng = Rng::new(seed); + let input = gen_structured(&mut gen_rng, 6000); + let run = || { + let mut vt = VtParser::new(30, 100, 300); + let mut chunk_rng = Rng::new(seed ^ 0xA5A5); + feed(&mut vt, &input, &mut chunk_rng); + (vt.snapshot().contents(), vt.cursor_position(), vt.is_alternate_screen(), vt.window_title().map(str::to_string)) + }; + assert_eq!(run(), run(), "seed {seed}: the parser is not deterministic"); + } +} + +#[test] +fn one_megabyte_of_hostile_input_is_processed_in_bounded_time() { + let mut rng = Rng::new(0x0B16_B00B); + let mut input = Vec::with_capacity(1 << 20); + while input.len() < (1 << 20) { + let chunk = gen_structured(&mut rng, 4096); + input.extend_from_slice(&chunk); + } + let mut vt = VtParser::new(50, 200, 1000); + let started = Instant::now(); + for chunk in input.chunks(8192) { + vt.process(chunk); + let _ = vt.take_query_responses(); + } + let took = started.elapsed(); + check_invariants(&vt, 50, 200, "1 MiB hostile stream"); + assert!(took < Duration::from_secs(60), "1 MiB took {took:?} (budget 60 s): throughput collapsed"); + assert_eq!(vt.total_bytes_processed(), input.len(), "every byte must be accounted for"); +} + +// ---------------------------------------------------------------- minimal reproducers (see module docs) + +#[test] +fn repro_escape_followed_by_non_ascii_character_must_not_panic() { + let mut vt = VtParser::new(24, 80, 0); + vt.process("\x1b\u{e9}".as_bytes()); // ESC, then U+00E9 + vt.process("ok \x1b\u{1f680} still alive\r\n".as_bytes()); + check_invariants(&vt, 24, 80, "ESC + non-ASCII"); +} + +#[test] +fn repro_xtgettcap_with_non_ascii_payload_must_not_panic() { + let mut vt = VtParser::new(24, 80, 0); + vt.process("\x1bP+q5\u{e9}4\x1b\\".as_bytes()); // DCS + q <5, U+00E9, 4> ST: even byte length, odd boundary + check_invariants(&vt, 24, 80, "XTGETTCAP + non-ASCII"); +} diff --git a/tests/vt_worker_and_snapshot_seam.rs b/tests/vt_worker_and_snapshot_seam.rs index f84f40a9..aba1e674 100644 --- a/tests/vt_worker_and_snapshot_seam.rs +++ b/tests/vt_worker_and_snapshot_seam.rs @@ -305,6 +305,7 @@ fn tmux_control_subprocess_polls_into_window_manager() { } #[test] +#[cfg_attr(windows, ignore = "drives a POSIX /bin/sh child; the Windows PTY path is covered by tests/smoke_binary_per_os.rs")] fn floating_prompt_submit_command_executes_in_live_shell_pty() { use bilterm::input::handle_key_event; use bilterm::pane::CompositePane;