File size: 4,323 Bytes
edc06cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
import json
import shutil
from pathlib import Path
from typing import Any

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient

from voicevox_engine.app.application import generate_app
from voicevox_engine.core.core_initializer import initialize_cores
from voicevox_engine.engine_manifest import load_manifest
from voicevox_engine.library.library_manager import LibraryManager
from voicevox_engine.preset.preset_manager import PresetManager
from voicevox_engine.setting.setting_manager import SettingHandler
from voicevox_engine.tts_pipeline.tts_engine import make_tts_engines_from_cores
from voicevox_engine.user_dict.user_dict_manager import (
    DEFAULT_DICT_PATH,
    UserDictionary,
)
from voicevox_engine.utility.path_utility import engine_manifest_path, get_save_dir


def _copy_under_dir(file_path: Path, dir_path: Path) -> Path:
    """ๆŒ‡ๅฎšใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชไธ‹ใธใƒ•ใ‚กใ‚คใƒซใ‚’ใ‚ณใƒ”ใƒผใ—ใ€็”Ÿๆˆใ•ใ‚ŒใŸใƒ•ใ‚กใ‚คใƒซใฎใƒ‘ใ‚นใ‚’่ฟ”ใ™ใ€‚"""
    copied_file_path = dir_path / file_path.name
    shutil.copyfile(file_path, copied_file_path)
    return copied_file_path


@pytest.fixture()
def app_params(tmp_path: Path) -> dict[str, Any]:
    core_manager = initialize_cores(use_gpu=False, enable_mock=True)
    tts_engines = make_tts_engines_from_cores(core_manager)
    setting_loader = SettingHandler(tmp_path / "not_exist.yaml")

    # ใƒ†ใ‚นใƒˆ็”จใซ้š”้›ขใ•ใ‚ŒใŸใƒ—ใƒชใ‚ปใƒƒใƒˆใ‚’็”Ÿๆˆใ™ใ‚‹
    preset_path = Path("./presets.yaml")
    preset_manager = PresetManager(_copy_under_dir(preset_path, tmp_path))

    # ใƒ†ใ‚นใƒˆ็”จใซ้š”้›ขใ•ใ‚ŒใŸใƒฆใƒผใ‚ถใƒผ่พžๆ›ธใ‚’็”Ÿๆˆใ™ใ‚‹
    user_dict = UserDictionary(
        default_dict_path=_copy_under_dir(DEFAULT_DICT_PATH, tmp_path),
        user_dict_path=_generate_user_dict(tmp_path),
        compiled_dict_path=tmp_path / "user.dic",
    )

    engine_manifest = load_manifest(engine_manifest_path())
    library_manager = LibraryManager(
        get_save_dir() / "installed_libraries",
        engine_manifest.supported_vvlib_manifest_version,
        engine_manifest.brand_name,
        engine_manifest.name,
        engine_manifest.uuid,
    )

    return {
        "tts_engines": tts_engines,
        "core_manager": core_manager,
        "setting_loader": setting_loader,
        "preset_manager": preset_manager,
        "user_dict": user_dict,
        "engine_manifest": engine_manifest,
        "library_manager": library_manager,
    }


@pytest.fixture()
def app(app_params: dict) -> FastAPI:
    return generate_app(**app_params)


@pytest.fixture()
def client(app: FastAPI) -> TestClient:
    return TestClient(app)


def _generate_user_dict(dir_path: Path) -> Path:
    """ๆŒ‡ๅฎšใ•ใ‚ŒใŸใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชไธ‹ใซใƒฆใƒผใ‚ถใƒผ่พžๆ›ธใƒ•ใ‚กใ‚คใƒซใ‚’็”Ÿๆˆใ—ใ€็”Ÿๆˆใ•ใ‚ŒใŸใƒ•ใ‚กใ‚คใƒซใฎใƒ‘ใ‚นใ‚’่ฟ”ใ™ใ€‚"""
    contents = {
        "a89596ad-caa8-4f4e-8eb3-3d2261c798fd": {
            "surface": "๏ฝ”๏ฝ…๏ฝ“๏ฝ”๏ผ‘",
            "context_id": 1348,
            "part_of_speech": "ๅ่ฉž",
            "part_of_speech_detail_1": "ๅ›บๆœ‰ๅ่ฉž",
            "part_of_speech_detail_2": "ไธ€่ˆฌ",
            "part_of_speech_detail_3": "*",
            "inflectional_type": "*",
            "inflectional_form": "*",
            "stem": "*",
            "yomi": "ใƒ†ใ‚นใƒˆใ‚คใƒ",
            "pronunciation": "ใƒ†ใ‚นใƒˆใ‚คใƒ",
            "accent_type": 1,
            "mora_count": 3,
            "accent_associative_rule": "*",
            "cost": 8609,
        },
        "c89596ad-caa8-4f4e-8eb3-3d2261c798fd": {
            "surface": "๏ฝ”๏ฝ…๏ฝ“๏ฝ”๏ผ’",
            "context_id": 1348,
            "part_of_speech": "ๅ่ฉž",
            "part_of_speech_detail_1": "ๅ›บๆœ‰ๅ่ฉž",
            "part_of_speech_detail_2": "ไธ€่ˆฌ",
            "part_of_speech_detail_3": "*",
            "inflectional_type": "*",
            "inflectional_form": "*",
            "stem": "*",
            "yomi": "ใƒ†ใ‚นใƒˆใƒ‹",
            "pronunciation": "ใƒ†ใ‚นใƒˆใƒ‹",
            "accent_type": 1,
            "mora_count": 2,
            "accent_associative_rule": "*",
            "cost": 8608,
        },
    }
    contents_json = json.dumps(contents, ensure_ascii=False)

    file_path = dir_path / "user_dict_for_test.json"
    file_path.write_text(contents_json, encoding="utf-8")

    return file_path