From 893f9f2388955a42b36136cb91d590bb72f82736 Mon Sep 17 00:00:00 2001 From: Aarya1801 Date: Mon, 11 May 2026 18:57:43 +1000 Subject: [PATCH] TEST009: Add recommendation engine functional test and report --- ...ST009_recommendation_engine_test_report.md | 31 ++++ scripts/test_recommendation_engine.js | 146 ++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 scripts/TEST009_recommendation_engine_test_report.md create mode 100644 scripts/test_recommendation_engine.js diff --git a/scripts/TEST009_recommendation_engine_test_report.md b/scripts/TEST009_recommendation_engine_test_report.md new file mode 100644 index 0000000..709a32e --- /dev/null +++ b/scripts/TEST009_recommendation_engine_test_report.md @@ -0,0 +1,31 @@ + +# TEST009 – Recommendation Engine Functional Testing + +## Tests Performed +1. Created a new test branch from main. +2. Developed a test script (`scripts/test_recommendation_engine.js`) to run the backend recommendation engine with valid sample inputs. +3. Ran the script using Node.js to simulate recommendations for a sample product and user profile. +4. Verified that the recommendations matched the acceptance criteria for filtering and logical ranking. + +### Test Inputs +- **Original Product:** Milk Chocolate (contains milk, high sugar/fat) +- **User Profile:** Vegan, allergic to milk, avoids additive 621 +- **Candidate Products:** Dark Chocolate, Fruit Bar, White Chocolate + +### Results +- **Recommendations Returned:** + - #1: Dark Chocolate (Score: 95, Safety: green, Reasons: Healthier, Safe, Vegan) + - #2: Fruit Bar (Score: 67.5, Safety: green, Reasons: Healthier, Safe) +- **Filtering:** Products with allergens or not matching dietary preferences were filtered out as expected. +- **Scoring:** Results were logically ranked by healthiness and user safety, with the highest scoring and safest products at the top. + +### Issues Found +- No issues or errors were encountered during the test run. The engine performed as expected. + +### Changes Made +- Added a new test script for functional backend testing. +- Adjusted the script for compatibility with Node.js and the current codebase. + +### Acceptance Criteria +- Correct recommendations were returned for valid inputs. +- Results were logically ranked according to filtering and scoring logic. \ No newline at end of file diff --git a/scripts/test_recommendation_engine.js b/scripts/test_recommendation_engine.js new file mode 100644 index 0000000..6dc2a98 --- /dev/null +++ b/scripts/test_recommendation_engine.js @@ -0,0 +1,146 @@ +// @ts-nocheck +// TEST009: Recommendation Engine Functional Test +// This script runs the recommendation engine with valid inputs and prints the results. + +// @ts-ignore +const { getAlternatives } = require("../mobile-app/services/recommendations.js"); + +// Sample original product (simulate a scanned product) +const originalProduct = { + barcode: "12345", + productName: "Milk Chocolate", + categories: ["chocolates", "sweets"], + nutrientLevels: { fat: "high", sugars: "high", salt: "low", "saturated-fat": "high" }, + nutriscoreGrade: "D", + allergens: ["milk"], + traces: "wheat", + additives: ["621"], + labels: ["vegetarian"], + ingredientsText: null, + ingredientsAnalysis: null, + ingredients: [], + tracesFromIngredients: null, + nutriments: {}, + genericName: null, + brand: null, + completeness: 1, + productQuantity: null, + productQuantityUnit: null, + servingQuantity: null, + servingQuantityUnit: null, + images: { root: "", primary: null, variants: {} }, +}; + +// Sample candidate products (alternatives) +const candidates = [ + { + barcode: "23456", + productName: "Dark Chocolate", + categories: ["chocolates", "sweets"], + nutrientLevels: { fat: "moderate", sugars: "moderate", salt: "low", "saturated-fat": "moderate" }, + nutriscoreGrade: "B", + allergens: [], + traces: "", + additives: [], + labels: ["vegan", "gluten-free"], + ingredientsText: null, + ingredientsAnalysis: null, + ingredients: [], + tracesFromIngredients: null, + nutriments: {}, + genericName: null, + brand: null, + completeness: 1, + productQuantity: null, + productQuantityUnit: null, + servingQuantity: null, + servingQuantityUnit: null, + images: { root: "", primary: null, variants: {} }, + }, + { + barcode: "34567", + productName: "Fruit Bar", + categories: ["snacks", "fruit bars"], + nutrientLevels: { fat: "low", sugars: "low", salt: "low", "saturated-fat": "low" }, + nutriscoreGrade: "A", + allergens: ["nuts"], + traces: "", + additives: [], + labels: ["vegetarian", "gluten-free"], + ingredientsText: null, + ingredientsAnalysis: null, + ingredients: [], + tracesFromIngredients: null, + nutriments: {}, + genericName: null, + brand: null, + completeness: 1, + productQuantity: null, + productQuantityUnit: null, + servingQuantity: null, + servingQuantityUnit: null, + images: { root: "", primary: null, variants: {} }, + }, + { + barcode: "45678", + productName: "White Chocolate", + categories: ["chocolates"], + nutrientLevels: { fat: "high", sugars: "high", salt: "high", "saturated-fat": "high" }, + nutriscoreGrade: "E", + allergens: ["milk"], + traces: "wheat", + additives: ["621"], + labels: ["vegetarian"], + ingredientsText: null, + ingredientsAnalysis: null, + ingredients: [], + tracesFromIngredients: null, + nutriments: {}, + genericName: null, + brand: null, + completeness: 1, + productQuantity: null, + productQuantityUnit: null, + servingQuantity: null, + servingQuantityUnit: null, + images: { root: "", primary: null, variants: {} }, + }, +]; + +// Sample user profile +const profile = { + userId: "user-123", + profileId: "profile-123", + firstName: "Test", + lastName: "User", + status: true, + relationship: "Self", + age: 25, + avatarUrl: "", + additives: ["621"], + allergies: ["milk"], + intolerances: [], + dietaryForm: ["vegan"], +}; + +console.log("Running recommendation engine test...\n"); + +const recommendations = getAlternatives(originalProduct, candidates, profile, 5); + +console.log("Recommendations (sorted):\n"); +/** + * @param {{ product: any, score: number, safetyRating: string, reasons: string[] }} rec + * @param {number} idx + */ +recommendations.forEach(function (rec, idx) { + console.log(`#${idx + 1}: ${rec.product.productName}`); + console.log(` Score: ${rec.score}`); + console.log(` Safety: ${rec.safetyRating}`); + console.log(` Reasons: ${rec.reasons.join("; ")}`); +}); + +if (recommendations.length === 0) { + console.log("No suitable recommendations found."); +} + +console.log("\nTest complete.");