Spaces:
Sleeping

File size: 2,592 Bytes
aadb6d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
test_base_tool.py
base_tool.py のユニットテスト
"""
import os
import zipfile
import pytest

from tools.base_tool import get_session_paths, zip_output, BASE_TEMP_DIR


# ------------------------------------------------------------------ #
#  get_session_paths
# ------------------------------------------------------------------ #

def test_get_session_paths_returns_three_values():
    """3つのパスを返すことを確認"""
    session, input_p, output_p = get_session_paths("test-task-001")
    assert session is not None
    assert input_p is not None
    assert output_p is not None


def test_get_session_paths_structure(tmp_path, monkeypatch):
    """パスの構造が正しいことを確認"""
    monkeypatch.setattr("tools.base_tool.BASE_TEMP_DIR", str(tmp_path))
    task_id = "test-task-abc"
    session, input_p, output_p = get_session_paths(task_id)

    assert session == str(tmp_path / task_id)
    assert input_p  == str(tmp_path / task_id / "input")
    assert output_p == str(tmp_path / task_id / "output")


def test_get_session_paths_creates_output_dir(tmp_path, monkeypatch):
    """output ディレクトリが自動作成されることを確認"""
    monkeypatch.setattr("tools.base_tool.BASE_TEMP_DIR", str(tmp_path))
    _, _, output_p = get_session_paths("test-task-xyz")
    assert os.path.isdir(output_p)


# ------------------------------------------------------------------ #
#  zip_output
# ------------------------------------------------------------------ #

def test_zip_output_creates_zip(tmp_path):
    """result.zip が作成されることを確認"""
    session_path = tmp_path / "session"
    output_path  = tmp_path / "session" / "output"
    session_path.mkdir()
    output_path.mkdir()

    # ダミーファイルを出力ディレクトリに作成
    (output_path / "file1.txt").write_text("hello")
    (output_path / "file2.txt").write_text("world")

    zip_path = zip_output(str(session_path), str(output_path))

    assert os.path.isfile(zip_path)
    assert zip_path.endswith("result.zip")


def test_zip_output_contains_files(tmp_path):
    """ZIP の中身が正しいことを確認"""
    session_path = tmp_path / "session"
    output_path  = tmp_path / "session" / "output"
    session_path.mkdir()
    output_path.mkdir()

    (output_path / "a.txt").write_text("aaa")
    (output_path / "b.txt").write_text("bbb")

    zip_path = zip_output(str(session_path), str(output_path))

    with zipfile.ZipFile(zip_path, "r") as z:
        names = z.namelist()
    assert "a.txt" in names
    assert "b.txt" in names