2021-08-17 10:27:16 +00:00
|
|
|
# Standard Modules
|
2022-07-19 23:01:56 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from PIL import Image
|
2022-09-12 13:42:41 +00:00
|
|
|
from urllib.parse import quote
|
|
|
|
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
# External Packages
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
# Internal Packages
|
2023-02-14 20:50:51 +00:00
|
|
|
from khoj.main import app
|
2023-03-01 06:35:21 +00:00
|
|
|
from khoj.configure import configure_routes, configure_search_types
|
|
|
|
from khoj.utils import state
|
2023-07-14 08:19:38 +00:00
|
|
|
from khoj.utils.config import SearchModels
|
|
|
|
from khoj.utils.state import search_models, content_index, config
|
2023-02-14 20:50:51 +00:00
|
|
|
from khoj.search_type import text_search, image_search
|
|
|
|
from khoj.utils.rawconfig import ContentConfig, SearchConfig
|
|
|
|
from khoj.processor.org_mode.org_to_jsonl import OrgToJsonl
|
|
|
|
from khoj.search_filter.word_filter import WordFilter
|
|
|
|
from khoj.search_filter.file_filter import FileFilter
|
2021-08-17 10:27:16 +00:00
|
|
|
|
|
|
|
|
2021-08-22 02:21:38 +00:00
|
|
|
# Test
|
2021-09-30 02:02:55 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_search_with_invalid_content_type(client):
|
2021-09-30 02:02:55 +00:00
|
|
|
# Arrange
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote("How to call Khoj from Emacs?")
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&t=invalid_content_type")
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_search_with_valid_content_type(client):
|
2023-07-19 02:34:09 +00:00
|
|
|
for content_type in ["all", "org", "markdown", "image", "pdf", "github", "notion", "plugin1"]:
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/search?q=random&t={content_type}")
|
2021-09-30 02:02:55 +00:00
|
|
|
# Assert
|
2023-07-19 02:34:09 +00:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
|
2022-06-29 19:09:48 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_update_with_invalid_content_type(client):
|
2022-06-29 19:09:48 +00:00
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/update?t=invalid_content_type")
|
2022-06-29 19:09:48 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_update_with_valid_content_type(client):
|
2023-07-29 01:47:56 +00:00
|
|
|
for content_type in ["all", "org", "markdown", "image", "pdf", "notion", "plugin1"]:
|
2022-06-29 19:09:48 +00:00
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/update?t={content_type}")
|
2022-06-29 19:09:48 +00:00
|
|
|
# Assert
|
2023-07-19 02:34:09 +00:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
|
2022-06-29 19:09:48 +00:00
|
|
|
|
|
|
|
|
2023-07-29 01:47:56 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_update_with_github_fails_without_pat(client):
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/update?t=github")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 500, f"Returned status: {response.status_code} for content type: github"
|
|
|
|
assert (
|
|
|
|
response.json()["detail"]
|
|
|
|
== "🚨 Failed to update server via API: Github PAT token is not set. Skipping github content"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_regenerate_with_invalid_content_type(client):
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/update?force=true&t=invalid_content_type")
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
|
|
|
2021-09-30 03:47:58 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_regenerate_with_valid_content_type(client):
|
2023-07-29 01:47:56 +00:00
|
|
|
for content_type in ["all", "org", "markdown", "image", "pdf", "notion", "plugin1"]:
|
2021-09-30 02:02:55 +00:00
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/update?force=true&t={content_type}")
|
2021-09-30 02:02:55 +00:00
|
|
|
# Assert
|
2023-07-19 02:34:09 +00:00
|
|
|
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
|
2021-09-30 02:02:55 +00:00
|
|
|
|
|
|
|
|
2023-07-29 01:47:56 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_regenerate_with_github_fails_without_pat(client):
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/update?force=true&t=github")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 500, f"Returned status: {response.status_code} for content type: github"
|
|
|
|
assert (
|
|
|
|
response.json()["detail"]
|
|
|
|
== "🚨 Failed to update server via API: Github PAT token is not set. Skipping github content"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-01 04:08:26 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_get_configured_types_via_api(client):
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/config/types")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-06-29 03:10:57 +00:00
|
|
|
assert response.json() == ["all", "org", "image", "plugin1"]
|
2023-03-01 04:08:26 +00:00
|
|
|
|
|
|
|
|
2023-03-01 06:35:21 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_get_configured_types_with_only_plugin_content_config(content_config):
|
|
|
|
# Arrange
|
|
|
|
config.content_type = ContentConfig()
|
|
|
|
config.content_type.plugins = content_config.plugins
|
|
|
|
state.SearchType = configure_search_types(config)
|
|
|
|
|
|
|
|
configure_routes(app)
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/config/types")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-06-29 03:10:57 +00:00
|
|
|
assert response.json() == ["all", "plugin1"]
|
2023-03-01 06:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_get_configured_types_with_no_plugin_content_config(content_config):
|
|
|
|
# Arrange
|
|
|
|
config.content_type = content_config
|
|
|
|
config.content_type.plugins = None
|
|
|
|
state.SearchType = configure_search_types(config)
|
|
|
|
|
|
|
|
configure_routes(app)
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/config/types")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert "plugin1" not in response.json()
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
def test_get_configured_types_with_no_content_config():
|
|
|
|
# Arrange
|
|
|
|
config.content_type = ContentConfig()
|
|
|
|
state.SearchType = configure_search_types(config)
|
|
|
|
|
|
|
|
configure_routes(app)
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
response = client.get(f"/api/config/types")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2023-06-29 03:10:57 +00:00
|
|
|
assert response.json() == ["all"]
|
2023-03-01 06:35:21 +00:00
|
|
|
|
|
|
|
|
2021-10-03 03:28:33 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_image_search(client, content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 03:28:33 +00:00
|
|
|
# Arrange
|
2023-07-14 08:19:38 +00:00
|
|
|
search_models.image_search = image_search.initialize_model(search_config.image)
|
|
|
|
content_index.image = image_search.setup(
|
|
|
|
content_config.image, search_models.image_search.image_encoder, regenerate=False
|
|
|
|
)
|
2023-02-17 16:04:26 +00:00
|
|
|
query_expected_image_pairs = [
|
|
|
|
("kitten", "kitten_park.jpg"),
|
|
|
|
("a horse and dog on a leash", "horse_dog.jpg"),
|
|
|
|
("A guinea pig eating grass", "guineapig_grass.jpg"),
|
|
|
|
]
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
for query, expected_image_name in query_expected_image_pairs:
|
2022-07-19 23:01:56 +00:00
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/search?q={query}&n=1&t=image")
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-07-19 23:01:56 +00:00
|
|
|
actual_image = Image.open(BytesIO(client.get(response.json()[0]["entry"]).content))
|
|
|
|
expected_image = Image.open(content_config.image.input_directories[0].joinpath(expected_image_name))
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|
|
|
|
|
|
|
|
|
2021-08-17 10:27:16 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_notes_search(client, content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 03:24:27 +00:00
|
|
|
# Arrange
|
2023-07-14 08:19:38 +00:00
|
|
|
search_models.text_search = text_search.initialize_model(search_config.asymmetric)
|
|
|
|
content_index.org = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=False
|
|
|
|
)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote("How to git install application?")
|
2021-09-30 03:24:27 +00:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org&r=true")
|
2021-09-30 03:24:27 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-07-19 14:26:16 +00:00
|
|
|
# assert actual_data contains "Khoj via Emacs" entry
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 11:13:40 +00:00
|
|
|
assert "git clone" in search_result
|
|
|
|
|
|
|
|
|
2022-09-12 13:42:41 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_notes_search_with_only_filters(client, content_config: ContentConfig, search_config: SearchConfig):
|
2022-09-12 13:42:41 +00:00
|
|
|
# Arrange
|
|
|
|
filters = [WordFilter(), FileFilter()]
|
2023-07-14 08:19:38 +00:00
|
|
|
search_models.text_search = text_search.initialize_model(search_config.asymmetric)
|
|
|
|
content_index.org = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=False, filters=filters
|
2023-02-17 16:04:26 +00:00
|
|
|
)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote('+"Emacs" file:"*.org"')
|
|
|
|
|
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
2022-09-12 13:42:41 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
|
|
|
# assert actual_data contains word "Emacs"
|
|
|
|
search_result = response.json()[0]["entry"]
|
|
|
|
assert "Emacs" in search_result
|
|
|
|
|
|
|
|
|
2021-09-30 11:13:40 +00:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_notes_search_with_include_filter(client, content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 11:13:40 +00:00
|
|
|
# Arrange
|
2022-09-06 23:43:58 +00:00
|
|
|
filters = [WordFilter()]
|
2023-07-14 08:19:38 +00:00
|
|
|
search_models.text_search = text_search.initialize_model(search_config.asymmetric)
|
|
|
|
content_index.org = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search, regenerate=False, filters=filters
|
2023-02-17 16:04:26 +00:00
|
|
|
)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote('How to git install application? +"Emacs"')
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-09-04 14:18:47 +00:00
|
|
|
# assert actual_data contains word "Emacs"
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 11:13:40 +00:00
|
|
|
assert "Emacs" in search_result
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-03-01 01:27:12 +00:00
|
|
|
def test_notes_search_with_exclude_filter(client, content_config: ContentConfig, search_config: SearchConfig):
|
2021-09-30 11:13:40 +00:00
|
|
|
# Arrange
|
2022-09-06 23:43:58 +00:00
|
|
|
filters = [WordFilter()]
|
2023-07-14 08:19:38 +00:00
|
|
|
search_models.text_search = text_search.initialize_model(search_config.asymmetric)
|
|
|
|
content_index.org = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=False, filters=filters
|
2023-02-17 16:04:26 +00:00
|
|
|
)
|
2022-09-12 13:42:41 +00:00
|
|
|
user_query = quote('How to git install application? -"clone"')
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Act
|
2022-10-08 10:16:08 +00:00
|
|
|
response = client.get(f"/api/search?q={user_query}&n=1&t=org")
|
2021-09-30 11:13:40 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert response.status_code == 200
|
2022-09-04 14:18:47 +00:00
|
|
|
# assert actual_data does not contains word "Emacs"
|
2022-07-20 16:33:27 +00:00
|
|
|
search_result = response.json()[0]["entry"]
|
2021-09-30 11:13:40 +00:00
|
|
|
assert "clone" not in search_result
|