| from pathlib import Path | |
| import pytest | |
| from tools.markdown_logic import analyze_markdown_operation, solve_markdown_question | |
| from tools.python_exec import execute_python_file | |
| from tools.spreadsheet import ( | |
| answer_spreadsheet_question, | |
| describe_workbook, | |
| filter_rows, | |
| read_sheet, | |
| sum_column, | |
| ) | |
| from tools.text_transform import solve_text_transformation | |
| from tools.video import extract_contact_sheets | |
| def test_reversed_prompt_literal_answer(): | |
| prompt = 'Write "right" as the answer.'[::-1] | |
| assert solve_text_transformation(prompt) == "right" | |
| def test_reversed_prompt_opposite_answer(): | |
| prompt = 'If you understand this sentence, write the opposite of the word "left" as the answer.'[ | |
| ::-1 | |
| ] | |
| assert solve_text_transformation(prompt) == "right" | |
| def test_markdown_group_analysis(): | |
| table = """|*|e|a| | |
| |--|--|--| | |
| |e|e|a| | |
| |a|a|e|""" | |
| result = analyze_markdown_operation(table) | |
| assert '"commutative": true' in result | |
| assert '"associative": true' in result | |
| assert '"identities": ["e"]' in result | |
| question = ( | |
| table | |
| + "\nProvide elements involved in counter-examples proving it is not commutative." | |
| ) | |
| assert solve_markdown_question(question) == "" | |
| def test_python_execution_and_blocking(tmp_path: Path): | |
| safe = tmp_path / "safe.py" | |
| safe.write_text("print(sum(range(5)))\n", encoding="utf-8") | |
| assert execute_python_file(safe) == "10" | |
| seeded = tmp_path / "seeded.py" | |
| seeded.write_text("import random\nrandom.seed(1)\nprint(random.randint(1, 9))\n") | |
| assert execute_python_file(seeded) == "3" | |
| unsafe = tmp_path / "unsafe.py" | |
| unsafe.write_text("import subprocess\n", encoding="utf-8") | |
| with pytest.raises(ValueError, match="Blocked import"): | |
| execute_python_file(unsafe) | |
| def test_food_sales_excludes_beverages(tmp_path: Path): | |
| pandas = pytest.importorskip("pandas") | |
| workbook = tmp_path / "sales.xlsx" | |
| pandas.DataFrame( | |
| {"Item": ["Burger", "Cola", "Fries"], "Sales": [12.5, 3.0, 4.25]} | |
| ).to_excel(workbook, index=False) | |
| question = "What were the total sales from food, not including drinks?" | |
| assert answer_spreadsheet_question(question, workbook) == "$16.75" | |
| assert '"headers": ["Item", "Sales"]' in describe_workbook(workbook) | |
| assert read_sheet(workbook, "Sheet1")[0]["Item"] == "Burger" | |
| assert len(filter_rows(workbook, "Sheet1", "Item", "Cola")) == 1 | |
| assert ( | |
| sum_column(workbook, "Sheet1", "Sales", "Item", "Cola", exclude=True) == 16.75 | |
| ) | |
| from openpyxl import load_workbook | |
| editable = load_workbook(workbook) | |
| editable["Sheet1"]["C1"] = "Double" | |
| editable["Sheet1"]["C2"] = "=B2*2" | |
| editable.save(workbook) | |
| assert '"formula_count": 1' in describe_workbook(workbook) | |
| wide = tmp_path / "wide.xlsx" | |
| pandas.DataFrame( | |
| { | |
| "Location": ["A", "B"], | |
| "Burgers": [10, 20], | |
| "Fries": [4, 6], | |
| "Soda": [100, 200], | |
| } | |
| ).to_excel(wide, index=False) | |
| assert answer_spreadsheet_question(question, wide) == "$40.00" | |
| def test_extended_text_transformations(): | |
| assert solve_text_transformation('Apply ROT13 to "uryyb"') == "hello" | |
| assert solve_text_transformation('Sort "10, 2, -1" numerically') == "-1, 2, 10" | |
| assert solve_text_transformation("Calculate 2 * (3 + 4)") == "14" | |
| assert ( | |
| solve_text_transformation('Extract the 2nd word from "alpha beta gamma"') | |
| == "beta" | |
| ) | |
| def test_video_contact_sheet_extraction(tmp_path: Path): | |
| import imageio_ffmpeg | |
| video = tmp_path / "sample.mp4" | |
| writer = imageio_ffmpeg.write_frames(str(video), (32, 32), fps=2) | |
| writer.send(None) | |
| for shade in (0, 64, 128, 255): | |
| writer.send(bytes([shade, shade, shade]) * (32 * 32)) | |
| writer.close() | |
| sheets = extract_contact_sheets(video, interval_seconds=0.5, max_frames=4) | |
| assert len(sheets) == 1 | |
| assert sheets[0].size == (960, 700) | |