nominal 0.5.1

Automate Nominal workflows in Rust
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
use std::sync::Arc;

use chrono::{DateTime, Utc};
use conjure_http::client::{AsyncService, ConjureRuntime};
use conjure_object::BearerToken;
use conjure_runtime::Client;
use futures::Stream;
use nominal_api::clients::scout::assets::{AsyncAssetService, AsyncAssetServiceClient};
use nominal_api::objects::api::{
    Label, PropertyName, PropertyValue, SetOperator, TagName, TagValue,
};
use nominal_api::objects::scout::asset::api::{
    AddDataScopesToAssetRequest, AssetSortField, AssetSortOptions, CreateAssetDataScope,
    CreateAssetRequest, SearchAssetsQuery, SearchAssetsRequest, SearchAssetsResponse, SortKey,
    UpdateAssetRequest,
};
use nominal_api::objects::scout::rids::api::{LabelsFilter, PropertiesFilter};
use std::collections::{BTreeMap, BTreeSet, HashMap};

use crate::core::datasource::DataSource;
use crate::core::rid::{parse_rid, rid_to_string};
use crate::core::utils::paginate_stream;
use crate::{Error, Result};
use futures::TryStreamExt;

/// Represents an asset in Nominal.
///
/// Assets are the top-level organizational unit in Nominal, containing datasets, videos,
/// connections, and attachments related to a specific test, flight, or analysis.
#[derive(Debug, Clone)]
pub struct Asset {
    rid: String,
    name: String,
    description: Option<String>,
    properties: HashMap<String, String>,
    labels: Vec<String>,
    data_sources: HashMap<String, DataSource>,
    created_at: DateTime<Utc>,
    app_base_url: String,
}

impl Asset {
    pub fn rid(&self) -> &str {
        &self.rid
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    pub fn properties(&self) -> &HashMap<String, String> {
        &self.properties
    }

    pub fn labels(&self) -> &[String] {
        &self.labels
    }

    /// Data sources attached to this asset, keyed by scope name.
    pub fn data_sources(&self) -> &HashMap<String, DataSource> {
        &self.data_sources
    }

    pub fn created_at(&self) -> &DateTime<Utc> {
        &self.created_at
    }

    /// Get the URL to view this asset in the Nominal web app.
    pub fn nominal_url(&self) -> String {
        format!("{}/assets/{}", self.app_base_url, self.rid)
    }

    pub(crate) fn from_conjure(
        asset: nominal_api::objects::scout::asset::api::Asset,
        app_base_url: &str,
    ) -> Self {
        let data_sources = asset
            .data_scopes()
            .iter()
            .filter_map(|scope| {
                DataSource::from_conjure(scope.data_source())
                    .map(|ds| (scope.data_scope_name().to_string(), ds))
            })
            .collect();
        Self {
            rid: rid_to_string(asset.rid()),
            name: asset.title().to_string(),
            description: asset
                .description()
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string()),
            properties: asset
                .properties()
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect(),
            labels: asset.labels().iter().map(|l| l.to_string()).collect(),
            data_sources,
            created_at: asset.created_at().to_utc(),
            app_base_url: app_base_url.to_string(),
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct AssetUpdate {
    name: Option<String>,
    description: Option<String>,
    properties: Option<HashMap<String, String>>,
    labels: Option<Vec<String>>,
}

impl AssetUpdate {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn name(mut self, value: impl Into<String>) -> Self {
        self.name = Some(value.into());
        self
    }

    #[must_use]
    pub fn description(mut self, value: impl Into<String>) -> Self {
        self.description = Some(value.into());
        self
    }

    #[must_use]
    pub fn properties<I, K, V>(mut self, value: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        self.properties = Some(
            value
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        );
        self
    }

    #[must_use]
    pub fn labels<I>(mut self, value: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        self.labels = Some(value.into_iter().map(Into::into).collect());
        self
    }

    pub(crate) fn into_request(self) -> UpdateAssetRequest {
        let AssetUpdate {
            name,
            description,
            properties,
            labels,
        } = self;

        let mut b = UpdateAssetRequest::builder();
        if let Some(n) = name {
            b = b.title(n);
        }
        if let Some(d) = description {
            b = b.description(d);
        }
        if let Some(p) = properties {
            b = b.properties(
                p.into_iter()
                    .map(|(k, v)| (k.into(), v.into()))
                    .collect::<BTreeMap<_, _>>(),
            );
        }
        if let Some(l) = labels {
            b = b.labels(l.into_iter().map(|s| s.into()).collect::<BTreeSet<_>>());
        }
        b.build()
    }
}

/// Parameters for creating a new asset.
#[derive(Debug, Clone)]
pub struct AssetCreate {
    name: String,
    description: Option<String>,
    properties: Option<HashMap<String, String>>,
    labels: Option<Vec<String>>,
}

impl AssetCreate {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: None,
            properties: None,
            labels: None,
        }
    }

    #[must_use]
    pub fn description(mut self, value: impl Into<String>) -> Self {
        self.description = Some(value.into());
        self
    }

    #[must_use]
    pub fn properties<I, K, V>(mut self, value: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        self.properties = Some(
            value
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        );
        self
    }

    #[must_use]
    pub fn labels<I>(mut self, value: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        self.labels = Some(value.into_iter().map(Into::into).collect());
        self
    }

    pub(crate) fn into_request(self, workspace_rid: Option<&str>) -> Result<CreateAssetRequest> {
        use nominal_api::objects::api::rids::WorkspaceRid;

        let AssetCreate {
            name,
            description,
            properties,
            labels,
        } = self;

        let mut b = CreateAssetRequest::builder().title(name);

        if let Some(d) = description {
            b = b.description(d);
        }
        if let Some(p) = properties {
            b = b.properties(
                p.into_iter()
                    .map(|(k, v)| (PropertyName(k), PropertyValue(v)))
                    .collect::<BTreeMap<_, _>>(),
            );
        }
        if let Some(l) = labels {
            b = b.labels(l.into_iter().map(Label).collect::<BTreeSet<_>>());
        }
        if let Some(wid) = workspace_rid {
            b = b.workspace(parse_rid::<WorkspaceRid>(wid)?);
        }

        Ok(b.build())
    }
}

/// A query for searching assets, which can be composed into a tree with [`and`](AssetQuery::and) and [`or`](AssetQuery::or).
#[derive(Debug, Clone)]
pub enum AssetQuery {
    /// Fuzzy full-text search against title and description.
    SearchText(String),
    /// Case-insensitive substring match against the asset name.
    SubstringMatch(String),
    /// Filter by label.
    Label(String),
    /// Filter by property key and value.
    Property(String, String),
    /// All sub-queries must match.
    And(Vec<AssetQuery>),
    /// At least one sub-query must match.
    Or(Vec<AssetQuery>),
}

impl AssetQuery {
    pub fn search_text(text: impl Into<String>) -> Self {
        Self::SearchText(text.into())
    }

    pub fn substring_match(text: impl Into<String>) -> Self {
        Self::SubstringMatch(text.into())
    }

    pub fn label(label: impl Into<String>) -> Self {
        Self::Label(label.into())
    }

    pub fn property(key: impl Into<String>, value: impl Into<String>) -> Self {
        Self::Property(key.into(), value.into())
    }

    pub fn and(queries: impl IntoIterator<Item = AssetQuery>) -> Self {
        Self::And(queries.into_iter().collect())
    }

    pub fn or(queries: impl IntoIterator<Item = AssetQuery>) -> Self {
        Self::Or(queries.into_iter().collect())
    }

    pub(crate) fn collect_substring_matches(&self) -> Vec<String> {
        let mut out = Vec::new();
        self.collect_substring_matches_into(&mut out);
        out
    }

    fn collect_substring_matches_into(&self, out: &mut Vec<String>) {
        match self {
            Self::SubstringMatch(s) => out.push(s.clone()),
            Self::And(qs) => qs
                .iter()
                .for_each(|q| q.collect_substring_matches_into(out)),
            _ => {}
        }
    }

    fn into_conjure(self) -> SearchAssetsQuery {
        match self {
            Self::SearchText(s) => SearchAssetsQuery::SearchText(s),
            Self::SubstringMatch(s) => SearchAssetsQuery::ExactSubstring(s),
            Self::Label(l) => SearchAssetsQuery::Labels(
                LabelsFilter::builder()
                    .operator(SetOperator::Or)
                    .extend_labels([Label(l)])
                    .build(),
            ),
            Self::Property(k, v) => SearchAssetsQuery::Properties(
                PropertiesFilter::builder()
                    .name(PropertyName(k))
                    .extend_values([PropertyValue(v)])
                    .build(),
            ),
            Self::And(qs) => {
                SearchAssetsQuery::And(qs.into_iter().map(Self::into_conjure).collect())
            }
            Self::Or(qs) => SearchAssetsQuery::Or(qs.into_iter().map(Self::into_conjure).collect()),
        }
    }
}

/// Client for asset collection operations (list, get).
pub struct AssetsClient {
    service: AsyncAssetServiceClient<Client>,
    token: BearerToken,
    workspace_rid: Option<String>,
    app_base_url: String,
}

impl AssetsClient {
    pub(crate) fn new(
        client: Client,
        runtime: &Arc<ConjureRuntime>,
        token: BearerToken,
        workspace_rid: Option<String>,
        app_base_url: String,
    ) -> Self {
        Self {
            service: AsyncAssetServiceClient::new(client, runtime),
            token,
            workspace_rid,
            app_base_url,
        }
    }

    /// Create a new asset.
    pub async fn create(&self, create: AssetCreate) -> Result<Asset> {
        let request = create.into_request(self.workspace_rid.as_deref())?;
        let response = self
            .service
            .create_asset(&self.token, &request)
            .await
            .map_err(Error::from)?;
        Ok(Asset::from_conjure(response, &self.app_base_url))
    }

    /// Get an asset by RID.
    pub async fn get(&self, rid: &str) -> Result<Asset> {
        let parsed = parse_rid(rid)?;
        let rid_set = std::collections::BTreeSet::from([parsed]);
        let response = self
            .service
            .get_assets(&self.token, &rid_set)
            .await
            .map_err(Error::from)?;

        let asset = response
            .into_iter()
            .next()
            .ok_or(Error::NotFound {
                resource: "asset with given RID",
            })?
            .1;

        Ok(Asset::from_conjure(asset, &self.app_base_url))
    }

    /// Get multiple assets by RID.
    ///
    /// Returns a map from RID string to Asset. RIDs not found in Nominal are omitted.
    pub async fn get_batch<I, S>(&self, rids: I) -> Result<HashMap<String, Asset>>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let rid_set = rids
            .into_iter()
            .map(|s| parse_rid(s.as_ref()).map_err(Error::from))
            .collect::<Result<std::collections::BTreeSet<_>>>()?;
        let response = self
            .service
            .get_assets(&self.token, &rid_set)
            .await
            .map_err(Error::from)?;
        Ok(response
            .into_iter()
            .map(|(k, v)| {
                (
                    rid_to_string(&k),
                    Asset::from_conjure(v, &self.app_base_url),
                )
            })
            .collect())
    }

    fn list_stream(&self) -> impl Stream<Item = Result<Asset>> {
        self.search_stream(AssetQuery::search_text(""))
    }

    /// List assets, sorted by creation date descending.
    pub async fn list(&self) -> Result<Vec<Asset>> {
        self.list_stream().try_collect().await
    }

    fn search_stream(&self, query: AssetQuery) -> impl Stream<Item = Result<Asset>> {
        let conjure_query = query.into_conjure();
        let service = self.service.clone();
        let token = self.token.clone();
        let app_base_url = self.app_base_url.clone();
        paginate_stream(
            move |page_token| {
                SearchAssetsRequest::builder()
                    .sort(
                        AssetSortOptions::builder()
                            .is_descending(true)
                            .sort_key(SortKey::Field(AssetSortField::CreatedAt))
                            .build(),
                    )
                    .query(conjure_query.clone())
                    .next_page_token(page_token)
                    .build()
            },
            move |req| {
                let service = service.clone();
                let token = token.clone();
                async move {
                    service
                        .search_assets(&token, &req)
                        .await
                        .map_err(Error::from)
                }
            },
            |resp: &SearchAssetsResponse| resp.next_page_token().cloned(),
            move |resp| {
                resp.results()
                    .iter()
                    .map(|a| Asset::from_conjure(a.clone(), &app_base_url))
                    .collect()
            },
        )
    }

    /// Search assets with a query, collecting all pages eagerly.
    ///
    /// # Example
    /// ```no_run
    /// # async fn example(client: nominal::core::NominalClient) -> nominal::Result<()> {
    /// use nominal::core::AssetQuery;
    /// let assets = client.assets()
    ///     .search(AssetQuery::and([
    ///         AssetQuery::label("production"),
    ///         AssetQuery::property("vehicle", "rocket"),
    ///     ]))
    ///     .await?;
    /// # Ok(()) }
    /// ```
    pub async fn search(&self, query: AssetQuery) -> Result<Vec<Asset>> {
        let substrings = query.collect_substring_matches();
        let assets: Vec<Asset> = self.search_stream(query).try_collect().await?;
        Ok(assets
            .into_iter()
            .filter(|a| crate::core::utils::name_matches_all(a.name(), &substrings))
            .collect())
    }

    /// Update asset metadata. Returns the updated asset.
    ///
    /// Only fields set on the update will be changed; the rest remain untouched.
    ///
    /// # Example
    /// ```no_run
    /// # use nominal::core::AssetUpdate;
    /// # async fn example(client: nominal::core::NominalClient) -> nominal::Result<()> {
    /// let asset = client.assets()
    ///     .update("ri.scout.cerulean-staging.asset.<uuid>", AssetUpdate::new().name("New Name").labels(["tag1", "tag2"]))
    ///     .await?;
    /// # Ok(()) }
    /// ```
    pub async fn update(&self, rid: &str, update: AssetUpdate) -> Result<Asset> {
        let request = update.into_request();
        let asset_rid = parse_rid(rid)?;
        let response = self
            .service
            .update_asset(&self.token, &asset_rid, &request)
            .await
            .map_err(Error::from)?;
        Ok(Asset::from_conjure(response, &self.app_base_url))
    }

    /// Attach data sources to an asset under the given scope names.
    ///
    /// Scope names should be stable across assets of the same type, since
    /// checklists and templates use them to reference data sources.
    /// Returns the updated asset.
    ///
    /// # Example
    /// ```no_run
    /// # async fn example(client: nominal::core::NominalClient) -> nominal::Result<()> {
    /// use nominal::core::DataSource;
    /// client.assets().add_data_sources("ri.scout.cerulean-staging.asset.<uuid>", [
    ///     ("flight-data", DataSource::dataset("ri.catalog.cerulean-staging.dataset.<uuid>")),
    ///     ("cockpit-cam", DataSource::video("ri.catalog.cerulean-staging.video.<uuid>")),
    /// ]).await?;
    /// # Ok(()) }
    /// ```
    pub async fn add_data_sources<I, N>(&self, rid: &str, sources: I) -> Result<Asset>
    where
        I: IntoIterator<Item = (N, DataSource)>,
        N: Into<String>,
    {
        let scopes = sources
            .into_iter()
            .map(|(name, ds)| {
                ds.into_conjure().map(|conjure_ds| {
                    CreateAssetDataScope::builder()
                        .data_scope_name(name.into().into())
                        .data_source(conjure_ds)
                        .build()
                })
            })
            .collect::<Result<BTreeSet<_>>>()?;
        let request = AddDataScopesToAssetRequest::builder()
            .data_scopes(scopes)
            .build();
        let asset_rid = parse_rid(rid)?;
        let response = self
            .service
            .add_data_scopes_to_asset(&self.token, &asset_rid, &request)
            .await
            .map_err(Error::from)?;
        Ok(Asset::from_conjure(response, &self.app_base_url))
    }

    /// Attach a dataset to an asset under the given scope name. See [`add_data_sources`](Self::add_data_sources).
    pub async fn add_dataset(&self, rid: &str, name: &str, dataset_rid: &str) -> Result<Asset> {
        self.add_data_sources(rid, [(name, DataSource::dataset(dataset_rid))])
            .await
    }

    /// Attach a dataset to an asset with a series tag filter under the given scope name.
    ///
    /// Series tags filter which series from the dataset are included in the asset's data scope.
    /// Returns the updated asset.
    ///
    /// # Example
    /// ```no_run
    /// # async fn example(client: nominal::core::NominalClient) -> nominal::Result<()> {
    /// client.assets().add_dataset_with_tags(
    ///     "ri.scout.cerulean-staging.asset.<uuid>",
    ///     "flight-data",
    ///     "ri.catalog.cerulean-staging.dataset.<uuid>",
    ///     [("vehicle", "rocket-1"), ("phase", "ascent")],
    /// ).await?;
    /// # Ok(()) }
    /// ```
    pub async fn add_dataset_with_tags<I, K, V>(
        &self,
        rid: &str,
        name: &str,
        dataset_rid: &str,
        tags: I,
    ) -> Result<Asset>
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        let conjure_ds = DataSource::dataset(dataset_rid).into_conjure()?;
        let scope = CreateAssetDataScope::builder()
            .data_scope_name(name.to_string().into())
            .data_source(conjure_ds)
            .series_tags(
                tags.into_iter()
                    .map(|(k, v)| (TagName(k.into()), TagValue(v.into()))),
            )
            .build();
        let request = AddDataScopesToAssetRequest::builder()
            .data_scopes(BTreeSet::from([scope]))
            .build();
        let asset_rid = parse_rid(rid)?;
        let response = self
            .service
            .add_data_scopes_to_asset(&self.token, &asset_rid, &request)
            .await
            .map_err(Error::from)?;
        Ok(Asset::from_conjure(response, &self.app_base_url))
    }

    /// Attach a video to an asset under the given scope name. See [`add_data_sources`](Self::add_data_sources).
    pub async fn add_video(&self, rid: &str, name: &str, video_rid: &str) -> Result<Asset> {
        self.add_data_sources(rid, [(name, DataSource::video(video_rid))])
            .await
    }

    /// Attach a connection to an asset under the given scope name. See [`add_data_sources`](Self::add_data_sources).
    pub async fn add_connection(
        &self,
        rid: &str,
        name: &str,
        connection_rid: &str,
    ) -> Result<Asset> {
        self.add_data_sources(rid, [(name, DataSource::connection(connection_rid))])
            .await
    }

    /// Archive an asset. Archived assets are hidden from the UI but not deleted.
    pub async fn archive(&self, rid: &str) -> Result<()> {
        let asset_rid = parse_rid(rid)?;
        self.service
            .archive(&self.token, &asset_rid, None)
            .await
            .map_err(Error::from)?;
        Ok(())
    }

    /// Unarchive an asset, restoring its visibility in the UI.
    pub async fn unarchive(&self, rid: &str) -> Result<()> {
        let asset_rid = parse_rid(rid)?;
        self.service
            .unarchive(&self.token, &asset_rid, None)
            .await
            .map_err(Error::from)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nominal_api::objects::scout::asset::api::SearchAssetsQuery;

    // --- AssetQuery::into_conjure ---

    #[test]
    fn query_search_text() {
        let q = AssetQuery::search_text("hello");
        assert_eq!(
            q.into_conjure(),
            SearchAssetsQuery::SearchText("hello".into())
        );
    }

    #[test]
    fn query_substring_match() {
        let q = AssetQuery::substring_match("foo");
        assert_eq!(
            q.into_conjure(),
            SearchAssetsQuery::ExactSubstring("foo".into())
        );
    }

    #[test]
    fn query_label() {
        let q = AssetQuery::label("my-label");
        let SearchAssetsQuery::Labels(f) = q.into_conjure() else {
            panic!("expected Labels variant");
        };
        assert_eq!(
            f.labels(),
            [nominal_api::objects::api::Label("my-label".into())]
        );
    }

    #[test]
    fn query_property() {
        let q = AssetQuery::property("key", "val");
        let SearchAssetsQuery::Properties(f) = q.into_conjure() else {
            panic!("expected Properties variant");
        };
        assert_eq!(
            f.name(),
            &nominal_api::objects::api::PropertyName("key".into())
        );
        assert_eq!(
            f.values(),
            [nominal_api::objects::api::PropertyValue("val".into())]
        );
    }

    #[test]
    fn query_and_flattens_children() {
        let q = AssetQuery::and([AssetQuery::search_text("a"), AssetQuery::search_text("b")]);
        let SearchAssetsQuery::And(children) = q.into_conjure() else {
            panic!("expected And variant");
        };
        assert_eq!(children.len(), 2);
        assert_eq!(children[0], SearchAssetsQuery::SearchText("a".into()));
        assert_eq!(children[1], SearchAssetsQuery::SearchText("b".into()));
    }

    #[test]
    fn query_or_flattens_children() {
        let q = AssetQuery::or([AssetQuery::label("x"), AssetQuery::label("y")]);
        let SearchAssetsQuery::Or(children) = q.into_conjure() else {
            panic!("expected Or variant");
        };
        assert_eq!(children.len(), 2);
    }

    #[test]
    fn query_nested_and_or() {
        let q = AssetQuery::and([
            AssetQuery::label("prod"),
            AssetQuery::or([
                AssetQuery::property("env", "us"),
                AssetQuery::property("env", "eu"),
            ]),
        ]);
        let SearchAssetsQuery::And(children) = q.into_conjure() else {
            panic!("expected And variant");
        };
        assert!(matches!(children[0], SearchAssetsQuery::Labels(_)));
        assert!(matches!(children[1], SearchAssetsQuery::Or(_)));
    }

    // --- AssetUpdate::into_request ---

    #[test]
    fn update_empty_request_has_no_fields() {
        let req = AssetUpdate::new().into_request();
        assert!(req.title().is_none());
        assert!(req.description().is_none());
        assert!(req.properties().is_none());
        assert!(req.labels().is_none());
    }

    #[test]
    fn update_name_only() {
        let req = AssetUpdate::new().name("New Name").into_request();
        assert_eq!(req.title(), Some("New Name"));
        assert!(req.description().is_none());
    }

    #[test]
    fn update_description_only() {
        let req = AssetUpdate::new().description("desc").into_request();
        assert!(req.title().is_none());
        assert_eq!(req.description(), Some("desc"));
    }

    #[test]
    fn update_properties_converted_correctly() {
        let req = AssetUpdate::new()
            .properties([("k1", "v1"), ("k2", "v2")])
            .into_request();
        let props = req.properties().expect("properties should be set");
        assert_eq!(props.len(), 2);
        assert_eq!(
            props.get(&nominal_api::objects::api::PropertyName("k1".into())),
            Some(&nominal_api::objects::api::PropertyValue("v1".into()))
        );
        assert_eq!(
            props.get(&nominal_api::objects::api::PropertyName("k2".into())),
            Some(&nominal_api::objects::api::PropertyValue("v2".into()))
        );
    }

    #[test]
    fn update_labels_converted_and_deduplicated() {
        let req = AssetUpdate::new()
            .labels(["tag1", "tag2", "tag1"])
            .into_request();
        let labels = req.labels().expect("labels should be set");
        // BTreeSet deduplicates
        assert_eq!(labels.len(), 2);
        assert!(labels.contains(&nominal_api::objects::api::Label("tag1".into())));
        assert!(labels.contains(&nominal_api::objects::api::Label("tag2".into())));
    }

    #[test]
    fn update_all_fields() {
        let req = AssetUpdate::new()
            .name("name")
            .description("desc")
            .properties([("k", "v")])
            .labels(["t"])
            .into_request();
        assert_eq!(req.title(), Some("name"));
        assert_eq!(req.description(), Some("desc"));
        assert!(req.properties().is_some());
        assert!(req.labels().is_some());
    }
}