feat: 串联 Brain 独立纵切与内部事件 (#16)
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from yovision_brain.app import run_pipeline
|
||||
from yovision_brain.events import JsonLinesSink
|
||||
from yovision_brain.input import CancellationToken
|
||||
|
||||
|
||||
FIXTURE = Path(__file__).parents[1] / "fixtures" / "events" / "area.json"
|
||||
|
||||
|
||||
def test_pipeline_generates_stable_internal_event() -> None:
|
||||
raw = json.loads(FIXTURE.read_text(encoding="utf-8"))
|
||||
first_stream, second_stream = io.StringIO(), io.StringIO()
|
||||
first = run_pipeline(raw, JsonLinesSink(first_stream), base_dir=FIXTURE.parent)
|
||||
second = run_pipeline(raw, JsonLinesSink(second_stream), base_dir=FIXTURE.parent)
|
||||
assert first.events == second.events == 1
|
||||
assert first_stream.getvalue() == second_stream.getvalue()
|
||||
event = json.loads(first_stream.getvalue())
|
||||
assert event["event_type"] == "danger_area_entered"
|
||||
assert event["schema"] == "brain.internal.event-candidate/v1"
|
||||
|
||||
|
||||
def test_no_hit_has_explicit_zero_event_summary() -> None:
|
||||
raw = json.loads(FIXTURE.read_text(encoding="utf-8"))
|
||||
raw["detector"]["threshold"] = 255
|
||||
raw["detector"]["minimum_area"] = 1000
|
||||
stream = io.StringIO()
|
||||
summary = run_pipeline(raw, JsonLinesSink(stream))
|
||||
assert (summary.status, summary.events, stream.getvalue()) == ("completed", 0, "")
|
||||
|
||||
|
||||
def test_cli_runs_without_sense_or_bell() -> None:
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "yovision_brain.app", "--config", str(FIXTURE), "--output", "-"],
|
||||
check=False,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
assert result.returncode == 0
|
||||
assert json.loads(result.stdout)["schema"] == "brain.internal.event-candidate/v1"
|
||||
assert json.loads(result.stderr)["events"] == 1
|
||||
|
||||
|
||||
def test_pre_cancelled_run_is_explicit() -> None:
|
||||
raw = json.loads(FIXTURE.read_text(encoding="utf-8"))
|
||||
token = CancellationToken()
|
||||
token.cancel()
|
||||
summary = run_pipeline(raw, JsonLinesSink(io.StringIO()), cancellation=token)
|
||||
assert (summary.status, summary.frames, summary.events) == ("cancelled", 0, 0)
|
||||
|
||||
|
||||
def test_cli_config_failure_is_nonzero_and_does_not_echo_path(tmp_path: Path) -> None:
|
||||
missing = tmp_path / "private-machine-path.json"
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "yovision_brain.app", "--config", str(missing)],
|
||||
check=False,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
assert result.returncode == 2
|
||||
assert json.loads(result.stderr)["status"] == "error"
|
||||
assert str(tmp_path) not in result.stderr
|
||||
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
|
||||
from yovision_brain.events import JsonLinesSink, candidate_from_decision
|
||||
from yovision_brain.rules import NormalizedPoint, RuleDecision
|
||||
from yovision_brain.vision import BoundingBox, DetectorMetadata, TrackedObject
|
||||
|
||||
|
||||
def test_internal_event_id_is_stable_and_payload_is_safe() -> None:
|
||||
track = TrackedObject("track-000001", BoundingBox(1, 2, 3, 4), "anonymous_target", 1.0, 7, 123)
|
||||
decision = RuleDecision("yard", "danger_area", track.track_id, "entered", True, "entered polygon", "rules-v1", "main", 10, 10, NormalizedPoint(0.2, 0.4), 123)
|
||||
metadata = DetectorMetadata("detector", "1", "first-party", "no external weights", "none")
|
||||
first = candidate_from_decision(decision, track, logical_input_id="camera-01", detector=metadata)
|
||||
second = candidate_from_decision(decision, track, logical_input_id="camera-01", detector=metadata)
|
||||
assert first == second
|
||||
assert first.event_id.startswith("brain-local-")
|
||||
payload = json.dumps(first.to_dict())
|
||||
for forbidden in ("password", "rtsp://", "evidence", "face"):
|
||||
assert forbidden not in payload.lower()
|
||||
|
||||
|
||||
def test_json_lines_sink_writes_one_canonical_line() -> None:
|
||||
track = TrackedObject("track-000001", BoundingBox(0, 0, 1, 1), "anonymous_target", 1.0, 0, 0)
|
||||
decision = RuleDecision("yard", "danger_area", track.track_id, "entered", True, "entered", "v1", "main", 2, 2, NormalizedPoint(0.25, 0.5), 0)
|
||||
candidate = candidate_from_decision(decision, track, logical_input_id="synthetic", detector=DetectorMetadata("d", "1", "first", "none", "none"))
|
||||
stream = io.StringIO()
|
||||
JsonLinesSink(stream).write(candidate)
|
||||
assert json.loads(stream.getvalue())["schema"] == "brain.internal.event-candidate/v1"
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
# Brain internal-event fixtures
|
||||
|
||||
These fixtures are synthetic and explicitly internal. They are not the future
|
||||
Brain-to-Bell event contract and must not contain evidence references, customer
|
||||
media, identities, credentials, or machine-specific paths.
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"rules_version": "fixture-rules-v1",
|
||||
"detector": {
|
||||
"backend": "python",
|
||||
"threshold": 0,
|
||||
"minimum_area": 1
|
||||
},
|
||||
"input": {
|
||||
"schema": "brain.internal.input/v1",
|
||||
"logical_device_id": "synthetic-camera-01",
|
||||
"profile": {
|
||||
"id": "main",
|
||||
"width": 4,
|
||||
"height": 3,
|
||||
"fps": 5
|
||||
},
|
||||
"source": {
|
||||
"kind": "synthetic",
|
||||
"seed": 17,
|
||||
"frame_count": 3
|
||||
},
|
||||
"areas": [
|
||||
{
|
||||
"id": "full-frame-area",
|
||||
"points": [[0, 0], [1, 0], [1, 1], [0, 1]]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user