|
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | +import { UnsupportedFieldException } from '@/exceptions' |
| 3 | +import type { RecipeFields } from '@/types/recipe.interface' |
| 4 | +import { isList } from '@/utils/ingredients' |
| 5 | +import { load } from 'cheerio' |
| 6 | +import { SchemaOrgPlugin } from '../index' |
| 7 | + |
| 8 | +const minimalJsonLd = ` |
| 9 | +<script type="application/ld+json"> |
| 10 | +{ |
| 11 | + "@graph": [ |
| 12 | + { "@type": "WebSite", "name": "MySite" }, |
| 13 | + { |
| 14 | + "@type": "Recipe", |
| 15 | + "name": "RecipeName", |
| 16 | + "description": "Desc", |
| 17 | + "image": "https://img.jpg", |
| 18 | + "recipeIngredient": [" a ", "b"], |
| 19 | + "recipeInstructions": ["step1", "step2"], |
| 20 | + "recipeCategory": "Cat", |
| 21 | + "recipeYield": "4", |
| 22 | + "totalTime": "PT10M", |
| 23 | + "cookTime": "PT5M", |
| 24 | + "prepTime": "PT5M", |
| 25 | + "recipeCuisine": ["Cuisine"], |
| 26 | + "cookingMethod": "Bake", |
| 27 | + "aggregateRating": { |
| 28 | + "@type": "AggregateRating", |
| 29 | + "@id": "r1", |
| 30 | + "ratingValue": 4.5, |
| 31 | + "ratingCount": 10 |
| 32 | + }, |
| 33 | + "nutrition": { "calories": "100" }, |
| 34 | + "keywords": ["kw1", " kw2 "], |
| 35 | + "suitableForDiet": "http://schema.org/Vegetarian" |
| 36 | + } |
| 37 | + ] |
| 38 | +} |
| 39 | +</script>` |
| 40 | + |
| 41 | +describe('SchemaOrgPlugin', () => { |
| 42 | + const $ = load(minimalJsonLd) |
| 43 | + const plugin = new SchemaOrgPlugin($) |
| 44 | + |
| 45 | + it('supports known recipe fields', () => { |
| 46 | + // biome-ignore lint/complexity/useLiteralKeys: private use only |
| 47 | + const keys = Object.keys(plugin['extractors']) |
| 48 | + expect(plugin.supports('title')).toBe(keys.includes('title')) |
| 49 | + expect(plugin.supports('ingredients')).toBe(true) |
| 50 | + expect(plugin.supports('dietaryRestrictions')).toBe(true) |
| 51 | + expect(plugin.supports('unknown' as keyof RecipeFields)).toBe(false) |
| 52 | + }) |
| 53 | + |
| 54 | + it('extracts simple string fields', () => { |
| 55 | + expect(plugin.extract('siteName')).toBe('MySite') |
| 56 | + expect(plugin.extract('title')).toBe('RecipeName') |
| 57 | + expect(plugin.extract('description')).toBe('Desc') |
| 58 | + expect(plugin.extract('cookingMethod')).toBe('Bake') |
| 59 | + }) |
| 60 | + |
| 61 | + it('extracts image and validates URL', () => { |
| 62 | + expect(plugin.extract('image')).toBe('https://img.jpg') |
| 63 | + }) |
| 64 | + |
| 65 | + it('extracts numeric durations and times', () => { |
| 66 | + expect(plugin.extract('totalTime')).toBe(10) |
| 67 | + expect(plugin.extract('cookTime')).toBe(5) |
| 68 | + expect(plugin.extract('prepTime')).toBe(5) |
| 69 | + expect(plugin.extract('yields')).toBe('4') |
| 70 | + }) |
| 71 | + |
| 72 | + it('extracts ingredient and instruction lists', () => { |
| 73 | + const ingredients = plugin.extract('ingredients') |
| 74 | + |
| 75 | + expect(isList(ingredients)).toBe(true) |
| 76 | + // @ts-expect-error |
| 77 | + expect(Array.from(ingredients)).toEqual(['a', 'b']) |
| 78 | + expect(Array.from(plugin.extract('instructions'))).toEqual([ |
| 79 | + 'step1', |
| 80 | + 'step2', |
| 81 | + ]) |
| 82 | + }) |
| 83 | + |
| 84 | + it('extracts categorical and list fields', () => { |
| 85 | + expect(Array.from(plugin.extract('category'))).toEqual(['Cat']) |
| 86 | + expect(Array.from(plugin.extract('cuisine'))).toEqual(['Cuisine']) |
| 87 | + expect(Array.from(plugin.extract('keywords'))).toEqual(['kw1', 'kw2']) |
| 88 | + }) |
| 89 | + |
| 90 | + it('extracts ratings and counts correctly', () => { |
| 91 | + expect(plugin.extract('ratings')).toBe(4.5) |
| 92 | + expect(plugin.extract('ratingsCount')).toBe(10) |
| 93 | + }) |
| 94 | + |
| 95 | + it('extracts nutrients and dietary restrictions', () => { |
| 96 | + const nutrients = plugin.extract('nutrients') |
| 97 | + expect(nutrients.get('calories')).toBe('100') |
| 98 | + const diets = Array.from(plugin.extract('dietaryRestrictions')) |
| 99 | + expect(diets).toEqual(['Vegetarian']) |
| 100 | + }) |
| 101 | + |
| 102 | + it('throws UnsupportedFieldException for unsupported field', () => { |
| 103 | + expect(() => plugin.extract('foo' as keyof RecipeFields)).toThrow( |
| 104 | + UnsupportedFieldException, |
| 105 | + ) |
| 106 | + }) |
| 107 | + |
| 108 | + it('throws SchemaOrgException for missing required field', () => { |
| 109 | + // JSON-LD missing 'name' for Recipe |
| 110 | + const badJson = `<script type="application/ld+json">{"@type":"Recipe"}</script>` |
| 111 | + const badPlugin = new SchemaOrgPlugin(load(badJson)) |
| 112 | + expect(() => badPlugin.extract('title')).toThrow( |
| 113 | + /Missing required field: title/, |
| 114 | + ) |
| 115 | + }) |
| 116 | + |
| 117 | + it('throws SchemaOrgException for invalid image', () => { |
| 118 | + const badImgJson = `<script type="application/ld+json">{"@type":"Recipe","image":"nope"}</script>` |
| 119 | + const badPlugin = new SchemaOrgPlugin(load(badImgJson)) |
| 120 | + expect(() => badPlugin.extract('image')).toThrow( |
| 121 | + /Invalid value for "image": nope/, |
| 122 | + ) |
| 123 | + }) |
| 124 | +}) |
0 commit comments