Files
task_pilot/backend/tests/uv.rs
T
CNWei 1914d38451 feat: 本地 uv 与托管 Python 环境管理
- 新增 uv 管理:只认程序目录副本或指定路径,支持从科大镜像下载并按 sha256 校验

- uv 环境变量(UV_*)可配置,默认相对程序目录(python / uv-cache),同时注入 uv 调用与任务进程

- 新增托管虚拟环境与 Python 版本管理:用 uv 下载解释器、预创建环境、查看已装包

- 新建任务保存时自动创建 .venv,运行期只校验、不再自动装依赖;镜像只在装包时使用

- 前端改为顶部品牌栏 + 三栏工作台,uv / 镜像源 / 虚拟环境各自独立弹窗

- 发布包构建后去掉工作区继承的低完整性标签

- 修复调度集成测试并发时误取 skipped 运行的问题
2026-11-07 16:57:27 +08:00

209 lines
6.1 KiB
Rust

use std::{fs, path::Path};
use axum::{
body::Body,
http::{Method, Request, StatusCode, header},
};
use http_body_util::BodyExt;
use serde_json::{Value, json};
use tempfile::TempDir;
use tower::ServiceExt;
async fn test_app() -> (axum::Router, TempDir) {
let temp_dir = TempDir::new().expect("temporary directory should be created");
let pool = task_pilot_backend::database::connect("sqlite::memory:")
.await
.expect("test database should initialize");
let app = task_pilot_backend::app(pool, temp_dir.path().join("logs"))
.await
.expect("test app should start");
(app, temp_dir)
}
async fn auth_token(app: &axum::Router) -> String {
let response = app
.clone()
.oneshot(
Request::builder()
.uri("/api/v1/session")
.body(Body::empty())
.expect("request should build"),
)
.await
.expect("router should respond");
let body = response
.into_body()
.collect()
.await
.expect("body should be readable")
.to_bytes();
let json: Value = serde_json::from_slice(&body).expect("session should be JSON");
json["token"]
.as_str()
.expect("token should be a string")
.to_owned()
}
async fn send_json(
app: &axum::Router,
method: Method,
uri: &str,
token: Option<&str>,
body: Option<Value>,
) -> (StatusCode, Value) {
let mut builder = Request::builder().method(method).uri(uri);
if let Some(token) = token {
builder = builder.header("x-taskpilot-token", token);
}
let request = if let Some(body) = body {
builder
.header(header::CONTENT_TYPE, "application/json")
.body(Body::from(
serde_json::to_vec(&body).expect("body should serialize"),
))
.expect("request should build")
} else {
builder.body(Body::empty()).expect("request should build")
};
let response = app
.clone()
.oneshot(request)
.await
.expect("router should respond");
let status = response.status();
let body = response
.into_body()
.collect()
.await
.expect("body should be readable")
.to_bytes();
(status, serde_json::from_slice(&body).unwrap_or(Value::Null))
}
#[tokio::test]
async fn uv_settings_default_to_the_managed_uv() {
let (app, _temp_dir) = test_app().await;
let (status, body) = send_json(&app, Method::GET, "/api/v1/uv", None, None).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body["source"], "managed");
assert_eq!(body["custom_path"], Value::Null);
let managed_dir = body["managed_dir"]
.as_str()
.expect("managed_dir should be a string");
assert_eq!(
Path::new(managed_dir).file_name().unwrap(),
std::ffi::OsStr::new("uv")
);
// 不再读系统 PATH,候选里只有程序目录 uv(自定义路径未设置时也不出现)。
let sources = body["candidates"]
.as_array()
.expect("candidates should be an array")
.iter()
.map(|candidate| candidate["source"].as_str().unwrap().to_owned())
.collect::<Vec<_>>();
assert_eq!(sources, vec!["managed"]);
}
#[tokio::test]
async fn uv_settings_reject_unusable_custom_path_and_persist_valid_ones() {
let (app, temp_dir) = test_app().await;
let token = auth_token(&app).await;
let (status, body) = send_json(
&app,
Method::PUT,
"/api/v1/uv",
Some(&token),
Some(json!({ "source": "custom", "custom_path": null })),
)
.await;
assert_eq!(status, StatusCode::BAD_REQUEST);
assert_eq!(body["field"], "custom_path");
let (status, body) = send_json(
&app,
Method::PUT,
"/api/v1/uv",
Some(&token),
Some(json!({ "source": "custom", "custom_path": "C:\\missing\\uv.exe" })),
)
.await;
assert_eq!(status, StatusCode::BAD_REQUEST);
assert_eq!(body["field"], "custom_path");
// 文件存在但不是可运行的 uv,也要拒绝保存。
let fake = temp_dir.path().join("fake.exe");
fs::write(&fake, b"not a program").expect("fake executable should be written");
let (status, body) = send_json(
&app,
Method::PUT,
"/api/v1/uv",
Some(&token),
Some(json!({
"source": "custom",
"custom_path": fake.to_string_lossy()
})),
)
.await;
assert_eq!(status, StatusCode::BAD_REQUEST);
assert_eq!(body["field"], "custom_path");
// 切回程序目录 uv 时保留已填写的自定义路径,方便再次切换。
let (status, saved) = send_json(
&app,
Method::PUT,
"/api/v1/uv",
Some(&token),
Some(json!({
"source": "managed",
"custom_path": " C:\\tools\\uv.exe "
})),
)
.await;
assert_eq!(status, StatusCode::OK);
assert_eq!(saved["source"], "managed");
assert_eq!(saved["custom_path"], "C:\\tools\\uv.exe");
let (status, body) = send_json(&app, Method::GET, "/api/v1/uv", None, None).await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body["source"], "managed");
assert_eq!(body["custom_path"], "C:\\tools\\uv.exe");
assert!(
body["candidates"]
.as_array()
.expect("candidates should be an array")
.iter()
.any(|candidate| candidate["source"] == "custom")
);
}
#[tokio::test]
async fn uv_settings_mutations_require_token() {
let (app, _temp_dir) = test_app().await;
let (status, body) = send_json(
&app,
Method::PUT,
"/api/v1/uv",
None,
Some(json!({ "source": "managed", "custom_path": null })),
)
.await;
assert_eq!(status, StatusCode::FORBIDDEN);
assert_eq!(body["code"], "FORBIDDEN");
}
#[tokio::test]
async fn uv_download_requires_token() {
let (app, _temp_dir) = test_app().await;
let (status, body) = send_json(&app, Method::POST, "/api/v1/uv/download", None, None).await;
assert_eq!(status, StatusCode::FORBIDDEN);
assert_eq!(body["code"], "FORBIDDEN");
}