Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
feat(demo): fill the demo brand's Site Audit, AI traffic, catalog and fan-out intents (demo brand, part 3) (#1736)
Cihan3ce802d01992
diff --git a/server/src/lib/audit/context.js b/server/src/lib/audit/context.jsindex 8bd827bc4..3b6c39f51 100644--- a/server/src/lib/audit/context.js+++ b/server/src/lib/audit/context.js@@ -69,33 +69,62 @@ export async function buildAuditContext(rawUrl, { query = null } = {}) {throw Object.assign(new Error(`Failed to fetch page (status ${page.status})`), { status: 502 });}- const $ = cheerio.load(page.html);+ const [robotsTxt, llmsTxt] = await Promise.all([+ fetchText(`${origin}/robots.txt`),+ fetchText(`${origin}/llms.txt`),+ ]);++ return auditContextOf({+ url,+ statusCode: page.status,+ html: page.html,+ robotsTxt,+ llmsTxt,+ query,+ });+}++/**+ * The context of a page already read: its HTML parsed once, its text+ * extracted. Pure, so a page that isn't fetched (the demo brand's, lib/demo)+ * is read exactly as a fetched one.+ *+ * @param {{ url: string, statusCode: number, html: string, robotsTxt: string|null,+ * llmsTxt: string|null, query?: string|null, now?: number }} page+ * @returns {AuditContext}+ */+export function auditContextOf({+ url,+ statusCode,+ html,+ robotsTxt,+ llmsTxt,+ query = null,+ now = Date.now(),+}) {+ const u = new URL(url);+ const $ = cheerio.load(html);// Plain-text extraction on a throwaway parse so the shared `$` keeps its// <script type="application/ld+json"> blocks intact for structural signals.- const $text = cheerio.load(page.html);+ const $text = cheerio.load(html);$text('script, style, noscript, template, svg').remove();const text = $text('body').text().replace(/\s+/g, ' ').trim();const wordCount = text ? text.split(/\s+/).length : 0;- const [robotsTxt, llmsTxt] = await Promise.all([- fetchText(`${origin}/robots.txt`),- fetchText(`${origin}/llms.txt`),- ]);-return {url,- origin,+ origin: `${u.protocol}//${u.host}`,protocol: u.protocol.replace(':', ''),- statusCode: page.status,- html: page.html,- htmlBytes: Buffer.byteLength(page.html, 'utf8'),+ statusCode,+ html,+ htmlBytes: Buffer.byteLength(html, 'utf8'),$,text,wordCount,robotsTxt,llmsTxt,query,- now: Date.now(),+ now,};}diff --git a/server/src/lib/audit/context.test.js b/server/src/lib/audit/context.test.jsnew file mode 100644index 000000000..4cf2a394c--- /dev/null+++ b/server/src/lib/audit/context.test.js@@ -0,0 +1,40 @@+import { describe, expect, it } from 'vitest';+import { auditContextOf } from './context.js';+import { runSignals } from './engine.js';++/** A page already read, made the context every signal reads (auditContextOf). */+describe('auditContextOf', () => {+ const html = `<!doctype html><html><head><title>Fresh beans</title>+<script type="application/ld+json">{"@context":"https://schema.org","@type":"Article","headline":"Fresh beans"}</script>+<style>body { color: red }</style></head>+<body><h1>Fresh beans</h1><p>Coffee tastes best soon after roasting.</p><script>track()</script></body></html>`;+ const ctx = auditContextOf({+ url: 'https://shop.example/blog/fresh',+ statusCode: 200,+ html,+ robotsTxt: null,+ llmsTxt: null,+ now: 1_700_000_000_000,+ });++ it('reads the address, the text without scripts or styles, and its words', () => {+ expect(ctx).toMatchObject({+ url: 'https://shop.example/blog/fresh',+ origin: 'https://shop.example',+ protocol: 'https',+ statusCode: 200,+ query: null,+ now: 1_700_000_000_000,+ });+ expect(ctx.text).toContain('Coffee tastes best soon after roasting.');+ expect(ctx.text).not.toMatch(/track\(|color/);+ expect(ctx.wordCount).toBe(ctx.text.split(/\s+/).length);+ expect(ctx.htmlBytes).toBe(Buffer.byteLength(html, 'utf8'));+ });++ it('keeps the JSON-LD in the parsed page for the structural signals', async () => {+ expect(ctx.$('script[type="application/ld+json"]')).toHaveLength(1);+ const results = await runSignals(ctx);+ expect(results.find((r) => r.key === 'json-ld-presence').status).toBe('pass');+ });+});diff --git a/server/src/lib/catalog/sources.js b/server/src/lib/catalog/sources.jsindex 984da740a..e40774dfc 100644--- a/server/src/lib/catalog/sources.js+++ b/server/src/lib/catalog/sources.js@@ -148,7 +148,8 @@ export function redact(message, credentials) {/*** The live products a source may keep (ECOM-019): its workspace's cap less- * the live products of the workspace's other sources, on any of its brands.+ * the live products of the workspace's other sources, on any of its brands+ * but its demo brand (00380), whose sample catalog never counts.* `room` is Infinity (and `cap` -1) on a self-hosted server, or for a plan* without a cap.*/@@ -169,7 +170,8 @@ export async function productRoom(const { data: brands, error: brandsError } = await db.from('brands').select('id')- .eq('organization_id', brand.organization_id);+ .eq('organization_id', brand.organization_id)+ .eq('is_demo', false);if (brandsError) throw new Error(`brands read: ${brandsError.message}`);const { count, error: countError } = await db.from('catalog_products')diff --git a/server/src/lib/catalog/sources.test.js b/server/src/lib/catalog/sources.test.jsindex e4144e50b..9ceee28e5 100644--- a/server/src/lib/catalog/sources.test.js+++ b/server/src/lib/catalog/sources.test.js@@ -581,14 +581,17 @@ describe("the plan's product cap (ECOM-019)", () => {fake.rows('brands').push(- { id: 'brand-1', organization_id: 'org-1' },- { id: 'brand-2', organization_id: 'org-1' },- { id: 'brand-x', organization_id: 'org-2' },+ { id: 'brand-1', organization_id: 'org-1', is_demo: false },+ { id: 'brand-2', organization_id: 'org-1', is_demo: false },+ { id: 'brand-demo', organization_id: 'org-1', is_demo: true },+ { id: 'brand-x', organization_id: 'org-2', is_demo: false },);fake.rows('catalog_products').push(// The workspace's other brand: one live, one removed.{ id: 'p1', brand_id: 'brand-2', source: OTHER, external_id: 'X1', removed_at: null },{ id: 'p2', brand_id: 'brand-2', source: OTHER, external_id: 'X2', removed_at: NOW },+ // The workspace's demo brand's sample catalog (00380): never counts.+ { id: 'pd', brand_id: 'brand-demo', source: OTHER, external_id: 'D1', removed_at: null },// Another workspace's, and this source's own: neither counts.{ id: 'p3', brand_id: 'brand-x', source: OTHER, external_id: 'X3', removed_at: null },{diff --git a/server/src/lib/demo/catalog.js b/server/src/lib/demo/catalog.jsnew file mode 100644index 000000000..ead40d10f--- /dev/null+++ b/server/src/lib/demo/catalog.js@@ -0,0 +1,266 @@+/**+ * The demo brand's product catalog (demo-site), for Product Visibility, the+ * Content Editor's shopping path and the catalog's category pages.+ *+ * Brewline's catalog comes from a product feed URL, as a store's does: the+ * feed is written here in Google's XML, read by the feed parser+ * (feeds/index.js) and turned into products and variants by the catalog+ * sync's own catalogRecords and hashOf (catalog/sync.js). Rows keep the+ * demo's stable ids rather than the database's, so a rebuild keeps links to+ * a product. The products AI answers show as product cards (brewline.js)+ * are the same products here: same page, same title, same price.+ *+ * No product has an image: the demo loads nothing from outside.+ */++import supabaseAdmin from '../../config/supabase.js';+import { sourceKey } from '../catalog/sources.js';+import { catalogRecords, hashOf } from '../catalog/sync.js';+import { parseFeed } from '../feeds/index.js';+import { BRAND } from './brewline.js';+import { demoId } from './random.js';+import { DAY_MS } from './site-common.js';+import { allSettledOrThrow, demoWriter } from './write.js';+import { productDescription, variantsOf } from './site-html.js';+import { CATALOG, CATEGORY_DECISIONS, REMOVED_PRODUCT, SITE_URL, pageUrl } from './site-world.js';++/** The feed's address and when it was added; it is read every morning. */+export const FEED_URL = `${SITE_URL}/feeds/google-shopping.xml`;+const SOURCE_DAYS_AGO = 30;+const SYNC_HOUR_UTC = 4;++const xml = (text) =>+ String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');++/** A variant's title: the product's, its grind and size ("…, Whole Bean 12 oz") or its bags. */+function variantTitle(product, label) {+ const name = `Brewline ${product.title.replace(/^Brewline /, '')}`;+ if (!label) return name;+ return product.sizes ? `${name}, Whole Bean ${label}` : `${name} - ${label}`;+}++/** One feed item per variant, as a Google product feed lists them. */+export function feedItems(product) {+ const variants = variantsOf(product);+ return variants.map(([label, price, availability, sale], i) => {+ const suffix = label ? `-${label.replace(/\s+/g, '').toUpperCase()}` : '';+ return {+ id: `${product.id}${suffix}`,+ item_group_id: variants.length > 1 ? product.id : null,+ title: variantTitle(product, label),+ description: productDescription(product),+ link:+ i === 0+ ? pageUrl(product.path)+ : `${pageUrl(product.path)}?${product.sizes ? 'size' : 'bags'}=${label.replace(/\s+/g, '').toLowerCase()}`,+ brand: BRAND.name,+ mpn: `${product.id}${suffix}`,+ price: `${price.toFixed(2)} USD`,+ sale_price: sale ? `${sale.toFixed(2)} USD` : null,+ availability: availability ?? 'in stock',+ condition: 'new',+ size: label,+ product_type: product.type,+ };+ });+}++/** The feed as Brewline's store publishes it: RSS 2.0 with Google's g: namespace. */+export function feedXml(products) {+ const items = products.flatMap(feedItems).map(+ (item) =>+ `<item>${Object.entries(item)+ .filter(([, value]) => value !== null && value !== undefined)+ .map(([name, value]) =>+ name === 'title' || name === 'description' || name === 'link'+ ? `<${name}>${xml(value)}</${name}>`+ : `<g:${name}>${xml(value)}</g:${name}>`,+ )+ .join('')}</item>`,+ );+ return `<?xml version="1.0" encoding="UTF-8"?>+<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">+<channel>+<title>${xml(BRAND.name)}</title>+<link>${SITE_URL}</link>+<description>${xml(BRAND.name)} product feed</description>+${items.join('\n')}+</channel>+</rss>`;+}++/**+ * The catalog's rows for one source's products, as syncCatalog writes them+ * (its upsert's columns), with the demo's ids.+ */+function recordRows(brandId, source, items, { at, firstSeen, removedAt = null }) {+ const { products, variants } = catalogRecords(items);+ const productIds = new Map(+ products.map((p) => [p.external_id, demoId(brandId, `catalog-product:${p.external_id}`)]),+ );+ return {+ products: products.map((product) => ({+ id: productIds.get(product.external_id),+ brand_id: brandId,+ source,+ ...product,+ content_hash: hashOf(product),+ removed_at: removedAt,+ first_seen_at: firstSeen,+ updated_at: removedAt ?? at,+ })),+ variants: variants.map((variant) => {+ const { product_external_id, images: _images, ...columns } = variant;+ return {+ id: demoId(brandId, `catalog-variant:${variant.external_id}`),+ brand_id: brandId,+ source,+ product_id: productIds.get(product_external_id),+ ...columns,+ content_hash: hashOf(variant),+ removed_at: removedAt,+ first_seen_at: firstSeen,+ updated_at: removedAt ?? at,+ };+ }),+ };+}++/** When the feed was last read: this morning's sync, or yesterday's before it ran. */+function lastSync(now) {+ const at = new Date(now);+ at.setUTCHours(SYNC_HOUR_UTC, 10, 0, 0);+ if (at > now) at.setUTCDate(at.getUTCDate() - 1);+ return at;+}++/**+ * Every catalog row of the demo brand, built in memory: the same every time+ * but for the dates, which count back from `now`.+ *+ * @param {{ brandId: string, now: Date }} args+ */+export function catalogRows({ brandId, now }) {+ const sourceId = demoId(brandId, 'catalog-source');+ const source = sourceKey(sourceId);+ const createdAt = new Date(now.getTime() - SOURCE_DAYS_AGO * DAY_MS).toISOString();+ const syncedAt = lastSync(now).toISOString();++ const parsed = parseFeed(feedXml(CATALOG), 'google_xml');+ if (parsed.errors.length) throw new Error(`demo feed: ${parsed.errors[0].message}`);+ const live = recordRows(brandId, source, parsed.items, { at: syncedAt, firstSeen: createdAt });+ // A seasonal blend gone from the feed: kept, marked removed (sync.js).+ const removed = recordRows(+ brandId,+ source,+ parseFeed(feedXml([REMOVED_PRODUCT]), 'google_xml').items,+ {+ at: syncedAt,+ firstSeen: createdAt,+ removedAt: new Date(now.getTime() - REMOVED_PRODUCT.removedDaysAgo * DAY_MS).toISOString(),+ },+ );+ const products = [...live.products, ...removed.products];+ const variants = [...live.variants, ...removed.variants];++ const names = [...new Set(products.map((p) => p.product_type).filter(Boolean))];+ const categories = names.map((name) => ({+ id: demoId(brandId, `catalog-category:${name}`),+ brand_id: brandId,+ name,+ created_at: createdAt,+ }));+ const categoryId = new Map(categories.map((c) => [c.name, c.id]));+ const productCategories = products+ .filter((p) => p.product_type)+ .map((p) => ({+ brand_id: brandId,+ product_id: p.id,+ category_id: categoryId.get(p.product_type),+ }));++ const sourceRow = {+ id: sourceId,+ brand_id: brandId,+ kind: 'feed_url',+ format: 'google_xml',+ url: FEED_URL,+ label: 'Brewline product feed',+ last_synced_at: syncedAt,+ last_status: 'ok',+ last_error: null,+ // What this morning's read recorded (sources.js): nothing new in the feed.+ last_counts: {+ format: parsed.format,+ items: parsed.items.length,+ skipped: 0,+ errors: [],+ products: { added: 0, changed: 0, removed: 0, unchanged: live.products.length },+ variants: { added: 0, changed: 0, removed: 0, unchanged: live.variants.length },+ },+ created_by: null,+ created_at: createdAt,+ account: null,+ };++ const categoryPages = CATEGORY_DECISIONS.map(({ category, path }) => ({+ brand_id: brandId,+ category,+ url: pageUrl(path),+ set_by: null,+ set_at: new Date(now.getTime() - 18 * DAY_MS).toISOString(),+ }));++ return { source: sourceRow, products, variants, categories, productCategories, categoryPages };+}++/** The catalog module: its brand-scoped tables, in the order written, and its build. */+/** Its tables' key columns where a row has no `id` of its own. */+const KEYS = {+ catalog_product_categories: 'product_id,category_id',+ catalog_category_pages: 'brand_id,category',+};++export const catalog = {+ name: 'catalog',+ // Rebuilt in place (write.js): written over, what isn't written again deleted after.+ inPlace: true,+ keys: KEYS,+ tables: [+ 'catalog_sources',+ // And, through them (on delete cascade), the products' images: the demo has none.+ 'catalog_products',+ 'catalog_variants',+ 'catalog_categories',+ 'catalog_product_categories',+ 'catalog_category_pages',+ ],++ /**+ * Its independent tables side by side, through the build's writer (a+ * writer over `db` for a first build without one).+ *+ * @param {{ brandId: string, now: Date, db?: object, rebuild?: boolean, write?: Function }} ctx+ */+ async build({+ brandId,+ now,+ db = supabaseAdmin,+ rebuild = false,+ write = demoWriter({ db, brandId, rebuild, keys: KEYS }).write,+ }) {+ const rows = catalogRows({ brandId, now });+ await allSettledOrThrow([+ write('catalog_sources', [rows.source]),+ write('catalog_products', rows.products),+ write('catalog_categories', rows.categories),+ write('catalog_category_pages', rows.categoryPages),+ ]);+ // Variants point at their product; the product categories at both.+ await allSettledOrThrow([+ write('catalog_variants', rows.variants),+ write('catalog_product_categories', rows.productCategories),+ ]);+ return { products: rows.products.length, variants: rows.variants.length };+ },+};diff --git a/server/src/lib/demo/catalog.test.js b/server/src/lib/demo/catalog.test.jsnew file mode 100644index 000000000..35e3dd1fc--- /dev/null+++ b/server/src/lib/demo/catalog.test.js@@ -0,0 +1,130 @@+import { describe, expect, it, vi } from 'vitest';++vi.mock('../../config/supabase.js', () => ({ default: {} }));++const { FEED_URL, catalog, catalogRows, feedXml } = await import('./catalog.js');+const { CATALOG, CATEGORY_DECISIONS } = await import('./site-world.js');+const { PRODUCTS } = await import('./brewline.js');+const { parseFeed } = await import('../feeds/index.js');+const { syncCatalog } = await import('../catalog/sync.js');+const { sourceKey } = await import('../catalog/sources.js');+const { matchCategoryPages } = await import('../catalog/category-pages.js');+const { fakeDb } = await import('../catalog/__fixtures__/fake-db.js');++/**+ * The demo brand's catalog (demo-site): Brewline's product feed, read by+ * the feed parser and kept as the catalog sync keeps it, with the products+ * the demo's AI answers show as cards among them.+ */++const BRAND_ID = '11111111-2222-4333-8444-555555555555';+const NOW = new Date('2026-10-28T12:00:00Z');+const rows = catalogRows({ brandId: BRAND_ID, now: NOW });+const live = rows.products.filter((p) => !p.removed_at);++describe('catalogRows', () => {+ it('is a lean shop: about two dozen products, their variants and categories', () => {+ expect(live).toHaveLength(CATALOG.length);+ expect(rows.variants.length).toBeGreaterThanOrEqual(35);+ expect(rows.categories.map((c) => c.name).sort()).toEqual([+ 'Brewing Equipment',+ 'Coffee > Whole Bean > Blends',+ 'Coffee > Whole Bean > Decaf',+ 'Coffee > Whole Bean > Espresso',+ 'Coffee > Whole Bean > Single Origin',+ 'Gifts',+ 'Subscriptions',+ ]);+ expect(rows.productCategories).toHaveLength(rows.products.length);+ for (const row of [...rows.products, ...rows.variants]) {+ expect(row.url).toMatch(/^https:\/\/brewline\.example\//);+ }+ });++ it('writes what the catalog sync writes for the same feed', async () => {+ const fake = fakeDb();+ const source = sourceKey(rows.source.id);+ await syncCatalog(BRAND_ID, source, parseFeed(feedXml(CATALOG), 'google_xml').items, {+ db: fake.db,+ now: NOW,+ });+ const columns = (row) => {+ const { id: _i, product_id: _p, first_seen_at: _f, updated_at: _u, ...rest } = row;+ return rest;+ };+ const byId = (list) => [...list].sort((a, b) => a.external_id.localeCompare(b.external_id));+ expect(byId(live).map(columns)).toEqual(byId(fake.rows('catalog_products')).map(columns));+ const own = rows.variants.filter((v) => !v.removed_at);+ expect(byId(own).map(columns)).toEqual(byId(fake.rows('catalog_variants')).map(columns));+ // No images: nothing for the demo to load from outside.+ expect(fake.rows('catalog_images')).toEqual([]);+ });++ it("has the AI answers' product cards: same page, title and price", () => {+ for (const card of PRODUCTS.filter((p) => p.owner === 'brand')) {+ const url = `https://brewline.example${card.path}`;+ const variant = rows.variants.find((v) => v.url === url);+ expect(variant, card.title).toBeTruthy();+ expect(variant.price).toBe(card.price);+ expect(variant.title.startsWith(card.title.replace(/, Whole Bean.*$/, ''))).toBe(true);+ }+ });++ it('keeps a removed seasonal product, an out-of-stock size and a sale', () => {+ const removed = rows.products.filter((p) => p.removed_at);+ expect(removed.map((p) => p.title)).toEqual(['Brewline Holiday Blend, Whole Bean 12 oz']);+ expect(rows.variants.some((v) => v.availability === 'out_of_stock')).toBe(true);+ expect(rows.variants.some((v) => v.sale_price !== null)).toBe(true);+ });++ it('reads from a feed URL source whose last read was this morning', () => {+ expect(rows.source).toMatchObject({+ kind: 'feed_url',+ format: 'google_xml',+ url: FEED_URL,+ last_status: 'ok',+ last_synced_at: '2026-10-28T04:10:00.000Z',+ });+ expect(rows.source.last_counts.products.unchanged).toBe(live.length);+ expect(new Set(rows.products.map((p) => p.source))).toEqual(+ new Set([sourceKey(rows.source.id)]),+ );+ });++ it('matches most categories to their collection page, and the rest by hand', () => {+ const pages = ['single-origin', 'blends', 'espresso', 'decaf'].map((slug) => ({+ url: `https://brewline.example/collections/${slug}`,+ title: null,+ }));+ const matched = matchCategoryPages(+ rows.categories.map((c) => c.name),+ pages,+ ).filter((m) => m.page);+ expect(matched).toHaveLength(4);+ expect(rows.categoryPages.map((d) => d.category)).toEqual(+ CATEGORY_DECISIONS.map((d) => d.category),+ );+ for (const decision of rows.categoryPages) expect(decision.set_by).toBeNull();+ });+});++describe('catalog.build', () => {+ it('writes only its own tables, the source first, the variants after their products', async () => {+ const writes = [];+ const db = {+ from: (table) => ({+ upsert: async () => {+ writes.push(table);+ return { error: null };+ },+ }),+ };+ await catalog.build({ brandId: BRAND_ID, now: NOW, db });+ expect([...writes].sort()).toEqual([...catalog.tables].sort());+ expect(writes[0]).toBe('catalog_sources');+ expect(writes.indexOf('catalog_products')).toBeLessThan(writes.indexOf('catalog_variants'));+ expect(writes.indexOf('catalog_categories')).toBeLessThan(+ writes.indexOf('catalog_product_categories'),+ );+ });+});diff --git a/server/src/lib/demo/coverage.js b/server/src/lib/demo/coverage.jsindex 455f406f1..5bc69d8c7 100644--- a/server/src/lib/demo/coverage.js+++ b/server/src/lib/demo/coverage.js@@ -22,6 +22,18 @@ export const SKIPPED = {"A person's saved Tracked Prompts columns (00363): the demo brand starts on the default ones.",sentiment_overrides:"A person's sentiment corrections on Mentions: the demo's answers carry their own.",++ // demo-site+ brand_cms_connections:+ "A CMS connection holds a Composio account's ids, which reach a real CMS account; the demo's Webflow shows as connected from the web's own list (web lib/demo/integrations.ts), and nothing is ever sent.",+ content_draft_deliveries:+ "Drafts sent to a webhook or a CMS: a demo brand never sends anything out (its drafts are the Content module's).",+ catalog_images:+ 'Product images: the demo shows none, so no page of it ever loads an image from outside.',+ catalog_item_issues:+ "Merchant Center's item issues: the demo's catalog comes from a product feed, and Merchant Center shows as Soon in the demo.",++ // demo-workflowsaction_candidates:"The nightly engine's queue of findings not yet raised as actions: no page reads it, and the engine never runs for a demo brand.",action_task_runs:@@ -36,42 +48,13 @@ export const SKIPPED = {"A person's request to their Admins for a Chat change (00403): theirs, not sample data, and a demo brand's refusal offers no Ask your Admin.",};-const SITE = 'demo-site (PR 3): Site Audit, AI traffic, integrations, Product Visibility';+const FOLLOW_UP =+ 'demo follow-up (PR 7): a draft with product links, checked against the demo catalog';const CHAT = 'demo Chat, once Chat ships (the demo shows Chat as Soon until then)';/** Tables a later demo PR builds; listed so the gap stays visible until then. */export const PENDING = {- site_audits: SITE,- site_pages: SITE,- site_trust: SITE,- site_audit_brand_settings: SITE,- site_audit_page_prefs: SITE,- brand_facts: SITE,- brand_fact_pages: SITE,- ai_traffic_logs: SITE,- ai_traffic_events: SITE,- ai_bot_connections: SITE,- ai_bot_daily: SITE,- ai_bot_events: SITE,- ga_ai_traffic_stats: SITE,- ga_channel_stats: SITE,- ga_item_stats: SITE,- ga_page_stats: SITE,- gsc_page_stats: SITE,- gsc_query_stats: SITE,- page_opportunities: SITE,- brand_cms_connections: SITE,- content_draft_deliveries: SITE,- // A draft's shopping path check reads the products it links to: the catalog demo-site builds.- content_shopping_checks: SITE,- catalog_sources: SITE,- catalog_products: SITE,- catalog_variants: SITE,- catalog_images: SITE,- catalog_categories: SITE,- catalog_product_categories: SITE,- catalog_category_pages: SITE,- catalog_item_issues: SITE,+ content_shopping_checks: FOLLOW_UP,cited_page_features:"demo follow-up (PR 7): what the pages a demo answer cites have (learning C1, 00369); never read for a demo brand, whose tasks don't run",diff --git a/server/src/lib/demo/fanout-intents.js b/server/src/lib/demo/fanout-intents.jsnew file mode 100644index 000000000..c59214fc9--- /dev/null+++ b/server/src/lib/demo/fanout-intents.js@@ -0,0 +1,73 @@+/**+ * The search intent of the demo's fan-out searches (demo-site), written+ * ahead into the shared cache Query Fan-out reads (fanout_query_intents,+ * #333), so its intent badges never wait on a model for the demo.+ *+ * The cache is keyed by the search itself, as routes/prompts.js keeps it+ * (normalizeIntentQuery), not by brand: a search's intent is the same for+ * every brand. A row already there is left as it is (ignoreDuplicates), so+ * the demo never overwrites a classification a live brand's read made, and+ * it is never cleared with the demo brand.+ */++import supabaseAdmin from '../../config/supabase.js';+import { normalizeIntentQuery } from '../intent-extraction.js';+import { PROMPTS } from './brewline.js';++/** Each search the demo's answers ran (brewline.js PROMPTS[].searches), by intent. */+export const FANOUT_INTENTS = {+ 'best coffee subscription 2026': 'best-top',+ 'coffee subscription reviews': 'vs-review',+ 'fresh roasted coffee subscription': 'recommendation',+ 'coffee subscription pause anytime': 'recommendation',+ 'flexible coffee delivery plan': 'recommendation',+ 'coffee subscription gift ideas': 'recommendation',+ 'prepaid coffee subscription gift': 'recommendation',+ 'is a coffee subscription worth it': 'vs-review',+ 'coffee subscription cost per cup': 'what-is',+ 'best espresso beans for home': 'best-top',+ 'whole bean espresso roast': 'recommendation',+ 'buy fresh roasted coffee beans online': 'recommendation',+ 'coffee roaster ships same day': 'recommendation',+ 'low acid coffee beans': 'recommendation',+ 'coffee for sensitive stomach': 'problem-solving',+ 'single origin vs blend coffee': 'comparison',+ 'best single origin coffee beans': 'best-top',+ 'organic fair trade coffee roasters': 'recommendation',+ 'direct trade coffee beans': 'what-is',+ 'best tasting decaf coffee beans': 'best-top',+ 'water process decaf coffee': 'what-is',+ 'how long do coffee beans stay fresh': 'what-is',+ 'coffee roast date explained': 'what-is',+ 'brewline coffee vs northbound roasters': 'vs-review',+ 'northbound roasters subscription review': 'vs-review',+};++/** The cache rows: one per distinct search the demo's prompts list. */+export function fanoutIntentRows() {+ const searches = [...new Set(PROMPTS.flatMap((p) => p.searches ?? []).map(normalizeIntentQuery))];+ return searches.map((query) => {+ const intent = FANOUT_INTENTS[query];+ if (!intent) throw new Error(`demo fan-out search without an intent: ${query}`);+ return { query, intent };+ });+}++/**+ * The fan-out intent module: no brand-scoped table of its own (the cache is+ * shared, and kept across rebuilds), only rows added where missing.+ */+export const fanoutIntents = {+ name: 'fanout-intents',+ tables: [],++ /** @param {{ db?: object }} ctx */+ async build({ db = supabaseAdmin } = {}) {+ const rows = fanoutIntentRows();+ const { error } = await db+ .from('fanout_query_intents')+ .upsert(rows, { onConflict: 'query', ignoreDuplicates: true });+ if (error) throw new Error(`demo fanout_query_intents write: ${error.message}`);+ return { intents: rows.length };+ },+};diff --git a/server/src/lib/demo/fanout-intents.test.js b/server/src/lib/demo/fanout-intents.test.jsnew file mode 100644index 000000000..16d6e99ac--- /dev/null+++ b/server/src/lib/demo/fanout-intents.test.js@@ -0,0 +1,49 @@+import { describe, expect, it, vi } from 'vitest';++vi.mock('../../config/supabase.js', () => ({ default: {} }));++const { FANOUT_INTENTS, fanoutIntentRows, fanoutIntents } = await import('./fanout-intents.js');+const { INTENT_CRITERIA, normalizeIntentQuery } = await import('../intent-extraction.js');+const { PROMPTS } = await import('./brewline.js');++/**+ * The demo's fan-out searches, classified ahead into the shared intent+ * cache, so Query Fan-out shows the demo's intents without a model call.+ */++describe('fanoutIntentRows', () => {+ it('classifies every search the demo answers ran, with a label of the taxonomy', () => {+ const rows = fanoutIntentRows();+ const searches = new Set(PROMPTS.flatMap((p) => p.searches).map(normalizeIntentQuery));+ expect(rows.map((r) => r.query).sort()).toEqual([...searches].sort());+ for (const { query, intent } of rows) {+ expect(query).toBe(normalizeIntentQuery(query));+ expect(Object.keys(INTENT_CRITERIA)).toContain(intent);+ }+ // No intent kept for a search the demo no longer runs.+ expect(Object.keys(FANOUT_INTENTS).sort()).toEqual([...searches].sort());+ });+});++describe('fanoutIntents.build', () => {+ it('adds the rows the cache lacks, never overwriting one', async () => {+ const calls = [];+ const db = {+ from: (table) => ({+ upsert: async (rows, options) => {+ calls.push({ table, rows, options });+ return { error: null };+ },+ }),+ };+ expect(fanoutIntents.tables).toEqual([]);+ await fanoutIntents.build({ brandId: 'b', now: new Date(), db });+ expect(calls).toEqual([+ {+ table: 'fanout_query_intents',+ rows: fanoutIntentRows(),+ options: { onConflict: 'query', ignoreDuplicates: true },+ },+ ]);+ });+});diff --git a/server/src/lib/demo/index.js b/server/src/lib/demo/index.jsindex daf40fce9..dce1df7dc 100644--- a/server/src/lib/demo/index.js+++ b/server/src/lib/demo/index.js@@ -21,6 +21,11 @@ import { actionCenter } from './action-center.js';import { BRAND } from './brewline.js';import { content } from './content.js';import { core } from './core.js';+// demo-site: AI Referral Traffic, Site Audit, the catalog and fan-out intents.+import { catalog } from './catalog.js';+import { fanoutIntents } from './fanout-intents.js';+import { site } from './site.js';+import { traffic } from './traffic.js';import { reports } from './reports.js';import { suggestions } from './suggestions.js';import { demoWriter } from './write.js';@@ -28,8 +33,9 @@ import { demoWriter } from './write.js';/*** Bump to rebuild every demo brand on its next visit.* 2: Action Center, Content, suggestions and reports (demo-workflows).+ * 3: AI Referral Traffic, Site Audit, the catalog and fan-out intents (demo-site).*/-export const DEMO_VERSION = 2;+export const DEMO_VERSION = 3;/** Data older than this is rebuilt, so the demo's latest run stays recent. */export const DEMO_MAX_AGE_MS = 3 * 24 * 60 * 60 * 1000;@@ -62,10 +68,22 @@ const WAIT_POLL_MS = 1000;* are deleted once it is built (on a first build, none are). Any other* module has its `tables` cleared, in reverse, just before it is built.* (Content after the Action Center: a draft points at the action its- * opportunity was sent as.) Site Audit, traffic and Product Visibility- * follow as modules of their own.+ * opportunity was sent as.)*/-export const DEMO_MODULES = [core, suggestions, actionCenter, content, reports];+export const DEMO_MODULES = [+ core,+ // demo-site: AI Referral Traffic, Site Audit, Product Visibility's catalog,+ // and the intents of the core's fan-out searches (a shared cache).+ traffic,+ site,+ catalog,+ fanoutIntents,+ // demo-workflows: suggestions, the Action Center, Content and reports.+ suggestions,+ actionCenter,+ content,+ reports,+];const COLUMNS = 'id, demo_version, demo_built_at, demo_hidden_at';diff --git a/server/src/lib/demo/site-common.js b/server/src/lib/demo/site-common.jsnew file mode 100644index 000000000..50f3ec55d--- /dev/null+++ b/server/src/lib/demo/site-common.js@@ -0,0 +1,51 @@+/**+ * What the demo-site modules (traffic, site, catalog) share: the days they+ * count back over, and the citations of Brewline's own pages+ * in the core module's answers, so a page's citations, its AI visits and its+ * audit read the same month.+ */++import { BRAND } from './brewline.js';+import { coreRows } from './core.js';++export const DAY_MS = 24 * 60 * 60 * 1000;++/** The UTC day (YYYY-MM-DD) `days` before `now`. */+export function dayBefore(now, days) {+ return new Date(now.getTime() - days * DAY_MS).toISOString().slice(0, 10);+}++/** Midnight UTC of a day, in ms. */+export const dayStart = (day) => Date.parse(`${day}T00:00:00.000Z`);++/** The last built core, kept so the demo-site modules of one build read it once. */+let lastCore = null;++/**+ * The citations of Brewline's own pages in the core module's answers:+ * address, when, and the prompt whose answer cited it. Deterministic, as+ * the core is.+ *+ * @param {string} brandId+ * @param {Date} now+ * @returns {{ url: string, created_at: string, prompt_id: string }[]}+ */+export function brandCitations(brandId, now) {+ const key = `${brandId}|${now.toISOString()}`;+ if (lastCore?.key !== key) {+ const rows = coreRows({ brandId, now });+ const promptOf = new Map(rows.results.map((r) => [r.id, r.prompt_id]));+ const host = `https://${BRAND.domain}`;+ lastCore = {+ key,+ citations: rows.citations+ .filter((c) => c.url === host || c.url.startsWith(`${host}/`))+ .map((c) => ({+ url: c.url,+ created_at: c.created_at,+ prompt_id: promptOf.get(c.prompt_result_id),+ })),+ };+ }+ return lastCore.citations;+}diff --git a/server/src/lib/demo/site-html.js b/server/src/lib/demo/site-html.jsnew file mode 100644index 000000000..69f91aeee--- /dev/null+++ b/server/src/lib/demo/site-html.js@@ -0,0 +1,358 @@+/**+ * Brewline's pages as HTML (demo-site), for Site Audit to read: the demo's+ * audits are the real audit engine's verdicts on these pages, so a score,+ * a signal and its evidence are what the live product would show for them.+ *+ * A page is rendered as it stood in one audit round: a fix named in its+ * `since` (site-world.js) is on the page from that round on, so later+ * audits score higher. Links go only to Brewline's pages and the demo's+ * fictional publishers; no image is ever loaded (an <img> has an address on+ * brewline.example that nothing fetches).+ */++import { BRAND } from './brewline.js';+import { AUTHORS, CATALOG, FOOTER_LINKS, SITE_URL, pageUrl } from './site-world.js';++const DAY_MS = 24 * 60 * 60 * 1000;++const esc = (text) =>+ String(text)+ .replace(/&/g, '&')+ .replace(/</g, '<')+ .replace(/>/g, '>')+ .replace(/"/g, '"');++const ld = (data) =>+ `<script type="application/ld+json">${JSON.stringify({ '@context': 'https://schema.org', ...data })}</script>`;++/** Whether a fix is on the page in this round. */+const has = (page, fix, round) => round >= (page.since?.[fix] ?? 0);++const productOf = (id) => CATALOG.find((p) => p.id === id);++/** A product's variants as [label, price, availability, sale price]. */+export function variantsOf(product) {+ if (product.sizes) return product.sizes;+ if (product.bags) return product.bags;+ return [[null, product.price]];+}++const money = (amount) => `$${amount.toFixed(2)}`;+const fromPrice = (product) => Math.min(...variantsOf(product).map(([, price]) => price));++/** The product page's description: its opening, then the rest. */+export function productDescription(product, { opening = true } = {}) {+ const coffee = product.type.startsWith('Coffee');+ const sizes = variantsOf(product)+ .map(([label]) => label)+ .filter(Boolean);+ const lead = coffee+ ? `Whole bean ${product.roast} roast coffee with notes of ${product.notes}, in ${sizes.join(' and ')} bags, roasted to order.`+ : product.type === 'Subscriptions'+ ? `A coffee subscription of ${product.notes}, delivered weekly, every two weeks or monthly in ${sizes.join(' or ')} a delivery.`+ : `${product.title}: ${product.notes}.`;+ const fluff =+ 'Meet your new favorite cup. Some coffees just make the morning better, and this is one of them.';+ const rest = coffee+ ? `We roast ${product.title} in small batches and ship it within 24 hours, so it arrives at its best.${product.process ? ` Process: ${product.process}.` : ''}`+ : 'Every order ships within 24 hours of roasting, and subscriptions can be paused or skipped anytime.';+ return opening ? `${lead} ${rest}` : `${fluff} ${lead} ${rest}`;+}++function head(page, { url, round, jsonLd }) {+ const meta = [];+ const description = has(page, 'metaDescription', round) ? page.description : null;+ if (description) meta.push(`<meta name="description" content="${esc(description)}">`);+ meta.push(+ `<meta property="og:title" content="${esc(page.title)}">`,+ `<meta property="og:type" content="${page.kind === 'article' ? 'article' : 'website'}">`,+ `<meta property="og:url" content="${url}">`,+ );+ if (description) meta.push(`<meta property="og:description" content="${esc(description)}">`);+ if (has(page, 'ogImage', round)) {+ meta.push(`<meta property="og:image" content="${SITE_URL}/share/brewline-card.png">`);+ }+ if (has(page, 'twitterCard', round)) {+ meta.push(+ '<meta name="twitter:card" content="summary_large_image">',+ `<meta name="twitter:image" content="${SITE_URL}/share/brewline-card.png">`,+ );+ }+ return `<head>+<meta charset="utf-8">+<meta name="viewport" content="width=device-width, initial-scale=1">+<title>${esc(page.title)}</title>+${meta.join('\n')}+<link rel="canonical" href="${url}">+${jsonLd.map(ld).join('\n')}+</head>`;+}++const NAV = [+ ['/subscriptions', 'Subscriptions'],+ ['/collections/single-origin', 'Single origin'],+ ['/collections/blends', 'Blends'],+ ['/collections/espresso', 'Espresso'],+ ['/collections/decaf', 'Decaf'],+ ['/gifts', 'Gifts'],+ ['/blog', 'Guides'],+];++const header = () =>+ `<header><a href="${SITE_URL}/">${BRAND.name}</a><nav>${NAV.map(+ ([path, label]) => `<a href="${pageUrl(path)}">${label}</a>`,+ ).join(' ')}</nav></header>`;++const footer = () =>+ `<footer><nav>${FOOTER_LINKS.map(+ ([path, label]) => `<a href="${pageUrl(path)}">${label}</a>`,+ ).join(' ')}</nav><p>© ${BRAND.name}</p></footer>`;++function sectionHtml(section) {+ const parts = [`<h2>${esc(section.h2)}</h2>`];+ for (const p of section.p ?? []) parts.push(`<p>${esc(p)}</p>`);+ if (section.list) {+ parts.push(`<ul>${section.list.map((item) => `<li>${esc(item)}</li>`).join('')}</ul>`);+ }+ if (section.table) {+ parts.push(+ `<table><thead><tr>${section.table.head.map((h) => `<th>${esc(h)}</th>`).join('')}</tr></thead><tbody>${section.table.rows+ .map((row) => `<tr>${row.map((cell) => `<td>${esc(cell)}</td>`).join('')}</tr>`)+ .join('')}</tbody></table>`,+ );+ }+ return `<section>${parts.join('\n')}</section>`;+}++const faqHtml = (faq) =>+ `<section><h2>Frequently asked questions</h2>${faq+ .map(([q, a]) => `<h3>${esc(q)}</h3><p>${esc(a)}</p>`)+ .join('\n')}</section>`;++const faqLd = (faq) => ({+ '@type': 'FAQPage',+ mainEntity: faq.map(([q, a]) => ({+ '@type': 'Question',+ name: q,+ acceptedAnswer: { '@type': 'Answer', text: a },+ })),+});++const productCard = (product) =>+ `<li><a href="${pageUrl(product.path)}">${esc(product.title)}</a>: ${esc(product.notes)}, from ${money(fromPrice(product))}</li>`;++function productJsonLd(product, url, description) {+ return {+ '@type': 'Product',+ name: product.title,+ description,+ brand: { '@type': 'Brand', name: BRAND.name },+ sku: product.id,+ url,+ offers: variantsOf(product).map(([label, price, availability]) => ({+ '@type': 'Offer',+ name: label ?? product.title,+ price: price.toFixed(2),+ priceCurrency: 'USD',+ availability:+ availability === 'out of stock'+ ? 'https://schema.org/OutOfStock'+ : 'https://schema.org/InStock',+ })),+ };+}++function productBody(page, product, round) {+ const opening = has(page, 'descriptionOpening', round);+ const variants = variantsOf(product);+ const parts = [+ `<h1>${esc(page.h1)}</h1>`,+ `<p itemprop="description">${esc(productDescription(product, { opening }))}</p>`,+ `<ul>${variants+ .map(+ ([label, price, availability, sale]) =>+ `<li>${esc(label ?? product.title)}: ${sale ? `${money(sale)} (regular ${money(price)})` : money(price)}${availability ? `, ${availability}` : ''}</li>`,+ )+ .join('')}</ul>`,+ ];+ if (product.type.startsWith('Coffee')) {+ parts.push(+ sectionHtml({+ h2: 'Details',+ list: [+ `Roast: ${product.roast}`,+ `Tasting notes: ${product.notes}`,+ ...(product.process ? [`Process: ${product.process}`] : []),+ 'Ships within 24 hours of roasting',+ ],+ }),+ sectionHtml({+ h2: 'How to brew it',+ p: [+ 'For pour-over, use 15 grams of coffee to 250 grams of water at 94 degrees Celsius. For espresso, start at 18 grams in and 36 grams out.',+ ],+ }),+ );+ } else {+ parts.push(+ sectionHtml({+ h2: 'How it works',+ list: [+ 'Choose weekly, every two weeks or monthly deliveries',+ 'Pick whole beans or a grind for your brewer',+ 'Pause, skip or cancel anytime',+ ],+ }),+ );+ }+ parts.push(+ `<p><a href="${pageUrl('/subscriptions')}">See all subscriptions</a> or <a href="${pageUrl('/collections/single-origin')}">browse single origins</a>.</p>`,+ );+ if (page.faq) parts.push(faqHtml(page.faq));+ return parts.join('\n');+}++function articleBody(page, round, now) {+ const author = AUTHORS.find((a) => a.key === page.author);+ const published = new Date(now - page.published * DAY_MS).toISOString().slice(0, 10);+ const modified = new Date(now - page.modified * DAY_MS).toISOString().slice(0, 10);+ const parts = [`<h1>${esc(page.h1)}</h1>`];+ if (!page.noByline) {+ parts.push(+ `<p class="byline">By <a rel="author" href="${pageUrl(author.path)}">${esc(author.name)}</a>, ${esc(author.role)}. Updated <time datetime="${modified}">${modified}</time></p>`,+ );+ }+ parts.push(`<p>${esc(page.intro)}</p>`);+ for (const section of page.sections) parts.push(sectionHtml(section));+ if (page.quote && has(page, 'quote', round)) {+ parts.push(+ `<blockquote><p>${esc(page.quote[0])}</p><cite>${esc(page.quote[1])}</cite></blockquote>`,+ );+ }+ if (page.links) {+ parts.push(+ `<p>Further reading: ${page.links.map(([href, text]) => `<a href="${href}">${esc(text)}</a>`).join(', ')}.</p>`,+ );+ }+ if (page.faq) parts.push(faqHtml(page.faq));+ if (!page.noByline) {+ parts.push(`<aside><h2>About the author</h2><p>${esc(author.bio)}</p></aside>`);+ }+ return {+ body: parts.join('\n'),+ jsonLd: {+ '@type': 'BlogPosting',+ headline: page.h1,+ ...(page.noByline ? {} : { author: { '@type': 'Person', name: author.name } }),+ publisher: { '@type': 'Organization', name: BRAND.name },+ datePublished: published,+ dateModified: modified,+ },+ };+}++const ORGANIZATION = {+ '@type': 'Organization',+ name: BRAND.name,+ url: SITE_URL,+ email: '[email protected]',+ foundingDate: '2016',+};++/**+ * One audited page's HTML as it stood in audit round `round`.+ *+ * @param {object} page an AUDITED_PAGES entry+ * @param {{ round: number, now: number }} at the round, and when the audit ran (ms)+ */+export function pageHtml(page, { round, now }) {+ const url = pageUrl(page.path);+ const jsonLd = [];+ let body;+ const { h1 } = page;++ if (page.kind === 'product') {+ const product = productOf(page.product);+ body = productBody(page, product, round);+ if (has(page, 'productJsonLd', round)) {+ const opening = has(page, 'descriptionOpening', round);+ jsonLd.push(productJsonLd(product, url, productDescription(product, { opening })));+ }+ } else if (page.kind === 'article') {+ const article = articleBody(page, round, now);+ body = article.body;+ jsonLd.push(article.jsonLd);+ } else {+ const parts = [`<h1>${esc(h1)}</h1>`, `<p>${esc(page.intro)}</p>`];+ for (const section of page.sections ?? []) parts.push(sectionHtml(section));+ if (page.products) {+ parts.push(`<ul>${page.products.map(productOf).map(productCard).join('')}</ul>`);+ jsonLd.push({+ '@type': 'CollectionPage',+ name: h1,+ mainEntity: {+ '@type': 'ItemList',+ itemListElement: page.products.map((id, i) => ({+ '@type': 'ListItem',+ position: i + 1,+ url: pageUrl(productOf(id).path),+ })),+ },+ });+ }+ if (page.kind === 'contact') {+ parts.push(+ '<p>Email <a href="mailto:[email protected]">[email protected]</a>. Wholesale: <a href="mailto:[email protected]">[email protected]</a>.</p>',+ );+ jsonLd.push({ '@type': 'ContactPage', name: h1 });+ }+ if (page.kind === 'home' || page.kind === 'about') jsonLd.push(ORGANIZATION);+ if (page.kind === 'home') {+ jsonLd.push({ '@type': 'WebSite', name: BRAND.name, url: SITE_URL });+ }+ if (page.faq && page.kind !== 'faq') parts.push(faqHtml(page.faq));+ if (page.kind === 'faq') {+ parts.push(page.faq.map(([q, a]) => `<h2>${esc(q)}</h2><p>${esc(a)}</p>`).join('\n'));+ }+ body = parts.join('\n');+ }+ // FAQPage markup: on the FAQ page always, elsewhere once added.+ if (page.faq && (page.kind === 'faq' || has(page, 'faqSchema', round))) {+ jsonLd.push(faqLd(page.faq));+ }++ return `<!doctype html>+<html lang="en">+${head(page, { url, round, jsonLd })}+<body>+${header()}+<main>+${body}+</main>+${footer()}+</body>+</html>`;+}++/** Brewline's robots.txt: AI crawlers named and allowed. */+export const ROBOTS_TXT = `User-agent: *+Disallow: /cart+Disallow: /account++User-agent: GPTBot+Allow: /++User-agent: OAI-SearchBot+Allow: /++User-agent: ClaudeBot+Allow: /++User-agent: PerplexityBot+Allow: /++Sitemap: ${SITE_URL}/sitemap.xml+`;++/** Brewline has no llms.txt yet: one of the gaps its audits show. */+export const LLMS_TXT = null;diff --git a/server/src/lib/demo/site-world.js b/server/src/lib/demo/site-world.jsnew file mode 100644index 000000000..f8f6efe85--- /dev/null+++ b/server/src/lib/demo/site-world.js@@ -0,0 +1,954 @@+/**+ * Brewline Coffee's own website, for the demo brand's site pages (demo-site):+ * its pages and what they say, its catalog, its authors and the searches it+ * shows up for. Site Audit audits these pages, AI Referral Traffic counts+ * visits to them and Product Visibility reads the catalog.+ *+ * Everything here is made up, on `brewline.example` like the rest of the+ * demo (brewline.js): no real company, site or product. The pages link only+ * to Brewline's own pages and the demo's fictional publishers, and name no+ * image the demo would load.+ */++import { BRAND, SOURCES } from './brewline.js';++export const SITE_URL = `https://${BRAND.domain}`;++/** The page's full address: the home page without a trailing slash, as site_pages keeps it. */+export const pageUrl = (path) => (path === '/' ? SITE_URL : `${SITE_URL}${path}`);++/** The authors whose names the blog posts carry. */+export const AUTHORS = [+ {+ key: 'maya',+ name: 'Maya Okafor',+ path: '/authors/maya-okafor',+ role: 'Head Roaster',+ bio: 'Maya has roasted specialty coffee for 12 years and writes every Brewline roast profile. She cups each batch before it ships.',+ },+ {+ key: 'daniel',+ name: 'Daniel Reyes',+ path: '/authors/daniel-reyes',+ role: 'Coffee Editor',+ bio: 'Daniel writes the Brewline brewing guides and tests every recipe at home with a hand grinder and a basic kettle.',+ },+];++/**+ * The catalog: one entry per product, its variants by bag size (or bag+ * count, for a subscription). `card` marks the products the demo's AI+ * answers show as product cards (brewline.js PRODUCTS): same page, same+ * price, so the two sides match.+ */+export const CATALOG = [+ // Single origins+ {+ id: 'BL-ETH',+ path: '/coffee/ethiopia-yirgacheffe',+ title: 'Ethiopia Yirgacheffe',+ type: 'Coffee > Whole Bean > Single Origin',+ notes: 'jasmine, bergamot and lemon',+ roast: 'light',+ process: 'washed',+ sizes: [+ ['12 oz', 19.5],+ ['2 lb', 46],+ ],+ },+ {+ id: 'BL-COL',+ path: '/coffee/colombia-huila',+ title: 'Colombia Huila',+ type: 'Coffee > Whole Bean > Single Origin',+ notes: 'red apple, caramel and cocoa',+ roast: 'medium',+ process: 'washed',+ sizes: [+ ['12 oz', 17.5],+ ['2 lb', 41],+ ],+ },+ {+ id: 'BL-GUA',+ path: '/coffee/guatemala-antigua',+ title: 'Guatemala Antigua',+ type: 'Coffee > Whole Bean > Single Origin',+ notes: 'milk chocolate, toffee and orange',+ roast: 'medium',+ process: 'washed',+ sizes: [+ ['12 oz', 18],+ ['2 lb', 42],+ ],+ },+ {+ id: 'BL-KEN',+ path: '/coffee/kenya-nyeri',+ title: 'Kenya Nyeri',+ type: 'Coffee > Whole Bean > Single Origin',+ notes: 'blackcurrant, grapefruit and brown sugar',+ roast: 'light',+ process: 'washed',+ sizes: [+ ['12 oz', 21],+ ['2 lb', 49, 'out of stock'],+ ],+ },+ {+ id: 'BL-SUM',+ path: '/coffee/sumatra-mandheling',+ title: 'Sumatra Mandheling',+ type: 'Coffee > Whole Bean > Single Origin',+ notes: 'cedar, dark chocolate and molasses',+ roast: 'dark',+ process: 'wet-hulled',+ sizes: [+ ['12 oz', 17.5],+ ['2 lb', 41],+ ],+ },+ {+ id: 'BL-BRA',+ path: '/coffee/brazil-cerrado',+ title: 'Brazil Cerrado',+ type: 'Coffee > Whole Bean > Single Origin',+ notes: 'peanut, milk chocolate and low acidity',+ roast: 'medium',+ process: 'natural',+ sizes: [+ ['12 oz', 15.5],+ ['2 lb', 36],+ ],+ },+ {+ id: 'BL-CRC',+ path: '/coffee/costa-rica-tarrazu',+ title: 'Costa Rica Tarrazu',+ type: 'Coffee > Whole Bean > Single Origin',+ notes: 'honey, peach and almond',+ roast: 'light',+ process: 'honey',+ sizes: [+ ['12 oz', 19],+ ['2 lb', 44],+ ],+ },+ // Blends+ {+ id: 'BL-HOU',+ path: '/coffee/house-blend',+ title: 'House Blend',+ type: 'Coffee > Whole Bean > Blends',+ notes: 'chocolate, hazelnut and a round finish',+ roast: 'medium',+ sizes: [+ ['12 oz', 15],+ ['2 lb', 35],+ ],+ },+ {+ id: 'BL-BRK',+ path: '/coffee/breakfast-blend',+ title: 'Breakfast Blend',+ type: 'Coffee > Whole Bean > Blends',+ notes: 'brown sugar, citrus and a bright finish',+ roast: 'light',+ sizes: [+ ['12 oz', 15, null, 13.5],+ ['2 lb', 35],+ ],+ },+ {+ id: 'BL-MID',+ path: '/coffee/midnight-dark-roast',+ title: 'Midnight Dark Roast',+ type: 'Coffee > Whole Bean > Blends',+ notes: 'smoke, cocoa and toasted walnut',+ roast: 'dark',+ sizes: [+ ['12 oz', 16],+ ['2 lb', 37],+ ],+ },+ // Espresso+ {+ id: 'BL-ESP',+ path: '/coffee/espresso-roast',+ title: 'Espresso Roast',+ type: 'Coffee > Whole Bean > Espresso',+ notes: 'dark chocolate, cherry and a thick crema',+ roast: 'medium-dark',+ card: true,+ sizes: [+ ['12 oz', 16.5],+ ['2 lb', 38],+ ],+ },+ {+ id: 'BL-CRM',+ path: '/coffee/crema-espresso-blend',+ title: 'Crema Espresso Blend',+ type: 'Coffee > Whole Bean > Espresso',+ notes: 'caramel, almond and a syrupy body',+ roast: 'medium',+ sizes: [+ ['12 oz', 17.5],+ ['2 lb', 40],+ ],+ },+ // Decaf+ {+ id: 'BL-DEC',+ path: '/coffee/decaf',+ title: 'Water Process Decaf',+ type: 'Coffee > Whole Bean > Decaf',+ notes: 'milk chocolate, plum and brown sugar',+ roast: 'medium',+ process: 'water process decaf',+ card: true,+ sizes: [+ ['12 oz', 17],+ ['2 lb', 39],+ ],+ },+ {+ id: 'BL-DES',+ path: '/coffee/decaf-espresso',+ title: 'Decaf Espresso',+ type: 'Coffee > Whole Bean > Decaf',+ notes: 'cocoa, dates and a heavy body',+ roast: 'medium-dark',+ process: 'water process decaf',+ sizes: [+ ['12 oz', 17.5],+ ['2 lb', 40],+ ],+ },+ // Subscriptions: a variant per bags a delivery.+ {+ id: 'BL-SUB-HOU',+ path: '/subscriptions/house-blend',+ title: 'House Blend Subscription',+ type: 'Subscriptions',+ notes: 'our House Blend, roasted to order',+ card: true,+ bags: [+ ['1 bag', 18],+ ['2 bags', 34],+ ],+ },+ {+ id: 'BL-SUB-SOC',+ path: '/subscriptions/single-origin-club',+ title: 'Single Origin Club',+ type: 'Subscriptions',+ notes: 'a different single origin every delivery',+ bags: [+ ['1 bag', 22],+ ['2 bags', 42],+ ],+ },+ {+ id: 'BL-SUB-ESP',+ path: '/subscriptions/espresso-club',+ title: 'Espresso Club',+ type: 'Subscriptions',+ notes: 'our espresso roasts in rotation',+ bags: [+ ['1 bag', 20],+ ['2 bags', 38],+ ],+ },+ // Gifts and brewing gear: one variant each.+ {+ id: 'BL-GFT-3M',+ path: '/gifts/3-month-gift-subscription',+ title: '3-Month Gift Subscription',+ type: 'Gifts',+ notes: 'three monthly deliveries of a single origin, prepaid',+ price: 54,+ },+ {+ id: 'BL-GFT-TST',+ path: '/gifts/single-origin-tasting-set',+ title: 'Single Origin Tasting Set',+ type: 'Gifts',+ notes: 'four 4 oz bags of our current single origins',+ price: 29,+ },+ {+ id: 'BL-EQ-GRN',+ path: '/brewing/burr-grinder',+ title: 'Brewline Burr Grinder',+ type: 'Brewing Equipment',+ notes: 'a conical burr grinder with 30 grind settings',+ price: 89,+ },+ {+ id: 'BL-EQ-POK',+ path: '/brewing/pour-over-starter-kit',+ title: 'Pour-Over Starter Kit',+ type: 'Brewing Equipment',+ notes: 'a ceramic dripper, 100 filters and a glass server',+ price: 45,+ },+ {+ id: 'BL-EQ-CBB',+ path: '/brewing/cold-brew-filter-bags',+ title: 'Cold Brew Filter Bags',+ type: 'Brewing Equipment',+ notes: 'a pack of 50 bags for one-quart batches',+ price: 9,+ },+];++/** A product taken off the store: still in the catalog, marked removed. */+export const REMOVED_PRODUCT = {+ id: 'BL-HOL',+ path: '/coffee/holiday-blend',+ title: 'Holiday Blend',+ type: 'Coffee > Whole Bean > Blends',+ notes: 'spice, dried cherry and dark chocolate',+ roast: 'medium-dark',+ sizes: [['12 oz', 17]],+ removedDaysAgo: 41,+};++/** The collection pages (Shopify style), one per catalog category that has one. */+export const COLLECTIONS = [+ {+ path: '/collections/single-origin',+ type: 'Coffee > Whole Bean > Single Origin',+ title: 'Single Origin Coffee | Brewline Coffee',+ h1: 'Single Origin Coffee Beans',+ description:+ 'Single origin whole bean coffee from Ethiopia, Colombia, Kenya and more, roasted to order and shipped within a day of roasting.',+ },+ {+ path: '/collections/blends',+ type: 'Coffee > Whole Bean > Blends',+ title: 'Coffee Blends | Brewline Coffee',+ h1: 'Coffee Blends',+ description:+ 'Balanced everyday coffee blends, from a bright breakfast roast to a smoky dark roast, roasted fresh every weekday.',+ },+ {+ path: '/collections/espresso',+ type: 'Coffee > Whole Bean > Espresso',+ title: 'Espresso Beans | Brewline Coffee',+ h1: 'Espresso Beans for Home Espresso',+ description:+ 'Whole bean espresso roasts developed for home machines: thick crema, chocolate sweetness and a forgiving extraction window.',+ },+ {+ path: '/collections/decaf',+ type: 'Coffee > Whole Bean > Decaf',+ title: 'Decaf Coffee | Brewline Coffee',+ h1: 'Water Process Decaf Coffee',+ description:+ 'Decaf coffee decaffeinated with water only, no solvents, roasted to taste like the real thing.',+ },+];++/** A collection page's title, h1 and description, for its audited page. */+const collection = (path) => {+ const { type: _type, ...page } = COLLECTIONS.find((c) => c.path === path);+ return page;+};++/**+ * The categories a person set a page for by hand (catalog_category_pages):+ * a themed collection the matcher leaves alone, and the subscriptions page.+ */+export const CATEGORY_DECISIONS = [+ { category: 'Gifts', path: '/gifts' },+ { category: 'Subscriptions', path: '/subscriptions' },+];++/** A page cited in the demo's answers keeps the title the answers show for it. */+const citedTitle = (path) => SOURCES.brand.find((p) => p.path === path)?.label;++const FOOTER_LINKS = [+ ['/about', 'About us'],+ ['/contact', 'Contact'],+ ['/faq', 'FAQ'],+ ['/shipping', 'Shipping'],+ ['/privacy-policy', 'Privacy policy'],+ ['/terms', 'Terms of service'],+];+export { FOOTER_LINKS };++/**+ * The pages Site Audit audits, with what they say. `since` names the audit+ * round (0, 1, 2: oldest first) from which a fix is on the page; a page+ * without it had it all along. `rounds` are the rounds the page was audited+ * in (every page is in the latest).+ */+export const AUDITED_PAGES = [+ {+ path: '/',+ kind: 'home',+ title: 'Brewline Coffee | Fresh Roasted Coffee Beans & Subscriptions',+ description:+ 'Specialty coffee roasted to order and shipped within a day of roasting. Whole beans by the bag or on a flexible subscription you can pause anytime.',+ h1: 'Fresh Roasted Coffee, Shipped the Day After Roasting',+ rounds: [0, 1, 2],+ since: { metaDescription: 1, faqSchema: 2, twitterCard: 1 },+ intro:+ 'Brewline roasts specialty coffee to order in small batches and ships every bag within 24 hours of roasting. Pick a single origin, a house blend or a subscription you can pause, skip or cancel anytime.',+ sections: [+ {+ h2: 'Why fresh roasted coffee tastes better',+ p: [+ 'Coffee is at its best between 4 and 21 days after roasting. Supermarket bags often sit on shelves for months, so the aromatics that make a cup interesting are long gone by the time you brew.',+ 'We print the roast date on every bag and roast only what has been ordered, so the beans you open are always inside that window.',+ ],+ },+ {+ h2: 'Shop by how you brew',+ list: [+ 'Espresso at home: our Espresso Roast and Crema Espresso Blend',+ 'Pour-over and drip: single origins from Ethiopia, Colombia and Kenya',+ 'French press and cold brew: Sumatra Mandheling and Midnight Dark Roast',+ 'Evenings: Water Process Decaf, with no solvents',+ ],+ },+ {+ h2: 'Subscriptions that fit your week',+ p: [+ 'Choose weekly, every two weeks or monthly deliveries. Change the coffee, the grind or the date from your account, and pause whenever you travel.',+ ],+ },+ ],+ faq: [+ [+ 'How soon after roasting do you ship?',+ 'Every order ships within 24 hours of roasting, Monday to Friday.',+ ],+ [+ 'Can I pause my subscription?',+ 'Yes. You can pause, skip a delivery or cancel anytime from your account.',+ ],+ [+ 'Do you sell ground coffee?',+ 'Yes. Every coffee comes as whole beans or ground for espresso, drip or French press.',+ ],+ ],+ },+ {+ path: '/subscriptions',+ kind: 'pricing',+ title: citedTitle('/subscriptions'),+ description:+ 'Coffee subscriptions from $18 a bag: House Blend, Single Origin Club and Espresso Club, delivered weekly, every two weeks or monthly. Pause anytime.',+ h1: 'Coffee Subscriptions, Roasted to Order',+ rounds: [0, 1, 2],+ since: { faqSchema: 1, twitterCard: 2 },+ intro:+ 'A Brewline subscription sends freshly roasted coffee on your schedule. Every plan ships within a day of roasting, and you can pause, skip or cancel anytime.',+ sections: [+ {+ h2: 'Our plans',+ p: [+ 'House Blend Subscription: $18 per bag. Our chocolatey everyday blend, the same every delivery.',+ 'Single Origin Club: $22 per bag. A different single origin each delivery, with tasting notes from our roasters.',+ 'Espresso Club: $20 per bag. Our espresso roasts in rotation, developed for home machines.',+ ],+ },+ {+ h2: 'Compare plans',+ table: {+ head: ['Plan', 'Price per bag', 'Coffee', 'Best for'],+ rows: [+ ['House Blend Subscription', '$18', 'House Blend', 'Daily drip or French press'],+ ['Single Origin Club', '$22', 'Rotating single origins', 'Pour-over and exploring'],+ ['Espresso Club', '$20', 'Espresso roasts', 'Home espresso machines'],+ ],+ },+ },+ {+ h2: 'How deliveries work',+ list: [+ 'Choose weekly, every two weeks or monthly deliveries',+ 'Pick whole beans or a grind for your brewer',+ 'Shipping is free on every subscription order in the US',+ 'Pause, skip or cancel anytime from your account',+ ],+ },+ ],+ faq: [+ ['Is shipping included?', 'Yes, shipping is free on every subscription order in the US.'],+ [+ 'Can I change my coffee?',+ 'Yes. Switch plans or coffees before your next roast date and the change applies to that delivery.',+ ],+ [+ 'What if I am away?',+ 'Pause your subscription or skip a delivery from your account. There is no fee.',+ ],+ ],+ },+ {+ path: '/subscriptions/house-blend',+ kind: 'product',+ product: 'BL-SUB-HOU',+ title: 'House Blend Coffee Subscription | Brewline Coffee',+ description:+ 'Our House Blend on a subscription: chocolate and hazelnut notes, roasted to order and delivered on your schedule from $18 a bag.',+ h1: 'House Blend Coffee Subscription',+ rounds: [0, 1, 2],+ since: { productJsonLd: 1, descriptionOpening: 2 },+ },+ {+ path: '/gifts',+ kind: 'collection',+ title: citedTitle('/gifts'),+ description:+ 'Coffee gifts that keep arriving: prepaid gift subscriptions and tasting sets, with a personal note and a delivery date you choose.',+ h1: 'Coffee Gift Subscriptions and Gift Sets',+ rounds: [2],+ intro:+ 'Give freshly roasted coffee: a prepaid subscription delivered for three, six or twelve months, or a tasting set of our current single origins.',+ sections: [+ {+ h2: 'Gift subscriptions',+ p: [+ 'Prepaid gift subscriptions send a fresh bag every month. Add a personal note and choose the date the first bag arrives.',+ ],+ },+ {+ h2: 'Tasting sets',+ p: [+ 'The Single Origin Tasting Set has four 4 oz bags of our current single origins, with tasting cards for each.',+ ],+ },+ ],+ products: ['BL-GFT-3M', 'BL-GFT-TST', 'BL-SUB-SOC'],+ },+ {+ ...collection('/collections/single-origin'),+ kind: 'collection',+ rounds: [2],+ intro:+ 'Our single origins come from one farm or one region, so each bag tastes of where it was grown. We roast them light to medium to keep that character.',+ sections: [+ {+ h2: 'Finding your single origin',+ p: [+ 'East African coffees like Ethiopia Yirgacheffe and Kenya Nyeri are floral and bright. Central and South American coffees are rounder, with caramel and chocolate.',+ ],+ },+ ],+ products: ['BL-ETH', 'BL-COL', 'BL-GUA', 'BL-KEN', 'BL-SUM', 'BL-BRA', 'BL-CRC'],+ },+ {+ ...collection('/collections/espresso'),+ kind: 'collection',+ rounds: [2],+ since: { metaDescription: 3 },+ intro: 'Espresso roasts for home machines.',+ sections: [],+ products: ['BL-ESP', 'BL-CRM', 'BL-DES'],+ },+ {+ path: '/coffee/espresso-roast',+ kind: 'product',+ product: 'BL-ESP',+ title: citedTitle('/coffee/espresso-roast'),+ description:+ 'Espresso Roast whole bean coffee with dark chocolate and cherry notes and a thick crema. Roasted to order for home espresso machines.',+ h1: 'Espresso Roast Whole Bean Coffee',+ rounds: [0, 1, 2],+ since: { faqSchema: 2, descriptionOpening: 1 },+ faq: [+ [+ 'What grind should I use?',+ 'Start fine, at about 18 grams in and 36 grams out in 28 seconds, then adjust.',+ ],+ [+ 'How long does a bag stay fresh?',+ 'Use it within a month of the roast date, and rest it 5 to 7 days before pulling shots.',+ ],+ ],+ },+ {+ path: '/coffee/decaf',+ kind: 'product',+ product: 'BL-DEC',+ title: citedTitle('/coffee/decaf'),+ description:+ 'Water Process Decaf whole bean coffee: decaffeinated with water only, no solvents, with milk chocolate and plum notes.',+ h1: 'Water Process Decaf Coffee',+ rounds: [0, 1, 2],+ since: { faqSchema: 1, descriptionOpening: 2 },+ faq: [+ [+ 'How much caffeine is left?',+ 'The water process removes 99.9% of the caffeine, so a cup has about 2 mg.',+ ],+ [+ 'Does decaf taste different?',+ 'Ours keeps its sweetness because no solvents touch the beans.',+ ],+ ],+ },+ {+ path: '/coffee/ethiopia-yirgacheffe',+ kind: 'product',+ product: 'BL-ETH',+ title: 'Ethiopia Yirgacheffe Coffee Beans | Brewline Coffee',+ description:+ 'Ethiopia Yirgacheffe single origin coffee with jasmine, bergamot and lemon notes, light roasted to order.',+ h1: 'Ethiopia Yirgacheffe Single Origin',+ rounds: [2],+ since: { descriptionOpening: 0 },+ },+ {+ path: '/coffee/colombia-huila',+ kind: 'product',+ product: 'BL-COL',+ title: 'Colombia Huila Coffee Beans | Brewline Coffee',+ description: 'Colombia Huila coffee beans.',+ h1: 'Colombia Huila',+ rounds: [2],+ since: { descriptionOpening: 3, productJsonLd: 3 },+ },+ {+ path: '/coffee/sumatra-mandheling',+ kind: 'product',+ product: 'BL-SUM',+ title: 'Sumatra Mandheling Low Acid Coffee | Brewline Coffee',+ description:+ 'Sumatra Mandheling dark roast whole bean coffee: low acidity, with cedar, dark chocolate and molasses notes.',+ h1: 'Sumatra Mandheling Dark Roast',+ rounds: [2],+ },+ {+ path: '/blog/how-fresh-should-coffee-beans-be',+ kind: 'article',+ author: 'maya',+ published: 160,+ modified: 19,+ title: citedTitle('/blog/how-fresh-should-coffee-beans-be'),+ description:+ 'Coffee beans taste best 4 to 21 days after roasting. Here is how to read a roast date, store beans and tell when coffee has gone stale.',+ h1: 'How Fresh Should Coffee Beans Be When They Arrive?',+ rounds: [0, 1, 2],+ since: { faqSchema: 1, quote: 2 },+ intro:+ 'Coffee beans taste best between 4 and 21 days after their roast date. Before day 4 they are still releasing carbon dioxide, and after about three weeks the aromatics fade. When we tested the same Colombia Huila every few days for six weeks, our team found the sweetest cups between day 7 and day 14.',+ sections: [+ {+ h2: 'What a roast date tells you',+ p: [+ 'A roast date is the day the beans left the roaster. A best-by date is usually set 12 to 18 months later and says nothing about flavor. Look for the roast date first; if a bag only shows a best-by date, assume it was roasted long ago.',+ 'Degassing is the release of carbon dioxide trapped during roasting. It is why fresh beans bloom when water hits them, and why espresso needs a few more days of rest than filter coffee.',+ ],+ },+ {+ h2: 'The freshness window by brew method',+ table: {+ head: ['Brew method', 'Rest after roasting', 'Best within'],+ rows: [+ ['Pour-over and drip', '3 to 5 days', '21 days'],+ ['Espresso', '5 to 7 days', '28 days'],+ ['French press', '2 to 4 days', '21 days'],+ ['Cold brew', '2 days', '30 days'],+ ],+ },+ p: [+ 'These are starting points, not rules. Darker roasts degas faster and fade sooner; light roasts often need a week before they open up.',+ ],+ },+ {+ h2: 'How to store coffee beans',+ list: [+ 'Keep beans in an airtight, opaque container',+ 'Store them at room temperature, away from the stove and sunlight',+ 'Grind right before brewing: ground coffee goes stale within an hour',+ 'Freeze only what you will not use within a month, in small sealed portions',+ ],+ p: [+ 'In our experience, a one-way valve bag kept closed with a clip works as well as most canisters for the first two weeks.',+ ],+ },+ {+ h2: 'Signs your coffee has gone stale',+ p: [+ 'Stale beans smell flat or papery, barely bloom when you pour water over them, and brew a cup that tastes hollow and bitter. If a fresh grind smells of little at all, the beans are past their best.',+ 'For a side by side test, brew one cup from a bag roasted last week and one from a supermarket bag. The difference in aroma and sweetness is the clearest lesson in freshness there is.',+ ],+ },+ {+ h2: 'Why we roast to order',+ p: [+ 'Every Brewline bag is roasted after it is ordered and ships within 24 hours, so it arrives on day 2 or 3 and is ready to brew by the time you finish the bag before it. Independent guides such as the Roast Notes freshness guide reach the same window.',+ ],+ },+ ],+ links: [+ ['https://roastnotes.example/guides/espresso-beans', 'Roast Notes guide to espresso beans'],+ [+ 'https://beanbulletin.example/best-coffee-subscriptions-2026',+ 'Bean Bulletin subscription review',+ ],+ ],+ quote: [+ 'Most home brewers open a bag too early for espresso and too late for pour-over.',+ 'Maya Okafor, Head Roaster',+ ],+ faq: [+ [+ 'Is coffee bad after the best-by date?',+ 'It is safe, but it will taste flat. Use the roast date to judge freshness.',+ ],+ [+ 'Should I freeze coffee beans?',+ 'Only beans you will not use within a month, sealed in small portions.',+ ],+ [+ 'How long does ground coffee stay fresh?',+ 'Less than an hour at its best, so grind right before you brew.',+ ],+ ],+ },+ {+ path: '/blog/single-origin-vs-blend',+ kind: 'article',+ author: 'daniel',+ published: 95,+ modified: 33,+ title: 'Single Origin vs Blend: Which Coffee Should You Buy? | Brewline',+ description:+ 'Single origin coffee shows off one place; a blend aims for balance. How they differ in taste, price and brewing, and which to buy for your brewer.',+ h1: 'Single Origin vs Blend: Which Coffee Should You Buy?',+ rounds: [2],+ intro:+ 'Buy a single origin when you want to taste one place, and a blend when you want the same balanced cup every morning. Neither is better; they answer different questions.',+ sections: [+ {+ h2: 'What single origin means',+ p: [+ 'A single origin coffee comes from one country, region or farm. Its flavor depends on the variety, the altitude and the process, so an Ethiopia Yirgacheffe and a Brazil Cerrado can taste nothing alike.',+ ],+ },+ {+ h2: 'What a blend is for',+ p: [+ 'A blend combines two or more coffees to balance sweetness, body and acidity, and to stay consistent as harvests change. Our House Blend pairs a Brazil for body with a Colombia for sweetness.',+ ],+ },+ {+ h2: 'How they compare',+ table: {+ head: ['', 'Single origin', 'Blend'],+ rows: [+ ['Flavor', 'Distinct, changes by harvest', 'Balanced, consistent'],+ ['Best brewers', 'Pour-over, drip', 'Espresso, French press, drip'],+ ['Price per bag', '$17.50 to $21', '$15 to $16'],+ ],+ },+ },+ {+ h2: 'Which to choose',+ list: [+ 'Pick a single origin to explore flavors on pour-over',+ 'Pick a blend for milk drinks and a reliable daily cup',+ 'Try the Single Origin Club to taste a new origin each month',+ ],+ },+ ],+ links: [['https://roastnotes.example/guides/single-origin-vs-blend', 'Roast Notes comparison']],+ },+ {+ path: '/blog/low-acid-coffee-guide',+ kind: 'article',+ author: 'daniel',+ published: 70,+ modified: 70,+ title: 'Low Acid Coffee: A Guide for Sensitive Stomachs | Brewline',+ description: 'Low acid coffee beans and brewing tips.',+ h1: 'Low Acid Coffee Guide',+ rounds: [2],+ noByline: true,+ intro:+ 'Some people find coffee hard on the stomach. Darker roasts, beans from low-altitude farms and cold brew all tend to taste less acidic.',+ sections: [+ {+ h2: 'Which beans are lower in acid',+ p: [+ 'Sumatra Mandheling and Brazil Cerrado are naturally low in perceived acidity. A darker roast lowers it further.',+ ],+ },+ {+ h2: 'Brewing for less acidity',+ p: [+ 'Cold brew extracts fewer acids than hot water. A coarser grind and a shorter brew help too.',+ ],+ },+ ],+ },+ {+ path: '/blog/espresso-at-home',+ kind: 'article',+ author: 'maya',+ published: 45,+ modified: 12,+ title: 'Espresso at Home: A Beginner Recipe That Works | Brewline',+ description:+ 'A simple espresso recipe for home machines: 18 grams in, 36 grams out in 28 seconds, plus how to fix sour or bitter shots.',+ h1: 'Espresso at Home: A Beginner Recipe That Works',+ rounds: [2],+ intro:+ 'Start every espresso at 18 grams of coffee in, 36 grams out, in about 28 seconds. That 1:2 ratio is the baseline most cafes begin from, and it is the easiest place to learn what to change.',+ sections: [+ {+ h2: 'The base recipe',+ list: [+ 'Dose: 18 grams of freshly ground coffee',+ 'Yield: 36 grams of espresso in the cup',+ 'Time: 25 to 30 seconds from the pump starting',+ 'Water: 93 degrees Celsius if your machine lets you set it',+ ],+ },+ {+ h2: 'Fixing sour or bitter shots',+ p: [+ 'A sour, thin shot ran too fast: grind finer. A bitter, harsh shot ran too slow: grind coarser. Change one thing at a time and taste after each change.',+ 'We tested this recipe on four entry-level machines; in our testing the grind setting mattered more than the machine.',+ ],+ },+ {+ h2: 'Choosing beans for espresso',+ p: [+ 'Medium to medium-dark roasts are the most forgiving. Our Espresso Roast was developed for home machines and pulls well from day 5 to day 28 after roasting.',+ ],+ },+ ],+ links: [['https://roastnotes.example/guides/espresso-beans', 'Roast Notes espresso guide']],+ },+ {+ path: '/about',+ kind: 'about',+ title: 'About Us | Brewline Coffee',+ description:+ 'Brewline Coffee is a small-batch roaster founded in 2016. We roast every order fresh and ship it within a day of roasting.',+ h1: 'About Brewline Coffee',+ rounds: [2],+ intro:+ 'Brewline Coffee was founded in 2016 by two home baristas who were tired of stale supermarket beans. Today our team of 14 roasts every order in small batches of 12 kg.',+ sections: [+ {+ h2: 'How we roast',+ p: [+ 'Every bag ships within 24 hours of roasting. We roast in small batches of 12 kg and cup every batch before it is packed.',+ ],+ },+ {+ h2: 'Who we buy from',+ p: [+ 'We buy green coffee from importers who publish what farmers are paid, and we list the farm or cooperative on every single origin bag.',+ ],+ },+ {+ h2: 'Our team',+ list: [+ 'Maya Okafor, Head Roaster',+ 'Daniel Reyes, Coffee Editor',+ 'A customer care team that answers every email within one business day',+ ],+ },+ ],+ },+ {+ path: '/contact',+ kind: 'contact',+ title: 'Contact Us | Brewline Coffee',+ description:+ 'Questions about an order or a subscription? Email [email protected] and we answer within one business day.',+ h1: 'Contact Brewline Coffee',+ rounds: [2],+ intro:+ 'Email [email protected] about an order, a subscription or a wholesale account. We answer within one business day, Monday to Friday.',+ sections: [],+ },+ {+ path: '/faq',+ kind: 'faq',+ title: 'Frequently Asked Questions | Brewline Coffee',+ description:+ 'Answers about roast dates, shipping, subscriptions, grinding and returns for Brewline Coffee orders.',+ h1: 'Frequently Asked Questions',+ rounds: [2],+ intro: 'Answers to the questions we hear most about orders, subscriptions and our coffee.',+ sections: [],+ faq: [+ [+ 'When will my order ship?',+ 'Within 24 hours of roasting, Monday to Friday. You get a tracking link by email.',+ ],+ [+ 'How do I pause a subscription?',+ 'Open your account, choose the subscription and select Pause or Skip.',+ ],+ ['Do you ship outside the US?', 'Not yet. We ship to all 50 US states.'],+ [+ 'What if I do not like a coffee?',+ 'Tell us within 30 days and we will send a different coffee or a refund.',+ ],+ ],+ },+];++/**+ * Pages of the site that aren't audited: they only list in Site Audit's page+ * inventory (and its templates) with their title.+ */+export const OTHER_PAGES = [+ {+ path: '/shipping',+ title: 'Shipping | Brewline Coffee',+ h1: 'Shipping',+ description: 'Free shipping on subscriptions; orders ship within 24 hours of roasting.',+ },+ {+ path: '/blog',+ title: 'Coffee Guides | Brewline Coffee',+ h1: 'Coffee Guides',+ description: 'Brewing guides, buying advice and roast notes from the Brewline team.',+ },+ ...AUTHORS.map((a) => ({+ path: a.path,+ title: `${a.name} | Brewline Coffee`,+ h1: a.name,+ description: `${a.name}, ${a.role} at Brewline Coffee. ${a.bio}`.slice(0, 160),+ })),+];++/**+ * A page that no longer exists: AI answers still link to it, so the+ * snippet sees visits land on a 404 (Broken AI links).+ */+export const BROKEN_PAGE = '/blog/cold-brew-ratio-guide';++/**+ * The searches Brewline shows up for on Google (Search Console): query,+ * daily impressions at the start and end of the window, average position.+ */+export const SEARCH_QUERIES = [+ ['brewline coffee', 140, 190, 1.2],+ ['brewline coffee subscription', 55, 80, 1.4],+ ['brewline espresso roast', 20, 34, 1.6],+ ['coffee subscription', 420, 520, 18.5],+ ['fresh roasted coffee beans', 260, 330, 9.8],+ ['how long do coffee beans stay fresh', 380, 470, 6.2],+ ['coffee roast date', 90, 120, 4.1],+ ['water process decaf', 150, 210, 7.4],+ ['best decaf coffee beans', 130, 170, 12.6],+ ['single origin vs blend coffee', 70, 95, 5.3],+ ['low acid coffee beans', 210, 240, 21.4],+ ['espresso beans for home', 160, 230, 11.1],+ ['colombia huila coffee', 45, 60, 8.9],+ ['coffee gift subscription', 180, 260, 15.7],+];diff --git a/server/src/lib/demo/site.js b/server/src/lib/demo/site.jsnew file mode 100644index 000000000..8987177e8--- /dev/null+++ b/server/src/lib/demo/site.js@@ -0,0 +1,754 @@+/**+ * The demo brand's Site Audit (demo-site): its audits, its page inventory,+ * its site trust and its brand facts.+ *+ * The audits are the audit engine's own: each of Brewline's pages+ * (site-html.js) is parsed by auditContextOf and scored by runSignals and+ * scoreAudit, as a fetched page is. Only what a model or an outside lookup+ * would say (the LLM-judged signals, Wikidata's brand entity and the fix+ * recommendations) is written here, in the shapes those steps store. The+ * page inventory goes through site-pages.js's selectPages and parseHead,+ * site trust through site-trust.js's checks, and the brand facts through+ * brand-facts.js's factText and keptFacts, as if a model had quoted them.+ *+ * Three audit rounds: six key pages audited by a person three weeks ago and+ * again by the weekly auto-audit, then every page by the latest one. Fixes+ * land on the pages between rounds, so their scores climb.+ */++import supabaseAdmin from '../../config/supabase.js';+import { auditContextOf } from '../audit/context.js';+import { runSignals } from '../audit/engine.js';+import { LLM_SIGNAL_KEYS } from '../audit/llm-signals.js';+import { opportunityKey } from '../audit/opportunities.js';+import { RECOMMENDABLE_SIGNALS } from '../audit/recommendations.js';+import { RUBRIC_VERSION, TOTAL_SIGNALS, signalsByKey } from '../audit/rubric.js';+import { scoreAudit } from '../audit/scorer.js';+import { factText, factTextHash, keptFacts } from '../brand-facts.js';+import { sourceClassModelId } from '../content-source-class.js';+import { parseHead, selectPages } from '../site-pages.js';+import { SITE_TRUST_CHECKS, authorPage, findPage, hasOrganization } from '../site-trust.js';+import { BRAND } from './brewline.js';+import { demoId } from './random.js';+import { brandCitations } from './site-common.js';+import { allSettledOrThrow, demoWriter } from './write.js';+import { LLMS_TXT, ROBOTS_TXT, pageHtml, productDescription } from './site-html.js';+import {+ AUDITED_PAGES,+ AUTHORS,+ CATALOG,+ COLLECTIONS,+ OTHER_PAGES,+ SITE_URL,+ pageUrl,+} from './site-world.js';+import { gaSessionsByPath } from './traffic.js';++const DAY_MS = 24 * 60 * 60 * 1000;++/** The audit rounds, oldest first: when, and who started them. */+export const AUDIT_ROUNDS = [+ { daysAgo: 24, source: 'user' },+ { daysAgo: 12, source: 'auto' },+ { daysAgo: 2, source: 'auto' },+];++/** Seconds between two audits of a round, and how long one takes. */+const AUDIT_GAP_MS = 40_000;+const AUDIT_TAKES_MS = 25_000;++/** llm-signals.js's default model, which the stored verdicts name. */+const AUDIT_MODEL = 'google/gemini-3-flash-preview';++/** When the page inventory and the brand facts were last read. */+const PAGES_READ_DAYS_AGO = 6;+const FACTS_READ_DAYS_AGO = 1;++const has = (page, fix, round) => round >= (page.since?.[fix] ?? 0);+const productOf = (id) => CATALOG.find((p) => p.id === id);++/** What a page is about, in a few words, for the verdicts' reasons. */+function topicOf(page) {+ if (page.kind === 'product') return productOf(page.product).title;+ return page.h1.replace(/\?$/, '').toLowerCase();+}++/**+ * The LLM-judged verdicts of one page in one round, in llm-signals.js's+ * stored shape: [status, reason] by signal; 'na' carries no score.+ */+function llmVerdicts(page, round) {+ const topic = topicOf(page);+ const product = page.kind === 'product' ? productOf(page.product) : null;+ const pass = (reason) => ['pass', reason];+ const warn = (reason) => ['warn', reason];+ const fail = (reason) => ['fail', reason];+ const na = (reason) => ['na', reason];+ const noPress = fail('No reference to press or independent reviews of the brand on this page.');++ if (page.kind === 'article') {+ const byline = !page.noByline;+ return {+ 'direct-answer': pass(`The first sentence answers ${topic} directly.`),+ 'bluf-answer': page.noByline+ ? warn('The opening describes the problem but states no clear recommendation.')+ : pass("The opening paragraph states the page's main claim before any detail."),+ 'query-coverage': pass(`The body uses the terms a reader searching for ${topic} would use.`),+ 'sub-query-coverage': warn(+ 'Covers the main follow-up questions, but not cost per cup or how to brew decaf.',+ ),+ 'entity-coverage': pass(+ 'Names origins, roast levels and brew methods relevant to the topic.',+ ),+ 'info-density': page.noByline+ ? warn('Short paragraphs with general advice and few specifics.')+ : pass('Most paragraphs carry a specific number, method or recommendation.'),+ 'mega-page-coverage': warn(+ 'Related sub-topics are touched on briefly rather than covered in their own sections.',+ ),+ definitions: page.path.includes('fresh')+ ? pass('Defines degassing and the roast date where they first appear.')+ : warn('Uses terms such as extraction and body without defining them.'),+ 'technical-terms': pass('Uses roasting and brewing terms correctly and in context.'),+ 'case-study-evidence': warn(+ "Describes the team's own tests but names no customer outcome or before-and-after result.",+ ),+ 'author-credentials': byline+ ? pass("The byline gives the author's role, and the bio their years of experience.")+ : fail('No author is named, so no credentials are visible.'),+ 'byline-depth': byline+ ? pass('The byline links to an author page with a short bio.')+ : fail('There is no byline to link to an author page.'),+ 'press-mentions': noPress,+ };+ }++ if (page.kind === 'product') {+ const opening = has(page, 'descriptionOpening', round);+ return {+ 'direct-answer': opening+ ? pass(`The opening names ${topic}, its roast and its tasting notes.`)+ : warn('The description opens with a slogan; the product facts come after it.'),+ 'bluf-answer': opening+ ? pass('The first sentence says what the product is and who it suits.')+ : fail('The first paragraph makes no clear claim about the product.'),+ 'query-coverage': pass(`The page uses the words a buyer of ${topic} would search for.`),+ 'sub-query-coverage': warn(+ 'Brewing and freshness are covered; caffeine, origin story and grind options are not.',+ ),+ 'entity-coverage': warn(+ product?.process+ ? 'Names the origin and process but not the farm or cooperative.'+ : 'Names the coffees in it but not where they come from.',+ ),+ 'info-density': pass('Short, specific copy: prices, sizes, notes and a brew recipe.'),+ 'mega-page-coverage': na('A single product page; broad sub-topic coverage does not apply.'),+ definitions: product?.process+ ? warn(`Uses "${product.process}" without saying what the process means.`)+ : na('No specialist terms that need a definition.'),+ 'technical-terms': pass('Uses roast level, process and brew ratios correctly.'),+ 'case-study-evidence': na('Not expected on a product page.'),+ 'author-credentials': na('A product page has no author.'),+ 'byline-depth': na('A product page has no byline.'),+ 'press-mentions': fail('No reviews or press quotes are shown for this coffee.'),+ };+ }++ // Home, pricing, collections and the company pages.+ const thin = ['contact', 'collection'].includes(page.kind) && (page.sections ?? []).length < 2;+ return {+ 'direct-answer':+ round === 0+ ? warn('The opening sells the brand before it says what the page offers.')+ : thin+ ? warn('The page says what it offers only in a short line.')+ : pass('The opening sentence says what the page offers and for whom.'),+ 'bluf-answer': thin+ ? warn('The page has no opening paragraph that states a clear claim.')+ : pass("The first paragraph states the page's main promise."),+ 'query-coverage': pass(`The page covers the terms a visitor looking for ${topic} would use.`),+ 'sub-query-coverage':+ page.kind === 'faq' || page.kind === 'pricing'+ ? pass('Answers the follow-up questions about shipping, pausing and changing plans.')+ : warn('Leaves common follow-up questions about shipping and grind options unanswered.'),+ 'entity-coverage': pass('Names the coffees, origins and plans the page is about.'),+ 'info-density': thin+ ? warn('Very little content beyond navigation and a short introduction.')+ : pass('The copy is specific: prices, delivery options and product names.'),+ 'mega-page-coverage':+ page.kind === 'home'+ ? pass('Covers shopping, subscriptions and freshness in sections of their own.')+ : na('A focused page; broad sub-topic coverage does not apply.'),+ definitions: na('No specialist terms that need a definition.'),+ 'technical-terms':+ page.kind === 'contact'+ ? na('No subject matter where technical terms apply.')+ : pass('Uses roast and brewing terms where they fit.'),+ 'case-study-evidence':+ page.kind === 'home' || page.kind === 'pricing'+ ? fail('No customer stories or results are shown.')+ : na('Not expected on this kind of page.'),+ 'author-credentials':+ page.kind === 'about'+ ? pass("Names the head roaster and the team's experience.")+ : na('No authored content on this page.'),+ 'byline-depth': na('No byline on this kind of page.'),+ 'press-mentions': noPress,+ };+}++/** Wikidata knows no entity for a brand that doesn't exist (external-signals.js's no-match). */+const BRAND_ENTITY = { key: 'brand-entity', status: 'fail', score: 0, evidence: { matches: 0 } };++/**+ * A recommendation for a failing or warning signal, in recommendations.js's+ * stored shape (after its guard): page-specific, nothing invented, an+ * unknown fact as a [placeholder]. Null for a signal with none here.+ */+/** Signals a model leaves out for a page of this kind: nothing specific to say. */+const NOT_FOR = {+ 'author-byline': ['product', 'collection', 'home', 'pricing', 'contact', 'faq', 'about'],+ 'author-credentials': ['product', 'collection', 'home', 'pricing', 'contact', 'faq'],+ 'byline-depth': ['product', 'collection', 'home', 'pricing', 'contact', 'faq'],+ 'first-person': ['product', 'collection', 'contact', 'faq'],+ 'citation-density': ['product', 'collection', 'home', 'pricing', 'contact', 'faq'],+ 'outbound-authority-links': ['product', 'collection', 'home', 'pricing', 'contact', 'faq'],+ quotations: ['product', 'collection', 'pricing', 'contact', 'faq'],+ 'statistic-density': ['contact', 'faq'],+ 'case-study-evidence': ['product', 'collection', 'contact', 'faq'],+ 'faq-schema': ['contact'],+};++function recommendationFor(key, page, result) {+ if (NOT_FOR[key]?.includes(page.kind)) return null;+ const product = page.kind === 'product' ? productOf(page.product) : null;+ const topic = topicOf(page);+ const write = (recommendation, draft = null, code = null) => ({ recommendation, draft, code });+ switch (key) {+ case 'faq-schema':+ return page.faq+ ? write(+ 'Mark up the questions this page already answers as FAQPage JSON-LD.',+ page.faq.map(([q, a]) => `Q: ${q}\nA: ${a}`).join('\n\n'),+ JSON.stringify(+ {+ '@context': 'https://schema.org',+ '@type': 'FAQPage',+ mainEntity: page.faq.map(([q, a]) => ({+ '@type': 'Question',+ name: q,+ acceptedAnswer: { '@type': 'Answer', text: a },+ })),+ },+ null,+ 2,+ ),+ )+ : write(+ `Add three short questions buyers ask about ${topic}, with answers and FAQPage markup.`,+ 'Q: [question a buyer asks]\nA: [answer from this page]',+ );+ case 'meta-description':+ return write(+ 'Write a meta description that says what the page offers in one sentence.',+ product+ ? `${product.title}: ${product.notes}, roasted to order and shipped within 24 hours of roasting.`+ : (page.description ?? `${page.h1} from ${BRAND.name}, roasted to order.`),+ );+ case 'h1-quality':+ return write(+ 'Make the H1 a full name of 20 to 70 characters that says what the page sells.',+ product ? `${product.title} Whole Bean Coffee` : null,+ );+ case 'h2-coverage':+ return write(+ 'Split the page into at least three sections with H2 headings buyers scan for.',+ product+ ? 'Tasting notes\nHow to brew it\nShipping and freshness'+ : 'What you get\nHow it works\nQuestions',+ );+ case 'json-ld-relevance':+ return product+ ? write(+ 'Add Product markup with the name, brand and offers so engines read the price.',+ null,+ JSON.stringify(+ {+ '@context': 'https://schema.org',+ '@type': 'Product',+ name: product.title,+ brand: { '@type': 'Brand', name: BRAND.name },+ offers: { '@type': 'Offer', price: '[price]', priceCurrency: 'USD' },+ },+ null,+ 2,+ ),+ )+ : write('Describe the page with a schema.org type that fits it, such as CollectionPage.');+ case 'open-graph':+ return write('Add og:description and og:image so shared links show a preview.');+ case 'outbound-authority-links':+ return write(+ 'Cite an independent source for the brewing and storage advice on this page.',+ 'Link [source URL] where the page states [claim].',+ );+ case 'citation-density':+ return write('Link the sources behind the main claims, about two per 1,000 words.');+ case 'statistic-density':+ return write(+ 'Back the main claims with numbers, such as roast-to-ship time or grind settings.',+ );+ case 'quotations':+ return write(+ `Add a short attributed quote from your roaster about ${topic}.`,+ `"[quote from the roaster]" (${AUTHORS[0].name}, ${AUTHORS[0].role})`,+ );+ case 'author-byline':+ return write('Name the author of this guide in a byline under the title.');+ case 'direct-answer':+ case 'bluf-answer':+ return write(+ "Open with one sentence that answers the page's main question.",+ product ? productDescription(product).split('. ')[0].replace(/\.?$/, '.') : null,+ );+ case 'product-description-opening':+ return write(+ 'Start the description with the product type and two attributes, not a slogan.',+ product ? productDescription(product).split('. ')[0].replace(/\.?$/, '.') : null,+ );+ case 'readability': {+ const flesch = result?.evidence?.fleschReadingEase;+ return typeof flesch === 'number' && flesch > 70+ ? write('Add the detail readers expect: fuller sentences on taste, origin and brewing.')+ : write('Shorten the longest sentences; aim for a Flesch score between 50 and 70.');+ }+ case 'length':+ return write(+ product+ ? 'Add a section on how this coffee tastes on different brewers.'+ : 'Expand the thinnest sections with specifics buyers ask about.',+ );+ case 'first-person':+ return write(+ 'Say how your team tested or brews this, in your own words.',+ 'When we tested [brew method], our team found [result].',+ );+ case 'case-study-evidence':+ return write(+ 'Add one customer example with a real outcome.',+ '[customer name] switched to [plan] and [real metric].',+ );+ case 'press-mentions':+ return write(+ 'Link press or review coverage of Brewline, if there is any.',+ '[publication]: [article URL]',+ );+ case 'definitions':+ return write(+ product?.process+ ? `Define "${product.process}" in one sentence where it first appears.`+ : 'Define the coffee terms the page uses where they first appear.',+ );+ case 'sub-query-coverage':+ return write(+ 'Answer the follow-up questions buyers ask, such as which grind to pick and how long a bag keeps.',+ );+ case 'mega-page-coverage':+ case 'entity-coverage':+ return write('Cover the related questions in sections of their own, each with a heading.');+ case 'author-credentials':+ case 'byline-depth':+ return write("Link the byline to an author page with the writer's experience.");+ default:+ return null;+ }+}++const TIER_RANK = { high: 0, medium: 1, standard: 2 };+/** Recommendations kept per audit, and how many of its failures lead as high priority. */+const MAX_RECOMMENDATIONS = 5;+const HIGH_PRIORITY = 2;++/**+ * The audit's recommendations, most important first: failures before+ * warnings, by the signal's impact; the leading failures (and any of high+ * impact) are high priority, other failures medium, warnings low.+ */+function recommendationsOf(page, results) {+ return results+ .filter((r) => RECOMMENDABLE_SIGNALS.has(r.key) && (r.status === 'fail' || r.status === 'warn'))+ .map((r) => ({ r, rec: recommendationFor(r.key, page, r) }))+ .filter(({ rec }) => rec)+ .map(({ r, rec }) => ({ r, rec, tier: TIER_RANK[signalsByKey[r.key]?.impactTier] ?? 2 }))+ .sort(+ (a, b) =>+ (a.r.status === 'fail' ? 0 : 1) - (b.r.status === 'fail' ? 0 : 1) || a.tier - b.tier,+ )+ .slice(0, MAX_RECOMMENDATIONS)+ .map(({ r, rec, tier }, i) => ({+ signalKey: r.key,+ label: signalsByKey[r.key]?.label ?? r.key,+ category: signalsByKey[r.key]?.category ?? null,+ priority: r.status === 'warn' ? 'low' : tier === 0 || i < HIGH_PRIORITY ? 'high' : 'medium',+ ...rec,+ }));+}++/** One page audited in one round: the audit row and its signal rows, as runAuditJob writes them. */+async function auditPage(page, round, { brandId, id, at }) {+ const url = pageUrl(page.path);+ const ctx = auditContextOf({+ url,+ statusCode: 200,+ html: pageHtml(page, { round, now: at }),+ robotsTxt: ROBOTS_TXT,+ llmsTxt: LLMS_TXT,+ now: at,+ });+ const deterministic = await runSignals(ctx);+ const verdicts = llmVerdicts(page, round);+ const llm = LLM_SIGNAL_KEYS.map((key) => {+ const [status, reason] = verdicts[key];+ const score = status === 'na' ? null : status === 'pass' ? 1 : status === 'warn' ? 0.5 : 0;+ return { key, status, score, evidence: { reason, model: AUDIT_MODEL } };+ });+ const results = [...deterministic, ...llm, BRAND_ENTITY];+ const { totalScore, categoryScores } = scoreAudit(results);++ const auditId = id(`audit:${round}:${page.path}`);+ const createdAt = new Date(at).toISOString();+ return {+ audit: {+ id: auditId,+ brand_id: brandId,+ url,+ final_url: ctx.url,+ status: 'completed',+ total_score: totalScore,+ category_scores: categoryScores,+ signals_evaluated: results.filter((r) => r.status !== 'na').length,+ signals_total: TOTAL_SIGNALS,+ rubric_version: RUBRIC_VERSION,+ error: null,+ recommendations: recommendationsOf(page, results),+ source: AUDIT_ROUNDS[round].source,+ created_at: createdAt,+ completed_at: new Date(at + AUDIT_TAKES_MS).toISOString(),+ },+ signals: results.map((r) => ({+ audit_id: auditId,+ signal_key: r.key,+ category: signalsByKey[r.key]?.category ?? null,+ status: r.status,+ score: r.score,+ evidence: r.evidence ?? {},+ created_at: new Date(at + AUDIT_TAKES_MS).toISOString(),+ })),+ };+}++/** The pages of the site the sitemap lists, with when each last changed. */+function sitemapPages(now) {+ const paths = [+ ...AUDITED_PAGES.map((p) => p.path),+ ...COLLECTIONS.map((c) => c.path),+ ...CATALOG.map((p) => p.path),+ ...OTHER_PAGES.map((p) => p.path),+ ];+ return [...new Set(paths)].map((path, i) => ({+ loc: pageUrl(path),+ lastmod: new Date(now - (3 + ((i * 7) % 60)) * DAY_MS).toISOString().slice(0, 10),+ }));+}++/** A page's head as the page inventory reads it (title, description, h1). */+function headOf(path, now) {+ const audited = AUDITED_PAGES.find((p) => p.path === path);+ if (audited) return parseHead(pageHtml(audited, { round: 2, now }));+ const collection = COLLECTIONS.find((c) => c.path === path);+ if (collection) return collection;+ const product = CATALOG.find((p) => p.path === path);+ if (product) {+ return {+ title: `${product.title} | ${BRAND.name}`,+ description: productDescription(product).slice(0, 300),+ h1: product.title,+ };+ }+ return OTHER_PAGES.find((p) => p.path === path) ?? { title: null, description: null, h1: null };+}++/**+ * The page inventory (site_pages), as refreshSitePages keeps it: pages AI+ * answers cite and GA reports traffic for first, then the sitemap's.+ */+function sitePageRows({ brandId, now, citations }) {+ const cutoff = now - 30 * DAY_MS;+ const counts = new Map();+ for (const c of citations) {+ if (Date.parse(c.created_at) < cutoff) continue;+ counts.set(c.url, (counts.get(c.url) ?? 0) + 1);+ }+ const selected = selectPages(+ {+ sitemap: sitemapPages(now),+ ga: [...gaSessionsByPath(now)],+ citations: [...counts].map(([url, n]) => ({ url, cur_citations: n })),+ },+ BRAND.domain,+ );+ const readAt = new Date(now - PAGES_READ_DAYS_AGO * DAY_MS).toISOString();+ return selected.map((page) => {+ const { title, description, h1 } = headOf(new URL(page.url).pathname || '/', now);+ return {+ brand_id: brandId,+ ...page,+ title: title ?? null,+ description: description ?? null,+ h1: h1 ?? null,+ fetch_status: 200,+ fetched_via: 'direct',+ fetched_at: readAt,+ updated_at: readAt,+ };+ });+}++/** The five site trust checks (site-trust.js), read from the page list and the home page. */+function siteTrustRow({ brandId, pages, now }) {+ const home = AUDITED_PAGES.find((p) => p.path === '/');+ const onSite = (link) => new URL(link).hostname === BRAND.domain;+ const about = findPage(pages, 'about');+ const contact = findPage(pages, 'contact');+ const editorial = findPage(pages, 'editorial');+ const authors = AUTHORS.map((a) => ({ name: a.name, url: authorPage(pages, a, onSite) }));+ const checks = {+ about: { ok: !!about, url: about },+ contact: { ok: !!contact, url: contact },+ editorial: { ok: !!editorial, url: editorial },+ organizationSchema: {+ ok: hasOrganization(pageHtml(home, { round: 2, now })),+ url: `${SITE_URL}/`,+ },+ authorPages: { ok: authors.every((a) => a.url), authors },+ };+ return {+ brand_id: brandId,+ checks,+ score: SITE_TRUST_CHECKS.filter((k) => checks[k].ok).length,+ complete: true,+ checked_at: new Date(now).toISOString(),+ scraped_at: null,+ };+}++/**+ * What a model reading the pricing, About and product pages would quote+ * (brand-facts.js keeps a fact only when its words are on the page), and+ * which a person has approved.+ */+const FACT_READS = [+ {+ kind: 'pricing',+ path: '/subscriptions',+ facts: [+ { kind: 'price', quote: '$18 per bag', plan: 'House Blend Subscription', approved: true },+ { kind: 'price', quote: '$22 per bag', plan: 'Single Origin Club', approved: true },+ { kind: 'price', quote: '$20 per bag', plan: 'Espresso Club', approved: true },+ {+ kind: 'claim',+ quote: 'Shipping is free on every subscription order in the US',+ approved: true,+ },+ { kind: 'claim', quote: 'Pause, skip or cancel anytime from your account' },+ ],+ },+ {+ kind: 'about',+ path: '/about',+ facts: [+ { kind: 'founded', quote: 'founded in 2016', approved: true },+ { kind: 'claim', quote: 'Every bag ships within 24 hours of roasting.', approved: true },+ { kind: 'claim', quote: 'We roast in small batches of 12 kg' },+ ],+ },+ {+ kind: 'product',+ path: '/coffee/espresso-roast',+ facts: [+ { kind: 'price', quote: '$16.50', plan: '12 oz', approved: true },+ { kind: 'price', quote: '$38.00', plan: '2 lb', approved: true },+ ],+ },+];++function brandFactRows({ brandId, now, id }) {+ const checkedAt = new Date(now - FACTS_READ_DAYS_AGO * DAY_MS).toISOString();+ const firstSeen = new Date(now - 27 * DAY_MS).toISOString();+ const reviewedAt = new Date(now - 25 * DAY_MS).toISOString();+ const model = sourceClassModelId().slice(0, 100);+ const pages = [];+ const facts = [];+ for (const read of FACT_READS) {+ const page = AUDITED_PAGES.find((p) => p.path === read.path);+ const text = factText(pageHtml(page, { round: 2, now }));+ const url = pageUrl(read.path);+ pages.push({+ brand_id: brandId,+ kind: read.kind,+ url,+ status: 'ok',+ text_hash: factTextHash(text),+ model,+ checked_at: checkedAt,+ extracted_at: checkedAt,+ });+ const kept = keptFacts(text, { facts: read.facts }, { now });+ for (const fact of kept) {+ const asked = read.facts.find(+ (f) => f.kind === fact.kind && fact.quote.toLowerCase().includes(f.quote.toLowerCase()),+ );+ facts.push({+ id: id(`brand-fact:${fact.slot}:${fact.value_key}`),+ brand_id: brandId,+ ...fact,+ source_url: url,+ source_kind: read.kind,+ status: asked?.approved ? 'approved' : 'pending',+ reviewed_by: null,+ reviewed_at: asked?.approved ? reviewedAt : null,+ first_seen_at: asked?.approved ? firstSeen : checkedAt,+ last_seen_at: checkedAt,+ gone_at: null,+ });+ }+ }+ return { pages, facts };+}++/**+ * Every Site Audit row of the demo brand, built in memory: the same every+ * time but for the dates, which count back from `now`.+ *+ * @param {{ brandId: string, now: Date }} args+ */+export async function siteRows({ brandId, now }) {+ const id = (key) => demoId(brandId, key);+ const nowMs = now.getTime();+ const audits = [];+ const signals = [];+ for (const [round, { daysAgo }] of AUDIT_ROUNDS.entries()) {+ const day = new Date(nowMs - daysAgo * DAY_MS);+ day.setUTCHours(7, 0, 0, 0);+ let at = day.getTime();+ for (const page of AUDITED_PAGES.filter((p) => p.rounds.includes(round))) {+ at += AUDIT_GAP_MS;+ const audited = await auditPage(page, round, { brandId, id, at });+ audits.push(audited.audit);+ signals.push(...audited.signals);+ }+ }++ const sitePages = sitePageRows({ brandId, now: nowMs, citations: brandCitations(brandId, now) });+ const since = new Date(nowMs - 32 * DAY_MS).toISOString();+ const updatedAt = new Date(nowMs - 20 * DAY_MS).toISOString();+ const { pages: factPages, facts } = brandFactRows({ brandId, now: nowMs, id });++ return {+ audits,+ signals,+ sitePages: sitePages.map((p) => ({ ...p, created_at: since })),+ siteTrust: siteTrustRow({ brandId, pages: sitePages, now: nowMs }),+ // The brand's gear pages aren't worth auditing for AI answers.+ settings: {+ brand_id: brandId,+ exclude_patterns: ['/brewing/*'],+ updated_by: null,+ updated_at: updatedAt,+ },+ prefs: [+ ['/coffee/espresso-roast', 'pinned'],+ ['/subscriptions/espresso-club', 'pinned'],+ ['/shipping', 'excluded'],+ ].map(([path, state]) => ({+ id: id(`audit-pref:${path}`),+ brand_id: brandId,+ page_key: opportunityKey(pageUrl(path)),+ url: pageUrl(path),+ state,+ created_by: null,+ created_at: updatedAt,+ updated_at: updatedAt,+ })),+ factPages,+ facts,+ };+}++/** Signal rows written per statement: an audit's 48 a few times over. */+const SIGNAL_CHUNK = 480;++/** Its tables' key columns where a row has no `id` of its own. */+const KEYS = {+ site_pages: 'brand_id,url',+ audit_signal_results: 'audit_id,signal_key',+ site_trust: 'brand_id',+ site_audit_brand_settings: 'brand_id',+ brand_fact_pages: 'brand_id,kind',+};+/** The audits' signals have no brand_id: found through their audit. */+const SCOPES = { audit_signal_results: ['audit_id', 'site_audits'] };++/**+ * The Site Audit module: its brand-scoped tables and its build.+ */+export const site = {+ name: 'site',+ // Rebuilt in place (write.js): written over, what isn't written again deleted after.+ inPlace: true,+ keys: KEYS,+ scopes: SCOPES,+ tables: [+ 'site_pages',+ // And, through it (on delete cascade), audit_signal_results.+ 'site_audits',+ 'site_trust',+ 'site_audit_brand_settings',+ 'site_audit_page_prefs',+ 'brand_fact_pages',+ 'brand_facts',+ ],++ /**+ * Its independent tables side by side, through the build's writer (a+ * writer over `db` for a first build without one).+ *+ * @param {{ brandId: string, now: Date, db?: object, rebuild?: boolean, write?: Function }} ctx+ */+ async build({+ brandId,+ now,+ db = supabaseAdmin,+ rebuild = false,+ write = demoWriter({ db, brandId, rebuild, keys: KEYS, scopes: SCOPES }).write,+ }) {+ const rows = await siteRows({ brandId, now });+ await allSettledOrThrow([+ write('site_pages', rows.sitePages),+ write('site_audits', rows.audits),+ write('site_trust', [rows.siteTrust]),+ write('site_audit_brand_settings', [rows.settings]),+ write('site_audit_page_prefs', rows.prefs),+ write('brand_fact_pages', rows.factPages),+ write('brand_facts', rows.facts),+ ]);+ // The signals point at their audit.+ await write('audit_signal_results', rows.signals, { chunk: SIGNAL_CHUNK });+ return {+ audits: rows.audits.length,+ signals: rows.signals.length,+ pages: rows.sitePages.length,+ facts: rows.facts.length,+ };+ },+};diff --git a/server/src/lib/demo/site.test.js b/server/src/lib/demo/site.test.jsnew file mode 100644index 000000000..6cca8a25b--- /dev/null+++ b/server/src/lib/demo/site.test.js@@ -0,0 +1,216 @@+import { describe, expect, it, vi } from 'vitest';++vi.mock('../../config/supabase.js', () => ({ default: {} }));++const { AUDIT_ROUNDS, site, siteRows } = await import('./site.js');+const { AUDITED_PAGES } = await import('./site-world.js');+const { TOTAL_SIGNALS, signalsByKey } = await import('../audit/rubric.js');+const { buildAuditPages } = await import('../audit/history.js');+const { buildOverview, primaryPages } = await import('../audit/overview.js');++/**+ * The demo brand's Site Audit (demo-site): Brewline's pages scored by the+ * audit engine itself over three rounds, its page inventory, site trust and+ * brand facts.+ */++const BRAND_ID = '11111111-2222-4333-8444-555555555555';+const NOW = new Date('2026-10-28T12:00:00Z');+const DAY = 86_400_000;+const rows = await siteRows({ brandId: BRAND_ID, now: NOW });++const path = (url) => new URL(url).pathname;+const latest = rows.audits.filter((a) => a.created_at.startsWith('2026-10-26'));++describe('siteRows: audits', () => {+ it('audits six key pages three times and every page in the latest round', () => {+ const perRound = AUDIT_ROUNDS.map(+ (_, round) => AUDITED_PAGES.filter((p) => p.rounds.includes(round)).length,+ );+ expect(perRound).toEqual([6, 6, AUDITED_PAGES.length]);+ expect(rows.audits).toHaveLength(6 + 6 + AUDITED_PAGES.length);+ expect(latest).toHaveLength(AUDITED_PAGES.length);+ expect(rows.audits.map((a) => a.source)).toContain('user');+ for (const audit of rows.audits) {+ expect(audit).toMatchObject({ brand_id: BRAND_ID, status: 'completed', error: null });+ expect(audit.url).toMatch(/^https:\/\/brewline\.example(\/|$)/);+ expect(audit.signals_total).toBe(TOTAL_SIGNALS);+ expect(Date.parse(audit.completed_at)).toBeLessThan(NOW.getTime());+ expect(NOW.getTime() - Date.parse(audit.created_at)).toBeLessThan(25 * DAY);+ }+ });++ it('stores every rubric signal of an audit, scored as the scorer reads it', () => {+ for (const audit of rows.audits) {+ const signals = rows.signals.filter((s) => s.audit_id === audit.id);+ expect(signals.map((s) => s.signal_key).sort()).toEqual(Object.keys(signalsByKey).sort());+ expect(audit.signals_evaluated).toBe(signals.filter((s) => s.status !== 'na').length);+ for (const s of signals) {+ expect(['pass', 'warn', 'fail', 'na']).toContain(s.status);+ expect(s.status === 'na' ? s.score : typeof s.score).toBe(+ s.status === 'na' ? null : 'number',+ );+ expect(s.category).toBe(signalsByKey[s.signal_key].category);+ }+ }+ // The model-judged signals say why, and Wikidata knows no fictional brand.+ const direct = rows.signals.find((s) => s.signal_key === 'direct-answer');+ expect(direct.evidence.reason.length).toBeGreaterThan(10);+ const entity = rows.signals.filter((s) => s.signal_key === 'brand-entity');+ expect(entity.every((s) => s.status === 'fail')).toBe(true);+ });++ it('shows the key pages improving, a few good pages and some that need work', () => {+ const pages = buildAuditPages(+ [...rows.audits].sort((a, b) => b.created_at.localeCompare(a.created_at)),+ NOW.getTime(),+ );+ const status = (p) => pages.find((x) => x.path === p).status;+ for (const p of AUDITED_PAGES.filter((x) => x.rounds.length === 3)) {+ const page = pages.find((x) => x.path === p.path);+ expect(page.trend).toHaveLength(3);+ expect(page.trend.at(-1)).toBeGreaterThan(page.trend[0]);+ }+ const statuses = pages.map((p) => p.status);+ expect(statuses).toContain('good');+ expect(statuses).toContain('improving');+ expect(statuses).toContain('needs_work');+ expect(status('/blog/how-fresh-should-coffee-beans-be')).toBe('good');++ const { own } = primaryPages(pages, 'brewline.example');+ const overview = buildOverview(+ own,+ new Map(rows.audits.map((a) => [a.id, a.category_scores])),+ [],+ );+ expect(overview.pages).toBe(AUDITED_PAGES.length);+ expect(overview.siteScore).toBeGreaterThan(0.5);+ expect(overview.siteScore).toBeLessThan(0.75);+ // Against each page's previous audit: up a few points.+ expect(overview.siteScoreChange).toBeGreaterThan(0);+ });++ it('recommends fixes in the stored shape, fitting the page, with no fact made up', () => {+ const keys = ['signalKey', 'label', 'category', 'priority', 'recommendation', 'draft', 'code'];+ for (const audit of rows.audits) {+ expect(audit.recommendations.length).toBeGreaterThan(0);+ expect(audit.recommendations.length).toBeLessThanOrEqual(5);+ for (const r of audit.recommendations) {+ expect(Object.keys(r).sort()).toEqual([...keys].sort());+ expect(['high', 'medium', 'low']).toContain(r.priority);+ expect(r.recommendation.length).toBeLessThanOrEqual(150);+ // No address outside the demo's fictional sites.+ expect(`${r.draft ?? ''} ${r.code ?? ''}`).not.toMatch(+ /https?:\/\/(?!schema\.org)(?![\w.-]*\.example\b)/,+ );+ }+ }+ const product = latest.find((a) => path(a.url) === '/coffee/colombia-huila');+ const keysOf = (a) => a.recommendations.map((r) => r.signalKey);+ expect(keysOf(product)).not.toContain('author-byline');+ expect(keysOf(product)).toContain('json-ld-relevance');+ });++ // Audits 30 pages again: allowed more than the default 5 s on a busy machine.+ it('is the same every time but for its dates, and its ids are its brand’s own', async () => {+ const other = await siteRows({+ brandId: '99999999-2222-4333-8444-555555555555',+ now: new Date(NOW.getTime() + 2 * DAY),+ });+ expect(other.audits[0].id).not.toBe(rows.audits[0].id);+ const strip = ({ id: _i, brand_id: _b, created_at: _c, completed_at: _d, ...a }) => a;+ expect(other.audits.map(strip)).toEqual(rows.audits.map(strip));+ }, 30_000);+});++describe('siteRows: pages, trust and facts', () => {+ it("lists the site's pages as the page inventory keeps them, cited and visited first", () => {+ const urls = rows.sitePages.map((p) => p.url);+ expect(new Set(urls).size).toBe(urls.length);+ for (const p of AUDITED_PAGES)+ expect(urls).toContain(`https://brewline.example${p.path === '/' ? '' : p.path}`);+ expect(urls.some((u) => /privacy|terms|cart/.test(u))).toBe(false);+ expect(rows.sitePages[0].sources).toContain('citation');+ const subscriptions = rows.sitePages.find((p) => path(p.url) === '/subscriptions');+ expect(subscriptions.ai_citations).toBeGreaterThan(0);+ expect(subscriptions.ga_sessions).toBeGreaterThan(0);+ expect(subscriptions.title).toBe('Coffee Subscriptions | Brewline Coffee');+ for (const page of rows.sitePages) {+ expect(page).toMatchObject({ brand_id: BRAND_ID, fetch_status: 200, fetched_via: 'direct' });+ expect(page.title).toBeTruthy();+ }+ });++ it('reads site trust 4 of 5: no editorial policy page', () => {+ expect(rows.siteTrust.score).toBe(4);+ expect(rows.siteTrust.checks.editorial.ok).toBe(false);+ expect(rows.siteTrust.checks.organizationSchema.ok).toBe(true);+ expect(rows.siteTrust.checks.authorPages.authors.map((a) => a.url)).toEqual([+ 'https://brewline.example/authors/maya-okafor',+ 'https://brewline.example/authors/daniel-reyes',+ ]);+ });++ it('keeps the brand facts the pages say, mostly approved, a few to review', () => {+ expect(rows.factPages.map((p) => p.kind).sort()).toEqual(['about', 'pricing', 'product']);+ for (const page of rows.factPages) expect(page.text_hash).toMatch(/^[0-9a-f]{16}$/);+ const prices = rows.facts.filter((f) => f.kind === 'price');+ expect(prices.map((f) => [f.value.plan, f.value.amount])).toEqual([+ ['House Blend Subscription', 18],+ ['Single Origin Club', 22],+ ['Espresso Club', 20],+ ['12 oz', 16.5],+ ['2 lb', 38],+ ]);+ expect(rows.facts.find((f) => f.kind === 'founded').value).toEqual({ year: 2016 });+ const statuses = rows.facts.map((f) => f.status);+ expect(statuses.filter((s) => s === 'pending').length).toBeGreaterThan(0);+ expect(statuses.filter((s) => s === 'approved').length).toBeGreaterThan(5);+ });++ it('keeps one exclude pattern and a few pinned or excluded pages', () => {+ expect(rows.settings.exclude_patterns).toEqual(['/brewing/*']);+ expect(rows.prefs.map((p) => [p.page_key, p.state])).toEqual([+ ['brewline.example/coffee/espresso-roast', 'pinned'],+ ['brewline.example/subscriptions/espresso-club', 'pinned'],+ ['brewline.example/shipping', 'excluded'],+ ]);+ });+});++describe('site.build', () => {+ it('writes only its own tables (and the audits’ signals), the signals after their audits', async () => {+ const writes = [];+ const keys = {};+ const db = {+ from: (table) => ({+ upsert: async (list, { onConflict }) => {+ writes.push([table, list.length]);+ keys[table] = onConflict;+ return { error: null };+ },+ }),+ };+ const built = await site.build({ brandId: BRAND_ID, now: NOW, db });+ const tables = [...new Set(writes.map(([t]) => t))];+ expect(tables).toEqual([+ 'site_pages',+ 'site_audits',+ 'site_trust',+ 'site_audit_brand_settings',+ 'site_audit_page_prefs',+ 'brand_fact_pages',+ 'brand_facts',+ 'audit_signal_results',+ ]);+ expect(keys).toMatchObject({+ site_pages: 'brand_id,url',+ site_audits: 'id',+ audit_signal_results: 'audit_id,signal_key',+ site_trust: 'brand_id',+ brand_fact_pages: 'brand_id,kind',+ });+ expect(tables.filter((t) => !site.tables.includes(t))).toEqual(['audit_signal_results']);+ expect(built.signals).toBe(built.audits * TOTAL_SIGNALS);+ });+});diff --git a/server/src/lib/demo/traffic.js b/server/src/lib/demo/traffic.jsnew file mode 100644index 000000000..065373cca--- /dev/null+++ b/server/src/lib/demo/traffic.js@@ -0,0 +1,844 @@+/**+ * The demo brand's AI Referral Traffic (demo-site): its tracking snippet's+ * visits and journeys, its AI crawler hits, and what Google Analytics and+ * Search Console report for it, over 90 days.+ *+ * Each goes through the code that stores it live wherever that is pure:+ * Analytics' rows are GA4 reports in GA4's own shape, mapped by ga-sync.js;+ * Search Console's are its API rows, mapped by gsc-sync.js; a journey event+ * is the snippet's body, made a row by traffic-events.js's eventRow; a+ * visit's source and surface come from ai-sources.js. The page+ * opportunities are page-opportunities.js's own findings over these rows.+ *+ * The story it tells: AI visits grow over the quarter; Analytics has the+ * whole quarter, the snippet the last four weeks (a day with a snippet visit+ * is the snippet's, any other Analytics'); two of the shop's best-selling+ * pages get no AI visit at all; a guide AI answers still link to is gone.+ */++import { createHash } from 'node:crypto';+import supabaseAdmin from '../../config/supabase.js';+import { AI_SOURCES_VERSION, classifyAiSurface, landingMedium } from '../ai-sources.js';+import {+ mapAiTrafficRows,+ mapChannelRows,+ mapItemRows,+ mapPageRows as mapGaPageRows,+} from '../ga-sync.js';+import { mapAnalyticsRows, mapPageRows as mapGscPageRows } from '../gsc-sync.js';+import { detectPageOpportunities } from '../page-opportunities.js';+import { eventRow } from '../traffic-events.js';+import { BRAND } from './brewline.js';+import { demoId, seededRandom } from './random.js';+import { DAY_MS, brandCitations, dayBefore, dayStart } from './site-common.js';+import { allSettledOrThrow, demoWriter } from './write.js';+import { BROKEN_PAGE, CATALOG, SEARCH_QUERIES, SITE_URL } from './site-world.js';++/** Days of Google Analytics history, and of Search Console page history. */+export const GA_DAYS = 90;+/** The snippet went on the site this many days ago. */+export const SNIPPET_DAYS = 28;+/** Conversions (CE-017) were turned on this many days ago. */+export const CONVERSION_DAYS = 20;+/** Days of Search Console query history (its sync's window) and of AI crawler counts. */+export const QUERY_DAYS = 28;+export const BOT_DAYS = 30;+/** Raw AI crawler hits kept: the last few days of the daily counts. */+const BOT_EVENT_DAYS = 2;+/** Search Console reports a day this late. */+const GSC_DELAY_DAYS = 2;++/** The demo's GA4 property (the web's DEMO_ANALYTICS_PROPERTY_ID) and Search Console property. */+export const GA_PROPERTY_ID = '100000000';+export const GSC_PROPERTY = `sc-domain:${BRAND.domain}`;++/** The AI assistants visitors come from: their source key (ai-sources.json), share and referrer. */+const AI_PLATFORMS = [+ { key: 'chatgpt.com', share: 0.5, referrer: 'https://chatgpt.com/' },+ { key: 'perplexity.ai', share: 0.19, referrer: 'https://www.perplexity.ai/' },+ { key: 'gemini.google.com', share: 0.12, referrer: 'https://gemini.google.com/' },+ { key: 'copilot.microsoft.com', share: 0.11, referrer: 'https://copilot.microsoft.com/' },+ { key: 'claude.ai', share: 0.08, referrer: 'https://claude.ai/' },+];++/** Where AI visitors land, by weight: the pages the demo's answers cite and link. */+const AI_PAGES = [+ ['/coffee/espresso-roast', 16],+ ['/blog/how-fresh-should-coffee-beans-be', 15],+ ['/subscriptions', 14],+ ['/', 10],+ ['/subscriptions/house-blend', 9],+ ['/coffee/decaf', 8],+ ['/gifts', 6],+ ['/blog/single-origin-vs-blend', 6],+ ['/blog/espresso-at-home', 5],+ ['/coffee/ethiopia-yirgacheffe', 4],+ ['/blog/low-acid-coffee-guide', 3],+ ['/collections/single-origin', 2],+];++/**+ * Every landing page Analytics reports, with its daily sessions from all+ * channels and its revenue a day: Colombia Huila and the Espresso Club sell+ * well and get no AI visit (the page opportunities).+ */+const GA_PAGES = [+ ['/', 120, 60],+ ['/subscriptions', 62, 400],+ ['/coffee/colombia-huila', 34, 300],+ ['/subscriptions/espresso-club', 22, 280],+ ['/coffee/espresso-roast', 46, 250],+ ['/subscriptions/house-blend', 26, 150],+ ['/coffee/house-blend', 24, 120],+ ['/coffee/decaf', 21, 90],+ ['/gifts', 16, 80],+ ['/coffee/ethiopia-yirgacheffe', 14, 55],+ ['/collections/single-origin', 27, 40],+ ['/blog/how-fresh-should-coffee-beans-be', 41, 12],+ ['/blog/single-origin-vs-blend', 17, 6],+ ['/blog/espresso-at-home', 12, 5],+ ['/blog/low-acid-coffee-guide', 9, 2],+];++/** Sessions a day by Analytics' default channel group, AI Assistant aside (it is the AI sessions). */+const CHANNELS = [+ ['Organic Search', 0.46],+ ['Direct', 0.24],+ ['Email', 0.1],+ ['Paid Search', 0.1],+ ['Organic Social', 0.1],+];++/** The products Analytics reports item views and sales for: the best sellers' first variant. */+const GA_ITEMS = ['BL-ESP', 'BL-SUB-HOU', 'BL-COL', 'BL-SUB-ESP', 'BL-DEC', 'BL-ETH'];++/** Pages Search Console reports clicks for; the low-acid guide loses clicks while impressions rise. */+const GSC_PAGES = [+ ['/', 210, 260, 2.1],+ ['/subscriptions', 160, 230, 9.4],+ ['/blog/how-fresh-should-coffee-beans-be', 300, 420, 5.8],+ ['/coffee/espresso-roast', 120, 170, 10.2],+ ['/coffee/decaf', 110, 150, 7.9],+ ['/blog/single-origin-vs-blend', 70, 95, 5.6],+ ['/coffee/colombia-huila', 50, 65, 8.7],+ ['/blog/low-acid-coffee-guide', 150, 260, 6.1, 'losing'],+];++/** AI crawlers (ai-bots.json), how many hits a day each makes, and the pages they read. */+const BOTS = [+ ['gptbot', 'training', 9],+ ['oai-searchbot', 'search', 6],+ ['chatgpt-user', 'user', 7],+ ['claudebot', 'training', 5],+ ['perplexitybot', 'search', 4],+ ['perplexity-user', 'user', 3],+];+const BOT_PAGES = [+ ['/', 5],+ ['/subscriptions', 4],+ ['/coffee/espresso-roast', 4],+ ['/blog/how-fresh-should-coffee-beans-be', 4],+ ['/coffee/decaf', 2],+ ['/blog/single-origin-vs-blend', 2],+ ['/collections/single-origin', 2],+ ['/robots.txt', 2],+ ['/sitemap.xml', 1],+ [BROKEN_PAGE, 1],+];++const USER_AGENTS = [+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15',+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1',+ 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36',+];+const SCREENS = ['1440x900', '1920x1080', '390x844', '412x915'];+const COUNTRIES = [+ ['US', 0.78],+ ['CA', 0.09],+ ['GB', 0.08],+ ['AU', 0.05],+];++const SEED = 'brewline-traffic';++/** A page's address as a browser has it (the home page with its slash). */+const href = (path) => `${SITE_URL}${path}`;++/** One of a weighted list's items ([item, weight, ...]). */+function weighted(rng, list, weightAt = 1) {+ const total = list.reduce((sum, item) => sum + item[weightAt], 0);+ let roll = rng.next() * total;+ for (const item of list) {+ roll -= item[weightAt];+ if (roll < 0) return item;+ }+ return list.at(-1);+}++const platformOf = (rng) => {+ let roll = rng.next();+ for (const platform of AI_PLATFORMS) {+ roll -= platform.share;+ if (roll < 0) return platform;+ }+ return AI_PLATFORMS[0];+};++/** About `mean`, give or take `spread` of it. */+const around = (rng, mean, spread = 0.2) => Math.max(0, mean * (1 + (rng.next() * 2 - 1) * spread));++const isWeekend = (day) => [0, 6].includes(new Date(dayStart(day)).getUTCDay());++/** A page's share of its visitors that buy, and what an order is worth. */+function buyerRate(path) {+ if (path.startsWith('/subscriptions')) return 0.08;+ if (path.startsWith('/coffee/') || path === '/gifts') return 0.06;+ if (path.startsWith('/blog/')) return 0.012;+ return 0.03;+}++/**+ * Analytics' AI sessions, one at a time over the quarter: the day, the+ * assistant, the landing page, and what the visitor did. Shared by the GA+ * rows and the snippet, which counts a little more of the same visits.+ */+function aiDays(now) {+ const days = [];+ for (let back = GA_DAYS; back >= 1; back--) {+ const day = dayBefore(now, back);+ const rng = seededRandom(`${SEED}:ai:${back}`);+ const growth = (GA_DAYS - back) / (GA_DAYS - 1);+ const mean = (11 + 13 * growth) * (isWeekend(day) ? 0.75 : 1);+ days.push({ day, back, sessions: Math.round(around(rng, mean)) });+ }+ return days;+}++/** A GA4 report in the Data API's shape, from rows of { dimension: value, metric: value }. */+function gaReport(dimensions, metrics, rows) {+ return {+ dimensionHeaders: dimensions.map((name) => ({ name })),+ metricHeaders: metrics.map((name) => ({ name, type: 'TYPE_INTEGER' })),+ rows: rows.map((row) => ({+ dimensionValues: dimensions.map((name) => ({ value: String(row[name]) })),+ metricValues: metrics.map((name) => ({ value: String(row[name] ?? 0) })),+ })),+ };+}++const TRAFFIC_METRICS = [+ 'sessions',+ 'engagedSessions',+ 'keyEvents',+ 'sessionKeyEventRate',+ 'totalUsers',+ 'transactions',+ 'purchaseRevenue',+ 'userEngagementDuration',+];++/** A group of sessions' GA4 metrics: engagement, key events (orders and signups), revenue. */+function sessionMetrics(rng, path, sessions) {+ let orders = 0;+ let signups = 0;+ let revenue = 0;+ let engaged = 0;+ let seconds = 0;+ for (let i = 0; i < sessions; i++) {+ if (rng.chance(path.startsWith('/blog/') ? 0.74 : 0.64)) engaged++;+ seconds += Math.round(around(rng, path.startsWith('/blog/') ? 150 : 80, 0.5));+ if (rng.chance(buyerRate(path))) {+ orders++;+ revenue += rng.pick([16.5, 17.5, 18, 22, 34, 38, 54]);+ } else if (rng.chance(0.02)) signups++;+ }+ const keyEventSessions = orders + signups;+ return {+ sessions,+ engagedSessions: engaged,+ keyEvents: orders + signups,+ sessionKeyEventRate: sessions ? keyEventSessions / sessions : 0,+ totalUsers: Math.max(1, sessions - Math.floor(sessions / 9)),+ transactions: orders,+ purchaseRevenue: Math.round(revenue * 100) / 100,+ userEngagementDuration: seconds,+ };+}++/** The GA4 rows of the quarter: AI sessions by source and page, pages, channels and items. */+function analyticsRows(brandId, now) {+ const aiRows = [];+ const aiByDayPage = new Map();+ for (const { day, back, sessions } of aiDays(now)) {+ const rng = seededRandom(`${SEED}:ga-ai:${back}`);+ const groups = new Map();+ for (let i = 0; i < sessions; i++) {+ const platform = platformOf(rng);+ const [path] = weighted(rng, AI_PAGES);+ // ChatGPT Shopping: a product card's link carries utm_medium=feed (#1283).+ const shopping =+ platform.key === 'chatgpt.com' && path.startsWith('/coffee/') && rng.chance(0.3);+ const key = `${platform.key}|${path}|${shopping}`;+ groups.set(key, { platform, path, shopping, n: (groups.get(key)?.n ?? 0) + 1 });+ }+ for (const { platform, path, shopping, n } of groups.values()) {+ const query =+ platform.key === 'chatgpt.com'+ ? `${path}?utm_source=chatgpt.com${shopping ? '&utm_medium=feed' : ''}`+ : path;+ aiRows.push({+ date: day.replace(/-/g, ''),+ sessionSource: platform.key,+ sessionMedium: shopping ? 'feed' : 'referral',+ sessionDefaultChannelGroup: 'AI Assistant',+ landingPage: path,+ landingPagePlusQueryString: query,+ ...sessionMetrics(rng, path, n),+ });+ aiByDayPage.set(`${day}|${path}`, (aiByDayPage.get(`${day}|${path}`) ?? 0) + n);+ }+ }++ const pageRows = [];+ const channelRows = [];+ for (let back = GA_DAYS; back >= 1; back--) {+ const day = dayBefore(now, back);+ const rng = seededRandom(`${SEED}:ga-pages:${back}`);+ const weekend = isWeekend(day) ? 0.8 : 1;+ let other = 0;+ let ai = 0;+ for (const [path, daily, revenue] of GA_PAGES) {+ const aiSessions = aiByDayPage.get(`${day}|${path}`) ?? 0;+ const sessions = Math.round(around(rng, daily * weekend)) + aiSessions;+ const metrics = sessionMetrics(rng, path, sessions);+ // The page's own revenue level, so the best sellers rank as they should.+ metrics.purchaseRevenue = Math.round(around(rng, revenue * weekend, 0.3) * 100) / 100;+ metrics.transactions = Math.max(+ metrics.transactions,+ Math.round(metrics.purchaseRevenue / 24),+ );+ pageRows.push({ date: day.replace(/-/g, ''), landingPage: path, ...metrics });+ other += sessions - aiSessions;+ ai += aiSessions;+ }+ for (const [channel, share] of CHANNELS) {+ const sessions = Math.round(other * 1.3 * share);+ channelRows.push({+ date: day.replace(/-/g, ''),+ sessionDefaultChannelGroup: channel,+ ...sessionMetrics(rng, '/', sessions),+ });+ }+ channelRows.push({+ date: day.replace(/-/g, ''),+ sessionDefaultChannelGroup: 'AI Assistant',+ ...sessionMetrics(rng, '/coffee/', ai),+ });+ }++ const itemRows = [];+ for (let back = QUERY_DAYS; back >= 1; back--) {+ const rng = seededRandom(`${SEED}:ga-items:${back}`);+ for (const id of GA_ITEMS) {+ const product = CATALOG.find((p) => p.id === id);+ const [label, price] = (product.sizes ?? product.bags)[0];+ const viewed = Math.round(around(rng, 30));+ const added = Math.round(viewed * around(rng, 0.18, 0.3));+ const purchased = Math.round(added * around(rng, 0.45, 0.3));+ itemRows.push({+ date: dayBefore(now, back).replace(/-/g, ''),+ itemId: `${id}-${label.replace(/\s+/g, '').toUpperCase()}`,+ itemName: `${product.title} - ${label}`,+ itemCategory: product.type,+ itemsViewed: viewed,+ itemsAddedToCart: added,+ itemsPurchased: purchased,+ itemRevenue: Math.round(purchased * price * 100) / 100,+ });+ }+ }++ // Stored as each day's sync wrote it: the morning after.+ const synced = (row) => ({+ ...row,+ created_at: new Date(Math.min(dayStart(row.date) + DAY_MS * 1.27, now.getTime())).toISOString(),+ });+ return {+ ai: mapAiTrafficRows(+ brandId,+ gaReport(+ [+ 'date',+ 'sessionSource',+ 'sessionMedium',+ 'sessionDefaultChannelGroup',+ 'landingPage',+ 'landingPagePlusQueryString',+ ],+ TRAFFIC_METRICS,+ aiRows,+ ),+ ).map(synced),+ pages: mapGaPageRows(brandId, gaReport(['date', 'landingPage'], TRAFFIC_METRICS, pageRows)).map(+ synced,+ ),+ channels: mapChannelRows(+ brandId,+ gaReport(['date', 'sessionDefaultChannelGroup'], TRAFFIC_METRICS, channelRows),+ ).map(synced),+ items: mapItemRows(+ brandId,+ gaReport(+ ['date', 'itemId', 'itemName', 'itemCategory'],+ ['itemsViewed', 'itemsAddedToCart', 'itemsPurchased', 'itemRevenue'],+ itemRows,+ ),+ ).map(synced),+ };+}++/** Sessions per landing page over the last 30 days, as the page inventory reads Analytics. */+export function gaSessionsByPath(now) {+ const date = now instanceof Date ? now : new Date(now);+ const since = dayBefore(date, 30);+ const sessions = new Map();+ for (const row of analyticsRows('', date).pages) {+ if (row.date < since) continue;+ sessions.set(row.landing_page, (sessions.get(row.landing_page) ?? 0) + row.sessions);+ }+ return sessions;+}++/** Click-through rate at a Google position. */+const ctrAt = (position) => Math.min(0.32, 0.32 / position ** 1.15);++/** Search Console's query and page rows, as its API returns them ({ keys, clicks, … }). */+function searchRows(brandId, now) {+ const queries = [];+ for (let back = QUERY_DAYS + GSC_DELAY_DAYS - 1; back >= GSC_DELAY_DAYS; back--) {+ const rng = seededRandom(`${SEED}:gsc-q:${back}`);+ const progress = (QUERY_DAYS + GSC_DELAY_DAYS - 1 - back) / (QUERY_DAYS - 1);+ for (const [query, from, to, position] of SEARCH_QUERIES) {+ const impressions = Math.round(around(rng, from + (to - from) * progress, 0.15));+ const pos = Math.max(1, position + (rng.next() * 2 - 1) * 0.8);+ const clicks = Math.round(impressions * ctrAt(pos) * around(rng, 1, 0.2));+ queries.push({+ keys: [query, dayBefore(now, back)],+ clicks,+ impressions,+ ctr: impressions ? clicks / impressions : 0,+ position: Math.round(pos * 10) / 10,+ });+ }+ }+ const pages = [];+ for (let back = GA_DAYS + GSC_DELAY_DAYS - 1; back >= GSC_DELAY_DAYS; back--) {+ const rng = seededRandom(`${SEED}:gsc-p:${back}`);+ const progress = (GA_DAYS + GSC_DELAY_DAYS - 1 - back) / (GA_DAYS - 1);+ for (const [path, from, to, position, trend] of GSC_PAGES) {+ const impressions = Math.round(around(rng, from + (to - from) * progress, 0.15));+ const pos = Math.max(1, position + (rng.next() * 2 - 1) * 0.6);+ // An AI Overview answers it on the results page: fewer clicks as it shows more.+ const ctr = ctrAt(pos) * (trend === 'losing' ? 1 - 0.6 * progress : 1);+ const clicks = Math.round(impressions * ctr * around(rng, 1, 0.2));+ pages.push({+ keys: [href(path), dayBefore(now, back)],+ clicks,+ impressions,+ ctr: impressions ? clicks / impressions : 0,+ position: Math.round(pos * 10) / 10,+ });+ }+ }+ const synced = (row) => ({+ ...row,+ created_at: new Date(+ Math.min(dayStart(row.date) + DAY_MS * (GSC_DELAY_DAYS + 0.27), now.getTime()),+ ).toISOString(),+ });+ return {+ queries: mapAnalyticsRows(brandId, queries).map(synced),+ pages: mapGscPageRows(brandId, pages).map(synced),+ };+}++const hex = (text) => createHash('sha256').update(text).digest('hex');+/** The snippet's own visitor id (a random 22-character id, CE-017). */+const visitorIdOf = (n) =>+ createHash('sha256').update(`${SEED}:journey:${n}`).digest('base64url').slice(0, 22);++/**+ * The snippet's visits (ai_traffic_logs) over its four weeks, and the+ * journeys (ai_traffic_events) of the visitors since conversions went on.+ */+function snippetRows(brandId, now) {+ const nowMs = now.getTime();+ const days = new Map(aiDays(now).map((d) => [d.back, d.sessions]));+ const visits = [];+ const pageviews = [];+ const events = [];+ let journey = 0;+ for (let back = SNIPPET_DAYS; back >= 0; back--) {+ const rng = seededRandom(`${SEED}:snippet:${back}`);+ const day = dayBefore(now, back);+ // The snippet counts a little more than Analytics (blocked cookies, no consent).+ const gaSessions = days.get(back) ?? days.get(1);+ let count = Math.round(around(rng, gaSessions * 1.15, 0.1));+ if (back === 0) count = Math.floor((count * (nowMs - dayStart(day))) / DAY_MS);+ for (let i = 0; i < count; i++) {+ // US daytime, mostly (13:00 to 04:00 UTC); today's up to a few minutes ago.+ const at =+ back === 0+ ? dayStart(day) + Math.floor(rng.next() * (nowMs - 5 * 60 * 1000 - dayStart(day)))+ : dayStart(day) + Math.floor(((13 + rng.next() * 15) % 24) * 60 * 60 * 1000);+ const platform = platformOf(rng);+ const broken = rng.chance(0.025);+ const [path] = broken ? [BROKEN_PAGE] : weighted(rng, AI_PAGES);+ const shopping =+ platform.key === 'chatgpt.com' && path.startsWith('/coffee/') && rng.chance(0.3);+ const url =+ platform.key === 'chatgpt.com'+ ? `${href(path)}?utm_source=chatgpt.com${shopping ? '&utm_medium=feed' : ''}`+ : href(path);+ const visitor = {+ visitor_key: hex(`${SEED}:visitor:${back}:${i}`),+ user_agent: rng.pick(USER_AGENTS),+ country: weighted(rng, COUNTRIES)[0],+ language: rng.chance(0.9) ? 'en-US' : 'en-GB',+ screen: rng.pick(SCREENS),+ };+ const id = demoId(brandId, `visit:${back}:${i}`);+ const row = (pageId, pageUrlValue, when, status) => ({+ id: pageId,+ brand_id: brandId,+ url: pageUrlValue,+ referrer: platform.referrer,+ source_platform: platform.key,+ source_surface: classifyAiSurface(platform.key, landingMedium(pageUrlValue)),+ ...visitor,+ visit_of: pageId === id ? null : id,+ source_list_version: AI_SOURCES_VERSION,+ http_status: status,+ not_found_title: false,+ created_at: new Date(when).toISOString(),+ });+ visits.push(row(id, url, at, broken ? 404 : 200));+ // A reload or a second link from the same answer continues the visit (AT-003).+ const more = rng.chance(0.25) ? rng.int(1, 2) : 0;+ for (let j = 1; j <= more && at + j * 4 * 60_000 < nowMs; j++) {+ const [next] = weighted(rng, AI_PAGES);+ pageviews.push(+ row(demoId(brandId, `visit:${back}:${i}:${j}`), href(next), at + j * 4 * 60_000, 200),+ );+ }++ // Journeys: the visitor's next pages and what they bought or signed up for.+ if (back > CONVERSION_DAYS || broken || !rng.chance(0.18)) continue;+ journey += 1;+ const visitorId = visitorIdOf(journey);+ const first = { s: platform.key, u: url, at };+ const steps = [+ path,+ ...rng+ .shuffle(['/subscriptions', '/coffee/espresso-roast', '/cart', '/gifts'])+ .slice(0, rng.int(0, 2)),+ ];+ steps.forEach((step, k) => {+ const when = Math.min(at + k * 90_000, nowMs - 60_000);+ const event = eventRow(brandId, { k: 'pv', id: visitorId, u: href(step), f: first }, when);+ if (event) {+ events.push({+ id: demoId(brandId, `journey:${journey}:${k}`),+ ...event,+ occurred_at: new Date(when).toISOString(),+ });+ }+ });+ const buys = rng.chance(buyerRate(path) * 4);+ const signs = !buys && rng.chance(0.08);+ if (!buys && !signs) continue;+ const when = Math.min(at + steps.length * 90_000 + 60_000, nowMs - 30_000);+ const body = buys+ ? {+ k: 'cv',+ id: visitorId,+ u: `${SITE_URL}/checkout/thank-you`,+ f: first,+ g: 'order',+ o: 'call',+ val: rng.pick([16.5, 18, 22, 34, 38, 54]),+ cur: 'USD',+ q: rng.int(1, 2),+ ref: `BL-${10_200 + journey}`,+ }+ : {+ k: 'cv',+ id: visitorId,+ u: href(path),+ f: first,+ g: 'newsletter',+ o: 'form',+ n: 'newsletter-footer',+ };+ const conversion = eventRow(brandId, body, when);+ if (conversion) {+ events.push({+ id: demoId(brandId, `journey:${journey}:cv`),+ ...conversion,+ occurred_at: new Date(when).toISOString(),+ });+ }+ }+ }+ return { visits, pageviews, events };+}++/** The AI crawlers' daily counts, their last days' raw hits, and the Worker sending them. */+function botRows(brandId, now) {+ const nowMs = now.getTime();+ const daily = [];+ const events = [];+ let lastHit = 0;+ for (let back = BOT_DAYS - 1; back >= 0; back--) {+ const day = dayBefore(now, back);+ const rng = seededRandom(`${SEED}:bots:${back}`);+ const end = back === 0 ? nowMs - 10 * 60 * 1000 : dayStart(day) + DAY_MS - 1;+ for (const [bot, type, mean] of BOTS) {+ const hits = Math.round(around(rng, mean, 0.5) * ((end - dayStart(day)) / DAY_MS));+ const pages = new Map();+ const read = rng.shuffle(BOT_PAGES).slice(0, 3);+ for (let h = 0; h < hits; h++) {+ const [path] = weighted(rng, read);+ const at = dayStart(day) + Math.floor(rng.next() * (end - dayStart(day)));+ const page = path.toLowerCase();+ const seen = pages.get(page) ?? { hits: 0, last: 0 };+ pages.set(page, { hits: seen.hits + 1, last: Math.max(seen.last, at) });+ lastHit = Math.max(lastHit, at);+ if (back < BOT_EVENT_DAYS) {+ events.push({+ brand_id: brandId,+ bot,+ bot_type: type,+ page,+ status: path === BROKEN_PAGE ? 404 : 200,+ country: 'US',+ occurred_at: new Date(at).toISOString(),+ received_at: new Date(at + 20_000).toISOString(),+ });+ }+ }+ for (const [page, { hits: count, last }] of pages) {+ daily.push({+ brand_id: brandId,+ day,+ bot,+ bot_type: type,+ page,+ hits: count,+ last_seen: new Date(last).toISOString(),+ });+ }+ }+ }+ events.sort((a, b) => a.occurred_at.localeCompare(b.occurred_at));+ const connection = {+ id: demoId(brandId, 'bot-connection'),+ brand_id: brandId,+ kind: 'cloudflare_worker',+ // Not a hash of any secret (bot-ingest.js keeps sha256 hex): no sender+ // can ever send for the demo brand.+ secret_hash: `demo:${demoId(brandId, 'bot-connection')}`,+ created_by: null,+ created_at: new Date(nowMs - (BOT_DAYS + 1) * DAY_MS).toISOString(),+ last_event_at: lastHit ? new Date(lastHit + 20_000).toISOString() : null,+ revoked_at: null,+ };+ return { daily, events, connection };+}++/** Page opportunities (#719): the nightly findings over the last 28 days, by its own rules. */+function opportunityRows(brandId, now, { analytics, snippet, citations }) {+ const since = dayBefore(now, 28);+ const pages = new Map();+ const page = (path) => {+ if (!pages.has(path)) {+ pages.set(path, {+ landingPage: path,+ sessions: 0,+ engagedSessions: 0,+ keyEvents: 0,+ transactions: 0,+ revenue: 0,+ engagementSeconds: 0,+ aiSessions: 0,+ aiPlatforms: new Set(),+ citations: 0,+ citingPrompts: new Set(),+ targetingPrompts: 0,+ snippetVisits: 0,+ });+ }+ return pages.get(path);+ };+ for (const row of analytics.pages) {+ if (row.date < since) continue;+ const p = page(row.landing_page);+ p.sessions += row.sessions;+ p.engagedSessions += row.engaged_sessions;+ p.keyEvents += row.key_events;+ p.transactions += row.transactions;+ p.revenue += row.purchase_revenue;+ p.engagementSeconds += row.engagement_duration_seconds;+ }+ const known = (path) => pages.get(path);+ for (const row of analytics.ai) {+ if (row.date < since || !known(row.landing_page)) continue;+ known(row.landing_page).aiSessions += row.sessions;+ known(row.landing_page).aiPlatforms.add(row.platform);+ }+ for (const visit of snippet.visits) {+ if (visit.created_at.slice(0, 10) < since) continue;+ const path = new URL(visit.url).pathname;+ if (known(path)) known(path).snippetVisits += 1;+ }+ for (const c of citations) {+ if (c.created_at.slice(0, 10) < since) continue;+ const path = new URL(c.url).pathname || '/';+ if (!known(path)) continue;+ known(path).citations += 1;+ known(path).citingPrompts.add(c.prompt_id);+ }+ const findings = detectPageOpportunities({+ pages: [...pages.values()].map((p) => ({+ ...p,+ aiPlatforms: [...p.aiPlatforms],+ citingPrompts: p.citingPrompts.size,+ })),+ });+ const detected = new Date(now.getTime() - 9 * DAY_MS).toISOString();+ const latest = new Date(dayStart(dayBefore(now, 0)) + 5 * 60 * 60 * 1000);+ const last = (latest > now ? new Date(latest.getTime() - DAY_MS) : latest).toISOString();+ return findings.map((f) => ({+ id: demoId(brandId, `page-opportunity:${f.landing_page}`),+ brand_id: brandId,+ ...f,+ first_detected_at: detected,+ last_detected_at: last,+ resolved_at: null,+ }));+}++/**+ * Every AI Referral Traffic row of the demo brand, built in memory: the+ * same every time but for its dates, which count back from `now`.+ *+ * @param {{ brandId: string, now: Date }} args+ */+export function trafficRows({ brandId, now }) {+ const analytics = analyticsRows(brandId, now);+ const search = searchRows(brandId, now);+ const snippet = snippetRows(brandId, now);+ const bots = botRows(brandId, now);+ const opportunities = opportunityRows(brandId, now, {+ analytics,+ snippet,+ citations: brandCitations(brandId, now),+ });+ return {+ brand: {+ ga_property_id: GA_PROPERTY_ID,+ gsc_property: GSC_PROPERTY,+ conversions_enabled: true,+ },+ analytics,+ search,+ snippet,+ bots,+ opportunities,+ };+}++/** The AI Referral Traffic module: its brand-scoped tables, in the order written, and its build. */+/**+ * Its tables' key columns where a row has no `id` of its own: the Google+ * Analytics and Search Console rows by their day and what they count, as+ * their sync writes them. ai_bot_events has neither (an identity id): it is+ * replaced (write's `generated`).+ */+const KEYS = {+ ga_ai_traffic_stats: 'brand_id,date,landing_page_query,medium,source',+ ga_page_stats: 'brand_id,date,landing_page',+ ga_channel_stats: 'brand_id,channel,date',+ ga_item_stats: 'brand_id,date,item_id,item_name',+ gsc_query_stats: 'brand_id,date,query',+ gsc_page_stats: 'brand_id,date,page',+ ai_bot_daily: 'brand_id,day,bot,page',+};++export const traffic = {+ name: 'traffic',+ // Rebuilt in place (write.js): written over, what isn't written again deleted after.+ inPlace: true,+ keys: KEYS,+ tables: [+ 'ga_ai_traffic_stats',+ 'ga_page_stats',+ 'ga_channel_stats',+ 'ga_item_stats',+ 'gsc_query_stats',+ 'gsc_page_stats',+ 'ai_traffic_logs',+ 'ai_traffic_events',+ 'ai_bot_connections',+ 'ai_bot_daily',+ 'ai_bot_events',+ 'page_opportunities',+ ],++ /**+ * Its independent tables side by side, through the build's writer (a+ * writer over `db` for a first build without one).+ *+ * @param {{ brandId: string, now: Date, db?: object, rebuild?: boolean, write?: Function }} ctx+ */+ async build({+ brandId,+ now,+ db = supabaseAdmin,+ rebuild = false,+ write = demoWriter({ db, brandId, rebuild, keys: KEYS }).write,+ }) {+ const rows = trafficRows({ brandId, now });+ const settings = async () => {+ const { error } = await db.from('brands').update(rows.brand).eq('id', brandId);+ if (error) throw new Error(`demo brand traffic settings: ${error.message}`);+ };+ await allSettledOrThrow([+ settings(),+ write('ga_ai_traffic_stats', rows.analytics.ai),+ write('ga_page_stats', rows.analytics.pages),+ write('ga_channel_stats', rows.analytics.channels),+ write('ga_item_stats', rows.analytics.items),+ write('gsc_query_stats', rows.search.queries),+ write('gsc_page_stats', rows.search.pages),+ (async () => {+ // A visit before the pageviews that continue it (visit_of).+ await write('ai_traffic_logs', rows.snippet.visits);+ await write('ai_traffic_logs', rows.snippet.pageviews);+ })(),+ write('ai_traffic_events', rows.snippet.events),+ write('ai_bot_connections', [rows.bots.connection]),+ write('ai_bot_daily', rows.bots.daily),+ write('ai_bot_events', rows.bots.events, { generated: true }),+ write('page_opportunities', rows.opportunities),+ ]);+ return {+ gaRows: rows.analytics.ai.length + rows.analytics.pages.length,+ visits: rows.snippet.visits.length,+ events: rows.snippet.events.length,+ bots: rows.bots.daily.length,+ opportunities: rows.opportunities.length,+ };+ },+};diff --git a/server/src/lib/demo/traffic.test.js b/server/src/lib/demo/traffic.test.jsnew file mode 100644index 000000000..82de51fe9--- /dev/null+++ b/server/src/lib/demo/traffic.test.js@@ -0,0 +1,192 @@+import { describe, expect, it, vi } from 'vitest';++vi.mock('../../config/supabase.js', () => ({ default: {} }));++const { GA_DAYS, GA_PROPERTY_ID, SNIPPET_DAYS, traffic, trafficRows } =+ await import('./traffic.js');+const { mapAiTrafficRows } = await import('../ga-sync.js');+const { classifyAiSource } = await import('../ai-sources.js');+const { AI_BOTS } = await import('../ai-bots.js');++/**+ * The demo brand's AI Referral Traffic (demo-site): a quarter of Analytics+ * and Search Console, four weeks of snippet visits and journeys, a month of+ * AI crawler hits, and the page opportunities they make.+ */++const BRAND_ID = '11111111-2222-4333-8444-555555555555';+const NOW = new Date('2026-10-28T12:00:00Z');+const DAY = 86_400_000;+const rows = trafficRows({ brandId: BRAND_ID, now: NOW });+const { analytics, search, snippet, bots, opportunities } = rows;++const days = (list, key) => new Set(list.map((r) => r[key]));+const ago = (day) => Math.floor((NOW.getTime() - Date.parse(`${day}T00:00:00Z`)) / DAY);++describe('trafficRows: Google Analytics and Search Console', () => {+ it('has a quarter of Analytics, up to yesterday, in the GA sync’s own row shape', () => {+ for (const list of [analytics.ai, analytics.pages, analytics.channels]) {+ const dates = [...days(list, 'date')].sort();+ expect(dates).toHaveLength(GA_DAYS);+ expect(ago(dates.at(-1))).toBe(1);+ for (const row of list) expect(Date.parse(row.created_at)).toBeLessThanOrEqual(NOW.getTime());+ }+ const shape = Object.keys(+ mapAiTrafficRows('b', {+ dimensionHeaders: [{ name: 'date' }],+ metricHeaders: [{ name: 'sessions' }],+ rows: [{ dimensionValues: [{ value: '20261027' }], metricValues: [{ value: '1' }] }],+ })[0],+ );+ for (const row of analytics.ai) {+ expect(Object.keys(row).sort()).toEqual([...shape, 'created_at'].sort());+ expect(classifyAiSource(row.source, { origin: 'analytics' })).toBe(row.platform);+ expect(row.landing_page.startsWith('/')).toBe(true);+ }+ // ChatGPT Shopping's product-card visits (#1283).+ expect(analytics.ai.some((r) => r.surface === 'shopping' && r.medium === 'feed')).toBe(true);+ });++ it('grows its AI sessions over the quarter, and pages carry their AI sessions', () => {+ const sum = (list) => list.reduce((s, r) => s + r.sessions, 0);+ const first = sum(analytics.ai.filter((r) => ago(r.date) > 60));+ const last = sum(analytics.ai.filter((r) => ago(r.date) <= 30));+ expect(last).toBeGreaterThan(first * 1.3);+ const day = analytics.ai[analytics.ai.length - 1].date;+ for (const ai of analytics.ai.filter((r) => r.date === day)) {+ const page = analytics.pages.find(+ (p) => p.date === day && p.landing_page === ai.landing_page,+ );+ expect(page.sessions).toBeGreaterThanOrEqual(ai.sessions);+ }+ expect(days(analytics.channels, 'channel')).toContain('AI Assistant');+ expect(analytics.items.every((i) => i.item_revenue >= 0 && i.item_name)).toBe(true);+ });++ it("has Search Console's queries for its sync window and pages for the quarter", () => {+ expect(days(search.queries, 'date').size).toBe(28);+ expect(days(search.pages, 'date').size).toBe(GA_DAYS);+ expect(Math.min(...search.queries.map((r) => ago(r.date)))).toBe(2);+ for (const row of search.pages) expect(row.page).toMatch(/^https:\/\/brewline\.example/);+ // The low-acid guide: impressions up, clicks down (AT-008).+ const guide = search.pages.filter((r) => r.page.endsWith('/low-acid-coffee-guide'));+ const half = (list, key) => list.reduce((s, r) => s + r[key], 0);+ const [early, late] = [guide.slice(0, 30), guide.slice(-30)];+ expect(half(late, 'impressions')).toBeGreaterThan(half(early, 'impressions'));+ expect(half(late, 'clicks')).toBeLessThan(half(early, 'clicks'));+ });+});++describe('trafficRows: the snippet', () => {+ it('records four weeks of visits, each from an AI source, none later than now', () => {+ const visitDays = [...new Set(snippet.visits.map((v) => v.created_at.slice(0, 10)))];+ expect(Math.max(...visitDays.map(ago))).toBe(SNIPPET_DAYS);+ for (const v of [...snippet.visits, ...snippet.pageviews]) {+ expect(classifyAiSource(v.source_platform)).toBe(v.source_platform);+ expect(v.url).toMatch(/^https:\/\/brewline\.example\//);+ expect(v.visitor_key).toMatch(/^[0-9a-f]{64}$/);+ expect(Date.parse(v.created_at)).toBeLessThan(NOW.getTime());+ }+ expect(snippet.visits.every((v) => v.visit_of === null)).toBe(true);+ const ids = new Set(snippet.visits.map((v) => v.id));+ expect(snippet.pageviews.every((p) => ids.has(p.visit_of))).toBe(true);+ expect(snippet.visits.some((v) => v.source_surface === 'shopping')).toBe(true);+ // AI answers still link a removed guide: Broken AI links.+ expect(snippet.visits.some((v) => v.http_status === 404)).toBe(true);+ });++ it('records journeys and conversions as the snippet’s events are stored', () => {+ const conversions = snippet.events.filter((e) => e.kind === 'conversion');+ expect(conversions.length).toBeGreaterThan(5);+ expect(new Set(conversions.map((e) => e.goal))).toEqual(new Set(['order', 'newsletter']));+ for (const e of snippet.events) {+ expect(e.visitor_id).toMatch(/^[A-Za-z0-9_-]{16,64}$/);+ expect(Date.parse(e.first_at)).toBeLessThanOrEqual(Date.parse(e.occurred_at));+ expect(NOW.getTime() - Date.parse(e.occurred_at)).toBeLessThan(21 * DAY);+ }+ const order = conversions.find((e) => e.goal === 'order');+ expect(order).toMatchObject({ origin: 'call', currency: 'USD' });+ expect(order.order_ref).toMatch(/^[0-9a-f]{64}$/);+ });+});++describe('trafficRows: AI crawlers', () => {+ it('counts a month of known crawlers by page, with the last days’ raw hits', () => {+ const known = new Map(AI_BOTS.map((b) => [b.key, b.type]));+ for (const row of bots.daily) expect(known.get(row.bot)).toBe(row.bot_type);+ expect(days(bots.daily, 'day').size).toBe(30);+ const recent = bots.daily.filter((r) => ago(r.day) < 2);+ expect(recent.reduce((s, r) => s + r.hits, 0)).toBe(bots.events.length);+ // A sender's secret hashes to sha256 hex: this one matches none.+ expect(bots.connection.secret_hash).not.toMatch(/^[0-9a-f]{64}$/);+ expect(bots.connection.last_event_at > bots.events.at(-1).occurred_at).toBe(true);+ });+});++describe('trafficRows: page opportunities and settings', () => {+ it('finds the best sellers AI never sends anyone to', () => {+ expect(opportunities.map((o) => o.landing_page)).toEqual([+ '/coffee/colombia-huila',+ '/subscriptions/espresso-club',+ ]);+ for (const o of opportunities) {+ expect(o).toMatchObject({ kind: 'no_ai_traffic', value_signal: 'revenue', ai_sessions: 0 });+ expect(o.value_percentile).toBeGreaterThanOrEqual(70);+ expect(o.resolved_at).toBeNull();+ }+ });++ it('maps the demo’s sample properties and turns conversions on', () => {+ expect(rows.brand).toEqual({+ ga_property_id: GA_PROPERTY_ID,+ gsc_property: 'sc-domain:brewline.example',+ conversions_enabled: true,+ });+ });++ it('is the same every time but for its dates', () => {+ const later = trafficRows({ brandId: BRAND_ID, now: new Date(NOW.getTime() + 7 * DAY) });+ const strip = ({ date: _d, created_at: _c, ...r }) => r;+ expect(later.analytics.channels.map(strip)).toEqual(analytics.channels.map(strip));+ expect(later.search.queries.map(strip)).toEqual(search.queries.map(strip));+ });+});++describe('traffic.build', () => {+ it('maps the brand, then writes only its own tables, a visit before its pageviews', async () => {+ const writes = [];+ const db = {+ from: (table) => ({+ upsert: async (list, { onConflict }) => {+ writes.push({ table, list, onConflict });+ return { error: null };+ },+ // ai_bot_events: an identity id, no key to write over.+ insert: async (list) => {+ writes.push({ table, list, inserted: true });+ return { error: null };+ },+ update: (values) => ({+ eq: async () => {+ writes.push({ table, values });+ return { error: null };+ },+ }),+ }),+ };+ await traffic.build({ brandId: BRAND_ID, now: NOW, db });+ expect(writes[0]).toEqual({ table: 'brands', values: rows.brand });+ const tables = writes.slice(1).map((w) => w.table);+ expect(tables.filter((t) => !traffic.tables.includes(t))).toEqual([]);+ const logs = writes.filter((w) => w.table === 'ai_traffic_logs').flatMap((w) => w.list);+ const firstPageview = logs.findIndex((l) => l.visit_of !== null);+ expect(logs.slice(firstPageview).every((l) => l.visit_of !== null)).toBe(true);+ // Written over by what each row counts, as the syncs write them.+ const keyOf = (table) => writes.find((w) => w.table === table).onConflict;+ expect(keyOf('ga_page_stats')).toBe('brand_id,date,landing_page');+ expect(keyOf('gsc_query_stats')).toBe('brand_id,date,query');+ expect(keyOf('ai_bot_daily')).toBe('brand_id,day,bot,page');+ expect(keyOf('ai_traffic_logs')).toBe('id');+ expect(writes.filter((w) => w.inserted).map((w) => w.table)).toEqual(['ai_bot_events']);+ });+});diff --git a/server/src/lib/intent-extraction.js b/server/src/lib/intent-extraction.jsindex b3b57971f..9e0e2f781 100644--- a/server/src/lib/intent-extraction.js+++ b/server/src/lib/intent-extraction.js@@ -184,6 +184,18 @@ export async function classifyIntentsByDecider(queries, decideFn) {return intents;}+/**+ * A fan-out sub-query as its intent is cached (fanout_query_intents, #333):+ * whitespace collapsed, trimmed, lower-cased, at most 512 characters; empty+ * when it isn't a string.+ *+ * @param {unknown} raw+ * @returns {string}+ */+export function normalizeIntentQuery(raw) {+ return typeof raw === 'string' ? raw.replace(/\s+/g, ' ').trim().toLowerCase().slice(0, 512) : '';+}+/*** Classifies the queries the cache doesn't hold yet. With* FANOUT_INTENTS_DECIDER set and OpenRouter configured, a decision modeldiff --git a/server/src/routes/prompts.js b/server/src/routes/prompts.jsindex 83ce10f4f..32b5b68e3 100644--- a/server/src/routes/prompts.js+++ b/server/src/routes/prompts.js@@ -5,7 +5,7 @@ import supabaseAdmin from '../config/supabase.js';import { MANAGER_ROLES, assertRole } from '../lib/access.js';import { resolveModel } from '../lib/ai-provider.js';import { decide, isDeciderConfigured } from '../lib/decider.js';-import { classifyQueryIntents } from '../lib/intent-extraction.js';+import { classifyQueryIntents, normalizeIntentQuery } from '../lib/intent-extraction.js';import { promptsFromTopicsSchema } from '../lib/brand-setup-input.js';import { getLanguageName } from '../lib/languages.js';import {@@ -940,10 +940,6 @@ const FANOUT_INTENT_MAX_QUERIES = 200;// PostgREST / proxy length limits even with long sub-queries.const FANOUT_INTENT_LOOKUP_CHUNK = 40;-function normalizeIntentQuery(raw) {- return typeof raw === 'string' ? raw.replace(/\s+/g, ' ').trim().toLowerCase().slice(0, 512) : '';-}-router.post('/fanout-intents', async (req, res) => {try {const raw = Array.isArray(req.body?.queries) ? req.body.queries : [];