objectiveai-sdk 2.0.8

ObjectiveAI SDK, definitions, and utilities
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
use super::*;

#[test]
fn manifest_minimal_roundtrip() {
    let m = Manifest {
        description: "tiny test plugin".to_string(),
        version: "0.1.0".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: Binaries::default(),
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    };
    let json = serde_json::to_value(&m).unwrap();
    // `skip_serializing_if = "Option::is_none"` keeps the wire shape lean.
    let obj = json.as_object().unwrap();
    assert_eq!(obj.len(), 2);
    assert_eq!(obj["description"], "tiny test plugin");
    assert_eq!(obj["version"], "0.1.0");
    // Roundtrip back.
    let back: Manifest = serde_json::from_value(json).unwrap();
    assert_eq!(back.description, "tiny test plugin");
    assert_eq!(back.version, "0.1.0");
    assert!(back.author.is_none());
    assert!(back.homepage.is_none());
    assert!(back.license.is_none());
    assert!(back.binaries.is_empty());
}

#[test]
fn manifest_full_roundtrip() {
    let m = Manifest {
        description: "Generate viral psyops content from a topic spec".to_string(),
        version: "0.3.1".to_string(),
        author: Some("Wiggidy".to_string()),
        homepage: Some("https://github.com/Wiggidy/psychological-operations".to_string()),
        license: Some("MIT".to_string()),
        binaries: Binaries::default(),
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    };
    let json = serde_json::to_value(&m).unwrap();
    let back: Manifest = serde_json::from_value(json).unwrap();
    assert_eq!(back.description, m.description);
    assert_eq!(back.version, m.version);
    assert_eq!(back.author, m.author);
    assert_eq!(back.homepage, m.homepage);
    assert_eq!(back.license, m.license);
}

#[test]
fn manifest_with_name_and_source_field_order() {
    let m = ManifestWithNameAndSource {
        name: "psyops".to_string(),
        manifest: Manifest {
            description: "do things".to_string(),
            version: "1.2.3".to_string(),
            author: Some("Wiggidy".to_string()),
            homepage: None,
            license: Some("MIT".to_string()),
            binaries: Binaries::default(),
            viewer_zip: None,
            viewer_url: None,
            viewer_routes: vec![],
            mobile_ready: false,
        },
        source: "/home/user/.objectiveai/plugins/psyops.manifest.json".to_string(),
    };
    let s = serde_json::to_string(&m).unwrap();
    // With preserve_order, name comes first, the flattened manifest
    // fields in declaration order, then source last. Optional `None`s
    // are skipped (homepage); empty `binaries` map is also skipped.
    let expected = concat!(
        r#"{"#,
        r#""name":"psyops","#,
        r#""description":"do things","#,
        r#""version":"1.2.3","#,
        r#""author":"Wiggidy","#,
        r#""license":"MIT","#,
        r#""source":"/home/user/.objectiveai/plugins/psyops.manifest.json""#,
        r#"}"#,
    );
    assert_eq!(s, expected);

    // Roundtrip back.
    let back: ManifestWithNameAndSource = serde_json::from_str(&s).unwrap();
    assert_eq!(back.name, "psyops");
    assert_eq!(back.manifest.description, "do things");
    assert_eq!(back.manifest.version, "1.2.3");
    assert_eq!(back.manifest.author.as_deref(), Some("Wiggidy"));
    assert!(back.manifest.homepage.is_none());
    assert_eq!(back.manifest.license.as_deref(), Some("MIT"));
    assert!(back.manifest.binaries.is_empty());
    assert_eq!(back.source, "/home/user/.objectiveai/plugins/psyops.manifest.json");
}

#[test]
fn manifest_deserializes_minimal_json() {
    let json = serde_json::json!({
        "description": "x",
        "version": "0.1.0"
    });
    let m: Manifest = serde_json::from_value(json).unwrap();
    assert_eq!(m.description, "x");
    assert_eq!(m.version, "0.1.0");
    assert!(m.author.is_none());
    assert!(m.homepage.is_none());
    assert!(m.license.is_none());
    assert!(m.binaries.is_empty());
}

fn full_binaries() -> Binaries {
    Binaries {
        linux_x86_64: Some("psyops-linux-x86_64".to_string()),
        linux_aarch64: Some("psyops-linux-aarch64".to_string()),
        windows_x86_64: Some("psyops-windows-x86_64.exe".to_string()),
        macos_aarch64: Some("psyops-macos-aarch64".to_string()),
        ..Default::default()
    }
}

#[test]
fn manifest_with_binaries_roundtrip() {
    let m = Manifest {
        description: "x".to_string(),
        version: "1.0.0".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: full_binaries(),
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    };
    let json = serde_json::to_value(&m).unwrap();
    let back: Manifest = serde_json::from_value(json).unwrap();
    assert_eq!(back.binaries.len(), 4);
    assert_eq!(back.binaries.get(Platform::LinuxX86_64).map(String::as_str), Some("psyops-linux-x86_64"));
    assert_eq!(back.binaries.get(Platform::LinuxAarch64).map(String::as_str), Some("psyops-linux-aarch64"));
    assert_eq!(back.binaries.get(Platform::WindowsX86_64).map(String::as_str), Some("psyops-windows-x86_64.exe"));
    assert_eq!(back.binaries.get(Platform::MacosAarch64).map(String::as_str), Some("psyops-macos-aarch64"));
}

#[test]
fn manifest_omits_empty_binaries_field() {
    let m = Manifest {
        description: "x".to_string(),
        version: "1.0.0".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: Binaries::default(),
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    };
    let json = serde_json::to_value(&m).unwrap();
    let obj = json.as_object().unwrap();
    assert!(!obj.contains_key("binaries"), "empty map should be skipped, got {obj:?}");
}

#[test]
fn manifest_deserializes_without_binaries_field() {
    let json = serde_json::json!({
        "description": "x",
        "version": "1.0.0"
    });
    let m: Manifest = serde_json::from_value(json).unwrap();
    assert!(m.binaries.is_empty());
}

#[test]
fn manifest_with_binaries_field_order() {
    let m = Manifest {
        description: "x".to_string(),
        version: "1.0.0".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: full_binaries(),
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    };
    let s = serde_json::to_string(&m).unwrap();
    // Field declaration order on `Binaries`: linux_x86_64, linux_aarch64,
    // windows_x86_64, windows_aarch64, macos_x86_64, macos_aarch64.
    // serde respects that for serialization.
    let i_lx = s.find("linux_x86_64").unwrap();
    let i_la = s.find("linux_aarch64").unwrap();
    let i_wx = s.find("windows_x86_64").unwrap();
    let i_ma = s.find("macos_aarch64").unwrap();
    assert!(i_lx < i_la, "linux_x86_64 should come before linux_aarch64: {s}");
    assert!(i_la < i_wx, "linux_aarch64 should come before windows_x86_64: {s}");
    assert!(i_wx < i_ma, "windows_x86_64 should come before macos_aarch64: {s}");

    let back: Manifest = serde_json::from_str(&s).unwrap();
    assert_eq!(back.binaries.linux_x86_64.as_deref(), Some("psyops-linux-x86_64"));
    assert_eq!(back.binaries.linux_aarch64.as_deref(), Some("psyops-linux-aarch64"));
    assert_eq!(back.binaries.windows_x86_64.as_deref(), Some("psyops-windows-x86_64.exe"));
    assert!(back.binaries.windows_aarch64.is_none());
    assert!(back.binaries.macos_x86_64.is_none());
    assert_eq!(back.binaries.macos_aarch64.as_deref(), Some("psyops-macos-aarch64"));
}

#[test]
fn manifest_with_sparse_binaries_is_valid() {
    // Plugin that only ships a Linux x86_64 binary.
    let m = Manifest {
        description: "linux-only plugin".to_string(),
        version: "0.1.0".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: Binaries {
            linux_x86_64: Some("psyops-linux-x86_64".to_string()),
            ..Default::default()
        },
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    };
    let s = serde_json::to_string(&m).unwrap();
    let back: Manifest = serde_json::from_str(&s).unwrap();
    assert_eq!(back.binaries.len(), 1);
    assert_eq!(back.binaries.get(Platform::LinuxX86_64).map(String::as_str), Some("psyops-linux-x86_64"));
    assert!(back.binaries.get(Platform::LinuxAarch64).is_none());
    assert!(back.binaries.get(Platform::WindowsX86_64).is_none());
    assert!(back.binaries.get(Platform::WindowsAarch64).is_none());
    assert!(back.binaries.get(Platform::MacosX86_64).is_none());
    assert!(back.binaries.get(Platform::MacosAarch64).is_none());
}

#[test]
fn manifest_with_viewer_fields_roundtrip() {
    let m = Manifest {
        description: "viewer plugin".to_string(),
        version: "1.0.0".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: Binaries::default(),
        viewer_zip: Some("psyops-viewer.zip".to_string()),
        viewer_url: None,
        viewer_routes: vec![
            ViewerRoute {
                path: "/say".to_string(),
                method: HttpMethod::Post,
                r#type: "say_request".to_string(),
            },
            ViewerRoute {
                path: "/status".to_string(),
                method: HttpMethod::Get,
                r#type: "status_request".to_string(),
            },
        ],
        mobile_ready: true,
    };
    let json = serde_json::to_value(&m).unwrap();
    let back: Manifest = serde_json::from_value(json.clone()).unwrap();
    assert_eq!(back.viewer_zip.as_deref(), Some("psyops-viewer.zip"));
    assert_eq!(back.viewer_routes.len(), 2);
    assert_eq!(back.viewer_routes[0].path, "/say");
    assert_eq!(back.viewer_routes[0].method, HttpMethod::Post);
    assert_eq!(back.viewer_routes[0].r#type, "say_request");
    assert_eq!(back.viewer_routes[1].method, HttpMethod::Get);
    assert!(back.mobile_ready);

    // The two viewer routes should serialize methods as uppercase strings.
    let routes_json = json.get("viewer_routes").unwrap();
    assert_eq!(routes_json[0]["method"], "POST");
    assert_eq!(routes_json[1]["method"], "GET");
}

#[test]
fn manifest_omits_viewer_fields_when_absent() {
    let m = Manifest {
        description: "x".to_string(),
        version: "1.0.0".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: Binaries::default(),
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    };
    let json = serde_json::to_value(&m).unwrap();
    let obj = json.as_object().unwrap();
    assert!(!obj.contains_key("viewer_zip"));
    assert!(!obj.contains_key("viewer_routes"));
    assert!(!obj.contains_key("mobile_ready"));
}

#[test]
fn manifest_deserializes_without_viewer_fields() {
    let json = serde_json::json!({
        "description": "x",
        "version": "1.0.0"
    });
    let m: Manifest = serde_json::from_value(json).unwrap();
    assert!(m.viewer_zip.is_none());
    assert!(m.viewer_routes.is_empty());
    assert!(!m.mobile_ready);
}

#[test]
fn http_method_serializes_uppercase() {
    let cases = [
        (HttpMethod::Get, "\"GET\""),
        (HttpMethod::Post, "\"POST\""),
        (HttpMethod::Put, "\"PUT\""),
        (HttpMethod::Patch, "\"PATCH\""),
        (HttpMethod::Delete, "\"DELETE\""),
    ];
    for (m, expected) in cases {
        let got = serde_json::to_string(&m).unwrap();
        assert_eq!(got, expected);
        let back: HttpMethod = serde_json::from_str(&got).unwrap();
        assert_eq!(back, m);
    }
}

// Helper: a minimal Manifest with no viewer source set. Tests below
// override one or both viewer fields before calling `validate`.
fn manifest_without_viewer() -> Manifest {
    Manifest {
        description: "t".to_string(),
        version: "0.0.1".to_string(),
        author: None,
        homepage: None,
        license: None,
        binaries: Binaries::default(),
        viewer_zip: None,
        viewer_url: None,
        viewer_routes: vec![],
        mobile_ready: false,
    }
}

#[test]
fn has_viewer_returns_true_for_either_source() {
    let mut m = manifest_without_viewer();
    assert!(!m.has_viewer());

    m.viewer_zip = Some("v.zip".to_string());
    assert!(m.has_viewer());
    m.viewer_zip = None;

    m.viewer_url = Some("https://x.example.com".to_string());
    assert!(m.has_viewer());
}

#[test]
fn manifest_validate_rejects_both_viewer_sources() {
    let mut m = manifest_without_viewer();
    m.viewer_zip = Some("v.zip".to_string());
    m.viewer_url = Some("https://x.example.com".to_string());
    let err = m.validate().unwrap_err();
    assert!(err.contains("mutually exclusive"), "got {err:?}");
}

#[test]
fn manifest_validate_accepts_viewer_url_only() {
    let mut m = manifest_without_viewer();
    m.viewer_url = Some("https://x.example.com".to_string());
    assert!(m.validate().is_ok());
}

#[test]
fn manifest_validate_accepts_viewer_zip_only() {
    let mut m = manifest_without_viewer();
    m.viewer_zip = Some("v.zip".to_string());
    assert!(m.validate().is_ok());
}

#[test]
fn manifest_validate_accepts_no_viewer_source() {
    let m = manifest_without_viewer();
    assert!(m.validate().is_ok());
}

#[test]
fn manifest_validate_accepts_localhost_http() {
    for url in [
        "http://localhost",
        "http://localhost:5173",
        "http://localhost:5173/foo/bar?q=1#frag",
        "http://127.0.0.1",
        "http://127.0.0.1:8080/index.html",
    ] {
        let mut m = manifest_without_viewer();
        m.viewer_url = Some(url.to_string());
        assert!(m.validate().is_ok(), "expected ok for {url:?}");
    }
}

#[test]
fn manifest_validate_rejects_non_localhost_http() {
    for url in [
        "http://example.com",
        "http://evil.example.com:8080",
        "http://1.2.3.4",
    ] {
        let mut m = manifest_without_viewer();
        m.viewer_url = Some(url.to_string());
        let err = m.validate().unwrap_err();
        assert!(err.contains("localhost"), "got {err:?} for {url:?}");
    }
}

#[test]
fn manifest_validate_rejects_other_schemes() {
    for url in [
        "ftp://example.com",
        "file:///tmp/x",
        "javascript:alert(1)",
        "",
    ] {
        let mut m = manifest_without_viewer();
        m.viewer_url = Some(url.to_string());
        assert!(m.validate().is_err(), "expected err for {url:?}");
    }
}

#[test]
fn manifest_with_viewer_url_serde_roundtrip() {
    let mut m = manifest_without_viewer();
    m.viewer_url = Some("https://plugin.example.com/index.html".to_string());
    let json = serde_json::to_value(&m).unwrap();
    assert_eq!(
        json["viewer_url"],
        serde_json::json!("https://plugin.example.com/index.html")
    );
    assert!(!json.as_object().unwrap().contains_key("viewer_zip"));
    let back: Manifest = serde_json::from_value(json).unwrap();
    assert_eq!(back.viewer_url, m.viewer_url);
    assert!(back.viewer_zip.is_none());
    assert!(back.has_viewer());
}