Skip to content

Commit ba1b6fc

Browse files
committed
Run usage ledger tests without caw installed
CI installs requirements.txt only, so importing caw failed collection. Use a stand-in usage object and a stub backend; the CawBackend test is skipped when caw is missing.
1 parent 38cb3d4 commit ba1b6fc

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

‎tests/test_usage_ledger.py‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44
import threading
55
from types import SimpleNamespace
66

7-
from caw.models import UsageStats
7+
import pytest
88

99
from codewiki.cli.utils.instructions import _echo_usage
10-
from codewiki.src.be import caw_backend
11-
from codewiki.src.be.backend import UsageLedger
12-
from codewiki.src.be.caw_backend import CawBackend
10+
from codewiki.src.be.backend import LLMBackend, UsageLedger
1311
from codewiki.src.be.documentation_generator import DocumentationGenerator
1412

1513

14+
def _usage(input_tokens=0, output_tokens=0, cache_read_tokens=0, cost_usd=0.0):
15+
"""Stand-in for caw's UsageStats (CI does not install caw)."""
16+
return SimpleNamespace(
17+
input_tokens=input_tokens,
18+
output_tokens=output_tokens,
19+
cache_read_tokens=cache_read_tokens,
20+
cache_write_tokens=0,
21+
cost_usd=cost_usd,
22+
)
23+
24+
1625
def test_summary_totals_and_phases():
1726
ledger = UsageLedger()
1827
with ledger.in_phase("clustering"):
@@ -74,7 +83,11 @@ def worker():
7483
assert summary["totals"]["input_tokens"] == 4000
7584

7685

77-
def _caw_backend(monkeypatch, usage: UsageStats) -> CawBackend:
86+
def _caw_backend(monkeypatch, usage):
87+
pytest.importorskip("caw")
88+
from codewiki.src.be import caw_backend
89+
from codewiki.src.be.caw_backend import CawBackend
90+
7891
class FakeCawAgent:
7992
def __init__(self, **kwargs):
8093
self.model = kwargs.get("model")
@@ -96,7 +109,7 @@ def completion(self, prompt):
96109

97110

98111
def test_caw_complete_records_usage(monkeypatch):
99-
usage = UsageStats(input_tokens=1000, output_tokens=200, cache_read_tokens=50, cost_usd=0.12)
112+
usage = _usage(input_tokens=1000, output_tokens=200, cache_read_tokens=50, cost_usd=0.12)
100113
backend = _caw_backend(monkeypatch, usage)
101114

102115
with backend.usage_ledger.in_phase("overview", target="repo"):
@@ -112,8 +125,17 @@ def test_caw_complete_records_usage(monkeypatch):
112125
assert call["cost_usd"] == 0.12
113126

114127

115-
def test_metadata_includes_usage(tmp_path, monkeypatch):
116-
backend = _caw_backend(monkeypatch, UsageStats(input_tokens=10, cost_usd=0.25))
128+
class _StubBackend(LLMBackend):
129+
def complete(self, prompt, *, model=None, system_prompt=None):
130+
self._record_usage("complete", "", _usage(input_tokens=10, cost_usd=0.25))
131+
return "ok"
132+
133+
async def run_module_agent(self, *args, **kwargs):
134+
return {}
135+
136+
137+
def test_metadata_includes_usage(tmp_path):
138+
backend = _StubBackend()
117139
backend.complete("prompt")
118140

119141
gen = object.__new__(DocumentationGenerator)

0 commit comments

Comments
 (0)