# Path: siptools/scripts/create_agent.py
# def create_agent(**kwargs):
#     """
#     The script collects provenance metadata for the package. The metadata
#     consists of information about the agent and linking information to
#     the event that the agent relates to. The result of this function is
#     a JSON file containing metadata about the agent and its role in
#     relation to the event.
#     If the given JSON file exists, the new agent metadata is appended
#     to the existing data, allowing for multiple agents to be related to
#     the same event.
# 
#     :kwargs: Given arguments
#              agent_name: agent name
#              workspace: Workspace path
#              agent_type: agent type
#              agent_version: Agent version if agent type is "software"
#              agent_role: agent role in relation to the event
#              agent_note: Agent note as a string
#              agent_identifier: Agent identifier type and value (tuple)
#              create_agent_file: The name of the JSON file that collects
#                                 agent information in relation to the
#                                 event
# 
#     :returns: The agent identifier value as a string
#     """
#     attributes = _attribute_values(kwargs)
# 
#     agent_dict = {
#         "identifier_type": attributes["agent_identifier"][0],
#         "identifier_value": attributes["agent_identifier"][1],
#         "agent_name": attributes["agent_name"],
#         "agent_type": attributes["agent_type"],
#     }
#     if attributes["agent_version"]:
#         agent_dict["agent_version"] = attributes["agent_version"]
#     if attributes["agent_role"]:
#         agent_dict["agent_role"] = attributes["agent_role"]
#     if attributes["agent_note"]:
#         agent_dict["agent_note"] = attributes["agent_note"]
# 
#     output_path = os.path.join(
#         attributes["workspace"],
#         attributes["create_agent_file"] + '-AGENTS-amd.json')
# 
#     agents_list = []
#     if os.path.exists(output_path):
#         with open(output_path) as in_file:
#             agents_list = json.load(in_file)
# 
#     agents_list.append(agent_dict)
# 
#     with open(output_path, 'wt') as outfile:
#         json.dump(agents_list, outfile, indent=4)
# 
#     print(
#         "Collected agent metadata with identifier %s" %
#         attributes["agent_identifier"][1])
# 
#     return attributes["agent_identifier"][1]


import os
import io
import json
import pytest
from siptools.scripts import create_agent
    ],
    ids=("Agent with minimum data",
         "Agent with event role",
         "Agent with given identifier that should be written to the output",
         "Agent_version that shouldn't be written due to the agent_type",
         "Agent with agent_version that should be written",
         "Multiple agents, all should be written to the same output"))
# pylint: disable=too-many-arguments
def test_create_agent_ok(
        testpath, run_cli, given_identifier, role, version, ag_type, ag_count):
    """Test that main function produces a json file with
    correct data for different input.
    """
    for i in range(ag_count):
        agent_name = 'test-agent%i' % i
        cli_args = [
            agent_name,
            '--workspace', testpath,
            '--agent_type', ag_type,
            '--agent_version', version,
            '--agent_note', 'Notes',
            '--create_agent_file', 'test-file',
        ]
        if given_identifier:
            cli_args.append('--agent_identifier')
            cli_args.append('test')
            cli_args.append('foo')
        if role:
            cli_args.append('--agent_role')
            cli_args.append(role)

# Path: siptools/scripts/create_agent.py
# def create_agent(**kwargs):
#     """
#     The script collects provenance metadata for the package. The metadata
#     consists of information about the agent and linking information to
#     the event that the agent relates to. The result of this function is
#     a JSON file containing metadata about the agent and its role in
#     relation to the event.
#     If the given JSON file exists, the new agent metadata is appended
#     to the existing data, allowing for multiple agents to be related to
#     the same event.
# 
#     :kwargs: Given arguments
#              agent_name: agent name
#              workspace: Workspace path
#              agent_type: agent type
#              agent_version: Agent version if agent type is "software"
#              agent_role: agent role in relation to the event
#              agent_note: Agent note as a string
#              agent_identifier: Agent identifier type and value (tuple)
#              create_agent_file: The name of the JSON file that collects
#                                 agent information in relation to the
#                                 event
# 
#     :returns: The agent identifier value as a string
#     """
#     attributes = _attribute_values(kwargs)
# 
#     agent_dict = {
#         "identifier_type": attributes["agent_identifier"][0],
#         "identifier_value": attributes["agent_identifier"][1],
#         "agent_name": attributes["agent_name"],
#         "agent_type": attributes["agent_type"],
#     }
#     if attributes["agent_version"]:
#         agent_dict["agent_version"] = attributes["agent_version"]
#     if attributes["agent_role"]:
#         agent_dict["agent_role"] = attributes["agent_role"]
#     if attributes["agent_note"]:
#         agent_dict["agent_note"] = attributes["agent_note"]
# 
#     output_path = os.path.join(
#         attributes["workspace"],
#         attributes["create_agent_file"] + '-AGENTS-amd.json')
# 
#     agents_list = []
#     if os.path.exists(output_path):
#         with open(output_path) as in_file:
#             agents_list = json.load(in_file)
# 
#     agents_list.append(agent_dict)
# 
#     with open(output_path, 'wt') as outfile:
#         json.dump(agents_list, outfile, indent=4)
# 
#     print(
#         "Collected agent metadata with identifier %s" %
#         attributes["agent_identifier"][1])
# 
#     return attributes["agent_identifier"][1]


# Path: tests/scripts/test_create_agent.py
import os
import io
import json
import pytest
from siptools.scripts import create_agent

    ],
    ids=("Agent with minimum data",
         "Agent with event role",
         "Agent with given identifier that should be written to the output",
         "Agent_version that shouldn't be written due to the agent_type",
         "Agent with agent_version that should be written",
         "Multiple agents, all should be written to the same output"))
# pylint: disable=too-many-arguments
def test_create_agent_ok(
        testpath, run_cli, given_identifier, role, version, ag_type, ag_count):
    """Test that main function produces a json file with
    correct data for different input.
    """
    for i in range(ag_count):
        agent_name = 'test-agent%i' % i
        cli_args = [
            agent_name,
            '--workspace', testpath,
            '--agent_type', ag_type,
            '--agent_version', version,
            '--agent_note', 'Notes',
            '--create_agent_file', 'test-file',
        ]
        if given_identifier:
            cli_args.append('--agent_identifier')
            cli_args.append('test')
            cli_args.append('foo')
        if role:
            cli_args.append('--agent_role')
            cli_args.append(role)

        run_cli(create_agent.main, cli_args)