Recipes phase 5

This commit is contained in:
Joren de Graaf
2026-10-12 14:51:58 +02:00
parent 2a888e5346
commit fe66dde2fb
9 changed files with 947 additions and 27 deletions
+86 -1
View File
@@ -26,6 +26,7 @@ from oauth2_provider.models import Application
from food.api import create_token
from food.models import FoodLogEntry, FoodProduct, InventoryLocation, KitchenInventoryItem
from planner.timezones import localdate_for_user
from recipes.models import Recipe, RecipeUse
from .server import ClientImageFile, _allowed_file_host, _resolve_image
@@ -211,7 +212,7 @@ class McpHttpTests(LiveServerTestCase):
async def check(client):
names = {tool.name for tool in (await client.list_tools()).tools}
self.assertEqual(len(names), 26)
self.assertEqual(len(names), 41)
self.assertTrue({'food_lookup_product', 'food_create_product', 'food_import_product',
'inventory_import_groceries'} <= names)
today = (await client.call_tool('food_get_today')).structured_content
@@ -323,3 +324,87 @@ class McpHttpTests(LiveServerTestCase):
'transaction_id': used['transaction_id']})).structured_content
self.assertEqual(undone['reverses'], used['transaction_id'])
asyncio.run(self._client(self.secret, check))
def test_recipe_tools_end_to_end_with_separate_stock_and_meal_actions(self):
location = InventoryLocation.objects.create(user=self.a, name='Recipe pantry')
package = KitchenInventoryItem.objects.create(
user=self.a, product=self.a_product, location=location,
original_amount=Decimal('300'), remaining_amount=Decimal('300'), inventory_unit='g')
async def check(client):
created = await client.call_tool('recipe_create', {'recipe': {
'title': 'Pasta supper', 'default_servings': '2',
'instructions_markdown': 'Cook pasta.'}, 'idempotency_key': 'recipe-supper'})
self.assertFalse(created.is_error, created)
recipe_id = created.structured_content['recipe']['id']
again = (await client.call_tool('recipe_create', {'recipe': {
'title': 'Pasta supper', 'default_servings': '2',
'instructions_markdown': 'Cook pasta.'}, 'idempotency_key': 'recipe-supper'})).structured_content
self.assertEqual(again['recipe']['id'], recipe_id)
self.assertEqual((await client.call_tool('recipe_search',
{'query': 'Pasta supper'})).structured_content['count'], 1)
ingredient = (await client.call_tool('recipe_ingredient_add', {'recipe_id': recipe_id,
'ingredient': {'name': 'Pasta', 'quantity': '200', 'unit': 'g',
'inventory_product_id': str(self.a_product.api_id),
'nutrition_product_id': str(self.a_product.api_id)},
'idempotency_key': 'recipe-pasta-ingredient'})).structured_content['ingredient']
self.assertEqual(ingredient['name'], 'Pasta')
self.assertEqual((await client.call_tool('recipe_get',
{'recipe_id': recipe_id})).structured_content['ingredients'][0]['id'], ingredient['id'])
self.assertFalse((await client.call_tool('recipe_ingredient_update', {
'recipe_id': recipe_id, 'ingredient_id': ingredient['id'],
'changes': {'quantity': '150'}})).is_error)
variation = (await client.call_tool('recipe_create_variation', {
'recipe_id': recipe_id, 'title': 'Spicy pasta',
'idempotency_key': 'recipe-spicy'})).structured_content['recipe']
self.assertEqual(variation['parent_id'], recipe_id)
self.assertEqual(variation['ingredients'][0]['quantity'], '150.000')
component = (await client.call_tool('recipe_create', {
'recipe': {'title': 'Pasta topping'}, 'idempotency_key': 'recipe-topping'})).structured_content['recipe']
attached = (await client.call_tool('recipe_subrecipe_add', {
'recipe_id': recipe_id, 'component_id': component['id'],
'idempotency_key': 'recipe-attach-topping'})).structured_content['subrecipe']
self.assertEqual(attached['component_id'], component['id'])
self.assertTrue((await client.call_tool('recipe_subrecipe_add', {
'recipe_id': component['id'], 'component_id': recipe_id})).is_error)
self.assertFalse((await client.call_tool('recipe_subrecipe_remove', {
'recipe_id': recipe_id, 'subrecipe_id': attached['id']})).is_error)
preview = (await client.call_tool('recipe_check_inventory', {
'recipe_id': recipe_id})).structured_content
self.assertTrue(preview['stock_ready'])
self.assertEqual(preview['products'][0]['deductions'][0]['amount'], '150.000')
stock = (await client.call_tool('recipe_deduct_inventory', {
'recipe_id': recipe_id, 'confirmation': preview['confirmation']})).structured_content
self.assertTrue(stock['created'])
replay = (await client.call_tool('recipe_deduct_inventory', {
'recipe_id': recipe_id, 'confirmation': preview['confirmation']})).structured_content
self.assertFalse(replay['created'])
logged = (await client.call_tool('recipe_log_meal', {'recipe_id': recipe_id,
'consumed_servings': '0.5', 'servings': '2', 'meal': 'dinner',
'idempotency_key': 'recipe-meal-supper'})).structured_content
self.assertEqual(logged['nutrition']['calories_kcal'], '131.250')
again = (await client.call_tool('recipe_log_meal', {'recipe_id': recipe_id,
'consumed_servings': '0.5', 'servings': '2', 'meal': 'dinner',
'idempotency_key': 'recipe-meal-supper'})).structured_content
self.assertEqual(again['entry_id'], logged['entry_id'])
self.assertFalse((await client.call_tool('recipe_update', {'recipe_id': recipe_id,
'changes': {'title': 'Updated supper'}})).is_error)
self.assertFalse((await client.call_tool('recipe_undo_inventory', {
'stock_use_id': stock['stock_use_id']})).is_error)
self.assertFalse((await client.call_tool('recipe_ingredient_delete', {
'recipe_id': recipe_id, 'ingredient_id': ingredient['id']})).is_error)
self.assertFalse((await client.call_tool('recipe_delete', {
'recipe_id': component['id']})).is_error)
self.assertFalse((await client.call_tool('recipe_delete', {
'recipe_id': recipe_id})).is_error)
return recipe_id, logged['entry_id']
recipe_id, entry_id = asyncio.run(self._client(self.secret, check))
package.refresh_from_db()
self.assertEqual(package.remaining_amount, Decimal('300'))
self.assertEqual(RecipeUse.objects.count(), 1)
self.assertEqual(FoodLogEntry.objects.get(api_id=entry_id).product_name_snapshot, 'Pasta supper')
async def isolated(client):
self.assertTrue((await client.call_tool('recipe_get', {'recipe_id': recipe_id})).is_error)
self.assertTrue((await client.call_tool('recipe_check_inventory', {'recipe_id': recipe_id})).is_error)
asyncio.run(self._client(self.b_secret, isolated))