cviz 2.0.2

A CLI tool to visualize WebAssembly component composition structure.
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
/// Shared graph builders for unit tests across output modules.
///
/// Each builder constructs a [`CompositionGraph`] representing a specific
/// topology.  Instance indices are assigned as follows so tests can refer
/// to them by number:
///
///   0  — the implicit "host" (any import whose source_instance is 0 and
///         has no corresponding graph node is later marked as a host import
///         by the parser's postprocess step; in unit tests we set
///         `is_host_import` directly instead).
///
/// All builders use consecutive indices starting at 1 for real components.
use crate::model::{
    ComponentNode, CompositionGraph, FuncSignature, InstanceInterface, InterfaceConnection,
    InterfaceType, ValueType,
};
use std::collections::BTreeMap;

// ---------------------------------------------------------------------------
// Simple chain: host → $srv$middleware → export(wasi:http/handler)
//
//   idx 1  $srv       — imports handler from host
//   idx 2  $middleware — imports handler from $srv, imports log from host
//   export wasi:http/handler@0.3.0 from idx 2
// ---------------------------------------------------------------------------
pub(crate) fn simple_chain_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    let mut srv = ComponentNode::new("$srv".to_string(), 0, 0);
    srv.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(1, srv);

    let mut mw = ComponentNode::new("$middleware".to_string(), 1, 1);
    mw.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    mw.add_import(InterfaceConnection {
        interface_name: "wasi:logging/log@0.1.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(2, mw);

    graph.add_export("wasi:http/handler@0.3.0".to_string(), 2, None);
    graph
}

// ---------------------------------------------------------------------------
// Two independent chains:
//
//   idx 1  $srv-http  — imports wasi:http/handler from host
//   idx 2  $mw-http   — imports wasi:http/handler from $srv-http
//   export wasi:http/handler@0.3.0 from idx 2
//
//   idx 3  $db        — imports wasi:keyvalue/store from host
//   idx 4  $cache     — imports wasi:keyvalue/store from $db
//   export wasi:keyvalue/store@0.1.0 from idx 4
// ---------------------------------------------------------------------------
pub(crate) fn two_chain_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    // — HTTP chain —
    let mut srv_http = ComponentNode::new("$srv-http".to_string(), 0, 0);
    srv_http.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(1, srv_http);

    let mut mw_http = ComponentNode::new("$mw-http".to_string(), 1, 1);
    mw_http.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(2, mw_http);

    graph.add_export("wasi:http/handler@0.3.0".to_string(), 2, None);

    // — Keyvalue chain —
    let mut db = ComponentNode::new("$db".to_string(), 2, 2);
    db.add_import(InterfaceConnection {
        interface_name: "wasi:keyvalue/store@0.1.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(3, db);

    let mut cache = ComponentNode::new("$cache".to_string(), 3, 3);
    cache.add_import(InterfaceConnection {
        interface_name: "wasi:keyvalue/store@0.1.0".to_string(),
        source_instance: Some(3),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(4, cache);

    graph.add_export("wasi:keyvalue/store@0.1.0".to_string(), 4, None);

    graph
}

// ---------------------------------------------------------------------------
// Three-node chain using a non-http interface (wasi:messaging/consumer) to
// demonstrate that chain detection is generic.
//
//   idx 1  $backend  — imports wasi:messaging/consumer from host
//   idx 2  $service  — imports wasi:messaging/consumer from $backend
//   idx 3  $gateway  — imports wasi:messaging/consumer from $service
//   export wasi:messaging/consumer@0.2.0 from idx 3
//
// Request-flow order: $gateway$service$backend
// ---------------------------------------------------------------------------
pub(crate) fn long_chain_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    let mut backend = ComponentNode::new("$backend".to_string(), 0, 0);
    backend.add_import(InterfaceConnection {
        interface_name: "wasi:messaging/consumer@0.2.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(1, backend);

    let mut service = ComponentNode::new("$service".to_string(), 1, 1);
    service.add_import(InterfaceConnection {
        interface_name: "wasi:messaging/consumer@0.2.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(2, service);

    let mut gateway = ComponentNode::new("$gateway".to_string(), 2, 2);
    gateway.add_import(InterfaceConnection {
        interface_name: "wasi:messaging/consumer@0.2.0".to_string(),
        source_instance: Some(2),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(3, gateway);

    graph.add_export("wasi:messaging/consumer@0.2.0".to_string(), 3, None);
    graph
}

// ---------------------------------------------------------------------------
// Chain plus a utility node that isn't part of any chain:
//
//   idx 1  $srv         — imports wasi:http/handler from host
//   idx 2  $middleware  — imports wasi:http/handler from $srv
//   idx 3  $logger      — imports wasi:logging/log from host only
//   export wasi:http/handler@0.3.0 from idx 2
//
// $logger has no inter-component connections so it does NOT form a chain.
// It should appear in AllInterfaces/Full but NOT in HandlerChain.
// ---------------------------------------------------------------------------
pub(crate) fn chain_plus_utility_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    let mut srv = ComponentNode::new("$srv".to_string(), 0, 0);
    srv.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(1, srv);

    let mut mw = ComponentNode::new("$middleware".to_string(), 1, 1);
    mw.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(2, mw);

    let mut logger = ComponentNode::new("$logger".to_string(), 2, 2);
    logger.add_import(InterfaceConnection {
        interface_name: "wasi:logging/log@0.1.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(3, logger);

    graph.add_export("wasi:http/handler@0.3.0".to_string(), 2, None);
    graph
}

// ---------------------------------------------------------------------------
// Type-annotated simple chain (same topology as simple_chain_graph but with
// type info on all connections for type-display tests).
//
// Both connections carry `handle(u32) -> bool`.
// ---------------------------------------------------------------------------
pub(crate) fn typed_chain_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    let u32_id = graph.arena.intern_val(ValueType::U32);
    let bool_id = graph.arena.intern_val(ValueType::Bool);

    let handle_sig = FuncSignature {
        is_async: false,
        param_names: vec![],
        params: vec![u32_id],
        results: vec![bool_id],
    };
    let mut fns = BTreeMap::new();
    fns.insert("handle".to_string(), handle_sig);
    let iface_type = InterfaceType::Instance(InstanceInterface {
        functions: fns,
        type_exports: BTreeMap::new(),
    });

    let mut srv = ComponentNode::new("$srv".to_string(), 0, 0);
    srv.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: Some(iface_type.clone()),
        fingerprint: Some(iface_type.fingerprint(&graph.arena)),
    });
    graph.add_node(1, srv);

    let mut mw = ComponentNode::new("$middleware".to_string(), 1, 1);
    mw.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: Some(iface_type.clone()),
        fingerprint: Some(iface_type.fingerprint(&graph.arena)),
    });
    graph.add_node(2, mw);

    graph.add_export("wasi:http/handler@0.3.0".to_string(), 2, Some(iface_type));
    graph
}

// ---------------------------------------------------------------------------
// Two distinct typed chains — handler uses handle(u32)->bool,
// keyvalue uses get(string)->string.  Tests that different types get
// different symbols in the SymbolMap.
// ---------------------------------------------------------------------------
pub(crate) fn two_typed_chain_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    // handler type: handle(u32) -> bool
    let u32_id = graph.arena.intern_val(ValueType::U32);
    let bool_id = graph.arena.intern_val(ValueType::Bool);
    let handler_sig = FuncSignature {
        is_async: false,
        param_names: vec![],
        params: vec![u32_id],
        results: vec![bool_id],
    };
    let mut handler_fns = BTreeMap::new();
    handler_fns.insert("handle".to_string(), handler_sig);
    let handler_type = InterfaceType::Instance(InstanceInterface {
        functions: handler_fns,
        type_exports: BTreeMap::new(),
    });

    // keyvalue type: get(string) -> string
    let str_id = graph.arena.intern_val(ValueType::String);
    let get_sig = FuncSignature {
        is_async: false,
        param_names: vec![],
        params: vec![str_id],
        results: vec![str_id],
    };
    let mut kv_fns = BTreeMap::new();
    kv_fns.insert("get".to_string(), get_sig);
    let kv_type = InterfaceType::Instance(InstanceInterface {
        functions: kv_fns,
        type_exports: BTreeMap::new(),
    });

    // HTTP chain
    let mut srv_http = ComponentNode::new("$srv-http".to_string(), 0, 0);
    srv_http.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: Some(handler_type.clone()),
        fingerprint: Some(handler_type.fingerprint(&graph.arena)),
    });
    graph.add_node(1, srv_http);

    let mut mw_http = ComponentNode::new("$mw-http".to_string(), 1, 1);
    mw_http.add_import(InterfaceConnection {
        interface_name: "wasi:http/handler@0.3.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: Some(handler_type.clone()),
        fingerprint: Some(handler_type.fingerprint(&graph.arena)),
    });
    graph.add_node(2, mw_http);

    graph.add_export("wasi:http/handler@0.3.0".to_string(), 2, Some(handler_type));

    // Keyvalue chain
    let mut db = ComponentNode::new("$db".to_string(), 2, 2);
    db.add_import(InterfaceConnection {
        interface_name: "wasi:keyvalue/store@0.1.0".to_string(),
        source_instance: None,
        is_host_import: true,
        interface_type: Some(kv_type.clone()),
        fingerprint: Some(kv_type.fingerprint(&graph.arena)),
    });
    graph.add_node(3, db);

    let mut cache = ComponentNode::new("$cache".to_string(), 3, 3);
    cache.add_import(InterfaceConnection {
        interface_name: "wasi:keyvalue/store@0.1.0".to_string(),
        source_instance: Some(3),
        is_host_import: false,
        interface_type: Some(kv_type.clone()),
        fingerprint: Some(kv_type.fingerprint(&graph.arena)),
    });
    graph.add_node(4, cache);

    graph.add_export("wasi:keyvalue/store@0.1.0".to_string(), 4, Some(kv_type));

    graph
}

// ---------------------------------------------------------------------------
// Shim-export topologies (WAC-compiled style).
//
// In WAC-compiled compositions the exported interface is backed by a synthetic
// shim instance whose constructor arguments are individual function refs rather
// than instance refs.  The parser therefore records no interface imports on the
// shim node, and `get_chain_for` must fall back to the inter-component import
// graph to reconstruct the provider chain.
//
// All three builders export "test:svc/api@1.0.0" from a shim node (highest
// idx) that has no imports for that interface.
// ---------------------------------------------------------------------------

// Direct (no middleware):
//
//   idx 1  $base      — base provider, no inter-component imports for api
//   idx 2  $consumer  — imports api from $base (terminal consumer)
//   idx 3  $shim      — export source, no api imports
//   export test:svc/api@1.0.0 from idx 3
//
// Expected chain: [1]
pub(crate) fn shim_export_direct_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    let base = ComponentNode::new("$base".to_string(), 0, 0);
    graph.add_node(1, base);

    let mut consumer = ComponentNode::new("$consumer".to_string(), 1, 1);
    consumer.add_import(InterfaceConnection {
        interface_name: "test:svc/api@1.0.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(2, consumer);

    let shim = ComponentNode::new("$shim".to_string(), 2, 2);
    graph.add_node(3, shim);

    graph.add_export("test:svc/api@1.0.0".to_string(), 3, None);
    graph
}

// One middleware:
//
//   idx 1  $base       — base provider
//   idx 2  $middleware — imports api from $base
//   idx 3  $consumer   — imports api from $middleware (terminal consumer)
//   idx 4  $shim       — export source, no api imports
//   export test:svc/api@1.0.0 from idx 4
//
// Expected chain: [2, 1]
pub(crate) fn shim_export_one_middleware_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    let base = ComponentNode::new("$base".to_string(), 0, 0);
    graph.add_node(1, base);

    let mut middleware = ComponentNode::new("$middleware".to_string(), 1, 1);
    middleware.add_import(InterfaceConnection {
        interface_name: "test:svc/api@1.0.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(2, middleware);

    let mut consumer = ComponentNode::new("$consumer".to_string(), 2, 2);
    consumer.add_import(InterfaceConnection {
        interface_name: "test:svc/api@1.0.0".to_string(),
        source_instance: Some(2),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(3, consumer);

    let shim = ComponentNode::new("$shim".to_string(), 3, 3);
    graph.add_node(4, shim);

    graph.add_export("test:svc/api@1.0.0".to_string(), 4, None);
    graph
}

// Three middlewares (deepest chain):
//
//   idx 1  $base     — base provider
//   idx 2  $mdl-c    — imports api from $base
//   idx 3  $mdl-b    — imports api from $mdl-c
//   idx 4  $mdl-a    — imports api from $mdl-b
//   idx 5  $consumer — imports api from $mdl-a (terminal consumer)
//   idx 6  $shim     — export source, no api imports
//   export test:svc/api@1.0.0 from idx 6
//
// Expected chain: [4, 3, 2, 1]
pub(crate) fn shim_export_three_middleware_graph() -> CompositionGraph {
    let mut graph = CompositionGraph::new();

    let base = ComponentNode::new("$base".to_string(), 0, 0);
    graph.add_node(1, base);

    let mut mdl_c = ComponentNode::new("$mdl-c".to_string(), 1, 1);
    mdl_c.add_import(InterfaceConnection {
        interface_name: "test:svc/api@1.0.0".to_string(),
        source_instance: Some(1),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(2, mdl_c);

    let mut mdl_b = ComponentNode::new("$mdl-b".to_string(), 2, 2);
    mdl_b.add_import(InterfaceConnection {
        interface_name: "test:svc/api@1.0.0".to_string(),
        source_instance: Some(2),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(3, mdl_b);

    let mut mdl_a = ComponentNode::new("$mdl-a".to_string(), 3, 3);
    mdl_a.add_import(InterfaceConnection {
        interface_name: "test:svc/api@1.0.0".to_string(),
        source_instance: Some(3),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(4, mdl_a);

    let mut consumer = ComponentNode::new("$consumer".to_string(), 4, 4);
    consumer.add_import(InterfaceConnection {
        interface_name: "test:svc/api@1.0.0".to_string(),
        source_instance: Some(4),
        is_host_import: false,
        interface_type: None,
        fingerprint: None,
    });
    graph.add_node(5, consumer);

    let shim = ComponentNode::new("$shim".to_string(), 5, 5);
    graph.add_node(6, shim);

    graph.add_export("test:svc/api@1.0.0".to_string(), 6, None);
    graph
}