#!/bin/sh
# Smoke test for the installed material_color_utilities library; runs offline.
set -eu

work=$(mktemp -d)
export HOME="$work"
trap 'rm -rf "$work"' EXIT INT TERM

DART=$(command -v dart 2>/dev/null || true)
[ -n "$DART" ] || DART=/usr/lib/dart/bin/dart

mkdir -p "$work/.dart_tool"
cat > "$work/.dart_tool/package_config.json" <<'JSON'
{"configVersion":2,"packages":[{"name":"collection","rootUri":"file:///usr/share/dart/packages/collection","packageUri":"lib/","languageVersion":"3.4"},{"name":"material_color_utilities","rootUri":"file:///usr/share/dart/packages/material_color_utilities","packageUri":"lib/","languageVersion":"3.5"}]}
JSON

cat > "$work/main.dart" <<'DART'
import "package:material_color_utilities/material_color_utilities.dart";

void main() {
  // Google Blue (#4285F4) as a Material source colour.
  final hct = Hct.fromInt(0xFF4285F4);
  if (hct.hue < 260.0 || hct.hue > 272.0) {
    throw StateError("hue: ${hct.hue}");
  }
  if (hct.tone < 50.0 || hct.tone > 62.0) {
    throw StateError("tone: ${hct.tone}");
  }
  // Converting back to ARGB must give the colour we started from.
  if (hct.toInt() != 0xFF4285F4) {
    throw StateError("roundtrip: ${hct.toInt().toRadixString(16)}");
  }

  final light = SchemeTonalSpot(
    sourceColorHct: hct,
    isDark: false,
    contrastLevel: 0.0,
  );
  final lightTone = Hct.fromInt(light.surface).tone;
  if (lightTone < 95.0 || lightTone > 100.0) {
    throw StateError("light surface tone: $lightTone");
  }

  final dark = SchemeTonalSpot(
    sourceColorHct: hct,
    isDark: true,
    contrastLevel: 0.0,
  );
  final darkTone = Hct.fromInt(dark.surface).tone;
  if (darkTone > 10.0) {
    throw StateError("dark surface tone: $darkTone");
  }

  print("material_color_utilities smoke test passed");
}
DART

cd "$work"
"$DART" run main.dart
