jacs 0.9.5

JACS JSON AI Communication Standard
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
//! Test utilities for JACS storage backend conformance testing.
//!
//! This module provides:
//! - [`make_test_doc`] — helper to create test documents
//! - [`storage_conformance_tests!`] — macro generating 11 `StorageDocumentTraits` tests
//! - [`database_conformance_tests!`] — macro generating 9 `DatabaseDocumentTraits` tests
//!
//! These are intended for use by storage backend crates (jacs-postgresql,
//! jacs-surrealdb, etc.) to verify trait implementation correctness.
//!
//! # Usage from an external crate
//!
//! ```rust,ignore
//! use jacs::testing::make_test_doc;
//! use serial_test::serial;
//!
//! async fn create_my_storage() -> MyStorage { /* ... */ }
//!
//! jacs::storage_conformance_tests!(create_my_storage);
//! jacs::database_conformance_tests!(create_my_storage);
//! ```

use crate::agent::document::JACSDocument;
use serde_json::json;

/// Create a test document with the given fields.
///
/// If `agent_id` is provided, a `jacsSignature` block is attached so that
/// `store_document` can extract the agent ID column.
pub fn make_test_doc(
    id: &str,
    version: &str,
    jacs_type: &str,
    agent_id: Option<&str>,
) -> JACSDocument {
    let mut value = json!({
        "jacsId": id,
        "jacsVersion": version,
        "jacsType": jacs_type,
        "jacsLevel": "raw",
        "data": "test content"
    });
    if let Some(aid) = agent_id {
        value["jacsSignature"] = json!({
            "jacsSignatureAgentId": aid
        });
    }
    JACSDocument {
        id: id.to_string(),
        version: version.to_string(),
        value,
        jacs_type: jacs_type.to_string(),
    }
}

/// Generates 11 conformance tests for `StorageDocumentTraits`.
///
/// Tests cover: store/retrieve, exists, not found, remove, versions,
/// latest document, merge (error), bulk store/retrieve, idempotent store,
/// invalid key format.
///
/// Note: `list_documents` and `get_documents_by_agent` are excluded because
/// their semantics differ between file-based and database backends. They are
/// tested in [`database_conformance_tests!`] instead.
///
/// # Arguments
///
/// `$factory` — an async function that returns an instance implementing
/// `StorageDocumentTraits`. Each test calls it to get a fresh storage backend.
#[macro_export]
macro_rules! storage_conformance_tests {
    ($factory:expr) => {
        use jacs::storage::StorageDocumentTraits;
        use jacs::testing::make_test_doc;

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_store_and_retrieve() {
            let storage = $factory().await;
            let doc = make_test_doc("conf-sr-1", "v1", "agent", Some("agent-alpha"));
            storage.store_document(&doc).expect("store_document failed");

            let retrieved = storage
                .get_document("conf-sr-1:v1")
                .expect("get_document failed");
            assert_eq!(retrieved.id, "conf-sr-1");
            assert_eq!(retrieved.version, "v1");
            assert_eq!(retrieved.jacs_type, "agent");
            assert_eq!(retrieved.value["jacsId"], "conf-sr-1");
            assert_eq!(retrieved.value["data"], "test content");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_document_exists() {
            let storage = $factory().await;
            let doc = make_test_doc("conf-de-1", "v1", "agent", None);
            storage.store_document(&doc).expect("store_document failed");

            assert!(
                storage
                    .document_exists("conf-de-1:v1")
                    .expect("document_exists failed"),
                "Stored document should exist"
            );
            assert!(
                !storage
                    .document_exists("nonexistent:v1")
                    .expect("document_exists failed"),
                "Non-existent document should not exist"
            );
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_document_not_found() {
            let storage = $factory().await;
            let result = storage.get_document("missing-doc:v1");
            assert!(result.is_err(), "get_document on missing key should error");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_remove_document() {
            let storage = $factory().await;
            let doc = make_test_doc("conf-rm-1", "v1", "config", None);
            storage.store_document(&doc).expect("store_document failed");

            assert!(storage.document_exists("conf-rm-1:v1").unwrap());

            let removed = storage
                .remove_document("conf-rm-1:v1")
                .expect("remove_document failed");
            assert_eq!(removed.id, "conf-rm-1");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_get_document_versions() {
            let storage = $factory().await;
            storage
                .store_document(&make_test_doc("conf-dv-1", "v1", "agent", None))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-dv-1", "v2", "agent", None))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-dv-1", "v3", "agent", None))
                .unwrap();

            let versions = storage
                .get_document_versions("conf-dv-1")
                .expect("get_document_versions failed");
            assert_eq!(versions.len(), 3, "Should have 3 versions");
            for key in &versions {
                assert!(
                    key.starts_with("conf-dv-1:"),
                    "Key '{}' should start with 'conf-dv-1:'",
                    key
                );
            }
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_get_latest_document() {
            let storage = $factory().await;
            storage
                .store_document(&make_test_doc("conf-gl-1", "v1", "agent", None))
                .unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            storage
                .store_document(&make_test_doc("conf-gl-1", "v2", "agent", None))
                .unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            storage
                .store_document(&make_test_doc("conf-gl-1", "v3", "agent", None))
                .unwrap();

            let latest = storage
                .get_latest_document("conf-gl-1")
                .expect("get_latest_document failed");
            assert_eq!(latest.version, "v3", "Latest should be v3");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_merge_documents() {
            let storage = $factory().await;
            let result = storage.merge_documents("some-id", "v1", "v2");
            assert!(result.is_err(), "merge_documents should return an error");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_store_documents_bulk() {
            let storage = $factory().await;
            let docs = vec![
                make_test_doc("conf-bulk-1", "v1", "agent", None),
                make_test_doc("conf-bulk-2", "v1", "agent", None),
                make_test_doc("conf-bulk-3", "v1", "config", None),
            ];

            storage
                .store_documents(docs)
                .expect("store_documents failed");

            assert!(storage.document_exists("conf-bulk-1:v1").unwrap());
            assert!(storage.document_exists("conf-bulk-2:v1").unwrap());
            assert!(storage.document_exists("conf-bulk-3:v1").unwrap());
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_get_documents_bulk() {
            let storage = $factory().await;
            let docs = vec![
                make_test_doc("conf-gbulk-1", "v1", "agent", None),
                make_test_doc("conf-gbulk-2", "v1", "config", None),
            ];
            storage
                .store_documents(docs)
                .expect("store_documents failed");

            let keys = vec!["conf-gbulk-1:v1".to_string(), "conf-gbulk-2:v1".to_string()];
            let retrieved = storage.get_documents(keys).expect("get_documents failed");
            assert_eq!(retrieved.len(), 2, "Should retrieve 2 documents");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_idempotent_store() {
            let storage = $factory().await;
            let doc = make_test_doc("conf-idem-1", "v1", "agent", None);
            storage
                .store_document(&doc)
                .expect("First store should succeed");
            storage
                .store_document(&doc)
                .expect("Second store (idempotent) should not error");

            let versions = storage
                .get_document_versions("conf-idem-1")
                .expect("get_document_versions failed");
            assert_eq!(
                versions.len(),
                1,
                "Duplicate insert should not create a second row"
            );
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_invalid_key_format() {
            let storage = $factory().await;
            let result = storage.get_document("invalid-key-no-colon");
            assert!(
                result.is_err(),
                "get_document with invalid key format should error"
            );
        }
    };
}

/// Generates 9 conformance tests for `DatabaseDocumentTraits`.
///
/// Includes both `DatabaseDocumentTraits` methods and the database-specific
/// `StorageDocumentTraits` methods (`list_documents`, `get_documents_by_agent`)
/// which have different semantics in file-based backends.
///
/// # Arguments
///
/// `$factory` — an async function that returns an instance implementing
/// `DatabaseDocumentTraits + StorageDocumentTraits`. Each test calls it to
/// get a fresh storage backend.
#[macro_export]
macro_rules! database_conformance_tests {
    ($factory:expr) => {
        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_list_documents() {
            let storage = $factory().await;
            storage
                .store_document(&make_test_doc("conf-ls-a1", "v1", "agent", None))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-ls-a2", "v1", "agent", None))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-ls-c1", "v1", "config", None))
                .unwrap();

            let agent_docs = storage
                .list_documents("agent")
                .expect("list_documents failed");
            assert_eq!(agent_docs.len(), 2, "Should list exactly 2 agent documents");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_get_documents_by_agent() {
            let storage = $factory().await;
            storage
                .store_document(&make_test_doc("conf-ba-1", "v1", "agent", Some("alice")))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-ba-2", "v1", "config", Some("alice")))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-ba-3", "v1", "agent", Some("bob")))
                .unwrap();

            let alice_docs = storage
                .get_documents_by_agent("alice")
                .expect("get_documents_by_agent failed");
            assert_eq!(alice_docs.len(), 2, "Alice should have 2 documents");

            let bob_docs = storage
                .get_documents_by_agent("bob")
                .expect("get_documents_by_agent failed");
            assert_eq!(bob_docs.len(), 1, "Bob should have 1 document");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_query_by_type() {
            let storage = $factory().await;
            for i in 0..5 {
                let id = format!("conf-qbt-{}", i);
                storage
                    .store_document(&make_test_doc(&id, "v1", "task", None))
                    .unwrap();
                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
            }

            let page1 = storage
                .query_by_type("task", 3, 0)
                .expect("query_by_type page1 failed");
            assert_eq!(page1.len(), 3, "Page 1 should have 3 results");

            let page2 = storage
                .query_by_type("task", 3, 3)
                .expect("query_by_type page2 failed");
            assert_eq!(page2.len(), 2, "Page 2 should have 2 results");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_query_by_field() {
            let storage = $factory().await;

            let mut doc_a = make_test_doc("conf-qbf-a", "v1", "config", None);
            doc_a.value["status"] = serde_json::json!("active");
            storage.store_document(&doc_a).unwrap();

            let mut doc_b = make_test_doc("conf-qbf-b", "v1", "config", None);
            doc_b.value["status"] = serde_json::json!("inactive");
            storage.store_document(&doc_b).unwrap();

            let active = storage
                .query_by_field("status", "active", None, 100, 0)
                .expect("query_by_field failed");
            assert_eq!(active.len(), 1, "Should find 1 active document");

            let missing = storage
                .query_by_field("status", "archived", None, 100, 0)
                .expect("query_by_field for missing value failed");
            assert!(missing.is_empty(), "Should find no 'archived' documents");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_count_by_type() {
            let storage = $factory().await;
            for i in 0..4 {
                storage
                    .store_document(&make_test_doc(
                        &format!("conf-cnt-{}", i),
                        "v1",
                        "message",
                        None,
                    ))
                    .unwrap();
            }
            storage
                .store_document(&make_test_doc("conf-cnt-other", "v1", "agent", None))
                .unwrap();

            let count = storage
                .count_by_type("message")
                .expect("count_by_type failed");
            assert_eq!(count, 4, "Should count exactly 4 message documents");

            let zero = storage
                .count_by_type("nonexistent")
                .expect("count_by_type failed");
            assert_eq!(zero, 0, "Non-existent type should have count 0");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_get_versions() {
            let storage = $factory().await;
            storage
                .store_document(&make_test_doc("conf-gv-1", "v1", "agent", Some("agent-x")))
                .unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(30)).await;
            storage
                .store_document(&make_test_doc("conf-gv-1", "v2", "agent", Some("agent-x")))
                .unwrap();

            let versions = storage
                .get_versions("conf-gv-1")
                .expect("get_versions failed");
            assert_eq!(versions.len(), 2);
            assert_eq!(versions[0].version, "v1", "Ordered by created_at ASC");
            assert_eq!(versions[1].version, "v2");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_get_latest() {
            let storage = $factory().await;
            storage
                .store_document(&make_test_doc("conf-lt-1", "v1", "config", None))
                .unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(30)).await;
            storage
                .store_document(&make_test_doc("conf-lt-1", "v2", "config", None))
                .unwrap();

            let latest = storage.get_latest("conf-lt-1").expect("get_latest failed");
            assert_eq!(latest.version, "v2");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_query_by_agent() {
            let storage = $factory().await;
            storage
                .store_document(&make_test_doc("conf-qba-1", "v1", "agent", Some("alice")))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-qba-2", "v1", "config", Some("alice")))
                .unwrap();
            storage
                .store_document(&make_test_doc("conf-qba-3", "v1", "agent", Some("bob")))
                .unwrap();

            let alice_all = storage
                .query_by_agent("alice", None, 100, 0)
                .expect("query_by_agent failed");
            assert_eq!(alice_all.len(), 2, "Alice should have 2 documents");

            let alice_agents = storage
                .query_by_agent("alice", Some("agent"), 100, 0)
                .expect("query_by_agent with type failed");
            assert_eq!(alice_agents.len(), 1, "Alice should have 1 agent document");
        }

        #[tokio::test(flavor = "multi_thread")]
        #[serial]
        async fn conformance_db_migrations_idempotent() {
            let storage = $factory().await;
            storage
                .run_migrations()
                .expect("Second run_migrations should not error");
        }
    };
}