#!/bin/sh
# scaladoc test: document a small library compiled with scalac-3 (API pages
# from TASTy, Markdown in doc comments), then the same library with a
# static site (Markdown pages, a Liquid variable, sidebar.yml), with the
# installed scaladoc-3. No generated page may load anything from another
# site, and /usr/bin/scaladoc is scala3's while scala3 is the selected
# alternative.
set -e

WORK="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORK"

mkdir -p src/shapes
cat > src/shapes/Shape.scala <<'X'
package shapes

/** A plane figure.
  *
  * Areas are in **square units**; see [[Shape.Circle]] and [[Shape.Rect]].
  */
enum Shape:
  /** A circle of radius `r`. */
  case Circle(r: Double)
  /** A rectangle of width `w` and height `h`. */
  case Rect(w: Double, h: Double)

  /** The area of this shape. */
  def area: Double = this match
    case Circle(r)  => math.Pi * r * r
    case Rect(w, h) => w * h

/** Pretty printing.
  *
  *  - one item
  *  - another item :smile:
  */
trait Show[A]:
  /** Renders `a`. */
  def show(a: A): String

object Show:
  /** Shows a shape with its area. */
  given Show[Shape] with
    def show(s: Shape): String = f"$s (area ${s.area}%.2f)"
X

mkdir -p site/_docs/guide
cat > site/_docs/index.md <<'X'
# Shapes guide

Start with the [[shapes.Shape]] enum.
X
cat > site/_docs/guide/index.md <<'X'
# Guide

The guide.
X
cat > site/_docs/guide/areas.md <<'X'
---
title: Computing areas
---
# {{ page.title }}

How to compute areas.
X
cat > site/sidebar.yml <<'X'
index: index.md
subsection:
  - title: The guide
    index: guide/index.md
    directory: guide
    subsection:
      - page: guide/areas.md
X

mkdir classes api site-out
scalac-3 -d classes src/shapes/Shape.scala

scaladoc-3 -d api -project shapes -project-version 1.0 classes
p=api/shapes/Shape.html
test -f "$p"
grep -q 'A plane figure.' "$p"
grep -q '<strong>square units</strong>' "$p"
grep -rq 'The area of this shape.' api/shapes
grep -q '<code>a</code>' api/shapes/Show.html
echo "api pages: $(find api -name '*.html' | wc -l)"

scaladoc-3 -d site-out -project shapes -siteroot site classes
grep -q 'Shapes guide' site-out/docs/index.html
grep -q 'The guide' site-out/docs/index.html
grep -q '</a>Computing areas</h1>' site-out/docs/guide/areas.html
grep -q 'How to compute areas.' site-out/docs/guide/areas.html
test -f site-out/shapes/Shape.html
echo "site pages: $(find site-out -name '*.html' | wc -l)"

# Nothing is loaded from other sites; emoji are Unicode characters.
if grep -rlE '<(script|img|iframe)[^>]* src="(https?:)?//|<link[^>]* href="(https?:)?//' \
     api site-out --include='*.html'; then
    echo "pages above load remote resources" >&2
    exit 1
fi
grep -rq "$(printf '\360\237\230\204')" api/shapes/Show.html

# The Scala.js front end (search box, snippet tools) is in the pages, with
# the search data it reads.
for d in api site-out; do
    test -s $d/scripts/scaladoc-scalajs.js
    test -s $d/scripts/contributors.js
    grep -rlq 'scripts/scaladoc-scalajs.js' $d --include='*.html'
done
grep -q 'Shape' api/scripts/searchData.js

# The type hierarchy graph: its DOT source in a class page, and its three
# scripts written next to scaladoc's own.
grep -q 'id="dot"' api/shapes/Shape.html
for f in d3.min.js graphlib-dot.min.js dagre-d3.min.js; do
    test -s api/scripts/$f
    grep -q "scripts/$f" api/shapes/Shape.html
done

# The alternative
if [ "$(readlink -f /usr/bin/scala)" = /usr/share/scala3/bin/scala ]; then
    test "$(readlink -f /usr/bin/scaladoc)" = /usr/share/scala3/bin/scaladoc
fi
echo "scaladoc test: PASS"
