tablo-test 0.9.0

In-memory HTTP test client for Tablo panels, re-exported as `tablo::testing`.
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
//! Typed requests to one resource of a mounted panel: [`ResourceClient`], the [`Submission`] a
//! form posts, and the [`Reply`] each request answers with.
//!
//! The client reads every URL from [`tablo_core::url`] and every form key from the resource's
//! [`RecordForm::fields`]: a test follows a renamed slug or form key, fails to compile on a
//! renamed field or action, and panics naming the resource when the panel serves no such page,
//! instead of asserting on a 404.

use std::{any::type_name, convert::Infallible, marker::PhantomData};

use http::{StatusCode, header::LOCATION};
use tablo_core::{Action, FormField, Notification, PanelHandle, RecordForm, Resource, url};
use topcoat::context::Cx;

use crate::{
    TestClient,
    protocol::{
        EmptyTable, ErrorKind, Group, Row, body_string, declined_message, dirty_form, empty_table,
        field_error, field_error_message, form_body, groups, input_value, notification_in,
        response_cookies, row_keys, rows, stale_form,
    },
};

/// The field enum of `R`'s record form, e.g. `PostFormField`.
pub type FieldOf<R> = <<R as Resource>::Form as RecordForm>::Field;

/// Requests to `R` in the panel [`TestClient::panel`] names, built by
/// [`TestClient::resource`].
///
/// ```rust,no_run
/// # use tablo_core::Resource;
/// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns, tablo_core::ModelForm)]
/// # struct Post { #[key] #[auto] id: uuid::Uuid, title: String }
/// # struct PostResource;
/// # impl Resource for PostResource { type Model = Post; type Form = PostForm; }
/// # async fn create(client: &tablo_test::TestClient<'_>) {
/// let posts = client.resource::<PostResource>();
/// let reply = posts
///     .create()
///     .set(PostFormField::Title, "Hello")
///     .submit()
///     .await;
/// assert_eq!(reply.status(), 303);
/// assert_eq!(reply.follow().await.rows().len(), 1);
/// # }
/// ```
pub struct ResourceClient<'c, 'a, R> {
    client: &'c TestClient<'a>,
    panel: &'c PanelHandle,
    resource: PhantomData<fn() -> R>,
}

impl<'c, 'a, R: Resource> ResourceClient<'c, 'a, R> {
    pub(crate) fn new(client: &'c TestClient<'a>, panel: &'c PanelHandle) -> Self {
        Self {
            client,
            panel,
            resource: PhantomData,
        }
    }

    /// `R`'s list URL, e.g. `/admin/posts`.
    ///
    /// # Panics
    ///
    /// When the panel does not mount `R`.
    pub fn url(&self) -> String {
        resolved::<R>(url::resource::<R>(&self.cx()), "a list")
    }

    /// The list page.
    pub async fn list(&self) -> Reply<'c, 'a, FieldOf<R>> {
        self.get(self.url()).await
    }

    /// The list page with `query`, e.g. `"search=ada&page=2"`.
    pub async fn list_with(&self, query: &str) -> Reply<'c, 'a, FieldOf<R>> {
        self.get(format!("{}?{query}", self.url())).await
    }

    /// The create page.
    ///
    /// # Panics
    ///
    /// When `R` serves no create page.
    pub async fn create_page(&self) -> Reply<'c, 'a, FieldOf<R>> {
        self.get(self.create_url()).await
    }

    /// A submission of the create form, posting only the fields [`Submission::set`] names.
    ///
    /// # Panics
    ///
    /// When `R` serves no create page.
    pub fn create(&self) -> Submission<'c, 'a, FieldOf<R>> {
        Submission::new(self.client, self.create_url(), R::Form::fields())
    }

    /// `record`'s detail page.
    ///
    /// # Panics
    ///
    /// When `R` declares no detail page, or its rows have no route.
    pub async fn view(&self, record: &R::Model) -> Reply<'c, 'a, FieldOf<R>> {
        let url = resolved::<R>(url::record::<R>(&self.cx(), record), "a detail page");
        self.get(url).await
    }

    /// `record`'s edit page.
    ///
    /// # Panics
    ///
    /// When `R` has no record form, or its rows have no route.
    pub async fn edit_page(&self, record: &R::Model) -> Reply<'c, 'a, FieldOf<R>> {
        self.get(self.edit_url(record)).await
    }

    /// A submission of `record`'s edit form. It posts only the fields [`Submission::set`] names,
    /// and the edit leaves every other field as stored.
    ///
    /// # Panics
    ///
    /// When `R` has no record form, or its rows have no route.
    pub fn edit(&self, record: &R::Model) -> Submission<'c, 'a, FieldOf<R>> {
        Submission::new(self.client, self.edit_url(record), R::Form::fields())
    }

    /// A confirmed delete of `record`, as the delete dialog posts it.
    ///
    /// # Panics
    ///
    /// When `R`'s rows have no route.
    pub fn delete(&self, record: &R::Model) -> Submission<'c, 'a, Infallible> {
        let url = resolved::<R>(url::delete::<R>(&self.cx(), record), "a delete route");
        Submission::new(self.client, url, Vec::new()).set_key(CONFIRM, "1")
    }

    /// A confirmed run of the action `A` on `record`. An input the action asks for goes in with
    /// [`Submission::set_key`].
    ///
    /// # Panics
    ///
    /// When `R` does not declare `A` on a record.
    pub fn action<A: Action<R>>(&self, record: &R::Model) -> Submission<'c, 'a, Infallible> {
        let url = url::action::<R, A>(&self.cx(), record).unwrap_or_else(|| {
            panic!(
                "`{}` declares no record action `{}`",
                type_name::<R>(),
                A::NAME
            )
        });
        Submission::new(self.client, url, Vec::new()).set_key(CONFIRM, "1")
    }

    /// A confirmed run of the bulk action `A` on the rows keyed `keys`, as [`Reply::row_keys`]
    /// reads them.
    ///
    /// # Panics
    ///
    /// When `R` does not declare `A` in its bulk bar.
    pub fn bulk_action<A: Action<R>>(
        &self,
        keys: impl IntoIterator<Item = impl AsRef<str>>,
    ) -> Submission<'c, 'a, Infallible> {
        let url = url::bulk_action::<R, A>(&self.cx()).unwrap_or_else(|| {
            panic!(
                "`{}` declares no bulk action `{}`",
                type_name::<R>(),
                A::NAME
            )
        });
        let ids = keys
            .into_iter()
            .map(|key| key.as_ref().to_string())
            .collect::<Vec<_>>()
            .join(",");
        Submission::new(self.client, url, Vec::new())
            .set_key(IDS, &ids)
            .set_key(CONFIRM, "1")
    }

    /// A confirmed run of the bulk action `A` on every record the list's `query` selects, as the
    /// bulk bar's Select all matching posts it: `query` spells the list's URL query, without the
    /// leading `?`, such as `f.status=draft`.
    ///
    /// # Panics
    ///
    /// When `R` does not declare `A` in its bulk bar.
    pub fn bulk_action_matching<A: Action<R>>(
        &self,
        query: &str,
    ) -> Submission<'c, 'a, Infallible> {
        let url = url::bulk_action::<R, A>(&self.cx()).unwrap_or_else(|| {
            panic!(
                "`{}` declares no bulk action `{}`",
                type_name::<R>(),
                A::NAME
            )
        });
        Submission::new(self.client, url, Vec::new())
            .set_key(MATCHING, query)
            .set_key(CONFIRM, "1")
    }

    fn create_url(&self) -> String {
        resolved::<R>(url::create::<R>(&self.cx()), "a create page")
    }

    fn edit_url(&self, record: &R::Model) -> String {
        resolved::<R>(url::edit::<R>(&self.cx(), record), "an edit page")
    }

    fn cx(&self) -> Cx {
        self.panel.enter(&self.panel.context())
    }

    async fn get(&self, url: String) -> Reply<'c, 'a, FieldOf<R>> {
        Reply::read(self.client, self.client.get(&url).await, R::Form::fields()).await
    }
}

/// The key a confirmation dialog posts, which a delete and an action that asks first require.
const CONFIRM: &str = "confirm";

/// The key the bulk bar posts its selection in, comma-separated.
const IDS: &str = "ids";

/// The key the bulk bar posts the list's query in when it selects every matching record.
const MATCHING: &str = "matching";

/// The key the create page's Create & create another button posts.
const CREATE_ANOTHER: &str = "create_another";

fn resolved<R>(url: Option<String>, what: &str) -> String {
    url.unwrap_or_else(|| panic!("the panel serves `{}` no {what}", type_name::<R>()))
}

/// A form post being built: the pairs it sends, keyed by the form's field enum `K`.
///
/// A record form's field names its key, so [`set`](Self::set) takes `PostFormField::Title`
/// rather than `"title"`. [`set_key`](Self::set_key) posts a key no field enum names: an
/// action's input, one key of an embedded field, a repeater row's key.
#[must_use = "a submission posts nothing until `submit`"]
pub struct Submission<'c, 'a, K> {
    client: &'c TestClient<'a>,
    url: String,
    fields: Vec<FormField<K>>,
    pairs: Vec<(String, String)>,
}

impl<'c, 'a, K: PartialEq> Submission<'c, 'a, K> {
    fn new(client: &'c TestClient<'a>, url: String, fields: Vec<FormField<K>>) -> Self {
        Self {
            client,
            url,
            fields,
            pairs: Vec::new(),
        }
    }

    /// The URL the submission posts to.
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Posts `value` as `field`, replacing any value set before.
    ///
    /// # Panics
    ///
    /// When `field` binds more than one key, as an embedded field does: set each with
    /// [`set_key`](Self::set_key).
    pub fn set(self, field: K, value: &str) -> Self {
        let key = single_key(&self.fields, &field).to_string();
        self.set_key(&key, value)
    }

    /// Posts `value` as one more value of `field`, as a multiple choice posts each pick.
    ///
    /// # Panics
    ///
    /// When `field` binds more than one key.
    pub fn add(mut self, field: K, value: &str) -> Self {
        let key = single_key(&self.fields, &field).to_string();
        self.pairs.push((key, value.to_string()));
        self
    }

    /// Posts `value` under `key`, replacing any value set before.
    pub fn set_key(mut self, key: &str, value: &str) -> Self {
        self.pairs.retain(|(kept, _)| kept != key);
        self.pairs.push((key.to_string(), value.to_string()));
        self
    }

    /// Posts as the create page's Create & create another button does, which lands back on a
    /// fresh create form.
    pub fn create_another(self) -> Self {
        self.set_key(CREATE_ANOTHER, "1")
    }

    /// Posts as the create page a creatable choice's dialog frames does, which lands on the page
    /// [`Reply::picked`] reads.
    pub fn picking(mut self) -> Self {
        let joint = if self.url.contains('?') { '&' } else { '?' };
        self.url = format!("{}{joint}pick=1", self.url);
        self
    }

    /// Posts the pairs with a fresh CSRF token, as [`TestClient::submit`] does.
    pub async fn submit(self) -> Reply<'c, 'a, K> {
        let pairs: Vec<(&str, &str)> = self
            .pairs
            .iter()
            .map(|(key, value)| (key.as_str(), value.as_str()))
            .collect();
        let response = self.client.submit(&self.url, &form_body(&pairs)).await;
        Reply::read(self.client, response, self.fields).await
    }
}

fn single_key<'f, K: PartialEq>(fields: &'f [FormField<K>], field: &K) -> &'f str {
    let field = fields
        .iter()
        .find(|candidate| candidate.field == *field)
        .expect("a field enum's variant is one of its form's fields");
    match field.keys.as_slice() {
        [key] => key,
        keys => panic!(
            "`{}` binds the keys {keys:?}: post each with `set_key`",
            field.name
        ),
    }
}

/// A response with its body read, keyed by the form's field enum `K` for the queries over its
/// form.
///
/// The queries are those of the free functions this crate exports, over the response's HTML.
pub struct Reply<'c, 'a, K> {
    client: &'c TestClient<'a>,
    fields: Vec<FormField<K>>,
    status: StatusCode,
    location: Option<String>,
    cookies: Vec<(String, String)>,
    html: String,
}

impl<'c, 'a, K: PartialEq> Reply<'c, 'a, K> {
    async fn read(
        client: &'c TestClient<'a>,
        response: http::Response<topcoat::router::Body>,
        fields: Vec<FormField<K>>,
    ) -> Self {
        let location = response
            .headers()
            .get(LOCATION)
            .and_then(|value| value.to_str().ok())
            .map(str::to_string);
        let cookies = response_cookies(&response);
        Self {
            client,
            fields,
            status: response.status(),
            location,
            cookies,
            html: body_string(response).await,
        }
    }

    pub fn status(&self) -> StatusCode {
        self.status
    }

    /// The `Location` a redirect names.
    pub fn location(&self) -> Option<&str> {
        self.location.as_deref()
    }

    /// The `(name, value)` pairs the response's `Set-Cookie` headers carry.
    pub fn cookies(&self) -> &[(String, String)] {
        &self.cookies
    }

    /// The flash notification the response set for the next page.
    pub fn notification(&self) -> Option<Notification> {
        notification_in(&self.cookies)
    }

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

    /// Follows the redirect, carrying the cookies the response set, as a browser does.
    ///
    /// # Panics
    ///
    /// When the response names no `Location`.
    pub async fn follow(&self) -> Reply<'c, 'a, K>
    where
        K: Clone,
    {
        let location = self
            .location
            .as_deref()
            .unwrap_or_else(|| panic!("a {} response redirects nowhere", self.status));
        let response = self.client.cookies(&self.cookies).get(location).await;
        Reply::read(self.client, response, self.fields.clone()).await
    }

    /// See [`picked`](crate::picked).
    pub fn picked(&self) -> Option<crate::Picked> {
        crate::picked(&self.html)
    }

    /// See [`create_option_links`](crate::create_option_links).
    pub fn create_option_links(&self) -> Vec<crate::CreateOptionLink> {
        crate::create_option_links(&self.html)
    }

    /// See [`rows`](crate::rows).
    pub fn rows(&self) -> Vec<Row> {
        rows(&self.html)
    }

    /// See [`row_keys`](crate::row_keys).
    pub fn row_keys(&self) -> Vec<String> {
        row_keys(&self.html)
    }

    /// See [`groups`](crate::groups).
    pub fn groups(&self) -> Vec<Group> {
        groups(&self.html)
    }

    /// See [`empty_table`](crate::empty_table).
    pub fn empty_table(&self) -> Option<EmptyTable> {
        empty_table(&self.html)
    }

    /// See [`tabs`](crate::tabs).
    pub fn tabs(&self) -> Vec<crate::Tab> {
        crate::tabs(&self.html)
    }

    /// See [`wizard_steps`](crate::wizard_steps).
    pub fn wizard_steps(&self) -> Vec<crate::WizardStep> {
        crate::wizard_steps(&self.html)
    }

    /// See [`sections`](crate::sections).
    pub fn sections(&self) -> Vec<crate::Section> {
        crate::sections(&self.html)
    }

    /// Whether the edit form refused the save as stale; see [`stale_form`](crate::stale_form).
    pub fn stale(&self) -> bool {
        stale_form(&self.html)
    }

    /// The message a record fn declined the save with; see
    /// [`declined_message`](crate::declined_message).
    pub fn declined(&self) -> Option<String> {
        declined_message(&self.html)
    }

    /// Whether the form renders dirty, guarding what it holds against leaving the page; see
    /// [`dirty_form`](crate::dirty_form).
    pub fn dirty(&self) -> bool {
        dirty_form(&self.html)
    }

    /// Why `field`'s error slot renders, read under its first key; see
    /// [`field_error`](crate::field_error).
    pub fn field_error(&self, field: K) -> Option<ErrorKind> {
        field_error(&self.html, self.first_key(&field))
    }

    /// The message `field`'s error slot renders; see
    /// [`field_error_message`](crate::field_error_message).
    pub fn field_error_message(&self, field: K) -> Option<String> {
        field_error_message(&self.html, self.first_key(&field))
    }

    /// The value `field`'s input renders, as an edit page fills it; see
    /// [`input_value`](crate::input_value).
    pub fn input_value(&self, field: K) -> Option<String> {
        input_value(&self.html, self.first_key(&field))
    }

    fn first_key(&self, field: &K) -> &str {
        self.fields
            .iter()
            .find(|candidate| candidate.field == *field)
            .and_then(|field| field.keys.first())
            .expect("a field enum's variant binds a key of its form")
    }
}