# Makefile to drive test cases for difficult subsystems.
# Intended to make sure that not too much breakage can sneak past unnoticed.

.PHONY: all

SAFECSSMIN=script/safecssmin
all:: ../${SAFECSSMIN}

# Ensure that whitespace is stripped.
all:: test0
test0: ../${SAFECSSMIN}
	@echo Checking that whitespace is stripped...
	@cd ..; [ ".ad{width:100}" = "`echo .ad { width : 100 } | ${SAFECSSMIN}`" ]
	@touch $@

# Ensure that fallback cupport for old browsers is not stripped.
#    .fullw,.fullwcdisp,.fullwctbl{width:100%;width:100vw;position:relative;left:50%;right:50%;margin-left:-50%;margin-left:-50vw;margin-right:-50%;margin-right:-50vw}.fullwcdisp{clear:both;text-align:center}.fullwctbl{clear:both}
# Eg note "margin-left:-50%;margin-left:-50vw;"
all:: test1
test1: ../${SAFECSSMIN} ../img/css/fullw-20200805.min.css
	@echo Checking that fallbacks for older browsers are not stripped...
	@(cd ..; ${SAFECSSMIN} < img/css/fullw-20200805.min.css )> $@.tmp
	@if [ "`cat ../img/css/fullw-20200805.min.css`" != "`cat $@.tmp`" ]; then echo "ERROR: should be:"; cat ../img/css/fullw-20200805.min.css; echo ""; echo "Is:"; cat $@.tmp; echo ""; exit 1; fi
	@mv $@.tmp $@

# Ensure that repeated clauses for same selector can be merged.
# This is a common residue when stripping CSS with static analysis.
REPEATED=".respfloatr{float:right}.respfloatr{max-width:50}.respfloatr{border-radius:4px}"
all:: test2
test2: ../${SAFECSSMIN}
	@echo Checking that adjacent same-selector rules are merged...
	@(cd ..; echo "${REPEATED}" | ${SAFECSSMIN}) > $@.tmp
	@[ ".respfloatr{float:right;max-width:50;border-radius:4px}" = "`cat $@.tmp`" ]
	@mv $@.tmp $@

# Image converter/extracter.
GHII=script/get_hero_img_inline
# Default value for GALLERYCMSPATH.
GALLERYCMSPATH ?= /rw/galleryDB/photos/

# Ensure that correct images are selected for IMG tag for desktop, etc.
# test3a is a conventional static raster image under img/.
all:: test3a
TARGETIMGa=img/iButton/iButton-temperature-recording-devices-DS1921G.jpg
test3a: ../${GHII} ../${TARGETIMGa}
	@test -s ../${TARGETIMGa}
	@test "`wc -c < ../${TARGETIMGa}`" -eq 207537
	@echo Checking correct body images auto-generated for static raster image a.
	@cd ..; [ 'img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.400x256.jpg' = "`${GHII} autogenImg $(TARGETIMGa) unit-test3a http://www.earth.org.uk/ false 400`" ]
	@cd ..; [ 'img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.320x205.jpg' = "`${GHII} autogenImg $(TARGETIMGa) unit-test3a http://www.earth.org.uk/ true 320`" ]
	@cd ..; [ 'img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.320x205.jpg' = "`${GHII} autogenImg $(TARGETIMGa) unit-test3a http://www.earth.org.uk/ amp 320`" ]
	@echo Checking correct desktop image selected for image a.
	@cd ..; [ '<a href=img/iButton/iButton-temperature-recording-devices-DS1921G.jpg><img class=respfloatr width=400 height=256 src=img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.400x256.jpg srcset="img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.400x256.jpg 400w, img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.320x205.jpg 320w, img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.270x173.jpg 270w" sizes=50vw alt="iButton temperature recording devices DS1921G" title="iButton temperature recording devices DS1921G" style=background:#999 decoding=async></a>' = "`${GHII} floatImg $(TARGETIMGa) unit-test3a http://www.earth.org.uk/ false '' '' respfloatr`" ]
	@echo Checking correct mobile image selected for image a.
	@cd ..; [ '<a href=http://www.earth.org.uk/img/iButton/iButton-temperature-recording-devices-DS1921G.jpg><img class=respfloatr width=320 height=205 src=http://www.earth.org.uk/img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.270x173.jpg srcset="http://www.earth.org.uk/img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.320x205.jpg 320w, http://www.earth.org.uk/img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.270x173.jpg 270w" sizes=50vw alt="iButton temperature recording devices DS1921G" style=background:#999 loading=lazy></a>' = "`${GHII} floatImg $(TARGETIMGa) unit-test3a http://www.earth.org.uk/ true '' '' respfloatr`" ]
	@echo Checking correct AMP image selected for image a.
	@cd ..; [ '<a href=http://www.earth.org.uk/img/iButton/iButton-temperature-recording-devices-DS1921G.jpg><amp-img layout=intrinsic class=respfloatr width=320 height=205 src=http://www.earth.org.uk/img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.320x205.jpg srcset="http://www.earth.org.uk/img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.320x205.jpg 320w, http://www.earth.org.uk/img/a/b/iButton-temperature-recording-devices-DS1921G.l207537.270x173.jpg 270w" sizes=50vw alt="iButton temperature recording devices DS1921G"></amp-img></a>' = "`${GHII} floatImg $(TARGETIMGa) unit-test3a http://www.earth.org.uk/ amp '' '' respfloatr`" ]
# test3b is a static vector image under img/.
all:: test3b
TARGETIMGb=img/20180507-16WW-HeatingEfficiency-co-Pilio.svg
test3b: ../${GHII} ../${TARGETIMGb}
	@test -s ../${TARGETIMGb}
	@test "`wc -c < ../${TARGETIMGb}`" -eq 6597
	@echo Checking correct desktop image selected for static vector image b.
	@cd ..; [ '<img class=respF width=600 height=400 src=img/20180507-16WW-HeatingEfficiency-co-Pilio.svg alt=heating title=heating decoding=async>' = "`${GHII} floatImg $(TARGETIMGb) unit-test3b http://www.earth.org.uk/ false '' '' respF heating`" ]
# TODO: test3c is a dynamic image under out/.
#all:: test3c
# test3d is a large raster image from the Gallery.
all:: test3d
TARGETIMGdRAW=http://gallery.hd.org/_tn/std/energy-matters/_more2011/_more12/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-with-humidistat-being-fitted-to-small-bathroom-33-DHD.jpg
ABSTARGETIMGd=$(GALLERYCMSPATH)energy-matters/_more2011/_more12/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-with-humidistat-being-fitted-to-small-bathroom-33-DHD.jpg
test3d: ../${GHII} ${ABSTARGETIMGd}
	@test -s ${ABSTARGETIMGd}
	@test "`wc -c < ${ABSTARGETIMGd}`" -eq 4992530
	@echo Checking correct desktop image selected for large gallery raster image d.
	@cd ..; [ '<a href=http://gallery.hd.org/_c/energy-matters/_more2011/_more12/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-with-humidistat-being-fitted-to-small-bathroom-33-DHD.jpg.html><img class=respfloatlsml width=264 height=198 src=img/a/b/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-with-humidistat-being-fitted-to-small-bathroom-33-DHD.l4992530.264x198.jpg srcset="img/a/b/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-with-humidistat-being-fitted-to-small-bathroom-33-DHD.l4992530.264x198.jpg 264w, img/a/b/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-with-humidistat-being-fitted-to-small-bathroom-33-DHD.l4992530.211x158.jpg 211w, img/a/b/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-with-humidistat-being-fitted-to-small-bathroom-33-DHD.l4992530.178x133.jpg 178w" sizes=33vw alt=external title=external style=background:#666 decoding=async></a>' = "`${GHII} floatImg $(TARGETIMGdRAW) unit-test3d http://www.earth.org.uk/ false '' '' respfloatlsml external`" ]
# test3e is a small-enough-to-use-directly raster image from the Gallery.
all:: test3e
TARGETIMGeRAW=http://gallery.hd.org/_tn/std/energy-matters/_more2015/_more01/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-seen-working-in-thermal-image-imaging-with-handheld-IR-infrared-camera-warm-outgoing-on-left-cool-incoming-on-right-1-DHD.jpg
ABSTARGETIMGe=$(GALLERYCMSPATH)energy-matters/_more2015/_more01/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-seen-working-in-thermal-image-imaging-with-handheld-IR-infrared-camera-warm-outgoing-on-left-cool-incoming-on-right-1-DHD.jpg
test3e: ../${GHII} ${ABSTARGETIMGe}
	@test -s ${ABSTARGETIMGe}
	@test "`wc -c < ${ABSTARGETIMGe}`" -eq 32404
	@echo Checking correct desktop image selected for small gallery raster image e.
	@cd ..; [ '<a href=http://gallery.hd.org/_c/energy-matters/_more2015/_more01/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-seen-working-in-thermal-image-imaging-with-handheld-IR-infrared-camera-warm-outgoing-on-left-cool-incoming-on-right-1-DHD.jpg.html><img class=respfloatlsml width=240 height=240 src=img/a/b/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-seen-working-in-thermal-image-imaging-with-handheld-IR-infrared-camera-warm-outgoing-on-left-cool-incoming-on-right-1-DHD.l32404.240x240.jpg alt=IR title=IR style=background:#993 decoding=async></a>' = "`${GHII} floatImg $(TARGETIMGeRAW) unit-test3e http://www.earth.org.uk/ false '' '' respfloatlsml IR`" ]
	@echo Checking correct mobile image selected for small gallery raster image e.
	@cd ..; [ '<a href=http://gallery.hd.org/_c/energy-matters/_more2015/_more01/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-seen-working-in-thermal-image-imaging-with-handheld-IR-infrared-camera-warm-outgoing-on-left-cool-incoming-on-right-1-DHD.jpg.html><img class=respfloatlsml width=211 height=211 src=http://www.earth.org.uk/img/a/b/MHRV-mechanical-heat-recovery-ventilation-Vent-Axia-HR25H-seen-working-in-thermal-image-imaging-with-handheld-IR-infrared-camera-warm-outgoing-on-left-cool-incoming-on-right-1-DHD.l32404.211x211.jpg alt=IR style=background:#993 loading=lazy></a>' = "`${GHII} floatImg $(TARGETIMGeRAW) unit-test3e http://www.earth.org.uk/ true '' '' respfloatlsml IR`" ]

# Ensure that correct alt text is auto-extracted, incl for non-static cases.
all:: test4
test4: ../script/altTextFromFilename
	@echo Checking correct alt text is auto-extracted for static img.
	@cd ..; [ 'hob reflections' = "`script/altTextFromFilename img/SMAKLIG/hob-reflections-1-128w.jpg`" ]
	@cd ..; [ 'solar PV grid tie roof mounted power system on slate roof on outbuilding shed garage 1 DHD' = "`script/altTextFromFilename http://gallery.hd.org/_tn/std/mechanoids/_more2010/_more06/solar-PV-grid-tie-roof-mounted-power-system-on-slate-roof-on-outbuilding-shed-garage-1-DHD.jpg`" ]

# Ensure that cross-page promotion can work.
all:: test5
PTP=.work/script/pickPageToPromote
test5: ../${PTP}
	@echo Checking that cross-page promotion can work.
	@cd ..; [ "" != "`${PTP} index.html`" -o "" != "`${PTP} about-16WW.html`" ]



# Tests of creation test-page output for desktop/mobile/AMP.

# Test for decription and title in suitable forms.
all::testDESC
testDESC:: ../test-page.html ../m/test-page.html ../amp/test-page.html
	@echo "Testing for title, description, etc, in metadata."
	@awk '{print;exit}' < ../test-page.html | fgrep -q '<title>Test Page'
	@awk '{print;exit}' < ../test-page.html | fgrep -q 'content="Avert your eyes'
	@awk '{print;exit}' < ../amp/test-page.html | fgrep -q '<title>Test Page'
	@awk '{print;exit}' < ../amp/test-page.html | fgrep -q 'content="Avert your eyes'
	@cat < ../m/test-page.html | fgrep -q '<title>Test Page'
	@cat < ../m/test-page.html | fgrep -q 'content="Avert your eyes'
	@echo "Testing for title, description, etc, in body."
	@awk 'NR<=3{print}' < ../test-page.html | fgrep -q 'itemprop="headline name">Test Page'
	@awk 'NR<=5{print}' < ../test-page.html | fgrep -q 'itemprop=description>Avert your eyes'
	@awk 'NR<=3{print}' < ../amp/test-page.html | fgrep -q 'itemprop="headline name">Test Page'
	@awk 'NR<=5{print}' < ../amp/test-page.html | fgrep -q 'itemprop=description>Avert your eyes'
	@cat < ../m/test-page.html | fgrep -q 'itemprop="headline name">Test Page'
	@cat < ../m/test-page.html | fgrep -q 'itemprop=description>Avert your eyes'
        
# Test for canonical and other key rel links.
all:: testREL
testREL: ../test-page.html ../m/test-page.html ../amp/test-page.html
	@echo "Testing for correct header rel links."
	@fgrep -q '<link href=http://www.earth.org.uk/test-page.html rel=canonical>' < ../m/test-page.html
	@fgrep -q '<link href=http://www.earth.org.uk/test-page.html rel=canonical>' < ../amp/test-page.html
	@fgrep -q '<link href=http://amp.earth.org.uk/test-page.html rel=amphtml>' < ../test-page.html
	@fgrep -q '<link href=http://m.earth.org.uk/test-page.html rel=alternate media="only screen and (max-width:640px)">' < ../test-page.html

# Based on these source lines:
#     <!--DESK-->Page type: desktop
#     <!--MOB-->Page type: mobile (including AMP)
#     <!--HTML5-->Page type2: vanilla HTML5 (not AMP)
#     <!--AMP-->Page type2: AMP
# The test-page.html files must already have been brought up to date.
all:: testPTBL
testPTBL: ../test-page.html ../m/test-page.html ../amp/test-page.html
	@echo Testing for correct stripping of DESK/MOB/etc.
	@echo Testing for correct stripping of DESK/MOB/etc on desktop.
	@egrep -q '^<h2 id="Desktop">PTBL: Desktop vs Mobile Page Type Line By Line</h2>$$' ../test-page.html
	@[ 'Page type: desktop' = "`egrep '^Page type:' < ../test-page.html`" ]
	@[ 'Page type2: vanilla HTML5 (not AMP)' = "`egrep '^Page type2:' < ../test-page.html`" ]
	@echo Testing for correct stripping of DESK/MOB/etc on AMP.
	@egrep -q '^<h2 id="Desktop">PTBL: Desktop vs Mobile Page Type Line By Line</h2>$$' ../amp/test-page.html
	@[ 'Page type: mobile (including AMP)' = "`egrep '^Page type:' < ../amp/test-page.html`" ]
	@[ 'Page type2: AMP' = "`egrep '^Page type2:' < ../amp/test-page.html`" ]
	@echo Testing for correct stripping of DESK/MOB/etc on vanilla mobile.
	@egrep -q '<h2 id=Desktop>PTBL: Desktop vs Mobile Page Type Line By Line</h2>' ../m/test-page.html
	@[ '' != "`egrep 'Page type: mobile' < ../m/test-page.html`" ]
	@[ '' != "`egrep 'Page type2: vanilla' < ../m/test-page.html`" ]
