tilo-resource 0.5.2

The Tilo resource layer: typed, hierarchical app resources.
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
//! An app's search entries as the system's persistent search index takes
//! them.
//!
//! A running app publishes what its search finds (each entry's title,
//! secondary line and short fields, keyed by the stable resource id a hit
//! opens) to the session over its certified broker route, and the session
//! keeps it after the app closes, so the app's content stays findable
//! whether or not it runs. Only short metadata crosses: prose bodies stay in
//! the app, and a secret-bearing entry ([`IndexEntry::secret`]) carries what
//! the app chose to show of it (a name, a username, a site), never a secret.
//!
//! Every text is the one the session's person reads: text derived from rich
//! text reads each reference as it reads for them (a resource they hold by
//! its title now, one they cannot read as not shared,
//! `tilo_reference::embed::read_text`), never as the run it was written
//! with, so the index never matches a query against a title they cannot
//! read.
//!
//! The publication is incremental: a [`Publisher`] remembers what its
//! connection last sent and sends an [`IndexDelta`] of what changed, the
//! whole set again (`reset`) only on a new route.
//!
//! The types live here, in the toolkit-free resource layer, because both
//! halves that publish to the one index read them: a toolkit app's window
//! (`tilo_ui`) and a tilolang app's instance, whose host links no widget.

use std::collections::{BTreeMap, HashMap};

/// Encoded bytes one [`IndexDelta`] may take; a larger change is sent as
/// several.
pub const MAX_DELTA_BYTES: usize = 512 * 1024;
/// Bytes of one text an entry carries (its title, its secondary line, one
/// field); longer text is cut at a character boundary.
pub const MAX_TEXT_BYTES: usize = 512;
/// Bytes of a resource id, action or key.
pub const MAX_ID_BYTES: usize = 1024;
/// Short fields one entry is found by.
pub const MAX_FIELDS: usize = 32;
/// Entries one publication holds; the rest of a larger set is not indexed.
pub const MAX_ENTRIES: usize = 20_000;

/// A published action a hit invokes: an action of a resource the app
/// publishes, by their ids.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct IndexAction {
    pub resource: String,
    pub action: String,
}

/// How much a match in one of an entry's fields counts.
///
/// It is what an app tells the index about its entry, and what a search hit carries back, so the
/// two read one vocabulary.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, crate::JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum IndexWeight {
    /// The name a person looks for.
    Title,
    /// A stable identity typed verbatim: an address, a site, a username.
    Identity,
    /// A secondary line: an author, a sender, a folder.
    Subtitle,
    /// Other short fields: labels, tags, a subject.
    Metadata,
    /// Short prose.
    Body,
}

/// What kind of thing an entry is, where the app keeps something the shell's
/// generic rows would read poorly: each is its own section, drawn as what it
/// is rather than as a record.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, crate::JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum IndexKind {
    /// An agent of the workspace, opened in the conversation it answers in.
    Agent,
    /// A conversation: a channel, a group or a direct message.
    Conversation,
    /// One message in a conversation, opened where it sits.
    Message,
    /// A person or an agent the workspace knows, drawn as themselves.
    Person,
}

/// A person or an agent an entry is about: who a row shows and what it says
/// under its name.
#[derive(
    Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, crate::JsonSchema,
)]
pub struct IndexPerson {
    /// The name the record gives them, when it can say it; empty: what their
    /// `anchor` is called now.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub name: String,
    /// The resource that names them when the record only holds their id
    /// (`chat.workspace/people/…`), empty when there is none.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub anchor: String,
    /// Their stable id: picks their colour and their picture.
    pub identity: String,
    /// Whether they are an agent.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub agent: bool,
}

/// One search entry, as the system index keeps it.
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IndexEntry {
    /// Stable within the app's publication: the resource id, or the
    /// resource and action a hit invokes.
    pub key: String,
    /// The stable resource id a hit opens: the entry's anchor, else the
    /// resource of its action. Without an action (or once it is gone) a
    /// hit opens the app here through its `open` action.
    pub resource: String,
    /// The published action a hit invokes, when the entry names one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub action: Option<IndexAction>,
    /// The resource this one lives in, when it is an item of one: the book a
    /// chapter is of, the notebook a note is in, the folder a file is in.
    /// What a search scoped to that resource narrows to; `None` for a
    /// resource that lives on its own.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub owner: Option<String>,
    pub title: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subtitle: Option<String>,
    /// The result group the app lists the entry under ("Passwords").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub group: Option<String>,
    /// What kind of thing it is, when the app keeps something the shell's
    /// generic rows would read poorly (an agent, a conversation, a message).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kind: Option<IndexKind>,
    /// Who the row shows, in the order people read them: the first is its
    /// avatar, the rest its faces. The sender of a message, the people in a
    /// conversation, the agent itself.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub people: Vec<IndexPerson>,
    /// Where it is, for a message: the conversation it sits in.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub place: Option<IndexPerson>,
    /// When it happened, in seconds since the Unix epoch: the row says how
    /// long ago.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub when: Option<i64>,
    /// A named icon (a lucide kebab name).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,
    /// A nudge over an equal match (a favourite), never an override.
    #[serde(default)]
    pub boost: i32,
    /// More short text the entry is found by, each with its weight.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<(String, IndexWeight)>,
    /// The entry describes a secret: found only while the app reports its
    /// secrets unlocked.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub secret: bool,
    /// What its text refers to, each in the one form a reference in rich
    /// text is written in (`tilo_reference::embed`): the system index finds
    /// the entry by what each is called now, not by the title written into
    /// the text, and answers which entries refer to a resource (its
    /// backlinks).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub references: Vec<String>,
    /// The picture the entry is about, as its address in the content store:
    /// what the system's image analysis reads, so every app's pictures are
    /// found by the words in them and by what they show.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub image: Option<String>,
    /// Who alone may find it, when its resource is sealed to its readers (a
    /// direct conversation, an unsent draft): each member as a change's
    /// readers name them (`crate::contract::reader_name`). `None` for
    /// whoever may read the record it was published from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub readers: Option<Vec<String>>,
}

/// `text` cut to at most `max` bytes at a character boundary.
fn cut(mut text: String, max: usize) -> String {
    if text.len() > max {
        let mut end = max;
        while !text.is_char_boundary(end) {
            end = end.saturating_sub(1);
        }
        text.truncate(end);
    }
    text
}

/// `entries` with one entry per key, in the order each key first appears: an
/// app that publishes several entries about one resource (a note, and each
/// paragraph of it, all opening the note) is one entry the index keeps,
/// found by every text each of them is found by. The first entry of a key
/// says what the row shows; the later ones add their fields (their title
/// where they have none), so a resource is found by what it holds as well
/// as by its name. An index keyed by entry would otherwise keep one of them
/// and lose what the others say.
#[must_use]
pub fn merged_by_key(entries: impl IntoIterator<Item = IndexEntry>) -> Vec<IndexEntry> {
    let mut merged: Vec<IndexEntry> = Vec::new();
    let mut at: BTreeMap<String, usize> = BTreeMap::new();
    for entry in entries {
        let Some(&first) = at.get(&entry.key) else {
            at.insert(entry.key.clone(), merged.len());
            merged.push(entry);
            continue;
        };
        let Some(kept) = merged.get_mut(first) else {
            continue;
        };
        let IndexEntry {
            title,
            fields,
            references,
            ..
        } = entry;
        // A later entry's title is what its own fields shorten (a paragraph's
        // first words): counted only where it has none.
        let titled = fields.is_empty().then_some((title, IndexWeight::Body));
        let said = titled.into_iter().chain(fields);
        for (text, weight) in said {
            let text = text.trim();
            if text.is_empty()
                || text == kept.title
                || kept.fields.iter().any(|(held, _)| held == text)
            {
                continue;
            }
            kept.fields.push((text.to_owned(), weight));
        }
        for reference in references {
            if !kept.references.contains(&reference) {
                kept.references.push(reference);
            }
        }
    }
    merged
}

/// The picture `text` names, when it is a content address: what the system's
/// image analysis reads, so a record that keeps a picture is found by the
/// words in it and by what it shows.
#[must_use]
pub fn picture_address(text: &str) -> Option<String> {
    let text = text.trim();
    (text.starts_with("tilo-blob://") || text.starts_with("tilo.content:")).then(|| text.to_owned())
}

impl IndexEntry {
    /// Whether `reader` (a member, as [`Self::readers`] names them; `None`
    /// for a caller who is no member) may find the entry among what it may
    /// read of its record: anyone may, unless the entry names its readers.
    #[must_use]
    pub fn found_by(&self, reader: Option<&str>) -> bool {
        self.readers.as_ref().is_none_or(|named| {
            reader.is_some_and(|reader| named.iter().any(|name| name == reader))
        })
    }

    /// Whether a hit opens the entry's resource: through the `open` it
    /// names (of the resource itself, or of the one holding it), or its
    /// resource's own when it names no action.
    #[must_use]
    pub fn opens_its_resource(&self) -> bool {
        self.action
            .as_ref()
            .is_none_or(|action| action.action == tilo_actions::remote::APPLICATION_OPEN_ACTION)
    }

    /// The entry within this module's bounds: texts cut, fields capped,
    /// empty fields dropped. `None` for an entry with nothing to find it by
    /// or nothing to open.
    #[must_use]
    pub fn bounded(self) -> Option<Self> {
        let title = cut(self.title.trim().to_owned(), MAX_TEXT_BYTES);
        if title.is_empty()
            || self.resource.is_empty()
            || self.key.is_empty()
            || self.resource.len() > MAX_ID_BYTES
            || self.key.len() > MAX_ID_BYTES
            || self.action.as_ref().is_some_and(|action| {
                [&action.resource, &action.action]
                    .into_iter()
                    .any(|id| id.is_empty() || id.len() > MAX_ID_BYTES)
            })
        {
            return None;
        }
        let short = |text: Option<String>| {
            text.map(|text| cut(text.trim().to_owned(), MAX_TEXT_BYTES))
                .filter(|text| !text.is_empty())
        };
        let person = |person: IndexPerson| {
            (!person.identity.is_empty() && person.identity.len() <= MAX_ID_BYTES).then(|| {
                IndexPerson {
                    name: cut(person.name.trim().to_owned(), MAX_TEXT_BYTES),
                    anchor: cut(person.anchor.trim().to_owned(), MAX_ID_BYTES),
                    ..person
                }
            })
        };
        Some(Self {
            title,
            owner: self.owner.and_then(|owner| {
                (!owner.is_empty() && owner.len() <= MAX_ID_BYTES).then_some(owner)
            }),
            subtitle: short(self.subtitle),
            group: short(self.group),
            icon: short(self.icon),
            people: self
                .people
                .into_iter()
                .filter_map(person)
                .take(MAX_FIELDS)
                .collect(),
            place: self.place.and_then(person),
            fields: self
                .fields
                .into_iter()
                .map(|(text, weight)| (cut(text.trim().to_owned(), MAX_TEXT_BYTES), weight))
                .filter(|(text, _)| !text.is_empty())
                .take(MAX_FIELDS)
                .collect(),
            references: self
                .references
                .into_iter()
                .filter(|reference| !reference.is_empty() && reference.len() <= MAX_ID_BYTES)
                .take(MAX_FIELDS)
                .collect(),
            image: self.image.and_then(|image| {
                (!image.is_empty() && image.len() <= MAX_ID_BYTES).then_some(image)
            }),
            ..self
        })
    }

    /// Every text the entry is found by, with its weight: its title, its
    /// secondary line and its fields.
    pub fn searchable(&self) -> impl Iterator<Item = (&str, IndexWeight)> {
        std::iter::once((self.title.as_str(), IndexWeight::Title))
            .chain(
                self.subtitle
                    .as_deref()
                    .map(|subtitle| (subtitle, IndexWeight::Subtitle)),
            )
            .chain(
                self.fields
                    .iter()
                    .map(|(text, weight)| (text.as_str(), *weight)),
            )
    }
}

/// A change to what one app connection published.
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IndexDelta {
    /// Discard what this app published in its slot before applying the rest.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub reset: bool,
    /// Entries new or changed, by their key.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub upsert: Vec<IndexEntry>,
    /// Keys of entries no longer offered.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub remove: Vec<String>,
    /// Whether the app's secrets are unlocked now; `None` says nothing.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub secrets_unlocked: Option<bool>,
}

impl IndexDelta {
    /// The delta as it crosses the broker.
    #[must_use]
    pub fn encode(&self) -> Option<Vec<u8>> {
        serde_json::to_vec(self).ok()
    }

    /// The delta `bytes` encode, within [`MAX_DELTA_BYTES`].
    pub fn decode(bytes: &[u8]) -> Result<Self, String> {
        if bytes.len() > MAX_DELTA_BYTES {
            return Err(format!(
                "a search index delta takes at most {MAX_DELTA_BYTES} bytes"
            ));
        }
        serde_json::from_slice(bytes).map_err(|error| error.to_string())
    }
}

/// What an app's search source is of now, read every frame: a publication
/// is rebuilt only when it changes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IndexState {
    /// Changes whenever the source's entries may have.
    pub generation: u64,
    /// Whether the secrets behind secret-bearing entries are unlocked;
    /// `None` says nothing.
    pub secrets_unlocked: Option<bool>,
}

/// What one route last published, so only what changed is sent again.
#[derive(Clone, Debug, Default)]
pub struct Publisher {
    published: Option<HashMap<String, IndexEntry>>,
    state: Option<IndexState>,
}

impl Publisher {
    /// Forget what was sent: the next publication sends the whole set.
    pub fn reset(&mut self) {
        self.published = None;
        self.state = None;
    }

    /// Whether the source moved since the last publication.
    #[must_use]
    pub fn wants(&self, state: IndexState) -> bool {
        self.state != Some(state)
    }

    /// The deltas, each within [`MAX_DELTA_BYTES`], that bring the index from what this
    /// publisher sent to `entries` at `state`. What they carry counts as sent.
    pub fn publish(&mut self, state: IndexState, entries: Vec<IndexEntry>) -> Vec<IndexDelta> {
        let mut next: BTreeMap<String, IndexEntry> = BTreeMap::new();
        for entry in entries.into_iter().filter_map(IndexEntry::bounded) {
            if next.len() >= MAX_ENTRIES {
                break;
            }
            next.entry(entry.key.clone()).or_insert(entry);
        }
        let reset = self.published.is_none();
        let (upsert, remove): (Vec<IndexEntry>, Vec<String>) = match &self.published {
            None => (next.values().cloned().collect(), Vec::new()),
            Some(before) => (
                next.values()
                    .filter(|entry| before.get(&entry.key) != Some(*entry))
                    .cloned()
                    .collect(),
                {
                    let mut gone: Vec<String> = before
                        .keys()
                        .filter(|key| !next.contains_key(*key))
                        .cloned()
                        .collect();
                    gone.sort_unstable();
                    gone
                },
            ),
        };
        self.published = Some(next.into_iter().collect());
        self.delta(state, reset, upsert, remove)
    }

    /// The deltas that send `upsert` entries, new or changed, and drop `remove` keys, from what
    /// this publisher sent: what a source that knows what moved publishes instead of reading
    /// its whole set again. `None` when what this publisher sent does not cover the change (the
    /// first publication on a route, or a source that never sent the whole set): the caller
    /// publishes the whole set again instead.
    pub fn publish_moved(
        &mut self,
        state: IndexState,
        upsert: Vec<IndexEntry>,
        remove: Vec<String>,
    ) -> Option<Vec<IndexDelta>> {
        let published = self.published.as_mut()?;
        let upsert: Vec<IndexEntry> = upsert
            .into_iter()
            .filter_map(IndexEntry::bounded)
            .filter(|entry| published.get(&entry.key) != Some(entry))
            .collect();
        // The set the whole publication kept has room; one that would grow past it is read
        // whole again, which caps it.
        let added = upsert
            .iter()
            .filter(|entry| !published.contains_key(&entry.key))
            .count();
        if published.len().saturating_add(added) > MAX_ENTRIES {
            return None;
        }
        for entry in &upsert {
            published.insert(entry.key.clone(), entry.clone());
        }
        let mut remove: Vec<String> = remove
            .into_iter()
            .filter(|key| published.remove(key).is_some())
            .collect();
        remove.sort_unstable();
        remove.dedup();
        Some(self.delta(state, false, upsert, remove))
    }

    /// The deltas `upsert` and `remove` make at `state`, once they count as sent.
    fn delta(
        &mut self,
        state: IndexState,
        reset: bool,
        upsert: Vec<IndexEntry>,
        remove: Vec<String>,
    ) -> Vec<IndexDelta> {
        let secrets_moved = self
            .state
            .is_none_or(|before| before.secrets_unlocked != state.secrets_unlocked);
        self.state = Some(state);
        if !reset && upsert.is_empty() && remove.is_empty() && !secrets_moved {
            return Vec::new();
        }
        chunked(IndexDelta {
            reset,
            upsert,
            remove,
            secrets_unlocked: state.secrets_unlocked,
        })
    }
}

/// The encoded length of `value`, for chunking.
fn encoded_len<T: serde::Serialize>(value: &T) -> usize {
    serde_json::to_vec(value).map_or(0, |bytes| bytes.len())
}

/// `delta` as deltas of at most [`MAX_DELTA_BYTES`] each, in order: the
/// first carries the reset, the removals and the lock.
fn chunked(delta: IndexDelta) -> Vec<IndexDelta> {
    // Room left for the envelope and the separators between entries.
    const SLACK: usize = 256;
    let IndexDelta {
        reset,
        upsert,
        remove,
        secrets_unlocked,
    } = delta;
    let mut current = IndexDelta {
        reset,
        upsert: Vec::new(),
        remove: Vec::new(),
        secrets_unlocked,
    };
    let mut used = encoded_len(&current).saturating_add(SLACK);
    let mut out = Vec::new();
    for key in remove {
        let len = encoded_len(&key).saturating_add(1);
        if used.saturating_add(len) > MAX_DELTA_BYTES && !current.remove.is_empty() {
            out.push(std::mem::take(&mut current));
            used = SLACK;
        }
        used = used.saturating_add(len);
        current.remove.push(key);
    }
    for entry in upsert {
        let len = encoded_len(&entry).saturating_add(1);
        if used.saturating_add(len) > MAX_DELTA_BYTES
            && !(current.upsert.is_empty() && current.remove.is_empty())
        {
            out.push(std::mem::take(&mut current));
            used = SLACK;
        }
        used = used.saturating_add(len);
        current.upsert.push(entry);
    }
    out.push(current);
    out
}

#[cfg(test)]
mod tests {
    #[test]
    fn a_content_address_is_a_picture_and_a_name_is_not() {
        assert_eq!(
            super::picture_address("tilo-blob://abc.def"),
            Some("tilo-blob://abc.def".to_owned())
        );
        assert_eq!(
            super::picture_address("tilo.content:abc"),
            Some("tilo.content:abc".to_owned())
        );
        assert_eq!(super::picture_address("A dog on a beach"), None);
        assert_eq!(super::picture_address(""), None);
    }

    use super::*;

    fn entry(key: &str, title: &str) -> IndexEntry {
        IndexEntry {
            key: key.into(),
            resource: key.into(),
            action: None,
            title: title.into(),
            subtitle: None,
            group: None,
            icon: None,
            boost: 0,
            fields: Vec::new(),
            secret: false,
            references: Vec::new(),
            ..IndexEntry::default()
        }
    }

    fn state(generation: u64) -> IndexState {
        IndexState {
            generation,
            secrets_unlocked: None,
        }
    }

    tilo_test_env::scenario! {
        fn a_search_index_is_published_in_bounded_deltas;

        phase a_publisher_sends_the_whole_set_once_then_only_what_changed;
        {
            let mut publisher = Publisher::default();
            let first = publisher.publish(state(1), vec![entry("a", "Alpha"), entry("b", "Beta")]);
            assert_eq!(first.len(), 1);
            assert!(first[0].reset);
            assert_eq!(first[0].upsert.len(), 2);

            assert!(!publisher.wants(state(1)));
            let changed = publisher.publish(state(2), vec![entry("a", "Alpha 2"), entry("c", "Gamma")]);
            assert_eq!(changed.len(), 1);
            assert!(!changed[0].reset);
            let keys: Vec<&str> = changed[0]
                .upsert
                .iter()
                .map(|entry| entry.key.as_str())
                .collect();
            assert_eq!(keys, ["a", "c"]);
            assert_eq!(changed[0].remove, ["b"]);

            let same = publisher.publish(state(3), vec![entry("a", "Alpha 2"), entry("c", "Gamma")]);
            assert!(same.is_empty(), "nothing changed, nothing is sent");

            publisher.reset();
            let again = publisher.publish(state(3), vec![entry("a", "Alpha 2")]);
            assert!(again[0].reset, "a new route gets the whole set again");
        }

        phase a_lock_change_alone_is_published;
        {
            let mut publisher = Publisher::default();
            let unlocked = IndexState {
                generation: 1,
                secrets_unlocked: Some(true),
            };
            publisher.publish(unlocked, vec![entry("a", "Alpha")]);
            let locked = IndexState {
                secrets_unlocked: Some(false),
                ..unlocked
            };
            assert!(publisher.wants(locked));
            let deltas = publisher.publish(locked, vec![entry("a", "Alpha")]);
            assert_eq!(deltas.len(), 1);
            assert_eq!(deltas[0].secrets_unlocked, Some(false));
            assert!(deltas[0].upsert.is_empty());
        }

        phase a_large_set_crosses_in_bounded_deltas;
        {
            let mut publisher = Publisher::default();
            let long = "x".repeat(MAX_TEXT_BYTES);
            let entries: Vec<IndexEntry> = (0..4_000)
                .map(|at| IndexEntry {
                    subtitle: Some(long.clone()),
                    ..entry(&format!("item-{at}"), &long)
                })
                .collect();
            let deltas = publisher.publish(state(1), entries);
            assert!(deltas.len() > 1);
            assert!(deltas[0].reset);
            assert!(deltas.iter().skip(1).all(|delta| !delta.reset));
            assert_eq!(
                deltas.iter().map(|delta| delta.upsert.len()).sum::<usize>(),
                4_000
            );
            for delta in &deltas {
                let bytes = delta.encode().expect("encodes");
                assert!(bytes.len() <= MAX_DELTA_BYTES);
                assert_eq!(IndexDelta::decode(&bytes).expect("decodes"), *delta);
            }
        }

        phase entries_are_bounded_and_need_something_to_open;
        {
            let long = "é".repeat(MAX_TEXT_BYTES);
            let bounded = IndexEntry {
                fields: (0..100)
                    .map(|at| (format!("field {at}"), IndexWeight::Metadata))
                    .collect(),
                ..entry("a", &long)
            }
            .bounded()
            .expect("kept");
            assert!(bounded.title.len() <= MAX_TEXT_BYTES);
            assert_eq!(bounded.fields.len(), MAX_FIELDS);
            assert!(entry("", "Nothing to open").bounded().is_none());
            assert!(entry("a", "   ").bounded().is_none());
        }
    }
}