okbot-testkit 0.8.0

Test doubles and fixtures for okbot crates and third-party adapters.
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
//! Conformance suites: call them from an implementation's tests.
//!
//! ```ignore
//! #[tokio::test]
//! async fn conformance() {
//!     let clock = Arc::new(ManualClock::default());
//!     let store = MyStore::new(clock.clone());
//!     okbot_testkit::conformance::conversation_store(&store, &clock).await;
//! }
//! ```

use std::time::Duration;

use okbot_core::{
    AgentRunRecord, ChannelId, Clock, ConversationKey, ConversationStore, MessageRole, RoleGrant,
    RunId, TokenUsage, ToolCallRecord, ToolDecision, ToolPolicy, Usage, UserId,
};

use crate::{ManualClock, bot_message, conversation, user_message};

/// Checks a fresh, empty [`ConversationStore`] that reads time from `clock`.
///
/// # Panics
/// On the first behaviour that differs from the port's contract.
pub async fn conversation_store(store: &dyn ConversationStore, clock: &ManualClock) {
    let a = conversation("zalo:shop", "room-a");
    let b = conversation("zalo:shop", "room-b");
    dedupe(store, clock).await;
    pairing(store, clock).await;
    fires_and_direct_chats(store, clock).await;
    usage_and_tool_calls(store, clock).await;
    role_grants(store, clock).await;

    // History: oldest first, limited to the newest, separated by conversation.
    for i in 0..5 {
        store
            .append(user_message(&a, &format!("q{i}"), clock.now()))
            .await
            .expect("append");
        store
            .append(bot_message(&a, &format!("r{i}"), clock.now()))
            .await
            .expect("append");
    }
    store
        .append(user_message(&b, "other room", clock.now()))
        .await
        .expect("append");
    let recent = store.recent(&a, 3).await.expect("recent");
    let texts: Vec<&str> = recent.iter().map(|m| m.text.as_str()).collect();
    assert_eq!(texts, ["r3", "q4", "r4"]);
    assert_eq!(recent[2].role, MessageRole::Bot);
    assert_eq!(store.recent(&b, 10).await.expect("recent").len(), 1);
    assert_eq!(
        store
            .recent(&conversation("zalo:shop", "none"), 10)
            .await
            .expect("recent"),
        []
    );

    // Sessions: stored per conversation, cleared by reset.
    assert_eq!(store.session(&a).await.expect("session"), None);
    store
        .set_session(&a, "claude", "s-1")
        .await
        .expect("set_session");
    store
        .set_session(&a, "claude", "s-2")
        .await
        .expect("set_session");
    assert_eq!(
        store.session(&a).await.expect("session"),
        Some(("claude".into(), "s-2".into()))
    );
    assert_eq!(store.session(&b).await.expect("session"), None);

    // Reset hides earlier messages of that conversation only.
    store.reset(&a).await.expect("reset");
    assert_eq!(
        store.session(&a).await.expect("session"),
        None,
        "reset forgets the session"
    );
    assert_eq!(store.recent(&a, 10).await.expect("recent"), []);
    store
        .append(user_message(&a, "after reset", clock.now()))
        .await
        .expect("append");
    assert_eq!(store.recent(&a, 10).await.expect("recent").len(), 1);
    assert_eq!(store.recent(&b, 10).await.expect("recent").len(), 1);

    // Audit records are accepted.
    store
        .record_run(AgentRunRecord {
            run_id: RunId::new("run-1"),
            conversation: a.clone(),
            backend: "fake".into(),
            model: None,
            tools: ToolPolicy::Web,
            usage: None,
            duration: Duration::from_millis(1500),
            error: None,
            started_at: clock.now(),
            person: None,
        })
        .await
        .expect("record_run");

    // Retention removes old messages.
    clock.advance(Duration::from_secs(3600));
    store
        .append(user_message(&b, "new", clock.now()))
        .await
        .expect("append");
    let cutoff = clock
        .now()
        .checked_sub(jiff::SignedDuration::from_secs(60))
        .expect("in range");
    let removed = store.purge_before(cutoff).await.expect("purge");
    assert!(removed >= 1, "purge removed nothing");
    let left: Vec<String> = store
        .recent(&b, 10)
        .await
        .expect("recent")
        .into_iter()
        .map(|m| m.text)
        .collect();
    assert_eq!(left, ["new"]);
}

/// Usage per person over a period, and the tool call audit.
async fn usage_and_tool_calls(store: &dyn ConversationStore, clock: &ManualClock) {
    let a = conversation("zalo:shop", "usage");
    let run = |id: &str, person: Option<&str>, tokens: u64| AgentRunRecord {
        run_id: RunId::new(id),
        conversation: a.clone(),
        backend: "fake".into(),
        model: None,
        tools: ToolPolicy::Web,
        usage: Some(TokenUsage {
            input: tokens,
            output: 1,
            ..TokenUsage::default()
        }),
        duration: Duration::from_millis(10),
        error: None,
        started_at: clock.now(),
        person: person.map(str::to_string),
    };
    let since = clock.now();
    store
        .record_run(run("u-0", Some("person:lan"), 99))
        .await
        .expect("record_run");
    clock.advance(Duration::from_secs(10));
    let later = clock.now();
    store
        .record_run(run("u-1", Some("person:lan"), 9))
        .await
        .expect("record_run");
    store
        .record_run(run("u-2", Some("zalo:shop/other"), 5))
        .await
        .expect("record_run");
    store
        .record_run(run("u-3", None, 5))
        .await
        .expect("record_run");
    assert_eq!(
        store.usage_since("person:lan", since).await.expect("usage"),
        Usage {
            turns: 2,
            tokens: 110
        }
    );
    assert_eq!(
        store.usage_since("person:lan", later).await.expect("usage"),
        Usage {
            turns: 1,
            tokens: 10
        }
    );
    assert_eq!(
        store.usage_since("nobody", since).await.expect("usage"),
        Usage::default()
    );

    let call = |tool: &str, decision| ToolCallRecord {
        run_id: Some(RunId::new("u-1")),
        command: None,
        person: "person:lan".into(),
        conversation: a.clone(),
        server: "ops".into(),
        tool: tool.into(),
        decision,
        error: None,
        duration: Duration::from_millis(7),
        at: clock.now(),
    };
    store
        .record_tool_call(call("status", ToolDecision::Allowed))
        .await
        .expect("record_tool_call");
    clock.advance(Duration::from_secs(1));
    let mut denied = call("deploy", ToolDecision::Denied);
    denied.run_id = None;
    denied.command = Some("deploy".into());
    denied.error = Some("not allowed".into());
    store
        .record_tool_call(denied.clone())
        .await
        .expect("record_tool_call");
    let calls = store.tool_calls(since, 10).await.expect("tool_calls");
    assert_eq!(calls.len(), 2);
    assert_eq!(calls[0], denied, "newest first, read back as written");
    assert_eq!(calls[1].tool, "status");
    assert_eq!(
        store.tool_calls(since, 1).await.expect("tool_calls").len(),
        1
    );
    assert_eq!(
        store
            .tool_calls(clock.now() + jiff::SignedDuration::from_secs(1), 10)
            .await
            .expect("tool_calls"),
        []
    );
}

/// Temporary roles: listed until they expire, replaced by a new grant, revocable.
async fn role_grants(store: &dyn ConversationStore, clock: &ManualClock) {
    let grant = |role: &str, account: &str, secs: i64| RoleGrant {
        role: role.into(),
        account: account.into(),
        granted_by: "operator".into(),
        granted_at: clock.now(),
        expires_at: clock.now() + jiff::SignedDuration::from_secs(secs),
    };
    store
        .grant_role(grant("ops", "zalo:shop/u1", 60))
        .await
        .expect("grant");
    store
        .grant_role(grant("ops", "zalo:shop/u2", 600))
        .await
        .expect("grant");
    store
        .grant_role(grant("ops", "zalo:shop/u1", 120))
        .await
        .expect("grant again");
    let now = clock.now();
    let grants = store.role_grants(now).await.expect("grants");
    assert_eq!(grants.len(), 2, "one grant per role and account");
    assert_eq!(grants[0].account, "zalo:shop/u1");
    assert_eq!(
        grants[0].expires_at,
        now + jiff::SignedDuration::from_secs(120)
    );
    assert!(
        store
            .revoke_role("ops", "zalo:shop/u2")
            .await
            .expect("revoke")
    );
    assert!(
        !store
            .revoke_role("ops", "zalo:shop/u2")
            .await
            .expect("revoke")
    );
    clock.advance(Duration::from_secs(121));
    assert!(
        store
            .role_grants(clock.now())
            .await
            .expect("grants")
            .is_empty(),
        "expired grants are gone"
    );
    assert!(
        !store
            .revoke_role("ops", "zalo:shop/u1")
            .await
            .expect("revoke"),
        "an expired grant is not revoked"
    );
}

/// Dedupe: first sighting is new, repeats are not, expiry makes it new again; per channel.
async fn dedupe(store: &dyn ConversationStore, clock: &ManualClock) {
    let channel: okbot_core::ChannelId = "zalo:shop".parse().expect("id");
    let ttl = Duration::from_secs(60);
    assert!(
        store
            .mark_seen(&channel, "e1", ttl)
            .await
            .expect("mark_seen")
    );
    assert!(
        !store
            .mark_seen(&channel, "e1", ttl)
            .await
            .expect("mark_seen")
    );
    let other: okbot_core::ChannelId = "zalo:other".parse().expect("id");
    assert!(
        store.mark_seen(&other, "e1", ttl).await.expect("mark_seen"),
        "dedupe is per channel"
    );
    clock.advance(Duration::from_secs(61));
    assert!(
        store
            .mark_seen(&channel, "e1", ttl)
            .await
            .expect("mark_seen"),
        "expired mark"
    );
}

/// Checks the outbound side of a running [`Channel`](okbot_core::Channel): call it while
/// [`Channel::run`](okbot_core::Channel::run) is serving (against your fake platform), with a
/// target in a conversation that channel owns.
///
/// It checks the capabilities are usable, that `send` returns a reference to what it sent, that a
/// message of exactly `max_message_len` is accepted, and that `edit` and `typing` agree with the
/// capabilities.
///
/// # Panics
/// On the first behaviour that differs from the port's contract.
pub async fn channel(channel: &dyn okbot_core::Channel, to: &okbot_core::ReplyTarget) {
    use okbot_core::{ChannelError, RenderedMessage};

    let caps = channel.caps().clone();
    assert!(caps.max_message_len > 0, "max_message_len must be positive");
    assert_eq!(
        &to.conversation.channel,
        channel.id(),
        "the target belongs to this channel"
    );

    let sent = channel
        .send(
            to,
            &RenderedMessage {
                text: "conformance: hello".into(),
            },
        )
        .await
        .expect("send");
    assert_eq!(&sent.target, to, "send returns the target it sent to");
    assert!(
        !sent.message_id.as_str().is_empty(),
        "send returns the platform message id"
    );

    let longest = RenderedMessage {
        text: "x".repeat(caps.max_message_len),
    };
    channel
        .send(to, &longest)
        .await
        .expect("a message of exactly max_message_len is accepted");

    let edited = channel
        .edit(
            &sent,
            &RenderedMessage {
                text: "conformance: edited".into(),
            },
        )
        .await;
    match (caps.edit, edited) {
        (true, Ok(())) | (false, Err(ChannelError::Unsupported(_))) => {}
        (true, Err(e)) => panic!("caps.edit is set but edit failed: {e}"),
        (false, other) => panic!("caps.edit is unset, so edit must be Unsupported: {other:?}"),
    }

    // Typing is best effort: it never fails a turn, even where the platform has none.
    channel
        .typing(to)
        .await
        .expect("typing succeeds (a no-op when unsupported)");
}

/// Checks that [`Channel::run`](okbot_core::Channel::run) returns soon after shutdown.
///
/// # Panics
/// When `run` fails or is still running 10 seconds after the token is cancelled.
pub async fn channel_stops(
    channel: std::sync::Arc<dyn okbot_core::Channel>,
    sink: std::sync::Arc<dyn okbot_core::InboundSink>,
    shutdown: okbot_core::CancellationToken,
) {
    let run = tokio::spawn({
        let shutdown = shutdown.clone();
        async move { channel.run(sink, shutdown).await }
    });
    tokio::time::sleep(Duration::from_millis(200)).await;
    shutdown.cancel();
    tokio::time::timeout(Duration::from_secs(10), run)
        .await
        .expect("run returns within 10 s of shutdown")
        .expect("run does not panic")
        .expect("run returns Ok on shutdown");
}

async fn fires_and_direct_chats(store: &dyn ConversationStore, clock: &ManualClock) {
    use okbot_core::FireState;
    let at = clock.now();
    let first = store.claim_fire("tasks/x/t-1", at).await.expect("claim");
    assert_eq!((first.state, first.attempts), (FireState::Sending, 0));
    store
        .set_fire("tasks/x/t-1", at, FireState::Sending, 2)
        .await
        .expect("set_fire");
    let again = store.claim_fire("tasks/x/t-1", at).await.expect("claim");
    assert_eq!(again.attempts, 2, "a claim keeps the existing record");
    store
        .set_fire("tasks/x/t-1", at, FireState::Sent, 3)
        .await
        .expect("set_fire");
    let sent = store.claim_fire("tasks/x/t-1", at).await.expect("claim");
    assert_eq!(sent.state, FireState::Sent, "a sent firing stays sent");
    let other = store
        .claim_fire("tasks/x/t-1", at + jiff::SignedDuration::from_hours(24))
        .await
        .expect("claim");
    assert_eq!(
        other.state,
        FireState::Sending,
        "each firing time is its own record"
    );
    let recent = store.recent_fires(10).await.expect("recent_fires");
    assert_eq!(recent.len(), 2);

    let channel: ChannelId = "discord:main".parse().expect("valid id");
    let user = UserId::new("u1");
    assert_eq!(
        store
            .direct_chat(&channel, &user)
            .await
            .expect("direct_chat"),
        None
    );
    let dm = ConversationKey::room(channel.clone(), "dm-1");
    store
        .set_direct_chat(&channel, &user, &dm)
        .await
        .expect("set_direct_chat");
    let newer = ConversationKey::room(channel.clone(), "dm-2");
    store
        .set_direct_chat(&channel, &user, &newer)
        .await
        .expect("set_direct_chat");
    assert_eq!(
        store
            .direct_chat(&channel, &user)
            .await
            .expect("direct_chat"),
        Some(newer)
    );
}

async fn pairing(store: &dyn ConversationStore, clock: &ManualClock) {
    let zalo: okbot_core::ChannelId = "zalo:shop".parse().expect("valid id");
    let discord: okbot_core::ChannelId = "discord:shop".parse().expect("valid id");
    let me = okbot_core::UserId::new("me");
    assert_eq!(store.owners().await.expect("owners"), vec![]);

    store
        .create_pairing_code("111111", Duration::from_secs(600))
        .await
        .expect("create");
    assert!(
        store
            .redeem_pairing_code("111111", &zalo, &me)
            .await
            .expect("redeem"),
        "a fresh code pairs"
    );
    assert!(
        !store
            .redeem_pairing_code("111111", &discord, &me)
            .await
            .expect("redeem"),
        "codes are single-use"
    );
    assert_eq!(
        store.owners().await.expect("owners"),
        vec![(zalo.clone(), me.clone())]
    );

    // Expired codes do not pair.
    store
        .create_pairing_code("222222", Duration::from_secs(60))
        .await
        .expect("create");
    clock.advance(Duration::from_secs(61));
    assert!(
        !store
            .redeem_pairing_code("222222", &discord, &me)
            .await
            .expect("redeem")
    );

    // Too many wrong guesses cancel pending codes.
    store
        .create_pairing_code("333333", Duration::from_secs(600))
        .await
        .expect("create");
    for _ in 0..okbot_core::PAIRING_MAX_FAILURES {
        assert!(
            !store
                .redeem_pairing_code("000000", &discord, &me)
                .await
                .expect("redeem")
        );
    }
    assert!(
        !store
            .redeem_pairing_code("333333", &discord, &me)
            .await
            .expect("redeem"),
        "cancelled after too many failures"
    );

    assert!(store.remove_owner(&zalo, &me).await.expect("remove"));
    assert!(!store.remove_owner(&zalo, &me).await.expect("remove"));
    assert_eq!(store.owners().await.expect("owners"), vec![]);
}