ironclaw 0.22.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
//! Error handling and edge case tests for OpenClaw import.
//!
//! These tests verify proper error handling for:
//! - Missing/corrupt files
//! - Invalid configurations
//! - Database corruption
//! - Permission issues
//! - Edge cases in data

#![cfg(feature = "import")]

#[cfg(feature = "import")]
mod error_handling_tests {
    use std::path::PathBuf;
    use tempfile::TempDir;

    use ironclaw::import::ImportError;
    use ironclaw::import::openclaw::reader::OpenClawReader;

    // ────────────────────────────────────────────────────────────────────
    // Missing Directory Tests
    // ────────────────────────────────────────────────────────────────────

    #[test]
    fn test_error_nonexistent_openclaw_directory() {
        let nonexistent = PathBuf::from("/nonexistent/path/openclaw");
        let result = OpenClawReader::new(&nonexistent);

        assert!(result.is_err());
        if let Err(e) = result {
            match e {
                ImportError::NotFound { .. } => (), // Expected
                _ => panic!("Expected NotFound, got: {}", e),
            }
        }
    }

    #[test]
    fn test_error_empty_openclaw_directory() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let result = OpenClawReader::new(temp_dir.path());

        // Should succeed (directory exists)
        assert!(result.is_ok());

        let reader = result.unwrap();
        let config_result = reader.read_config();

        // But reading config should fail
        assert!(config_result.is_err());
    }

    // ────────────────────────────────────────────────────────────────────
    // Config File Errors
    // ────────────────────────────────────────────────────────────────────

    #[test]
    fn test_error_missing_openclaw_json() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let result = reader.read_config();
        assert!(result.is_err());
    }

    #[test]
    fn test_error_invalid_json5_syntax() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        // Invalid JSON5: missing closing brace
        let bad_config = r#"{ llm: { provider: "openai" }"#;
        std::fs::write(openclaw_path.join("openclaw.json"), bad_config).expect("write failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let result = reader.read_config();
        assert!(result.is_err());
    }

    #[test]
    fn test_error_truncated_json5() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        // Truncated JSON5
        std::fs::write(openclaw_path.join("openclaw.json"), "{").expect("write failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let result = reader.read_config();
        assert!(result.is_err());
    }

    #[test]
    fn test_error_empty_openclaw_json() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        // Empty file
        std::fs::write(openclaw_path.join("openclaw.json"), "").expect("write failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let result = reader.read_config();
        assert!(result.is_err());
    }

    // ────────────────────────────────────────────────────────────────────
    // SQLite Database Errors
    // ────────────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_error_corrupt_sqlite_file() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        // Write invalid SQLite data
        std::fs::write(
            agents_dir.join("bad.sqlite"),
            "this is definitely not a sqlite database",
        )
        .expect("write failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");
        assert_eq!(dbs.len(), 1);

        // But reading should fail
        let result = reader.read_memory_chunks(&dbs[0].1).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_error_missing_chunks_table() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        let db_path = agents_dir.join("no_chunks.sqlite");

        // Create valid SQLite but without chunks table
        let db = libsql::Builder::new_local(&db_path)
            .build()
            .await
            .expect("db creation failed");
        let conn = db.connect().expect("connect failed");
        conn.execute(
            "CREATE TABLE metadata (key TEXT PRIMARY KEY, value TEXT)",
            (),
        )
        .await
        .expect("create table failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");
        assert_eq!(dbs.len(), 1);

        // Should fail: chunks table doesn't exist
        let result = reader.read_memory_chunks(&dbs[0].1).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_error_missing_conversations_table() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        let db_path = agents_dir.join("no_conversations.sqlite");

        let db = libsql::Builder::new_local(&db_path)
            .build()
            .await
            .expect("db creation failed");
        let conn = db.connect().expect("connect failed");
        // Only create chunks table, not conversations
        conn.execute(
            "CREATE TABLE chunks (id TEXT, path TEXT, content TEXT, embedding BLOB, chunk_index INTEGER)",
            (),
        )
        .await
        .expect("create table failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");
        assert_eq!(dbs.len(), 1);

        // Should fail: conversations table doesn't exist
        let result = reader.read_conversations(&dbs[0].1).await;
        assert!(result.is_err());
    }

    // ────────────────────────────────────────────────────────────────────
    // Edge Cases
    // ────────────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_edge_case_empty_chunks_table() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        let db_path = agents_dir.join("empty.sqlite");

        let db = libsql::Builder::new_local(&db_path)
            .build()
            .await
            .expect("db creation failed");
        let conn = db.connect().expect("connect failed");
        conn.execute(
            "CREATE TABLE chunks (id TEXT, path TEXT, content TEXT, embedding BLOB, chunk_index INTEGER)",
            (),
        )
        .await
        .expect("create table failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");

        // Should succeed but return empty list
        let chunks = reader
            .read_memory_chunks(&dbs[0].1)
            .await
            .expect("read chunks failed");
        assert_eq!(chunks.len(), 0);
    }

    #[tokio::test]
    async fn test_edge_case_empty_conversations_table() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        let db_path = agents_dir.join("empty_conv.sqlite");

        let db = libsql::Builder::new_local(&db_path)
            .build()
            .await
            .expect("db creation failed");
        let conn = db.connect().expect("connect failed");
        conn.execute(
            "CREATE TABLE conversations (id TEXT, channel TEXT, created_at TEXT)",
            (),
        )
        .await
        .expect("create table failed");
        conn.execute(
            "CREATE TABLE messages (id TEXT, conversation_id TEXT, role TEXT, content TEXT, created_at TEXT)",
            (),
        )
        .await
        .expect("create table failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");

        // Should succeed but return empty list
        let conversations = reader
            .read_conversations(&dbs[0].1)
            .await
            .expect("read conversations failed");
        assert_eq!(conversations.len(), 0);
    }

    #[tokio::test]
    async fn test_edge_case_very_large_content() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        let db_path = agents_dir.join("large.sqlite");

        let db = libsql::Builder::new_local(&db_path)
            .build()
            .await
            .expect("db creation failed");
        let conn = db.connect().expect("connect failed");
        conn.execute(
            "CREATE TABLE chunks (id TEXT, path TEXT, content TEXT, embedding BLOB, chunk_index INTEGER)",
            (),
        )
        .await
        .expect("create table failed");

        // Insert very large content (1MB)
        let large_content = "x".repeat(1024 * 1024);
        conn.execute(
            "INSERT INTO chunks VALUES (?, ?, ?, ?, ?)",
            libsql::params!["id1", "path", large_content, libsql::Value::Null, 0i64],
        )
        .await
        .expect("insert failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");

        // Should still succeed
        let chunks = reader
            .read_memory_chunks(&dbs[0].1)
            .await
            .expect("read chunks failed");
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].content.len(), 1024 * 1024);
    }

    #[tokio::test]
    async fn test_edge_case_special_characters_in_content() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        let db_path = agents_dir.join("special.sqlite");

        let db = libsql::Builder::new_local(&db_path)
            .build()
            .await
            .expect("db creation failed");
        let conn = db.connect().expect("connect failed");
        conn.execute(
            "CREATE TABLE chunks (id TEXT, path TEXT, content TEXT, embedding BLOB, chunk_index INTEGER)",
            (),
        )
        .await
        .expect("create table failed");

        // Insert content with special characters
        let special_content = "Content with emoji \u{1f680} and UTF-8: \u{4e2d}\u{6587}, \u{0627}\u{0644}\u{0639}\u{0631}\u{0628}\u{064a}\u{0629}, \u{03b5}\u{03bb}\u{03bb}\u{03b7}\u{03bd}\u{03b9}\u{03ba}\u{03ac}";
        conn.execute(
            "INSERT INTO chunks VALUES (?, ?, ?, ?, ?)",
            libsql::params!["id1", "path", special_content, libsql::Value::Null, 0i64],
        )
        .await
        .expect("insert failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");

        // Should handle special characters
        let chunks = reader
            .read_memory_chunks(&dbs[0].1)
            .await
            .expect("read chunks failed");
        assert_eq!(chunks.len(), 1);
        assert!(chunks[0].content.contains("\u{1f680}"));
        assert!(chunks[0].content.contains("\u{4e2d}\u{6587}"));
    }

    #[tokio::test]
    async fn test_edge_case_null_values_in_fields() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let agents_dir = openclaw_path.join("agents");
        std::fs::create_dir_all(&agents_dir).expect("mkdir failed");

        let db_path = agents_dir.join("nulls.sqlite");

        let db = libsql::Builder::new_local(&db_path)
            .build()
            .await
            .expect("db creation failed");
        let conn = db.connect().expect("connect failed");
        conn.execute(
            "CREATE TABLE conversations (id TEXT, channel TEXT, created_at TEXT)",
            (),
        )
        .await
        .expect("create table failed");
        conn.execute(
            "CREATE TABLE messages (id TEXT, conversation_id TEXT, role TEXT, content TEXT, created_at TEXT)",
            (),
        )
        .await
        .expect("create table failed");

        // Insert conversation with NULL created_at
        conn.execute(
            "INSERT INTO conversations VALUES (?, ?, ?)",
            libsql::params!["conv1", "telegram", libsql::Value::Null],
        )
        .await
        .expect("insert failed");

        // Insert message with NULL created_at
        conn.execute(
            "INSERT INTO messages VALUES (?, ?, ?, ?, ?)",
            libsql::params!["msg1", "conv1", "user", "hello", libsql::Value::Null],
        )
        .await
        .expect("insert failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let dbs = reader.list_agent_dbs().expect("list agent dbs failed");

        // Should handle NULL timestamps gracefully
        let conversations = reader
            .read_conversations(&dbs[0].1)
            .await
            .expect("read conversations failed");
        assert_eq!(conversations.len(), 1);
        assert!(conversations[0].created_at.is_none());
        assert!(conversations[0].messages[0].created_at.is_none());
    }

    // ────────────────────────────────────────────────────────────────────
    // Workspace File Errors
    // ────────────────────────────────────────────────────────────────────

    #[test]
    fn test_error_workspace_not_directory() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        // Create "workspace" as a file, not a directory
        std::fs::write(openclaw_path.join("workspace"), "not a directory").expect("write failed");

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        // Should handle gracefully (no files found)
        let count = reader
            .list_workspace_files()
            .expect("list workspace files failed");
        assert_eq!(count, 0);
    }

    #[test]
    fn test_edge_case_many_markdown_files() {
        let temp_dir = TempDir::new().expect("temp dir creation failed");
        let openclaw_path = temp_dir.path().to_path_buf();

        let workspace_dir = openclaw_path.join("workspace");
        std::fs::create_dir_all(&workspace_dir).expect("mkdir failed");

        // Create 100 markdown files
        for i in 0..100 {
            std::fs::write(workspace_dir.join(format!("doc_{}.md", i)), "content")
                .expect("write failed");
        }

        let reader = OpenClawReader::new(&openclaw_path).expect("reader creation failed");

        let count = reader
            .list_workspace_files()
            .expect("list workspace files failed");
        assert_eq!(count, 100);
    }
}