File size: 4,077 Bytes
c641d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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)