|
| 1 | +import { |
| 2 | + ALLOW_TEST_SUITE_WEBSITE, |
| 3 | + describeIf, |
| 4 | + HAS_MENU_SERVICE, |
| 5 | + TEST_SUITE_WEBSITE, |
| 6 | +} from "../lib"; |
| 7 | +import { scrape, scrapeTimeout, idmux, Identity } from "./lib"; |
| 8 | + |
| 9 | +let identity: Identity; |
| 10 | + |
| 11 | +beforeAll(async () => { |
| 12 | + identity = await idmux({ |
| 13 | + name: "menu", |
| 14 | + concurrency: 100, |
| 15 | + credits: 1000000, |
| 16 | + }); |
| 17 | +}, 10000 + scrapeTimeout); |
| 18 | + |
| 19 | +// The `menu` scrape format extracts a structured restaurant menu from a page's |
| 20 | +// HTML. The extraction itself runs in the menu-search Rust service, called over |
| 21 | +// HTTP when MENU_EXTRACTION_SERVICE_URL is set (the product → PRODUCT_EXTRACTION_ |
| 22 | +// SERVICE_URL pattern). These tests target a representative real restaurant menu |
| 23 | +// page for the positive path, and the static test-site non-menu pages |
| 24 | +// (`/about`, `/product`) for the negative / absence paths. Because the format |
| 25 | +// requires the external extractor, we gate on HAS_MENU_SERVICE so the suite only |
| 26 | +// runs where the service is configured (mirroring how the product snips are |
| 27 | +// gated on HAS_PRODUCT_SERVICE); and we also gate on ALLOW_TEST_SUITE_WEBSITE |
| 28 | +// because the negative / absence cases fetch the local test-site. |
| 29 | +describeIf(ALLOW_TEST_SUITE_WEBSITE && HAS_MENU_SERVICE)( |
| 30 | + "Menu scrape format", |
| 31 | + () => { |
| 32 | + const base = TEST_SUITE_WEBSITE; |
| 33 | + // A representative restaurant menu page (Toast online ordering) for the live |
| 34 | + // extraction path. The page embeds a structured menu the service can parse. |
| 35 | + const menuUrl = |
| 36 | + "https://order.toasttab.com/online/the-coffee-shop-123-main-st"; |
| 37 | + const nonMenuUrl = `${base}/about`; |
| 38 | + const productUrl = `${base}/product`; |
| 39 | + |
| 40 | + it.concurrent( |
| 41 | + "extracts a structured menu from a restaurant menu page", |
| 42 | + async () => { |
| 43 | + const response = await scrape( |
| 44 | + { |
| 45 | + url: menuUrl, |
| 46 | + formats: [{ type: "menu" }], |
| 47 | + }, |
| 48 | + identity, |
| 49 | + ); |
| 50 | + |
| 51 | + expect(response.menu).toBeDefined(); |
| 52 | + // Canonical shape: a merchant profile plus an ordered list of sections, |
| 53 | + // each holding items. |
| 54 | + expect(response.menu?.isMenu).toBe(true); |
| 55 | + expect(response.menu?.merchant).toBeDefined(); |
| 56 | + expect(typeof response.menu?.merchant?.name).toBe("string"); |
| 57 | + expect(Array.isArray(response.menu?.sections)).toBe(true); |
| 58 | + expect(response.menu?.sections?.length).toBeGreaterThan(0); |
| 59 | + const section = response.menu?.sections?.[0]; |
| 60 | + expect(section).toBeDefined(); |
| 61 | + expect(typeof section?.name).toBe("string"); |
| 62 | + expect(Array.isArray(section?.items)).toBe(true); |
| 63 | + }, |
| 64 | + scrapeTimeout, |
| 65 | + ); |
| 66 | + |
| 67 | + it.concurrent( |
| 68 | + "sets a no-menu warning for a non-menu page", |
| 69 | + async () => { |
| 70 | + const response = await scrape( |
| 71 | + { |
| 72 | + url: nonMenuUrl, |
| 73 | + formats: [{ type: "menu" }], |
| 74 | + }, |
| 75 | + identity, |
| 76 | + ); |
| 77 | + |
| 78 | + expect(response.menu).toBeUndefined(); |
| 79 | + expect(response.warning).toBeDefined(); |
| 80 | + expect(response.warning).toContain("No menu found"); |
| 81 | + }, |
| 82 | + scrapeTimeout, |
| 83 | + ); |
| 84 | + |
| 85 | + it.concurrent( |
| 86 | + "does not populate menu when the menu format is not requested", |
| 87 | + async () => { |
| 88 | + const response = await scrape( |
| 89 | + { |
| 90 | + url: productUrl, |
| 91 | + formats: ["markdown"], |
| 92 | + }, |
| 93 | + identity, |
| 94 | + ); |
| 95 | + |
| 96 | + // Additive-only contract: a scrape that does not request the `menu` |
| 97 | + // format must be byte-identical in shape to a pre-feature scrape. |
| 98 | + // No `menu` field... |
| 99 | + expect(response.menu).toBeUndefined(); |
| 100 | + // ...and no spurious `warning` (the no-menu warning is only emitted when |
| 101 | + // the format is actually requested). |
| 102 | + expect(response.warning).toBeUndefined(); |
| 103 | + // ...while the rest of the document is present and unchanged. |
| 104 | + expect(response.markdown).toBeDefined(); |
| 105 | + expect(typeof response.markdown).toBe("string"); |
| 106 | + expect(response.markdown?.length).toBeGreaterThan(0); |
| 107 | + expect(response.metadata).toBeDefined(); |
| 108 | + expect(response.metadata.sourceURL ?? response.metadata.url).toBe( |
| 109 | + productUrl, |
| 110 | + ); |
| 111 | + expect(response.metadata.statusCode).toBe(200); |
| 112 | + }, |
| 113 | + scrapeTimeout, |
| 114 | + ); |
| 115 | + }, |
| 116 | +); |
0 commit comments