use core_protocol::{
    KernelBlockAssetRelation, KernelObjectIdentity, KernelObjectKind, KernelProjectionAssetKind,
    KernelProjectionResourceKind, KernelProjectionResourceMeta,
};

#[test]
fn object_identity_separates_index_body_from_mindmap_asset() {
    let index_identity = KernelObjectIdentity {
        object_kind: KernelObjectKind::Index,
        document_id: Some("doc_1".into()),
        block_id: None,
        asset_id: None,
    };
    let mindmap_identity = KernelObjectIdentity {
        object_kind: KernelObjectKind::Mindmap,
        document_id: Some("doc_1".into()),
        block_id: Some("block_mindmap_1".into()),
        asset_id: Some("mind_1".into()),
    };

    assert_ne!(index_identity, mindmap_identity);

    let index_json = serde_json::to_value(&index_identity).expect("index identity 序列化");
    let mindmap_json = serde_json::to_value(&mindmap_identity).expect("mindmap identity 序列化");

    assert_eq!(index_json["objectKind"], "index");
    assert_eq!(index_json["documentId"], "doc_1");
    assert_eq!(mindmap_json["objectKind"], "mindmap");
    assert_eq!(mindmap_json["assetId"], "mind_1");
}

#[test]
fn resource_meta_can_describe_asset_object_kinds() {
    let cases = [
        (
            KernelObjectKind::Mindmap,
            KernelProjectionResourceKind::Mindmap,
            KernelProjectionAssetKind::Mindmap,
            "mind_1",
        ),
        (
            KernelObjectKind::OnlyOffice,
            KernelProjectionResourceKind::OnlyOffice,
            KernelProjectionAssetKind::File,
            "office_1",
        ),
        (
            KernelObjectKind::Attachment,
            KernelProjectionResourceKind::Attachment,
            KernelProjectionAssetKind::Image,
            "image_1",
        ),
        (
            KernelObjectKind::Code,
            KernelProjectionResourceKind::Code,
            KernelProjectionAssetKind::File,
            "code_1",
        ),
    ];

    for (object_kind, resource_kind, asset_kind, asset_id) in cases {
        let meta = KernelProjectionResourceMeta {
            resource_kind: Some(resource_kind),
            document_id: Some("doc_1".into()),
            asset_id: Some(asset_id.into()),
            workspace_id: Some("ws_1".into()),
            asset_kind: Some(asset_kind.clone()),
            icon_hint: None,
            object_identity: Some(KernelObjectIdentity {
                object_kind,
                document_id: Some("doc_1".into()),
                block_id: Some("block_1".into()),
                asset_id: Some(asset_id.into()),
            }),
            block_asset_relation: Some(KernelBlockAssetRelation {
                document_id: "doc_1".into(),
                block_id: "block_1".into(),
                asset_id: asset_id.into(),
                asset_kind,
            }),
            extra: Default::default(),
        };

        let json = serde_json::to_value(&meta).expect("resource meta 序列化");
        assert_eq!(json["objectIdentity"]["documentId"], "doc_1");
        assert_eq!(json["blockAssetRelation"]["blockId"], "block_1");
        assert_eq!(json["blockAssetRelation"]["assetId"], asset_id);
    }
}
