test-render-graphs.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. set -u
  3. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  4. REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
  5. SCRIPT_UNDER_TEST="$REPO_ROOT/skills/writing-skills/render-graphs.js"
  6. NODE_BIN="$(command -v node)"
  7. PASSES=0
  8. FAILURES=0
  9. TEST_ROOT="$(mktemp -d)"
  10. cleanup() {
  11. rm -rf "$TEST_ROOT"
  12. }
  13. trap cleanup EXIT
  14. pass() {
  15. echo " [PASS] $1"
  16. PASSES=$((PASSES + 1))
  17. }
  18. fail() {
  19. echo " [FAIL] $1"
  20. FAILURES=$((FAILURES + 1))
  21. }
  22. assert_contains() {
  23. local haystack="$1"
  24. local needle="$2"
  25. local description="$3"
  26. if printf '%s' "$haystack" | grep -Fq -- "$needle"; then
  27. pass "$description"
  28. else
  29. fail "$description"
  30. echo " expected to find: $needle"
  31. fi
  32. }
  33. assert_not_contains() {
  34. local haystack="$1"
  35. local needle="$2"
  36. local description="$3"
  37. if printf '%s' "$haystack" | grep -Fq -- "$needle"; then
  38. fail "$description"
  39. echo " did not expect to find: $needle"
  40. else
  41. pass "$description"
  42. fi
  43. }
  44. fixture="$TEST_ROOT/fixture-skill"
  45. mkdir -p "$fixture" "$TEST_ROOT/empty-path"
  46. cat >"$fixture/SKILL.md" <<'EOF'
  47. ---
  48. name: fixture-skill
  49. ---
  50. # Fixture Skill
  51. ```dot
  52. digraph fixture_graph {
  53. start -> end;
  54. }
  55. ```
  56. EOF
  57. echo "Writing-skills render-graphs tests"
  58. missing_dot_output="$(PATH="$TEST_ROOT/empty-path" "$NODE_BIN" "$SCRIPT_UNDER_TEST" "$fixture" 2>&1)"
  59. missing_dot_status=$?
  60. if [[ "$missing_dot_status" -ne 0 ]]; then
  61. pass "missing Graphviz exits non-zero"
  62. else
  63. fail "missing Graphviz exits non-zero"
  64. fi
  65. assert_contains "$missing_dot_output" "Error: graphviz (dot) not found." "missing Graphviz reports install guidance"
  66. assert_not_contains "$missing_dot_output" "ReferenceError: require is not defined" "script runs as an ES module"
  67. render_output="$("$NODE_BIN" "$SCRIPT_UNDER_TEST" "$fixture" 2>&1)"
  68. render_status=$?
  69. if [[ "$render_status" -eq 0 ]]; then
  70. pass "fixture diagram renders"
  71. else
  72. fail "fixture diagram renders"
  73. printf '%s\n' "$render_output"
  74. fi
  75. assert_contains "$render_output" "Found 1 diagram(s)" "reports discovered diagram"
  76. assert_contains "$render_output" "Rendered: fixture_graph.svg" "reports rendered SVG"
  77. if [[ -f "$fixture/diagrams/fixture_graph.svg" ]]; then
  78. pass "writes SVG output"
  79. else
  80. fail "writes SVG output"
  81. fi
  82. if [[ -f "$fixture/diagrams/fixture_graph.svg" ]] && grep -Fq "<svg" "$fixture/diagrams/fixture_graph.svg"; then
  83. pass "SVG output has SVG markup"
  84. else
  85. fail "SVG output has SVG markup"
  86. fi
  87. echo
  88. echo "Results: $PASSES passed, $FAILURES failed"
  89. if [[ "$FAILURES" -gt 0 ]]; then
  90. exit 1
  91. fi