Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0185ukXgk6UunFCKyXqLe2Cq
80 lines
5.1 KiB
TypeScript
80 lines
5.1 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import type { SitesmithResponse, SerializedTreeNode } from '../types/sitesmith';
|
|
import { pruneDeadLinks, emptiedBandsNote, findContactEmail } from './prune-dead-links';
|
|
import { planReplace } from './replace-plan';
|
|
|
|
const n = (resolvedName: string, props: Record<string, unknown> = {}, nodes?: SerializedTreeNode[]): SerializedTreeNode => ({ type: { resolvedName }, props, ...(nodes ? { nodes } : {}) });
|
|
const site = (...trees: SerializedTreeNode[]): Extract<SitesmithResponse, { type: 'replace' }> => ({ type: 'replace', scope: 'site', message: '', pages: trees.map((t, i) => ({ name: i ? `P${i}` : 'Home', tree: t })) });
|
|
const band = (extra: Record<string, unknown> = {}) => n('CallToAction', { heading: 'Ready?', ctas: [{ text: 'Contact us', href: '/contact', variant: 'primary' }], ...extra });
|
|
const ctasOf = (r: SitesmithResponse) => ((r as any).pages[0].tree.nodes[0].props.ctas);
|
|
|
|
describe('pruneDeadLinks', () => {
|
|
it('turns the only CTA into a mailto link when a contact email is known', () => {
|
|
const r = pruneDeadLinks(site(n('Container', {}, [band({ text: 'Write to hello@acme.com' })])), { missing: ['Contact'] });
|
|
expect(ctasOf(r.resp)).toEqual([{ text: 'Contact us', href: 'mailto:hello@acme.com', variant: 'primary' }]);
|
|
expect(r.mailtoAdded).toBe(1);
|
|
expect(r.emptiedButtons).toBe(0);
|
|
});
|
|
it('finds the email in the brief / site context too', () => {
|
|
const r = pruneDeadLinks(site(n('Container', {}, [band()])), { missing: ['Contact'], context: ['Our email is owner@shop.org'] });
|
|
expect(ctasOf(r.resp)[0].href).toBe('mailto:owner@shop.org');
|
|
});
|
|
it('keeps the band and counts the removed button when no email is known', () => {
|
|
const r = pruneDeadLinks(site(n('Container', {}, [band()])), { missing: ['Contact'] });
|
|
expect(ctasOf(r.resp)).toEqual([]);
|
|
expect((r.resp as any).pages[0].tree.nodes[0].props.heading).toBe('Ready?');
|
|
expect(r.emptiedButtons).toBe(1);
|
|
expect(r.mailtoAdded).toBe(0);
|
|
expect(emptiedBandsNote(r.emptiedButtons)).toBe('1 call-to-action button was removed because its page was not built. The section kept its heading and text; add a button or contact link there if you want one.');
|
|
expect(emptiedBandsNote(2)).toMatch(/^2 call-to-action buttons were removed because their pages were not built/);
|
|
expect(emptiedBandsNote(0)).toBeNull();
|
|
});
|
|
it('leaves a band alone when another button survives, and ignores built pages and external links', () => {
|
|
const b = band({ ctas: [{ text: 'Contact', href: '/contact' }, { text: 'About', href: '/about.html' }, { text: 'Ext', href: 'https://x.com/contact' }] });
|
|
const r = pruneDeadLinks(site(n('Container', {}, [b])), { missing: ['Contact'] });
|
|
expect(ctasOf(r.resp).map((c: any) => c.text)).toEqual(['About', 'Ext']);
|
|
expect(r.emptiedButtons).toBe(0);
|
|
expect(r.removed).toBe(1);
|
|
});
|
|
it('handles a lone ButtonLink child in both branches', () => {
|
|
const mk = () => site(n('Container', {}, [n('Container', {}, [n('Heading', { text: 'Hi' }), n('ButtonLink', { text: 'Go', href: '/contact' })])]));
|
|
const withMail = pruneDeadLinks(mk(), { missing: ['Contact'], contactEmail: 'a@b.co' });
|
|
expect((withMail.resp as any).pages[0].tree.nodes[0].nodes[1].props.href).toBe('mailto:a@b.co');
|
|
const without = pruneDeadLinks(mk(), { missing: ['Contact'] });
|
|
expect((without.resp as any).pages[0].tree.nodes[0].nodes.map((x: any) => x.type.resolvedName)).toEqual(['Heading']);
|
|
expect(without.emptiedButtons).toBe(1);
|
|
});
|
|
it('rewrites legacy buttonText/buttonHref props', () => {
|
|
const t = n('Container', {}, [n('CTASection', { buttonText: 'Book', buttonHref: '/contact' })]);
|
|
const r = pruneDeadLinks(site(t), { missing: ['Contact'] });
|
|
expect((r.resp as any).pages[0].tree.nodes[0].props.buttonText).toBe('');
|
|
expect(r.emptiedButtons).toBe(1);
|
|
});
|
|
it('is a no-op without missing pages and does not mutate its input', () => {
|
|
const resp = site(n('Container', {}, [band()]));
|
|
const copy = JSON.stringify(resp);
|
|
expect(pruneDeadLinks(resp, { missing: [] }).resp).toBe(resp);
|
|
pruneDeadLinks(resp, { missing: ['Contact'] });
|
|
expect(JSON.stringify(resp)).toBe(copy);
|
|
});
|
|
it('findContactEmail prefers mailto links', () => {
|
|
expect(findContactEmail('x a@b.com', { href: 'mailto:z@y.org?subject=1' })).toBe('z@y.org');
|
|
expect(findContactEmail('nothing')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('planReplace partial path', () => {
|
|
const ctx = { userText: 'build the whole site', chromeDefault: false, pages: [], siteBuild: { planned: ['Home', 'Contact'], built: ['Home'], missing: ['Contact'] } };
|
|
it('surfaces a note when a band is emptied', () => {
|
|
const p = planReplace(site(n('Container', {}, [band()])), ctx);
|
|
expect(p.confirm).toBe('site-partial');
|
|
expect(p.note).toMatch(/1 call-to-action button was removed/);
|
|
expect(ctasOf(p.resp)).toEqual([]);
|
|
});
|
|
it('uses mailto and no note when the brief has an email', () => {
|
|
const p = planReplace(site(n('Container', {}, [band()])), { ...ctx, userText: 'build the whole site, email is me@x.io' });
|
|
expect(p.note).toBeUndefined();
|
|
expect(ctasOf(p.resp)[0].href).toBe('mailto:me@x.io');
|
|
});
|
|
});
|