test(v1.1.3-modules): resolver, cache, validator, kind-rejection coverage
Adds ~46 new tests across the v1.1.3 surface:
executor-core/tests/modules.rs (NEW, 23 tests):
- resolver_loads_simple_module / endpoint_can_import_module /
module_can_import_module — end-to-end through Engine::execute.
- resolver_cross_app_blocked / resolver_cross_app_module_not_found /
module_cache_keyed_by_app — same-name modules in different apps
resolve independently; cross-app lookup returns ModuleNotFound.
- resolver_self_import_detected / resolver_circular_detected —
cycle detector reports the chain.
- resolver_depth_limit_enforced / resolver_depth_limit_just_under_succeeds.
- resolver_module_not_found / resolver_backend_error_surfaces.
- resolver_runtime_validation_rejects_top_level_expr — defense-in-
depth: a module with a top-level expression that bypassed the
admin gate is rejected at resolve time.
- module_cache_hit_reuses_compiled_module /
module_cache_stale_invalidated_on_updated_at_change /
module_cache_lru_evicts_when_capacity_exceeded.
- validate_module_{accepts_fn_const_import_only,
rejects_top_level_let, rejects_top_level_expr,
rejects_top_level_while}.
- validate_endpoint_{extracts_literal_imports,
top_level_expr_still_allowed,
skips_dynamic_imports_in_imports_list}.
orchestrator-core/src/client.rs cache_tests (6 tests):
- cache_hit_when_identity_matches / cache_invalidated_when_updated_at_changes
/ distinct_script_ids_cache_independently / lru_eviction_caps_cache_size
/ script_identity_is_copy / compile_error_does_not_poison_cache.
shared/src/script.rs kind_tests (3 tests):
- default_is_endpoint / round_trips_through_serde_lowercase
/ parse_str_round_trip.
manager-core/src/triggers_api.rs v1.1.3 tests (6 tests):
- kv_trigger_rejects_module_target / docs_trigger_rejects_module_target
/ dl_trigger_rejects_module_target — modules cannot be trigger
targets.
- kv_trigger_rejects_missing_script / kv_trigger_rejects_cross_app_script
— closes the latent v1.1.1/v1.1.2 isolation gap.
- kv_trigger_accepts_endpoint_target — happy path through the
validate_trigger_target check.
picloud/tests/api.rs (8 #[ignore]'d Postgres-gated integration tests):
- create_script_default_kind_is_endpoint / create_module_kind_persists.
- create_module_with_top_level_expr_rejected /
create_module_with_reserved_name_rejected.
- route_bind_rejects_module.
- endpoint_imports_module_end_to_end /
module_edit_visible_on_next_invocation / cross_app_import_blocked.
Lint cleanup along the way:
- `ScriptKind::from_str` renamed to `parse_str` to dodge the
`should_implement_trait` lint (FromStr's `Result<…,Err>` shape
doesn't fit a 0-info lookup).
- `derive(Default)` on `ScriptKind` (Endpoint marked `#[default]`).
- Match-arm collapse in `check_module_shape` for Import + Noop.
- `#[allow(clippy::too_many_lines)]` on `resolve()` (the bridge
logic is genuinely cohesive and would lose clarity if split).
- Elided `'r` lifetime on `StackGuard`.
Three gates clean on this commit's HEAD:
- cargo fmt --all -- --check: clean
- cargo clippy --all-targets --all-features -- -D warnings: clean
- cargo test --workspace: 358 passed, 140 ignored (Postgres-gated)
- npm run check: 0 errors, 0 warnings
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1221,3 +1221,270 @@ async fn execution_errors_are_still_logged(pool: PgPool) {
|
||||
assert_eq!(logs[0]["status"], "error");
|
||||
assert!(logs[0]["response_body"]["error"].is_string());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// v1.1.3 — Modules: scripts.kind, route + trigger rejection, end-to-end import
|
||||
// ============================================================================
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn create_script_default_kind_is_endpoint(pool: PgPool) {
|
||||
let (s, app_id) = server_with_app(pool).await;
|
||||
let r = s
|
||||
.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_id,
|
||||
json!({ "name": "default-kind", "source": "1" }),
|
||||
))
|
||||
.await;
|
||||
r.assert_status(axum::http::StatusCode::CREATED);
|
||||
let body: Value = r.json();
|
||||
assert_eq!(body["kind"], "endpoint");
|
||||
}
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn create_module_kind_persists(pool: PgPool) {
|
||||
let (s, app_id) = server_with_app(pool).await;
|
||||
let r = s
|
||||
.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_id,
|
||||
json!({
|
||||
"name": "helpers",
|
||||
"kind": "module",
|
||||
"source": "fn add(a, b) { a + b }"
|
||||
}),
|
||||
))
|
||||
.await;
|
||||
r.assert_status(axum::http::StatusCode::CREATED);
|
||||
let body: Value = r.json();
|
||||
assert_eq!(body["kind"], "module");
|
||||
}
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn create_module_with_top_level_expr_rejected(pool: PgPool) {
|
||||
let (s, app_id) = server_with_app(pool).await;
|
||||
let r = s
|
||||
.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_id,
|
||||
json!({
|
||||
"name": "badmod",
|
||||
"kind": "module",
|
||||
"source": "42; fn ok() { 1 }"
|
||||
}),
|
||||
))
|
||||
.await;
|
||||
r.assert_status(axum::http::StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let body: Value = r.json();
|
||||
assert!(body["error"].as_str().unwrap().contains("module"));
|
||||
}
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn create_module_with_reserved_name_rejected(pool: PgPool) {
|
||||
let (s, app_id) = server_with_app(pool).await;
|
||||
let r = s
|
||||
.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_id,
|
||||
json!({
|
||||
"name": "kv",
|
||||
"kind": "module",
|
||||
"source": "fn ok() { 1 }"
|
||||
}),
|
||||
))
|
||||
.await;
|
||||
r.assert_status(axum::http::StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let body: Value = r.json();
|
||||
assert!(body["error"].as_str().unwrap().contains("reserved"));
|
||||
}
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn route_bind_rejects_module(pool: PgPool) {
|
||||
let (s, app_id) = server_with_app(pool).await;
|
||||
let r = s
|
||||
.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_id,
|
||||
json!({
|
||||
"name": "lib",
|
||||
"kind": "module",
|
||||
"source": "fn pong() { 42 }"
|
||||
}),
|
||||
))
|
||||
.await;
|
||||
r.assert_status(axum::http::StatusCode::CREATED);
|
||||
let body: Value = r.json();
|
||||
let id = body["id"].as_str().unwrap();
|
||||
|
||||
let r = s
|
||||
.post(&format!("/api/v1/admin/scripts/{id}/routes"))
|
||||
.json(&json!({
|
||||
"host_kind": "any",
|
||||
"path_kind": "exact",
|
||||
"path": "/lib"
|
||||
}))
|
||||
.await;
|
||||
r.assert_status(axum::http::StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn endpoint_imports_module_end_to_end(pool: PgPool) {
|
||||
let (s, app_id) = server_with_app(pool).await;
|
||||
|
||||
// Create a module script.
|
||||
s.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_id,
|
||||
json!({
|
||||
"name": "math",
|
||||
"kind": "module",
|
||||
"source": "fn add(a, b) { a + b }"
|
||||
}),
|
||||
))
|
||||
.await
|
||||
.assert_status(axum::http::StatusCode::CREATED);
|
||||
|
||||
// Create an endpoint that imports it.
|
||||
let id = create_basic_script(
|
||||
&s,
|
||||
&app_id,
|
||||
"calc",
|
||||
r#"import "math" as m; #{ statusCode: 200, body: m::add(2, 3) }"#,
|
||||
)
|
||||
.await;
|
||||
|
||||
// Bind a route.
|
||||
s.post(&format!("/api/v1/admin/scripts/{id}/routes"))
|
||||
.json(&json!({
|
||||
"host_kind": "any",
|
||||
"path_kind": "exact",
|
||||
"path": "/calc"
|
||||
}))
|
||||
.await
|
||||
.assert_status(axum::http::StatusCode::CREATED);
|
||||
|
||||
// Hit it — the endpoint should consume the module and return 5.
|
||||
let r = s.get("/calc").add_header("host", "localhost").await;
|
||||
r.assert_status_ok();
|
||||
let body: Value = r.json();
|
||||
assert_eq!(body, json!(5));
|
||||
}
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn module_edit_visible_on_next_invocation(pool: PgPool) {
|
||||
let (s, app_id) = server_with_app(pool).await;
|
||||
|
||||
let lib: Value = s
|
||||
.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_id,
|
||||
json!({
|
||||
"name": "greet",
|
||||
"kind": "module",
|
||||
"source": r"fn say(n) { `hello, ${n}` }"
|
||||
}),
|
||||
))
|
||||
.await
|
||||
.json();
|
||||
let lib_id = lib["id"].as_str().unwrap();
|
||||
|
||||
let id = create_basic_script(
|
||||
&s,
|
||||
&app_id,
|
||||
"hello",
|
||||
r#"import "greet" as g; #{ statusCode: 200, body: g::say("world") }"#,
|
||||
)
|
||||
.await;
|
||||
s.post(&format!("/api/v1/admin/scripts/{id}/routes"))
|
||||
.json(&json!({
|
||||
"host_kind": "any",
|
||||
"path_kind": "exact",
|
||||
"path": "/hello"
|
||||
}))
|
||||
.await
|
||||
.assert_status(axum::http::StatusCode::CREATED);
|
||||
|
||||
let r1: Value = s.get("/hello").add_header("host", "localhost").await.json();
|
||||
assert_eq!(r1, json!("hello, world"));
|
||||
|
||||
// Edit the module — bump updated_at.
|
||||
s.put(&format!("/api/v1/admin/scripts/{lib_id}"))
|
||||
.json(&json!({ "source": r"fn say(n) { `hi, ${n}` }" }))
|
||||
.await
|
||||
.assert_status_ok();
|
||||
|
||||
// Cache invalidation must surface the new behavior.
|
||||
let r2: Value = s.get("/hello").add_header("host", "localhost").await.json();
|
||||
assert_eq!(r2, json!("hi, world"));
|
||||
}
|
||||
|
||||
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
||||
#[sqlx::test(migrations = "../manager-core/migrations")]
|
||||
async fn cross_app_import_blocked(pool: PgPool) {
|
||||
// Two apps each have a module named "helpers" with different
|
||||
// behavior. An endpoint in app A must import A's module, not B's.
|
||||
|
||||
// App A is already created by `server_with_app`. Create app B.
|
||||
let (s, app_a) = server_with_app(pool).await;
|
||||
let app_b: Value = s
|
||||
.post("/api/v1/admin/apps")
|
||||
.json(&json!({ "slug": "appb", "name": "App B" }))
|
||||
.await
|
||||
.json();
|
||||
let app_b_id = app_b["id"].as_str().unwrap();
|
||||
|
||||
// App A's module returns "A". App B's returns "B".
|
||||
s.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
&app_a,
|
||||
json!({
|
||||
"name": "helpers",
|
||||
"kind": "module",
|
||||
"source": r#"fn who() { "A" }"#
|
||||
}),
|
||||
))
|
||||
.await
|
||||
.assert_status(axum::http::StatusCode::CREATED);
|
||||
s.post("/api/v1/admin/scripts")
|
||||
.json(&with_app(
|
||||
app_b_id,
|
||||
json!({
|
||||
"name": "helpers",
|
||||
"kind": "module",
|
||||
"source": r#"fn who() { "B" }"#
|
||||
}),
|
||||
))
|
||||
.await
|
||||
.assert_status(axum::http::StatusCode::CREATED);
|
||||
|
||||
// Endpoint in app A imports "helpers" and exposes the result.
|
||||
let id = create_basic_script(
|
||||
&s,
|
||||
&app_a,
|
||||
"who-am-i",
|
||||
r#"import "helpers" as h; #{ statusCode: 200, body: h::who() }"#,
|
||||
)
|
||||
.await;
|
||||
s.post(&format!("/api/v1/admin/scripts/{id}/routes"))
|
||||
.json(&json!({
|
||||
"host_kind": "any",
|
||||
"path_kind": "exact",
|
||||
"path": "/who-am-i"
|
||||
}))
|
||||
.await
|
||||
.assert_status(axum::http::StatusCode::CREATED);
|
||||
|
||||
let r: Value = s
|
||||
.get("/who-am-i")
|
||||
.add_header("host", "localhost")
|
||||
.await
|
||||
.json();
|
||||
assert_eq!(r, json!("A"), "must see app A's module, not app B's");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user