|
17 | 17 | SEARCH_OPTION_KEYS, |
18 | 18 | SearchExpectation, |
19 | 19 | deterministic_checks, |
| 20 | + object_urls, |
20 | 21 | parse_expectation, |
21 | 22 | raw_ids, |
22 | 23 | returned_refs, |
| 24 | + widget_refs, |
23 | 25 | ) |
24 | 26 | from gooddata_eval.core.agentic.search_tool import ( |
25 | 27 | SEARCH_CONDITIONAL_SCORES, |
@@ -59,9 +61,16 @@ def _turn( |
59 | 61 | text: str = "Found 2 results.", |
60 | 62 | calls: list[dict] | None = None, |
61 | 63 | widgets: list[dict] | None = None, |
| 64 | + parts: list[dict] | None = None, |
62 | 65 | ) -> ChatResult: |
63 | 66 | return ChatResult.model_validate( |
64 | | - {"textResponse": text, "toolCallEvents": calls or [], "searchResults": widgets or [], "responseId": "resp"} |
| 67 | + { |
| 68 | + "textResponse": text, |
| 69 | + "toolCallEvents": calls or [], |
| 70 | + "searchResults": widgets or [], |
| 71 | + "unhandledParts": parts or [], |
| 72 | + "responseId": "resp", |
| 73 | + } |
65 | 74 | ) |
66 | 75 |
|
67 | 76 |
|
@@ -335,6 +344,160 @@ def test_widget(expect: bool, widgets: int, ok: bool): |
335 | 344 | assert deterministic_checks(parse_expectation({"expect_widget": expect}), result)[0] == {"search_widget": ok} |
336 | 345 |
|
337 | 346 |
|
| 347 | +_DOC_ID = "9a3c8e7e-2f0b-4c55-a1f1-6b0f6a4f1d20" |
| 348 | +_DOC_SEARCH = _search_call( |
| 349 | + {"keywords": ["Weekly Sales Brief"], "object_types": ["document"], "limit": 10, "user_requested_search": True}, |
| 350 | + [_obj("document", _DOC_ID)], |
| 351 | +) |
| 352 | +_DOC_LOOKUP = _search_call( |
| 353 | + {"keywords": ["Weekly Sales Brief"], "object_types": ["document"], "limit": 10, "user_requested_search": False}, |
| 354 | + [_obj("document", _DOC_ID)], |
| 355 | +) |
| 356 | + |
| 357 | + |
| 358 | +@pytest.mark.parametrize( |
| 359 | + ("calls", "expect", "ok"), |
| 360 | + [ |
| 361 | + ([_DOC_SEARCH], True, True), |
| 362 | + ([_DOC_LOOKUP], True, False), |
| 363 | + ([_DOC_LOOKUP, _DOC_SEARCH], True, True), |
| 364 | + ([], True, False), |
| 365 | + ([_DOC_LOOKUP], False, True), |
| 366 | + ([_DOC_SEARCH], False, False), |
| 367 | + ], |
| 368 | +) |
| 369 | +def test_user_requested_search_reads_every_search_call(calls: list[dict], expect: bool, ok: bool): |
| 370 | + spec = parse_expectation({"expect_user_requested_search": expect}) |
| 371 | + checks, details = deterministic_checks(spec, _turn(calls=calls)) |
| 372 | + assert checks == {"search_user_requested": ok} |
| 373 | + if not ok: |
| 374 | + assert details["search_user_requested"]["expected"] is expect |
| 375 | + |
| 376 | + |
| 377 | +def test_user_requested_search_is_not_read_from_a_string(): |
| 378 | + call = _search_call({"keywords": ["x"], "user_requested_search": "true"}) |
| 379 | + spec = parse_expectation({"expect_user_requested_search": True}) |
| 380 | + assert deterministic_checks(spec, _turn(calls=[call]))[0] == {"search_user_requested": False} |
| 381 | + |
| 382 | + |
| 383 | +def test_widget_objects_are_read_from_the_widget_only(): |
| 384 | + spec = parse_expectation({"widget_objects": [f"document/{_DOC_ID}"]}) |
| 385 | + internal_only = _turn(calls=[_DOC_SEARCH]) |
| 386 | + checks, details = deterministic_checks(spec, internal_only) |
| 387 | + assert checks == {"search_widget_objects": False} |
| 388 | + assert details["search_widget_objects"] == {"missing": [f"document/{_DOC_ID}"], "listed": []} |
| 389 | + listed = _turn(calls=[_DOC_SEARCH], widgets=[_widget(("dashboard", "sales"), ("document", _DOC_ID))]) |
| 390 | + assert deterministic_checks(spec, listed)[0] == {"search_widget_objects": True} |
| 391 | + |
| 392 | + |
| 393 | +def test_every_widget_object_must_be_listed(): |
| 394 | + spec = parse_expectation({"widget_objects": [f"document/{_DOC_ID}", "dashboard/sales"]}) |
| 395 | + result = _turn(widgets=[_widget(("document", _DOC_ID))]) |
| 396 | + checks, details = deterministic_checks(spec, result) |
| 397 | + assert checks == {"search_widget_objects": False} |
| 398 | + assert details["search_widget_objects"]["missing"] == ["dashboard/sales"] |
| 399 | + |
| 400 | + |
| 401 | +def test_a_widget_object_of_another_type_is_not_the_expected_one(): |
| 402 | + spec = parse_expectation({"widget_objects": [f"document/{_DOC_ID}"]}) |
| 403 | + result = _turn(widgets=[_widget(("dashboard", _DOC_ID))]) |
| 404 | + assert deterministic_checks(spec, result)[0] == {"search_widget_objects": False} |
| 405 | + |
| 406 | + |
| 407 | +def test_widget_refs_skip_malformed_objects(): |
| 408 | + widget = { |
| 409 | + "type": "searchResults", |
| 410 | + "objects": [{"id": "a"}, {"type": "document"}, "x", {"id": "b", "type": "document"}], |
| 411 | + } |
| 412 | + assert widget_refs(_turn(widgets=[widget, {"type": "searchResults", "objects": None}])) == ["document/b"] |
| 413 | + |
| 414 | + |
| 415 | +def test_forbidden_parts_fail_on_a_draft(): |
| 416 | + spec = parse_expectation({"forbidden_parts": ["document", "report", "dashboard"]}) |
| 417 | + clarify = {"type": "clarifyingQuestions", "questions": []} |
| 418 | + assert deterministic_checks(spec, _turn(parts=[clarify]))[0] == {"search_forbidden_parts": True} |
| 419 | + draft = {"type": "document", "document": {"title": "Weekly Sales Brief"}} |
| 420 | + checks, details = deterministic_checks(spec, _turn(parts=[clarify, draft])) |
| 421 | + assert checks == {"search_forbidden_parts": False} |
| 422 | + assert details["search_forbidden_parts"] == { |
| 423 | + "forbidden": ["document"], |
| 424 | + "parts": ["clarifyingQuestions", "document"], |
| 425 | + } |
| 426 | + |
| 427 | + |
| 428 | +@pytest.mark.parametrize("value", [["visualization"], ["searchResults"], ["Document"], ["documnet"]]) |
| 429 | +def test_forbidden_parts_take_only_part_types_that_reach_the_unhandled_list(value: list[str]): |
| 430 | + with pytest.raises(ValueError, match="forbidden_parts"): |
| 431 | + parse_expectation({"forbidden_parts": value}) |
| 432 | + |
| 433 | + |
| 434 | +@pytest.mark.parametrize( |
| 435 | + ("text", "found"), |
| 436 | + [ |
| 437 | + ("Open it from the Search list below.", []), |
| 438 | + ("See the help page https://www.gooddata.com/docs/cloud/create-reports/.", []), |
| 439 | + ("Our site is https://example.com.", []), |
| 440 | + ( |
| 441 | + "Open https://acme.gooddata.com/workspace/ecommerce_demo/reports/document/abc here.", |
| 442 | + ["https://acme.gooddata.com/workspace/ecommerce_demo/reports/document/abc"], |
| 443 | + ), |
| 444 | + ( |
| 445 | + "[Sales](https://acme.gooddata.com/dashboards/#/workspace/ws/dashboard/bf43)", |
| 446 | + ["https://acme.gooddata.com/dashboards/#/workspace/ws/dashboard/bf43"], |
| 447 | + ), |
| 448 | + ("[Weekly brief](/workspace/ws/reports/document/abc)", ["/workspace/ws/reports/document/abc"]), |
| 449 | + ( |
| 450 | + "Go to <https://h/api/v1/entities/workspaces/ws/documents/abc>", |
| 451 | + ["https://h/api/v1/entities/workspaces/ws/documents/abc"], |
| 452 | + ), |
| 453 | + ("[Docs](/docs/reports)", []), |
| 454 | + ("Read https://www.gooddata.com/docs/cloud/workspaces/overview/ first.", []), |
| 455 | + ("[Workspaces](/docs/cloud/workspaces/manage)", []), |
| 456 | + ( |
| 457 | + '[Brief](/workspace/ws/reports/document/abc "Weekly brief")', |
| 458 | + ["/workspace/ws/reports/document/abc"], |
| 459 | + ), |
| 460 | + ( |
| 461 | + "https://h/metrics/#/workspace/ws/metric/net_sales", |
| 462 | + ["https://h/metrics/#/workspace/ws/metric/net_sales"], |
| 463 | + ), |
| 464 | + ("https://www.gooddata.com/docs/cloud/reports/overview/", []), |
| 465 | + ("https://h/analyze/#/ecommerce_demo/viz_1/edit", ["https://h/analyze/#/ecommerce_demo/viz_1/edit"]), |
| 466 | + ], |
| 467 | +) |
| 468 | +def test_object_urls_catch_links_to_workspace_objects(text: str, found: list[str]): |
| 469 | + assert object_urls(text) == found |
| 470 | + |
| 471 | + |
| 472 | +def test_no_object_urls_check(): |
| 473 | + spec = parse_expectation({"no_object_urls": True}) |
| 474 | + assert deterministic_checks(spec, _turn(text="Open it from the list."))[0] == {"search_no_object_urls": True} |
| 475 | + link = "https://h/workspace/ws/reports/document/abc" |
| 476 | + checks, details = deterministic_checks(spec, _turn(text=f"Here: {link}")) |
| 477 | + assert checks == {"search_no_object_urls": False} |
| 478 | + assert details["search_no_object_urls"] == {"object_urls": [link]} |
| 479 | + |
| 480 | + |
| 481 | +@pytest.mark.parametrize( |
| 482 | + ("expected_output", "message"), |
| 483 | + [ |
| 484 | + ({"expect_user_requested_search": "yes"}, "expect_user_requested_search"), |
| 485 | + ({"widget_objects": []}, "widget_objects"), |
| 486 | + ({"widget_objects": ["doc/abc"]}, "expected_objects entry"), |
| 487 | + ({"forbidden_parts": "document"}, "forbidden_parts"), |
| 488 | + ({"no_object_urls": 1}, "no_object_urls"), |
| 489 | + ], |
| 490 | +) |
| 491 | +def test_malformed_new_options_are_rejected(expected_output: dict, message: str): |
| 492 | + with pytest.raises(ValueError, match=message): |
| 493 | + parse_expectation(expected_output) |
| 494 | + |
| 495 | + |
| 496 | +def test_search_required_false_with_a_new_check_is_an_item(): |
| 497 | + spec = parse_expectation({"search_required": False, "forbidden_parts": ["dashboard"]}) |
| 498 | + assert spec.has_turn_checks |
| 499 | + |
| 500 | + |
338 | 501 | def test_routing_needs_every_required_tool_and_no_forbidden_one(): |
339 | 502 | describe = {"functionName": "get_object_with_children", "functionArguments": "{}"} |
340 | 503 | result = _turn(calls=[_NET_SALES_CALL, describe]) |
@@ -702,9 +865,15 @@ def test_every_check_is_a_published_score_name(): |
702 | 865 | "answer_rubric": ["Lists net sales"], |
703 | 866 | "expected_limit": 20, |
704 | 867 | "no_raw_ids": True, |
| 868 | + "expect_user_requested_search": False, |
| 869 | + "widget_objects": ["metric/net_sales"], |
| 870 | + "forbidden_parts": ["document"], |
| 871 | + "no_object_urls": True, |
705 | 872 | } |
| 873 | + turn = _turn(calls=[_NET_SALES_CALL], widgets=[_widget(("metric", "net_sales"))]) |
| 874 | + expected["expect_widget"] = True |
706 | 875 | with patch(f"{_MODULE}.score_run", return_value=JudgeVerdict(passed=True, reasoning="ok")): |
707 | | - run, _ = _run(expected, _turn(calls=[_NET_SALES_CALL]), judge=MagicMock()) |
| 876 | + run, _ = _run(expected, turn, judge=MagicMock()) |
708 | 877 | assert set(run.checks) == set(SEARCH_CONDITIONAL_SCORES) |
709 | 878 | assert run.passed |
710 | 879 |
|
@@ -868,6 +1037,10 @@ def test_every_check_of_a_report_only_item_is_a_published_diagnostic_name(): |
868 | 1037 | "answer_rubric": ["Lists net sales"], |
869 | 1038 | "expected_limit": 20, |
870 | 1039 | "no_raw_ids": True, |
| 1040 | + "expect_user_requested_search": True, |
| 1041 | + "widget_objects": ["metric/net_sales"], |
| 1042 | + "forbidden_parts": ["document"], |
| 1043 | + "no_object_urls": True, |
871 | 1044 | "report_only": True, |
872 | 1045 | } |
873 | 1046 | ctx = _scored(expected, JudgeVerdict(passed=True, reasoning="ok")) |
|
0 commit comments