#!/usr/bin/env bats
# vim:set ft=sh:

load ../lib/common

@test "db-move shows help" {
	run pkgctl db move --help
	assert_success
	assert_output --partial "Usage: pkgctl db move [OPTIONS] [SOURCE_REPO] [TARGET_REPO] [PKGBASE]..."
}

@test "db-move requires source and target repos and package" {
	run pkgctl db move
	assert_failure
	assert_output --partial "Usage: pkgctl db move [OPTIONS] [SOURCE_REPO] [TARGET_REPO] [PKGBASE]..."
}

@test "db-move validates source repository" {
	run pkgctl db move invalid-repo extra libfoo
	assert_failure
	assert_output "==> ERROR: Invalid source repository: invalid-repo"
}

@test "db-move validates target repository" {
	run pkgctl db move extra invalid-repo libfoo
	assert_failure
	assert_output "==> ERROR: Invalid target repository: invalid-repo"
}

@test "db-move prompts for confirmation" {
	# Mock read to simulate user input 'n'
	function read() { echo "n"; }
	export -f read

	run pkgctl db move extra core libfoo
	assert_success
	assert_output --partial "Move packages from [extra] to [core]: libfoo? [Y/n]"
}

@test "db-move skips confirmation with -y" {
	run pkgctl db move -y extra core libfoo
	assert_success
	assert_output --partial "==> Moving [libfoo] from [extra] to [core]"
}

@test "db-move skips confirmation with --yes" {
	run pkgctl db move --yes extra core libfoo
	assert_success
	assert_output --partial "==> Moving [libfoo] from [extra] to [core]"
}

@test "db-move aborts on negative confirmation" {
	# Mock read to simulate user input 'n' 
	function read() { echo "n"; }
	export -f read

	run pkgctl db move extra core libfoo
	assert_success
	refute_output --partial "==> Moving [libfoo] from [extra] to [core]"
}

@test "db-move proceeds on positive confirmation" {
	# Mock read to simulate user input 'y'
	function read() { echo "y"; }
	export -f read

	run pkgctl db move extra core libfoo
	assert_success
	assert_output --partial "==> Moving [libfoo] from [extra] to [core]"
}

@test "db-move handles multiple packages" {
	run pkgctl db move -y extra core libfoo libbar
	assert_success
	assert_output --partial "==> Moving [libfoo] from [extra] to [core]"
	assert_output --partial "==> Moving [libbar] from [extra] to [core]"
} 