App-karr

 view release on metacpan or  search on metacpan

t/248-delete-json-prompt-channel.t  view on Meta::CPAN

    is( $data->{id},      1,       '...and it names the card' );
    is( $data->{deleted}, 0,       '...and reports it was not deleted' );
    is( $data->{title},   'Card one', '...with the title it carries' );

    like( $rv->{stderr}, qr/Delete task 1: Card one\? \[y\/N\] /,
        'the question was asked, on STDERR where dialogue belongs' );
    unlike( $rv->{stdout}, qr/\[y\/N\]/,
        'and nothing of it reached STDOUT' );

    is( _karr( $repo, [], 'show', '1', '--json' )->{exit}, 0,
        'the card is still on the board' );
};

subtest 'answering yes leaves stdout decodable as a whole (#248)' => sub {
    my $repo = _board();

    my $rv = _karr( $repo, ["y\n"], 'delete', '1', '--json' );
    is( $rv->{exit}, 0, 'the delete succeeds' ) or diag $rv->{stderr};

    my $data = _decodes( $rv->{stdout},
        'the whole stream decodes on the answering half too' );
    is( $data->{id},      1, '...and it names the card' );
    is( $data->{deleted}, 1, '...and reports the delete happened' );

    like( $rv->{stderr}, qr/Delete task 1: Card one\? \[y\/N\] /,
        'the question was asked on STDERR' );
    unlike( $rv->{stdout}, qr/\[y\/N\]/,
        'and nothing of it reached STDOUT' );

    isnt( _karr( $repo, [], 'show', '1', '--json' )->{exit}, 0,
        'the card is gone' );
};

subtest 'a batch with mixed answers decodes as one array (#248)' => sub {
    my $repo = _board();

    # Two prompts, two different answers: the case --yes cannot express, and
    # therefore the reason --json without --yes stays a legal invocation
    # instead of being refused.
    my $rv = _karr( $repo, [ "n\n", "y\n" ], 'delete', '1,2', '--json' );
    is( $rv->{exit}, 0, 'the batch exits 0' ) or diag $rv->{stderr};

    my $data = _decodes( $rv->{stdout},
        'two prompts still leave one decodable array behind' );
    is( ref $data, 'ARRAY', 'a batch renders as an array' );
    is( scalar @$data, 2, '...with one entry per id' );
    is( $data->[0]{id},      1, 'the declined card is first' );
    is( $data->[0]{deleted}, 0, '...and was kept' );
    is( $data->[1]{id},      2, 'the confirmed card is second' );
    is( $data->[1]{deleted}, 1, '...and was deleted' );

    unlike( $rv->{stdout}, qr/\[y\/N\]/,
        'neither question reached STDOUT' );
};

subtest 'the plain path keeps the question off stdout too (#248)' => sub {
    my $repo = _board();

    # Without --json there is no object to corrupt, but the same rule decides
    # the channel: `karr delete 1 > kept.txt` used to write the question into
    # the file and show the operator a blank cursor.
    my $rv = _karr( $repo, ["n\n"], 'delete', '1' );
    is( $rv->{exit}, 0, 'answering no exits 0' ) or diag $rv->{stderr};

    like( $rv->{stderr}, qr/Delete task 1: Card one\? \[y\/N\] /,
        'the question is on STDERR' );
    unlike( $rv->{stdout}, qr/\[y\/N\]/,
        'and not on STDOUT' );
    like( $rv->{stdout}, qr/Skipped task 1: Card one/,
        'while the outcome stays on STDOUT, where results live' );
};

done_testing;



( run in 0.867 second using v1.01-cache-2.11-cpan-d01c6094234 )