Files
onepager/tests/schema-test.sh
mAi 29965c1164 mAi: #9 - GEO Schema-Slot {{schema_jsonld}} in templates/base.html + render.sh
Schema-Markup-Mechanismus für templated Sites (custom Sites bleiben unberührt).

- templates/base.html: {{schema_jsonld}} Slot im <head>.
- site.yaml: optionaler `schema:` Block. `type:` -> `@type`, `@context`
  wird automatisch ergänzt. Fehlt der Block, bleibt der Slot leer.
- render.sh: liest schema via `yq -o=json`, transformiert mit jq, fügt
  Template-Default für `type` ein (person-* -> Person, product-* -> Product,
  editorial -> Article).
- render.sh: literal-string replace (lreplace) statt awk gsub für multiline-
  Substitution. Behebt nebenbei einen latenten Bug, bei dem `&copy;` im
  template_body als `{{body}}copy;` corrupted wurde (gsub interpretierte
  `&` als matched text).
- tests/schema-test.sh + 4 Fixtures: validiert explicit type, Template-
  Defaults für 3 Templates, leerer Slot ohne schema-Block.
- README.md: Schema.org-Konvention dokumentiert (Block-Format, Defaults,
  Custom-Sites-Hinweis, Schema.org-Validator-Link).

QA: ./build.sh -> 59 sites OK, custom Sites byte-identical zur Source,
3 templated Fixtures rendern valides JSON-LD (Person/Product/Article),
no-schema-Fixture produziert keinen <script>-Tag.

Closes #9 nicht - head reviewed + merged.
2026-04-30 02:50:08 +02:00

93 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Schema-slot tests for render.sh
# Renders fixture site.yaml files and verifies JSON-LD output.
#
# Run from repo root: ./tests/schema-test.sh
set -euo pipefail
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
ROOT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
FIXTURES="$SCRIPT_DIR/fixtures"
pass=0
fail=0
assert_jsonld() {
local name="$1" yaml="$2" template="$3" expected_type="$4"
local out
out=$("$ROOT_DIR/render.sh" "$yaml" "$ROOT_DIR/templates/${template}.html")
local script
script=$(echo "$out" | grep -oE '<script type="application/ld\+json">[^<]+</script>' || true)
if [ -z "$script" ]; then
echo " [FAIL] $name — no JSON-LD <script> found"
fail=$((fail + 1))
return
fi
local json
json=$(echo "$script" | sed -E 's|<script[^>]*>||; s|</script>||')
if ! echo "$json" | jq empty 2>/dev/null; then
echo " [FAIL] $name — invalid JSON: $json"
fail=$((fail + 1))
return
fi
local ctx atype
ctx=$(echo "$json" | jq -r '."@context"')
atype=$(echo "$json" | jq -r '."@type"')
if [ "$ctx" != "https://schema.org" ]; then
echo " [FAIL] $name — wrong @context: $ctx"
fail=$((fail + 1))
return
fi
if [ "$atype" != "$expected_type" ]; then
echo " [FAIL] $name — expected @type=$expected_type, got $atype"
fail=$((fail + 1))
return
fi
if echo "$json" | jq -e 'has("type")' >/dev/null; then
echo " [FAIL] $name — raw 'type' key still present (should be '@type')"
fail=$((fail + 1))
return
fi
echo " [PASS] $name — @type=$atype, valid JSON-LD"
pass=$((pass + 1))
}
assert_no_jsonld() {
local name="$1" yaml="$2" template="$3"
local out
out=$("$ROOT_DIR/render.sh" "$yaml" "$ROOT_DIR/templates/${template}.html")
if echo "$out" | grep -q 'application/ld+json'; then
echo " [FAIL] $name — unexpected JSON-LD present"
fail=$((fail + 1))
return
fi
echo " [PASS] $name — no JSON-LD (as expected)"
pass=$((pass + 1))
}
echo "== schema-test.sh =="
# Explicit type
assert_jsonld "person-explicit (type: Person)" \
"$FIXTURES/person-explicit.yaml" "person-dark" "Person"
# Default from template (no .schema.type)
assert_jsonld "product-default (default Product)" \
"$FIXTURES/product-default.yaml" "product-dark" "Product"
assert_jsonld "article-editorial (default Article)" \
"$FIXTURES/article-editorial.yaml" "editorial" "Article"
# No schema → no script
assert_no_jsonld "no-schema (empty slot)" \
"$FIXTURES/no-schema.yaml" "minimal"
echo "== $pass passed, $fail failed =="
[ $fail -eq 0 ]