tandem-server 0.4.25

HTTP server for Tandem engine APIs
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
use super::*;

#[tokio::test]
async fn packs_detect_requires_root_marker() {
    let state = test_state().await;
    let mut rx = state.event_bus.subscribe();
    let root = std::env::temp_dir().join(format!("tandem-pack-detect-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&root).expect("mkdir");
    let plain_zip = root.join("plain.zip");
    write_pack_zip(
        &plain_zip,
        "name: detect-test\nversion: 1.0.0\ntype: skill\npack_id: detect-test\n",
    );
    let app = app_router(state);
    let req = Request::builder()
        .method("POST")
        .uri("/packs/detect")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "path": plain_zip.to_string_lossy()
            })
            .to_string(),
        ))
        .expect("request");
    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(payload.get("is_pack").and_then(|v| v.as_bool()), Some(true));
    let event = next_event_of_type(&mut rx, "pack.detected").await;
    assert_eq!(
        event.properties.get("marker").and_then(|v| v.as_str()),
        Some("tandempack.yaml")
    );
    assert_eq!(
        event.properties.get("path").and_then(|v| v.as_str()),
        Some(plain_zip.to_string_lossy().as_ref())
    );
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn packs_detect_returns_false_without_marker() {
    let state = test_state().await;
    let root = std::env::temp_dir().join(format!("tandem-pack-detect-none-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&root).expect("mkdir");
    let plain_zip = root.join("plain.zip");
    write_plain_zip_without_marker(&plain_zip);
    let app = app_router(state);
    let req = Request::builder()
        .method("POST")
        .uri("/packs/detect")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "path": plain_zip.to_string_lossy()
            })
            .to_string(),
        ))
        .expect("request");
    let resp = app.oneshot(req).await.expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(
        payload.get("is_pack").and_then(|v| v.as_bool()),
        Some(false)
    );
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn packs_install_list_and_uninstall_roundtrip() {
    let state = test_state().await;
    let mut rx = state.event_bus.subscribe();
    let root = std::env::temp_dir().join(format!("tandem-pack-install-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&root).expect("mkdir");
    let pack_zip = root.join("pack.zip");
    write_pack_zip(
        &pack_zip,
        "name: roundtrip-pack\nversion: 1.2.3\ntype: workflow\npack_id: roundtrip-pack\n",
    );
    let app = app_router(state.clone());
    let install_req = Request::builder()
        .method("POST")
        .uri("/packs/install")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "path": pack_zip.to_string_lossy(),
                "source": {"kind":"test"}
            })
            .to_string(),
        ))
        .expect("request");
    let install_resp = app.clone().oneshot(install_req).await.expect("response");
    assert_eq!(install_resp.status(), StatusCode::OK);
    let install_body = to_bytes(install_resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let install_payload: Value = serde_json::from_slice(&install_body).expect("json");
    let install_path = install_payload
        .get("installed")
        .and_then(|v| v.get("install_path"))
        .and_then(|v| v.as_str())
        .unwrap_or("");
    assert!(install_path.ends_with("/roundtrip-pack/1.2.3"));
    let current_pointer = std::path::PathBuf::from(install_path)
        .parent()
        .expect("parent")
        .join("current");
    let current_version = std::fs::read_to_string(&current_pointer).expect("read current");
    assert_eq!(current_version.trim(), "1.2.3");
    let started = next_event_of_type(&mut rx, "pack.install.started").await;
    assert_eq!(
        started.properties.get("path").and_then(|v| v.as_str()),
        Some(pack_zip.to_string_lossy().as_ref())
    );
    let succeeded = next_event_of_type(&mut rx, "pack.install.succeeded").await;
    assert_eq!(
        succeeded.properties.get("pack_id").and_then(|v| v.as_str()),
        Some("roundtrip-pack")
    );
    let registry = next_event_of_type(&mut rx, "registry.updated").await;
    assert_eq!(
        registry.properties.get("entity").and_then(|v| v.as_str()),
        Some("packs")
    );

    let list_req = Request::builder()
        .method("GET")
        .uri("/packs")
        .body(Body::empty())
        .expect("request");
    let list_resp = app.clone().oneshot(list_req).await.expect("response");
    assert_eq!(list_resp.status(), StatusCode::OK);
    let list_body = to_bytes(list_resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let list_payload: Value = serde_json::from_slice(&list_body).expect("json");
    let packs = list_payload
        .get("packs")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    assert!(packs.iter().any(|p| {
        p.get("pack_id").and_then(|v| v.as_str()) == Some("roundtrip-pack")
            && p.get("version").and_then(|v| v.as_str()) == Some("1.2.3")
    }));

    let uninstall_req = Request::builder()
        .method("POST")
        .uri("/packs/uninstall")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "pack_id": "roundtrip-pack",
                "version": "1.2.3"
            })
            .to_string(),
        ))
        .expect("request");
    let uninstall_resp = app.clone().oneshot(uninstall_req).await.expect("response");
    assert_eq!(uninstall_resp.status(), StatusCode::OK);
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn packs_updates_endpoints_return_stub_payload() {
    let state = test_state().await;
    let root = std::env::temp_dir().join(format!("tandem-pack-updates-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&root).expect("mkdir");
    let pack_zip = root.join("pack.zip");
    write_pack_zip(
        &pack_zip,
        "name: update-pack\nversion: 1.0.0\ntype: workflow\npack_id: update-pack\n",
    );
    let app = app_router(state.clone());
    let install_req = Request::builder()
        .method("POST")
        .uri("/packs/install")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "path": pack_zip.to_string_lossy(),
                "source": {"kind":"test"}
            })
            .to_string(),
        ))
        .expect("request");
    let install_resp = app.clone().oneshot(install_req).await.expect("response");
    assert_eq!(install_resp.status(), StatusCode::OK);

    let updates_req = Request::builder()
        .method("GET")
        .uri("/packs/update-pack/updates")
        .body(Body::empty())
        .expect("request");
    let updates_resp = app.clone().oneshot(updates_req).await.expect("response");
    assert_eq!(updates_resp.status(), StatusCode::OK);
    let updates_body = to_bytes(updates_resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let updates_payload: Value = serde_json::from_slice(&updates_body).expect("json");
    assert_eq!(
        updates_payload
            .get("current_version")
            .and_then(|v| v.as_str()),
        Some("1.0.0")
    );
    assert_eq!(
        updates_payload
            .get("reapproval_required")
            .and_then(|v| v.as_bool()),
        Some(false)
    );
    assert!(updates_payload
        .get("permissions_diff")
        .and_then(|v| v.as_object())
        .is_some());

    let apply_req = Request::builder()
        .method("POST")
        .uri("/packs/update-pack/update")
        .header("content-type", "application/json")
        .body(Body::from(json!({"target_version":"1.1.0"}).to_string()))
        .expect("request");
    let apply_resp = app.clone().oneshot(apply_req).await.expect("response");
    assert_eq!(apply_resp.status(), StatusCode::OK);
    let apply_body = to_bytes(apply_resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let apply_payload: Value = serde_json::from_slice(&apply_body).expect("json");
    assert_eq!(
        apply_payload.get("reason").and_then(|v| v.as_str()),
        Some("updates_not_implemented")
    );
    assert_eq!(
        apply_payload
            .get("reapproval_required")
            .and_then(|v| v.as_bool()),
        Some(false)
    );
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn packs_get_reports_workflow_extensions() {
    let state = test_state().await;
    let root = std::env::temp_dir().join(format!("tandem-pack-inspect-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&root).expect("mkdir");
    let pack_zip = root.join("inspect-pack.zip");
    let file = std::fs::File::create(&pack_zip).expect("create zip");
    let mut zip = zip::ZipWriter::new(file);
    let opts = zip::write::SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Deflated);
    zip.start_file("tandempack.yaml", opts).expect("manifest");
    std::io::Write::write_all(
        &mut zip,
        br#"name: workflow-inspect-pack
version: 1.0.0
type: workflow
pack_id: workflow-inspect-pack
entrypoints:
  workflows:
    - build_feature
contents:
  workflows:
    - id: build_feature
      path: workflows/build_feature.yaml
  workflow_hooks:
    - id: build_feature.task_completed.notify
      path: hooks/notify.yaml
"#,
    )
    .expect("write manifest");
    zip.start_file("workflows/build_feature.yaml", opts)
        .expect("workflow file");
    std::io::Write::write_all(
        &mut zip,
        b"workflow:\n  id: build_feature\n  name: Build Feature\n  steps:\n    - planner\n",
    )
    .expect("write workflow file");
    zip.start_file("hooks/notify.yaml", opts)
        .expect("hook file");
    std::io::Write::write_all(
        &mut zip,
        b"hooks:\n  - id: build_feature.task_completed.notify\n    workflow_id: build_feature\n    event: task_completed\n    actions:\n      - slack.notify\n",
    )
    .expect("write hook file");
    zip.finish().expect("finish zip");

    let app = app_router(state.clone());
    let install_resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/packs/install")
                .header("content-type", "application/json")
                .body(Body::from(
                    json!({
                        "path": pack_zip.to_string_lossy(),
                        "source": {"kind":"test"}
                    })
                    .to_string(),
                ))
                .expect("install request"),
        )
        .await
        .expect("install response");
    assert_eq!(install_resp.status(), StatusCode::OK);

    let inspect_resp = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/packs/workflow-inspect-pack")
                .body(Body::empty())
                .expect("inspect request"),
        )
        .await
        .expect("inspect response");
    assert_eq!(inspect_resp.status(), StatusCode::OK);
    let body = to_bytes(inspect_resp.into_body(), usize::MAX)
        .await
        .expect("inspect body");
    let payload: Value = serde_json::from_slice(&body).expect("inspect json");
    assert_eq!(
        payload["pack"]["workflow_extensions"]["workflow_count"].as_u64(),
        Some(1)
    );
    assert_eq!(
        payload["pack"]["workflow_extensions"]["workflow_hook_count"].as_u64(),
        Some(1)
    );
    assert_eq!(
        payload["pack"]["workflow_extensions"]["workflow_entrypoints"]
            .as_array()
            .map(|rows| rows.len()),
        Some(1)
    );
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn packs_install_with_marketplace_assets_exposes_files() {
    let state = test_state().await;
    let root = std::env::temp_dir().join(format!("tandem-pack-marketplace-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&root).expect("mkdir");
    let pack_zip = root.join("marketplace-pack.zip");
    write_pack_zip_with_entries(
        &pack_zip,
        r#"
manifest_schema_version: 1
pack_id: marketplace-pack
name: marketplace-pack
version: 1.0.0
type: workflow
engine:
  requires: ">=0.9.0 <2.0.0"
marketplace:
  publisher:
    publisher_id: pub_tandem_official
    display_name: Tandem
    verification_tier: official
  listing:
    display_name: Marketplace Pack
    description: Marketplace pack used for regression coverage.
    categories: ["testing"]
    tags: ["marketplace", "pack"]
    license_spdx: Apache-2.0
    icon: resources/marketplace/icon.svg
    screenshots: ["resources/marketplace/screenshot-1.svg"]
    changelog: CHANGELOG.md
entrypoints:
  workflows:
    - marketplace_workflow
capabilities:
  required: []
  optional: []
  non_portable: []
contents:
  workflows:
    - id: marketplace_workflow
      path: workflows/marketplace-workflow.yaml
"#,
        &[
            ("README.md", "# Marketplace Pack\n\nThis pack ships assets."),
            ("CHANGELOG.md", "## 1.0.0\n- Initial marketplace package"),
            (
                "resources/marketplace/icon.svg",
                "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 32 32\"><rect width=\"32\" height=\"32\" rx=\"6\" fill=\"#1d4ed8\"/></svg>",
            ),
            (
                "resources/marketplace/screenshot-1.svg",
                "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 32 32\"><rect width=\"32\" height=\"32\" rx=\"6\" fill=\"#0f172a\"/></svg>",
            ),
            (
                "workflows/marketplace-workflow.yaml",
                "workflow:\n  id: marketplace_workflow\n  name: Marketplace Workflow\n  steps:\n    - action: agent:project-manager\n      with:\n        prompt: Hello from marketplace\n",
            ),
        ],
    );
    let app = app_router(state.clone());
    let install_req = Request::builder()
        .method("POST")
        .uri("/packs/install")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "path": pack_zip.to_string_lossy(),
                "source": {"kind":"test"}
            })
            .to_string(),
        ))
        .expect("request");
    let install_resp = app.clone().oneshot(install_req).await.expect("response");
    assert_eq!(install_resp.status(), StatusCode::OK);

    let file_req = Request::builder()
        .method("GET")
        .uri("/packs/marketplace-pack/files/README.md")
        .body(Body::empty())
        .expect("request");
    let file_resp = app.clone().oneshot(file_req).await.expect("response");
    assert_eq!(file_resp.status(), StatusCode::OK);
    assert_eq!(
        file_resp
            .headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok()),
        Some("text/markdown; charset=utf-8")
    );
    let file_body = to_bytes(file_resp.into_body(), usize::MAX)
        .await
        .expect("body");
    let body_text = String::from_utf8(file_body.to_vec()).expect("utf8");
    assert!(body_text.contains("Marketplace Pack"));
    let _ = std::fs::remove_dir_all(root);
}

#[tokio::test]
async fn packs_install_rejects_missing_marketplace_assets() {
    let state = test_state().await;
    let root = std::env::temp_dir().join(format!(
        "tandem-pack-marketplace-missing-{}",
        Uuid::new_v4()
    ));
    std::fs::create_dir_all(&root).expect("mkdir");
    let pack_zip = root.join("marketplace-pack-missing.zip");
    write_pack_zip_with_entries(
        &pack_zip,
        r#"
manifest_schema_version: 1
pack_id: marketplace-pack-missing
name: marketplace-pack-missing
version: 1.0.0
type: workflow
engine:
  requires: ">=0.9.0 <2.0.0"
marketplace:
  publisher:
    publisher_id: pub_tandem_official
    display_name: Tandem
    verification_tier: official
  listing:
    display_name: Marketplace Pack Missing
    description: Missing asset coverage test.
    categories: ["testing"]
    tags: ["marketplace", "pack"]
    license_spdx: Apache-2.0
    icon: resources/marketplace/icon.svg
    screenshots: ["resources/marketplace/screenshot-1.svg"]
    changelog: CHANGELOG.md
entrypoints:
  workflows:
    - marketplace_workflow
capabilities:
  required: []
  optional: []
  non_portable: []
contents:
  workflows:
    - id: marketplace_workflow
      path: workflows/marketplace-workflow.yaml
"#,
        &[
            ("README.md", "# Marketplace Pack"),
            ("CHANGELOG.md", "## 1.0.0\n- Initial marketplace package"),
            (
                "workflows/marketplace-workflow.yaml",
                "workflow:\n  id: marketplace_workflow\n  name: Marketplace Workflow\n  steps:\n    - action: agent:project-manager\n      with:\n        prompt: Hello from marketplace\n",
            ),
        ],
    );
    let app = app_router(state);
    let install_req = Request::builder()
        .method("POST")
        .uri("/packs/install")
        .header("content-type", "application/json")
        .body(Body::from(
            json!({
                "path": pack_zip.to_string_lossy(),
                "source": {"kind":"test"}
            })
            .to_string(),
        ))
        .expect("request");
    let install_resp = app.oneshot(install_req).await.expect("response");
    assert_eq!(install_resp.status(), StatusCode::BAD_REQUEST);
    let _ = std::fs::remove_dir_all(root);
}