Files
PiCloud/crates/picloud-cli/tests/email_queue.rs
MechaCat02 d9b3e9973c fix(project-tool): address review findings on the apply foundation
Remediation of the single-app reconcile foundation after two independent
review passes. No new feature surface — closes correctness, parity, and
safety gaps in pull/plan/apply/prune.

apply engine (manager-core):
- validate_bundle reached parity with the interactive trigger API: reject
  an empty kv/docs/files collection_glob and a malformed pubsub
  topic_pattern (previously written and silently never matched), and lift
  the queue visibility floor to MIN_QUEUE_VISIBILITY_TIMEOUT_SECS (30) —
  apply had accepted a [5,29] value the dashboard refuses. Per-kind checks
  extracted into a pure, unit-tested validate_trigger_shape.
- An omitted script `description` now means "leave as-is" (matching the
  other optional fields and the function's own documented contract)
  instead of clearing the stored value.
- Doc fixes: drop stale "next milestone" notes; record the one deliberate
  plan/apply divergence (set-but-empty secret); document delete_route_tx
  as intentionally idempotent for reconcile.

CLI:
- Gate destructive `apply --prune` behind confirmation: interactive y/N,
  or `--yes` for CI; a non-interactive prune without `--yes` refuses
  rather than silently deleting. Journey tests pass `--yes`; a new test
  proves the gate refuses and deletes nothing.
- Harden pull's filename safety: reject all control characters and the
  Unicode bidi-override / zero-width chars used for terminal/filename
  spoofing, and cap length at 200 bytes (NAME_MAX headroom).

Tested: manager-core lib (incl. queue-floor + sparse-description parity)
+ CLI bins + 9 project-tool journeys green; clippy -D warnings clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 21:18:51 +02:00

223 lines
7.1 KiB
Rust

//! M5: `pic apply` creates email + queue triggers. The email trigger's
//! inbound secret is referenced by name (pushed via `pic secret set`) and
//! resolved + re-sealed server-side — never written into the manifest.
use std::fs;
use tempfile::TempDir;
use crate::common;
use crate::common::cleanup::AppGuard;
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
#[test]
fn apply_email_and_queue_triggers() {
let Some(fx) = common::fixture_or_skip() else {
return;
};
let env = common::admin_env(fx);
let slug = common::unique_slug("m5");
common::pic_as(&env)
.args(["apps", "create", &slug])
.assert()
.success();
let _guard = AppGuard::new(&env.url, &env.token, &slug);
// The email trigger references this secret by name; push its value
// out-of-band first.
common::pic_as(&env)
.args(["secrets", "set", "--app", &slug, "email-hmac"])
.write_stdin("super-secret-hmac")
.assert()
.success();
let dir = TempDir::new().unwrap();
fs::create_dir_all(dir.path().join("scripts")).unwrap();
fs::write(dir.path().join("scripts/handler.rhai"), "let x = 1; x").unwrap();
let manifest = format!(
"[app]\nslug = \"{slug}\"\nname = \"M5\"\n\n\
[secrets]\nnames = [\"email-hmac\"]\n\n\
[[scripts]]\nname = \"handler\"\nfile = \"scripts/handler.rhai\"\n\n\
[[triggers.queue]]\nscript = \"handler\"\nqueue_name = \"jobs\"\n\n\
[[triggers.email]]\nscript = \"handler\"\ninbound_secret_ref = \"email-hmac\"\n"
);
let manifest_path = dir.path().join("picloud.toml");
fs::write(&manifest_path, &manifest).unwrap();
let out = common::pic_as(&env)
.args(["apply", "--file"])
.arg(&manifest_path)
.output()
.expect("apply");
assert!(
out.status.success(),
"apply failed: {}",
String::from_utf8_lossy(&out.stderr)
);
// Both triggers exist.
let s = String::from_utf8(
common::pic_as(&env)
.args(["triggers", "ls", "--app", &slug])
.output()
.unwrap()
.stdout,
)
.unwrap();
assert!(
s.lines().any(|l| l.contains("queue")),
"queue trigger missing:\n{s}"
);
assert!(
s.lines().any(|l| l.contains("email")),
"email trigger missing:\n{s}"
);
// Re-apply is a no-op (both triggers match by identity).
let r = String::from_utf8(
common::pic_as(&env)
.args(["apply", "--file"])
.arg(&manifest_path)
.output()
.unwrap()
.stdout,
)
.unwrap();
assert!(!r.contains("+1"), "re-apply should be a no-op:\n{r}");
}
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
#[test]
fn prune_refuses_to_orphan_email_trigger() {
// `pull` can't represent email triggers, so a manifest that omits the
// script owning one would, under `--prune`, cascade-delete the trigger
// (and its sealed secret) when the script is dropped. Apply must refuse.
let Some(fx) = common::fixture_or_skip() else {
return;
};
let env = common::admin_env(fx);
let slug = common::unique_slug("m5-orphan");
common::pic_as(&env)
.args(["apps", "create", &slug])
.assert()
.success();
let _guard = AppGuard::new(&env.url, &env.token, &slug);
common::pic_as(&env)
.args(["secrets", "set", "--app", &slug, "email-hmac"])
.write_stdin("super-secret-hmac")
.assert()
.success();
let dir = TempDir::new().unwrap();
fs::create_dir_all(dir.path().join("scripts")).unwrap();
fs::write(dir.path().join("scripts/handler.rhai"), "let x = 1; x").unwrap();
let manifest_path = dir.path().join("picloud.toml");
// v1: a script with an email trigger.
let v1 = format!(
"[app]\nslug = \"{slug}\"\nname = \"M5\"\n\n\
[secrets]\nnames = [\"email-hmac\"]\n\n\
[[scripts]]\nname = \"handler\"\nfile = \"scripts/handler.rhai\"\n\n\
[[triggers.email]]\nscript = \"handler\"\ninbound_secret_ref = \"email-hmac\"\n"
);
fs::write(&manifest_path, &v1).unwrap();
common::pic_as(&env)
.args(["apply", "--file"])
.arg(&manifest_path)
.assert()
.success();
// v2: drop the script (and, implicitly, its un-representable email
// trigger). A prune apply must REFUSE rather than cascade-destroy it.
let v2 = format!("[app]\nslug = \"{slug}\"\nname = \"M5\"\n");
fs::write(&manifest_path, &v2).unwrap();
let out = common::pic_as(&env)
.args(["apply", "--file"])
.arg(&manifest_path)
.args(["--prune", "--yes"])
.output()
.expect("apply --prune");
assert!(
!out.status.success(),
"prune must refuse to orphan an email trigger"
);
// The script and its email trigger both survive the refused apply.
let scripts = String::from_utf8(
common::pic_as(&env)
.args(["scripts", "ls", "--app", &slug])
.output()
.unwrap()
.stdout,
)
.unwrap();
assert!(
scripts.contains("handler"),
"script must survive:\n{scripts}"
);
let triggers = String::from_utf8(
common::pic_as(&env)
.args(["triggers", "ls", "--app", &slug])
.output()
.unwrap()
.stdout,
)
.unwrap();
assert!(
triggers.lines().any(|l| l.contains("email")),
"email trigger must survive:\n{triggers}"
);
}
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
#[test]
fn apply_email_unset_secret_fails() {
let Some(fx) = common::fixture_or_skip() else {
return;
};
let env = common::admin_env(fx);
let slug = common::unique_slug("m5-nosecret");
common::pic_as(&env)
.args(["apps", "create", &slug])
.assert()
.success();
let _guard = AppGuard::new(&env.url, &env.token, &slug);
let dir = TempDir::new().unwrap();
fs::create_dir_all(dir.path().join("scripts")).unwrap();
fs::write(dir.path().join("scripts/handler.rhai"), "let x = 1; x").unwrap();
let manifest = format!(
"[app]\nslug = \"{slug}\"\nname = \"M5\"\n\n\
[[scripts]]\nname = \"handler\"\nfile = \"scripts/handler.rhai\"\n\n\
[[triggers.email]]\nscript = \"handler\"\ninbound_secret_ref = \"never-set\"\n"
);
let manifest_path = dir.path().join("picloud.toml");
fs::write(&manifest_path, &manifest).unwrap();
// The referenced secret was never set → apply must fail atomically.
let out = common::pic_as(&env)
.args(["apply", "--file"])
.arg(&manifest_path)
.output()
.expect("apply");
assert!(
!out.status.success(),
"apply must fail when an email secret is unset"
);
// Atomic: neither the script nor the email trigger was created.
let s = String::from_utf8(
common::pic_as(&env)
.args(["scripts", "ls", "--app", &slug])
.output()
.unwrap()
.stdout,
)
.unwrap();
assert!(
!s.contains("handler"),
"failed apply must leave nothing behind:\n{s}"
);
}