mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-20 16:44:12 +01:00
fix: scratchpad tz aware datetime
This commit is contained in:
@@ -459,4 +459,41 @@ mod tests {
|
||||
let retrieved: Option<Scratchpad> = db.get_item(&scratchpad_id).await.unwrap();
|
||||
assert!(retrieved.is_some());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_timezone_aware_scratchpad_conversion() {
|
||||
let db = SurrealDbClient::memory("test_ns", &Uuid::new_v4().to_string())
|
||||
.await
|
||||
.expect("Failed to create test database");
|
||||
|
||||
db.apply_migrations()
|
||||
.await
|
||||
.expect("Failed to apply migrations");
|
||||
|
||||
let user_id = "test_user_123";
|
||||
let scratchpad = Scratchpad::new(user_id.to_string(), "Test Timezone Scratchpad".to_string());
|
||||
let scratchpad_id = scratchpad.id.clone();
|
||||
|
||||
db.store_item(scratchpad).await.unwrap();
|
||||
|
||||
let retrieved = Scratchpad::get_by_id(&scratchpad_id, user_id, &db).await.unwrap();
|
||||
|
||||
// Test that datetime fields are preserved and can be used for timezone formatting
|
||||
assert!(retrieved.created_at.timestamp() > 0);
|
||||
assert!(retrieved.updated_at.timestamp() > 0);
|
||||
assert!(retrieved.last_saved_at.timestamp() > 0);
|
||||
|
||||
// Test that optional datetime fields work correctly
|
||||
assert!(retrieved.archived_at.is_none());
|
||||
assert!(retrieved.ingested_at.is_none());
|
||||
|
||||
// Archive the scratchpad to test optional datetime handling
|
||||
let archived = Scratchpad::archive(&scratchpad_id, user_id, &db, false)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(archived.archived_at.is_some());
|
||||
assert!(archived.archived_at.unwrap().timestamp() > 0);
|
||||
assert!(archived.ingested_at.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -5,6 +5,7 @@ use axum::{
|
||||
Form,
|
||||
};
|
||||
use axum_htmx::{HxBoosted, HxRequest, HX_TRIGGER};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::html_state::HtmlState;
|
||||
@@ -32,7 +33,7 @@ pub struct ScratchpadListItem {
|
||||
id: String,
|
||||
title: String,
|
||||
content: String,
|
||||
last_saved_at: String,
|
||||
last_saved_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -46,9 +47,9 @@ pub struct ScratchpadDetailData {
|
||||
pub struct ScratchpadArchiveItem {
|
||||
id: String,
|
||||
title: String,
|
||||
archived_at: String,
|
||||
archived_at: Option<DateTime<Utc>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
ingested_at: Option<String>,
|
||||
ingested_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -56,9 +57,9 @@ pub struct ScratchpadDetail {
|
||||
id: String,
|
||||
title: String,
|
||||
content: String,
|
||||
created_at: String,
|
||||
updated_at: String,
|
||||
last_saved_at: String,
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
last_saved_at: DateTime<Utc>,
|
||||
is_dirty: bool,
|
||||
}
|
||||
|
||||
@@ -75,7 +76,7 @@ impl From<&Scratchpad> for ScratchpadListItem {
|
||||
id: value.id.clone(),
|
||||
title: value.title.clone(),
|
||||
content: value.content.clone(),
|
||||
last_saved_at: value.last_saved_at.format("%Y-%m-%d %H:%M").to_string(),
|
||||
last_saved_at: value.last_saved_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,13 +86,8 @@ impl From<&Scratchpad> for ScratchpadArchiveItem {
|
||||
Self {
|
||||
id: value.id.clone(),
|
||||
title: value.title.clone(),
|
||||
archived_at: value
|
||||
.archived_at
|
||||
.map(|dt| dt.format("%Y-%m-%d %H:%M").to_string())
|
||||
.unwrap_or_else(|| "Unknown".to_string()),
|
||||
ingested_at: value
|
||||
.ingested_at
|
||||
.map(|dt| dt.format("%Y-%m-%d %H:%M").to_string()),
|
||||
archived_at: value.archived_at,
|
||||
ingested_at: value.ingested_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,9 +98,9 @@ impl From<&Scratchpad> for ScratchpadDetail {
|
||||
id: value.id.clone(),
|
||||
title: value.title.clone(),
|
||||
content: value.content.clone(),
|
||||
created_at: value.created_at.format("%Y-%m-%d %H:%M:%S").to_string(),
|
||||
updated_at: value.updated_at.format("%Y-%m-%d %H:%M:%S").to_string(),
|
||||
last_saved_at: value.last_saved_at.format("%Y-%m-%d %H:%M:%S").to_string(),
|
||||
created_at: value.created_at,
|
||||
updated_at: value.updated_at,
|
||||
last_saved_at: value.last_saved_at,
|
||||
is_dirty: value.is_dirty,
|
||||
}
|
||||
}
|
||||
@@ -391,6 +387,126 @@ pub async fn ingest_scratchpad(
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn archive_scratchpad(
|
||||
RequireUser(user): RequireUser,
|
||||
State(state): State<HtmlState>,
|
||||
Path(scratchpad_id): Path<String>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
Scratchpad::archive(&scratchpad_id, &user.id, &state.db, false).await?;
|
||||
|
||||
let scratchpads = Scratchpad::get_by_user(&user.id, &state.db).await?;
|
||||
let archived_scratchpads = Scratchpad::get_archived_by_user(&user.id, &state.db).await?;
|
||||
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
||||
|
||||
let scratchpad_list: Vec<ScratchpadListItem> =
|
||||
scratchpads.iter().map(ScratchpadListItem::from).collect();
|
||||
let archived_list: Vec<ScratchpadArchiveItem> = archived_scratchpads
|
||||
.iter()
|
||||
.map(ScratchpadArchiveItem::from)
|
||||
.collect();
|
||||
|
||||
Ok(TemplateResponse::new_template(
|
||||
"scratchpad/base.html",
|
||||
ScratchpadPageData {
|
||||
user,
|
||||
scratchpads: scratchpad_list,
|
||||
archived_scratchpads: archived_list,
|
||||
conversation_archive,
|
||||
new_scratchpad: None,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::Utc;
|
||||
|
||||
#[test]
|
||||
fn test_scratchpad_list_item_conversion() {
|
||||
// Create a test scratchpad with datetime values
|
||||
let now = Utc::now();
|
||||
let mut scratchpad = common::storage::types::scratchpad::Scratchpad::new(
|
||||
"test_user".to_string(),
|
||||
"Test Scratchpad".to_string(),
|
||||
);
|
||||
|
||||
// Override the timestamps with known values for testing
|
||||
scratchpad.last_saved_at = now;
|
||||
|
||||
// Test conversion to ScratchpadListItem
|
||||
let list_item = ScratchpadListItem::from(&scratchpad);
|
||||
|
||||
assert_eq!(list_item.id, scratchpad.id);
|
||||
assert_eq!(list_item.title, scratchpad.title);
|
||||
assert_eq!(list_item.content, scratchpad.content);
|
||||
assert_eq!(list_item.last_saved_at, scratchpad.last_saved_at);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scratchpad_detail_conversion() {
|
||||
// Create a test scratchpad with datetime values
|
||||
let now = Utc::now();
|
||||
let mut scratchpad = common::storage::types::scratchpad::Scratchpad::new(
|
||||
"test_user".to_string(),
|
||||
"Test Scratchpad".to_string(),
|
||||
);
|
||||
|
||||
// Override the timestamps with known values for testing
|
||||
scratchpad.last_saved_at = now;
|
||||
|
||||
// Test conversion to ScratchpadDetail
|
||||
let detail = ScratchpadDetail::from(&scratchpad);
|
||||
|
||||
assert_eq!(detail.id, scratchpad.id);
|
||||
assert_eq!(detail.title, scratchpad.title);
|
||||
assert_eq!(detail.content, scratchpad.content);
|
||||
assert_eq!(detail.created_at, scratchpad.created_at);
|
||||
assert_eq!(detail.updated_at, scratchpad.updated_at);
|
||||
assert_eq!(detail.last_saved_at, scratchpad.last_saved_at);
|
||||
assert_eq!(detail.is_dirty, scratchpad.is_dirty);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scratchpad_archive_item_conversion() {
|
||||
// Create a test scratchpad with optional datetime values
|
||||
let now = Utc::now();
|
||||
let mut scratchpad = common::storage::types::scratchpad::Scratchpad::new(
|
||||
"test_user".to_string(),
|
||||
"Test Scratchpad".to_string(),
|
||||
);
|
||||
|
||||
// Set optional datetime fields
|
||||
scratchpad.archived_at = Some(now);
|
||||
scratchpad.ingested_at = Some(now);
|
||||
|
||||
// Test conversion to ScratchpadArchiveItem
|
||||
let archive_item = ScratchpadArchiveItem::from(&scratchpad);
|
||||
|
||||
assert_eq!(archive_item.id, scratchpad.id);
|
||||
assert_eq!(archive_item.title, scratchpad.title);
|
||||
assert_eq!(archive_item.archived_at, scratchpad.archived_at);
|
||||
assert_eq!(archive_item.ingested_at, scratchpad.ingested_at);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scratchpad_archive_item_conversion_with_none_values() {
|
||||
// Create a test scratchpad without optional datetime values
|
||||
let scratchpad = common::storage::types::scratchpad::Scratchpad::new(
|
||||
"test_user".to_string(),
|
||||
"Test Scratchpad".to_string(),
|
||||
);
|
||||
|
||||
// Test conversion to ScratchpadArchiveItem
|
||||
let archive_item = ScratchpadArchiveItem::from(&scratchpad);
|
||||
|
||||
assert_eq!(archive_item.id, scratchpad.id);
|
||||
assert_eq!(archive_item.title, scratchpad.title);
|
||||
assert_eq!(archive_item.archived_at, None);
|
||||
assert_eq!(archive_item.ingested_at, None);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn restore_scratchpad(
|
||||
RequireUser(user): RequireUser,
|
||||
State(state): State<HtmlState>,
|
||||
@@ -439,52 +555,3 @@ pub async fn restore_scratchpad(
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn archive_scratchpad(
|
||||
RequireUser(user): RequireUser,
|
||||
State(state): State<HtmlState>,
|
||||
Path(scratchpad_id): Path<String>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
Scratchpad::archive(&scratchpad_id, &user.id, &state.db, false).await?;
|
||||
|
||||
let scratchpads = Scratchpad::get_by_user(&user.id, &state.db).await?;
|
||||
let archived_scratchpads = Scratchpad::get_archived_by_user(&user.id, &state.db).await?;
|
||||
let conversation_archive = User::get_user_conversations(&user.id, &state.db).await?;
|
||||
|
||||
let scratchpad_list: Vec<ScratchpadListItem> =
|
||||
scratchpads.iter().map(ScratchpadListItem::from).collect();
|
||||
let archived_list: Vec<ScratchpadArchiveItem> = archived_scratchpads
|
||||
.iter()
|
||||
.map(ScratchpadArchiveItem::from)
|
||||
.collect();
|
||||
|
||||
let trigger_payload = serde_json::json!({
|
||||
"toast": {
|
||||
"title": "Scratchpad archived",
|
||||
"description": "You can find it in the archive drawer below.",
|
||||
"type": "warning"
|
||||
}
|
||||
});
|
||||
let trigger_value = serde_json::to_string(&trigger_payload).unwrap_or_else(|_| {
|
||||
r#"{"toast":{"title":"Scratchpad archived","description":"You can find it in the archive drawer below.","type":"warning"}}"#.to_string()
|
||||
});
|
||||
|
||||
let template_response = TemplateResponse::new_partial(
|
||||
"scratchpad/base.html",
|
||||
"main",
|
||||
ScratchpadPageData {
|
||||
user,
|
||||
scratchpads: scratchpad_list,
|
||||
archived_scratchpads: archived_list,
|
||||
conversation_archive,
|
||||
new_scratchpad: None,
|
||||
},
|
||||
);
|
||||
|
||||
let mut response = template_response.into_response();
|
||||
if let Ok(header_value) = HeaderValue::from_str(&trigger_value) {
|
||||
response.headers_mut().insert(HX_TRIGGER, header_value);
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
{{ scratchpad.content[:100] }}{% if scratchpad.content|length > 100 %}...{% endif %}
|
||||
</div>
|
||||
<div class="text-xs text-base-content/50">
|
||||
Last saved: {{ scratchpad.last_saved_at }}
|
||||
Last saved: {{ scratchpad.last_saved_at | datetimeformat(format="short", tz=user.timezone) }}
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
@@ -76,9 +76,9 @@
|
||||
<div class="flex items-start justify-between gap-3">
|
||||
<div class="flex-1 min-w-0">
|
||||
<h4 class="font-semibold text-base truncate" title="{{ scratchpad.title }}">{{ scratchpad.title }}</h4>
|
||||
<div class="text-xs text-base-content/50">Archived {{ scratchpad.archived_at }}</div>
|
||||
<div class="text-xs text-base-content/50">Archived {{ scratchpad.archived_at | datetimeformat(format="short", tz=user.timezone) }}</div>
|
||||
{% if scratchpad.ingested_at %}
|
||||
<div class="text-xs text-base-content/40">Ingestion started {{ scratchpad.ingested_at }}</div>
|
||||
<div class="text-xs text-base-content/40">Ingestion started {{ scratchpad.ingested_at | datetimeformat(format="short", tz=user.timezone) }}</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="flex items-center gap-2 flex-shrink-0 flex-wrap justify-end">
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
<div class="flex flex-col gap-3">
|
||||
<div class="text-xs text-base-content/50 flex items-center gap-2">
|
||||
<span>Last saved: <span id="last-saved">{{ scratchpad.last_saved_at }}</span></span>
|
||||
<span>Last saved: <span id="last-saved">{{ scratchpad.last_saved_at | datetimeformat(format="short", tz=user.timezone) }}</span></span>
|
||||
<span id="save-status"
|
||||
class="inline-flex items-center gap-1 text-success opacity-0 transition-opacity duration-300 pointer-events-none">
|
||||
{% include "icons/check_icon.html" %} <span class="uppercase tracking-wider text-[0.7em]">Saved</span>
|
||||
|
||||
Reference in New Issue
Block a user