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

# See also tests/tests/t2u-gbp, which has some more tests of orig handling.

t-restrict x-dgit-skip-suite,bullseye
t-restrict x-dgit-skip-suite,buster
t-dependencies T2U DEBORIG mpack
export DGIT_TEST_MUNPACK=munpack

t-setup-import upstreamtar
t-t2u-settings
t-debpolicy

cd $p
t-t2u-setup-repo

: "perform an initial upload of the -1"

t-dgit -wgfa push-source --new
t-archive-process-incoming sid

: "run tag2upload, with reused origs"

v=1.0-2
t-dch-commit -D unstable -v $v -m 'reusing origs from suite'
t-refs-same-start
t-t2u-test
t-t2u-succeeded
grep -P 'suite has origs for this upstream version' $t2u_email_log
grep -P 'using existing orig\(s\)' $t2u_email_report

: "run tag2upload, with origs from other suite"

git checkout -b experimental
v=1.0-3~exp1
t-dch-commit -D experimental -v $v -m 'origs from other suite'
t-refs-same-start
t-t2u-test
t-t2u-succeeded
grep -P 'looking in whole archive' $t2u_email_log
grep -P 'using existing orig\(s\)' $t2u_email_report
git checkout master

: "run tag2upload, with missing origs"

rm $tmp/mirror/pool/main/${p}_1.0.orig.tar.gz

# test what happens if we regenerate wrongly
#
# This test case is also our end-to-end test of temporary failures
# from tag2upload-obtain-origs and tag2upload-fetch-inputs.
#
v=1.0-3
t-dch-commit -D unstable -v $v -m 'failed to download or reproduce origs'
t-t2u-test-core retriable unreported
grep -P 'orig file is missing \(404\) at archive mirror' $t2u_email_log
grep -P 'some orig\(s\) not available from archive mirrors' $t2u_email_report
grep -P 'hopefully the real orig will become available' $t2u_email_log

t-t2u-email-expect-headers <<END
    Subject: [tag2upload fake-job] retriable example 1.0-3
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary=???
    Content-Transfer-Encoding: 8bit
    X-Debian-Tag2upload-Distro: test-dummy
    X-Debian-Tag2upload-JobId: fake-job
    X-Debian-Tag2upload-Url: ???
    X-Debian-Tag2upload-Status: retriable
    X-Debian-Tag2upload-Package: example
    To: copies@example.org
    Reply-To: reply-to@example.org
    Message-Id: <???>
END

v=1.0-4
t-dch-commit -D unstable -v $v -m 'reproduced download origs'
t-refs-same-start
GZIP=-2 t-t2u-test
t-t2u-succeeded
grep -P 'orig file is missing \(404\) at archive mirror' $t2u_email_log
grep -P 'some orig\(s\) not available from archive mirrors' $t2u_email_report
grep -P 'successfully regenerated orig' $t2u_email_report

t-ok
