Fix issue 108: Replace manual JSON history creation with helper function
Add create_chat_file() helper to run_test.sh and update test_edit_history.sh and test_edit_history_flag.sh to use it instead of manually creating JSON. This follows the CONTRIBUTING.md guideline that tests should use helper functions instead of inline JSON handling.
This commit is contained in:
@@ -92,6 +92,23 @@ function dr_reasoning_content() {
|
||||
}
|
||||
export -f dr_reasoning_content
|
||||
|
||||
# Create a chat file with history messages
|
||||
# Usage: create_chat_file <filename> <role> <content> [role content ...]
|
||||
# Example: create_chat_file chat.json user "hello" assistant "hi there"
|
||||
function create_chat_file() {
|
||||
local file="$1"
|
||||
shift
|
||||
local history="[]"
|
||||
while [ $# -gt 0 ]; do
|
||||
local role="$1"
|
||||
local content="$2"
|
||||
shift 2
|
||||
history=$(echo "$history" | jq --arg r "$role" --arg c "$content" '. + [{"role": $r, "content": $c}]')
|
||||
done
|
||||
echo "$history" | jq '{history: ., tools: []}' > "$file"
|
||||
}
|
||||
export -f create_chat_file
|
||||
|
||||
# Resolve paths relative to this script
|
||||
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
|
||||
PROJECT_ROOT=$(cd "${SCRIPT_DIR}/.." && pwd)
|
||||
|
||||
+14
-14
@@ -23,7 +23,7 @@ test_pick_all_messages() {
|
||||
local test_name="pick_all_messages"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage the rebase editor output (pick all)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -88,7 +88,7 @@ test_no_edits_history_unchanged() {
|
||||
local test_name="no_edits_history_unchanged"
|
||||
|
||||
# Create a chat file with three messages
|
||||
echo '{"history":[{"role":"user","content":"first message"},{"role":"assistant","content":"second message"},{"role":"user","content":"third message"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "first message" assistant "second message" user "third message"
|
||||
|
||||
# Stage the rebase editor output exactly as generated by build_rebase_content
|
||||
# This simulates the user doing nothing and just saving the editor
|
||||
@@ -181,7 +181,7 @@ test_drop_single_message() {
|
||||
local test_name="drop_single_message"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage the rebase editor output (drop message 1)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -240,7 +240,7 @@ test_edit_message_content() {
|
||||
local test_name="edit_message_content"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage the rebase editor output (edit message 1)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -312,7 +312,7 @@ test_change_message_role() {
|
||||
local test_name="change_message_role"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage the rebase editor output (edit message 1 role to user)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -377,7 +377,7 @@ test_delete_line_equals_drop() {
|
||||
local test_name="delete_line_equals_drop"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage rebase editor output with only message 0 mentioned
|
||||
# Messages not mentioned are implicitly picked (not dropped)
|
||||
@@ -430,7 +430,7 @@ test_edit_first_message() {
|
||||
local test_name="edit_first_message"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage the rebase editor output (edit message 0)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -494,7 +494,7 @@ test_edit_prompt_noop() {
|
||||
local test_name="edit_prompt_noop"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage the rebase editor output (edit message 1)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -565,7 +565,7 @@ test_empty_history_rejected() {
|
||||
local test_name="empty_history_rejected"
|
||||
|
||||
# Create a chat file with no history
|
||||
echo '{"history":[],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json
|
||||
|
||||
# Run drharness with -b flag
|
||||
set +e
|
||||
@@ -594,7 +594,7 @@ test_invalid_command_error() {
|
||||
local test_name="invalid_command_error"
|
||||
|
||||
# Create a chat file with a message
|
||||
echo '{"history":[{"role":"user","content":"hello"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello"
|
||||
|
||||
# Stage the rebase editor output with invalid command
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -644,7 +644,7 @@ test_invalid_index_error() {
|
||||
local test_name="invalid_index_error"
|
||||
|
||||
# Create a chat file with one message
|
||||
echo '{"history":[{"role":"user","content":"hello"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello"
|
||||
|
||||
# Stage the rebase editor output with out-of-range index
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -694,7 +694,7 @@ test_invalid_role_error() {
|
||||
local test_name="invalid_role_error"
|
||||
|
||||
# Create a chat file with a message
|
||||
echo '{"history":[{"role":"user","content":"hello"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello"
|
||||
|
||||
# Stage the rebase editor output with invalid role
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -744,7 +744,7 @@ test_multiple_edits() {
|
||||
local test_name="multiple_edits"
|
||||
|
||||
# Create a chat file with three messages
|
||||
echo '{"history":[{"role":"user","content":"msg0"},{"role":"assistant","content":"msg1"},{"role":"user","content":"msg2"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "msg0" assistant "msg1" user "msg2"
|
||||
|
||||
# Stage the rebase editor output (edit messages 0 and 2)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
@@ -828,7 +828,7 @@ test_edit_then_continue() {
|
||||
local test_name="edit_then_continue"
|
||||
|
||||
# Create a chat file with two messages
|
||||
echo '{"history":[{"role":"user","content":"hello"},{"role":"assistant","content":"hi there"}],"tools":[]}' > chat.json
|
||||
create_chat_file chat.json user "hello" assistant "hi there"
|
||||
|
||||
# Stage the rebase editor output (edit message 0)
|
||||
cat > .editor_input_0 << 'EDITEOF'
|
||||
|
||||
@@ -78,7 +78,7 @@ test_b_with_no_history() {
|
||||
local test_name="b_with_no_history"
|
||||
|
||||
# Create file with valid JSON but empty history
|
||||
echo '{"history":[],"tools":[]}' > no_history.json
|
||||
create_chat_file no_history.json
|
||||
|
||||
set +e
|
||||
OUTPUT=$("${DRHARNESS}" -b -F no_history.json 2>&1)
|
||||
|
||||
Reference in New Issue
Block a user