Skip to main content

headless_lms_server/controllers/
mock_document_storage.rs

1use headless_lms_chatbot::citations::CourseMaterialDocument;
2
3use crate::prelude::*;
4
5const DOCUMENT_3_CHUNK: &str = r#"More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long. More content on the same mock course page. Another snippet. Long."#;
6
7/// One document the mock storage serves.
8pub(crate) struct MockDocument {
9    /// The last path segment of the url the chatbot fetches the document from.
10    pub(crate) id: &'static str,
11    pub(crate) chunk_id: &'static str,
12    pub(crate) title: &'static str,
13    pub(crate) chunk: &'static str,
14}
15
16/// The whole course material the mock chatbot cites. The mock Azure API builds its search results
17/// from these, so every document it points the chatbot at is one this endpoint serves.
18pub(crate) static MOCK_DOCUMENTS: [MockDocument; 3] = [
19    MockDocument {
20        id: "document1",
21        chunk_id: "1",
22        title: "Cited course page",
23        chunk: "Mock test page content\n This is test content blah",
24    },
25    MockDocument {
26        id: "document2",
27        chunk_id: "2",
28        title: "Cited course page 2",
29        chunk: "Mock test page content 2\n This is another test page.",
30    },
31    MockDocument {
32        id: "document3",
33        chunk_id: "3",
34        title: "Cited course page",
35        chunk: DOCUMENT_3_CHUNK,
36    },
37];
38
39// GET /api/v0/mock_document_storage/test/documents/{document_id}
40async fn mock_document_storage(
41    app_conf: web::Data<ApplicationConfiguration>,
42    document_id: web::Path<String>,
43) -> ControllerResult<String> {
44    assert!(app_conf.test_chatbot && app_conf.test_mode);
45    trace!("In mock document storage");
46
47    let base_url = app_conf.base_url.to_owned();
48
49    let res = match MOCK_DOCUMENTS
50        .iter()
51        .find(|document| document.id == document_id.as_str())
52    {
53        Some(document) => serde_json::to_string(&CourseMaterialDocument {
54            chunk_id: document.chunk_id.to_string(),
55            title: document.title.to_string(),
56            url: format!("{base_url}/{}", document.chunk_id),
57            filepath: document.id.to_string(),
58            chunk: document.chunk.to_string(),
59        })?,
60        None => "{}".to_string(),
61    };
62
63    let token = skip_authorize();
64    token.authorized_ok(res)
65}
66
67pub fn _add_routes(cfg: &mut ServiceConfig) {
68    cfg.route(
69        "/test/documents/{document_id}",
70        web::get().to(mock_document_storage),
71    );
72}