use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::path::PathBuf;
use serde::Serialize;
use crate::config::UserConfig;
use crate::inventory::{self, InventoryWarningCode, SkillRecord, SourceInventory};
use crate::source::{SourceConfig, SourceKind};
use crate::store::ApprovalRecord;
#[derive(Debug, Clone)]
pub struct ResolutionInput<'a> {
pub sources: &'a [SourceConfig],
pub inventories: Vec<SourceInventory>,
pub approvals: Vec<ApprovalRecord>,
}
#[derive(Debug, Clone)]
pub struct SourceScan {
pub source: SourceConfig,
pub inventory: Option<SourceInventory>,
pub error: Option<String>,
}
#[derive(Debug, Clone)]
pub struct LiveResolution {
pub scans: Vec<SourceScan>,
pub resolution: Resolution,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Resolution {
pub active_skills: Vec<ResolvedSkill>,
pub pending_approval_skills: Vec<ResolvedSkill>,
pub unlinked_skills: Vec<UnlinkedSkill>,
pub blocked_skills: Vec<BlockedSkill>,
pub diagnostics: Vec<ResolutionDiagnostic>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ResolvedSkill {
pub source_ref: String,
pub slot_name: String,
pub id: Option<String>,
pub source_id: String,
pub source_kind: SourceKind,
pub source_priority: i32,
pub path: PathBuf,
pub local_override: bool,
#[serde(skip)]
pub requires: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct UnlinkedSkill {
pub skill: ResolvedSkill,
pub status: UnlinkedStatus,
pub reason: UnlinkedReason,
pub shadowed_by: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct BlockedSkill {
pub skill: ResolvedSkill,
pub requirement: String,
pub reason: ClosureBlockReason,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ClosureBlockReason {
Missing,
PendingApproval,
ShadowedNotSatisfied,
SameNameBlocked,
Unlinked,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UnlinkedStatus {
Unlinked,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UnlinkedReason {
Shadowed,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ResolutionDiagnostic {
pub code: ResolutionDiagnosticCode,
pub message: String,
pub source_ref: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ResolutionDiagnosticCode {
PendingApproval,
LocalOverride,
Shadowed,
RequiredExpanded,
CrossSourceRequire,
RequiredBlocked,
LegacyBareApproval,
}
impl ResolutionDiagnosticCode {
#[must_use]
pub const fn requires_review(self) -> bool {
matches!(
self,
Self::PendingApproval
| Self::CrossSourceRequire
| Self::RequiredBlocked
| Self::LegacyBareApproval
)
}
}
#[derive(Debug, Clone)]
struct Candidate {
skill: ResolvedSkill,
owners: Vec<String>,
trusted: bool,
requires: Vec<String>,
}
pub fn resolve_from_config(config: &UserConfig, approvals: Vec<ApprovalRecord>) -> LiveResolution {
let enabled = config
.sources
.iter()
.filter(|source| source.enabled)
.cloned()
.collect::<Vec<_>>();
let mut scans = Vec::with_capacity(enabled.len());
let mut inventories = Vec::new();
for source in &enabled {
match scan_enabled_source(source) {
Ok(inventory) => {
inventories.push(inventory.clone());
scans.push(SourceScan {
source: source.clone(),
inventory: Some(inventory),
error: None,
});
}
Err(error) => scans.push(SourceScan {
source: source.clone(),
inventory: None,
error: Some(error),
}),
}
}
let resolution = resolve(&ResolutionInput {
sources: &enabled,
inventories,
approvals,
});
LiveResolution { scans, resolution }
}
fn scan_enabled_source(source: &SourceConfig) -> Result<SourceInventory, String> {
if !source.path.exists() {
return Err("source path does not exist".to_owned());
}
let inventory =
inventory::scan_source(&source.id, &source.path).map_err(|error| error.to_string())?;
Ok(inventory)
}
#[must_use]
pub fn inventory_degrades_source_for_removal(inventory: &SourceInventory) -> bool {
inventory.warnings.iter().any(|warning| {
matches!(
warning.code,
InventoryWarningCode::UnreadablePath
| InventoryWarningCode::InvalidSlotName
| InventoryWarningCode::SkippedSymlink
)
})
}
#[must_use]
pub fn resolve(input: &ResolutionInput) -> Resolution {
let source_by_id = input
.sources
.iter()
.filter(|source| source.enabled)
.map(|source| (source.id.as_str(), source))
.collect::<BTreeMap<_, _>>();
let effective_selection = expand_catalog_selections(&source_by_id, &input.inventories);
let mut candidates = Vec::new();
for inventory in &input.inventories {
let Some(source) = source_by_id.get(inventory.source_id.as_str()) else {
continue;
};
let selection = effective_selection
.get(source.id.as_str())
.map_or(source.selection.as_slice(), Vec::as_slice);
for skill in &inventory.skills {
if source.kind == SourceKind::Catalog
&& !crate::catalog::skill_is_selected(skill, selection, &source.path)
{
continue;
}
candidates.push(Candidate {
skill: ResolvedSkill {
source_ref: skill.source_ref.clone(),
slot_name: skill.slot_name.clone(),
id: skill.id.clone(),
source_id: source.id.clone(),
source_kind: source.kind,
source_priority: source.priority,
path: skill.path.clone(),
local_override: false,
requires: skill.requires.clone(),
},
owners: skill.owners.clone(),
trusted: source.trusted,
requires: skill.requires.clone(),
});
}
}
let requires_by_ref: BTreeMap<String, Vec<String>> = candidates
.iter()
.filter(|candidate| !candidate.requires.is_empty())
.map(|candidate| {
(
candidate.skill.source_ref.clone(),
candidate.requires.clone(),
)
})
.collect();
candidates.sort_by(|left, right| {
left.skill
.source_priority
.cmp(&right.skill.source_priority)
.then_with(|| left.skill.source_id.cmp(&right.skill.source_id))
.then_with(|| left.skill.source_ref.cmp(&right.skill.source_ref))
});
let mut groups: BTreeMap<String, Vec<Candidate>> = BTreeMap::new();
for candidate in candidates {
let slot_name = candidate.skill.slot_name.clone();
groups.entry(slot_name).or_default().push(candidate);
}
let mut active_skills = Vec::new();
let mut pending_approval_skills = Vec::new();
let mut unlinked_skills = Vec::new();
let mut diagnostics = Vec::new();
for group in groups.into_values() {
let Some(winner_index) = group
.iter()
.position(|candidate| is_approved(candidate, &input.approvals))
else {
for candidate in &group {
pending_approval_skills.push(candidate.skill.clone());
if let Some((legacy, suggested)) = legacy_bare_approval(candidate, &input.approvals)
{
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::LegacyBareApproval,
message: format!(
"legacy approval `{}` found for `{}`; re-approve as `{}`",
legacy, candidate.skill.source_ref, suggested
),
source_ref: Some(candidate.skill.source_ref.clone()),
});
}
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::PendingApproval,
message: format!("skill `{}` is pending approval", candidate.skill.source_ref),
source_ref: Some(candidate.skill.source_ref.clone()),
});
}
continue;
};
for candidate in group.iter().take(winner_index) {
pending_approval_skills.push(candidate.skill.clone());
if let Some((legacy, suggested)) = legacy_bare_approval(candidate, &input.approvals) {
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::LegacyBareApproval,
message: format!(
"legacy approval `{}` found for `{}`; re-approve as `{}`",
legacy, candidate.skill.source_ref, suggested
),
source_ref: Some(candidate.skill.source_ref.clone()),
});
}
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::PendingApproval,
message: format!("skill `{}` is pending approval", candidate.skill.source_ref),
source_ref: Some(candidate.skill.source_ref.clone()),
});
}
let mut winner = group[winner_index].skill.clone();
winner.local_override = winner.source_kind == SourceKind::Local && group.len() > 1;
if winner.local_override {
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::LocalOverride,
message: format!(
"local skill `{}` overrides another source",
winner.source_ref
),
source_ref: Some(winner.source_ref.clone()),
});
}
for candidate in group.iter().skip(winner_index + 1) {
unlinked_skills.push(UnlinkedSkill {
skill: candidate.skill.clone(),
status: UnlinkedStatus::Unlinked,
reason: UnlinkedReason::Shadowed,
shadowed_by: winner.source_ref.clone(),
});
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::Shadowed,
message: format!(
"skill `{}` is unlinked because `{}` wins the same slot",
candidate.skill.source_ref, winner.source_ref
),
source_ref: Some(candidate.skill.source_ref.clone()),
});
}
active_skills.push(winner);
}
let (mut blocked_skills, closure_diagnostics) = required_closure_preflight(
&mut active_skills,
&pending_approval_skills,
&requires_by_ref,
&input.inventories,
);
diagnostics.extend(closure_diagnostics);
active_skills.sort_by(|left, right| {
left.slot_name
.cmp(&right.slot_name)
.then_with(|| left.source_ref.cmp(&right.source_ref))
});
pending_approval_skills.sort_by(|left, right| {
left.slot_name
.cmp(&right.slot_name)
.then_with(|| left.source_ref.cmp(&right.source_ref))
});
unlinked_skills.sort_by(|left, right| {
left.skill
.slot_name
.cmp(&right.skill.slot_name)
.then_with(|| left.skill.source_ref.cmp(&right.skill.source_ref))
});
for (source_id, effective) in &effective_selection {
let Some(source) = source_by_id.get(source_id.as_str()) else {
continue;
};
for reference in effective {
if !source
.selection
.iter()
.any(|selected| selected == reference)
{
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::RequiredExpanded,
message: format!(
"`{reference}` was pulled into catalog `{source_id}` by a requires closure"
),
source_ref: Some(format!("{source_id}:{reference}")),
});
}
}
}
diagnostics.sort_by(|left, right| {
diagnostic_code_name(left.code)
.cmp(diagnostic_code_name(right.code))
.then_with(|| left.source_ref.cmp(&right.source_ref))
});
blocked_skills.sort_by(|left, right| {
left.skill
.slot_name
.cmp(&right.skill.slot_name)
.then_with(|| left.skill.source_ref.cmp(&right.skill.source_ref))
});
Resolution {
active_skills,
pending_approval_skills,
unlinked_skills,
blocked_skills,
diagnostics,
}
}
fn is_approved(candidate: &Candidate, approvals: &[ApprovalRecord]) -> bool {
if candidate.skill.source_kind == SourceKind::Local || candidate.trusted {
return true;
}
approvals.iter().any(|approval| {
approval_matches_skill(approval, candidate)
|| approval_matches_source(approval, candidate)
|| approval_matches_owner(approval, candidate)
})
}
fn legacy_bare_approval(
candidate: &Candidate,
approvals: &[ApprovalRecord],
) -> Option<(String, String)> {
approvals
.iter()
.filter(|approval| approval.scope == "skill" && !approval.value.contains(':'))
.find_map(|approval| {
if approval.value == candidate.skill.slot_name {
return Some((approval.value.clone(), candidate.skill.source_ref.clone()));
}
let id = candidate.skill.id.as_ref()?;
(approval.value == *id).then(|| {
(
approval.value.clone(),
format!("{}:{}", candidate.skill.source_id, id),
)
})
})
}
fn approval_matches_skill(approval: &ApprovalRecord, candidate: &Candidate) -> bool {
if approval.scope != "skill" {
return false;
}
if approval.value == candidate.skill.source_ref {
return true;
}
let Some((source_id, stable_id)) = approval.value.split_once(':') else {
return false;
};
source_id == candidate.skill.source_id
&& candidate
.skill
.id
.as_deref()
.is_some_and(|id| id == stable_id)
}
fn approval_matches_source(approval: &ApprovalRecord, candidate: &Candidate) -> bool {
approval.scope == "source" && approval.value == candidate.skill.source_id
}
fn approval_matches_owner(approval: &ApprovalRecord, candidate: &Candidate) -> bool {
if !matches!(approval.scope.as_str(), "author" | "org") {
return false;
}
let Some((source_id, owner)) = approval.value.split_once(':') else {
return false;
};
source_id == candidate.skill.source_id
&& candidate
.owners
.iter()
.any(|candidate_owner| candidate_owner == owner)
}
#[must_use]
pub fn diagnostic_code_name(code: ResolutionDiagnosticCode) -> &'static str {
match code {
ResolutionDiagnosticCode::PendingApproval => "pending_approval",
ResolutionDiagnosticCode::LocalOverride => "local_override",
ResolutionDiagnosticCode::Shadowed => "shadowed",
ResolutionDiagnosticCode::RequiredExpanded => "required_expanded",
ResolutionDiagnosticCode::CrossSourceRequire => "cross_source_require",
ResolutionDiagnosticCode::RequiredBlocked => "required_blocked",
ResolutionDiagnosticCode::LegacyBareApproval => "legacy_bare_approval",
}
}
#[must_use]
pub fn closure_block_reason_name(reason: ClosureBlockReason) -> &'static str {
match reason {
ClosureBlockReason::Missing => "missing",
ClosureBlockReason::PendingApproval => "pending approval",
ClosureBlockReason::ShadowedNotSatisfied => "shadowed but not satisfied",
ClosureBlockReason::SameNameBlocked => "blocked by a same-name target entry",
ClosureBlockReason::Unlinked => "unlinked",
}
}
fn ref_matches_skill(reference: &str, skill: &SkillRecord) -> bool {
skill.slot_name == reference
|| skill.source_ref == reference
|| skill.id.as_deref() == Some(reference)
}
fn inventory_for<'a>(
inventories: &'a [SourceInventory],
source_id: &str,
) -> Option<&'a [SkillRecord]> {
inventories
.iter()
.find(|inventory| inventory.source_id == source_id)
.map(|inventory| inventory.skills.as_slice())
}
fn expand_catalog_selections(
source_by_id: &BTreeMap<&str, &SourceConfig>,
inventories: &[SourceInventory],
) -> BTreeMap<String, Vec<String>> {
let mut expanded = BTreeMap::new();
for inventory in inventories {
let Some(source) = source_by_id.get(inventory.source_id.as_str()) else {
continue;
};
if source.kind != SourceKind::Catalog {
continue;
}
expanded.insert(
source.id.clone(),
closure_for_selection(&source.selection, &inventory.skills),
);
}
expanded
}
fn closure_for_selection(selection: &[String], skills: &[SkillRecord]) -> Vec<String> {
let mut result: Vec<String> = selection.to_vec();
let mut seen: BTreeSet<String> = selection.iter().cloned().collect();
let mut queue: VecDeque<String> = selection.iter().cloned().collect();
while let Some(reference) = queue.pop_front() {
let Some(skill) = skills
.iter()
.find(|skill| ref_matches_skill(&reference, skill))
else {
continue;
};
for requirement in &skill.requires {
let Some(required) = skills
.iter()
.find(|skill| ref_matches_skill(requirement, skill))
else {
continue;
};
let canonical = required.slot_name.clone();
if seen.insert(canonical.clone()) {
result.push(canonical.clone());
queue.push_back(canonical);
}
}
}
result
}
enum RequirementStatus {
Satisfied,
Block(ClosureBlockReason),
}
fn requirement_status(
requirement: &str,
required: &SkillRecord,
active: &[ResolvedSkill],
pending_refs: &BTreeSet<&str>,
) -> RequirementStatus {
let winner = active
.iter()
.find(|skill| skill.slot_name == required.slot_name);
match winner {
Some(skill) if skill.source_ref == required.source_ref => RequirementStatus::Satisfied,
Some(skill) if skill.id.is_some() && skill.id == required.id => {
RequirementStatus::Satisfied
}
Some(_) => {
if requirement == required.source_ref || required.id.as_deref() == Some(requirement) {
RequirementStatus::Block(ClosureBlockReason::ShadowedNotSatisfied)
} else {
RequirementStatus::Satisfied
}
}
None if pending_refs.contains(required.source_ref.as_str()) => {
RequirementStatus::Block(ClosureBlockReason::PendingApproval)
}
None => RequirementStatus::Block(ClosureBlockReason::Unlinked),
}
}
fn find_blocked(
active: &[ResolvedSkill],
pending_refs: &BTreeSet<&str>,
requires_by_ref: &BTreeMap<String, Vec<String>>,
inventories: &[SourceInventory],
) -> Option<(usize, BlockedSkill)> {
for (index, dependent) in active.iter().enumerate() {
let Some(requires) = requires_by_ref.get(&dependent.source_ref) else {
continue;
};
for requirement in requires {
let same_source = inventory_for(inventories, &dependent.source_id).and_then(|skills| {
skills
.iter()
.find(|skill| ref_matches_skill(requirement, skill))
});
let Some(required) = same_source else {
let exists_elsewhere = inventories.iter().any(|inventory| {
inventory.source_id != dependent.source_id
&& inventory
.skills
.iter()
.any(|skill| ref_matches_skill(requirement, skill))
});
if exists_elsewhere {
continue;
}
return Some((
index,
BlockedSkill {
skill: dependent.clone(),
requirement: requirement.clone(),
reason: ClosureBlockReason::Missing,
},
));
};
if let RequirementStatus::Block(reason) =
requirement_status(requirement, required, active, pending_refs)
{
return Some((
index,
BlockedSkill {
skill: dependent.clone(),
requirement: requirement.clone(),
reason,
},
));
}
}
}
None
}
fn required_closure_preflight(
active: &mut Vec<ResolvedSkill>,
pending: &[ResolvedSkill],
requires_by_ref: &BTreeMap<String, Vec<String>>,
inventories: &[SourceInventory],
) -> (Vec<BlockedSkill>, Vec<ResolutionDiagnostic>) {
let mut blocked = Vec::new();
let mut diagnostics = Vec::new();
let pending_refs: BTreeSet<&str> = pending
.iter()
.map(|skill| skill.source_ref.as_str())
.collect();
while let Some((index, block)) =
find_blocked(active, &pending_refs, requires_by_ref, inventories)
{
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::RequiredBlocked,
message: format!(
"skill `{}` is blocked: requirement `{}` is {}",
block.skill.source_ref,
block.requirement,
closure_block_reason_name(block.reason)
),
source_ref: Some(block.skill.source_ref.clone()),
});
active.remove(index);
blocked.push(block);
}
for dependent in active.iter() {
let Some(requires) = requires_by_ref.get(&dependent.source_ref) else {
continue;
};
for requirement in requires {
let same_source =
inventory_for(inventories, &dependent.source_id).is_some_and(|skills| {
skills
.iter()
.any(|skill| ref_matches_skill(requirement, skill))
});
if same_source {
continue;
}
let cross_source = inventories.iter().any(|inventory| {
inventory.source_id != dependent.source_id
&& inventory
.skills
.iter()
.any(|skill| ref_matches_skill(requirement, skill))
});
if cross_source {
diagnostics.push(ResolutionDiagnostic {
code: ResolutionDiagnosticCode::CrossSourceRequire,
message: format!(
"skill `{}` requires `{}` from another source; reported, not installed",
dependent.source_ref, requirement
),
source_ref: Some(dependent.source_ref.clone()),
});
}
}
}
(blocked, diagnostics)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::inventory::{InventoryWarning, InventoryWarningCode, SkillRecord, SourceInventory};
use proptest::prelude::*;
#[test]
fn diagnostic_review_classification_should_separate_information_from_action() {
for code in [
ResolutionDiagnosticCode::LocalOverride,
ResolutionDiagnosticCode::Shadowed,
ResolutionDiagnosticCode::RequiredExpanded,
] {
assert!(!code.requires_review());
}
for code in [
ResolutionDiagnosticCode::PendingApproval,
ResolutionDiagnosticCode::CrossSourceRequire,
ResolutionDiagnosticCode::RequiredBlocked,
ResolutionDiagnosticCode::LegacyBareApproval,
] {
assert!(code.requires_review());
}
}
#[test]
fn inventory_degrades_source_for_subtree_unreadable_warning() {
let inventory = SourceInventory {
source_id: "company".to_owned(),
skills: vec![skill("company", "review")],
warnings: vec![InventoryWarning {
code: InventoryWarningCode::UnreadablePath,
path: PathBuf::from("/repo/skills/private"),
message: "permission denied".to_owned(),
}],
};
assert!(inventory_degrades_source_for_removal(&inventory));
}
#[test]
fn inventory_degrades_source_for_invalidated_slot_name() {
let inventory = SourceInventory {
source_id: "company".to_owned(),
skills: Vec::new(),
warnings: vec![InventoryWarning {
code: InventoryWarningCode::InvalidSlotName,
path: PathBuf::from("/repo/skills/Review/SKILL.md"),
message: "folder name `Review` is not a valid slot name".to_owned(),
}],
};
assert!(inventory_degrades_source_for_removal(&inventory));
}
#[test]
fn resolve_should_pick_lower_priority_skill_for_same_slot() {
let resolution = resolve_with(
vec![
source("team-a", SourceKind::Team, 10),
source("team-b", SourceKind::Team, 20),
],
vec![
inventory("team-a", vec![skill("team-a", "copy-editing")]),
inventory("team-b", vec![skill("team-b", "copy-editing")]),
],
vec![approval("source", "team-a"), approval("source", "team-b")],
);
assert_eq!(
resolution.active_skills[0].source_ref,
"team-a:copy-editing"
);
}
#[test]
fn resolve_should_tie_break_equal_priority_by_source_id() {
let resolution = resolve_with(
vec![
source("z-team", SourceKind::Team, 10),
source("a-team", SourceKind::Team, 10),
],
vec![
inventory("z-team", vec![skill("z-team", "copy-editing")]),
inventory("a-team", vec![skill("a-team", "copy-editing")]),
],
vec![approval("source", "z-team"), approval("source", "a-team")],
);
assert_eq!(
resolution.active_skills[0].source_ref,
"a-team:copy-editing"
);
}
#[test]
fn resolve_should_mark_local_winner_as_local_override() {
let resolution = resolve_with(
vec![
source("local", SourceKind::Local, 0),
source("company", SourceKind::Team, 10),
],
vec![
inventory("local", vec![skill("local", "review")]),
inventory("company", vec![skill("company", "review")]),
],
vec![approval("source", "company")],
);
assert!(resolution.active_skills[0].local_override);
}
#[test]
fn resolve_should_keep_lower_priority_approved_skill_when_winner_is_unapproved() {
let resolution = resolve_with(
vec![
source("new-team", SourceKind::Team, 0),
source("old-team", SourceKind::Team, 10),
],
vec![
inventory("new-team", vec![skill("new-team", "review")]),
inventory("old-team", vec![skill("old-team", "review")]),
],
vec![approval("source", "old-team")],
);
assert_eq!(resolution.active_skills[0].source_ref, "old-team:review");
}
#[test]
fn resolve_should_not_match_bare_skill_approval() {
let resolution = resolve_with(
vec![source("company", SourceKind::Team, 10)],
vec![inventory("company", vec![skill("company", "review")])],
vec![approval("skill", "review")],
);
assert!(resolution.active_skills.is_empty());
assert_eq!(
resolution.pending_approval_skills[0].source_ref,
"company:review"
);
assert!(resolution.diagnostics.iter().any(|diagnostic| {
diagnostic.code == ResolutionDiagnosticCode::LegacyBareApproval
&& diagnostic
.message
.contains("re-approve as `company:review`")
}));
}
#[test]
fn resolve_should_not_match_bare_stable_id_approval() {
let resolution = resolve_with(
vec![source("company", SourceKind::Team, 10)],
vec![inventory(
"company",
vec![skill_with_id("company", "review", "shared.review")],
)],
vec![approval("skill", "shared.review")],
);
assert!(resolution.active_skills.is_empty());
assert_eq!(
resolution.pending_approval_skills[0].source_ref,
"company:review"
);
assert!(resolution.diagnostics.iter().any(|diagnostic| {
diagnostic.code == ResolutionDiagnosticCode::LegacyBareApproval
&& diagnostic
.message
.contains("re-approve as `company:shared.review`")
}));
}
#[test]
fn resolve_should_match_source_qualified_skill_approval_only() {
let resolution = resolve_with(
vec![
source("new-team", SourceKind::Team, 0),
source("old-team", SourceKind::Team, 10),
],
vec![
inventory("new-team", vec![skill("new-team", "review")]),
inventory("old-team", vec![skill("old-team", "review")]),
],
vec![approval("skill", "old-team:review")],
);
assert_eq!(resolution.active_skills[0].source_ref, "old-team:review");
assert_eq!(
resolution.pending_approval_skills[0].source_ref,
"new-team:review"
);
}
#[test]
fn resolve_should_match_source_qualified_stable_id_approval_only() {
let resolution = resolve_with(
vec![
source("new-team", SourceKind::Team, 0),
source("old-team", SourceKind::Team, 10),
],
vec![
inventory(
"new-team",
vec![skill_with_id("new-team", "review", "shared.review")],
),
inventory(
"old-team",
vec![skill_with_id("old-team", "review", "shared.review")],
),
],
vec![approval("skill", "old-team:shared.review")],
);
assert_eq!(resolution.active_skills[0].source_ref, "old-team:review");
assert_eq!(
resolution.pending_approval_skills[0].source_ref,
"new-team:review"
);
}
#[test]
fn resolve_should_match_source_qualified_author_approval_against_owners() {
let mut team_skill = skill("company", "review");
team_skill.owners = vec!["core-team".to_owned()];
let resolution = resolve_with(
vec![source("company", SourceKind::Team, 10)],
vec![inventory("company", vec![team_skill])],
vec![approval("author", "company:core-team")],
);
assert_eq!(resolution.active_skills[0].source_ref, "company:review");
}
#[test]
fn resolve_should_not_apply_owner_approval_to_another_source() {
let mut approved_source_skill = skill("reviewed", "review");
approved_source_skill.owners = vec!["core-team".to_owned()];
let mut spoofed_source_skill = skill("unreviewed", "review");
spoofed_source_skill.owners = vec!["core-team".to_owned()];
let resolution = resolve_with(
vec![
source("unreviewed", SourceKind::Team, 0),
source("reviewed", SourceKind::Team, 10),
],
vec![
inventory("unreviewed", vec![spoofed_source_skill]),
inventory("reviewed", vec![approved_source_skill]),
],
vec![approval("org", "reviewed:core-team")],
);
assert_eq!(resolution.active_skills[0].source_ref, "reviewed:review");
assert_eq!(
resolution.pending_approval_skills[0].source_ref,
"unreviewed:review"
);
}
#[test]
fn resolve_should_hold_unapproved_team_skill_as_pending() {
let resolution = resolve_with(
vec![source("company", SourceKind::Team, 10)],
vec![inventory("company", vec![skill("company", "review")])],
Vec::new(),
);
assert!(resolution.active_skills.is_empty());
}
#[test]
fn resolve_should_list_unapproved_team_skill_in_pending_approval() {
let resolution = resolve_with(
vec![source("company", SourceKind::Team, 10)],
vec![inventory("company", vec![skill("company", "review")])],
Vec::new(),
);
assert_eq!(
resolution.pending_approval_skills[0].source_ref,
"company:review"
);
}
#[test]
fn resolve_should_list_all_unapproved_same_slot_skills_in_pending_approval() {
let resolution = resolve_with(
vec![
source("team-a", SourceKind::Team, 10),
source("team-b", SourceKind::Team, 20),
],
vec![
inventory("team-a", vec![skill("team-a", "review")]),
inventory("team-b", vec![skill("team-b", "review")]),
],
Vec::new(),
);
let pending = resolution
.pending_approval_skills
.iter()
.map(|skill| skill.source_ref.as_str())
.collect::<Vec<_>>();
assert!(resolution.active_skills.is_empty());
assert_eq!(pending, vec!["team-a:review", "team-b:review"]);
}
#[test]
fn resolve_should_approve_trusted_source_without_record() {
let mut trusted = source("company", SourceKind::Team, 10);
trusted.trusted = true;
let resolution = resolve_with(
vec![trusted],
vec![inventory("company", vec![skill("company", "review")])],
Vec::new(),
);
assert_eq!(resolution.active_skills[0].source_ref, "company:review");
}
proptest! {
#[test]
fn resolve_should_be_order_independent_across_input_permutations(
source_order in prop::sample::select(index_permutations()),
inventory_order in prop::sample::select(index_permutations()),
) {
let sources = vec![
source("z-team", SourceKind::Team, 10),
source("a-team", SourceKind::Team, 10),
source("c-team", SourceKind::Team, 5),
];
let inventories = vec![
inventory(
"z-team",
vec![skill("z-team", "review"), skill("z-team", "format")],
),
inventory("a-team", vec![skill("a-team", "review")]),
inventory("c-team", vec![skill("c-team", "format")]),
];
let approvals = vec![
approval("source", "z-team"),
approval("source", "a-team"),
approval("source", "c-team"),
];
let baseline =
resolve_with(sources.clone(), inventories.clone(), approvals.clone()).active_skills;
prop_assert_eq!(
resolve_with(
permute(&sources, &source_order),
permute(&inventories, &inventory_order),
approvals,
)
.active_skills,
baseline
);
}
}
fn index_permutations() -> Vec<Vec<usize>> {
vec![
vec![0, 1, 2],
vec![0, 2, 1],
vec![1, 0, 2],
vec![1, 2, 0],
vec![2, 0, 1],
vec![2, 1, 0],
]
}
fn permute<T: Clone>(items: &[T], order: &[usize]) -> Vec<T> {
order.iter().map(|index| items[*index].clone()).collect()
}
fn resolve_with(
sources: Vec<SourceConfig>,
inventories: Vec<SourceInventory>,
approvals: Vec<ApprovalRecord>,
) -> Resolution {
resolve(&ResolutionInput {
sources: &sources,
inventories,
approvals,
})
}
fn source(id: &str, kind: SourceKind, priority: i32) -> SourceConfig {
SourceConfig {
id: id.to_owned(),
kind,
path: PathBuf::from(format!("/tmp/{id}")),
priority,
enabled: true,
trusted: false,
url: None,
branch: None,
update_policy: None,
selection: Vec::new(),
}
}
fn inventory(source_id: &str, skills: Vec<SkillRecord>) -> SourceInventory {
SourceInventory {
source_id: source_id.to_owned(),
skills,
warnings: Vec::new(),
}
}
fn skill(source_id: &str, slot_name: &str) -> SkillRecord {
SkillRecord {
source_id: source_id.to_owned(),
source_ref: format!("{source_id}:{slot_name}"),
id: None,
slot_name: slot_name.to_owned(),
path: PathBuf::from(format!("/tmp/{source_id}/{slot_name}")),
skill_file: PathBuf::from(format!("/tmp/{source_id}/{slot_name}/SKILL.md")),
description: None,
requires: Vec::new(),
owners: Vec::new(),
tags: Vec::new(),
}
}
fn skill_with_id(source_id: &str, slot_name: &str, id: &str) -> SkillRecord {
let mut skill = skill(source_id, slot_name);
skill.id = Some(id.to_owned());
skill
}
fn approval(scope: &str, value: &str) -> ApprovalRecord {
ApprovalRecord {
scope: scope.to_owned(),
value: value.to_owned(),
}
}
fn catalog(id: &str, priority: i32, selection: &[&str]) -> SourceConfig {
let mut source = source(id, SourceKind::Catalog, priority);
source.trusted = true;
source.selection = selection.iter().map(|item| (*item).to_owned()).collect();
source
}
fn skill_req(source_id: &str, slot_name: &str, requires: &[&str]) -> SkillRecord {
let mut record = skill(source_id, slot_name);
record.requires = requires.iter().map(|item| (*item).to_owned()).collect();
record
}
fn active_slots(resolution: &Resolution) -> Vec<&str> {
resolution
.active_skills
.iter()
.map(|skill| skill.slot_name.as_str())
.collect()
}
#[test]
fn resolve_should_expand_same_catalog_required_closure() {
let resolution = resolve_with(
vec![catalog("cat", 10, &["alpha"])],
vec![inventory(
"cat",
vec![skill_req("cat", "alpha", &["beta"]), skill("cat", "beta")],
)],
Vec::new(),
);
let active = active_slots(&resolution);
assert!(active.contains(&"alpha"));
assert!(active.contains(&"beta"));
assert!(resolution.blocked_skills.is_empty());
}
#[test]
fn resolve_should_expand_required_closure_transitively() {
let resolution = resolve_with(
vec![catalog("cat", 10, &["alpha"])],
vec![inventory(
"cat",
vec![
skill_req("cat", "alpha", &["beta"]),
skill_req("cat", "beta", &["gamma"]),
skill("cat", "gamma"),
],
)],
Vec::new(),
);
assert!(active_slots(&resolution).contains(&"gamma"));
}
#[test]
fn resolve_should_block_dependent_on_missing_requirement() {
let resolution = resolve_with(
vec![catalog("cat", 10, &["alpha"])],
vec![inventory(
"cat",
vec![skill_req("cat", "alpha", &["ghost"])],
)],
Vec::new(),
);
assert!(resolution.active_skills.is_empty());
assert_eq!(resolution.blocked_skills.len(), 1);
assert_eq!(
resolution.blocked_skills[0].reason,
ClosureBlockReason::Missing
);
}
#[test]
fn resolve_should_block_dependent_when_requirement_pending_approval() {
let resolution = resolve_with(
vec![source("team", SourceKind::Team, 10)],
vec![inventory(
"team",
vec![skill_req("team", "alpha", &["beta"]), skill("team", "beta")],
)],
vec![approval("skill", "team:alpha")],
);
assert!(!active_slots(&resolution).contains(&"alpha"));
assert_eq!(
resolution.blocked_skills[0].reason,
ClosureBlockReason::PendingApproval
);
}
#[test]
fn resolve_should_block_source_qualified_requirement_when_shadowed() {
let mut team = source("team", SourceKind::Team, 5);
team.trusted = true;
let resolution = resolve_with(
vec![catalog("cat", 10, &["alpha"]), team],
vec![
inventory(
"cat",
vec![
skill_req("cat", "alpha", &["cat:beta"]),
skill("cat", "beta"),
],
),
inventory("team", vec![skill("team", "beta")]),
],
Vec::new(),
);
assert!(
!resolution
.active_skills
.iter()
.any(|skill| skill.source_ref == "cat:alpha")
);
assert!(
resolution
.active_skills
.iter()
.any(|skill| skill.source_ref == "team:beta")
);
assert_eq!(
resolution.blocked_skills[0].reason,
ClosureBlockReason::ShadowedNotSatisfied
);
assert_eq!(resolution.blocked_skills[0].requirement, "cat:beta");
}
#[test]
fn resolve_should_report_cross_source_require_without_installing() {
let mut team = source("team", SourceKind::Team, 20);
team.trusted = true;
let resolution = resolve_with(
vec![catalog("cat", 10, &["alpha"]), team],
vec![
inventory("cat", vec![skill_req("cat", "alpha", &["beta"])]),
inventory("team", vec![skill("team", "beta")]),
],
Vec::new(),
);
assert!(active_slots(&resolution).contains(&"alpha"));
assert!(
resolution
.diagnostics
.iter()
.any(|diagnostic| diagnostic.code == ResolutionDiagnosticCode::CrossSourceRequire)
);
}
}