test(security-scanner): add TTY prompt tests using Bun.Terminal

Merged
RO
3mo ago
+8137-2588

Summary

  • Integrate TTY testing into the existing security scanner matrix tests
  • Uses the new Bun.Terminal API to create a pseudo-terminal environment for testing interactive prompts

Changes

test/cli/install/bun-security-scanner-matrix-runner.ts:

  • Add hasTTY and ttyResponse options to SecurityScannerTestOptions interface
  • When hasTTY is true, use Bun.Terminal to spawn the process in a PTY environment
  • Add TTY-specific assertions to verify:
    • The "Continue anyway? [y/N]" prompt appears
    • "Continuing with installation..." message when user responds 'y'
    • "Installation cancelled." message when user responds 'n'
    • Non-TTY mode shows "Security warnings found. Cannot prompt for confirmation (no TTY)."
  • Add new "TTY warning prompts" describe block with tests covering:
    • install and add commands
    • local and npm scanner types
    • y and n user responses
  • Update assertions to correctly handle the case where installation proceeds after user accepts warning

Example

// In TTY mode, use Bun.Terminal to handle interactive prompts
await using terminal = new Bun.Terminal({
  cols: 80,
  rows: 24,
  data(_term, data) {
    const text = new TextDecoder().decode(data);
    errAndOut += text;
    
    // When we see the prompt, send the configured response
    if (!responseSent && errAndOut.includes("Continue anyway? [y/N]")) {
      responseSent = true;
      terminal.write(ttyResponse + "\n");
    }
  },
});

await using proc = Bun.spawn(cmd, {
  cwd: dir,
  env: bunEnv,
  terminal,
});

Test plan

  • All 8 new TTY tests pass with bun bd test test/cli/install/bun-security-scanner-matrix-without-node-modules.test.ts -t "TTY"
  • Existing matrix tests still pass
  • Tests are skipped on Windows (PTY not available)

šŸ¤– Generated with Claude Code