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(content): write a page with its shopping path, the catalog's facts only (shopping path, part 4) (#1592)
Cihan5b3ffca649f9
diff --git a/server/src/lib/catalog/category-pages.js b/server/src/lib/catalog/category-pages.jsindex 7599b0b2f..cc133d6d8 100644--- a/server/src/lib/catalog/category-pages.js+++ b/server/src/lib/catalog/category-pages.js@@ -138,7 +138,7 @@ export function leafName(category) {}/** A word's singular, roughly: boots → boot, glasses → glass, boxes → box; dress stays. */-function singular(word) {+export function singular(word) {if (word.length > 4 && /(ss|x|z|ch|sh)es$/.test(word)) return word.slice(0, -2);if (word.length > 3 && word.endsWith('s') && !word.endsWith('ss')) return word.slice(0, -1);return word;@@ -387,23 +387,26 @@ export class CategoryPageError extends Error {}/**- * Whether `url` is on the brand's site: one of its domains (brand_domains),+ * The brand's site, as onBrandDomain reads it: its domains (brand_domains),* or, for a brand with none stored, the site its catalog's products link to.*/-async function onBrandSite(db, brandId, url) {+export async function brandSite(db, brandId) {const { data, error } = await db.from('brand_domains').select('domain').eq('brand_id', brandId);if (error) throw new Error(`brand_domains read: ${error.message}`);- let domains = (data ?? []).map((d) => d.domain).filter(Boolean);- if (!domains.length) {- const links = await db- .from('catalog_products')- .select('url')- .eq('brand_id', brandId)- .limit(MAX_PRODUCT_LINKS);- if (links.error) throw new Error(`catalog_products read: ${links.error.message}`);- domains = (links.data ?? []).map((p) => p.url).filter(Boolean);- }- return onBrandDomain(url, domains);+ const domains = (data ?? []).map((d) => d.domain).filter(Boolean);+ if (domains.length) return domains;+ const links = await db+ .from('catalog_products')+ .select('url')+ .eq('brand_id', brandId)+ .limit(MAX_PRODUCT_LINKS);+ if (links.error) throw new Error(`catalog_products read: ${links.error.message}`);+ return (links.data ?? []).map((p) => p.url).filter(Boolean);+}++/** Whether `url` is on the brand's site (brandSite). */+async function onBrandSite(db, brandId, url) {+ return onBrandDomain(url, await brandSite(db, brandId));}/**diff --git a/server/src/lib/content-agent.js b/server/src/lib/content-agent.jsindex 901938cca..1d3a27a39 100644--- a/server/src/lib/content-agent.js+++ b/server/src/lib/content-agent.js@@ -7,6 +7,7 @@ import { resolveModel } from './ai-provider.js';import { contentBriefModelId, rebuildSources } from './content-brief.js';import { inputSources, readDraftInputs } from './content-inputs.js';import { linkAllowList, linkKey, linkTargetsPrompt, readLinkTargets } from './content-links.js';+import { shoppingGiven, shoppingPathFor, withShopping } from './shopping-links.js';import { siteTypeLine, pageTypeFormat } from './content-page-types.js';import {NOT_NAMES,@@ -42,6 +43,7 @@ import {stripSourceIds,textPieces,todayText,+ withNamedNumbers,} from './content-sources.js';import { draftLanguage } from './content-language.js';import { editSpans } from './edit-spans.js';@@ -1204,7 +1206,7 @@ export async function fixDraftRules({ draftId, brandId, input, handoff = null })const { data: draft, error: draftError } = await supabaseAdmin.from('content_drafts').select(- `id, brand_id, title, body_html, page_type, rules, action_id, status, source_url, agent_state, agent_state_at${handoff ? ', quality_handoff' : ''}`,+ `id, brand_id, title, body_html, page_type, rules, action_id, status, source_url, agent_state, agent_state_at, shopping_path${handoff ? ', quality_handoff' : ''}`,).eq('id', draftId).maybeSingle();@@ -1349,9 +1351,11 @@ async function fixWithRun({// The data it writes from, numbered, Brief › Your input last (CE-048);// the numbers it may write unmarked.await advanceRun(run, 1);- const [prompts, inputRow] = await Promise.all([+ const [prompts, inputRow, shoppingPath] = await Promise.all([readPrompts(brandId, input.sources?.promptIds ?? []),readDraftInputs(draftId),+ // Its shopping path, checked: none when it isn't one (shopping path, part 4).+ shoppingPathFor(draft),]);const numbered = numberSources({prompts,@@ -1366,15 +1370,21 @@ async function fixWithRun({? input.rules.map((r) => r.name): [...input.rules.flatMap((r) => [r.name, r.detail]), ...input.textRules];const facts = handoff && ruleKeys.has('trust:brandFacts') ? await approvedFacts(brandId) : [];- const known = knownNumbers(- htmlText(draft.body_html),- draft.title,- brand.name,- brand.industry,- brand.description,- ...ruleWords,- ...facts,- todayText(),+ // The shopping path's prices and names: the catalog's, written unmarked.+ const shopping = shoppingGiven(shoppingPath);+ const known = withNamedNumbers(+ knownNumbers(+ htmlText(draft.body_html),+ draft.title,+ brand.name,+ brand.industry,+ brand.description,+ ...ruleWords,+ ...facts,+ shopping.prices,+ todayText(),+ ),+ shopping.names,);// The people and quotes it may name: the same words, its data's and the authors' (CE-044).const authors = await authorWords(supabaseAdmin, brand.organization_id);@@ -1385,6 +1395,7 @@ async function fixWithRun({brand.industry,brand.description,...ruleWords,+ ...shopping.names,...numbered.map(sourceWords),...authors,);@@ -1403,12 +1414,16 @@ async function fixWithRun({handoff && [...ruleKeys].some((key) => SOURCE_CHECKS.has(key))? new Set([...ruleKeys, 'outboundLinks', 'citeInline']): ruleKeys;- const targets = await readLinkTargets({- brandId,- draftId,- sourceUrl: draft.source_url,- ruleKeys: linkKeys,- });+ // …and its shopping path's shop pages (shopping path, part 4).+ const targets = withShopping(+ await readLinkTargets({+ brandId,+ draftId,+ sourceUrl: draft.source_url,+ ruleKeys: linkKeys,+ }),+ shoppingPath,+ );// Held on the budget before the call, so calls at once can't pass it.const budget = await reserveAiBudget(brand.organization_id, {feature: 'content',diff --git a/server/src/lib/content-agent.test.js b/server/src/lib/content-agent.test.jsindex 83bd937cf..21f9a0273 100644--- a/server/src/lib/content-agent.test.js+++ b/server/src/lib/content-agent.test.js@@ -25,6 +25,7 @@ vi.mock('../config/supabase.js', () => ({in: () => chain,gt: () => chain,order: () => chain,+ range: () => chain,then: (resolve) => resolve(result()),limit: async () => result(),insert: async (value) => (writes.push({ table, value }), { error: null }),@@ -1712,6 +1713,86 @@ describe('cleanInsertHtml', () => {});describe('fixDraftRules', () => {+ it("knows the shopping path's prices and names, a product's number only beside its name, and links its pages", async () => {+ rows.content_drafts = {+ ...rows.content_drafts,+ shopping_path: {+ version: 1,+ builtAt: '2026-10-27T10:00:00.000Z',+ pageType: 'guide',+ categories: [],+ products: [+ {+ id: 'k1',+ title: 'Ruth Miller Kettle 600',+ url: 'https://shop.example/products/k1',+ price: 49,+ salePrice: null,+ currency: 'USD',+ availability: 'in_stock',+ image: null,+ issues: [],+ score: 9,+ },+ ],+ leftOut: [],+ locked: [],+ removed: [],+ added: [],+ },+ };+ rows.brand_domains = [{ domain: 'shop.example' }];+ rows.catalog_variants = [+ { id: 'v1', product_id: 'k1', availability: 'in stock', price: 49, currency: 'USD' },+ ];+ generateObject.mockResolvedValue({+ object: {+ suggestions: [+ suggestion({+ after:+ 'good coffee and the <a href="https://shop.example/products/k1">Ruth Miller Kettle 600</a> at $49',+ sources: [],+ marks: [],+ }),+ // The name's number alone: a number with no source.+ suggestion({+ passage: 3,+ before: 'A dripper',+ after: 'A 600 ml dripper',+ sources: [],+ marks: [],+ }),+ // A name the path has, next to a role.+ suggestion({+ passage: 2,+ before: 'tastes better than other methods',+ after: 'tastes better, says designer Ruth Miller',+ sources: [],+ marks: [],+ }),+ ],+ },+ usage: { inputTokens: 900, outputTokens: 200 },+ });+ const result = await fixDraftRules({+ draftId: DRAFT,+ brandId: BRAND,+ input: input({ locked: [] }),+ });+ expect(generateObject.mock.calls[0][0].prompt).toContain(+ '- Ruth Miller Kettle 600 — https://shop.example/products/k1',+ );+ expect(result.count).toBe(2);+ const batch = writes.find((w) => w.table === 'content_draft_suggestions');+ expect(batch.value.map((r) => r.after_text)).toEqual([+ 'good coffee and the Ruth Miller Kettle 600 at $49',+ 'tastes better, says designer Ruth Miller',+ ]);+ expect(batch.value[0].after_links).toEqual([+ { start: 20, end: 42, href: 'https://shop.example/products/k1' },+ ]);+ });+it('reads attributions in the page’s language: its Language rule, or My rules’ when it has none', async () => {// ", per E-Mail." gives a claim to a source on an English page, never on a German one.const perEmail = suggestion({diff --git a/server/src/lib/content-links.js b/server/src/lib/content-links.jsindex 82be55d65..a79430fc0 100644--- a/server/src/lib/content-links.js+++ b/server/src/lib/content-links.js@@ -138,10 +138,19 @@ export async function readLinkTargets({ brandId, draftId, sourceUrl, ruleKeys })return { own, sources };}-/** The targets as the model reads them, by the rules asked for; '' when there are none. */+/**+ * The targets as the model reads them, by the rules asked for; '' when there+ * are none. A draft's shopping path (shopping-links.js withShopping) comes+ * first, whatever the rules: it's the page's plan.+ */export function linkTargetsPrompt(targets, ruleKeys) {const lines = (list) => list.map((p) => `- ${p.title} — ${p.url}`).join('\n');const parts = [];+ if (targets.shopping?.length) {+ parts.push(+ `The shopping path, the brand's shop pages this page sends readers to: link each where the page names it, once:\n${lines(targets.shopping)}`,+ );+ }if (targets.own.length && ruleKeys.has('internalLinks')) {parts.push(`Pages on the brand's site you may link to (internal links):\n${lines(targets.own)}`,@@ -160,5 +169,10 @@ export function linkTargetsPrompt(targets, ruleKeys) {* else is unwrapped.*/export function linkAllowList(targets) {- return new Map([...targets.own, ...targets.sources].map((p) => [linkKey(p.url), p.url]));+ return new Map(+ [...targets.own, ...targets.sources, ...(targets.shopping ?? [])].map((p) => [+ linkKey(p.url),+ p.url,+ ]),+ );}diff --git a/server/src/lib/content-sources.js b/server/src/lib/content-sources.jsindex 467c35143..fd77c444f 100644--- a/server/src/lib/content-sources.js+++ b/server/src/lib/content-sources.js@@ -183,6 +183,37 @@ export function knownNumbers(...texts) {);}+/**+ * The known numbers with names that hold their own ("Trail 450", a+ * shopping path's product: shopping-links.js): a name's numbers are known+ * where the name is written whole, never alone. Kept on the set, as+ * `named`; returns it.+ */+export function withNamedNumbers(known, names) {+ const named = [+ ...new Set(+ (names ?? [])+ .map((n) =>+ String(n ?? '')+ .replace(/\s+/g, ' ')+ .trim(),+ )+ .filter((n) => /\d/.test(n)),+ ),+ ].sort((a, b) => b.length - a.length);+ if (!named.length) return known;+ // Its spaces and apostrophes written any way: "Men’s Trail 450".+ const spelled = named.map((n) =>+ n+ .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')+ .replace(/ /g, '\\s+')+ .replace(/['’]/g, "['’]"),+ );+ return Object.assign(known, {+ named: new RegExp(`(?<![\\p{L}\\p{N}])(?:${spelled.join('|')})(?![\\p{L}\\p{N}])`, 'gu'),+ });+}+/** Today as the agent may write it: its year alone ("23" on the 23rd would let "23%" through). */export function todayText(now = new Date()) {return `${now.getUTCFullYear()}`;@@ -242,7 +273,10 @@ export function textPieces(html) {* or a currency sign is a claim and counts.*/export function addedNumbers(text, known) {- const s = String(text ?? '').replace(BRACKETS, (m) => ' '.repeat(m.length));+ const blank = (m) => ' '.repeat(m.length);+ const bare = String(text ?? '').replace(BRACKETS, blank);+ // A name's own numbers, where it is written (withNamedNumbers).+ const s = known.named ? bare.replace(known.named, blank) : bare;return numbersIn(s).filter((n) => {// A claim is known only with its unit or sign; a plain number as any.if (known.has(n.unit ? unitKey(n) : n.value)) return false;diff --git a/server/src/lib/content-writer.js b/server/src/lib/content-writer.jsindex 47e5f69f8..ee4f4e64f 100644--- a/server/src/lib/content-writer.js+++ b/server/src/lib/content-writer.js@@ -32,6 +32,13 @@ import {import { inputSources, readDraftInputs } from './content-inputs.js';import { draftLanguage } from './content-language.js';import { linkAllowList, linkTargetsPrompt, readLinkTargets } from './content-links.js';+import {+ linkShoppingPath,+ shoppingGiven,+ shoppingPathFor,+ shoppingPrompt,+ withShopping,+} from './shopping-links.js';import { siteTypeLine, pageTypeFormat } from './content-page-types.js';import { authorWords, guardPeople, guardPeopleInHtml, knownPeople } from './content-people.js';import {@@ -46,6 +53,7 @@ import {sourceWords,stripSourceIds,todayText,+ withNamedNumbers,} from './content-sources.js';import {MAX_PAGE_HTML,@@ -403,7 +411,7 @@ export async function startDraftWriting({ draftId, brandId, input, rewrite = falconst { data: draft, error: draftError } = await supabaseAdmin.from('content_drafts').select(- 'id, brand_id, title, page_type, rules, method, source_url, body_html, status, agent_state, agent_state_at, opportunity_id, brief, action_id',+ 'id, brand_id, title, page_type, rules, method, source_url, body_html, status, agent_state, agent_state_at, opportunity_id, brief, action_id, shopping_path',).eq('id', draftId).maybeSingle();@@ -572,6 +580,7 @@ async function letGo(draft) {async function rewritePage({attributable,draft,+ shoppingPath,brand,model,input,@@ -587,12 +596,16 @@ async function rewritePage({// The Length rule's target; with Length off, the page keeps its length.const length = ruleKeys.has('length') ? input.sources.length : textLength(draft.body_html);// Where it may link: the brand's pages and the draft's sources (A).- const targets = await readLinkTargets({- brandId: draft.brand_id,- draftId: draft.id,- sourceUrl: draft.source_url,- ruleKeys,- });+ // …and its shopping path's shop pages (shopping path, part 4).+ const targets = withShopping(+ await readLinkTargets({+ brandId: draft.brand_id,+ draftId: draft.id,+ sourceUrl: draft.source_url,+ ruleKeys,+ }),+ shoppingPath,+ );await advanceRun(run, 2);const { object, usage } = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>generateObject({@@ -842,7 +855,7 @@ function startWritingRun(draft, input, rewrite) {/** The run itself, step by step: nothing is kept once it is stopped. */async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }) {await advanceRun(run, 1);- const [prompts, inputRow, opportunityRes] = await Promise.all([+ const [prompts, inputRow, opportunityRes, shoppingPath] = await Promise.all([readPrompts(draft.brand_id, input.sources.promptIds),readDraftInputs(draft.id),draft.opportunity_id && !draft.brief@@ -853,6 +866,8 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }.eq('brand_id', draft.brand_id).maybeSingle(): Promise.resolve({ data: null, error: null }),+ // Its shopping path, checked: none when it isn't one (shopping path, part 4).+ shoppingPathFor(draft),]);if (opportunityRes.error) throw opportunityRes.error;// A new page's writer has no links rule: the brief's planned links are left out for it.@@ -863,6 +878,8 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }// "/top-37-tools", "/interview-with-john-smith"), as sourceWords keeps a// cited page's out.const briefGiven = brief.replace(/https?:\/\/\S+/g, ' ');+ // The shopping path's prices and names (shopping path, part 4): the catalog's, written unmarked.+ const shopping = shoppingGiven(shoppingPath);// The data it writes from, numbered (CE-033), Brief › Your input last// (CE-048); the numbers a suggestion may write unmarked (the page's, the// brand's, the brief's, the rules', today's), and those a page written@@ -874,18 +891,25 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }queries: input.sources.queries,inputs: inputSources(inputRow, brand.name),});- const known = knownNumbers(- htmlText(draft.body_html),- draft.title,- brand.name,- brand.industry,- brand.description,- briefGiven,- ...input.rules.flatMap((r) => [r.name, r.detail]),- ...input.textRules,- todayText(),+ const known = withNamedNumbers(+ knownNumbers(+ htmlText(draft.body_html),+ draft.title,+ brand.name,+ brand.industry,+ brand.description,+ briefGiven,+ shopping.prices,+ ...input.rules.flatMap((r) => [r.name, r.detail]),+ ...input.textRules,+ todayText(),+ ),+ shopping.names,+ );+ const knownInPage = withNamedNumbers(+ new Set([...known, ...knownNumbers(...numbered.map(sourceWords))]),+ shopping.names,);- const knownInPage = new Set([...known, ...knownNumbers(...numbered.map(sourceWords))]);// The people and quotes it may name: the same words, its data's and the authors' (CE-044).const authors = await authorWords(supabaseAdmin, brand.organization_id);const people = knownPeople(@@ -895,6 +919,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }brand.industry,brand.description,briefGiven,+ ...shopping.names,...input.rules.flatMap((r) => [r.name, r.detail]),...input.textRules,...numbered.map(sourceWords),@@ -914,6 +939,7 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }return await rewritePage({attributable,draft,+ shoppingPath,brand,model,input,@@ -933,7 +959,14 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }model,schema: pageSchema,system: writeSystemPrompt(),- prompt: buildWritePrompt({ brand, draft, numbered, brief, input }),+ prompt: buildWritePrompt({+ brand,+ draft,+ numbered,+ brief,+ input,+ linkTargets: shoppingPrompt(shoppingPath),+ }),abortSignal,}),);@@ -957,7 +990,8 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }draft,{title,- body_html: renderPage({ ...written, title }),+ // The shopping path's names it wrote, linked (shopping-links.js).+ body_html: linkShoppingPath(renderPage({ ...written, title }), shoppingPath).html,status: 'in_review',author: 'agent',agent_state: null,@@ -973,12 +1007,16 @@ async function writeWithRun({ draft, brand, model, input, budget, rewrite, run }const lockedKeys = new Set(input.locked.map(passageKey));const ruleKeys = new Set(input.rules.map((r) => r.key));// Where it may link: the brand's pages and the draft's sources (A).- const targets = await readLinkTargets({- brandId: draft.brand_id,- draftId: draft.id,- sourceUrl: draft.source_url,- ruleKeys,- });+ // …and its shopping path's shop pages (shopping path, part 4).+ const targets = withShopping(+ await readLinkTargets({+ brandId: draft.brand_id,+ draftId: draft.id,+ sourceUrl: draft.source_url,+ ruleKeys,+ }),+ shoppingPath,+ );await advanceRun(run, 2);const { object, usage } = await runCall(run, budget, CALL_TIMEOUT_MS, (abortSignal) =>generateObject({diff --git a/server/src/lib/content-writer.test.js b/server/src/lib/content-writer.test.jsindex eea2c74ce..7c672b9e9 100644--- a/server/src/lib/content-writer.test.js+++ b/server/src/lib/content-writer.test.js@@ -43,7 +43,10 @@ vi.mock('../config/supabase.js', () => ({neq: (col, v) => (op.filters.push([`${col}<>`, v]), chain),gt: (col, v) => (op.filters.push([`${col}>`, v]), chain),in: (col, v) => (op.filters.push([col, v]), chain),+ is: (col, v) => (op.filters.push([`${col} is`, v]), chain),or: (v) => (op.filters.push(['or', v]), chain),+ order: () => chain,+ range: () => chain,limit: () => chain,update: (value) => ((op.kind = 'update'), (op.value = value), chain),insert: (value) => ((op.kind = 'insert'), (op.value = value), chain),@@ -518,6 +521,113 @@ describe('runDraftWriting on a new page', () => {expect(written).not.toContain('3,400');});+ /** A draft's shopping path as content_drafts.shopping_path keeps it. */+ const shoppingPath = (over = {}) => ({+ version: 1,+ builtAt: '2026-10-27T10:00:00.000Z',+ pageType: 'guide',+ categories: [+ { name: 'Hiking Boots', url: 'https://shop.example/collections/hiking-boots', title: null },+ ],+ products: [+ {+ id: 'p1',+ title: 'Summit GTX Boot',+ url: 'https://shop.example/products/p1',+ price: 189,+ salePrice: null,+ currency: 'USD',+ availability: 'in_stock',+ image: null,+ issues: [],+ score: 9,+ },+ {+ id: 'p2',+ title: 'Trail 450 Boot',+ url: 'https://shop.example/products/p2',+ price: 129,+ salePrice: null,+ currency: 'USD',+ availability: 'in_stock',+ image: null,+ issues: [],+ score: 8,+ },+ {+ id: 'p3',+ title: 'Evil Boot',+ url: 'https://evil.example/products/p3',+ price: 99,+ salePrice: null,+ currency: 'USD',+ availability: 'in_stock',+ image: null,+ issues: [],+ score: 7,+ },+ ],+ leftOut: [],+ locked: [],+ removed: [],+ added: [],+ ...over,+ });++ it("writes with the draft's shopping path, its prices kept and its names linked (shopping path)", async () => {+ generateObject.mockResolvedValue({+ object: {+ ...page,+ intro: [+ 'Good hiking boots keep you dry. The Summit GTX Boot costs $189, or 189.00 USD; the Trail 450 Boot [figure, source].',+ 'At 450 g it is light, and $199 elsewhere.',+ ],+ },+ usage: { inputTokens: 10, outputTokens: 20 },+ });+ rows.brand_domains = [{ domain: 'shop.example' }];+ // Sold out since the path was built: the catalog's stock now.+ rows.catalog_variants = [+ { id: 'v1', product_id: 'p1', availability: 'out of stock', price: 189, currency: 'USD' },+ ];+ await expect(run(draftRow({ shopping_path: shoppingPath() }))).resolves.toEqual({+ written: true,+ });+ const { prompt } = generateObject.mock.calls[0][0];+ expect(prompt).toContain('- Summit GTX Boot: 189.00 USD, out of stock');+ // A page off the brand's site isn't offered.+ expect(prompt).not.toContain('Evil Boot');+ const written = draftUpdates().find((u) => u.body_html)?.body_html;+ expect(written).toContain(+ '<a href="https://shop.example/collections/hiking-boots">hiking boots</a>',+ );+ expect(written).toContain(+ 'The <a href="https://shop.example/products/p1">Summit GTX Boot</a> costs $189, or 189.00 USD;',+ );+ // A product's own number where its name is: nowhere else, nor another price.+ expect(written).toContain('Trail 450 Boot</a> [figure, source].');+ expect(written).toContain('At [figure, source] g it is light, and [figure, source] elsewhere.');+ });++ it('writes a draft whose shopping path is broken as one with none', async () => {+ generateObject.mockResolvedValue({+ object: { ...page, intro: ['The Summit GTX Boot costs $189.'] },+ usage: { inputTokens: 10, outputTokens: 20 },+ });+ rows.brand_domains = [{ domain: 'shop.example' }];+ for (const broken of [+ shoppingPath({ categories: { name: 'Hiking Boots' } }),+ shoppingPath({ products: [null] }),+ ]) {+ generateObject.mockClear();+ writes = [];+ await expect(run(draftRow({ shopping_path: broken }))).resolves.toEqual({ written: true });+ expect(generateObject.mock.calls[0][0].prompt).not.toContain('The shopping path');+ const written = draftUpdates().find((u) => u.body_html)?.body_html;+ expect(written).toContain('<p>The Summit GTX Boot costs [figure, source].</p>');+ }+ });+it('leaves a draft marked ready during the run as it is', async () => {finished = null;await expect(run(draftRow())).resolves.toEqual({ skipped: true });diff --git a/server/src/lib/shopping-links.js b/server/src/lib/shopping-links.jsnew file mode 100644index 000000000..87c8f4b27--- /dev/null+++ b/server/src/lib/shopping-links.js@@ -0,0 +1,400 @@+import * as cheerio from 'cheerio';+import supabaseAdmin from '../config/supabase.js';+import { onBrandDomain } from './audit/host.js';+import { brandSite, leafName, singular } from './catalog/category-pages.js';+import { isInStock, stockKey } from './catalog/stock.js';+import { linkHref, linkKey } from './content-links.js';+import { foldLetters } from './letters.js';+import { logger } from './logger.js';+import { readAllRpcRows } from './rpc-pages.js';+import { MAX_PRODUCTS, PRODUCTS_FOR, TITLE_CHARS, readShoppingPath } from './shopping-path.js';++/**+ * The Content Agent's side of a draft's shopping path (shopping path, part+ * 4): what it's told about the category page and the products the page+ * links (shopping-path.js picked them), and the links themselves.+ *+ * A page written whole is text the agent can't link (content-writer.js+ * renderPage), so the agent is asked to write each name exactly as given,+ * and the names it wrote are linked here: the first time each appears in a+ * paragraph or a list item, never in a heading or inside a link. A page it+ * changes (rewrite, suggestions, fixes) links them itself: they join the+ * pages it may link to (content-links.js), where anything else is+ * unwrapped. Either way, the only facts it's given about a product are the+ * catalog's: its name, price and stock.+ */++/**+ * What the writer is told of a product's stock, by stock.js's key (an+ * in-stock one is "in stock"); the best first. Nothing for one it can't read.+ */+const STOCK_WORDS = {+ limited_availability: 'low stock',+ preorder: 'available to pre-order',+ pre_order: 'available to pre-order',+ backorder: 'on backorder',+ back_order: 'on backorder',+ out_of_stock: 'out of stock',+ outofstock: 'out of stock',+ sold_out: 'out of stock',+};+const STOCK_ORDER = [+ 'in stock',+ 'low stock',+ 'available to pre-order',+ 'on backorder',+ 'out of stock',+];++/** A variant's stock in the writer's words, or null when it can't be read. */+export function stockWords(availability) {+ const key = stockKey(availability);+ if (Object.hasOwn(STOCK_WORDS, key)) return STOCK_WORDS[key];+ return isInStock(key) ? 'in stock' : null;+}++/** The sign a price is written with, by its currency, as the number guard reads it (content-sources.js). */+const CURRENCY_SIGNS = {+ USD: '$',+ CAD: '$',+ AUD: '$',+ NZD: '$',+ SGD: '$',+ HKD: '$',+ MXN: '$',+ EUR: '€',+ GBP: '£',+ JPY: '¥',+ CNY: '¥',+ TRY: '₺',+};++/** A product's price, the sale price first; null with none. */+function amountOf(product) {+ const amount = product.salePrice ?? product.price;+ return amount != null && Number.isFinite(Number(amount)) ? Number(amount) : null;+}++/** A product's price and stock as the agent is told them: "189.00 USD, in stock". */+function offerLine(product) {+ const amount = amountOf(product);+ const price =+ amount == null ? null : `${amount.toFixed(2)}${product.currency ? ` ${product.currency}` : ''}`;+ return [price, stockWords(product.availability)].filter(Boolean).join(', ');+}++/** The ways a product's price may be written: 189, 189.00, $189, $189.00 ("$1,299" reads as 1299). */+function priceSpellings(product) {+ const amount = amountOf(product);+ if (amount == null) return [];+ const values = [...new Set([amount.toFixed(2), String(amount)])];+ const signs = [+ '',+ Object.hasOwn(CURRENCY_SIGNS, product.currency ?? '') ? CURRENCY_SIGNS[product.currency] : '',+ ];+ return [...new Set(signs.flatMap((sign) => values.map((value) => `${sign}${value}`)))];+}++/**+ * Text as names are looked up in it, with where each of its characters+ * came from (`from`, `to`): apostrophes as one, runs of spaces as one, and+ * with `fold` its letters folded as letters.js does ("KADIN" reads as+ * "kadın", "İç" as "iç").+ */+function readable(text, fold) {+ const out = [];+ const from = [];+ const to = [];+ let at = 0;+ let space = false;+ for (const char of String(text ?? '')) {+ const end = at + char.length;+ let read = /\s/u.test(char) ? ' ' : /['’‘ʼ]/u.test(char) ? "'" : char;+ if (read === ' ' && space) {+ to[to.length - 1] = end;+ } else {+ if (fold) read = foldLetters(read);+ out.push(read);+ space = read.endsWith(' ');+ for (let i = 0; i < read.length; i += 1) {+ from.push(at);+ to.push(end);+ }+ }+ at = end;+ }+ return { text: out.join(''), from, to };+}++/** A name as it's compared: a category's folded, a product's as written (case kept: "Pro" isn't "a pro tip"). */+const nameKey = (name, kind) => readable(name, kind === 'category').text.trim();++/** An https address to link, without tracking; null for any other. */+function shopHref(url) {+ const href = linkHref(url);+ return href && href.startsWith('https:') ? href : null;+}++/**+ * The path's products the page is written with: as many as its page type+ * links (shopping-path.js PRODUCTS_FOR), the person's locked and added ones+ * all kept and counted in it, MAX_PRODUCTS at most.+ */+function pathProducts(path) {+ const products = path?.products ?? [];+ const own = new Set([...(path?.locked ?? []), ...(path?.added ?? [])]);+ const max = Object.hasOwn(PRODUCTS_FOR, path?.pageType ?? '')+ ? PRODUCTS_FOR[path.pageType]+ : MAX_PRODUCTS;+ let room = max - products.filter((p) => own.has(p.id)).length;+ return products.filter((p) => own.has(p.id) || (room -= 1) >= 0).slice(0, MAX_PRODUCTS);+}++/**+ * The path's category pages and products as link targets, each with the+ * name it's linked on and its catalog item: https only, each name and+ * address once (the first keeps it).+ */+function pathTargets(path) {+ const out = [];+ const seen = new Set();+ const add = (url, title, kind, item) => {+ const href = shopHref(url);+ const name = String(title ?? '')+ .replace(/\s+/g, ' ')+ .trim();+ if (!href || !name) return;+ const keys = [`${kind}:${nameKey(name, kind)}`, `url:${linkKey(href)}`];+ if (keys.some((key) => seen.has(key))) return;+ keys.forEach((key) => seen.add(key));+ out.push({ url: href, title: name, kind, item });+ };+ for (const category of path?.categories ?? []) {+ add(category.url, leafName(category.name), 'category', category);+ }+ for (const product of pathProducts(path)) add(product.url, product.title, 'product', product);+ return out;+}++/** A path's category pages and products as link targets, each with the name it's linked on. */+export function shoppingTargets(path) {+ return pathTargets(path).map(({ url, title, kind }) => ({ url, title, kind }));+}++/** The link targets with the shopping path's: offered first, kept whatever rules are asked for. */+export function withShopping(targets, path) {+ return { ...targets, shopping: shoppingTargets(path) };+}++/**+ * The products with their price and stock as the catalog has them now, both+ * from one live variant: the best stock, then the lowest price at it. A+ * product with no live variant left is gone from the catalog: nothing is+ * said of its price or stock. As the path saved them when the read fails.+ */+async function withOffer(db, brandId, products, draftId) {+ if (!products.length) return products;+ const { data, error } = await readAllRpcRows('catalog_variants', (from, to) =>+ db+ .from('catalog_variants')+ .select('id, product_id, availability, price, sale_price, currency')+ .eq('brand_id', brandId)+ .in(+ 'product_id',+ products.map((p) => p.id),+ )+ .is('removed_at', null)+ .order('id')+ .range(from, to),+ ).catch((err) => ({ data: null, error: err }));+ if (error) {+ logger.warn({ err: error, draftId }, "[shopping-links] reading stock failed; the path's used");+ return products;+ }+ // A stock it can't read comes last, and nothing is said of it.+ const rankOf = (v) => {+ const rank = STOCK_ORDER.indexOf(stockWords(v.availability));+ return rank === -1 ? STOCK_ORDER.length : rank;+ };+ const amount = (v) => amountOf({ price: v.price, salePrice: v.sale_price }) ?? Infinity;+ const best = new Map();+ for (const variant of data) {+ const held = best.get(variant.product_id);+ const rank = rankOf(variant);+ if (held && (held.rank < rank || (held.rank === rank && amount(held) <= amount(variant)))) {+ continue;+ }+ best.set(variant.product_id, { ...variant, rank });+ }+ return products.map((p) => {+ const v = best.get(p.id);+ if (!v) return { ...p, price: null, salePrice: null, availability: '' };+ return {+ ...p,+ price: v.price ?? null,+ salePrice: v.sale_price ?? null,+ currency: v.currency ?? null,+ availability: v.rank < STOCK_ORDER.length ? stockKey(v.availability) : '',+ };+ });+}++/**+ * The draft's shopping path as the agent writes with it, or null: read as+ * shopping-path.js reads it (none when it isn't a path), its pages only+ * https ones on the brand's own site (a stored path is a member's to+ * change), its products' stock as the catalog has it now. The site not+ * read, it has none: nothing it can't check is linked.+ */+export async function shoppingPathFor(draft, { db = supabaseAdmin } = {}) {+ const path = readShoppingPath(draft.shopping_path, { draftId: draft.id });+ if (!path) return null;+ let site;+ try {+ site = await brandSite(db, draft.brand_id);+ } catch (err) {+ logger.warn({ err, draftId: draft.id }, '[shopping-links] reading the brand site failed');+ return null;+ }+ const onSite = (page) => shopHref(page.url) && onBrandDomain(page.url, site);+ // A name longer than the build ever keeps isn't the build's: left out.+ const named = (name) => String(name ?? '').length <= TITLE_CHARS;+ const categories = path.categories.filter((c) => onSite(c) && named(c.name));+ const products = path.products.filter((p) => onSite(p) && named(p.title));+ const off = path.categories.length + path.products.length - categories.length - products.length;+ if (off) {+ logger.warn(+ { draftId: draft.id, off },+ "[shopping-links] path pages off the brand's site left out",+ );+ }+ return {+ ...path,+ categories,+ products: await withOffer(db, draft.brand_id, products, draft.id),+ };+}++/**+ * The shopping path as a new page's writer reads it; '' when the path has+ * nothing to link.+ */+export function shoppingPrompt(path) {+ const targets = pathTargets(path);+ const categories = targets.filter((t) => t.kind === 'category');+ const products = targets.filter((t) => t.kind === 'product');+ if (!targets.length) return '';+ const lines = [+ "The shopping path: the pages of the brand's shop this page sends readers to. Write each name exactly as given; the editor links it.",+ ];+ if (categories.length) {+ lines.push(+ `Category page: ${categories.map((c) => c.title).join('; ')}. Name it in the opening, and point readers to it again at the end.`,+ );+ }+ if (products.length) {+ lines.push(+ 'Products, in this order (recommend or compare them where the page does; one each at most where it fits):',+ ...products.map((p) => {+ const offer = offerLine(p.item);+ return `- ${p.title}${offer ? `: ${offer}` : ''}`;+ }),+ "About these products, write only what is given here and in your data, a price as given (its currency's sign may stand for its code). Never write a price, size, weight or other spec that isn't; write a placeholder in square brackets instead.",+ );+ }+ return lines.join('\n');+}++/**+ * What the path gives the guards (content-sources.js, content-people.js):+ * its prices, each way one may be written, known anywhere on the page; its+ * names, which a name may be, and whose own numbers ("Trail 450") are known+ * only where the name is written (withNamedNumbers).+ */+export function shoppingGiven(path) {+ const targets = pathTargets(path);+ return {+ prices: targets+ .filter((t) => t.kind === 'product')+ .flatMap((t) => priceSpellings(t.item))+ .join(' '),+ names: targets.map((t) => (t.kind === 'category' ? t.item.name : t.title)),+ };+}++const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');+const escapeHtml = (text) =>+ text.replace(+ /[&<>"']/g,+ (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c],+ );++/** A word's plural, roughly: boot → boots, glass → glasses, box → boxes (category-pages.js singular's other way). */+const plural = (word) => (/(ss|x|z|ch|sh)$/.test(word) ? `${word}es` : `${word}s`);++/**+ * The ways a name may be written, as `readable` reads them: a product's as+ * given; a category's folded, its last word singular or plural too+ * ("Reading Glasses": "reading glass").+ */+function spellings(target) {+ const name = nameKey(target.title, target.kind);+ if (target.kind !== 'category') return [name];+ const words = name.split(' ');+ const last = words.at(-1);+ if (!/^\p{L}+$/u.test(last)) return [name];+ const one = singular(last);+ return [name, [...words.slice(0, -1), one !== last ? one : plural(last)].join(' ')];+}++/**+ * The page's HTML with the path's names linked: each target the first time+ * one of its spellings appears in a paragraph or a list item's text, outside+ * a link and a heading; the products first, the longest names first, so a+ * category's name inside a product's isn't taken. A product's name is read+ * as written, a category's in any case. Returns the HTML and how many of+ * each kind it linked.+ */+export function linkShoppingPath(html, path) {+ const targets = shoppingTargets(path).sort(+ (a, b) =>+ (a.kind === 'product' ? 0 : 1) - (b.kind === 'product' ? 0 : 1) ||+ b.title.length - a.title.length,+ );+ const linked = { category: 0, product: 0 };+ if (!targets.length || !html) return { html, linked };+ const $ = cheerio.load(html, null, false);+ const textNodes = () => {+ const out = [];+ $('p, li').each((_, el) => {+ if ($(el).parents('h1, h2, h3, h4, a').length) return;+ $(el)+ .contents()+ .each((__, node) => {+ if (node.type === 'text') out.push(node);+ });+ });+ return out;+ };+ for (const target of targets) {+ const fold = target.kind === 'category';+ const pattern = new RegExp(+ `(?<![\\p{L}\\p{N}])(?:${spellings(target).map(escape).join('|')})(?![\\p{L}\\p{N}])`,+ 'u',+ );+ for (const node of textNodes()) {+ const text = node.data ?? '';+ const read = readable(text, fold);+ const m = pattern.exec(read.text);+ if (!m) continue;+ const start = read.from[m.index];+ const end = read.to[m.index + m[0].length - 1];+ $(node).replaceWith(+ `${escapeHtml(text.slice(0, start))}<a href="${escapeHtml(target.url)}">${escapeHtml(text.slice(start, end))}</a>${escapeHtml(text.slice(end))}`,+ );+ linked[target.kind] += 1;+ break;+ }+ }+ return { html: $.html(), linked };+}diff --git a/server/src/lib/shopping-links.test.js b/server/src/lib/shopping-links.test.jsnew file mode 100644index 000000000..bae7dbbf3--- /dev/null+++ b/server/src/lib/shopping-links.test.js@@ -0,0 +1,539 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++vi.mock('../config/supabase.js', () => ({ default: {} }));+const logger = vi.hoisted(() => ({ warn: vi.fn(), info: vi.fn(), error: vi.fn() }));+vi.mock('./logger.js', () => ({ logger }));++import { linkAllowList, linkTargetsPrompt } from './content-links.js';+import { knownNumbers, placeholderNumbers, withNamedNumbers } from './content-sources.js';+import {+ linkShoppingPath,+ shoppingGiven,+ shoppingPathFor,+ shoppingPrompt,+ shoppingTargets,+ stockWords,+ withShopping,+} from './shopping-links.js';++/** The Content Agent's side of a draft's shopping path (shopping path, part 4). */++const product = (id, title, extra = {}) => ({+ id,+ title,+ url: `https://shop.example/products/${id}?utm_source=feed`,+ price: 189,+ salePrice: null,+ currency: 'USD',+ availability: 'in_stock',+ image: null,+ issues: [],+ score: 9,+ ...extra,+});++const PATH = {+ version: 1,+ builtAt: '2026-10-27T10:00:00.000Z',+ pageType: 'buying_guide',+ categories: [+ {+ name: 'Shoes > Hiking Boots',+ url: 'https://shop.example/collections/hiking-boots',+ title: null,+ },+ ],+ products: [+ product('p1', 'Summit GTX Boot'),+ product('p2', 'Trail Mid', {+ price: 149,+ salePrice: 129,+ availability: 'limited_availability',+ }),+ ],+ leftOut: [],+ locked: [],+ removed: [],+ added: [],+};++beforeEach(() => {+ logger.warn.mockReset();+});++describe('shoppingTargets', () => {+ it('names the category page by its own name and each product by its title, tracking dropped', () => {+ expect(shoppingTargets(PATH)).toEqual([+ {+ url: 'https://shop.example/collections/hiking-boots',+ title: 'Hiking Boots',+ kind: 'category',+ },+ { url: 'https://shop.example/products/p1', title: 'Summit GTX Boot', kind: 'product' },+ { url: 'https://shop.example/products/p2', title: 'Trail Mid', kind: 'product' },+ ]);+ expect(shoppingTargets(null)).toEqual([]);+ });++ it('links https pages only, each name and address once', () => {+ const path = {+ ...PATH,+ products: [+ product('p1', 'Summit GTX Boot'),+ product('p2', 'Summit GTX Boot'),+ product('p3', 'Trail Mid', { url: 'http://shop.example/products/p3' }),+ product('p4', 'Other name', { url: 'https://shop.example/products/p1' }),+ product('p5', 'Ridge Low'),+ ],+ };+ expect(shoppingTargets(path).map((t) => t.title)).toEqual([+ 'Hiking Boots',+ 'Summit GTX Boot',+ 'Ridge Low',+ ]);+ });++ it("offers as many products as the page type links, the person's own all kept", () => {+ const many = Array.from({ length: 12 }, (_, i) => product(`p${i}`, `Boot ${i}`));+ const titles = (path) => shoppingTargets(path).filter((t) => t.kind === 'product');+ // A buying guide links 5.+ expect(titles({ ...PATH, products: many })).toHaveLength(5);+ // Six locked: all six, and no other.+ const locked = many.slice(0, 6).map((p) => p.id);+ expect(titles({ ...PATH, products: many, locked }).map((t) => t.title)).toEqual(+ many.slice(0, 6).map((p) => p.title),+ );+ // Two added: counted in the five.+ expect(titles({ ...PATH, products: many, added: ['p10', 'p11'] }).map((t) => t.title)).toEqual([+ 'Boot 0',+ 'Boot 1',+ 'Boot 2',+ 'Boot 10',+ 'Boot 11',+ ]);+ // A page type the table doesn't have: 40 at most.+ expect(titles({ ...PATH, pageType: null, products: many })).toHaveLength(12);+ });+});++describe('stockWords', () => {+ it('says only the stock the catalog reads, and nothing for one it cannot', () => {+ expect(stockWords('in_stock')).toBe('in stock');+ expect(stockWords('In Stock')).toBe('in stock');+ expect(stockWords('limited_availability')).toBe('low stock');+ expect(stockWords('out_of_stock')).toBe('out of stock');+ expect(stockWords('OUT OF STOCK')).toBe('out of stock');+ expect(stockWords('preorder')).toBe('available to pre-order');+ expect(stockWords('backorder')).toBe('on backorder');+ expect(stockWords('discontinued')).toBeNull();+ expect(stockWords('constructor')).toBeNull();+ expect(stockWords(null)).toBeNull();+ });+});++describe('shoppingPrompt', () => {+ it("tells a new page's writer the category and the products with the catalog's price and stock only", () => {+ const prompt = shoppingPrompt(PATH);+ expect(prompt).toContain('Category page: Hiking Boots.');+ expect(prompt).toContain('- Summit GTX Boot: 189.00 USD, in stock');+ expect(prompt).toContain('- Trail Mid: 129.00 USD, low stock');+ expect(prompt).toContain("Never write a price, size, weight or other spec that isn't");+ expect(prompt).toContain('a price as given');+ expect(shoppingPrompt({ categories: [], products: [] })).toBe('');+ });++ it('never says a product out of stock or on pre-order is in stock, nor any stock it cannot read', () => {+ const prompt = shoppingPrompt({+ ...PATH,+ products: [+ product('p1', 'Summit GTX Boot', { availability: 'out_of_stock' }),+ product('p2', 'Trail Mid', { availability: 'preorder' }),+ product('p3', 'Ridge Low', { availability: 'discontinued' }),+ product('p4', 'Canyon Sandal', { price: null, availability: '' }),+ ],+ });+ expect(prompt).toContain('- Summit GTX Boot: 189.00 USD, out of stock');+ expect(prompt).toContain('- Trail Mid: 189.00 USD, available to pre-order');+ expect(prompt).toContain('- Ridge Low: 189.00 USD\n');+ expect(prompt).toContain('- Canyon Sandal\n');+ expect(prompt).not.toMatch(/in stock/);+ });+});++describe('shoppingGiven and the number guard', () => {+ const guard = (path, text) => {+ const given = shoppingGiven(path);+ return placeholderNumbers(text, withNamedNumbers(knownNumbers(given.prices), given.names));+ };++ it("keeps the catalog's price written with a sign, a code or none, and only it", () => {+ for (const text of [+ 'It costs $189.',+ 'It costs $189.00.',+ 'It costs 189 USD.',+ 'It costs US$189.',+ 'It costs $129, down from [figure, source].',+ ]) {+ expect(guard(PATH, text)).toBe(text);+ }+ expect(guard(PATH, 'It costs $199.')).toBe('It costs [figure, source].');+ // A sign the currency doesn't take is a claim of its own.+ expect(guard(PATH, 'It costs €189.')).toBe('It costs [figure, source].');+ });++ it('reads a price written with thousands, as Turkish writes it', () => {+ const path = {+ ...PATH,+ products: [product('p1', 'Kışlık Bot', { price: 1299, currency: 'TRY' })],+ };+ for (const text of ['Fiyatı ₺1.299.', 'Fiyatı ₺1.299,00.', 'Fiyatı 1.299 TL.']) {+ expect(guard(path, text)).toBe(text);+ }+ expect(guard(path, 'Fiyatı ₺1.399.')).toBe('Fiyatı [figure, source].');+ });++ it("knows a product's own number only where its name is written", () => {+ const path = { ...PATH, products: [product('p1', "Men's Trail 450")] };+ expect(guard(path, 'The Men’s Trail 450 is light.')).toBe('The Men’s Trail 450 is light.');+ expect(guard(path, 'It weighs 450 g.')).toBe('It weighs [figure, source] g.');+ // Not inside a longer number.+ expect(guard(path, "The Men's Trail 4500 is new.")).toBe(+ "The Men's Trail [figure, source] is new.",+ );+ });++ it('gives the names the people guard reads: the categories whole, the products', () => {+ expect(shoppingGiven(PATH).names).toEqual([+ 'Shoes > Hiking Boots',+ 'Summit GTX Boot',+ 'Trail Mid',+ ]);+ expect(shoppingGiven(null)).toEqual({ prices: '', names: [] });+ });+});++describe('linkShoppingPath', () => {+ it('links a long paragraph, mostly spaces, in time near its length', () => {+ const html = `<p>${'word '.repeat(40_000)} the Trail Mid.</p><p>${' '.repeat(200_000)}hiking boots</p>`;+ const started = Date.now();+ const { linked } = linkShoppingPath(html, PATH);+ expect(Date.now() - started).toBeLessThan(2_000);+ expect(linked).toEqual({ category: 1, product: 1 });+ });++ it('links each name the first time a paragraph or a list item names it, never a heading', () => {+ const html =+ '<h1>Hiking boots</h1><p>Good hiking boots keep you dry. More hiking boots below.</p>' ++ '<h2>Summit GTX Boot</h2><ul><li><p>The Summit GTX Boot is light & dry.</p></li></ul>' ++ '<p>See the <a href="https://x.example">Trail Mid</a> and the Trail Mid again.</p>';+ const { html: out, linked } = linkShoppingPath(html, PATH);+ expect(linked).toEqual({ category: 1, product: 2 });+ expect(out).toContain('<h1>Hiking boots</h1>');+ expect(out).toContain(+ '<p>Good <a href="https://shop.example/collections/hiking-boots">hiking boots</a> keep you dry. More hiking boots below.</p>',+ );+ expect(out).toContain('<h2>Summit GTX Boot</h2>');+ expect(out).toContain(+ 'The <a href="https://shop.example/products/p1">Summit GTX Boot</a> is light & dry.',+ );+ // Not inside a link already there: the next time it's named.+ expect(out).toContain(+ '<a href="https://x.example">Trail Mid</a> and the <a href="https://shop.example/products/p2">Trail Mid</a> again.',+ );+ });++ it("takes a category's singular, and leaves a product's own words to the product", () => {+ const path = {+ ...PATH,+ products: [product('p3', 'Hiking Boots Care Kit')],+ };+ const { html, linked } = linkShoppingPath(+ '<p>The Hiking Boots Care Kit cleans any hiking boot.</p>',+ path,+ );+ expect(linked).toEqual({ category: 1, product: 1 });+ expect(html).toBe(+ '<p>The <a href="https://shop.example/products/p3">Hiking Boots Care Kit</a> cleans any <a href="https://shop.example/collections/hiking-boots">hiking boot</a>.</p>',+ );+ });++ it('links a whole name only: never the start of a longer word', () => {+ const { html, linked } = linkShoppingPath(+ '<p>A Trail Midsole and hiking bootstraps. Then the Trail Mid.</p>',+ PATH,+ );+ expect(linked).toEqual({ category: 0, product: 1 });+ expect(html).toBe(+ '<p>A Trail Midsole and hiking bootstraps. Then the <a href="https://shop.example/products/p2">Trail Mid</a>.</p>',+ );+ });++ it("reads a product's name as written: a common word in lower case isn't the product", () => {+ const path = { ...PATH, categories: [], products: [product('p9', 'Pro')] };+ const { html } = linkShoppingPath('<p>Here is a pro tip.</p><p>The Pro is light.</p>', path);+ expect(html).toBe(+ '<p>Here is a pro tip.</p><p>The <a href="https://shop.example/products/p9">Pro</a> is light.</p>',+ );+ });++ it("reads a name's apostrophes and spaces any way", () => {+ const path = { ...PATH, categories: [], products: [product('p9', "Men's Trail Boot")] };+ const { html } = linkShoppingPath('<p>The Men’s Trail Boot is light.</p>', path);+ expect(html).toBe(+ '<p>The <a href="https://shop.example/products/p9">Men’s Trail Boot</a> is light.</p>',+ );+ });++ it("reads a category in any case, Turkish's dotted and dotless i too", () => {+ const path = {+ ...PATH,+ categories: [+ { name: 'Giyim > İç Giyim', url: 'https://shop.example/kategori/ic-giyim', title: null },+ { name: 'Kadın Ayakkabı', url: 'https://shop.example/kategori/kadin', title: null },+ ],+ products: [],+ };+ const { html, linked } = linkShoppingPath(+ '<p>Rahat iç giyim seçin.</p><p>KADIN AYAKKABI modelleri.</p>',+ path,+ );+ expect(linked).toEqual({ category: 2, product: 0 });+ expect(html).toBe(+ '<p>Rahat <a href="https://shop.example/kategori/ic-giyim">iç giyim</a> seçin.</p>' ++ '<p><a href="https://shop.example/kategori/kadin">KADIN AYAKKABI</a> modelleri.</p>',+ );+ });++ it("takes a plural's singular as English writes it: glasses, glass", () => {+ const path = {+ ...PATH,+ categories: [+ { name: 'Reading Glasses', url: 'https://shop.example/c/reading-glasses', title: null },+ ],+ products: [],+ };+ expect(linkShoppingPath('<p>Pick a reading glass.</p>', path).html).toBe(+ '<p>Pick a <a href="https://shop.example/c/reading-glasses">reading glass</a>.</p>',+ );+ expect(linkShoppingPath('<p>Pick reading glasse frames.</p>', path).linked.category).toBe(0);+ });++ it('leaves a page with no path, or no names, as it is', () => {+ expect(linkShoppingPath('<p>Boots.</p>', null)).toEqual({+ html: '<p>Boots.</p>',+ linked: { category: 0, product: 0 },+ });+ expect(linkShoppingPath('<p>Nothing here.</p>', PATH).linked).toEqual({+ category: 0,+ product: 0,+ });+ });+});++describe('the shopping path as link targets', () => {+ it('comes first in the prompt whatever the rules, and its pages may be linked', () => {+ const targets = withShopping(+ { own: [{ url: 'https://shop.example/about', title: 'About' }], sources: [] },+ PATH,+ );+ const prompt = linkTargetsPrompt(targets, new Set());+ expect(prompt).toContain('The shopping path');+ expect(prompt).toContain('- Summit GTX Boot — https://shop.example/products/p1');+ expect(prompt).not.toContain('About');+ const allow = linkAllowList(targets);+ expect(allow.get('shop.example/products/p1')).toBe('https://shop.example/products/p1');+ expect(allow.get('shop.example/collections/hiking-boots')).toBeDefined();+ });+});++describe('shoppingPathFor', () => {+ /** A database whose reads answer each table's rows, or its error; `asked` lists them. */+ const fakeDb = (tables) => {+ const asked = [];+ const db = {+ asked,+ from: (table) => {+ asked.push(table);+ const answer = tables[table];+ const chain = {};+ for (const name of ['select', 'eq', 'in', 'is', 'order', 'range', 'limit']) {+ chain[name] = () => chain;+ }+ chain.then = (resolve, reject) =>+ Promise.resolve(+ answer instanceof Error+ ? { data: null, error: answer }+ : { data: answer ?? [], error: null },+ ).then(resolve, reject);+ return chain;+ },+ };+ return db;+ };+ const draft = (path) => ({ id: 'd1', brand_id: 'b1', shopping_path: path });++ it('leaves out a name longer than the build keeps', async () => {+ const db = fakeDb({+ brand_domains: [{ domain: 'shop.example' }],+ catalog_variants: [],+ });+ const path = {+ ...PATH,+ categories: [{ ...PATH.categories[0], name: `Shoes > ${'Boot '.repeat(10_000)}` }],+ products: [product('p1', 'Summit GTX Boot'), product('p2', 'Trail '.repeat(10_000))],+ };+ const read = await shoppingPathFor(draft(path), { db });+ expect(read.categories).toEqual([]);+ expect(read.products.map((p) => p.id)).toEqual(['p1']);+ });++ it('reads a broken path as none, logged, without reading the catalog', async () => {+ const db = fakeDb({});+ for (const broken of [+ { ...PATH, categories: { name: 'Boots' } },+ { ...PATH, products: [null] },+ 'not a path',+ ]) {+ expect(await shoppingPathFor(draft(broken), { db })).toBeNull();+ }+ expect(logger.warn).toHaveBeenCalledTimes(3);+ expect(await shoppingPathFor(draft(null), { db })).toBeNull();+ expect(db.asked).toEqual([]);+ });++ it("keeps the pages on the brand's own site over https only", async () => {+ const path = {+ ...PATH,+ categories: [+ ...PATH.categories,+ { name: 'Sale', url: 'https://evil.example/collections/sale', title: null },+ ],+ products: [+ product('p1', 'Summit GTX Boot'),+ product('p2', 'Trail Mid', { url: 'http://shop.example/products/p2' }),+ product('p3', 'Ridge Low', { url: 'https://eu.shop.example/products/p3' }),+ product('p4', 'Canyon Sandal', { url: 'https://shop.example.evil.example/p4' }),+ ],+ };+ const db = fakeDb({+ brand_domains: [{ domain: 'shop.example' }],+ catalog_variants: [+ { id: 'v1', product_id: 'p1', availability: 'in stock' },+ { id: 'v3', product_id: 'p3', availability: 'in_stock' },+ ],+ });+ const read = await shoppingPathFor(draft(path), { db });+ expect(read.categories.map((c) => c.name)).toEqual(['Shoes > Hiking Boots']);+ expect(read.products.map((p) => p.id)).toEqual(['p1', 'p3']);+ expect(logger.warn).toHaveBeenCalledWith(+ { draftId: 'd1', off: 3 },+ expect.stringContaining('off the brand'),+ );+ });++ it('with no domain stored, takes the site its catalog links to', async () => {+ const db = fakeDb({+ brand_domains: [],+ catalog_products: [{ url: 'https://shop.example/products/x' }],+ catalog_variants: [+ { id: 'v1', product_id: 'p1', availability: 'in stock', price: 189, currency: 'USD' },+ { id: 'v2', product_id: 'p2', availability: 'in stock', price: 189, currency: 'USD' },+ ],+ });+ const read = await shoppingPathFor(draft(PATH), { db });+ expect(read.products.map((p) => p.id)).toEqual(['p1', 'p2']);+ expect(db.asked).toContain('catalog_products');+ });++ it("links nothing when the brand's site can't be read", async () => {+ const db = fakeDb({ brand_domains: new Error('down') });+ expect(await shoppingPathFor(draft(PATH), { db })).toBeNull();+ });++ it("tells the stock the catalog has now: its live variants' best, none when none reads", async () => {+ const path = {+ ...PATH,+ products: [+ product('p1', 'Summit GTX Boot'),+ product('p2', 'Trail Mid'),+ product('p3', 'Ridge Low'),+ product('p4', 'Canyon Sandal'),+ ],+ };+ const db = fakeDb({+ brand_domains: [{ domain: 'shop.example' }],+ catalog_variants: [+ // Sold out since the path was built.+ { id: 'v1', product_id: 'p1', availability: 'out of stock', price: 189, currency: 'USD' },+ // One size low, another sold out: low stock, at the low one's price.+ { id: 'v2', product_id: 'p2', availability: 'OUT_OF_STOCK', price: 149, currency: 'USD' },+ {+ id: 'v3',+ product_id: 'p2',+ availability: 'limited availability',+ price: 199,+ currency: 'USD',+ },+ // A spelling it can't read.+ { id: 'v4', product_id: 'p3', availability: 'ask us', price: 189, currency: 'USD' },+ ],+ });+ const read = await shoppingPathFor(draft(path), { db });+ expect(read.products.map((p) => [p.id, p.availability])).toEqual([+ ['p1', 'out_of_stock'],+ ['p2', 'limited_availability'],+ ['p3', ''],+ ['p4', ''],+ ]);+ const prompt = shoppingPrompt(read);+ expect(prompt).toContain('- Summit GTX Boot: 189.00 USD, out of stock');+ expect(prompt).toContain('- Trail Mid: 199.00 USD, low stock');+ expect(prompt).toContain('- Ridge Low: 189.00 USD\n');+ // Gone from the catalog: nothing is said of its price or stock.+ expect(prompt).toContain('- Canyon Sandal\n');+ });++ it("tells the price the catalog has now, not the path's, from the variant whose stock it tells", async () => {+ const db = fakeDb({+ brand_domains: [{ domain: 'shop.example' }],+ catalog_variants: [+ // Its sale ended since the pick, and its price rose.+ { id: 'v1', product_id: 'p1', availability: 'in stock', price: 209, currency: 'USD' },+ // In stock at two prices: the lower one.+ { id: 'v2', product_id: 'p2', availability: 'in stock', price: 179, currency: 'USD' },+ {+ id: 'v3',+ product_id: 'p2',+ availability: 'in stock',+ price: 199,+ sale_price: 159,+ currency: 'USD',+ },+ ],+ });+ const path = {+ ...PATH,+ products: [product('p1', 'Summit GTX Boot', { salePrice: 149 }), product('p2', 'Trail Mid')],+ };+ const read = await shoppingPathFor(draft(path), { db });+ const prompt = shoppingPrompt(read);+ expect(prompt).toContain('- Summit GTX Boot: 209.00 USD, in stock');+ expect(prompt).toContain('- Trail Mid: 159.00 USD, in stock');+ const { prices } = shoppingGiven(read);+ expect(prices).toContain('$209');+ expect(prices).not.toContain('149');+ });++ it("keeps the path's stock when the catalog can't be read", async () => {+ const db = fakeDb({+ brand_domains: [{ domain: 'shop.example' }],+ catalog_variants: new Error('down'),+ });+ const read = await shoppingPathFor(draft(PATH), { db });+ expect(read.products.map((p) => p.availability)).toEqual(['in_stock', 'limited_availability']);+ expect(logger.warn).toHaveBeenCalledWith(+ expect.objectContaining({ draftId: 'd1' }),+ expect.stringContaining('reading stock failed'),+ );+ });+});