···11# pty — Development Guide
2233-A persistent terminal session manager. Run long-lived processes, detach, reconnect later — from any machine over SSH.
33+A persistent terminal session manager. Run long-lived processes, detach, reconnect later — from any machine over SSH. The npm package name is `ptym`; the CLI command remains `pty`.
4455## Objectives
66···2020## Quick Reference
21212222```sh
2323-npm install # install dependencies
2424-npm run typecheck # typecheck with tsc (no emit)
2525-npm test # run all tests once
2626-npm run test:watch # run tests in watch mode
2323+npm install # install dependencies
2424+npm run typecheck # typecheck with tsc (no emit)
2525+npm test # run all tests once
2626+npm run test:watch # run tests in watch mode
2727+npm run verify-docs # run executable examples in docs/testing.md
27282829# Usage (during development)
2930npx tsx src/cli.ts run <name> -- <command> [args...]
···3435npx tsx src/cli.ts list
3536npx tsx src/cli.ts restart <name>
3637npx tsx src/cli.ts kill <name>
3838+npx tsx src/cli.ts test # run tests via vitest
3939+npx tsx src/cli.ts test -t "pattern" # run matching tests
3740```
38413942Detach from any attached/following session with **Ctrl+\\**. Press Ctrl+\\ twice quickly to send it to the process.
···147150148151## Testing
149152150150-Tests use **vitest** and live in `tests/`.
153153+Tests use **vitest** and live in `tests/`. The `ptym/testing` library (`src/testing/`) provides a `Session` class used by the tests — see [docs/testing.md](docs/testing.md) for the full API.
151154152155- `protocol.test.ts` — Unit tests for packet encoding, decoding, and streaming reassembly (partial reads, split packets, large payloads)
156156+- `keys.test.ts` — Unit tests for key name resolution (`resolveKey`, `parseSeqValue`)
157157+- `sanitize.test.ts` — Unit tests for terminal sanitization escape sequences
153158- `integration.test.ts` — Full integration tests that spawn real PTY sessions, connect clients via sockets, and verify behavior
154154-- `screenshot.test.ts` — Screenshot-based tests that capture terminal state (ANSI and plain text) and assert on visual output from real programs (vim, nano, ls, bash). Covers control characters, terminal resize, alternate screen buffer, multiple clients, unicode, and daemon spawning.
159159+- `screenshot.test.ts` — Screenshot-based tests using `Session.server()` that capture terminal state (ANSI and plain text) and assert on visual output from real programs (vim, nano, ls, bash). Covers control characters, terminal resize, alternate screen buffer, multiple clients, unicode, and daemon spawning.
160160+- `tui.test.ts` — Interactive TUI tests using `Session.spawn()` that drive the `pty` session manager UI: session list rendering, filtering, keyboard navigation, attach/detach cycles, and the create wizard.
155161156162All tests run in `/tmp/` directories to avoid polluting the project folder (e.g., vim swap files). Integration and screenshot tests use real processes and real Unix sockets. Each test creates a uniquely-named session and cleans up afterward. There is a `200ms` delay in some tests to allow xterm-headless to process async writes before checking screen state — this is a known characteristic of xterm's write pipeline, not a flaky test.
157163158164### Running tests
159165160166```sh
161161-npm test # run once
162162-npm run test:watch # watch mode
167167+npm test # run once (or: pty test)
168168+npm run test:watch # watch mode (or: pty test watch)
163169npx vitest run -t "peek" # run tests matching "peek"
170170+npm run verify-docs # run executable examples in docs/testing.md
164171```
165172166173### node-pty on macOS
···180187 client.ts attach() and peek() functions
181188 protocol.ts Packet types, encoding, decoding, PacketReader
182189 sessions.ts Session discovery, socket/PID file management
190190+ keys.ts Key name resolution (e.g. "ctrl+c" → bytes)
191191+ spawn.ts Daemon spawning logic
192192+ tui/ Interactive session manager UI
193193+ testing/ Testing library (exported as ptym/testing)
194194+ index.ts Public re-exports
195195+ session.ts Session class (spawn + server backends)
196196+ screenshot.ts Screenshot capture helper
197197+ types.ts Screenshot, SpawnOptions, ServerOptions interfaces
183198tests/
184199 protocol.test.ts
200200+ keys.test.ts
201201+ sanitize.test.ts
185202 integration.test.ts
186203 screenshot.test.ts
204204+ tui.test.ts
205205+docs/
206206+ testing.md Testing library documentation (with executable examples)
207207+scripts/
208208+ verify-docs.ts Extracts and runs doc examples via vitest
187209completions/
188210 pty.bash Bash tab completion
189211 pty.zsh Zsh tab completion