mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
116 lines
5.0 KiB
Python
116 lines
5.0 KiB
Python
import argparse
|
|
from collections.abc import AsyncGenerator
|
|
|
|
import pytest
|
|
from fakeredis import aioredis as fakeredis
|
|
|
|
from llm_gateway.cli.reset_insights_code_usage import _non_empty_user_id, reset_usage
|
|
|
|
|
|
@pytest.fixture
|
|
async def redis() -> AsyncGenerator[fakeredis.FakeRedis]:
|
|
yield fakeredis.FakeRedis()
|
|
|
|
|
|
class TestResetUsage:
|
|
async def test_resets_every_user_regardless_of_plan(self, redis: fakeredis.FakeRedis) -> None:
|
|
# Free, pro, and unknown-plan users — all should be reset.
|
|
keys = [
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:100",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:100:period:0",
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:200",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:200:period:0",
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:300:tm2",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:300:tm2:period:1",
|
|
]
|
|
for k in keys:
|
|
await redis.set(k, "10.0")
|
|
|
|
deleted = await reset_usage(redis, dry_run=False)
|
|
|
|
assert deleted == len(keys)
|
|
for k in keys:
|
|
assert await redis.get(k) is None
|
|
|
|
@pytest.mark.parametrize("user_id", [None, "100"])
|
|
async def test_dry_run_counts_without_deleting(self, redis: fakeredis.FakeRedis, user_id: str | None) -> None:
|
|
await redis.set("ratelimit:cost:user:user_cost_burst:insights_code:100", "1.0")
|
|
await redis.set("ratelimit:cost:user:user_cost_sustained:insights_code:100:period:0", "1.0")
|
|
|
|
deleted = await reset_usage(redis, dry_run=True, user_id=user_id)
|
|
|
|
assert deleted == 2
|
|
assert await redis.get("ratelimit:cost:user:user_cost_burst:insights_code:100") is not None
|
|
assert await redis.get("ratelimit:cost:user:user_cost_sustained:insights_code:100:period:0") is not None
|
|
|
|
async def test_returns_zero_when_no_keys(self, redis: fakeredis.FakeRedis) -> None:
|
|
assert await reset_usage(redis, dry_run=False) == 0
|
|
|
|
async def test_leaves_unrelated_keys_untouched(self, redis: fakeredis.FakeRedis) -> None:
|
|
# Other products, plan cache, and unrelated rate-limit scopes must survive.
|
|
survivors = [
|
|
"ratelimit:cost:user:user_cost_burst:other_product:100",
|
|
"ratelimit:cost:user:user_cost_sustained:other_product:100:period:0",
|
|
"plan:insights_code:100",
|
|
"ratelimit:burst:user:100",
|
|
"some:other:key",
|
|
]
|
|
for k in survivors:
|
|
await redis.set(k, "x")
|
|
await redis.set("ratelimit:cost:user:user_cost_burst:insights_code:100", "1.0")
|
|
|
|
deleted = await reset_usage(redis, dry_run=False)
|
|
|
|
assert deleted == 1
|
|
for k in survivors:
|
|
assert await redis.get(k) is not None
|
|
|
|
async def test_user_id_resets_only_that_user(self, redis: fakeredis.FakeRedis) -> None:
|
|
target_keys = [
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:100",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:100:period:0",
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:100:tm2",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:100:tm2:period:1",
|
|
]
|
|
# Other users, including one whose id is a prefix of the target's id (1 vs 100)
|
|
# and one whose id starts with the target's id (1000 vs 100).
|
|
other_keys = [
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:1",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:1:period:0",
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:1000",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:1000:period:0",
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:200",
|
|
]
|
|
for k in target_keys + other_keys:
|
|
await redis.set(k, "1.0")
|
|
|
|
deleted = await reset_usage(redis, dry_run=False, user_id="100")
|
|
|
|
assert deleted == len(target_keys)
|
|
for k in target_keys:
|
|
assert await redis.get(k) is None
|
|
for k in other_keys:
|
|
assert await redis.get(k) is not None
|
|
|
|
async def test_user_id_glob_metachars_do_not_expand_match(self, redis: fakeredis.FakeRedis) -> None:
|
|
# A user_id like "10*" must be treated as a literal id, not a glob that
|
|
# would match every user starting with "10".
|
|
survivors = [
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:100",
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:1000",
|
|
"ratelimit:cost:user:user_cost_burst:insights_code:10abc",
|
|
"ratelimit:cost:user:user_cost_sustained:insights_code:100:period:0",
|
|
]
|
|
for k in survivors:
|
|
await redis.set(k, "1.0")
|
|
|
|
deleted = await reset_usage(redis, dry_run=False, user_id="10*")
|
|
|
|
assert deleted == 0
|
|
for k in survivors:
|
|
assert await redis.get(k) is not None
|
|
|
|
def test_non_empty_user_id_rejects_empty_string(self) -> None:
|
|
with pytest.raises(argparse.ArgumentTypeError):
|
|
_non_empty_user_id("")
|