Repository: TabbyML/tabby
File: ee/tabby-webserver/src/service/thread.rs (Rust)

Code before:
    #[tokio::test]
    async fn test_get_thread() {
        let db = DbConn::new_in_memory().await.unwrap();
        let user_id = create_user(&db).await.as_id();
        let service = create(db, None);

        let input = CreateThreadInput {
            user_message: CreateMessageInput {
                content: "Ping".to_string(),
                attachments: None,
            },
        };

        let thread_id = service.create(&user_id, &input).await.unwrap();

        let thread = service.get(&thread_id).await.unwrap();
        assert!(thread.is_some());
        assert_eq!(thread.unwrap().id, thread_id);

        let non_existent_id = ID::from("non_existent".to_string());
        let non_existent_thread = service.get(&non_existent_id).await.unwrap();
        assert!(non_existent_thread.is_none());
    }

    #[tokio::test]
    async fn test_delete_thread() {
        let db = DbConn::new_in_memory().await.unwrap();
        let user_id = create_user(&db).await.as_id();
        let service = create(db.clone(), None);

        let input = CreateThreadInput {
            user_message: CreateMessageInput {
                content: "ping".to_string(),
                attachments: None,
            },
        };

        let thread_id = service.create(&user_id, &input).await.unwrap();
        service.delete(&thread_id).await.unwrap();

        let deleted_thread = service.get(&thread_id).await.unwrap();
        assert!(deleted_thread.is_none());
    }

    #[tokio::test]
    async fn test_set_persisted() {
        let db = DbConn::new_in_memory().await.unwrap();
        let user_id = create_user(&db).await.as_id();
        let service = create(db.clone(), None);

        let input = CreateThreadInput {

Review comment (suggestion):
please also verify messages of the thread got deleted as part of cascading deletion

https://demo.tabbyml.com/files/github/TabbyML/tabby/-/blob/a7d29b822c7cdcad03873c308412656a727809da/ee/tabby-db/schema/schema.sql#L185

Code after:
    #[tokio::test]
    async fn test_get_thread() {
        let db = DbConn::new_in_memory().await.unwrap();
        let user_id = create_user(&db).await.as_id();
        let service = create(db, None);

        let input = CreateThreadInput {
            user_message: CreateMessageInput {
                content: "Ping".to_string(),
                attachments: None,
            },
        };

        let thread_id = service.create(&user_id, &input).await.unwrap();

        let thread = service.get(&thread_id).await.unwrap();
        assert!(thread.is_some());
        assert_eq!(thread.unwrap().id, thread_id);

        let non_existent_id = ID::from("non_existent".to_string());
        let non_existent_thread = service.get(&non_existent_id).await.unwrap();
        assert!(non_existent_thread.is_none());
    }

    #[tokio::test]
    async fn test_delete_thread() {
        let db = DbConn::new_in_memory().await.unwrap();
        let user_id = create_user(&db).await.as_id();
        let service = create(db.clone(), None);

        let input = CreateThreadInput {
            user_message: CreateMessageInput {
                content: "ping".to_string(),
                attachments: None,
            },
        };

        let thread_id = service.create(&user_id, &input).await.unwrap();
        service.delete(&thread_id).await.unwrap();

        let deleted_thread = service.get(&thread_id).await.unwrap();
        assert!(deleted_thread.is_none());

        // Verify that the messages were also deleted
        let messages = service
            .list_thread_messages(&thread_id, None, None, None, None)
            .await
            .unwrap();
        assert_eq!(messages.len(), 0);
    }