Skip to content

Commit f01fe68

Browse files
committed
refactor(all): ♻️ 🤖 💫 Automated AI magic at work
1 parent f2fbd19 commit f01fe68

11 files changed

+57
-30
lines changed

.husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bun x lint-staged

.husky/prepare-commit-msg

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bun x devmoji --edit --lint

audiokit/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def search_audio(
269269
"""Search audio analyses with natural language."""
270270
logger.info("Searching audio with query: {}", query)
271271

272-
return audio_index.search(query, n_results, filters)
272+
return audio_index.search_audio(query, n_results, filters)
273273

274274
# Create global instance
275275
ak = AudioKit()

cleaned.wav

-172 KB
Binary file not shown.

lint-staged.config.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
'**/*.ts': () => 'tsc --noEmit',
3+
'**/*.{ts,js,md,svelte,css,html,json}': ['eslint --fix', 'prettier --write --list-different'],
4+
};

tests/conftest.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,16 @@ def setup_test_env():
4242
os.environ["AUDIOKIT_LOG_LEVEL"] = "DEBUG" # Ensure DEBUG level
4343

4444
# Initialize logging
45-
get_logger()
45+
get_logger()
46+
47+
@pytest.fixture(autouse=True)
48+
def change_to_temp_dir(tmp_path, monkeypatch):
49+
"""
50+
Automatically change the current working directory to a temporary subdirectory
51+
within the test's tmp_path. This ensures that any files (e.g. .wav files) generated
52+
by the tests using relative paths are created in this folder rather than in the project root.
53+
"""
54+
generated_dir = tmp_path / "generated_audio"
55+
generated_dir.mkdir()
56+
monkeypatch.chdir(generated_dir)
57+
print("Current working directory changed to:", os.getcwd())

tests/test_cli.py

+21-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from time import time, sleep
88
from rich.console import Console # import Rich's Console to enable recording
99
import re
10-
import io
1110

1211
# Use the package logger consistently
1312
logger = get_logger(__name__)
@@ -23,38 +22,35 @@ def setup_config():
2322
os.environ.pop("AUDIOKIT_LOG_LEVEL", None)
2423
os.environ.pop("AUDIOKIT_LOG_FILE", None)
2524

26-
runner = CliRunner()
27-
28-
class AccumulatingStream(io.StringIO):
29-
def __init__(self, *args, **kwargs):
30-
super().__init__(*args, **kwargs)
31-
self.accumulated = []
32-
33-
def write(self, s):
34-
self.accumulated.append(s)
35-
return super().write(s)
25+
@pytest.fixture(autouse=True)
26+
def patch_console(monkeypatch):
27+
"""
28+
Monkeypatch the global console in audiokit.cli to use a recording Console.
29+
"""
30+
import audiokit.cli as cli # import the module that created the console
31+
recording_console = Console(force_terminal=True, record=True)
32+
monkeypatch.setattr(cli, "console", recording_console)
33+
return recording_console
3634

37-
def get_accumulated(self):
38-
return ''.join(self.accumulated)
35+
runner = CliRunner()
3936

4037
def strip_ansi(text: str) -> str:
4138
# Regular expression for ANSI escape sequences
4239
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
4340
return ansi_escape.sub('', text)
4441

45-
def test_cli_analyze(sample_audio_path):
42+
def test_cli_analyze(sample_audio_path, patch_console):
4643
"""Test CLI analyze command"""
4744
logger.info("Testing CLI analyze command")
4845

49-
# Create an accumulating stream and override the runner's output stream method.
50-
accum_stream = AccumulatingStream()
51-
original_method = runner._get_output_stream
52-
runner._get_output_stream = lambda: accum_stream
5346
result = runner.invoke(
5447
app, ["analyze", str(sample_audio_path)], catch_exceptions=False
5548
)
56-
runner._get_output_stream = original_method
57-
final_output = accum_stream.get_accumulated()
49+
# Allow a brief pause to let all output be recorded.
50+
sleep(0.2)
51+
final_output = patch_console.export_text(clear=False)
52+
if not final_output:
53+
final_output = result.stdout
5854

5955
logger.debug("CLI analyze command rendered output: {}", final_output)
6056
logger.debug("CLI analyze command exit code: {}", result.exit_code)
@@ -75,13 +71,9 @@ def test_cli_analyze(sample_audio_path):
7571
assert "0.85" in final_output_clean # Guitar
7672
assert "0.90" in final_output_clean # Drums
7773

78-
def test_cli_process(sample_audio_path, tmp_path):
74+
def test_cli_process(sample_audio_path, tmp_path, patch_console):
7975
"""Test CLI process command"""
8076
logger.info("Testing CLI process command")
81-
# Create an accumulating stream and override the runner's output stream method.
82-
accum_stream = AccumulatingStream()
83-
original_method = runner._get_output_stream
84-
runner._get_output_stream = lambda: accum_stream
8577
result = runner.invoke(
8678
app, [
8779
"process",
@@ -91,8 +83,10 @@ def test_cli_process(sample_audio_path, tmp_path):
9183
],
9284
catch_exceptions=False
9385
)
94-
runner._get_output_stream = original_method
95-
final_output = accum_stream.get_accumulated()
86+
sleep(0.2)
87+
final_output = patch_console.export_text(clear=False)
88+
if not final_output:
89+
final_output = result.stdout
9690

9791
logger.debug("CLI process command rendered output: {}", final_output)
9892
logger.debug("CLI process command exit code: {}", result.exit_code)

tests/test_indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from audiokit import ak
33
from audiokit.core.indexing import audio_index
44
from audiokit.core.logging import get_logger
5+
import json
56

67
logger = get_logger(__name__)
78

@@ -13,7 +14,7 @@ def test_index_data(sample_audio_path):
1314

1415
results = audio_index.search_audio("test", 1)
1516
assert len(results) > 0
16-
assert results[0]["content"] == test_data
17+
assert json.loads(results[0]["content"]) == test_data
1718

1819
def test_search_audio(sample_audio_path):
1920
"""Test audio search"""

title=tests/conftest.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
import os
3+
4+
# This fixture automatically changes the working directory for each test.
5+
# Any output files (e.g. .wav files) generated by the tested code using relative paths
6+
# will be created in a temporary directory instead of in the project root.
7+
@pytest.fixture(autouse=True)
8+
def change_to_temp_dir(tmp_path, monkeypatch):
9+
# Optionally, you can create a subdirectory (e.g., "generated_audio") inside tmp_path:
10+
generated_dir = tmp_path / "generated_audio"
11+
generated_dir.mkdir()
12+
monkeypatch.chdir(generated_dir)
13+
# Debug: print the current working directory (remove or comment in production)
14+
print("Current working directory changed to:", os.getcwd())

vocals.wav

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)