use serde_json::json;
use stratum_node_core::{
ApplicationId, ApplicationState, CreateInstallationRequest, DidNostr, DirectoryAppKey,
Enforcement, EnforcementParts, GrantState, InstallationId, InstallationState, InstallationView,
JobCounts, JobId, JobOutcome, JobState, KeyReason, KeysPart, LeaseState, RunResponse, UnixTime,
WithdrawResponse, WorkPart, WorkspaceMode,
};
const APP: &str = "c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5";
const OWNER: &str = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";
#[test]
fn e3b_35_identifiers_are_32_lower_case_hex() {
let id = InstallationId::from_bytes([0x5a; 16]);
assert_eq!(
serde_json::to_string(&id).expect("json"),
format!("\"{}\"", "5a".repeat(16)),
"text"
);
for bad in [
"5A".repeat(16),
"5a".repeat(15),
"5a".repeat(17),
format!("{}g", "5".repeat(31)),
] {
assert!(bad.parse::<ApplicationId>().is_err(), "{bad}");
assert!(bad.parse::<JobId>().is_err(), "{bad}");
}
}
#[test]
fn e3b_35_state_names_match_the_store_checks() {
let names = |all: &[&str]| all.join(",");
assert_eq!(
names(
&ApplicationState::ALL
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
),
"approved,withdrawn,disabled",
"application"
);
assert_eq!(
names(
&InstallationState::ALL
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
),
"pending_consent,active,withdrawn,removed",
"installation"
);
assert_eq!(
names(
&GrantState::ALL
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
),
"offered,consented,withdrawn,cleanup_reported",
"grant"
);
assert_eq!(
names(&JobState::ALL.iter().map(|s| s.as_str()).collect::<Vec<_>>()),
"pending,running,succeeded,failed,cancelled,conflict,skipped",
"job"
);
assert_eq!(
names(
&LeaseState::ALL
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
),
"claimed,started,ended,revoked,expired",
"lease"
);
assert_eq!(
names(
&KeyReason::ALL
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
),
"suspension,application,withdrawal,restore",
"key reasons"
);
assert!(
serde_json::from_str::<JobState>("\"Running\"").is_err(),
"names are case-sensitive"
);
assert_eq!(
serde_json::from_str::<JobOutcome>("\"not_granted\"").expect("outcome"),
JobOutcome::NotGranted,
"outcome"
);
}
#[test]
fn e3b_35_directory_app_keys_are_bare_hex_as_the_controller_emits() {
let entry = DirectoryAppKey {
pk: DidNostr::from_hex(APP).expect("app"),
owner: DidNostr::from_hex(OWNER).expect("owner"),
};
assert_eq!(
serde_json::to_value(entry).expect("json"),
json!({"pk": APP, "owner": OWNER}),
"bare hex, not did:nostr text"
);
assert!(
serde_json::from_value::<DirectoryAppKey>(
json!({"pk": format!("did:nostr:{APP}"), "owner": OWNER})
)
.is_err(),
"did text is refused where hex is required"
);
}
#[test]
fn e3b_35_tenant_bodies_have_the_contract_shapes() {
let app = ApplicationId::from_bytes([1; 16]);
let create = CreateInstallationRequest {
operation_id: "op-1".parse().expect("op"),
application: app,
schedule_secs: 3600,
workspace: WorkspaceMode::None,
};
assert_eq!(
serde_json::to_string(&create).expect("json"),
format!(
r#"{{"operation_id":"op-1","application":"{app}","schedule_secs":3600,"workspace":"none"}}"#
),
"create body"
);
assert!(
serde_json::from_str::<CreateInstallationRequest>(&format!(
r#"{{"operation_id":"op-1","application":"{app}","schedule_secs":3600,"node":"x"}}"#
))
.is_err(),
"unknown member refused"
);
let run = RunResponse {
job: JobId::from_bytes([2; 16]),
state: JobState::Pending,
};
assert_eq!(
serde_json::to_value(run).expect("json"),
json!({"job": "02".repeat(16), "state": "pending"}),
"run answer"
);
let withdraw = WithdrawResponse {
state: InstallationState::Withdrawn,
authorisation: Enforcement::Enforcing,
cleanup_pending: vec!["reports/".parse().expect("path")],
};
assert_eq!(
serde_json::to_value(&withdraw).expect("json"),
json!({"state": "withdrawn", "authorisation": "enforcing", "cleanup_pending": ["reports/"]}),
"withdraw answer"
);
}
#[test]
fn e3b_35_an_installation_view_reads_back_and_names_owner_as_did() {
let owner = DidNostr::from_hex(OWNER).expect("owner");
let mut jobs = JobCounts::default();
jobs.add(JobState::Succeeded, 3);
let view = InstallationView {
installation: InstallationId::from_bytes([3; 16]),
application: ApplicationId::from_bytes([4; 16]),
owner,
state: InstallationState::Withdrawn,
generation: 1,
schedule_secs: 0,
workspace: WorkspaceMode::None,
app_did: Some(DidNostr::from_hex(APP).expect("app")),
created_at: UnixTime::from_secs(10),
activated_at: Some(UnixTime::from_secs(20)),
withdrawn_at: Some(UnixTime::from_secs(30)),
key_reasons: vec![KeyReason::Withdrawal],
grant: GrantState::Withdrawn,
cleanup_pending: vec!["reports/".parse().expect("path")],
enforcement: Some(EnforcementParts {
authorisation: Enforcement::Enforcing,
keys: KeysPart::Disabling,
work: WorkPart::Stopping,
}),
jobs,
};
let value = serde_json::to_value(&view).expect("json");
assert_eq!(value["owner"], format!("did:nostr:{OWNER}"), "owner as did");
assert_eq!(value["jobs"]["succeeded"], 3, "job counts");
assert_eq!(value["enforcement"]["keys"], "disabling", "parts");
let back: InstallationView = serde_json::from_value(value).expect("read back");
assert_eq!(back, view, "round trip");
}