Skip to content

Commit 14c853b

Browse files
committed
Add tests/test_cli.py
1 parent 9589c76 commit 14c853b

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

firefly/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
image_app = typer.Typer()
2121

2222

23-
mock_image = "https://developer.adobe.com/firefly-services/docs/static/82044b6fe3cf44ec68c4872f784cd82d/96d48/cat-coding.webp"
23+
mock_image = "https://developer.adobe.com/firefly-services/docs/static/82044b6fe3cf44ec68c4872f784cd82d/96d48/cat-coding.png"
2424

2525

2626
def use_requests_mock():
@@ -29,7 +29,8 @@ def use_requests_mock():
2929

3030
ims_url = "https://ims-na1.adobelogin.com/ims/token/v3"
3131
image_url = "https://firefly-api.adobe.io/v3/images/generate"
32-
with open("tests/images/cat-coding.webp", "rb") as img_file:
32+
mock_image_path = os.path.join(os.path.dirname(__file__), "..", "tests", "images", "cat-coding.png")
33+
with open(mock_image_path, "rb") as img_file:
3334
img_data = img_file.read()
3435

3536
rsps.add(

tests/test_cli.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import os
2+
import shutil
3+
import tempfile
4+
import pytest
5+
from typer.testing import CliRunner
6+
from unittest import mock
7+
from firefly.cli import app, mock_image
8+
9+
runner = CliRunner()
10+
11+
def test_generate_success_with_mocks(monkeypatch):
12+
result = runner.invoke(
13+
app,
14+
[
15+
"image", "generate",
16+
"--client-id", "dummy_id",
17+
"--client-secret", "dummy_secret",
18+
"--prompt", "a cat coding",
19+
"--use-mocks"
20+
]
21+
)
22+
assert result.exit_code == 0
23+
assert "Generated image URL:" in result.output
24+
assert mock_image in result.output
25+
26+
def test_generate_download_image(monkeypatch):
27+
# Use a temp dir to avoid polluting cwd
28+
with tempfile.TemporaryDirectory() as tmpdir:
29+
monkeypatch.chdir(tmpdir)
30+
result = runner.invoke(
31+
app,
32+
[
33+
"image", "generate",
34+
"--client-id", "dummy_id",
35+
"--client-secret", "dummy_secret",
36+
"--prompt", "a cat coding",
37+
"--use-mocks",
38+
"--download"
39+
]
40+
)
41+
assert result.exit_code == 0
42+
# The file should be downloaded with the correct name
43+
filename = os.path.basename(mock_image)
44+
assert os.path.exists(filename)
45+
with open(filename, "rb") as f:
46+
content = f.read()
47+
# Should match the test image
48+
with open(os.path.join(os.path.dirname(__file__), "images", "cat-coding.png"), "rb") as f:
49+
expected = f.read()
50+
assert content == expected
51+
assert f"Downloaded image (" in result.output
52+
53+
@mock.patch("subprocess.run")
54+
def test_generate_show_images(mock_run):
55+
result = runner.invoke(
56+
app,
57+
[
58+
"image", "generate",
59+
"--client-id", "dummy_id",
60+
"--client-secret", "dummy_secret",
61+
"--prompt", "a cat coding",
62+
"--use-mocks",
63+
"--show-images"
64+
]
65+
)
66+
assert result.exit_code == 0
67+
# Should attempt to call subprocess.run for imgcat
68+
assert mock_run.called
69+
assert "Generated image URL:" in result.output
70+
71+
@pytest.mark.parametrize("missing,expected", [
72+
("--client-id", "client_id must be provided"),
73+
("--client-secret", "client_secret must be provided"),
74+
])
75+
def test_generate_missing_required(monkeypatch, missing, expected):
76+
# Ensure env vars are unset
77+
monkeypatch.delenv("FIREFLY_CLIENT_ID", raising=False)
78+
monkeypatch.delenv("FIREFLY_CLIENT_SECRET", raising=False)
79+
args = [
80+
"image", "generate",
81+
"--prompt", "a cat coding",
82+
"--use-mocks"
83+
]
84+
if missing != "--client-id":
85+
args.extend(["--client-id", "dummy_id"])
86+
if missing != "--client-secret":
87+
args.extend(["--client-secret", "dummy_secret"])
88+
result = runner.invoke(app, args)
89+
assert result.exit_code != 0
90+
assert expected in result.output
91+
92+
def test_generate_json_output(monkeypatch):
93+
result = runner.invoke(
94+
app,
95+
[
96+
"image", "generate",
97+
"--client-id", "dummy_id",
98+
"--client-secret", "dummy_secret",
99+
"--prompt", "a cat coding",
100+
"--use-mocks",
101+
"--format", "json"
102+
]
103+
)
104+
assert result.exit_code == 0
105+
# Should look like pretty-printed JSON
106+
assert '{' in result.output and 'outputs' in result.output
107+
108+
def test_generate_verbose(monkeypatch):
109+
result = runner.invoke(
110+
app,
111+
[
112+
"image", "generate",
113+
"--client-id", "dummy_id",
114+
"--client-secret", "dummy_secret",
115+
"--prompt", "a cat coding",
116+
"--use-mocks",
117+
"--verbose"
118+
]
119+
)
120+
assert result.exit_code == 0
121+
# Should include verbose status messages
122+
assert "Doing request to" in result.output
123+
assert "Received HTTP" in result.output

0 commit comments

Comments
 (0)