Files
PiCloud/crates/picloud-cli/tests/shared_topics.rs
MechaCat02 19ba6dbfb4 test/docs(shared-topics): dispatch test + journey + docs (D2)
Deterministic manager-core test (tests/shared_topics.rs) proves the
namespace boundary both ways: a shared publish fans out only to the
group's shared pubsub trigger (not a per-app or non-shared group
template), and a per-app publish never hits the shared trigger. CLI
journey (shared_topics) covers authoring + `pic triggers ls --group`
shared column + the two validation rejections (undeclared topic; shared
on an app), mirroring the shared_triggers norm. Docs: CLAUDE.md + design
doc §11.6 record D1 + D2 as shipped.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 21:54:32 +02:00

160 lines
5.5 KiB
Rust

//! §11.6 D2 — shared TOPICS + shared pub/sub triggers, declarative authoring
//! end to end via `pic`. A group declares a shared `topic` collection + a
//! `[[triggers.pubsub]] shared = true` handler watching it; the template
//! applies, `pic triggers ls --group` shows `shared = true`; a shared pubsub
//! trigger on an UNDECLARED topic is rejected, and `shared = true` on an app
//! pubsub trigger is rejected.
//!
//! The live FIRING (a `pubsub::shared_topic(...).publish(...)` matches the
//! shared trigger, a per-app publish does not) is pinned deterministically by
//! `manager-core/tests/shared_topics.rs` — driving the async dispatcher from a
//! journey is deliberately avoided (codebase norm, mirroring `shared_triggers`).
use std::fs;
use tempfile::TempDir;
use crate::common;
use crate::common::cleanup::{AppGuard, GroupGuard, ScriptGuard};
fn manifest_dir() -> TempDir {
let dir = TempDir::new().expect("tempdir");
fs::create_dir_all(dir.path().join("scripts")).expect("scripts dir");
dir
}
fn group_script_id(env: &common::TestEnv, group: &str, name: &str) -> String {
let ls = common::pic_as(env)
.args(["scripts", "ls", "--group", group])
.output()
.expect("scripts ls");
let table = String::from_utf8(ls.stdout).unwrap();
table
.lines()
.map(common::cells)
.find(|c| c.get(2) == Some(&name))
.and_then(|c| c.first().map(|s| (*s).to_string()))
.unwrap_or_else(|| panic!("group script `{name}` not found:\n{table}"))
}
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
#[test]
fn shared_topic_trigger_applies_lists_and_validates() {
let Some(fx) = common::fixture_or_skip() else {
return;
};
let env = common::admin_env(fx);
let group = common::unique_slug("shtop-grp");
let _g = GroupGuard::new(&env.url, &env.token, &group);
common::pic_as(&env)
.args(["groups", "create", &group])
.assert()
.success();
// Group handler for the shared topic.
let dir = manifest_dir();
fs::write(
dir.path().join("scripts/on-evt.rhai"),
r#"log::info("shared topic event"); "ok""#,
)
.unwrap();
common::pic_as(&env)
.args(["scripts", "deploy"])
.arg(dir.path().join("scripts/on-evt.rhai"))
.args(["--group", &group, "--name", "on-evt"])
.assert()
.success();
let _gs = ScriptGuard::new(
&env.url,
&env.token,
&group_script_id(&env, &group, "on-evt"),
);
// Group declares a shared `topic` collection `events` + a shared pubsub
// trigger watching `events.*`.
let gmanifest = format!(
"[group]\nslug = \"{group}\"\nname = \"ShTopG\"\n\
collections = [{{ name = \"events\", kind = \"topic\" }}]\n\n\
[[triggers.pubsub]]\nscript = \"on-evt\"\ntopic_pattern = \"events.*\"\nshared = true\n"
);
let gpath = dir.path().join("group.toml");
fs::write(&gpath, &gmanifest).unwrap();
common::pic_as(&env)
.args(["apply", "--file"])
.arg(&gpath)
.assert()
.success();
// `pic triggers ls --group` shows shared = true for the pubsub trigger.
let ls = String::from_utf8(
common::pic_as(&env)
.args(["triggers", "ls", "--group", &group])
.output()
.unwrap()
.stdout,
)
.unwrap();
let row = ls
.lines()
.map(common::cells)
.find(|c| c.contains(&"on-evt"))
.unwrap_or_else(|| panic!("no on-evt trigger row:\n{ls}"));
assert!(
row.contains(&"true"),
"the shared column must read true for the shared pubsub trigger:\n{ls}"
);
// A shared pubsub trigger whose topic root is NOT a declared shared topic is
// rejected at apply.
let bad_group = format!(
"[group]\nslug = \"{group}\"\nname = \"ShTopG\"\n\
collections = [{{ name = \"events\", kind = \"topic\" }}]\n\n\
[[triggers.pubsub]]\nscript = \"on-evt\"\ntopic_pattern = \"other.created\"\nshared = true\n"
);
fs::write(&gpath, &bad_group).unwrap();
let out = common::pic_as(&env)
.args(["apply", "--file"])
.arg(&gpath)
.output()
.expect("apply bad");
assert!(
!out.status.success(),
"a shared pubsub trigger on an undeclared topic must be rejected"
);
// `shared = true` on an APP pubsub trigger is rejected.
let app = common::unique_slug("shtop-app");
let _a = AppGuard::new(&env.url, &env.token, &app);
common::pic_as(&env)
.args(["apps", "create", &app])
.assert()
.success();
fs::write(
dir.path().join("scripts/app-h.rhai"),
r#"log::info("app"); "ok""#,
)
.unwrap();
let amanifest = format!(
"[app]\nslug = \"{app}\"\nname = \"App\"\n\n\
[[scripts]]\nname = \"app-h\"\nfile = \"scripts/app-h.rhai\"\n\n\
[[triggers.pubsub]]\nscript = \"app-h\"\ntopic_pattern = \"events.*\"\nshared = true\n"
);
let apath = dir.path().join("app.toml");
fs::write(&apath, &amanifest).unwrap();
let out = common::pic_as(&env)
.args(["apply", "--file"])
.arg(&apath)
.output()
.expect("apply app");
assert!(
!out.status.success(),
"a shared pubsub trigger on an app is rejected (apps don't own shared topics)"
);
let err = String::from_utf8_lossy(&out.stderr);
assert!(
err.to_lowercase().contains("shared"),
"the rejection must mention shared:\n{err}"
);
}