# 2023 January 13
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#

source [file join [file dirname [info script]] rbu_common.tcl]
if_no_rbu_support { finish_test ; return }
set ::testprefix rbupass

if {[info commands register_demovfs]==""} {
  finish_test
  return
}

db close

register_demovfs
sqlite3rbu_create_vfs myvfs demo

sqlite3 db file:test.db?vfs=myvfs -uri 1
do_execsql_test 1.0 {
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  INSERT INTO t1 VALUES(1, 2);
  SELECT * FROM t1;
} {1 2}

if {[permutation]!="inmemory_journal"} {
  do_execsql_test 1.1 {
    PRAGMA journal_mode = wal;
  } {delete}
}

do_execsql_test 1.2 {
  SELECT * FROM t1;
} {1 2}

do_test 1.3 {
  forcedelete rbu.db
  sqlite3 rbu rbu.db
  rbu eval {
    CREATE TABLE data_t1(a, b, rbu_control);
    INSERT INTO data_t1 VALUES(2, 4, 0);
  }
  rbu close
} {}

do_test 1.4 {
  sqlite3rbu rbu test.db rbu.db
} {rbu}
do_test 1.5 {
  rbu step
} {SQLITE_CANTOPEN}
do_test 1.6 {
  list [catch { rbu close } msg] $msg
} {1 {SQLITE_CANTOPEN - unable to open database file}}

do_test 1.7 {
  sqlite3rbu_vacuum rbu test.db
} {rbu}
do_test 1.8 {
  rbu step
  catch { rbu close } 
} {1}

do_execsql_test 1.9 {
  SELECT * FROM t1;
} {1 2}

db close
sqlite3rbu_destroy_vfs myvfs 
unregister_demovfs
sqlite3_shutdown
finish_test