
# Copyright (c) 2021-2026, PostgreSQL Global Development Group

use strict;
use warnings FATAL => 'all';

use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
use Data::Dumper;

# If we don't have IO::Pty, forget it, because IPC::Run depends on that
# to support pty connections
eval { require IO::Pty; };
if ($@)
{
	plan skip_all => 'IO::Pty is needed to run this test';
}

# start a new server
my $node = PostgreSQL::Test::Cluster->new('main');
$node->init;
$node->start;

for (my $i = 1; $i <= 100; $i++)
{
my $h = $node->interactive_psql('postgres');

# set the pty's window size to known values
# (requires undesirable chumminess with the innards of IPC::Run)
for my $pty (values %{ $h->{run}->{PTYS} })
{
	$pty->set_winsize(24, 80);
}

my $ptycheck_out = $h->query_until(
	qr/PTYCHECK_DONE\r?$/m,
	q{\! python3 -c "import fcntl,termios,struct; r,c,hp,wp=struct.unpack('HHHH', fcntl.ioctl(1, termios.TIOCGWINSZ, bytes(8))); print(f'ioctl1 rows={r} cols={c}')"}
	. "\n\\echo 'PTYCHECK_DONE'\n");
diag("psql PTY check output:\n$ptycheck_out");

ok($ptycheck_out !~ m/rows=0/, "non-zero size");

$h->quit or die "psql returned $?";
}

done_testing();
