#!/usr/bin/env bash
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Tests flows in a Python sample by name.
# Usage: py/bin/test_sample_flows <sample-name>
# Example: py/bin/test_sample_flows provider-google-genai-hello
# py/bin/test_sample_flows framework-evaluator-demo
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PY_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
SAMPLES_DIR="$PY_DIR/samples"
# List available samples that have main.py
list_samples() {
find "$SAMPLES_DIR" -mindepth 1 -maxdepth 1 -type d -not -name '.*' -not -name '__pycache__' | while read -r dir; do
if [[ -f "$dir/src/main.py" ]] || [[ -f "$dir/main.py" ]]; then
basename "$dir"
fi
done | sort
}
# Open results file with appropriate viewer
open_results() {
local file_to_open="$1"
echo "Opening results file..."
if command -v open &> /dev/null; then
# macOS
open "$file_to_open"
elif command -v xdg-open &> /dev/null; then
# Linux
xdg-open "$file_to_open"
else
# Fallback: display with less
less "$file_to_open"
fi
}
# Check if gum is available
GUM_CMD=""
if command -v gum &> /dev/null; then
GUM_CMD="gum"
elif [[ -f "$HOME/go/bin/gum" ]]; then
GUM_CMD="$HOME/go/bin/gum"
fi
# Check if fzf is available
FZF_CMD=""
if command -v fzf &> /dev/null; then
FZF_CMD="fzf"
elif [[ -f "$HOME/go/bin/fzf" ]]; then
FZF_CMD="$HOME/go/bin/fzf"
fi
if [[ $# -lt 1 ]]; then
while true; do
SAMPLES=$(list_samples)
if [[ -n "$FZF_CMD" ]]; then
# Use fzf with preview window showing README if available
SAMPLE_NAME=$(echo "$SAMPLES" | $FZF_CMD \
--preview "if [ -f $SAMPLES_DIR/{}/README.md ]; then cat $SAMPLES_DIR/{}/README.md; else echo 'No README found for {}'; fi" \
--preview-window=right:60%:wrap \
--height=100% \
--reverse \
--header="Select a sample to test flows (Ctrl-C to exit)")
if [[ -z "$SAMPLE_NAME" ]]; then
exit 0
fi
elif [[ -n "$GUM_CMD" ]]; then
SAMPLE_NAME=$($GUM_CMD filter --placeholder "Select a sample to test flows..." <<< "$SAMPLES")
if [[ -z "$SAMPLE_NAME" ]]; then
exit 0
fi
else
echo "Usage: $0 <sample-name>"
echo ""
echo "Available samples:"
echo "$SAMPLES"
echo ""
echo "Tip: Install 'fzf' or 'gum' for an interactive menu"
exit 1
fi
SAMPLE_DIR="$SAMPLES_DIR/$SAMPLE_NAME"
if [[ ! -d "$SAMPLE_DIR" ]]; then
echo "Error: Sample '$SAMPLE_NAME' not found in $SAMPLES_DIR"
continue
fi
echo ""
echo "============================================================"
echo "Testing flows in: $SAMPLE_NAME"
echo "============================================================"
echo ""
# Generate timestamped output filename
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
OUTPUT_FILE="$PY_DIR/flow_test_${SAMPLE_NAME}_${TIMESTAMP}.txt"
# Run the flow testing tool
cd "$PY_DIR"
# Don't trap SIGINT here - allow user to interrupt
if uv run samples/sample-test/review_sample_flows.py "$SAMPLE_DIR" --output "$OUTPUT_FILE"; then
TEST_SUCCESS=true
else
TEST_SUCCESS=false
fi
echo ""
if [[ "$TEST_SUCCESS" == "true" ]]; then
echo "Flow testing for '$SAMPLE_NAME' completed successfully."
else
echo "Flow testing for '$SAMPLE_NAME' was interrupted or failed."
fi
trap '' SIGINT
read -rp "Press Enter to continue..." || true
trap - SIGINT
done
else
SAMPLE_NAME="$1"
shift
SAMPLE_DIR="$SAMPLES_DIR/$SAMPLE_NAME"
if [[ ! -d "$SAMPLE_DIR" ]]; then
echo "Error: Sample '$SAMPLE_NAME' not found in $SAMPLES_DIR"
echo ""
echo "Available samples:"
list_samples
exit 1
fi
echo ""
echo "============================================================"
echo "Testing flows in: $SAMPLE_NAME"
echo "============================================================"
echo ""
# Generate timestamped output filename
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
OUTPUT_FILE="$PY_DIR/flow_test_${SAMPLE_NAME}_${TIMESTAMP}.txt"
# Run the flow testing tool
cd "$PY_DIR"
# Allow Ctrl+C to interrupt
if uv run samples/sample-test/review_sample_flows.py "$SAMPLE_DIR" --output "$OUTPUT_FILE"; then
echo "Flow testing completed for $SAMPLE_NAME"
else
echo ""
echo "Flow testing was interrupted or failed."
fi
fi