Skip to content

Commit 4943ef0

Browse files
mnriemCopilot
andcommitted
test: add CLI-level tests for preset add --from GitHub release URL resolution
Adds regression tests covering: - resolve_github_release_asset_api_url unit tests (passthrough, resolution, network error, URL encoding of special chars in tags) - CLI-level 'preset add --from <github-release-url>' end-to-end flow - CLI-level 'preset add --from <api-asset-url>' direct passthrough Co-authored-by: Copilot <[email protected]>
1 parent b706cf3 commit 4943ef0

2 files changed

Lines changed: 239 additions & 2 deletions

File tree

‎tests/test_github_http.py‎

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
"""Tests for GitHub-authenticated HTTP request helpers."""
22

3+
import io
4+
import json
35
import os
4-
from unittest.mock import patch
6+
from contextlib import contextmanager
7+
from unittest.mock import MagicMock, patch
58

69
import pytest
710

811
from specify_cli._github_http import (
912
build_github_request,
13+
resolve_github_release_asset_api_url,
1014
)
1115

1216

@@ -76,4 +80,112 @@ def test_no_auth_header_when_no_token(self):
7680
def test_missing_hostname_raises_value_error(self):
7781
"""build_github_request() must reject URLs with valid scheme but no hostname."""
7882
with pytest.raises(ValueError, match="url must include a hostname"):
79-
build_github_request("http://")
83+
build_github_request("http://")
84+
85+
86+
class TestResolveGitHubReleaseAssetApiUrl:
87+
"""Tests for resolve_github_release_asset_api_url()."""
88+
89+
def _make_open_url_fn(self, release_json):
90+
"""Create a fake open_url_fn that returns release JSON."""
91+
@contextmanager
92+
def fake_open(url, timeout=None, extra_headers=None):
93+
resp = MagicMock()
94+
resp.read.return_value = json.dumps(release_json).encode()
95+
yield resp
96+
return fake_open
97+
98+
def test_returns_none_for_non_github_url(self):
99+
"""Non-GitHub URLs should return None."""
100+
result = resolve_github_release_asset_api_url(
101+
"https://example.com/file.zip", lambda *a, **kw: None
102+
)
103+
assert result is None
104+
105+
def test_returns_none_for_non_release_github_url(self):
106+
"""GitHub URLs that aren't release downloads return None."""
107+
result = resolve_github_release_asset_api_url(
108+
"https://github.com/org/repo/archive/refs/tags/v1.zip",
109+
lambda *a, **kw: None,
110+
)
111+
assert result is None
112+
113+
def test_passthrough_for_existing_api_asset_url(self):
114+
"""Already-resolved REST API asset URLs are returned as-is."""
115+
url = "https://api.github.com/repos/org/repo/releases/assets/12345"
116+
result = resolve_github_release_asset_api_url(url, lambda *a, **kw: None)
117+
assert result == url
118+
119+
def test_resolves_browser_url_to_api_url(self):
120+
"""Browser release URL resolves to REST API asset URL."""
121+
release_json = {
122+
"assets": [
123+
{"name": "pack.zip", "url": "https://api.github.com/repos/org/repo/releases/assets/99"}
124+
]
125+
}
126+
result = resolve_github_release_asset_api_url(
127+
"https://github.com/org/repo/releases/download/v1.0/pack.zip",
128+
self._make_open_url_fn(release_json),
129+
)
130+
assert result == "https://api.github.com/repos/org/repo/releases/assets/99"
131+
132+
def test_returns_none_when_asset_not_found(self):
133+
"""Returns None when the release exists but asset name doesn't match."""
134+
release_json = {"assets": [{"name": "other.zip", "url": "https://api.github.com/repos/org/repo/releases/assets/1"}]}
135+
result = resolve_github_release_asset_api_url(
136+
"https://github.com/org/repo/releases/download/v1/missing.zip",
137+
self._make_open_url_fn(release_json),
138+
)
139+
assert result is None
140+
141+
def test_returns_none_on_network_error(self):
142+
"""Returns None when the API request fails."""
143+
import urllib.error
144+
145+
@contextmanager
146+
def failing_open(url, timeout=None, extra_headers=None):
147+
raise urllib.error.URLError("network error")
148+
yield # noqa: unreachable
149+
150+
result = resolve_github_release_asset_api_url(
151+
"https://github.com/org/repo/releases/download/v1/pack.zip",
152+
failing_open,
153+
)
154+
assert result is None
155+
156+
def test_tag_with_special_characters_is_url_encoded(self):
157+
"""Tags with reserved characters (e.g. '/') are encoded in the API URL."""
158+
captured_urls = []
159+
160+
@contextmanager
161+
def capturing_open(url, timeout=None, extra_headers=None):
162+
captured_urls.append(url)
163+
resp = MagicMock()
164+
resp.read.return_value = json.dumps({"assets": []}).encode()
165+
yield resp
166+
167+
resolve_github_release_asset_api_url(
168+
"https://github.com/org/repo/releases/download/feature%2Fv1/pack.zip",
169+
capturing_open,
170+
)
171+
# The tag "feature/v1" (decoded from %2F) must be re-encoded as "feature%2Fv1"
172+
assert len(captured_urls) == 1
173+
assert "releases/tags/feature%2Fv1" in captured_urls[0]
174+
175+
def test_tag_with_hash_is_url_encoded(self):
176+
"""Tags with '#' character are properly encoded."""
177+
captured_urls = []
178+
179+
@contextmanager
180+
def capturing_open(url, timeout=None, extra_headers=None):
181+
captured_urls.append(url)
182+
resp = MagicMock()
183+
resp.read.return_value = json.dumps({"assets": []}).encode()
184+
yield resp
185+
186+
resolve_github_release_asset_api_url(
187+
"https://github.com/org/repo/releases/download/v1%23beta/pack.zip",
188+
capturing_open,
189+
)
190+
assert len(captured_urls) == 1
191+
assert "releases/tags/v1%23beta" in captured_urls[0]

‎tests/test_presets.py‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,7 @@ def test_extension_command_skipped_when_extension_missing(self, project_dir, tem
22392239
"description": "Test",
22402240
},
22412241
"requires": {"speckit_version": ">=0.1.0"},
2242+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
22422243
"provides": {
22432244
"templates": [
22442245
{
@@ -2283,6 +2284,7 @@ def test_extension_command_registered_when_extension_present(self, project_dir,
22832284
"description": "Test",
22842285
},
22852286
"requires": {"speckit_version": ">=0.1.0"},
2287+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
22862288
"provides": {
22872289
"templates": [
22882290
{
@@ -2542,6 +2544,7 @@ def _create_command_preset(self, temp_dir, preset_id, command_name, description,
25422544
"description": "Test",
25432545
},
25442546
"requires": {"speckit_version": ">=0.1.0"},
2547+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
25452548
"provides": {
25462549
"templates": [
25472550
{
@@ -2699,6 +2702,7 @@ def test_extension_restore_resolves_command_refs(self, project_dir, temp_dir):
26992702
"description": "Test",
27002703
},
27012704
"requires": {"speckit_version": ">=0.1.0"},
2705+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
27022706
"provides": {
27032707
"commands": [
27042708
{
@@ -2752,6 +2756,7 @@ def test_core_command_override_skill_uses_preset_command_description(self, proje
27522756
"description": "Test",
27532757
},
27542758
"requires": {"speckit_version": ">=0.1.0"},
2759+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
27552760
"provides": {
27562761
"templates": [
27572762
{
@@ -2992,6 +2997,7 @@ def test_extension_skill_override_matches_hyphenated_multisegment_name(self, pro
29922997
"description": "Test",
29932998
},
29942999
"requires": {"speckit_version": ">=0.1.0"},
3000+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
29953001
"provides": {
29963002
"templates": [
29973003
{
@@ -3044,6 +3050,7 @@ def test_extension_skill_restored_on_preset_remove(self, project_dir, temp_dir):
30443050
"description": "Test",
30453051
},
30463052
"requires": {"speckit_version": ">=0.1.0"},
3053+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
30473054
"provides": {
30483055
"commands": [
30493056
{
@@ -3072,6 +3079,7 @@ def test_extension_skill_restored_on_preset_remove(self, project_dir, temp_dir):
30723079
"description": "Test",
30733080
},
30743081
"requires": {"speckit_version": ">=0.1.0"},
3082+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
30753083
"provides": {
30763084
"templates": [
30773085
{
@@ -3125,6 +3133,7 @@ def test_preset_remove_skips_skill_dir_without_skill_file(self, project_dir, tem
31253133
"description": "Test",
31263134
},
31273135
"requires": {"speckit_version": ">=0.1.0"},
3136+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
31283137
"provides": {
31293138
"templates": [
31303139
{
@@ -3221,6 +3230,7 @@ def test_kimi_new_skill_created_even_when_ai_skills_disabled(self, project_dir,
32213230
"description": "Test",
32223231
},
32233232
"requires": {"speckit_version": ">=0.1.0"},
3233+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
32243234
"provides": {
32253235
"templates": [
32263236
{
@@ -3274,6 +3284,7 @@ def test_kimi_preset_skill_override_resolves_script_placeholders(self, project_d
32743284
"description": "Test",
32753285
},
32763286
"requires": {"speckit_version": ">=0.1.0"},
3287+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
32773288
"provides": {
32783289
"templates": [
32793290
{
@@ -3330,6 +3341,7 @@ def test_agy_skill_restored_on_preset_remove(self, project_dir, temp_dir):
33303341
"description": "Test",
33313342
},
33323343
"requires": {"speckit_version": ">=0.1.0"},
3344+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
33333345
"provides": {
33343346
"templates": [
33353347
{
@@ -3896,6 +3908,119 @@ def test_bundled_preset_missing_locally_cli_error(self, project_dir):
38963908
assert "reinstall" in output, result.output
38973909

38983910

3911+
class TestPresetAddFromUrlResolution:
3912+
"""CLI-level tests for preset add --from <url> GitHub release resolution."""
3913+
3914+
def test_preset_add_from_github_release_url_resolves_and_downloads(self, project_dir):
3915+
"""'preset add --from <github-release-url>' resolves to API asset URL."""
3916+
from typer.testing import CliRunner
3917+
from unittest.mock import patch, MagicMock
3918+
from specify_cli import app
3919+
3920+
manifest_content = yaml.dump({
3921+
"schema_version": "1.0",
3922+
"preset": {"id": "my-preset", "name": "My Preset", "version": "1.0.0", "description": "Test preset", "author": "Test", "license": "MIT"},
3923+
"requires": {"speckit_version": ">=0.1.0"},
3924+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
3925+
})
3926+
zip_buf = __import__("io").BytesIO()
3927+
with zipfile.ZipFile(zip_buf, "w") as zf:
3928+
zf.writestr("preset.yml", manifest_content)
3929+
zip_bytes = zip_buf.getvalue()
3930+
3931+
captured_urls = []
3932+
3933+
class FakeResponse:
3934+
def __init__(self, data):
3935+
self._data = data
3936+
3937+
def read(self):
3938+
return self._data
3939+
3940+
def __enter__(self):
3941+
return self
3942+
3943+
def __exit__(self, *a):
3944+
return False
3945+
3946+
def fake_open_url(url, timeout=None, extra_headers=None):
3947+
captured_urls.append((url, extra_headers))
3948+
if "releases/tags/" in url:
3949+
return FakeResponse(json.dumps({
3950+
"assets": [{"name": "preset.zip", "url": "https://api.github.com/repos/org/repo/releases/assets/42"}]
3951+
}).encode())
3952+
return FakeResponse(zip_bytes)
3953+
3954+
runner = CliRunner()
3955+
with patch.object(Path, "cwd", return_value=project_dir), \
3956+
patch("specify_cli.get_speckit_version", return_value="1.0.0"), \
3957+
patch("specify_cli.authentication.http.open_url", side_effect=fake_open_url):
3958+
result = runner.invoke(app, [
3959+
"preset", "add",
3960+
"--from", "https://github.com/org/repo/releases/download/v1.0/preset.zip",
3961+
])
3962+
3963+
assert result.exit_code == 0, result.output
3964+
assert "My Preset" in result.output
3965+
# First call should resolve the release tag
3966+
assert any("releases/tags/v1.0" in url for url, _ in captured_urls)
3967+
# Second call should download from the resolved asset URL with octet-stream
3968+
asset_calls = [(url, h) for url, h in captured_urls if "releases/assets/" in url]
3969+
assert len(asset_calls) >= 1
3970+
assert asset_calls[0][1] == {"Accept": "application/octet-stream"}
3971+
3972+
def test_preset_add_from_direct_api_asset_url_passes_through(self, project_dir):
3973+
"""'preset add --from <api-asset-url>' uses URL directly with octet-stream."""
3974+
from typer.testing import CliRunner
3975+
from unittest.mock import patch
3976+
from specify_cli import app
3977+
3978+
manifest_content = yaml.dump({
3979+
"schema_version": "1.0",
3980+
"preset": {"id": "my-preset", "name": "My Preset", "version": "1.0.0", "description": "Test preset", "author": "Test", "license": "MIT"},
3981+
"requires": {"speckit_version": ">=0.1.0"},
3982+
"provides": {"templates": [{"type": "template", "name": "t", "file": "templates/t.md", "description": "t"}]},
3983+
})
3984+
zip_buf = __import__("io").BytesIO()
3985+
with zipfile.ZipFile(zip_buf, "w") as zf:
3986+
zf.writestr("preset.yml", manifest_content)
3987+
zip_bytes = zip_buf.getvalue()
3988+
3989+
captured_urls = []
3990+
3991+
class FakeResponse:
3992+
def __init__(self, data):
3993+
self._data = data
3994+
3995+
def read(self):
3996+
return self._data
3997+
3998+
def __enter__(self):
3999+
return self
4000+
4001+
def __exit__(self, *a):
4002+
return False
4003+
4004+
def fake_open_url(url, timeout=None, extra_headers=None):
4005+
captured_urls.append((url, extra_headers))
4006+
return FakeResponse(zip_bytes)
4007+
4008+
runner = CliRunner()
4009+
with patch.object(Path, "cwd", return_value=project_dir), \
4010+
patch("specify_cli.get_speckit_version", return_value="1.0.0"), \
4011+
patch("specify_cli.authentication.http.open_url", side_effect=fake_open_url):
4012+
result = runner.invoke(app, [
4013+
"preset", "add",
4014+
"--from", "https://api.github.com/repos/org/repo/releases/assets/42",
4015+
])
4016+
4017+
assert result.exit_code == 0, result.output
4018+
# Should go directly to the asset URL with Accept header
4019+
assert len(captured_urls) == 1
4020+
assert captured_urls[0][0] == "https://api.github.com/repos/org/repo/releases/assets/42"
4021+
assert captured_urls[0][1] == {"Accept": "application/octet-stream"}
4022+
4023+
38994024
class TestWrapStrategy:
39004025
"""Tests for strategy: wrap preset command substitution."""
39014026

0 commit comments

Comments
 (0)