nblm-core 0.2.2

Core library for NotebookLM Enterprise API client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct NotebookSource {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<NotebookSourceMetadata>,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub settings: Option<NotebookSourceSettings>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_id: Option<NotebookSourceId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct NotebookSourceMetadata {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_added_timestamp: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub word_count: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub youtube_metadata: Option<NotebookSourceYoutubeMetadata>,
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct NotebookSourceYoutubeMetadata {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_id: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct NotebookSourceSettings {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct NotebookSourceId {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UserContent {
    Web {
        #[serde(rename = "webContent")]
        web_content: WebContent,
    },
    Text {
        #[serde(rename = "textContent")]
        text_content: TextContent,
    },
    GoogleDrive {
        #[serde(rename = "googleDriveContent")]
        google_drive_content: GoogleDriveContent,
    },
    Video {
        #[serde(rename = "videoContent")]
        video_content: VideoContent,
    },
}

impl UserContent {
    pub fn web(url: String, source_name: Option<String>) -> Self {
        Self::Web {
            web_content: WebContent { url, source_name },
        }
    }

    pub fn text(content: String, source_name: Option<String>) -> Self {
        Self::Text {
            text_content: TextContent {
                content,
                source_name,
            },
        }
    }

    pub fn google_drive(
        document_id: String,
        mime_type: String,
        source_name: Option<String>,
    ) -> Self {
        Self::GoogleDrive {
            google_drive_content: GoogleDriveContent {
                document_id,
                mime_type,
                source_name,
            },
        }
    }

    pub fn video(url: String) -> Self {
        Self::Video {
            video_content: VideoContent { url },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct WebContent {
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TextContent {
    pub content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GoogleDriveContent {
    pub document_id: String,
    pub mime_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VideoContent {
    #[serde(rename = "youtubeUrl")]
    pub url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct BatchCreateSourcesRequest {
    #[serde(rename = "userContents")]
    pub user_contents: Vec<UserContent>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct BatchCreateSourcesResponse {
    #[serde(default)]
    pub sources: Vec<NotebookSource>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_count: Option<i32>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct BatchDeleteSourcesRequest {
    pub names: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct BatchDeleteSourcesResponse {
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct UploadSourceFileResponse {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_id: Option<NotebookSourceId>,
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

#[cfg(test)]
mod tests {
    use super::*;

    // Test 1: UserContent constructor methods
    #[test]
    fn test_user_content_web_constructor() {
        let url = "https://example.com".to_string();
        let source_name = Some("Test Web Source".to_string());

        let content = UserContent::web(url.clone(), source_name.clone());

        match content {
            UserContent::Web { web_content } => {
                assert_eq!(web_content.url, url);
                assert_eq!(web_content.source_name, source_name);
            }
            _ => panic!("Expected UserContent::Web variant"),
        }
    }

    #[test]
    fn test_user_content_web_constructor_without_name() {
        let url = "https://example.com".to_string();

        let content = UserContent::web(url.clone(), None);

        match content {
            UserContent::Web { web_content } => {
                assert_eq!(web_content.url, url);
                assert_eq!(web_content.source_name, None);
            }
            _ => panic!("Expected UserContent::Web variant"),
        }
    }

    #[test]
    fn test_user_content_text_constructor() {
        let text = "Sample text content".to_string();
        let source_name = Some("Test Text".to_string());

        let content = UserContent::text(text.clone(), source_name.clone());

        match content {
            UserContent::Text { text_content } => {
                assert_eq!(text_content.content, text);
                assert_eq!(text_content.source_name, source_name);
            }
            _ => panic!("Expected UserContent::Text variant"),
        }
    }

    #[test]
    fn test_user_content_google_drive_constructor() {
        let document_id = "doc-12345".to_string();
        let mime_type = "application/pdf".to_string();
        let source_name = Some("Drive Document".to_string());

        let content =
            UserContent::google_drive(document_id.clone(), mime_type.clone(), source_name.clone());

        match content {
            UserContent::GoogleDrive {
                google_drive_content,
            } => {
                assert_eq!(google_drive_content.document_id, document_id);
                assert_eq!(google_drive_content.mime_type, mime_type);
                assert_eq!(google_drive_content.source_name, source_name);
            }
            _ => panic!("Expected UserContent::GoogleDrive variant"),
        }
    }

    #[test]
    fn test_user_content_video_constructor() {
        let url = "https://youtube.com/watch?v=abc123".to_string();

        let content = UserContent::video(url.clone());

        match content {
            UserContent::Video { video_content } => {
                assert_eq!(video_content.url, url);
            }
            _ => panic!("Expected UserContent::Video variant"),
        }
    }

    // Test 2: Serialization/Deserialization correctness
    #[test]
    fn test_user_content_web_serialization() {
        let content = UserContent::web(
            "https://example.com".to_string(),
            Some("Web Source".to_string()),
        );

        let json = serde_json::to_value(&content).unwrap();

        assert_eq!(json["webContent"]["url"], "https://example.com");
        assert_eq!(json["webContent"]["sourceName"], "Web Source");
    }

    #[test]
    fn test_user_content_web_deserialization() {
        let json = serde_json::json!({
            "webContent": {
                "url": "https://example.com",
                "sourceName": "Web Source"
            }
        });

        let content: UserContent = serde_json::from_value(json).unwrap();

        match content {
            UserContent::Web { web_content } => {
                assert_eq!(web_content.url, "https://example.com");
                assert_eq!(web_content.source_name, Some("Web Source".to_string()));
            }
            _ => panic!("Expected UserContent::Web variant"),
        }
    }

    #[test]
    fn test_user_content_text_serialization() {
        let content = UserContent::text("Sample text".to_string(), Some("Text Source".to_string()));

        let json = serde_json::to_value(&content).unwrap();

        assert_eq!(json["textContent"]["content"], "Sample text");
        assert_eq!(json["textContent"]["sourceName"], "Text Source");
    }

    #[test]
    fn test_user_content_text_deserialization() {
        let json = serde_json::json!({
            "textContent": {
                "content": "Sample text",
                "sourceName": "Text Source"
            }
        });

        let content: UserContent = serde_json::from_value(json).unwrap();

        match content {
            UserContent::Text { text_content } => {
                assert_eq!(text_content.content, "Sample text");
                assert_eq!(text_content.source_name, Some("Text Source".to_string()));
            }
            _ => panic!("Expected UserContent::Text variant"),
        }
    }

    #[test]
    fn test_user_content_google_drive_serialization() {
        let content = UserContent::google_drive(
            "doc-123".to_string(),
            "application/pdf".to_string(),
            Some("Drive Doc".to_string()),
        );

        let json = serde_json::to_value(&content).unwrap();

        assert_eq!(json["googleDriveContent"]["documentId"], "doc-123");
        assert_eq!(json["googleDriveContent"]["mimeType"], "application/pdf");
        assert_eq!(json["googleDriveContent"]["sourceName"], "Drive Doc");
    }

    #[test]
    fn test_user_content_google_drive_deserialization() {
        let json = serde_json::json!({
            "googleDriveContent": {
                "documentId": "doc-123",
                "mimeType": "application/pdf",
                "sourceName": "Drive Doc"
            }
        });

        let content: UserContent = serde_json::from_value(json).unwrap();

        match content {
            UserContent::GoogleDrive {
                google_drive_content,
            } => {
                assert_eq!(google_drive_content.document_id, "doc-123");
                assert_eq!(google_drive_content.mime_type, "application/pdf");
                assert_eq!(
                    google_drive_content.source_name,
                    Some("Drive Doc".to_string())
                );
            }
            _ => panic!("Expected UserContent::GoogleDrive variant"),
        }
    }

    #[test]
    fn test_user_content_video_serialization() {
        let content = UserContent::video("https://youtube.com/watch?v=abc".to_string());

        let json = serde_json::to_value(&content).unwrap();

        assert_eq!(
            json["videoContent"]["youtubeUrl"],
            "https://youtube.com/watch?v=abc"
        );
    }

    #[test]
    fn test_user_content_video_deserialization() {
        let json = serde_json::json!({
            "videoContent": {
                "youtubeUrl": "https://youtube.com/watch?v=abc"
            }
        });

        let content: UserContent = serde_json::from_value(json).unwrap();

        match content {
            UserContent::Video { video_content } => {
                assert_eq!(video_content.url, "https://youtube.com/watch?v=abc");
            }
            _ => panic!("Expected UserContent::Video variant"),
        }
    }

    // Test 3: skip_serializing_if behavior (None fields are omitted)
    #[test]
    fn test_web_content_omits_none_source_name() {
        let content = WebContent {
            url: "https://example.com".to_string(),
            source_name: None,
        };

        let json = serde_json::to_value(&content).unwrap();
        let obj = json.as_object().unwrap();

        // sourceName should not be present in JSON
        assert!(!obj.contains_key("sourceName"));
        assert_eq!(obj.get("url").unwrap(), "https://example.com");
    }

    #[test]
    fn test_web_content_includes_some_source_name() {
        let content = WebContent {
            url: "https://example.com".to_string(),
            source_name: Some("My Source".to_string()),
        };

        let json = serde_json::to_value(&content).unwrap();
        let obj = json.as_object().unwrap();

        // sourceName should be present
        assert!(obj.contains_key("sourceName"));
        assert_eq!(obj.get("sourceName").unwrap(), "My Source");
    }

    #[test]
    fn test_text_content_omits_none_source_name() {
        let content = TextContent {
            content: "text".to_string(),
            source_name: None,
        };

        let json = serde_json::to_value(&content).unwrap();
        let obj = json.as_object().unwrap();

        assert!(!obj.contains_key("sourceName"));
        assert_eq!(obj.get("content").unwrap(), "text");
    }

    #[test]
    fn test_google_drive_content_omits_none_source_name() {
        let content = GoogleDriveContent {
            document_id: "doc-123".to_string(),
            mime_type: "application/pdf".to_string(),
            source_name: None,
        };

        let json = serde_json::to_value(&content).unwrap();
        let obj = json.as_object().unwrap();

        assert!(!obj.contains_key("sourceName"));
        assert_eq!(obj.get("documentId").unwrap(), "doc-123");
        assert_eq!(obj.get("mimeType").unwrap(), "application/pdf");
    }

    #[test]
    fn test_notebook_source_omits_none_fields() {
        let source = NotebookSource {
            name: "projects/123/notebooks/456/sources/789".to_string(),
            metadata: None,
            settings: None,
            source_id: None,
            title: None,
            extra: HashMap::new(),
        };

        let json = serde_json::to_value(&source).unwrap();
        let obj = json.as_object().unwrap();

        // Only 'name' should be present
        assert!(obj.contains_key("name"));
        assert!(!obj.contains_key("metadata"));
        assert!(!obj.contains_key("settings"));
        assert!(!obj.contains_key("sourceId"));
        assert!(!obj.contains_key("title"));
    }

    #[test]
    fn test_notebook_source_includes_some_fields() {
        let source = NotebookSource {
            name: "projects/123/notebooks/456/sources/789".to_string(),
            metadata: Some(NotebookSourceMetadata {
                word_count: Some(1000),
                ..Default::default()
            }),
            settings: Some(NotebookSourceSettings {
                status: Some("ACTIVE".to_string()),
                ..Default::default()
            }),
            source_id: Some(NotebookSourceId {
                id: Some("source-123".to_string()),
                ..Default::default()
            }),
            title: Some("My Document".to_string()),
            extra: HashMap::new(),
        };

        let json = serde_json::to_value(&source).unwrap();
        let obj = json.as_object().unwrap();

        assert!(obj.contains_key("name"));
        assert!(obj.contains_key("metadata"));
        assert!(obj.contains_key("settings"));
        assert!(obj.contains_key("sourceId"));
        assert!(obj.contains_key("title"));
    }

    #[test]
    fn test_batch_create_sources_request_serialization() {
        let request = BatchCreateSourcesRequest {
            user_contents: vec![
                UserContent::web("https://example.com".to_string(), None),
                UserContent::text("Sample text".to_string(), Some("Text".to_string())),
            ],
        };

        let json = serde_json::to_value(&request).unwrap();

        assert!(json["userContents"].is_array());
        assert_eq!(json["userContents"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn test_batch_create_sources_response_deserialization() {
        let json = serde_json::json!({
            "sources": [
                {
                    "name": "projects/123/notebooks/456/sources/789"
                }
            ],
            "errorCount": 0
        });

        let response: BatchCreateSourcesResponse = serde_json::from_value(json).unwrap();

        assert_eq!(response.sources.len(), 1);
        assert_eq!(response.error_count, Some(0));
    }

    #[test]
    fn test_batch_create_sources_response_empty_sources() {
        let json = serde_json::json!({
            "sources": []
        });

        let response: BatchCreateSourcesResponse = serde_json::from_value(json).unwrap();

        assert_eq!(response.sources.len(), 0);
        assert_eq!(response.error_count, None);
    }

    #[test]
    fn test_notebook_source_metadata_youtube() {
        let metadata = NotebookSourceMetadata {
            source_added_timestamp: Some("2024-01-01T00:00:00Z".to_string()),
            word_count: Some(5000),
            youtube_metadata: Some(NotebookSourceYoutubeMetadata {
                channel_name: Some("Test Channel".to_string()),
                video_id: Some("abc123".to_string()),
                extra: HashMap::new(),
            }),
            extra: HashMap::new(),
        };

        let json = serde_json::to_value(&metadata).unwrap();

        assert_eq!(json["sourceAddedTimestamp"], "2024-01-01T00:00:00Z");
        assert_eq!(json["wordCount"], 5000);
        assert_eq!(json["youtubeMetadata"]["channelName"], "Test Channel");
        assert_eq!(json["youtubeMetadata"]["videoId"], "abc123");
    }

    #[test]
    fn test_upload_source_file_response_omits_none_source_id() {
        let response = UploadSourceFileResponse {
            source_id: None,
            extra: HashMap::new(),
        };

        let json = serde_json::to_value(&response).unwrap();
        let obj = json.as_object().unwrap();

        assert!(!obj.contains_key("sourceId"));
    }

    #[test]
    fn test_camel_case_serialization() {
        // Verify that snake_case fields are converted to camelCase
        let metadata = NotebookSourceMetadata {
            source_added_timestamp: Some("2024-01-01T00:00:00Z".to_string()),
            word_count: Some(100),
            youtube_metadata: None,
            extra: HashMap::new(),
        };

        let json = serde_json::to_string(&metadata).unwrap();

        // Should contain camelCase keys
        assert!(json.contains("sourceAddedTimestamp"));
        assert!(json.contains("wordCount"));
        // Should NOT contain snake_case keys
        assert!(!json.contains("source_added_timestamp"));
        assert!(!json.contains("word_count"));
    }
}