|
| 1 | +// Copyright (C) 2026 Toit contributors. |
| 2 | +// Use of this source code is governed by a Zero-Clause BSD license that can |
| 3 | +// be found in the tests/LICENSE file. |
| 4 | +
|
| 5 | +import fs |
| 6 | +import host.directory |
| 7 | +import host.file |
| 8 | +import host.pipe |
| 9 | +import system |
| 10 | + |
| 11 | +/** |
| 12 | +A tmux session wrapper for testing interactive shell completions. |
| 13 | +*/ |
| 14 | +class Tmux: |
| 15 | + /** The socket name, unique per session to avoid server conflicts. */ |
| 16 | + socket-name/string |
| 17 | + |
| 18 | + constructor .socket-name --shell-cmd/List --width/int=200 --height/int=50: |
| 19 | + args := [ |
| 20 | + "tmux", |
| 21 | + "-L", socket-name, // Use a dedicated server socket. |
| 22 | + "new-session", |
| 23 | + "-d", // Detached. |
| 24 | + "-s", socket-name, |
| 25 | + "-x", "$width", |
| 26 | + "-y", "$height", |
| 27 | + ] + shell-cmd |
| 28 | + exit-code := pipe.run-program args |
| 29 | + if exit-code != 0: |
| 30 | + throw "tmux new-session failed with exit code $exit-code for shell '$shell-cmd'" |
| 31 | + // Wait for the shell to initialize. |
| 32 | + send-line "echo tmux-ready" |
| 33 | + wait-for "tmux-ready" |
| 34 | + |
| 35 | + /** |
| 36 | + Sends keystrokes to the tmux session. |
| 37 | + Each argument is a tmux key name (e.g. "Enter", "Tab", "C-c"). |
| 38 | + */ |
| 39 | + send-keys keys/List -> none: |
| 40 | + pipe.run-program ["tmux", "-L", socket-name, "send-keys", "-t", socket-name] + keys |
| 41 | + |
| 42 | + /** Sends text followed by Enter. */ |
| 43 | + send-line text/string -> none: |
| 44 | + send-keys [text, "Enter"] |
| 45 | + |
| 46 | + /** Sends Ctrl-C to cancel the current line, then waits for the shell to be ready. */ |
| 47 | + cancel -> none: |
| 48 | + send-keys ["C-c"] |
| 49 | + // Echo a marker and wait for it so we know the shell is ready. |
| 50 | + marker := "ready-$Time.monotonic-us" |
| 51 | + send-line "echo $marker" |
| 52 | + wait-for marker |
| 53 | + |
| 54 | + /** Captures the current pane content as a string. */ |
| 55 | + capture -> string: |
| 56 | + return pipe.backticks ["tmux", "-L", socket-name, "capture-pane", "-t", socket-name, "-p"] |
| 57 | + |
| 58 | + /** |
| 59 | + Waits until the pane contains the given $expected string, or throws on timeout. |
| 60 | + */ |
| 61 | + wait-for expected/string --timeout-ms/int=10000 -> none: |
| 62 | + deadline := Time.monotonic-us + timeout-ms * 1000 |
| 63 | + delay-ms := 10 |
| 64 | + while Time.monotonic-us < deadline: |
| 65 | + content := capture |
| 66 | + if content.contains expected: return |
| 67 | + sleep --ms=delay-ms |
| 68 | + delay-ms = min 500 (delay-ms * 2) |
| 69 | + content := capture |
| 70 | + throw "Timed out waiting for '$expected' in tmux pane. Content:\n$content" |
| 71 | + |
| 72 | + /** Kills the tmux server (and its session). */ |
| 73 | + close -> none: |
| 74 | + catch: pipe.run-program ["tmux", "-L", socket-name, "kill-server"] |
| 75 | + |
| 76 | +// Unique session prefix for this test run. |
| 77 | +session-id_ := 0 |
| 78 | + |
| 79 | +next-session-name_ -> string: |
| 80 | + session-id_++ |
| 81 | + return "completion-test-$Time.monotonic-us-$session-id_" |
| 82 | + |
| 83 | +with-tmp-dir [block]: |
| 84 | + tmpdir := directory.mkdtemp "/tmp/completion-shell-test-" |
| 85 | + try: |
| 86 | + block.call tmpdir |
| 87 | + finally: |
| 88 | + directory.rmdir --recursive --force tmpdir |
| 89 | + |
| 90 | +has-command_ name/string -> bool: |
| 91 | + exit-code := pipe.run-program ["which", name] |
| 92 | + return exit-code == 0 |
| 93 | + |
| 94 | +/** |
| 95 | +Compiles the test app binary and creates OptionPath test artifacts. |
| 96 | +Returns the path to the compiled binary. |
| 97 | +The $tmpdir must already exist. |
| 98 | +*/ |
| 99 | +setup-test-binary_ tmpdir/string -> string: |
| 100 | + test-dir := fs.dirname system.program-path |
| 101 | + app-source := "$test-dir/completion_shell_test_app.toit" |
| 102 | + binary := "$tmpdir/fleet" |
| 103 | + if system.platform == system.PLATFORM-WINDOWS: |
| 104 | + binary = "$tmpdir/fleet.exe" |
| 105 | + print "Compiling test app..." |
| 106 | + pipe.run-program ["toit", "compile", "-o", binary, app-source] |
| 107 | + print "Binary compiled: $binary" |
| 108 | + |
| 109 | + // Create artifacts for OptionPath completion testing. |
| 110 | + file.write-contents --path="$tmpdir/xfirmware.bin" "" |
| 111 | + directory.mkdir "$tmpdir/xreleases" |
| 112 | + |
| 113 | + return binary |
| 114 | + |
0 commit comments