optforge 0.3.9__tar.gz → 0.3.10__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. {optforge-0.3.9 → optforge-0.3.10}/PKG-INFO +1 -1
  2. optforge-0.3.10/src/optforge/_version.py +1 -0
  3. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/service.py +10 -2
  4. {optforge-0.3.9 → optforge-0.3.10}/tests/test_cli.py +13 -1
  5. {optforge-0.3.9 → optforge-0.3.10}/tests/test_portcheck.py +21 -0
  6. optforge-0.3.10/tests/test_volume.py +454 -0
  7. optforge-0.3.9/src/optforge/_version.py +0 -1
  8. optforge-0.3.9/tests/test_volume.py +0 -108
  9. {optforge-0.3.9 → optforge-0.3.10}/.gitignore +0 -0
  10. {optforge-0.3.9 → optforge-0.3.10}/LICENSE +0 -0
  11. {optforge-0.3.9 → optforge-0.3.10}/Makefile +0 -0
  12. {optforge-0.3.9 → optforge-0.3.10}/README.md +0 -0
  13. {optforge-0.3.9 → optforge-0.3.10}/pyproject.toml +0 -0
  14. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/__init__.py +0 -0
  15. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/__main__.py +0 -0
  16. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/cli.py +0 -0
  17. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/__init__.py +0 -0
  18. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/cli.py +0 -0
  19. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/console.py +0 -0
  20. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/errors.py +0 -0
  21. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/formatting.py +0 -0
  22. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/net.py +0 -0
  23. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/processes.py +0 -0
  24. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/redaction.py +0 -0
  25. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/core/version.py +0 -0
  26. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/diskcheck/__init__.py +0 -0
  27. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/diskcheck/cli.py +0 -0
  28. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/diskcheck/models.py +0 -0
  29. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/diskcheck/service.py +0 -0
  30. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/envcheck/__init__.py +0 -0
  31. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/envcheck/cli.py +0 -0
  32. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/envcheck/models.py +0 -0
  33. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/envcheck/service.py +0 -0
  34. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/killNotif/__init__.py +0 -0
  35. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/killNotif/cli.py +0 -0
  36. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/killNotif/core.py +0 -0
  37. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/killNotif/job.ps1 +0 -0
  38. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/__init__.py +0 -0
  39. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/binder.py +0 -0
  40. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/cli.py +0 -0
  41. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/connector.py +0 -0
  42. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/models.py +0 -0
  43. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/scanner.py +0 -0
  44. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/portcheck/waiter.py +0 -0
  45. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/py.typed +0 -0
  46. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/pycacheclean/__init__.py +0 -0
  47. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/pycacheclean/cleaner.py +0 -0
  48. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/pycacheclean/cli.py +0 -0
  49. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/pycacheclean/models.py +0 -0
  50. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/pycacheclean/scanner.py +0 -0
  51. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/pycacheclean/service.py +0 -0
  52. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/volume/__init__.py +0 -0
  53. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/volume/cli.py +0 -0
  54. {optforge-0.3.9 → optforge-0.3.10}/src/optforge/volume/core.py +0 -0
  55. {optforge-0.3.9 → optforge-0.3.10}/tests/test_console.py +0 -0
  56. {optforge-0.3.9 → optforge-0.3.10}/tests/test_diskcheck.py +0 -0
  57. {optforge-0.3.9 → optforge-0.3.10}/tests/test_envcheck.py +0 -0
  58. {optforge-0.3.9 → optforge-0.3.10}/tests/test_formatting.py +0 -0
  59. {optforge-0.3.9 → optforge-0.3.10}/tests/test_killnotif.py +0 -0
  60. {optforge-0.3.9 → optforge-0.3.10}/tests/test_net.py +0 -0
  61. {optforge-0.3.9 → optforge-0.3.10}/tests/test_processes.py +0 -0
  62. {optforge-0.3.9 → optforge-0.3.10}/tests/test_pycacheclean.py +0 -0
  63. {optforge-0.3.9 → optforge-0.3.10}/tests/test_redaction.py +0 -0
  64. {optforge-0.3.9 → optforge-0.3.10}/tests/test_version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: optforge
3
- Version: 0.3.9
3
+ Version: 0.3.10
4
4
  Summary: Cross-platform developer CLI tools for ports, environment variables, disk usage, and Python cache cleanup.
5
5
  Project-URL: Homepage, https://github.com/cmd-3324/OptForge
6
6
  Project-URL: Repository, https://github.com/cmd-3324/OptForge
@@ -0,0 +1 @@
1
+ __version__ = "0.3.10"
@@ -2,9 +2,11 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import contextlib
5
6
  from collections.abc import Callable
6
7
  from typing import Any
7
8
 
9
+ from optforge.core.errors import PermissionDenied
8
10
  from optforge.portcheck.binder import PortBinder
9
11
  from optforge.portcheck.connector import Connector
10
12
  from optforge.portcheck.models import (
@@ -63,8 +65,14 @@ class PortService:
63
65
  return [row for row in self._scanner.scan() if row.port == port]
64
66
 
65
67
  def check(self, port: int, host: str = DEFAULT_HOST) -> CheckResult:
66
- """Answer whether ``port`` is free, using owners plus a bind probe."""
67
- owners = self.find(port)
68
+ """Answer whether ``port`` is free, using owners plus a bind probe.
69
+
70
+ The bind probe answers on its own, so an unreadable socket table (macOS
71
+ without root) degrades to "no owner details" instead of failing.
72
+ """
73
+ owners: list[Listener] = []
74
+ with contextlib.suppress(PermissionDenied):
75
+ owners = self.find(port)
68
76
  bindable = self._binder.is_free(port, host)
69
77
  return CheckResult(
70
78
  port=port,
@@ -9,11 +9,13 @@ import pytest
9
9
  from typer.testing import CliRunner
10
10
 
11
11
  from optforge.cli import app as dispatcher
12
+ from optforge.core.errors import OptForgeError
12
13
  from optforge.core.version import get_version
13
14
  from optforge.diskcheck.cli import app as diskcheck_app
14
15
  from optforge.envcheck.cli import app as envcheck_app
15
16
  from optforge.killNotif import cli as killnotif_cli
16
17
  from optforge.portcheck.cli import app as portcheck_app
18
+ from optforge.portcheck.scanner import ListenerScanner
17
19
  from optforge.pycacheclean.cli import app as pycache_app
18
20
 
19
21
  runner = CliRunner()
@@ -70,8 +72,18 @@ def test_portcheck_check_answers_no_for_a_busy_port() -> None:
70
72
  assert runner.invoke(portcheck_app, ["check", port, "--expect", "free", "-H", "127.0.0.1"]).exit_code == 1
71
73
 
72
74
 
75
+ def _socket_table_readable() -> bool:
76
+ try:
77
+ ListenerScanner().scan()
78
+ except OptForgeError:
79
+ return False
80
+ return True
81
+
82
+
73
83
  def test_portcheck_find_answers_no_for_a_free_port() -> None:
74
- assert runner.invoke(portcheck_app, ["find", str(_free_port())]).exit_code == 1
84
+ result = runner.invoke(portcheck_app, ["find", str(_free_port())])
85
+ # macOS hides the socket table from normal users, where "could not run" is the honest answer.
86
+ assert result.exit_code == (1 if _socket_table_readable() else 3)
75
87
 
76
88
 
77
89
  def test_portcheck_rejects_an_out_of_range_port() -> None:
@@ -222,6 +222,27 @@ def test_reason_maps_socket_errors(exc: OSError, expected: str) -> None:
222
222
  assert _reason(exc) == expected
223
223
 
224
224
 
225
+ class UnreadableScanner:
226
+ def scan(self) -> list[Listener]:
227
+ raise PermissionDenied("Run as Administrator (Windows) or with sudo (Linux/macOS).")
228
+
229
+
230
+ def test_check_answers_from_the_bind_probe_without_a_socket_table() -> None:
231
+ service = PortService(scanner=UnreadableScanner(), binder=PortBinder())
232
+
233
+ free = service.check(free_port(), "127.0.0.1")
234
+ assert free.owners == []
235
+ assert free.in_use is False
236
+
237
+ server = listening_socket()
238
+ try:
239
+ busy = service.check(int(server.getsockname()[1]), "127.0.0.1")
240
+ finally:
241
+ server.close()
242
+ assert busy.owners == []
243
+ assert busy.in_use is True
244
+
245
+
225
246
  def test_binder_reports_a_bound_port_as_busy() -> None:
226
247
  server = listening_socket()
227
248
  port = server.getsockname()[1]
@@ -0,0 +1,454 @@
1
+ from __future__ import annotations
2
+
3
+ import subprocess
4
+ from types import SimpleNamespace
5
+
6
+ import pytest
7
+ from typer.testing import CliRunner
8
+
9
+ from optforge.core.errors import InvalidInput, OperationFailed
10
+ from optforge.volume import cli as volume_cli
11
+ from optforge.volume import core as volume_core
12
+ from optforge.volume.cli import app
13
+ from optforge.volume.core import (
14
+ MAX_VOLUME,
15
+ MIN_VOLUME,
16
+ LinuxVolumeBackend,
17
+ MacOSVolumeBackend,
18
+ VolumeController,
19
+ WindowsVolumeBackend,
20
+ _clamp,
21
+ _detect_linux_tool,
22
+ _parse_percent,
23
+ _validate,
24
+ _validate_step,
25
+ )
26
+
27
+
28
+ class FakeBackend:
29
+ name = "fake"
30
+
31
+ def __init__(self, volume: int = 36, muted: bool = False) -> None:
32
+ self.volume = volume
33
+ self.muted = muted
34
+
35
+ def get(self) -> int:
36
+ return self.volume
37
+
38
+ def set(self, percent: int) -> None:
39
+ self.volume = percent
40
+
41
+ def get_mute(self) -> bool:
42
+ return self.muted
43
+
44
+ def set_mute(self, muted: bool) -> None:
45
+ self.muted = muted
46
+
47
+
48
+ @pytest.fixture()
49
+ def backend(monkeypatch: pytest.MonkeyPatch) -> FakeBackend:
50
+ fake = FakeBackend()
51
+ controller = VolumeController(fake)
52
+ monkeypatch.setattr(volume_cli, "_controller", lambda: controller)
53
+ return fake
54
+
55
+
56
+ runner = CliRunner()
57
+
58
+
59
+ def test_get_reports_volume_and_mute(backend: FakeBackend) -> None:
60
+ result = runner.invoke(app, ["get"])
61
+ assert result.exit_code == 0
62
+ assert "36%" in result.stdout
63
+ assert "muted" not in result.stdout
64
+
65
+ backend.muted = True
66
+ result = runner.invoke(app, ["status", "--json"])
67
+ assert result.exit_code == 0
68
+ assert '"volume": 36' in result.stdout
69
+ assert '"muted": true' in result.stdout
70
+
71
+
72
+ def test_on_full_none(backend: FakeBackend) -> None:
73
+ assert runner.invoke(app, ["on", "50"]).exit_code == 0
74
+ assert backend.volume == 50
75
+
76
+ assert runner.invoke(app, ["full"]).exit_code == 0
77
+ assert backend.volume == MAX_VOLUME
78
+
79
+ assert runner.invoke(app, ["none"]).exit_code == 0
80
+ assert backend.volume == MIN_VOLUME
81
+
82
+
83
+ def test_up_down_and_clamping(backend: FakeBackend) -> None:
84
+ backend.volume = 96
85
+ assert runner.invoke(app, ["up"]).exit_code == 0
86
+ assert backend.volume == MAX_VOLUME
87
+
88
+ assert runner.invoke(app, ["up", "10"]).exit_code == 0
89
+ assert backend.volume == MAX_VOLUME
90
+
91
+ backend.volume = 4
92
+ assert runner.invoke(app, ["down", "10"]).exit_code == 0
93
+ assert backend.volume == MIN_VOLUME
94
+
95
+
96
+ def test_mute_commands(backend: FakeBackend) -> None:
97
+ assert runner.invoke(app, ["mute"]).exit_code == 0
98
+ assert backend.muted is True
99
+
100
+ assert runner.invoke(app, ["unmute", "--json"]).exit_code == 0
101
+ assert backend.muted is False
102
+
103
+ assert runner.invoke(app, ["toggle"]).exit_code == 0
104
+ assert backend.muted is True
105
+
106
+ assert runner.invoke(app, ["toggle", "--json"]).exit_code == 0
107
+ assert backend.muted is False
108
+
109
+
110
+ def test_setting_volume_unmutes(backend: FakeBackend) -> None:
111
+ backend.muted = True
112
+ assert runner.invoke(app, ["on", "25"]).exit_code == 0
113
+ assert backend.muted is False
114
+
115
+
116
+ def test_invalid_values(backend: FakeBackend) -> None:
117
+ assert runner.invoke(app, ["on", "101"]).exit_code == 2
118
+ assert runner.invoke(app, ["up", "0"]).exit_code == 2
119
+
120
+ with pytest.raises(InvalidInput):
121
+ VolumeController(backend).set(101)
122
+
123
+ with pytest.raises(InvalidInput):
124
+ VolumeController(backend).up(0)
125
+
126
+
127
+ def _stub_command(
128
+ monkeypatch: pytest.MonkeyPatch, *, stdout: str = "", stderr: str = "", returncode: int = 0
129
+ ) -> list[list[str]]:
130
+ calls: list[list[str]] = []
131
+
132
+ def fake_run(command: list[str], **_kwargs: object) -> SimpleNamespace:
133
+ calls.append(command)
134
+ return SimpleNamespace(returncode=returncode, stdout=stdout, stderr=stderr)
135
+
136
+ monkeypatch.setattr(volume_core.subprocess, "run", fake_run)
137
+ return calls
138
+
139
+
140
+ def _linux(tool: str) -> LinuxVolumeBackend:
141
+ backend = LinuxVolumeBackend.__new__(LinuxVolumeBackend)
142
+ backend._tool = tool
143
+ return backend
144
+
145
+
146
+ def test_controller_clamps_what_the_backend_reports() -> None:
147
+ assert VolumeController(FakeBackend(volume=150)).get() == MAX_VOLUME
148
+ assert VolumeController(FakeBackend(volume=-4)).get() == MIN_VOLUME
149
+ assert VolumeController(FakeBackend(volume=42)).backend_name == "fake"
150
+
151
+
152
+ def test_setting_zero_does_not_unmute() -> None:
153
+ backend = FakeBackend(muted=True)
154
+ assert VolumeController(backend).set(0) == 0
155
+ assert backend.muted is True
156
+
157
+
158
+ def test_mute_helpers_report_the_new_state() -> None:
159
+ controller = VolumeController(FakeBackend(muted=False))
160
+ assert controller.is_muted() is False
161
+ assert controller.mute() is True
162
+ assert controller.unmute() is False
163
+ assert controller.toggle_mute() is True
164
+ assert controller.toggle_mute() is False
165
+
166
+
167
+ def test_controller_builds_a_backend_when_none_is_given(monkeypatch: pytest.MonkeyPatch) -> None:
168
+ monkeypatch.setattr(volume_core, "create_backend", lambda: FakeBackend(volume=7))
169
+ assert VolumeController().get() == 7
170
+
171
+
172
+ @pytest.mark.parametrize("value", [True, False, 101, -1, 1.5, "50", None])
173
+ def test_validate_rejects_non_percentages(value: object) -> None:
174
+ with pytest.raises(InvalidInput):
175
+ _validate(value)
176
+
177
+
178
+ def test_validate_accepts_the_full_range() -> None:
179
+ assert _validate(0) == 0
180
+ assert _validate(100) == 100
181
+
182
+
183
+ def test_validate_step_bounds() -> None:
184
+ assert _validate_step(1) == 1
185
+ assert _validate_step(100) == 100
186
+ for value in (0, 101, True, "5"):
187
+ with pytest.raises(InvalidInput):
188
+ _validate_step(value)
189
+
190
+
191
+ def test_clamp_bounds() -> None:
192
+ assert _clamp(-1) == MIN_VOLUME
193
+ assert _clamp(101) == MAX_VOLUME
194
+ assert _clamp(50) == 50
195
+
196
+
197
+ def test_parse_percent_reads_the_first_percentage() -> None:
198
+ assert _parse_percent("Volume: front-left: 32768 / 50% / -18.06 dB") == 50
199
+ with pytest.raises(OperationFailed, match="unexpected volume output"):
200
+ _parse_percent("no number here")
201
+
202
+
203
+ def test_run_command_returns_stdout(monkeypatch: pytest.MonkeyPatch) -> None:
204
+ calls = _stub_command(monkeypatch, stdout="42\n")
205
+ assert volume_core._run_command(["tool"]) == "42\n"
206
+ assert calls == [["tool"]]
207
+
208
+
209
+ def test_run_command_reports_a_missing_binary(monkeypatch: pytest.MonkeyPatch) -> None:
210
+ def fake_run(*_args: object, **_kwargs: object) -> SimpleNamespace:
211
+ raise FileNotFoundError
212
+
213
+ monkeypatch.setattr(volume_core.subprocess, "run", fake_run)
214
+ with pytest.raises(OperationFailed, match="required command not found: osascript"):
215
+ volume_core._run_command(["osascript"])
216
+
217
+
218
+ def test_run_command_reports_a_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
219
+ def fake_run(*_args: object, **_kwargs: object) -> SimpleNamespace:
220
+ raise subprocess.TimeoutExpired(cmd="pactl", timeout=10)
221
+
222
+ monkeypatch.setattr(volume_core.subprocess, "run", fake_run)
223
+ with pytest.raises(OperationFailed, match="pactl timed out"):
224
+ volume_core._run_command(["pactl"])
225
+
226
+
227
+ def test_run_command_reports_an_os_error(monkeypatch: pytest.MonkeyPatch) -> None:
228
+ def fake_run(*_args: object, **_kwargs: object) -> SimpleNamespace:
229
+ raise OSError("cannot execute")
230
+
231
+ monkeypatch.setattr(volume_core.subprocess, "run", fake_run)
232
+ with pytest.raises(OperationFailed, match="could not run amixer"):
233
+ volume_core._run_command(["amixer"])
234
+
235
+
236
+ def test_run_command_surfaces_stderr(monkeypatch: pytest.MonkeyPatch) -> None:
237
+ _stub_command(monkeypatch, stderr="boom\n", returncode=2)
238
+ with pytest.raises(OperationFailed, match="boom"):
239
+ volume_core._run_command(["tool"])
240
+
241
+
242
+ def test_run_command_falls_back_to_stdout_then_the_exit_code(monkeypatch: pytest.MonkeyPatch) -> None:
243
+ _stub_command(monkeypatch, stdout="out", returncode=2)
244
+ with pytest.raises(OperationFailed, match="out"):
245
+ volume_core._run_command(["tool"])
246
+
247
+ _stub_command(monkeypatch, returncode=7)
248
+ with pytest.raises(OperationFailed, match="exit code 7"):
249
+ volume_core._run_command(["tool"])
250
+
251
+
252
+ def test_create_backend_follows_the_platform(monkeypatch: pytest.MonkeyPatch) -> None:
253
+ monkeypatch.setattr(volume_core.sys, "platform", "win32")
254
+ assert isinstance(volume_core.create_backend(), WindowsVolumeBackend)
255
+
256
+ monkeypatch.setattr(volume_core.sys, "platform", "darwin")
257
+ assert isinstance(volume_core.create_backend(), MacOSVolumeBackend)
258
+
259
+ monkeypatch.setattr(volume_core.sys, "platform", "linux")
260
+ monkeypatch.setattr(volume_core, "_detect_linux_tool", lambda: "pactl")
261
+ assert isinstance(volume_core.create_backend(), LinuxVolumeBackend)
262
+
263
+ monkeypatch.setattr(volume_core.sys, "platform", "plan9")
264
+ with pytest.raises(OperationFailed, match="not supported"):
265
+ volume_core.create_backend()
266
+
267
+
268
+ def test_detect_linux_tool_prefers_pactl(monkeypatch: pytest.MonkeyPatch) -> None:
269
+ monkeypatch.setattr(volume_core.shutil, "which", lambda name: f"/usr/bin/{name}")
270
+ assert _detect_linux_tool() == "pactl"
271
+
272
+ monkeypatch.setattr(volume_core.shutil, "which", lambda name: None if name == "pactl" else f"/usr/bin/{name}")
273
+ assert _detect_linux_tool() == "wpctl"
274
+
275
+ monkeypatch.setattr(volume_core.shutil, "which", lambda _name: None)
276
+ with pytest.raises(OperationFailed, match="no supported Linux volume tool"):
277
+ _detect_linux_tool()
278
+
279
+
280
+ def test_linux_pactl_round_trip(monkeypatch: pytest.MonkeyPatch) -> None:
281
+ calls = _stub_command(monkeypatch, stdout="Volume: front-left: 32768 / 50%")
282
+ backend = _linux("pactl")
283
+ assert backend.get() == 50
284
+
285
+ backend.set(40)
286
+ backend.set_mute(True)
287
+ backend.set_mute(False)
288
+ assert ["pactl", "set-sink-volume", "@DEFAULT_SINK@", "40%"] in calls
289
+ assert ["pactl", "set-sink-mute", "@DEFAULT_SINK@", "1"] in calls
290
+ assert ["pactl", "set-sink-mute", "@DEFAULT_SINK@", "0"] in calls
291
+
292
+ _stub_command(monkeypatch, stdout="Mute: yes\n")
293
+ assert _linux("pactl").get_mute() is True
294
+
295
+
296
+ def test_linux_wpctl_round_trip(monkeypatch: pytest.MonkeyPatch) -> None:
297
+ calls = _stub_command(monkeypatch, stdout="Volume: 0.45 [muted]\n")
298
+ backend = _linux("wpctl")
299
+ assert backend.get() == 45
300
+ assert backend.get_mute() is True
301
+
302
+ backend.set(30)
303
+ backend.set_mute(False)
304
+ assert ["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "0.30"] in calls
305
+ assert ["wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "0"] in calls
306
+
307
+
308
+ def test_linux_amixer_round_trip(monkeypatch: pytest.MonkeyPatch) -> None:
309
+ calls = _stub_command(monkeypatch, stdout="Front Left: Playback 40 [50%] [on]\n")
310
+ backend = _linux("amixer")
311
+ assert backend.get() == 50
312
+ assert backend.get_mute() is False
313
+
314
+ backend.set(20)
315
+ backend.set_mute(True)
316
+ assert ["amixer", "sset", "Master", "20%"] in calls
317
+ assert ["amixer", "sset", "Master", "mute"] in calls
318
+
319
+
320
+ def test_linux_tool_parse_failures_are_reported(monkeypatch: pytest.MonkeyPatch) -> None:
321
+ _stub_command(monkeypatch, stdout="nothing useful")
322
+ with pytest.raises(OperationFailed, match="unexpected wpctl output"):
323
+ _linux("wpctl").get()
324
+ with pytest.raises(OperationFailed, match="unexpected amixer output"):
325
+ _linux("amixer").get()
326
+
327
+
328
+ def test_macos_backend_round_trip(monkeypatch: pytest.MonkeyPatch) -> None:
329
+ calls = _stub_command(monkeypatch, stdout="55\n")
330
+ backend = MacOSVolumeBackend()
331
+ assert backend.get() == 55
332
+
333
+ backend.set(20)
334
+ backend.set_mute(True)
335
+ assert ["osascript", "-e", "set volume output volume 20"] in calls
336
+ assert ["osascript", "-e", "set volume output muted true"] in calls
337
+
338
+ _stub_command(monkeypatch, stdout="true\n")
339
+ assert backend.get_mute() is True
340
+ _stub_command(monkeypatch, stdout="false\n")
341
+ assert backend.get_mute() is False
342
+
343
+
344
+ def test_macos_backend_rejects_unexpected_output(monkeypatch: pytest.MonkeyPatch) -> None:
345
+ _stub_command(monkeypatch, stdout="loud\n")
346
+ with pytest.raises(OperationFailed, match="unexpected osascript output"):
347
+ MacOSVolumeBackend().get()
348
+
349
+
350
+ class FakeEndpoint:
351
+ def __init__(self) -> None:
352
+ self.scalar = 0.36
353
+ self.muted = False
354
+
355
+ def GetMasterVolumeLevelScalar(self) -> float:
356
+ return self.scalar
357
+
358
+ def SetMasterVolumeLevelScalar(self, scalar: float, _context: object) -> None:
359
+ self.scalar = scalar
360
+
361
+ def GetMute(self) -> int:
362
+ return 1 if self.muted else 0
363
+
364
+ def SetMute(self, value: int, _context: object) -> None:
365
+ self.muted = bool(value)
366
+
367
+
368
+ def test_windows_backend_round_trip() -> None:
369
+ endpoint = FakeEndpoint()
370
+ backend = WindowsVolumeBackend()
371
+ backend._endpoint = lambda: endpoint
372
+
373
+ assert backend.name == "windows"
374
+ assert backend.get() == 36
375
+ backend.set(50)
376
+ assert endpoint.scalar == 0.5
377
+ assert backend.get_mute() is False
378
+ backend.set_mute(True)
379
+ assert backend.get_mute() is True
380
+
381
+
382
+ def test_windows_backend_wraps_endpoint_errors() -> None:
383
+ class Broken:
384
+ def GetMasterVolumeLevelScalar(self) -> float:
385
+ raise RuntimeError("no device")
386
+
387
+ def SetMasterVolumeLevelScalar(self, *_args: object) -> None:
388
+ raise RuntimeError("no device")
389
+
390
+ def GetMute(self) -> int:
391
+ raise RuntimeError("no device")
392
+
393
+ def SetMute(self, *_args: object) -> None:
394
+ raise RuntimeError("no device")
395
+
396
+ backend = WindowsVolumeBackend()
397
+ backend._endpoint = lambda: Broken()
398
+
399
+ for call, message in (
400
+ (backend.get, "could not read the master volume"),
401
+ (lambda: backend.set(10), "could not set the master volume"),
402
+ (backend.get_mute, "could not read the mute state"),
403
+ (lambda: backend.set_mute(True), "could not set the mute state"),
404
+ ):
405
+ with pytest.raises(OperationFailed, match=message):
406
+ call()
407
+
408
+
409
+ def test_windows_backend_requires_pycaw(monkeypatch: pytest.MonkeyPatch) -> None:
410
+ def fake_import(name: str) -> object:
411
+ raise ImportError(name)
412
+
413
+ monkeypatch.setattr(volume_core, "import_module", fake_import)
414
+ with pytest.raises(OperationFailed, match="requires pycaw"):
415
+ WindowsVolumeBackend().get()
416
+
417
+
418
+ def test_windows_backend_rejects_a_partial_pycaw(monkeypatch: pytest.MonkeyPatch) -> None:
419
+ monkeypatch.setattr(volume_core, "import_module", lambda _name: SimpleNamespace())
420
+ with pytest.raises(OperationFailed, match="missing AudioUtilities"):
421
+ WindowsVolumeBackend().get()
422
+
423
+
424
+ def test_windows_backend_reports_a_speaker_failure(monkeypatch: pytest.MonkeyPatch) -> None:
425
+ def boom() -> object:
426
+ raise RuntimeError("no speakers")
427
+
428
+ module = SimpleNamespace(AudioUtilities=SimpleNamespace(GetSpeakers=boom))
429
+ monkeypatch.setattr(volume_core, "import_module", lambda _name: module)
430
+ with pytest.raises(OperationFailed, match="could not open the default audio device"):
431
+ WindowsVolumeBackend().get()
432
+
433
+
434
+ def test_windows_backend_activates_the_endpoint_when_missing(monkeypatch: pytest.MonkeyPatch) -> None:
435
+ class EndpointType:
436
+ _iid_ = "iid"
437
+
438
+ class Device:
439
+ EndpointVolume = None
440
+
441
+ def Activate(self, *_args: object) -> str:
442
+ return "raw-interface"
443
+
444
+ module = SimpleNamespace(
445
+ AudioUtilities=SimpleNamespace(GetSpeakers=lambda: Device()),
446
+ IAudioEndpointVolume=EndpointType,
447
+ )
448
+ fakes = {
449
+ "pycaw.pycaw": module,
450
+ "comtypes": SimpleNamespace(CLSCTX_ALL=1),
451
+ "ctypes": SimpleNamespace(POINTER=lambda kind: ("pointer", kind), cast=lambda _value, _kind: "endpoint"),
452
+ }
453
+ monkeypatch.setattr(volume_core, "import_module", lambda name: fakes[name])
454
+ assert WindowsVolumeBackend()._endpoint() == "endpoint"
@@ -1 +0,0 @@
1
- __version__ = "0.3.9"
@@ -1,108 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import pytest
4
- from typer.testing import CliRunner
5
-
6
- from optforge.core.errors import InvalidInput
7
- from optforge.volume import cli as volume_cli
8
- from optforge.volume.cli import app
9
- from optforge.volume.core import MAX_VOLUME, MIN_VOLUME, VolumeController
10
-
11
-
12
- class FakeBackend:
13
- name = "fake"
14
-
15
- def __init__(self, volume: int = 36, muted: bool = False) -> None:
16
- self.volume = volume
17
- self.muted = muted
18
-
19
- def get(self) -> int:
20
- return self.volume
21
-
22
- def set(self, percent: int) -> None:
23
- self.volume = percent
24
-
25
- def get_mute(self) -> bool:
26
- return self.muted
27
-
28
- def set_mute(self, muted: bool) -> None:
29
- self.muted = muted
30
-
31
-
32
- @pytest.fixture()
33
- def backend(monkeypatch: pytest.MonkeyPatch) -> FakeBackend:
34
- fake = FakeBackend()
35
- controller = VolumeController(fake)
36
- monkeypatch.setattr(volume_cli, "_controller", lambda: controller)
37
- return fake
38
-
39
-
40
- runner = CliRunner()
41
-
42
-
43
- def test_get_reports_volume_and_mute(backend: FakeBackend) -> None:
44
- result = runner.invoke(app, ["get"])
45
- assert result.exit_code == 0
46
- assert "36%" in result.stdout
47
- assert "muted" not in result.stdout
48
-
49
- backend.muted = True
50
- result = runner.invoke(app, ["status", "--json"])
51
- assert result.exit_code == 0
52
- assert '"volume": 36' in result.stdout
53
- assert '"muted": true' in result.stdout
54
-
55
-
56
- def test_on_full_none(backend: FakeBackend) -> None:
57
- assert runner.invoke(app, ["on", "50"]).exit_code == 0
58
- assert backend.volume == 50
59
-
60
- assert runner.invoke(app, ["full"]).exit_code == 0
61
- assert backend.volume == MAX_VOLUME
62
-
63
- assert runner.invoke(app, ["none"]).exit_code == 0
64
- assert backend.volume == MIN_VOLUME
65
-
66
-
67
- def test_up_down_and_clamping(backend: FakeBackend) -> None:
68
- backend.volume = 96
69
- assert runner.invoke(app, ["up"]).exit_code == 0
70
- assert backend.volume == MAX_VOLUME
71
-
72
- assert runner.invoke(app, ["up", "10"]).exit_code == 0
73
- assert backend.volume == MAX_VOLUME
74
-
75
- backend.volume = 4
76
- assert runner.invoke(app, ["down", "10"]).exit_code == 0
77
- assert backend.volume == MIN_VOLUME
78
-
79
-
80
- def test_mute_commands(backend: FakeBackend) -> None:
81
- assert runner.invoke(app, ["mute"]).exit_code == 0
82
- assert backend.muted is True
83
-
84
- assert runner.invoke(app, ["unmute", "--json"]).exit_code == 0
85
- assert backend.muted is False
86
-
87
- assert runner.invoke(app, ["toggle"]).exit_code == 0
88
- assert backend.muted is True
89
-
90
- assert runner.invoke(app, ["toggle", "--json"]).exit_code == 0
91
- assert backend.muted is False
92
-
93
-
94
- def test_setting_volume_unmutes(backend: FakeBackend) -> None:
95
- backend.muted = True
96
- assert runner.invoke(app, ["on", "25"]).exit_code == 0
97
- assert backend.muted is False
98
-
99
-
100
- def test_invalid_values(backend: FakeBackend) -> None:
101
- assert runner.invoke(app, ["on", "101"]).exit_code == 2
102
- assert runner.invoke(app, ["up", "0"]).exit_code == 2
103
-
104
- with pytest.raises(InvalidInput):
105
- VolumeController(backend).set(101)
106
-
107
- with pytest.raises(InvalidInput):
108
- VolumeController(backend).up(0)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes