From eda0151b4486f0f9700d024304e2b4d297e49653 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 26 Feb 2026 14:22:44 +0800 Subject: [PATCH] fix bugs Signed-off-by: unknown --- .../src/ets_agent_manager_utils.cpp | 9 +- .../ets/ets/@ohos.app.agent.AgentCard.ets | 4 +- .../src/js_agent_manager_utils.cpp | 2 +- .../interfaces/inner_api/include/agent_card.h | 2 +- .../interfaces/inner_api/src/agent_card.cpp | 103 ++++-- .../services/agentmgr/src/agent_card_mgr.cpp | 10 +- .../agent_card_test/agent_card_test.cpp | 346 ++++++++++++++---- 7 files changed, 364 insertions(+), 112 deletions(-) diff --git a/agent_runtime_framework/frameworks/ets/ani/agent_manager/src/ets_agent_manager_utils.cpp b/agent_runtime_framework/frameworks/ets/ani/agent_manager/src/ets_agent_manager_utils.cpp index 6cdaf9b807..53845d4952 100644 --- a/agent_runtime_framework/frameworks/ets/ani/agent_manager/src/ets_agent_manager_utils.cpp +++ b/agent_runtime_framework/frameworks/ets/ani/agent_manager/src/ets_agent_manager_utils.cpp @@ -169,12 +169,9 @@ ani_object CreateEtsAgentAppInfo(ani_env *env, const AgentAppInfo &appInfo) return nullptr; } } - if (!appInfo.deviceTypes.empty()) { - status = env->Object_SetPropertyByName_Ref(object, "deviceTypes", GetAniString(env, appInfo.deviceTypes)); - if (status != ANI_OK) { - TAG_LOGE(AAFwkTag::SER_ROUTER, "set deviceTypes failed: %{public}d", status); - return nullptr; - } + if (appInfo.deviceTypes.size() > 0 && !SetStringArrayProperty(env, object, "deviceTypes", appInfo.deviceTypes)) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "set deviceTypes failed"); + return nullptr; } if (!appInfo.minAppVersion.empty()) { status = env->Object_SetPropertyByName_Ref(object, "minAppVersion", GetAniString(env, appInfo.minAppVersion)); diff --git a/agent_runtime_framework/frameworks/ets/ets/@ohos.app.agent.AgentCard.ets b/agent_runtime_framework/frameworks/ets/ets/@ohos.app.agent.AgentCard.ets index a516e2f2b6..7d3e2ae84d 100644 --- a/agent_runtime_framework/frameworks/ets/ets/@ohos.app.agent.AgentCard.ets +++ b/agent_runtime_framework/frameworks/ets/ets/@ohos.app.agent.AgentCard.ets @@ -27,7 +27,7 @@ export interface AgentAppInfo { bundleName?: string; moduleName?: string; abilityName?: string; - deviceTypes?: string; + deviceTypes?: Array; minAppVersion?: string; } @@ -35,7 +35,7 @@ class AgentAppInfoImpl implements AgentAppInfo { public bundleName?: string; public moduleName?: string; public abilityName?: string; - public deviceTypes?: string; + public deviceTypes?: Array; public minAppVersion?: string; } diff --git a/agent_runtime_framework/frameworks/js/napi/agent_manager/src/js_agent_manager_utils.cpp b/agent_runtime_framework/frameworks/js/napi/agent_manager/src/js_agent_manager_utils.cpp index 99085b69cc..10d00639c6 100644 --- a/agent_runtime_framework/frameworks/js/napi/agent_manager/src/js_agent_manager_utils.cpp +++ b/agent_runtime_framework/frameworks/js/napi/agent_manager/src/js_agent_manager_utils.cpp @@ -58,7 +58,7 @@ napi_value CreateJsAgentAppInfo(napi_env env, const AgentAppInfo &appInfo) napi_set_named_property(env, object, "abilityName", CreateJsValue(env, appInfo.abilityName)); } if (!appInfo.deviceTypes.empty()) { - napi_set_named_property(env, object, "deviceTypes", CreateJsValue(env, appInfo.deviceTypes)); + napi_set_named_property(env, object, "deviceTypes", CreateNativeArray(env, appInfo.deviceTypes)); } if (!appInfo.minAppVersion.empty()) { napi_set_named_property(env, object, "minAppVersion", CreateJsValue(env, appInfo.minAppVersion)); diff --git a/agent_runtime_framework/interfaces/inner_api/include/agent_card.h b/agent_runtime_framework/interfaces/inner_api/include/agent_card.h index 88128f1cbb..b0c859f8fd 100644 --- a/agent_runtime_framework/interfaces/inner_api/include/agent_card.h +++ b/agent_runtime_framework/interfaces/inner_api/include/agent_card.h @@ -80,7 +80,7 @@ struct AgentAppInfo : public Parcelable { std::string bundleName; std::string moduleName; std::string abilityName; - std::string deviceTypes; + std::vector deviceTypes; std::string minAppVersion; bool ReadFromParcel(Parcel &parcel); virtual bool Marshalling(Parcel &parcel) const override; diff --git a/agent_runtime_framework/interfaces/inner_api/src/agent_card.cpp b/agent_runtime_framework/interfaces/inner_api/src/agent_card.cpp index 8e11133d42..9121f564e0 100644 --- a/agent_runtime_framework/interfaces/inner_api/src/agent_card.cpp +++ b/agent_runtime_framework/interfaces/inner_api/src/agent_card.cpp @@ -31,9 +31,10 @@ constexpr uint32_t MAX_AGENT_CARD_COUNT = 200000; constexpr uint32_t LENGTH_32 = 32; constexpr uint32_t LENGTH_64 = 64; constexpr uint32_t LENGTH_128 = 128; +constexpr uint32_t LENGTH_256 = 256; constexpr uint32_t LENGTH_512 = 512; -constexpr uint32_t LENGTH_1280 = 1280; -constexpr uint32_t LENGTH_51200 = 51200; +constexpr uint32_t LENGTH_1024 = 1024; +constexpr uint32_t LENGTH_5120 = 5120; bool AgentProvider::ReadFromParcel(Parcel &parcel) { @@ -168,7 +169,7 @@ AgentCapabilities AgentCapabilities::FromJson(const nlohmann::json &jsonObject) } if (jsonObject.contains("extension") && jsonObject["extension"].is_string()) { capabilities.extension = jsonObject["extension"]; - if (capabilities.extension.length() < 1 || capabilities.extension.length() > LENGTH_1280) { + if (capabilities.extension.length() < 1 || capabilities.extension.length() > LENGTH_1024) { TAG_LOGE(AAFwkTag::SER_ROUTER, "extension length is invalid"); capabilities.extension = ""; } @@ -309,28 +310,40 @@ bool AgentSkill::FromJson(const nlohmann::json &jsonObject, AgentSkill &skill) } if (jsonObject.contains("examples") && jsonObject["examples"].is_array()) { for (const auto &element : jsonObject.at("examples")) { - if (element.is_string()) { - skill.examples.push_back(element.get()); + if (!element.is_string()) { + continue; + } + std::string exampleStr = element.get(); + if (exampleStr.length() <= LENGTH_256 && exampleStr.length() > 0) { + skill.examples.push_back(exampleStr); } } } if (jsonObject.contains("inputModes") && jsonObject["inputModes"].is_array()) { for (const auto &element : jsonObject.at("inputModes")) { - if (element.is_string()) { - skill.inputModes.push_back(element.get()); + if (!element.is_string()) { + continue; + } + std::string inputModeStr = element.get(); + if (inputModeStr.length() <= LENGTH_64 && inputModeStr.length() > 0) { + skill.inputModes.push_back(inputModeStr); } } } if (jsonObject.contains("outputModes") && jsonObject["outputModes"].is_array()) { for (const auto &element : jsonObject.at("outputModes")) { - if (element.is_string()) { - skill.outputModes.push_back(element.get()); + if (!element.is_string()) { + continue; + } + std::string outputModeStr = element.get(); + if (outputModeStr.length() <= LENGTH_64 && outputModeStr.length() > 0) { + skill.outputModes.push_back(outputModeStr); } } } if (jsonObject.contains("extension") && jsonObject["extension"].is_string()) { skill.extension = jsonObject["extension"]; - if (skill.extension.length() < 1 || skill.extension.length() > LENGTH_1280) { + if (skill.extension.length() < 1 || skill.extension.length() > LENGTH_1024) { TAG_LOGE(AAFwkTag::SER_ROUTER, "extension length is invalid"); skill.extension = ""; } @@ -343,7 +356,10 @@ bool AgentAppInfo::ReadFromParcel(Parcel &parcel) bundleName = parcel.ReadString(); moduleName = parcel.ReadString(); abilityName = parcel.ReadString(); - deviceTypes = parcel.ReadString(); + if (!parcel.ReadStringVector(&deviceTypes)) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "read deviceTypes failed"); + return false; + } minAppVersion = parcel.ReadString(); return true; } @@ -362,7 +378,7 @@ bool AgentAppInfo::Marshalling(Parcel &parcel) const TAG_LOGE(AAFwkTag::SER_ROUTER, "write abilityName failed"); return false; } - if (!parcel.WriteString(deviceTypes)) { + if (!parcel.WriteStringVector(deviceTypes)) { TAG_LOGE(AAFwkTag::SER_ROUTER, "write deviceTypes failed"); return false; } @@ -406,12 +422,15 @@ bool AgentAppInfo::FromJson(const nlohmann::json &jsonObject, AgentAppInfo &appI if (jsonObject.contains("abilityName") && jsonObject["abilityName"].is_string()) { appInfo.abilityName = jsonObject["abilityName"]; } - if (jsonObject.contains("deviceTypes") && jsonObject["deviceTypes"].is_string()) { - appInfo.deviceTypes = jsonObject["deviceTypes"]; - if (appInfo.deviceTypes.length() < 1 || appInfo.deviceTypes.length() > LENGTH_128) { - TAG_LOGE(AAFwkTag::SER_ROUTER, "deviceTypes length is invalid"); - appInfo.deviceTypes = ""; - return false; + if (jsonObject.contains("deviceTypes") && jsonObject["deviceTypes"].is_array()) { + for (const auto &element : jsonObject.at("deviceTypes")) { + if (!element.is_string()) { + continue; + } + std::string deviceType = element.get(); + if (deviceType.length() > 0 && deviceType.length() <= LENGTH_32) { + appInfo.deviceTypes.push_back(deviceType); + } } } if (jsonObject.contains("minAppVersion") && jsonObject["minAppVersion"].is_string()) { @@ -419,7 +438,6 @@ bool AgentAppInfo::FromJson(const nlohmann::json &jsonObject, AgentAppInfo &appI if (appInfo.minAppVersion.length() < 1 || appInfo.minAppVersion.length() > LENGTH_32) { TAG_LOGE(AAFwkTag::SER_ROUTER, "minAppVersion length is invalid"); appInfo.minAppVersion = ""; - return false; } } return true; @@ -652,19 +670,40 @@ bool AgentCard::FromJson(nlohmann::json jsonObject, AgentCard &agentCard) } } - if (jsonObject.contains("defaultInputModes") && jsonObject["defaultInputModes"].is_array()) { - for (const auto &element : jsonObject.at("defaultInputModes")) { - if (element.is_string()) { - agentCard.defaultInputModes.push_back(element.get()); - } + if (!jsonObject.contains("defaultInputModes") || !jsonObject["defaultInputModes"].is_array()) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "defaultInputModes is empty"); + return false; + } + for (const auto &element : jsonObject.at("defaultInputModes")) { + if (!element.is_string()) { + continue; + } + std::string intputMode = element.get(); + if (intputMode.length() >= 1 && intputMode.length() <= LENGTH_32) { + agentCard.defaultInputModes.push_back(intputMode); } } - if (jsonObject.contains("defaultOutputModes") && jsonObject["defaultOutputModes"].is_array()) { - for (const auto &element : jsonObject.at("defaultOutputModes")) { - if (element.is_string()) { - agentCard.defaultOutputModes.push_back(element.get()); - } + if (agentCard.defaultInputModes.size() == 0) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "defaultInputModes is empty"); + return false; + } + + if (!jsonObject.contains("defaultOutputModes") || !jsonObject["defaultOutputModes"].is_array()) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "defaultOutputModes is empty"); + return false; + } + for (const auto &element : jsonObject.at("defaultOutputModes")) { + if (!element.is_string()) { + continue; } + std::string outputMode = element.get(); + if (outputMode.length() >= 1 && outputMode.length() <= LENGTH_32) { + agentCard.defaultOutputModes.push_back(outputMode); + } + } + if (agentCard.defaultOutputModes.size() == 0) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "defaultInputModes is empty"); + return false; } // Optional objects @@ -692,6 +731,10 @@ bool AgentCard::FromJson(nlohmann::json jsonObject, AgentCard &agentCard) } } } + if (agentCard.skills.size() == 0) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "skills is empty"); + return false; + } // Optional iconUrl field if (jsonObject.contains("iconUrl") && jsonObject["iconUrl"].is_string()) { @@ -705,7 +748,7 @@ bool AgentCard::FromJson(nlohmann::json jsonObject, AgentCard &agentCard) // Optional extension field if (jsonObject.contains("extension") && jsonObject["extension"].is_string()) { agentCard.extension = jsonObject["extension"]; - if (agentCard.extension.length() < 1 || agentCard.extension.length() > LENGTH_51200) { + if (agentCard.extension.length() < 1 || agentCard.extension.length() > LENGTH_5120) { TAG_LOGE(AAFwkTag::SER_ROUTER, "extension length is invalid"); agentCard.extension = ""; } diff --git a/agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp b/agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp index 27ee8ccd90..4be44977f9 100644 --- a/agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp +++ b/agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp @@ -54,7 +54,7 @@ int32_t AgentCardMgr::HandleBundleInstall(const std::string &bundleName, int32_t TAG_LOGE(AAFwkTag::SER_ROUTER, "Get Bundle Info fail"); return -1; } - std::vector cards; + std::map cards; for (auto const &extensionInfo : bundleInfo.extensionInfos) { if (extensionInfo.type != ExtensionAbilityType::AGENT) { continue; @@ -81,11 +81,15 @@ int32_t AgentCardMgr::HandleBundleInstall(const std::string &bundleName, int32_t card.appInfo->bundleName = bundleName; card.appInfo->moduleName = extensionInfo.moduleName; card.appInfo->abilityName = extensionInfo.name; - cards.push_back(card); + cards[card.agentId] = card; } } } - return AgentCardDbMgr::GetInstance().InsertData(bundleName, userId, cards); + std::vector cardVec; + for (const auto& [key, value] : cards) { + cardVec.push_back(value); + } + return AgentCardDbMgr::GetInstance().InsertData(bundleName, userId, cardVec); } int32_t AgentCardMgr::HandleBundleUpdate(const std::string &bundleName, int32_t userId) diff --git a/test/unittest/agent_runtime_framework/agent_card_test/agent_card_test.cpp b/test/unittest/agent_runtime_framework/agent_card_test/agent_card_test.cpp index 7b6921af0e..02b003ba06 100644 --- a/test/unittest/agent_runtime_framework/agent_card_test/agent_card_test.cpp +++ b/test/unittest/agent_runtime_framework/agent_card_test/agent_card_test.cpp @@ -1008,25 +1008,23 @@ HWTEST_F(AgentCardTest, AgentCardToJson_001, TestSize.Level1) */ HWTEST_F(AgentCardTest, AgentCardFromJson_001, TestSize.Level1) { - AgentProvider provider; - AgentCapabilities capabilities; - std::vector defaultInputModes; - std::vector defaultOutputModes; - AgentSkill skill; - nlohmann::json jsonArray = { skill.ToJson() }; nlohmann::json jsonObject = nlohmann::json { { "agentId", "1" }, { "name", "test" }, { "description", "test" }, { "version", "1.0" }, { "documentationUrl", "http://example.com/docs" }, - { "url", "test" }, { "category", "productivity" }, - { "provider", provider.ToJson() }, - { "capabilities", capabilities.ToJson() }, - { "defaultInputModes", defaultInputModes }, - { "defaultOutputModes", defaultOutputModes }, - { "skills", jsonArray }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; ASSERT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -1786,7 +1784,7 @@ HWTEST_F(AgentCardTest, SkillFromJson_027, TestSize.Level1) /** * @tc.name: SkillFromJson_028 - * @tc.desc: Test FromJson succeeds with valid extension field (length 1-1280) + * @tc.desc: Test FromJson succeeds with valid extension field (length 1-1024) * @tc.type: FUNC * @tc.require: AR000H1N32 */ @@ -1806,13 +1804,13 @@ HWTEST_F(AgentCardTest, SkillFromJson_028, TestSize.Level1) /** * @tc.name: SkillFromJson_029 - * @tc.desc: Test FromJson succeeds when extension length is exactly 1280 + * @tc.desc: Test FromJson succeeds when extension length is exactly 1024 * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, SkillFromJson_029, TestSize.Level1) { - std::string validExtension(1280, 'a'); + std::string validExtension(1024, 'a'); nlohmann::json jsonObject = nlohmann::json { { "id", "test" }, { "name", "test" }, @@ -1822,18 +1820,18 @@ HWTEST_F(AgentCardTest, SkillFromJson_029, TestSize.Level1) }; AgentSkill skill; EXPECT_TRUE(AgentSkill::FromJson(jsonObject, skill)); - EXPECT_EQ(skill.extension.length(), 1280); + EXPECT_EQ(skill.extension.length(), 1024); } /** * @tc.name: SkillFromJson_030 - * @tc.desc: Test FromJson sets extension to empty when length exceeds 1280 + * @tc.desc: Test FromJson sets extension to empty when length exceeds 1024 * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, SkillFromJson_030, TestSize.Level1) { - std::string longExtension(1281, 'a'); + std::string longExtension(1025, 'a'); nlohmann::json jsonObject = nlohmann::json { { "id", "test" }, { "name", "test" }, @@ -2331,6 +2329,12 @@ HWTEST_F(AgentCardTest, ToAgentCardVec_003, TestSize.Level1) */ HWTEST_F(AgentCardTest, ToAgentCardVec_004, TestSize.Level1) { + auto skill = std::make_shared(); + skill->id = "test"; + skill->name = "test"; + skill->description = "test"; + skill->tags = {"test"}; + AgentCard agentCard; agentCard.agentId = "1"; agentCard.name = "ExampleName"; @@ -2342,7 +2346,7 @@ HWTEST_F(AgentCardTest, ToAgentCardVec_004, TestSize.Level1) agentCard.capabilities = std::make_shared(); agentCard.defaultInputModes = {"input1", "input2"}; agentCard.defaultOutputModes = {"output1", "output2"}; - agentCard.skills = {std::make_shared()}; + agentCard.skills = {skill}; AgentCardsRawData rawData; std::vector vec = { agentCard }; @@ -2363,6 +2367,12 @@ HWTEST_F(AgentCardTest, ToAgentCardVec_004, TestSize.Level1) */ HWTEST_F(AgentCardTest, FromAgentCardVec_001, TestSize.Level1) { + auto skill = std::make_shared(); + skill->id = "test"; + skill->name = "test"; + skill->description = "test"; + skill->tags = {"test"}; + AgentCard agentCard; agentCard.agentId = "1"; agentCard.name = "ExampleName"; @@ -2373,7 +2383,7 @@ HWTEST_F(AgentCardTest, FromAgentCardVec_001, TestSize.Level1) agentCard.capabilities = std::make_shared(); agentCard.defaultInputModes = {"input1", "input2"}; agentCard.defaultOutputModes = {"output1", "output2"}; - agentCard.skills = {std::make_shared()}; + agentCard.skills = {skill}; AgentCardsRawData rawData; std::vector vec = { agentCard }; @@ -2475,6 +2485,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_006, TestSize.Level1) { "description", "test description" }, { "version", "1.0" }, { "documentationUrl", "http://example.com" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -2726,6 +2746,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_019, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, // documentationUrl is missing (optional) }; AgentCard agentCard; @@ -2749,6 +2779,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_020, TestSize.Level1) { "description", "test description" }, { "version", "1.0" }, { "documentationUrl", longUrl }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -2770,6 +2810,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_021, TestSize.Level1) { "description", "test description" }, { "version", "1.0" }, { "documentationUrl", "" }, // empty string is allowed + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -2795,6 +2845,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_022, TestSize.Level1) { "description", validDescription }, { "version", validVersion }, { "documentationUrl", validDocUrl }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -2819,6 +2879,15 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_023, TestSize.Level1) { "description", "test description" }, { "version", "1.0" }, { "defaultInputModes", nlohmann::json::array({ "input1", 123, true, "input2", nullptr }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -2841,7 +2910,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_024, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, { "defaultOutputModes", nlohmann::json::array({ "output1", 456, false, "output2", nullptr }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -2864,6 +2942,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_025, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "extension", "valid extension data" }, }; AgentCard agentCard; @@ -2873,41 +2961,61 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_025, TestSize.Level1) /** * @tc.name: AgentCardFromJson_026 - * @tc.desc: Test FromJson succeeds when extension length is exactly 51200 + * @tc.desc: Test FromJson succeeds when extension length is exactly 5120 * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, AgentCardFromJson_026, TestSize.Level1) { - std::string validExtension(51200, 'a'); + std::string validExtension(5120, 'a'); nlohmann::json jsonObject = nlohmann::json { { "agentId", "1" }, { "name", "test" }, { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "extension", validExtension }, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); - EXPECT_EQ(agentCard.extension.length(), 51200); + EXPECT_EQ(agentCard.extension.length(), 5120); } /** * @tc.name: AgentCardFromJson_027 - * @tc.desc: Test FromJson sets extension to empty when length exceeds 51200 + * @tc.desc: Test FromJson sets extension to empty when length exceeds 5120 * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, AgentCardFromJson_027, TestSize.Level1) { - std::string longExtension(51201, 'a'); + std::string longExtension(5121, 'a'); nlohmann::json jsonObject = nlohmann::json { { "agentId", "1" }, { "name", "test" }, { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "extension", longExtension }, }; AgentCard agentCard; @@ -2929,6 +3037,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_028, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "extension", "" }, }; AgentCard agentCard; @@ -2950,6 +3068,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_029, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -2970,6 +3098,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_030, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "extension", 12345 }, }; AgentCard agentCard; @@ -2991,6 +3129,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_041, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "iconUrl", "http://example.com/icon.png" }, }; AgentCard agentCard; @@ -3013,6 +3161,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_031, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "iconUrl", validIconUrl }, }; AgentCard agentCard; @@ -3035,6 +3193,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_032, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "iconUrl", longIconUrl }, }; AgentCard agentCard; @@ -3056,6 +3224,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_033, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "iconUrl", "" }, }; AgentCard agentCard; @@ -3077,6 +3255,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_034, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "iconUrl", 12345 }, }; AgentCard agentCard; @@ -3098,7 +3286,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_035, TestSize.Level1) { "category", "productivity" }, { "description", "test description" }, { "version", "1.0" }, - { "category", "productivity" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -3177,6 +3374,16 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_039, TestSize.Level1) { "description", "test description" }, { "version", "1.0" }, { "category", validCategory }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, }; AgentCard agentCard; EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); @@ -3216,11 +3423,21 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_042, TestSize.Level1) { "description", "test description" }, { "version", "1.0" }, { "category", "productivity" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "appInfo", nlohmann::json { { "bundleName", "test.bundle" }, { "moduleName", "test" }, { "abilityName", "TestAbility" }, - { "deviceTypes", "phone" }, + { "deviceTypes", nlohmann::json::array({ "phone" }) }, { "minAppVersion", "1.0.0" }, }}, }; @@ -3230,31 +3447,42 @@ HWTEST_F(AgentCardTest, AgentCardFromJson_042, TestSize.Level1) EXPECT_EQ(agentCard.appInfo->bundleName, "test.bundle"); EXPECT_EQ(agentCard.appInfo->moduleName, "test"); EXPECT_EQ(agentCard.appInfo->abilityName, "TestAbility"); - EXPECT_EQ(agentCard.appInfo->deviceTypes, "phone"); + EXPECT_EQ(agentCard.appInfo->deviceTypes.size(), 1); + EXPECT_EQ(agentCard.appInfo->deviceTypes[0], "phone"); EXPECT_EQ(agentCard.appInfo->minAppVersion, "1.0.0"); } /** * @tc.name: AgentCardFromJson_043 - * @tc.desc: Test AgentCard FromJson fails when appInfo has invalid deviceTypes + * @tc.desc: Test AgentCard FromJson succeeds with array deviceTypes * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, AgentCardFromJson_043, TestSize.Level1) { - std::string longDeviceTypes(129, 'a'); nlohmann::json jsonObject = nlohmann::json { { "agentId", "1" }, { "name", "test" }, { "description", "test description" }, { "version", "1.0" }, { "category", "productivity" }, + { "defaultInputModes", nlohmann::json::array({ "text" }) }, + { "defaultOutputModes", nlohmann::json::array({ "text" }) }, + { "skills", nlohmann::json::array({ + nlohmann::json { + { "id", "test" }, + { "name", "test" }, + { "description", "test" }, + { "tags", nlohmann::json::array({ "test" }) } + } + })}, { "appInfo", nlohmann::json { - { "deviceTypes", longDeviceTypes }, + { "deviceTypes", nlohmann::json::array({ "phone", "tablet" }) }, }}, }; AgentCard agentCard; - EXPECT_FALSE(AgentCard::FromJson(jsonObject, agentCard)); + EXPECT_TRUE(AgentCard::FromJson(jsonObject, agentCard)); + EXPECT_EQ(agentCard.appInfo->deviceTypes.size(), 2); } /** @@ -3269,7 +3497,7 @@ HWTEST_F(AgentCardTest, AgentAppInfoFromJson_001, TestSize.Level1) { "bundleName", "test.bundle" }, { "moduleName", "test" }, { "abilityName", "TestAbility" }, - { "deviceTypes", "phone" }, + { "deviceTypes", nlohmann::json::array({ "phone" }) }, { "minAppVersion", "1.0.0" }, }; AgentAppInfo appInfo; @@ -3277,7 +3505,8 @@ HWTEST_F(AgentCardTest, AgentAppInfoFromJson_001, TestSize.Level1) EXPECT_EQ(appInfo.bundleName, "test.bundle"); EXPECT_EQ(appInfo.moduleName, "test"); EXPECT_EQ(appInfo.abilityName, "TestAbility"); - EXPECT_EQ(appInfo.deviceTypes, "phone"); + EXPECT_EQ(appInfo.deviceTypes.size(), 1); + EXPECT_EQ(appInfo.deviceTypes[0], "phone"); EXPECT_EQ(appInfo.minAppVersion, "1.0.0"); } @@ -3301,40 +3530,41 @@ HWTEST_F(AgentCardTest, AgentAppInfoFromJson_002, TestSize.Level1) /** * @tc.name: AgentAppInfoFromJson_003 - * @tc.desc: Test AgentAppInfo FromJson fails when deviceTypes length exceeds 128 + * @tc.desc: Test AgentAppInfo FromJson skips non-string elements in deviceTypes array * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, AgentAppInfoFromJson_003, TestSize.Level1) { - std::string longDeviceTypes(129, 'a'); nlohmann::json jsonObject = nlohmann::json { - { "deviceTypes", longDeviceTypes }, + { "deviceTypes", nlohmann::json::array({ "phone", 123, true, "tablet", nullptr }) }, }; AgentAppInfo appInfo; - EXPECT_FALSE(AgentAppInfo::FromJson(jsonObject, appInfo)); + EXPECT_TRUE(AgentAppInfo::FromJson(jsonObject, appInfo)); + EXPECT_EQ(appInfo.deviceTypes.size(), 2); + EXPECT_EQ(appInfo.deviceTypes[0], "phone"); + EXPECT_EQ(appInfo.deviceTypes[1], "tablet"); } /** * @tc.name: AgentAppInfoFromJson_004 - * @tc.desc: Test AgentAppInfo FromJson succeeds when deviceTypes length is exactly 128 + * @tc.desc: Test AgentAppInfo FromJson succeeds with multiple deviceTypes * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, AgentAppInfoFromJson_004, TestSize.Level1) { - std::string validDeviceTypes(128, 'a'); nlohmann::json jsonObject = nlohmann::json { - { "deviceTypes", validDeviceTypes }, + { "deviceTypes", nlohmann::json::array({ "phone", "tablet", "tv", "watch" }) }, }; AgentAppInfo appInfo; EXPECT_TRUE(AgentAppInfo::FromJson(jsonObject, appInfo)); - EXPECT_EQ(appInfo.deviceTypes.length(), 128); + EXPECT_EQ(appInfo.deviceTypes.size(), 4); } /** * @tc.name: AgentAppInfoFromJson_005 - * @tc.desc: Test AgentAppInfo FromJson fails when minAppVersion length exceeds 32 + * @tc.desc: Test AgentAppInfo FromJson sets minAppVersion to empty when length exceeds 32 * @tc.type: FUNC * @tc.require: AR000H1N32 */ @@ -3345,7 +3575,8 @@ HWTEST_F(AgentCardTest, AgentAppInfoFromJson_005, TestSize.Level1) { "minAppVersion", longMinAppVersion }, }; AgentAppInfo appInfo; - EXPECT_FALSE(AgentAppInfo::FromJson(jsonObject, appInfo)); + EXPECT_TRUE(AgentAppInfo::FromJson(jsonObject, appInfo)); + EXPECT_EQ(appInfo.minAppVersion, ""); } /** @@ -3386,13 +3617,13 @@ HWTEST_F(AgentCardTest, CapabilitiesFromJson_009, TestSize.Level1) /** * @tc.name: CapabilitiesFromJson_010 - * @tc.desc: Test Capabilities FromJson fails when extension length exceeds 1280 + * @tc.desc: Test Capabilities FromJson fails when extension length exceeds 1024 * @tc.type: FUNC * @tc.require: AR000H1N32 */ HWTEST_F(AgentCardTest, CapabilitiesFromJson_010, TestSize.Level1) { - std::string longExtension(1281, 'a'); + std::string longExtension(1025, 'a'); nlohmann::json jsonObject = nlohmann::json { { "streaming", true }, { "extension", longExtension }, @@ -3400,28 +3631,5 @@ HWTEST_F(AgentCardTest, CapabilitiesFromJson_010, TestSize.Level1) AgentCapabilities capabilities = AgentCapabilities::FromJson(jsonObject); EXPECT_TRUE(capabilities.extension.empty()); } - -/** - * @tc.name: AgentAppInfoParcel_001 - * @tc.desc: Test AgentAppInfo Marshalling and Unmarshalling - * @tc.type: FUNC - * @tc.require: AR000H1N32 - */ -HWTEST_F(AgentCardTest, AgentAppInfoParcel_001, TestSize.Level1) -{ - AgentAppInfo originalAppInfo; - originalAppInfo.bundleName = "test.bundle"; - originalAppInfo.moduleName = "test.module"; - originalAppInfo.abilityName = "TestAbility"; - originalAppInfo.deviceTypes = "phone"; - originalAppInfo.minAppVersion = "1.0.0"; - - Parcel parcel; - EXPECT_TRUE(originalAppInfo.Marshalling(parcel)); - - AgentAppInfo *unmarshalledAppInfo = AgentAppInfo::Unmarshalling(parcel); - EXPECT_NE(unmarshalledAppInfo, nullptr); - delete unmarshalledAppInfo; -} } // namespace AgentRuntime } // namespace OHOS \ No newline at end of file