package shell import ( "bytes" "context" "io" "strings" "testing" "time" ) import ( "os" "codespace/internal/util" "github.com/creack/pty" ) func startTestShell(t *testing.T) (*LocalManager, io.WriteCloser, Subscription) { t.Helper() mgr := NewManager("bash", []string{"-i"}) root := t.TempDir() if err := mgr.Start("test-ws", root); err != nil { t.Fatalf("Start failed: %v", err) } stdin, err := mgr.Stdin("test-ws") if err != nil { t.Fatalf("Stdin failed: %v", err) } sub, err := mgr.Subscribe("test-ws") if err != nil { t.Fatalf("Subscribe failed: %v", err) } t.Cleanup(func() { sub.Close() _ = mgr.Stop("test-ws") }) return mgr, stdin, sub } func waitForMarker(t *testing.T, sub Subscription, stdin io.Writer, input, marker string) string { t.Helper() if _, err := stdin.Write([]byte(input)); err != nil { t.Fatalf("failed to write to stdin: %v", err) } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var out bytes.Buffer for { select { case chunk, ok := <-sub.Output(): if !ok { t.Fatalf("subscription closed before seeing %q; output so far:\n%s", marker, out.String()) } out.Write(chunk) if strings.Contains(out.String(), marker) { return out.String() } case <-ctx.Done(): t.Fatalf("timed out waiting for %q; output so far:\n%s", marker, out.String()) } } } func TestShellPTYIsRealTerminal(t *testing.T) { mgr, stdin, sub := startTestShell(t) _ = mgr out := waitForMarker(t, sub, stdin, "echo HELLO_PTY_TEST\n", "HELLO_PTY_TEST") if !strings.Contains(out, "echo HELLO_PTY_TEST") { t.Errorf("expected command echo in PTY output, got:\n%s", out) } if !strings.Contains(out, "HELLO_PTY_TEST") { t.Errorf("expected command output in PTY output, got:\n%s", out) } } func TestShellPTYTermEnv(t *testing.T) { _, stdin, sub := startTestShell(t) out := waitForMarker(t, sub, stdin, "echo \"$TERM\"\n", "xterm-256color") if !strings.Contains(out, "xterm-256color") { t.Errorf("expected TERM=xterm-256color in output, got:\n%s", out) } } func TestShellResizeUpdatesPTYSize(t *testing.T) { mgr, stdin, sub := startTestShell(t) _ = sub f, ok := stdin.(*os.File) if !ok { t.Fatal("stdin is not a pty file") } if err := mgr.Resize("test-ws", 120, 40); err != nil { t.Fatalf("Resize failed: %v", err) } rows, cols, err := pty.Getsize(f) if err != nil { t.Fatalf("Getsize failed: %v", err) } if cols != 120 || rows != 40 { t.Errorf("expected cols=120 rows=40, got cols=%d rows=%d", cols, rows) } } func TestShellResizeRejectsInvalidSize(t *testing.T) { mgr, _, _ := startTestShell(t) if err := mgr.Resize("test-ws", 0, 40); err == nil { t.Fatal("expected error for invalid size") } else if util.CodeOf(err) != util.CodeBadRequest { t.Errorf("expected CodeBadRequest, got %v", util.CodeOf(err)) } } func TestShellResizeRejectsMissingSession(t *testing.T) { mgr := NewManager("bash", []string{"-i"}) if err := mgr.Resize("missing-ws", 80, 24); err == nil { t.Fatal("expected error for missing session") } else if util.CodeOf(err) != util.CodeNotFound { t.Errorf("expected CodeNotFound, got %v", util.CodeOf(err)) } }