Metadata-Version: 2.3
Name: openbustest
Version: 0.1.0
Summary: AI-ready automotive bus automation testing platform (CAN/CAN-FD/LIN/UDS) powered by preset_rs
Author: LoveWonYoung
Author-email: LoveWonYoung <168514731+LoveWonYoung@users.noreply.github.com>
Requires-Dist: pytest>=8.0.0,<10
Requires-Dist: pyyaml>=6.0.0,<7
Requires-Dist: cantools>=39.0.0,<42
Requires-Dist: ldfparser>=0.25.0,<0.26
Requires-Dist: textual>=8.2.8
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# OpenBusTest

AI-ready 车载总线自动化测试平台（CAN / CAN-FD / LIN / UDS / Restbus），底层基于 Rust `preset_rs` ABI v12（支持 Toomoss、PEAK PCAN、TOSUN TSMaster、Vector XL），测试执行层基于标准 `pytest`。

## 核心理念

- **确定性执行在引擎内完成**：Restbus 周期仿真、后台旁路抓包、信号实时解码、ISO-TP/UDS 状态机全部在 Rust (`preset_rs`) + Python 运行时内以毫秒级精度跑完，不经过 LLM 往返。
- **AI 当测试工程师 & 根因分析专家**：AI 通过极简 CLI 查 DBC/LDF 信号字典 → 直接编写/修改标准 `pytest` 用例 → 执行测试 → 读取紧凑的失败证据切片 `evidence.json` 定位问题。
- **零 MCP 协议负担**：不需要配置和维护 MCP Server，任何具备终端和文件读写能力的 Coding Agent（Antigravity、Claude Code、Cursor、Codex）以及 CI/CD 均可直接接入。

## 安装（Windows 目标运行环境）

要求 Python 3.10+（64 位）。`preset_rs.dll` 与 Toomoss 64 位运行时依赖（`USB2XXX.dll`、`libusb-1.0.dll`）已内置随包分发；Vector / PCAN / TSMaster 等其他硬件后端按需动态加载系统已安装的厂商驱动。

**推荐方式：在已安装 `uv` 的台架机上一行全局安装**（之后任何业务目录都不需要建 `.venv`、`pyproject.toml`，也不需要手动配 `PRESET_RS_DLL`）：

```powershell
# 一行安装（通过 GitHub Pages PEP 503 索引自动拉取最新 Release wheel，内置 3 个 64 位 DLL）
uv tool install --extra-index-url https://lovewonyoung.github.io/openbustest/simple/ openbustest

# 后续一键检查并升级（或在 `obt tui` 中输入 `/update`）
obt update
obt update --check

# 如需从本地源码目录开发安装（--editable 便于随时改代码生效）
uv tool install --editable D:\src\openbustest
```

> **发版说明（维护者）**：在 GitHub 仓库 `Settings -> Pages -> Source` 选择 **GitHub Actions** 后，每次更新 `pyproject.toml` 中的 `version` 并推送 tag（`git tag v0.1.1 && git push origin v0.1.1`），`.github/workflows/release.yml` 会自动跑测试、构建内置 DLL 的 `.whl`、发布 GitHub Release，并刷新 GitHub Pages 静态索引与 `version.json`。

若是在当前仓库做框架开发，直接 `uv sync` + `uv run obt ...` 即可。

`preset_rs.dll` 默认搜索顺序（如需临时覆盖，设置环境变量 `PRESET_RS_DLL` 即可）：
1. 环境变量 `PRESET_RS_DLL`
2. 当前工作目录
3. 内置包目录 `openbustest/driver/bin/preset_rs.dll`（随 `.whl` 自动安装）
4. `openbustest/driver/`、`openbustest/`、仓库根目录及 Windows `PATH`

## 快速上手

### 1. 一键初始化测试目录（`obt new` / TUI `/new`）

在任意空目录或新目录运行：

```powershell
# 创建新的 ECU 测试目录（含 bench.yaml、db/、test_smoke.py、AGENTS.md、.gitignore）
obt new bcm_test --backend toomoss --bus canfd --ecu bcm --req-id 0x720 --resp-id 0x728

# 或使用交互式问答向导（一路回车即可用默认值）
obt new bcm_test -i
```

在 `obt tui` 中也可以直接输入 `/new bcm_test --backend toomoss`，创建完成后会自动加载新生成的 `bench.yaml`。

### 2. 定义台架 `bench.yaml`

见 [examples/bench.example.yaml](examples/bench.example.yaml)。一次配置硬件后端、CAN/LIN 通道、DBC/LDF 路径、Restbus 周期帧、ECU 诊断地址与 `SecKey.dll`。

### 3. 编写 `pytest` 测试用例

```python
def test_ign_on_and_read_vin(can, uds, step):
    with step("发送 IGN_ON 并等待 BCM 运行"):
        can.restbus.set_signal("VCU_PowerMode", "IGN_ON")
        can.wait_signal("BCM_PowerState", "RUN", timeout_ms=500)

    with step("扩展会话 + 安全解锁 + 读取 VIN"):
        uds.session(0x03).expect_positive()
        uds.unlock(level=1)
        resp = uds.read_did(0xF190).expect_positive()
        assert len(resp.data) == 17
```

多通道/多 ECU 台架可通过 `bench` fixture 按名获取：`bench.can("body_can")`、`bench.lin("door_lin")`、`bench.uds("bcm")`。

### 4. 运行与排障（CLI）

```powershell
# 查看台架与驱动状态
openbustest info --bench bench.yaml

# 按需检索 DBC/LDF 信号（避免把整份大文件塞进 AI 上下文）
openbustest db search PowerMode --bench bench.yaml
openbustest db msg VCU_PowerCmd --bench bench.yaml

# 运行测试并自动产出 summary.json / evidence/*.json / traces/*.asc / report.html
openbustest run --bench bench.yaml tests/test_bcm.py

# 查看最近一次运行的失败摘要或单个用例的解码报文切片
openbustest evidence
openbustest evidence latest test_ign_on_and_read_vin

# 单次探索性 UDS 探测
openbustest probe uds bcm "22 F1 90" --bench bench.yaml

# 启动交互式终端看板（浏览历史 Runs、失败证据、解码报文切片、Bench 拓扑，按 o 打开 HTML 报告）
openbustest tui --runs-dir runs --bench bench.yaml
```

也可以直接运行原生 `pytest`（插件已通过 `pytest11` 自动注册）：

```powershell
pytest --bench bench.yaml tests/test_bcm.py
```

每次运行自动生成 `runs/<run_id>/`：
- `summary.json`：结构化总览与失败清单
- `evidence/<case>.json`：失败用例的 Step 时间线 + 失败点前后 3 秒解码报文切片
- `traces/<channel>.asc`：标准 Vector ASC 原始总线日志
- `report.html`：独立可打印的 Vector CANoe 风格 HTML 测试报告

## 测试项目目录规范

`openbustest` 作为工具全局安装一次后，每个车型 / ECU 测试目录**只需要放属于它自己的文件**（无需 `pyproject.toml`、无需 `.venv`、无需复制 `preset_rs.dll`）：

```text
D:\ecus\
├── bcm\
│   ├── bench.yaml          # 默认台架配置（obt 自动识别）
│   ├── body.dbc            # 本 ECU 的 DBC / LDF
│   ├── SecKey.dll          # 可选：本 ECU 专属安全算法 DLL
│   ├── test_bcm.py         # AI 与工程师编写的 pytest 用例
│   └── runs\               # 自动生成的测试产物（建议 .gitignore）
└── door\
    ├── bench.yaml
    ├── door.ldf
    └── test_door.py
```

切到对应目录即可直接运行，零样板文件：

```powershell
cd D:\ecus\bcm
obt tui                     # 启动斜杠指令 TUI（自动加载 bench.yaml）
obt run                     # 跑当前目录全部用例并产出 runs/
obt run -k vin              # 按关键词跑单条用例
```

- **本仓库（`openbustest`）** 仅维护框架源码（`src/openbustest/`）、离线框架单测（`tests/`）与最小示例（`examples/`），请勿把真机 ECU 用例混入 `tests/`。

## 本地验证（macOS / Linux 无硬件环境）

```bash
uv sync
uv run pytest tests/
uv run python -m compileall -q src tests
uv build
```
