Files
tdc-rs/tests/bootstrap_fail.rs
T
admin 49e5fcefe4 refactor(b1b-c9): 状态分家 —— 平台 AppState 只留运行态,业务态进 TdcState
影响:平台层不再认识业务类型(引导身份 / 本地账号库 / TDS 客户端 / 业务配置
全部移出 AppState);对外行为零变化(237 条测试全绿、冒烟断言数仍 75)。

改动:
- src/platform/state.rs:AppState 收敛为 {config, db, db_lock, sessions}
- src/tdc/state.rs(新):TdcState = platform + 业务配置 + 引导身份 + 账号库 + TDS 客户端
  · Deref<Target = AppState> => 业务层字段访问(state.db 等)写法不变
  · impl FromRef<TdcState> for AppState => axum 里 State<AppState> 仍可用
- src/main.rs:装配顺序改为 platform -> bootstrap_identity -> TdcState
- 控制器/服务/测试:入参一律换成 &TdcState(bootstrap_identity 仍是 &AppState,
  它跑在装配期,那时 TdcState 还没造出来)
- scripts/check-boundary.sh:新增第 5 个接触点 CP5(平台层不得 use crate::tdc::),
  专门盯这条反向依赖;CP1 只查表名/端点前缀,查不出模块引用
- CLAUDE.md / .workbuddy/memory/2026-10-26.md 同步

设计:FromRef 在 axum::extract(不是 axum::core::extract)=> 手写 impl。
2026-10-26 22:24:54 +08:00

54 lines
2.1 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 引导失败的端到端用例:INSERT 失败 ⇒ **不得**回传密钥。
//!
//! ⚠️ 本文件是**独立测试二进制** ⇒ 独立进程 ⇒ 独立 PGlite
//! (pglite-rs 每进程只允许一个实例)。
use std::sync::Arc;
use pglite::PGlite;
use tdc_connector::platform::config::Config;
use tdc_connector::tdc::config::TdcConfig;
use tdc_connector::tdc::service::identity::bootstrap_identity;
use tdc_connector::platform::session::SessionStore;
use tdc_connector::platform::state::AppState;
use tdc_connector::tdc::state::{BootstrapIdentity, TdcState};
#[tokio::test]
async fn bootstrap_insert_failure_yields_no_secret() {
let db = Arc::new(PGlite::open_temp().await.expect("打开临时 PGlite 失败"));
tdc_connector::platform::db::ensure_schema(db.as_ref())
.await
.expect("建表失败");
// 让 INSERT 必然失败:把目标表删掉
// (`PGlite::exec(&self, sql: &str)` 是无参版本;带参的是 `query(sql, &[&(dyn ToSql + Sync)])`)
db.exec("DROP TABLE tdc_identity").await.expect("删表失败");
let config = Config::from_env(std::env::current_dir().unwrap());
let state = TdcState::new(
AppState::new(config, db, Arc::new(SessionStore::new())),
TdcConfig::default(),
BootstrapIdentity::default(),
);
// 🔴 业务配置走显式入参(v3 spec §2.3.1 C8)⇒ 身份码不再挂在平台 Config 上。
let tdc_config = TdcConfig {
identity_code: "TEST-CONNECTOR".to_string(),
identity_name: "测试连接器".to_string(),
identity_secret: Some("test-secret-key".to_string()),
bootstrap_admin_account: "admin".to_string(),
bootstrap_admin_password: None,
};
let bootstrap = bootstrap_identity(&state, &tdc_config).await;
// 🔴 失败 ⇒ 不得有密钥、不得有 id
assert!(
bootstrap.issued_secret.is_none(),
"引导失败时不得回传密钥,实际 {:?}",
bootstrap.issued_secret
);
assert!(
bootstrap.identity_id.is_empty(),
"引导失败时不得回传没落库的 id,实际 {:?}",
bootstrap.identity_id
);
}