#!/bin/bash
set -e
. tests/lib

t-archive pari-extra 3-1
t-git pari-extra 3-1
rp=$(echo $p | rev)

t-dgit clone $p

cd $p
t-cloned-fetched-good

v=3-2~dummy1
t-apply-diff 3-1 $v
debcommit -a
rv=$(echo $v | rev)

s=unstable
rs=$(echo $s | rev)

t-refs-same-start
t-ref-head

t-dgit-rpush-source-mitm () {
    field="$1"; nth="$2"; shift 2
    DGIT_SSH="$troot/ssh-rpush-mitm $field $nth" \
	    t-dgit "$@" rpush-source r-build:$tmp/$p
}

mkdir $tmp/empty
cd $tmp/empty

: '
 (1) Check we die if the parsed changelog the responder presents has
     values that are not what the initiator expects.
'

t-expect-fail "changelog field \`Source' is not expected \`$p'" \
t-dgit-rpush-source-mitm Source 1 --package=$p

t-expect-fail "changelog field \`Distribution' is not expected \`$s'" \
t-dgit-rpush-source-mitm Distribution 1 --expect-suite=unstable

t-expect-fail "changelog field \`Version' is not expected \`$v'" \
t-dgit-rpush-source-mitm Version 1 --expect-version=$v

: '
 (2) Now consider an rpush responder that sent us a parsed changelog
     we were expecting but then wants us to sign a .changes or .dsc
     with different values.
'

t-expect-fail "changes field \`Source' is not expected \`$p'" \
t-dgit-rpush-source-mitm Source 3 --package=$p

t-expect-fail "changes field \`Distribution' is not expected \`$s'" \
t-dgit-rpush-source-mitm Distribution 2 --expect-suite=unstable

t-expect-fail "changes field \`Version' is not expected \`$v'" \
t-dgit-rpush-source-mitm Version 3 --expect-version=$v

t-expect-fail "is for $rp $v but debian/changelog is for $p $v" \
t-dgit-rpush-source-mitm Source 2 --package=$p

t-expect-fail "is for $p $rv but debian/changelog is for $p $v" \
t-dgit-rpush-source-mitm Version 2 --expect-version=$v

: '
 (3) A non-broken push that should succeed.
'

t-dgit "$@" --ssh=$troot/ssh rpush-source r-build:$tmp/$p

cd $tmp/$p
t-pushed-good dgit/sid

: '
 (4) A non-broken push with sponshorthip
'

v=3-3
t-dch-commit -v $v -m sponsor
t-dch-commit-r
t-sponsee-dep14tag

t-refs-same-start
t-dgit "$@" --ssh=$troot/ssh rpush-source r-build:$tmp/$p
t-pushed-good dgit/sid

t-sponsee-dep14tag-check-kept

t-ok
