203 lines
6.5 KiB
Python
203 lines
6.5 KiB
Python
"""校准票据仅授权一个绑定源素材的短期任务,不能开放产品能力。"""
|
|
|
|
import hashlib
|
|
import json
|
|
import time
|
|
|
|
import pytest
|
|
from llm_services.store import Settings
|
|
|
|
OWNER, KEY = "a" * 32, "cmvr-visual:" + "b" * 32
|
|
PROFILE = "minimax-h3-fl2va-native-av-v1"
|
|
|
|
|
|
def module():
|
|
from llm_services import native_av_calibration
|
|
|
|
return native_av_calibration
|
|
|
|
|
|
def ticket(tmp_path, task="video.generate"):
|
|
settings = Settings(tmp_path / "app", tmp_path)
|
|
source = (
|
|
tmp_path
|
|
/ "inputs"
|
|
/ ("audio.wav" if task == "audio.dialogue.inspect" else "image.png")
|
|
)
|
|
source.parent.mkdir()
|
|
source.write_bytes(b"operator selected source")
|
|
if task == "video.generate":
|
|
inputs, params = (
|
|
{"prompt": "hello", "image_path": str(source)},
|
|
{"profile": PROFILE, "num_frames": 124},
|
|
)
|
|
else:
|
|
inputs = {
|
|
"audio_path": str(source),
|
|
"expected_text": "hello",
|
|
"speaker_id": OWNER,
|
|
}
|
|
params = {
|
|
"profile": "dialogue-audio-v1",
|
|
"native_av_policy": "native_av_v1",
|
|
"sample_rate": 48000,
|
|
}
|
|
digest = module().request_digest(task, inputs, params, OWNER, KEY)
|
|
path = (
|
|
settings.app_root
|
|
/ "config"
|
|
/ "native-av-validation"
|
|
/ "calibration"
|
|
/ (digest + ".json")
|
|
)
|
|
path.parent.mkdir(parents=True)
|
|
receipt = {
|
|
"schema_version": 1,
|
|
"request_sha256": digest,
|
|
"task_type": task,
|
|
"profile": params["profile"],
|
|
"owner_id": OWNER,
|
|
"idempotency_key": KEY,
|
|
"source_sha256": {
|
|
key: hashlib.sha256(source.read_bytes()).hexdigest()
|
|
for key in inputs
|
|
if key.endswith("_path")
|
|
},
|
|
"issued_at": time.time() - 10,
|
|
"expires_at": time.time() + 900,
|
|
"max_uses": 1,
|
|
}
|
|
path.write_text(json.dumps(receipt), encoding="utf-8")
|
|
path.chmod(0o600)
|
|
return settings, inputs, params, path, receipt, source
|
|
|
|
|
|
def test_ticket_allows_one_native_job_and_same_job_rechecks_without_enabling_capability(
|
|
tmp_path,
|
|
):
|
|
from llm_services.video_profiles import capabilities, validate_submission
|
|
|
|
settings, inputs, params, _, _, _ = ticket(tmp_path)
|
|
validate_submission(settings, params, inputs, principal=OWNER, idempotency_key=KEY)
|
|
validate_submission(
|
|
settings, params, inputs, principal=OWNER, idempotency_key=KEY, job_id="1" * 32
|
|
)
|
|
validate_submission(
|
|
settings, params, inputs, principal=OWNER, idempotency_key=KEY, job_id="1" * 32
|
|
)
|
|
with pytest.raises(ValueError, match="certified|enabled"):
|
|
validate_submission(
|
|
settings,
|
|
params,
|
|
inputs,
|
|
principal=OWNER,
|
|
idempotency_key=KEY,
|
|
job_id="2" * 32,
|
|
)
|
|
assert capabilities(settings)["native_av"]["available"] is False
|
|
with pytest.raises(ValueError, match="certified|enabled"):
|
|
validate_submission(settings, params)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"change", ["expired", "long_ttl", "uses", "owner", "profile", "source", "job"]
|
|
)
|
|
def test_private_ticket_rejects_wrong_expired_or_modified_bindings(tmp_path, change):
|
|
settings, inputs, params, path, receipt, source = ticket(tmp_path)
|
|
if change == "expired":
|
|
receipt["expires_at"] = time.time() - 1
|
|
elif change == "long_ttl":
|
|
receipt["expires_at"] = time.time() + 86400
|
|
elif change == "uses":
|
|
receipt["max_uses"] = 2
|
|
elif change == "owner":
|
|
receipt["owner_id"] = "c" * 32
|
|
elif change == "profile":
|
|
receipt["profile"] = "minimax-h3-fl2va-bf16-v1"
|
|
elif change == "source":
|
|
source.write_bytes(b"different source")
|
|
elif change == "job":
|
|
receipt["idempotency_key"] = "cmvr-visual:" + "c" * 32
|
|
path.write_text(json.dumps(receipt), encoding="utf-8")
|
|
assert (
|
|
module().admit(settings, "video.generate", inputs, params, OWNER, KEY) is False
|
|
)
|
|
|
|
|
|
def test_native_asr_calibrates_independently_from_silent_dialogue_ticket(tmp_path):
|
|
from llm_services.dialogue import profiles
|
|
|
|
settings, inputs, params, _, _, _ = ticket(tmp_path, "audio.dialogue.inspect")
|
|
profiles.admit(
|
|
settings,
|
|
"audio.dialogue.inspect",
|
|
params,
|
|
inputs,
|
|
principal=OWNER,
|
|
idempotency_key=KEY,
|
|
)
|
|
assert (
|
|
profiles.capabilities(settings)["speech_inspect"]["native_alignment"][
|
|
"available"
|
|
]
|
|
is False
|
|
)
|
|
with pytest.raises(ValueError, match="Native|native"):
|
|
profiles.admit(
|
|
settings,
|
|
"audio.dialogue.inspect",
|
|
params,
|
|
inputs,
|
|
principal=OWNER,
|
|
idempotency_key="cmvr-visual:" + "c" * 32,
|
|
)
|
|
|
|
|
|
def test_native_calibration_flows_through_api_and_worker_rechecks_real_queue_binding(
|
|
tmp_path,
|
|
):
|
|
import threading
|
|
from types import SimpleNamespace
|
|
|
|
from fastapi.testclient import TestClient
|
|
from llm_services.api import create_app
|
|
from llm_services.worker import Worker
|
|
|
|
settings, inputs, params, _, _, source = ticket(tmp_path)
|
|
settings.key_file.parent.mkdir(parents=True, exist_ok=True)
|
|
settings.key_file.write_text("native-av-calibration-test-key-123456789")
|
|
app = create_app(settings)
|
|
with TestClient(app) as client:
|
|
body = {
|
|
"task_type": "video.generate",
|
|
"input": inputs,
|
|
"parameters": params,
|
|
"scheduling": {"principal_id": OWNER, "execution_mode": "interactive"},
|
|
}
|
|
headers = {
|
|
"Authorization": "Bearer native-av-calibration-test-key-123456789",
|
|
"Idempotency-Key": KEY,
|
|
}
|
|
created = client.post("/jobs", json=body, headers=headers)
|
|
assert created.status_code == 202, created.text
|
|
assert (
|
|
client.post("/jobs", json=body, headers=headers).json()["id"]
|
|
== created.json()["id"]
|
|
)
|
|
holder = SimpleNamespace(
|
|
settings=settings,
|
|
store=app.state.store,
|
|
_active_lock=threading.RLock(),
|
|
_blocked_jobs={},
|
|
)
|
|
job = app.state.store.get(created.json()["id"])
|
|
assert Worker._reference_admission_reason(holder, job) is None
|
|
source.write_bytes(b"changed while waiting")
|
|
assert Worker._reference_admission_reason(holder, job)
|
|
assert (
|
|
client.get("/capabilities", headers=headers).json()["video_generation"][
|
|
"native_av"
|
|
]["available"]
|
|
is False
|
|
)
|