use app_tauri_lib::dto::{
    PluginAdminDto, PluginContributionSummaryDto, PluginRuntimeContributionCatalogDto,
    PluginRuntimePluginDto,
};
use domain::{PluginCapability, PluginContributionSet, PluginLifecycleState, PluginTrustLevel};

#[test]
fn plugin_admin_dto_serialises_exact_contract_shape() {
    let dto = PluginAdminDto {
        id: "dev.acme.gitgraph".to_owned(),
        display_name: "Git Graph".to_owned(),
        publisher: Some("Acme".to_owned()),
        version: "1.2.3".to_owned(),
        description: Some("Graph".to_owned()),
        icon_url: Some("idea-plugin://dev.acme.gitgraph/1.2.3/abc/assets/icon.svg".to_owned()),
        source_kind: "archive".to_owned(),
        source_label: Some("/tmp/gitgraph.ideaplug".to_owned()),
        lifecycle_state: PluginLifecycleState::PendingDisable,
        enabled: false,
        pending_enable_state: Some(false),
        pending_uninstall: false,
        restart_required: true,
        trust_level: PluginTrustLevel::Full,
        contribution_summary: PluginContributionSummaryDto {
            top_level_menus: 1,
            menu_items: 2,
            layouts: 3,
            mcp_servers: 4,
        },
        error: None,
    };

    let value = serde_json::to_value(dto).unwrap();
    assert_eq!(value["displayName"], "Git Graph");
    assert_eq!(value["lifecycleState"], "pending-disable");
    assert_eq!(value["trustLevel"], "full");
    assert_eq!(value["contributionSummary"]["topLevelMenus"], 1);
}

#[test]
fn runtime_catalog_dto_carries_bundle_hash_and_contributions() {
    let dto = PluginRuntimeContributionCatalogDto {
        plugins: vec![PluginRuntimePluginDto {
            id: "dev.acme.gitgraph".to_owned(),
            display_name: "Git Graph".to_owned(),
            publisher: None,
            version: "1.2.3".to_owned(),
            bundle_url: "idea-plugin://dev.acme.gitgraph/1.2.3/abc/dist/index.js".to_owned(),
            icon_url: None,
            content_hash: "abc".to_owned(),
            capabilities: vec![PluginCapability::Ui, PluginCapability::Tooling],
            activation_scope: domain::PluginActivationScope::Project,
            contributes: PluginContributionSet::default(),
        }],
    };

    let value = serde_json::to_value(dto).unwrap();
    assert_eq!(
        value["plugins"][0]["bundleUrl"],
        "idea-plugin://dev.acme.gitgraph/1.2.3/abc/dist/index.js"
    );
    assert_eq!(value["plugins"][0]["contentHash"], "abc");
    assert_eq!(
        value["plugins"][0]["capabilities"],
        serde_json::json!(["ui", "tooling"])
    );
    assert_eq!(value["plugins"][0]["activationScope"], "project");
    assert!(value["plugins"][0]["contributes"]["menus"]
        .as_array()
        .unwrap()
        .is_empty());
}
