use strict;
use warnings;
use TestLib;
use Test::More tests => 9;

program_help_ok('pg_recvlogical');
program_version_ok('pg_recvlogical');
program_options_handling_ok('pg_recvlogical');


my $tempdir = tempdir;
start_test_server $tempdir;

open HBA, ">>$tempdir/pgdata/pg_hba.conf";
print HBA "local replication all trust\n";
print HBA "host replication all 127.0.0.1/32 trust\n";
print HBA "host replication all ::1/128 trust\n";
close HBA;
system_or_bail 'pg_ctl', '-s', '-D', "$tempdir/pgdata", 'reload';

open HBA, ">>$tempdir/pgdata/pg_hba.conf";
print HBA "local replication all trust\n";
close HBA;

open CONF, ">>$tempdir/pgdata/postgresql.conf";
print CONF "max_wal_senders = 1\n";
print CONF "max_replication_slots = 2\n";
print CONF "wal_level = logical\n";
close CONF;

restart_test_server;

command_fails([ 'pg_recvlogical', '-d', 'postgres', '--slot', 'regress', '--plugin', 'barf', '--create'],
	   'pg recvlogical cannot create slot with nonexistant plugin');
command_ok([ 'pg_recvlogical', '-d', 'postgres', '--slot', 'regress', '--create'],
	   'pg recvlogical can create a slot');
command_fails([ 'pg_recvlogical', '-d', 'postgres', '--slot', 'regress', '--create'],
	   'pg recvlogical cannot create slot with a already used name');
command_fails([ 'pg_recvlogical', '-d', 'postgres', '--slot', 'regress', '--create'],
	   'pg recvlogical cannot create slot when all slots are in use');
command_ok([ 'pg_recvlogical', '-d', 'postgres', '--slot', 'regress', '--drop'],
	   'pg recvlogical can drop slot');
command_fails([ 'pg_recvlogical', '-d', 'postgres', '--slot', 'regress', '--start'],
	   'pg recvlogical cannot stream from nonexistant slot');

# cannot easily test streaming actual changes because that goes on
# forever.
