Refactor run_test.sh to generate history json blobs in only one spot (issue 137)
The create_state function now accepts role/content pairs as arguments and generates the JSON internally using jq for proper escaping. This centralizes all JSON blob generation in run_test.sh, eliminating the need for test files to manually construct JSON arrays.
This commit is contained in:
+21
-12
@@ -73,18 +73,27 @@ export -f dr_result
|
||||
|
||||
function create_state() {
|
||||
local file="$1"
|
||||
local history_json="$2"
|
||||
cat > "$file" <<EOF
|
||||
{
|
||||
"history": $history_json,
|
||||
"tools": [],
|
||||
"config": {
|
||||
"max_tool_iterations": 16,
|
||||
"verbose": false,
|
||||
"depth": 0
|
||||
}
|
||||
}
|
||||
EOF
|
||||
shift
|
||||
|
||||
# Build history array from role/content pairs using jq for proper escaping
|
||||
local history_json="[]"
|
||||
while [ $# -gt 0 ]; do
|
||||
local role="$1"
|
||||
local content="$2"
|
||||
shift 2
|
||||
history_json=$(echo "$history_json" | jq --arg r "$role" --arg c "$content" '. + [{"role": $r, "content": $c}]')
|
||||
done
|
||||
|
||||
# Generate the complete state file
|
||||
echo "$history_json" | jq '{
|
||||
"history": .,
|
||||
"tools": [],
|
||||
"config": {
|
||||
"max_tool_iterations": 16,
|
||||
"verbose": false,
|
||||
"depth": 0
|
||||
}
|
||||
}' > "$file"
|
||||
}
|
||||
export -f create_state
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ set -e
|
||||
|
||||
export EDITOR="$MOCK_EDITOR"
|
||||
|
||||
create_state test_drharness.json '[{"role": "assistant", "content": "This is the AI last response."}]'
|
||||
create_state test_drharness.json assistant "This is the AI last response."
|
||||
export DRHARNESS_EDITOR_MODE="default"
|
||||
be_done 'Mock response' | $DRHARNESS -F test_drharness.json -d
|
||||
# Make sure we got correct prompt out of it
|
||||
@@ -13,7 +13,7 @@ fi
|
||||
|
||||
|
||||
rm -f test_drharness_empty.json
|
||||
create_state test_drharness_empty.json '[{"role": "assistant", "content": "Mock response"}]'
|
||||
create_state test_drharness_empty.json assistant "Mock response"
|
||||
export DRHARNESS_EDITOR_MODE="empty"
|
||||
if $DRHARNESS -F test_drharness_empty.json -d < /dev/null 2>&1 | grep -q "Prompt is empty"; then
|
||||
echo "PASS: Empty input handled"
|
||||
@@ -23,7 +23,7 @@ else
|
||||
fi
|
||||
|
||||
rm -f test_drharness_sep.json
|
||||
create_state test_drharness_sep.json '[{"role": "assistant", "content": "Mock response"}]'
|
||||
create_state test_drharness_sep.json assistant "Mock response"
|
||||
export DRHARNESS_EDITOR_MODE="separator"
|
||||
be_done 'Mock response' | $DRHARNESS -F test_drharness_sep.json -d
|
||||
# Make sure we got correct prompt out of it
|
||||
@@ -42,7 +42,7 @@ EOF
|
||||
diff -u expected_history hist.md
|
||||
|
||||
rm -f test_drharness_full.json
|
||||
create_state test_drharness_full.json '[{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}]'
|
||||
create_state test_drharness_full.json user "Hello" assistant "Hi there!"
|
||||
export DRHARNESS_EDITOR_MODE="full_history"
|
||||
be_done 'Mock response' | $DRHARNESS -F test_drharness_full.json -f -d
|
||||
cat > expected_history <<EOF
|
||||
@@ -72,7 +72,7 @@ EOF
|
||||
|
||||
diff -u expected_history hist.md
|
||||
|
||||
create_state test_drharness_partial_line.json '[{"role": "assistant", "content": "Hi there!"}]'
|
||||
create_state test_drharness_partial_line.json assistant "Hi there!"
|
||||
export DRHARNESS_EDITOR_MODE="partial_line"
|
||||
be_done 'Mock response' | $DRHARNESS -F test_drharness_partial_line.json -f -d
|
||||
|
||||
|
||||
Reference in New Issue
Block a user