use strict;
use warnings;
use Test::More;
use File::Temp qw(tempdir);
use Data::Deque::Shared;

# A creator killed between ftruncate() and header init leaves a full-size,
# all-zero (magic==0) file. new() must recover it instead of bricking the
# path, but must never clobber a valid or foreign file.

my $dir = tempdir(CLEANUP => 1);
my $p   = "$dir/recover.deq";
my $capacity = 8;

# Learn the on-disk size for this geometry.
{ my $d = Data::Deque::Shared::Int->new($p, $capacity); }
my $total = -s $p;
unlink $p;

# 1. Recovery: an abandoned all-zero file of exactly $total bytes is re-initialized.
{
    open my $f, '>', $p or die $!; truncate $f, $total or die $!; close $f;
    is(-s $p, $total, "abandoned file is $total bytes (a killed creator's ftruncate)");
    my $d = eval { Data::Deque::Shared::Int->new($p, $capacity) };
    ok($d, "new() recovers an abandoned mid-init file instead of bricking") or diag $@;
  SKIP: {
        skip "no handle", 8 unless $d;
        is($d->size, 0, "recovered deque starts empty (size)");
        ok($d->is_empty, "recovered deque starts empty (is_empty)");
        $d->push_back(1);
        $d->push_back(2);
        $d->push_back(3);
        is($d->size, 3, "push after recovery works");
        is($d->pop_front, 1, "recovered deque readback is correct (no stale data)");
        is($d->pop_front, 2, "  ... FIFO order preserved");
        is($d->pop_front, 3, "  ... FIFO order preserved");
        ok($d->is_empty, "drains back to empty");
    }
    undef $d; unlink $p;
}

# 2. No clobber: a file with nonzero (foreign) magic still errors.
{
    open my $f, '>', $p or die $!; print $f "XXXX"; truncate $f, $total or die $!; close $f;
    my $d = eval { Data::Deque::Shared::Int->new($p, $capacity) };
    ok(!$d, "new() refuses a foreign nonzero-magic file (no clobber)");
    like($@, qr/invalid/i, "  ... reporting an invalid file");
    undef $d; unlink $p;
}

# 3. No recovery for the wrong size: magic==0 but size != total still errors.
{
    open my $f, '>', $p or die $!; truncate $f, $total + 8 or die $!; close $f;
    my $d = eval { Data::Deque::Shared::Int->new($p, $capacity) };
    ok(!$d, "new() refuses an uninitialized file of the wrong size");
    undef $d; unlink $p;
}

# 4. A valid file is attached, never re-initialized (its data survives).
{
    my $a = Data::Deque::Shared::Int->new($p, $capacity);
    $a->push_back(42);
    $a->push_back(99);
    undef $a;
    my $r = Data::Deque::Shared::Int->new($p, $capacity);
    is($r->size, 2, "reopening a valid file preserves size (not re-initialized)");
    is($r->pop_front, 42, "reopening a valid file preserves data");
    is($r->pop_front, 99, "  ... in FIFO order");
    undef $r; unlink $p;
}
# 5. A magic==0 file of the right size but with NON-zero data (not a fresh
#    ftruncate) is NOT recovered -- recovery only re-inits a provably-empty file.
{
    open my $zfh, '>', $p or die $!; truncate $zfh, $total or die $!; close $zfh;
    open $zfh, '+<', $p or die $!; seek $zfh, $total - 1, 0; print $zfh "\x01"; close $zfh;
    my $d = eval { Data::Deque::Shared::Int->new($p, $capacity) };
    ok(!$d, "new() refuses a magic==0 file that is not all-zero (no clobber of real data)");
    undef $d; unlink $p;
}

done_testing;
