1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "insight_intent/extract_insight_intent_profile.cpp"
19 #include "extract_insight_intent_profile.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace AbilityRuntime {
26 namespace {
27 const std::string errProfileJsonStr = "{"
28 "\"extractInsightIntents\": ["
29 "{"
30 "\"displayDescription\": \"game\","
31 "\"schema\": \"GameList\","
32 "\"keywords\": [\"123234345\",\"12345654321\"],"
33 "\"intentName\": \"123\","
34 "\"displayName\": \"Home\","
35 "\"decoratorClass\": \"base\","
36 "\"icon\": \"$r('app.media.startIcon')\","
37 "\"moduleName\": \"entry\","
38 "\"decoratorFile\": \"@normalized:N&&&entry/src/main/ets/pages/Index&\","
39 "\"uri\": \"/data/app/base\","
40 "\"example\": \"exampleAAA\","
41 "\"paramMappings\": ["
42 "{"
43 "\"paramCategory\": \"dddd\","
44 "\"paramMappingName\": \"ccc\","
45 "\"paramName\": \"aaa\""
46 "}"
47 "],"
48 "\"decoratorType\": \"@InsightIntentLinkErr\","
49 "\"llmDescription\": \"123111321\","
50 "\"domain\": \"game\","
51 "\"intentVersion\": \"1.0.2\","
52 "\"bundleName\": \"com.example.instent\","
53 "\"parameters\": {"
54 "\"type\": \"object\","
55 "\"items\": {"
56 "\"type\": \"array\","
57 "\"items\": {"
58 "\"propertyNames\": {"
59 "\"enum\": [\"entityId\",\"entityGroupId\",\"gameType\"]"
60 "},"
61 "\"type\": \"object\","
62 "\"required\": [\"entityId\"],"
63 "\"properties\": {"
64 "\"gameType\": {"
65 "\"description\": \"游戏类型\","
66 "\"type\": \"string\","
67 "\"enum\": [\"3D\",\"2D\",\"RPG\"]"
68 "},"
69 "\"entityId\": {"
70 "\"description\": \"游戏唯一实体 id\","
71 "\"type\": \"string\""
72 "},"
73 "\"entityGroupId\": {"
74 "\"description\": \"用于确定游戏的更新形式(每日游戏)\","
75 "\"type\": \"string\""
76 "}"
77 "}"
78 "}"
79 "}"
80 "}"
81 "},"
82 "{"
83 "\"displayDescription\": \"music\","
84 "\"schema\": \"ControlPlayback\","
85 "\"keywords\": [\"ControlPlayback\"],"
86 "\"intentName\": \"123\","
87 "\"displayName\": \"Home\","
88 "\"decoratorClass\": \"base\","
89 "\"icon\": \"$r('app.media.startIcon')\","
90 "\"moduleName\": \"entry\","
91 "\"decoratorFile\": \"@normalized:N&&&entry/src/main/ets/pages/Index&\","
92 "\"example\": \"exampleBBB\","
93 "\"uri\": \"/data/app/base\","
94 "\"paramMappings\": ["
95 "{"
96 "\"paramCategory\": \"dddd\","
97 "\"paramMappingName\": \"ccc\","
98 "\"paramName\": \"aaa\""
99 "}"
100 "],"
101 "\"decoratorType\": \"@InsightIntentLinkErr2\","
102 "\"llmDescription\": \"播放音乐控制\","
103 "\"domain\": \"control\","
104 "\"intentVersion\": \"1.0.1\","
105 "\"bundleName\": \"com.example.instent\","
106 "\"parameters\": {"
107 "\"oneOf\": ["
108 "{"
109 "\"required\": [\"playbackSpeed\"]"
110 "},"
111 "{"
112 "\"required\": [\"playbackProgress\"]"
113 "}"
114 "],"
115 "\"propertyNames\": {"
116 "\"enum\": [\"playbackSpeed\",\"playbackProgress\"]"
117 "},"
118 "\"type\": \"object\","
119 "\"properties\": {"
120 "\"playbackSpeed\": {"
121 "\"description\": \"播放倍速\","
122 "\"type\": \"number\","
123 "\"enum\": [0.5,0.75,1,1.25,1.5,2]"
124 "},"
125 "\"playbackProgress\": {"
126 "\"description\": \"播放进度,单位秒\","
127 "\"type\": \"number\""
128 "}"
129 "}"
130 "}"
131 "}"
132 "]"
133 "}";
134
135 const std::string profileJsonStr = "{"
136 "\"extractInsightIntents\": ["
137 "{"
138 "\"displayDescription\": \"game\","
139 "\"schema\": \"GameList\","
140 "\"keywords\": [\"123234345\",\"12345654321\"],"
141 "\"intentName\": \"123\","
142 "\"displayName\": \"Home\","
143 "\"decoratorClass\": \"base\","
144 "\"icon\": \"$r('app.media.startIcon')\","
145 "\"moduleName\": \"entry\","
146 "\"decoratorFile\": \"@normalized:N&&&entry/src/main/ets/pages/Index&\","
147 "\"uri\": \"/data/app/base\","
148 "\"example\": \"exampleAAA\","
149 "\"paramMappings\": ["
150 "{"
151 "\"paramCategory\": \"dddd\","
152 "\"paramMappingName\": \"ccc\","
153 "\"paramName\": \"aaa\""
154 "}"
155 "],"
156 "\"decoratorType\": \"@InsightIntentLink\","
157 "\"llmDescription\": \"123111321\","
158 "\"domain\": \"game\","
159 "\"intentVersion\": \"1.0.2\","
160 "\"bundleName\": \"com.example.instent\","
161 "\"parameters\": {"
162 "\"type\": \"object\","
163 "\"items\": {"
164 "\"type\": \"array\","
165 "\"items\": {"
166 "\"propertyNames\": {"
167 "\"enum\": [\"entityId\",\"entityGroupId\",\"gameType\"]"
168 "},"
169 "\"type\": \"object\","
170 "\"required\": [\"entityId\"],"
171 "\"properties\": {"
172 "\"gameType\": {"
173 "\"description\": \"游戏类型\","
174 "\"type\": \"string\","
175 "\"enum\": [\"3D\",\"2D\",\"RPG\"]"
176 "},"
177 "\"entityId\": {"
178 "\"description\": \"游戏唯一实体 id\","
179 "\"type\": \"string\""
180 "},"
181 "\"entityGroupId\": {"
182 "\"description\": \"用于确定游戏的更新形式(每日游戏)\","
183 "\"type\": \"string\""
184 "}"
185 "}"
186 "}"
187 "}"
188 "},"
189 "\"result\": {"
190 "\"type\": \"object\","
191 "\"items\": {"
192 "\"type\": \"array\","
193 "\"items\": {"
194 "\"propertyNames\": {"
195 "\"enum\": [\"entityId\",\"entityGroupId\",\"gameType\"]"
196 "},"
197 "\"type\": \"object\","
198 "\"required\": [\"entityId\"],"
199 "\"properties\": {"
200 "\"gameType\": {"
201 "\"description\": \"游戏类型\","
202 "\"type\": \"string\","
203 "\"enum\": [\"3D\",\"2D\",\"RPG\"]"
204 "},"
205 "\"entityId\": {"
206 "\"description\": \"游戏唯一实体 id\","
207 "\"type\": \"string\""
208 "},"
209 "\"entityGroupId\": {"
210 "\"description\": \"用于确定游戏的更新形式(每日游戏)\","
211 "\"type\": \"string\""
212 "}"
213 "}"
214 "}"
215 "}"
216 "},"
217 "\"entities\": ["
218 "{"
219 "\"decoratorFile\": \"@normalized:N&&&entry/src/main/ets/pages/Index&\","
220 "\"className\": \"SongPlayState\","
221 "\"decoratorType\": \"@IntentEntityDecorator\","
222 "\"entityId\": \"11\","
223 "\"entityCategory\": \"entity Category\","
224 "\"parentClassName\": \"base\","
225 "\"parameters\": {"
226 "\"type\": \"object\","
227 "\"items\": {"
228 "\"type\": \"array\","
229 "\"items\": {"
230 "\"propertyNames\": {"
231 "\"enum\": [\"entityId\",\"entityGroupId\",\"gameType\"]"
232 "},"
233 "\"type\": \"object\","
234 "\"required\": [\"entityId\"]"
235 "}"
236 "}"
237 "}"
238 "},"
239 "{"
240 "\"decoratorFile\": \"@normalized:N&&&entry/src/main/ets/pages/Index&\","
241 "\"className\": \"base\","
242 "\"decoratorType\": \"@IntentEntityDecorator\","
243 "\"entityId\": \"12\","
244 "\"entityCategory\": \"entity1 Category\","
245 "\"parameters\": {"
246 "\"type\": \"object\","
247 "\"items\": {"
248 "\"type\": \"array\","
249 "\"items\": {"
250 "\"propertyNames\": {"
251 "\"enum\": [\"entityId\",\"entityGroupId\",\"gameType\"]"
252 "},"
253 "\"type\": \"object\","
254 "\"required\": [\"entityId\"]"
255 "}"
256 "}"
257 "}"
258 "}"
259 "]"
260 "},"
261 "{"
262 "\"displayDescription\": \"music\","
263 "\"schema\": \"ControlPlayback\","
264 "\"keywords\": [\"ControlPlayback\"],"
265 "\"intentName\": \"InsightIntent2\","
266 "\"displayName\": \"Home\","
267 "\"decoratorClass\": \"base\","
268 "\"icon\": \"$r('app.media.startIcon')\","
269 "\"moduleName\": \"entry\","
270 "\"decoratorFile\": \"@normalized:N&&&entry/src/main/ets/pages/Index&\","
271 "\"uri\": \"/data/app/base\","
272 "\"example\": \"exampleBBB\","
273 "\"paramMappings\": ["
274 "{"
275 "\"paramCategory\": \"dddd\","
276 "\"paramMappingName\": \"ccc\","
277 "\"paramName\": \"aaa\""
278 "}"
279 "],"
280 "\"decoratorType\": \"@InsightIntentLink\","
281 "\"llmDescription\": \"播放音乐控制\","
282 "\"domain\": \"control\","
283 "\"intentVersion\": \"1.0.1\","
284 "\"bundleName\": \"com.example.instent\","
285 "\"parameters\": {"
286 "\"oneOf\": ["
287 "{"
288 "\"required\": [\"playbackSpeed\"]"
289 "},"
290 "{"
291 "\"required\": [\"playbackProgress\"]"
292 "}"
293 "],"
294 "\"propertyNames\": {"
295 "\"enum\": [\"playbackSpeed\",\"playbackProgress\"]"
296 "},"
297 "\"type\": \"object\","
298 "\"properties\": {"
299 "\"playbackSpeed\": {"
300 "\"description\": \"播放倍速\","
301 "\"type\": \"number\","
302 "\"enum\": [0.5,0.75,1,1.25,1.5,2]"
303 "},"
304 "\"playbackProgress\": {"
305 "\"description\": \"播放进度,单位秒\","
306 "\"type\": \"number\""
307 "}"
308 "}"
309 "},"
310 "\"result\": {"
311 "\"oneOf\": ["
312 "{"
313 "\"required\": [\"playbackSpeed\"]"
314 "},"
315 "{"
316 "\"required\": [\"playbackProgress\"]"
317 "}"
318 "],"
319 "\"propertyNames\": {"
320 "\"enum\": [\"playbackSpeed\",\"playbackProgress\"]"
321 "},"
322 "\"type\": \"object\","
323 "\"properties\": {"
324 "\"playbackSpeed\": {"
325 "\"description\": \"播放倍速\","
326 "\"type\": \"number\","
327 "\"enum\": [0.5,0.75,1,1.25,1.5,2]"
328 "},"
329 "\"playbackProgress\": {"
330 "\"description\": \"播放进度,单位秒\","
331 "\"type\": \"number\""
332 "}"
333 "}"
334 "}"
335 "}"
336 "]"
337 "}";
338 }
339
340 class ExtractInsightIntentProfileTest : public testing::Test {
341 public:
342 static void SetUpTestCase();
343 static void TearDownTestCase();
344 void SetUp() override;
345 void TearDown() override;
346 };
347
SetUpTestCase()348 void ExtractInsightIntentProfileTest::SetUpTestCase()
349 {}
350
TearDownTestCase()351 void ExtractInsightIntentProfileTest::TearDownTestCase()
352 {}
353
SetUp()354 void ExtractInsightIntentProfileTest::SetUp()
355 {}
356
TearDown()357 void ExtractInsightIntentProfileTest::TearDown()
358 {}
359
360 /**
361 * @tc.number: TransformTo_0100
362 * @tc.name: TransformTo
363 * @tc.desc: Test TransformTo invalid param profileStr.
364 */
365 HWTEST_F(ExtractInsightIntentProfileTest, TransformTo_0100, TestSize.Level0)
366 {
367 ExtractInsightIntentProfileInfoVec infos;
368 bool result = ExtractInsightIntentProfile::TransformTo(errProfileJsonStr, infos);
369 EXPECT_EQ(result, false);
370 }
371
372 /**
373 * @tc.number: TransformTo_0200
374 * @tc.name: TransformTo, ToJson, ProfileInfoFormat
375 * @tc.desc: Test TransformTo profileStr success.
376 */
377 HWTEST_F(ExtractInsightIntentProfileTest, TransformTo_0200, TestSize.Level0)
378 {
379 TAG_LOGI(AAFwkTag::TEST, "TransformTo_0200 called. start");
380 TAG_LOGI(AAFwkTag::TEST, "profileJsonStr: %{public}s", profileJsonStr.c_str());
381 ExtractInsightIntentProfileInfoVec profileInfos;
382 bool result = ExtractInsightIntentProfile::TransformTo(profileJsonStr, profileInfos);
383 EXPECT_EQ(result, true);
384 EXPECT_EQ(profileInfos.insightIntents.size(), 2);
385 EXPECT_EQ(profileInfos.insightIntents[0].decoratorType, "@InsightIntentLink");
386 EXPECT_EQ(profileInfos.insightIntents[0].intentName, "123");
387 EXPECT_EQ(profileInfos.insightIntents[0].example, "exampleAAA");
388 EXPECT_NE(profileInfos.insightIntents[0].result, "");
389 EXPECT_EQ(profileInfos.insightIntents[1].decoratorType, "@InsightIntentLink");
390 EXPECT_EQ(profileInfos.insightIntents[1].intentName, "InsightIntent2");
391 EXPECT_EQ(profileInfos.insightIntents[1].example, "exampleBBB");
392 EXPECT_NE(profileInfos.insightIntents[1].result, "");
393
394 EXPECT_EQ(profileInfos.insightIntents[0].entities.size(), 2);
395 EXPECT_EQ(profileInfos.insightIntents[0].entities[0].className, "SongPlayState");
396 EXPECT_EQ(profileInfos.insightIntents[0].entities[0].decoratorType, "@IntentEntityDecorator");
397 EXPECT_EQ(profileInfos.insightIntents[0].entities[0].entityId, "11");
398 EXPECT_EQ(profileInfos.insightIntents[0].entities[0].parentClassName, "base");
399 EXPECT_NE(profileInfos.insightIntents[0].entities[0].parameters, "");
400 EXPECT_EQ(profileInfos.insightIntents[0].entities[1].className, "base");
401 EXPECT_EQ(profileInfos.insightIntents[0].entities[1].decoratorType, "@IntentEntityDecorator");
402 EXPECT_EQ(profileInfos.insightIntents[0].entities[1].entityId, "12");
403 EXPECT_EQ(profileInfos.insightIntents[0].entities[1].parentClassName, "");
404 EXPECT_NE(profileInfos.insightIntents[0].entities[1].parameters, "");
405 EXPECT_EQ(profileInfos.insightIntents[1].entities.size(), 0);
406
407 nlohmann::json jsonObject1;
408 result = ExtractInsightIntentProfile::ToJson(profileInfos.insightIntents[0], jsonObject1);
409 EXPECT_EQ(result, true);
410 ExtractInsightIntentProfileInfoVec profileInfos1;
411 result = ExtractInsightIntentProfile::TransformTo(jsonObject1.dump(), profileInfos1);
412 EXPECT_EQ(result, true);
413 TAG_LOGI(AAFwkTag::TEST, "jsonObject1 dump: %{public}s", jsonObject1.dump().c_str());
414 EXPECT_EQ(profileInfos1.insightIntents.size(), 1);
415 EXPECT_EQ(profileInfos1.insightIntents[0].decoratorType, "@InsightIntentLink");
416 EXPECT_EQ(profileInfos1.insightIntents[0].intentName, "123");
417 EXPECT_EQ(profileInfos1.insightIntents[0].example, "exampleAAA");
418 EXPECT_EQ(profileInfos1.insightIntents[0].entities.size(), 2);
419 EXPECT_EQ(profileInfos1.insightIntents[0].entities[0].className, "SongPlayState");
420 EXPECT_EQ(profileInfos1.insightIntents[0].entities[0].decoratorType, "@IntentEntityDecorator");
421 EXPECT_EQ(profileInfos1.insightIntents[0].entities[0].entityId, "11");
422 EXPECT_EQ(profileInfos1.insightIntents[0].entities[0].parentClassName, "base");
423 EXPECT_NE(profileInfos1.insightIntents[0].entities[0].parameters, "");
424 EXPECT_EQ(profileInfos1.insightIntents[0].entities[1].className, "base");
425 EXPECT_EQ(profileInfos1.insightIntents[0].entities[1].decoratorType, "@IntentEntityDecorator");
426 EXPECT_EQ(profileInfos1.insightIntents[0].entities[1].entityId, "12");
427 EXPECT_EQ(profileInfos1.insightIntents[0].entities[1].parentClassName, "");
428 EXPECT_NE(profileInfos1.insightIntents[0].entities[1].parameters, "");
429
430 nlohmann::json jsonObject2;
431 result = ExtractInsightIntentProfile::ToJson(profileInfos.insightIntents[1], jsonObject2);
432 EXPECT_EQ(result, true);
433 ExtractInsightIntentProfileInfoVec profileInfos2;
434 result = ExtractInsightIntentProfile::TransformTo(jsonObject2.dump(), profileInfos2);
435 EXPECT_EQ(result, true);
436 TAG_LOGI(AAFwkTag::TEST, "jsonObject2 dump: %{public}s", jsonObject2.dump().c_str());
437 EXPECT_EQ(profileInfos2.insightIntents.size(), 1);
438 EXPECT_EQ(profileInfos2.insightIntents[0].decoratorType, "@InsightIntentLink");
439 EXPECT_EQ(profileInfos2.insightIntents[0].intentName, "InsightIntent2");
440 EXPECT_EQ(profileInfos2.insightIntents[0].example, "exampleBBB");
441 EXPECT_EQ(profileInfos2.insightIntents[0].entities.size(), 0);
442
443 ExtractInsightIntentInfo info1;
444 result = ExtractInsightIntentProfile::ProfileInfoFormat(profileInfos1.insightIntents[0], info1);
445 EXPECT_EQ(result, true);
446 EXPECT_EQ(info1.domain, "game");
447 EXPECT_NE(info1.result, "");
448 EXPECT_EQ(info1.entities.size(), 2);
449 EXPECT_EQ(info1.entities[0].className, "SongPlayState");
450 EXPECT_EQ(info1.entities[0].decoratorType, "@IntentEntityDecorator");
451 EXPECT_EQ(info1.entities[0].entityId, "11");
452 EXPECT_EQ(info1.entities[0].parentClassName, "base");
453 EXPECT_NE(info1.entities[0].parameters, "");
454 EXPECT_EQ(info1.entities[1].className, "base");
455 EXPECT_EQ(info1.entities[1].decoratorType, "@IntentEntityDecorator");
456 EXPECT_EQ(info1.entities[1].entityId, "12");
457 EXPECT_EQ(info1.entities[1].parentClassName, "");
458 EXPECT_NE(info1.entities[1].parameters, "");
459 EXPECT_EQ(info1.genericInfo.decoratorType, "@InsightIntentLink");
460 InsightIntentLinkInfo linkInfo1 = info1.genericInfo.get<InsightIntentLinkInfo>();
461 EXPECT_EQ(linkInfo1.uri, "/data/app/base");
462 EXPECT_EQ(linkInfo1.paramMapping.size(), 1);
463
464 ExtractInsightIntentInfo info2;
465 result = ExtractInsightIntentProfile::ProfileInfoFormat(profileInfos2.insightIntents[0], info2);
466 EXPECT_EQ(result, true);
467 EXPECT_EQ(info2.domain, "control");
468 EXPECT_NE(info2.result, "");
469 EXPECT_EQ(info2.entities.size(), 0);
470 EXPECT_EQ(info2.genericInfo.decoratorType, "@InsightIntentLink");
471 InsightIntentLinkInfo linkInfo2 = info2.genericInfo.get<InsightIntentLinkInfo>();
472 EXPECT_EQ(linkInfo2.uri, "/data/app/base");
473 EXPECT_EQ(linkInfo2.paramMapping.size(), 1);
474 TAG_LOGI(AAFwkTag::TEST, "TransformTo_0200 called. end");
475 }
476 } // namespace AbilityRuntime
477 } // namespace OHOS
478