about summary refs log tree commit diff stats
path: root/crates/rocie-server/tests/recipies/mod.rs
blob: 15680f162ff52b5d643ebae4e0f04c1978c9587c (plain) (blame)
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
use rocie_client::{
    apis::{
        api_get_auth_recipe_api::{recipe_by_id, recipe_by_name},
        api_set_auth_product_api::register_product,
        api_set_auth_recipe_api::add_recipe,
        api_set_auth_unit_property_api::register_unit_property,
    },
    models::{
        Content, ContentOneOf, CooklangRecipe, Ingredient, IngredientOneOf1NotRegisteredProduct,
        Item, ItemOneOf, ItemOneOf1, ItemOneOf1Ingredient, ItemOneOf3, ItemOneOfText, Metadata,
        NameAndUrl, ProductStub, RecipeStub, Section, Step, UnitPropertyStub,
    },
};

use crate::testenv::{TestEnv, init::function_name, log::request};

#[expect(clippy::unnecessary_wraps)]
fn name_and_url(name: &str, url: &str) -> Option<NameAndUrl> {
    Some(NameAndUrl {
        name: if name.is_empty() {
            None
        } else {
            Some(name.to_owned())
        },
        url: if url.is_empty() {
            None
        } else {
            Some(url.to_owned())
        },
    })
}

#[tokio::test]
async fn test_recipe_metadata() {
    let env = TestEnv::new(function_name!()).await;

    let recipe_id = request!(
        env,
        add_recipe(RecipeStub {
            content: "
---
author: James Connor
description: Meaty curry with sharp anacado source and a burning d-pad.
source: https://google.com/search?q=test
title: Curry
---
"
            .to_owned(),
            name: "Curry".to_owned(),
            parent: None,
        })
    );

    let output = request!(env, recipe_by_id(recipe_id));

    assert_eq!(output.name, "Curry".to_owned());
    assert_eq!(
        output.content,
        CooklangRecipe {
            cookware: vec![],
            ingredients: vec![],
            metadata: Metadata {
                author: name_and_url("James Connor", ""),
                description: Some(
                    "Meaty curry with sharp anacado source and a burning d-pad.".to_owned()
                ),
                source: name_and_url("", "https://google.com/search?q=test"),
                tags: None,
                title: Some("Curry".to_owned())
            },
            sections: vec![],
            timers: vec![]
        }
    );
}

#[tokio::test]
async fn test_recipe_whitespace() {
    let env = TestEnv::new(function_name!()).await;

    let name = "      Curry  ".to_owned();

    request!(
        env,
        add_recipe(RecipeStub {
            content: " nothing really ".to_owned(),
            name: name.clone(),
            parent: None,
        })
    );

    let output = request!(env, recipe_by_name(&name));

    assert_eq!(output.name, name);
}

#[tokio::test]
#[expect(clippy::too_many_lines)]
async fn test_recipe_contents() {
    let env = TestEnv::new(function_name!()).await;

    let up = request!(
        env,
        register_unit_property(UnitPropertyStub {
            description: None,
            name: "mass".to_owned()
        })
    );

    let rice_id = request!(
        env,
        register_product(ProductStub {
            description: None,
            name: "rice".to_owned(),
            parent: None,
            unit_property: up,
        })
    );

    let recipe_id = request!(
        env,
        add_recipe(RecipeStub {
            content: "
---
author: James Connor
title: Curry
---
Add @rice{} and @water{200%ml} to a pot.

Now add @curry-spice{200%g} let rest for ~{20%min}.
"
            .to_owned(),
            name: "Curry".to_owned(),
            parent: None,
        })
    );

    let output = request!(env, recipe_by_id(recipe_id));

    assert_eq!(output.name, "Curry".to_owned());
    assert_eq!(
        output.content.ingredients,
        vec![
            Ingredient::IngredientOneOf(rocie_client::models::IngredientOneOf {
                registered_product: rocie_client::models::IngredientOneOfRegisteredProduct {
                    alias: None,
                    id: rice_id,
                    quantity: None,
                }
            }),
            Ingredient::IngredientOneOf1(rocie_client::models::IngredientOneOf1 {
                not_registered_product: IngredientOneOf1NotRegisteredProduct {
                    name: "water".to_owned(),
                    quantity: None,
                }
            }),
            Ingredient::IngredientOneOf1(rocie_client::models::IngredientOneOf1 {
                not_registered_product: IngredientOneOf1NotRegisteredProduct {
                    name: "curry-spice".to_owned(),
                    quantity: None,
                }
            }),
        ]
    );

    assert_eq!(
        output.content.sections,
        vec![Section {
            content: vec![
                Content::ContentOneOf(ContentOneOf {
                    step: Step {
                        items: vec![
                            Item::ItemOneOf(ItemOneOf {
                                text: ItemOneOfText {
                                    value: "Add ".to_owned()
                                }
                            }),
                            Item::ItemOneOf1(ItemOneOf1 {
                                ingredient: ItemOneOf1Ingredient { index: 0 }
                            }),
                            Item::ItemOneOf(ItemOneOf {
                                text: ItemOneOfText {
                                    value: " and ".to_owned()
                                }
                            }),
                            Item::ItemOneOf1(ItemOneOf1 {
                                ingredient: ItemOneOf1Ingredient { index: 1 }
                            }),
                            Item::ItemOneOf(ItemOneOf {
                                text: ItemOneOfText {
                                    value: " to a pot.".to_owned()
                                }
                            })
                        ],
                        number: 1
                    }
                }),
                Content::ContentOneOf(ContentOneOf {
                    step: Step {
                        items: vec![
                            Item::ItemOneOf(ItemOneOf {
                                text: ItemOneOfText {
                                    value: "Now add ".to_owned()
                                }
                            }),
                            Item::ItemOneOf1(ItemOneOf1 {
                                ingredient: ItemOneOf1Ingredient { index: 2 }
                            }),
                            Item::ItemOneOf(ItemOneOf {
                                text: ItemOneOfText {
                                    value: " let rest for ".to_owned()
                                }
                            }),
                            Item::ItemOneOf3(ItemOneOf3 {
                                timer: ItemOneOf1Ingredient { index: 0 }
                            }),
                            Item::ItemOneOf(ItemOneOf {
                                text: ItemOneOfText {
                                    value: ".".to_owned()
                                }
                            })
                        ],
                        number: 2
                    }
                })
            ],
            name: None
        }]
    );
}

#[tokio::test]
async fn test_recipe_full_parse() {
    let env = TestEnv::new(function_name!()).await;

    // Recipe source: https://cook.md/https://bbcgoodfood.com/recipes/easy-pancakes
    let recipe_id = request!(
        env,
        add_recipe(RecipeStub {
            content: "
---
title: Easy pancakes
description: Learn how to make the perfect pancakes every time with our foolproof easy crêpe recipe – elaborate flip optional
image: https://images.immediate.co.uk/production/volatile/sites/30/2020/08/recipe-image-legacy-id-1273477_8-ad36e3b.jpg?resize=440,400
nutrition:
  calories: 61 calories
  fat: 2 grams fat
  saturated fat: 1 grams saturated fat
  carbohydrates: 7 grams carbohydrates
  sugar: 1 grams sugar
  protein: 3 grams protein
  sodium: 0.1 milligram of sodium
tags: Cassie Best, Cook school, Crepe, Crêpes, easy pancakes, Flip, Flipping, Good for you, healthy pancakes, How to make pancakes, Make ahead, Pancake day, Pancake filling, Shrove Tuesday, Skills, thin pancakes
source: https://bbcgoodfood.com/recipes/easy-pancakes
author: Cassie Best
prep time: 10 minutes
course: Breakfast, Brunch, Main course
time required: 30 minutes
cook time: 20 minutes
servings: Makes 12
cuisine: British
diet: Vegetarian
---

Put @plain flour{100%g}, @eggs{2}(large), @milk{300%ml}, @sunflower oil{1%tbsp} and a pinch of @salt{} into a #bowl{} or large jug, then whisk to a smooth batter. This should be similar in consistency to single cream.

Set aside for ~{30%minutes} to rest if you have time, or start cooking straight away.

Set a #medium frying pan{} or #crêpe pan{} over a medium heat and carefully wipe it with some oiled kitchen paper.

When hot, cook your pancakes for ~{1%minute} on each side until golden, using around half a ladleful of batter per pancake. Keep them warm in a low oven as you make the rest.

Serve with @?lemon wedges{} (optional) and @?caster sugar{} (optional), or your favourite filling. Once cold, you can layer the pancakes between baking parchment, then wrap in cling film and freeze for up to two months.
"
            .to_owned(),
            name: "Easy pancakes".to_owned(),
            parent: None,
        })
    );

    let output = request!(env, recipe_by_id(recipe_id));

    assert_eq!(output.name, "Easy pancakes".to_owned());
}