use 5.010;
use strict;
use warnings;
use utf8;

use Test::More;

BEGIN {
  require_ok 'Termbox::PP';
  use_ok 'Termbox', qw( :api :return );
}

# ---------------------------------------
note 'Test tb_present and tb_invalidate';
# ---------------------------------------

subtest 'tb_present basic render pass' => sub {
  plan tests => 4;

  # Mock cellbuf and output to verify tb_present
  my @calls;
  no warnings 'redefine';
  local *Termbox::cellbuf_get = sub {
    my ($buf, $x, $y, $out) = @_;
    $$out = {
      ch   => "e\x{301}",   # 'é'
      fg   => 1,
      bg   => 2,
    };
    return TB_OK;
  };
  local *Termbox::cell_cmp  = sub { return 1 }; # force redraw
  local *Termbox::send_attr = sub {
    push @calls, 'attr';
    return TB_OK;
  };
  local *Termbox::send_cluster = sub {
    push @calls, 'cluster';
    return TB_OK;
  };
  local *Termbox::send_cursor_if = sub {
    push @calls, 'cursor';
    return TB_OK;
  };
  local *Termbox::bytebuf_flush = sub {
    push @calls, 'flush';
    return TB_OK;
  };
  local *Termbox::tb_wcwidth = sub { 1 };

  # Initialize global state to allow tb_present to proceed
  local $Termbox::global->{initialized} = 1;
  local $Termbox::global->{front}       = cellbuf->new();
  local $Termbox::global->{back}        = cellbuf->new();
  local $Termbox::global->{out}         = '';
  $Termbox::global->{front}->init(1, 1);
  $Termbox::global->{back}->init(1, 1);

  # Test that tb_present calls the expected output functions
  is(tb_present(), TB_OK, 'tb_present returns TB_OK');
  ok(grep(/attr/,    @calls), 'send_attr was called');
  ok(grep(/cluster/, @calls), 'send_cluster was called');
  ok(grep(/flush/,   @calls), 'bytebuf_flush was called');
};

subtest 'tb_invalidate' => sub {
  plan tests => 3;

  no warnings 'redefine';

  # success path
  local *Termbox::resize_cellbufs = sub { TB_OK };
  local $Termbox::global->{initialized} = 1;
  is(
    tb_invalidate(),
    TB_OK,
    'tb_invalidate returns TB_OK'
  );

  # error propagation
  local *Termbox::resize_cellbufs = sub { TB_ERR_RESIZE_IOCTL };
  is(
    tb_invalidate(),
    TB_ERR_RESIZE_IOCTL,
    'tb_invalidate propagates resize_cellbufs error'
  );

  # not initialized
  local $Termbox::global->{initialized} = 0;
  is(
    tb_invalidate(),
    TB_ERR_NOT_INIT,
    'tb_invalidate fails when not initialized'
  );
};

done_testing;
