Files
ctask/cmd/persistent_test.go
T
typebasedio c204d87b47 polish(v0.5.3): v0.4 lease-prompt hint, invocation-name in user-facing commands, smoke-checklist fixes
Three independent v0.5.3 polish items surfaced during manual WSL smoke testing,
bundled into one commit since they're all string/UX touches with no behavior change.

1. v0.4 lease-prompt tightening (`internal/session/{lease,run,run_preflight}.go`,
   `cmd/entry.go`): when ctask is about to enter direct mode on a workspace that
   already has a live tmux session, the "Continue anyway?" prompt now suggests
   `ctask attach <slug>` as the reattach path. Threaded via
   `PreflightOpts.ActiveLeaseHint` / `LaunchOpts.ActiveLeaseHint`; computed in
   `cmd/entry.go::directModeTmuxHint` (best-effort: silent when no tmux on PATH
   or no session for the workspace). Closes the footgun where a user forgot to
   `export CTASK_SESSION_MODE=persistent` in a second terminal and hit the v0.4
   coexistence prompt without realizing tmux passive-reattach was the intended
   path.

2. Invocation-name in user-facing command hints (`cmd/invocation.go` new,
   `cmd/{persistent,entry,resume,doctor}.go`): the binary name printed in
   bypass / restore / "create one with" suggestions now reflects
   `filepath.Base(os.Args[0])` instead of a hard-coded "ctask". Local-build
   PowerShell users running `.\ctask.exe` see `ctask.exe new <ws> --direct`,
   matching what they need to type. Installed contexts continue to see `ctask`.
   Test seam (`invocationNameOverride`) pins the name to "ctask" in unit tests
   so substring assertions stay stable across Go test binary names. Descriptive
   prose ("ctask persistent mode requires...") and the ssh-remote hint
   (`ssh -t <host> ctask <subcmd>`) intentionally keep the literal "ctask" —
   they refer to the program identity / remote invocation, not the local
   command form. Affected tests: `cmd/{persistent,resume}_test.go` tightened to
   check the full `"<binary> <subcmd> <workspace> --direct"` form.

3. Smoke-checklist fixes (`docs/.../2026-05-08-v0.5.3-smoke-test-checklist.md`):
   six issues caught during the run -- S2 now exports CTASK_SESSION_MODE in
   both terminals (previously only WSL-A had it, which routed WSL-B's
   secondary resume through direct mode instead of passive reattach); O3/A2
   tmux-ls expectations corrected (tmux doesn't emit a literal "(detached)"
   token); P2 expected behavior rewritten (passive reattach detach returns to
   prompt immediately -- AttachExisting calls only shell.AttachSession with
   no PollSessionEnd, the owner is responsible for finalize); A1 no longer
   asks for Ctrl-C in WSL-B; A6 path now uses a glob (was hardcoded to the
   checklist's authoring date, broke on v0.5.1 local-time directory naming);
   M1 PATH-hide rewritten to `$HOME/.local/bin` only (was `:/bin` which is a
   symlink to /usr/bin on usrmerge systems, did not hide tmux) and uses
   `command -v` instead of `which` (which is itself in /usr/bin, unreachable
   under the minimal PATH); M3/M4 reordered so the no-workspace verification
   runs after PATH is restored (was silently failing under minimal PATH);
   T1 wording made "substring match" explicit; T2 wording made N/A
   unambiguous; section 10 split into 10a-10f, each a separate input, to
   work around PSReadLine multi-line paste parsing.
2026-05-14 18:22:27 -04:00

169 lines
5.1 KiB
Go

package cmd
import (
"errors"
"os"
"runtime"
"strings"
"testing"
"time"
"github.com/warrenronsiek/ctask/internal/session"
"github.com/warrenronsiek/ctask/internal/shell"
)
// withTTYCheck swaps the package-level isTTYCheck for the duration of the
// test. Tests that exercise refusal paths must NOT run in parallel — they
// mutate a package global.
func withTTYCheck(t *testing.T, fn func() bool) {
t.Helper()
orig := isTTYCheck
isTTYCheck = fn
t.Cleanup(func() { isTTYCheck = orig })
}
// withInvocationName pins the binary name surfaced in user-facing hints
// to a fixed value (typically "ctask") for the duration of the test, so
// substring assertions against rendered hints stay stable regardless of
// the Go test binary's name. Must NOT run in parallel — mutates a
// package global.
func withInvocationName(t *testing.T, name string) {
t.Helper()
orig := invocationNameOverride
invocationNameOverride = name
t.Cleanup(func() { invocationNameOverride = orig })
}
func TestPreflightRefusesNativeWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("native-Windows refusal applies only on Windows")
}
had := os.Getenv("WSL_DISTRO_NAME")
os.Unsetenv("WSL_DISTRO_NAME")
t.Cleanup(func() {
if had != "" {
os.Setenv("WSL_DISTRO_NAME", had)
}
})
_, err := preflightPersistentEntry("resume")
if err == nil {
t.Fatal("expected refusal on native Windows")
}
if !strings.Contains(err.Error(), "tmux") || !strings.Contains(err.Error(), "WSL") {
t.Errorf("expected tmux+WSL message: %v", err)
}
}
func TestPreflightRefusesNestedTmux(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("nested-tmux check runs on Unix paths only in this test")
}
withTTYCheck(t, func() bool { return true })
os.Setenv("TMUX", "/tmp/tmux-1000/default,1234,0")
t.Cleanup(func() { os.Unsetenv("TMUX") })
_, err := preflightPersistentEntry("resume")
if err == nil {
t.Fatal("expected refusal when $TMUX is set")
}
if !strings.Contains(err.Error(), "already inside tmux") {
t.Errorf("expected nested-tmux message: %v", err)
}
}
func TestPreflightRefusesNonTTY(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("focus this case on Unix; Windows TTY semantics covered by manual smoke")
}
os.Unsetenv("TMUX")
withTTYCheck(t, func() bool { return false })
withInvocationName(t, "ctask")
_, err := preflightPersistentEntry("resume")
if err == nil {
t.Fatal("expected refusal when not a TTY")
}
if !strings.Contains(err.Error(), "interactive terminal") {
t.Errorf("expected interactive-terminal message: %v", err)
}
if !strings.Contains(err.Error(), "ssh -t") {
t.Errorf("error should mention ssh -t: %v", err)
}
if !strings.Contains(err.Error(), "ctask resume <workspace> --direct") {
t.Errorf("error should mention command-specific bypass form: %v", err)
}
}
func TestPreflightCommandNameRendersInHints(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("path-based check covered on Unix")
}
os.Unsetenv("TMUX")
withTTYCheck(t, func() bool { return false })
withInvocationName(t, "ctask")
_, err := preflightPersistentEntry("attach")
if err == nil || !strings.Contains(err.Error(), "ctask attach <workspace> --direct") {
t.Errorf("commandName must appear in bypass hint: %v", err)
}
}
func TestPreflightTmuxNotFound(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("PATH manipulation applies on Unix here")
}
os.Unsetenv("TMUX")
withTTYCheck(t, func() bool { return true })
orig := os.Getenv("PATH")
t.Cleanup(func() { os.Setenv("PATH", orig) })
os.Setenv("PATH", "")
_, err := preflightPersistentEntry("resume")
if err == nil {
t.Fatal("expected refusal when tmux missing")
}
if !strings.Contains(err.Error(), "tmux is not installed") {
t.Errorf("expected install-hint message: %v", err)
}
}
// Confirm the helper returns the validated tmux path on the happy path so
// callers can pass it through LaunchOpts.TmuxPath without re-resolving.
func TestPreflightSuccessReturnsTmuxPath(t *testing.T) {
if _, _, err := shell.LookupTmux(); err != nil {
if errors.Is(err, shell.ErrTmuxNotFound) || errors.Is(err, shell.ErrTmuxTooOld) {
t.Skip("tmux not adequate on this host")
}
}
if runtime.GOOS == "windows" {
t.Skip("happy-path test on WSL/Linux only")
}
os.Unsetenv("TMUX")
withTTYCheck(t, func() bool { return true })
tmuxPath, err := preflightPersistentEntry("resume")
if err != nil {
t.Fatalf("expected success, got %v", err)
}
if tmuxPath == "" {
t.Error("expected non-empty tmuxPath on success")
}
}
func TestConfirmFreshRemoteAdoptionRefusesOnNonTTY(t *testing.T) {
wsDir := t.TempDir()
// Write a fresh remote lease so the prompt has data to display.
other := "remote-host-xyz"
l := &session.Lease{
SessionID: "x", Hostname: other,
StartedAt: time.Now().UTC(), LastHeartbeatAt: time.Now().UTC(),
}
if err := session.WriteLease(session.LeasePath(wsDir), l); err != nil {
t.Fatalf("WriteLease: %v", err)
}
withTTYCheck(t, func() bool { return false })
err := confirmFreshRemoteAdoption(wsDir)
if err == nil {
t.Fatal("expected refusal on non-TTY")
}
if !strings.Contains(err.Error(), other) {
t.Errorf("error should name the remote host: %v", err)
}
}