Files
daily-journal-prompt/test_prompt_logic.py

99 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify the prompt numbering logic.
"""
import json
import configparser
def get_num_prompts():
"""Get the number of prompts from settings.cfg or default."""
config = configparser.ConfigParser()
num_prompts = 6 # Default value
try:
config.read('settings.cfg')
if 'prompts' in config and 'num_prompts' in config['prompts']:
num_prompts = int(config['prompts']['num_prompts'])
except (FileNotFoundError, ValueError):
pass
return num_prompts
def test_renumbering():
"""Test the renumbering logic."""
# Get number of prompts from config
num_prompts = get_num_prompts()
# Create a sample historic prompts list
historic_prompts = []
for i in range(60):
historic_prompts.append({
f"prompt{i:02d}": f"Old prompt {i}"
})
print(f"Original prompts: {len(historic_prompts)}")
print(f"First prompt key: {list(historic_prompts[0].keys())[0]}")
print(f"Last prompt key: {list(historic_prompts[-1].keys())[0]}")
print(f"Number of prompts from config: {num_prompts}")
# Simulate adding new prompts (as the current code would create them)
new_prompts = []
for i in range(num_prompts):
new_prompts.append({
f"prompt{len(historic_prompts) + i:02d}": f"New prompt {i}"
})
print(f"\nNew prompts to add: {len(new_prompts)}")
for i, prompt in enumerate(new_prompts):
print(f" New prompt {i}: {list(prompt.keys())[0]}")
# Prepend new prompts (reverse to maintain order)
for prompt in reversed(new_prompts):
historic_prompts.insert(0, prompt)
print(f"\nAfter prepending: {len(historic_prompts)} prompts")
print(f"First 3 prompts keys:")
for i in range(3):
print(f" {i}: {list(historic_prompts[i].keys())[0]}")
# Renumber all prompts
renumbered_prompts = []
for i, prompt_dict in enumerate(historic_prompts):
prompt_key = list(prompt_dict.keys())[0]
prompt_text = prompt_dict[prompt_key]
new_prompt_key = f"prompt{i:02d}"
renumbered_prompts.append({
new_prompt_key: prompt_text
})
print(f"\nAfter renumbering: {len(renumbered_prompts)} prompts")
print(f"First 10 prompts keys:")
for i in range(10):
print(f" prompt{i:02d}: {list(renumbered_prompts[i].keys())[0]} = {renumbered_prompts[i][f'prompt{i:02d}'][:30]}...")
# Keep only first 60
if len(renumbered_prompts) > 60:
renumbered_prompts = renumbered_prompts[:60]
print(f"\nAfter keeping only first 60: {len(renumbered_prompts)} prompts")
print(f"First prompt: {list(renumbered_prompts[0].keys())[0]} = {renumbered_prompts[0]['prompt00'][:30]}...")
print(f"Last prompt: {list(renumbered_prompts[-1].keys())[0]} = {renumbered_prompts[-1]['prompt59'][:30]}...")
# Verify the range
for i in range(60):
expected_key = f"prompt{i:02d}"
actual_key = list(renumbered_prompts[i].keys())[0]
if expected_key != actual_key:
print(f"ERROR: Expected {expected_key}, got {actual_key}")
return False
print("\n✅ All tests passed! Prompt numbering is correct.")
return True
if __name__ == "__main__":
test_renumbering()