Publish now runs client-side checks (image alt, meta description, title length, H1, empty links, contrast, noindex landing, empty page) over the current state and shows a grouped checklist with Fix links and "Publish anyway". Adds ExportOptions.canonicalUrl -> <link rel="canonical"> after the favicon line in both Preview and export. Refs #54
169 lines
9.4 KiB
TypeScript
169 lines
9.4 KiB
TypeScript
import { describe, test, expect } from 'vitest';
|
|
import { runPublishChecks, parseColor, contrastRatio, MAX_CHECKS, type CheckPage } from './publish-checks';
|
|
import { DEFAULT_SITE_DESIGN } from '../state/SiteDesignContext';
|
|
import { PLACEHOLDER_SRC } from '../components/media/ImageBlock';
|
|
|
|
type N = { type: string; props?: any; children?: string[] };
|
|
|
|
/** Builds a SerializedNodes JSON: ROOT -> given nodes (parent links derived). */
|
|
function state(nodes: Record<string, N>, rootChildren: string[] = Object.keys(nodes).filter((k) => !Object.values(nodes).some((n) => n.children?.includes(k)))): string {
|
|
const out: Record<string, any> = {
|
|
ROOT: { type: { resolvedName: 'Container' }, props: {}, nodes: rootChildren, linkedNodes: {}, parent: null },
|
|
};
|
|
for (const [id, n] of Object.entries(nodes)) {
|
|
const parent = rootChildren.includes(id) ? 'ROOT' : Object.keys(nodes).find((k) => nodes[k].children?.includes(id)) ?? 'ROOT';
|
|
out[id] = { type: { resolvedName: n.type }, props: n.props ?? {}, nodes: n.children ?? [], linkedNodes: {}, parent };
|
|
}
|
|
return JSON.stringify(out);
|
|
}
|
|
|
|
const goodSeo = { metaDescription: 'A fine page.' };
|
|
const page = (over: Partial<CheckPage> & { nodes?: Record<string, N> }): CheckPage => ({
|
|
id: 'p1', name: 'Home', slug: 'index', seo: goodSeo,
|
|
craftState: over.nodes ? state(over.nodes) : over.craftState ?? null,
|
|
...(over.id ? { id: over.id } : {}), ...(over.name ? { name: over.name } : {}), ...(over.slug ? { slug: over.slug } : {}),
|
|
...('seo' in over ? { seo: over.seo } : {}),
|
|
});
|
|
const h1: N = { type: 'Heading', props: { level: 'h1', text: 'Hi', style: {} } };
|
|
const run = (pages: CheckPage[], zones?: any[]) => runPublishChecks({ pages, design: DEFAULT_SITE_DESIGN, zones });
|
|
const ids = (cs: { id: string }[]) => cs.map((c) => c.id);
|
|
|
|
describe('runPublishChecks', () => {
|
|
test('a clean page yields nothing', () => {
|
|
expect(run([page({ nodes: { h: h1 } })])).toEqual([]);
|
|
});
|
|
|
|
test('image alt: real src + empty alt warns and targets the node; alt, placeholder, empty src do not', () => {
|
|
const cs = run([page({ nodes: {
|
|
h: h1,
|
|
bad: { type: 'ImageBlock', props: { src: 'assets/a.png', alt: '' } },
|
|
ok: { type: 'ImageBlock', props: { src: 'assets/a.png', alt: 'A cat' } },
|
|
ph: { type: 'ImageBlock', props: { src: PLACEHOLDER_SRC, alt: '' } },
|
|
none: { type: 'ImageBlock', props: { src: '', alt: '' } },
|
|
} })]);
|
|
expect(cs).toHaveLength(1);
|
|
expect(cs[0]).toMatchObject({ id: 'alt:p1:bad', severity: 'warn', pageId: 'p1', nodeId: 'bad', fix: 'select' });
|
|
});
|
|
|
|
test('meta description missing warns with page-settings fix; present does not', () => {
|
|
const cs = run([page({ nodes: { h: h1 }, seo: {} })]);
|
|
expect(cs).toEqual([expect.objectContaining({ id: 'meta-desc:p1', fix: 'page-settings', pageId: 'p1' })]);
|
|
expect(run([page({ nodes: { h: h1 }, seo: { metaDescription: ' ' } })])).toHaveLength(1);
|
|
});
|
|
|
|
test('title over 60 chars (metaTitle wins over name); exactly 60 is fine', () => {
|
|
const long = 'x'.repeat(61);
|
|
expect(ids(run([page({ nodes: { h: h1 }, seo: { ...goodSeo, metaTitle: long } })]))).toEqual(['title-long:p1']);
|
|
expect(ids(run([page({ nodes: { h: h1 }, name: long })]))).toEqual(['title-long:p1']);
|
|
expect(run([page({ nodes: { h: h1 }, seo: { ...goodSeo, metaTitle: 'x'.repeat(60) } })])).toEqual([]);
|
|
});
|
|
|
|
test('H1: none warns, two warns, HeroSimple counts as one, h2 does not count', () => {
|
|
const h2: N = { type: 'Heading', props: { level: 'h2', text: 'x', style: {} } };
|
|
expect(ids(run([page({ nodes: { a: h2, b: h2 } })]))).toEqual(['h1-none:p1']);
|
|
expect(ids(run([page({ nodes: { a: h1, b: h1 } })]))).toEqual(['h1-multi:p1']);
|
|
expect(run([page({ nodes: { a: { type: 'HeroSimple', props: { heading: 'Welcome', ctas: [{ text: 'Go', href: '/x' }] } }, b: h2 } })])).toEqual([]);
|
|
expect(ids(run([page({ nodes: { a: { type: 'HeroSimple', props: { heading: 'Welcome', ctas: [] } }, b: h1 } })]))).toEqual(['h1-multi:p1']);
|
|
});
|
|
|
|
test('links: ButtonLink, Navbar/Menu items and hero CTAs with # or empty href warn; real hrefs and #anchor do not', () => {
|
|
const cs = run([page({ nodes: {
|
|
h: h1,
|
|
b1: { type: 'ButtonLink', props: { text: 'Buy', href: '#' } },
|
|
b2: { type: 'ButtonLink', props: { text: 'Ok', href: '/shop' } },
|
|
b3: { type: 'ButtonLink', props: { text: 'Anchor', href: '#about' } },
|
|
nav: { type: 'Navbar', props: { links: [{ text: 'Home', href: '/' }, { text: 'Blog', href: '' }] } },
|
|
menu: { type: 'Menu', props: { links: [{ text: 'X', href: '#' }] } },
|
|
hero: { type: 'HeroSimple', props: { heading: 'Two', ctas: [{ text: 'Start', href: '#' }] } },
|
|
} })]);
|
|
const linkIds = ids(cs).filter((i) => i.startsWith('link:'));
|
|
expect(linkIds.sort()).toEqual(['link:p1:b1:btn', 'link:p1:hero:c0', 'link:p1:menu:l0', 'link:p1:nav:l1'].sort());
|
|
});
|
|
|
|
test('header/footer zones: links are checked, H1 and SEO are not', () => {
|
|
const cs = run([page({ nodes: { h: h1 } })], [
|
|
{ id: '__header__', name: 'Header', craftState: state({ n: { type: 'Navbar', props: { links: [{ text: 'Go', href: '#' }] } } }) },
|
|
{ id: '__footer__', name: 'Footer', craftState: null },
|
|
]);
|
|
expect(cs).toEqual([expect.objectContaining({ id: 'link:__header__:n:l0', pageId: '__header__', nodeId: 'n' })]);
|
|
});
|
|
|
|
test('noindex on the landing page is info; on a later page is not reported', () => {
|
|
const cs = run([
|
|
page({ nodes: { h: h1 }, seo: { ...goodSeo, noindex: true } }),
|
|
page({ id: 'p2', name: 'About', slug: 'about', nodes: { h: h1 }, seo: { ...goodSeo, noindex: true } }),
|
|
]);
|
|
expect(cs).toEqual([expect.objectContaining({ id: 'noindex-landing:p1', severity: 'info' })]);
|
|
});
|
|
|
|
test('empty page: ROOT with no children, or no saved state', () => {
|
|
expect(ids(run([page({ craftState: state({}) }), page({ id: 'p2', name: 'B', slug: 'b', craftState: null })]))).toEqual(['empty:p1', 'empty:p2']);
|
|
});
|
|
|
|
test('unparseable state is treated as empty, never throws', () => {
|
|
expect(ids(run([page({ craftState: '{nope' })]))).toEqual(['empty:p1']);
|
|
});
|
|
|
|
describe('contrast', () => {
|
|
const sec = (bg: string, child: N, extra: any = {}): Record<string, N> => ({
|
|
s: { type: 'Section', props: { style: { backgroundColor: bg }, ...extra }, children: ['t'] },
|
|
t: child,
|
|
h: h1,
|
|
});
|
|
const text = (color: string, fontSize = '16px'): N => ({ type: 'TextBlock', props: { text: 'Hello', style: { color, fontSize } } });
|
|
|
|
test('maths: #ffffff on #ffffff is 1:1, #1f2937 on #ffffff passes, black on white is 21', () => {
|
|
expect(contrastRatio(parseColor('#fff')!, parseColor('#ffffff')!)).toBeCloseTo(1, 5);
|
|
expect(contrastRatio(parseColor('#000')!, parseColor('#fff')!)).toBeCloseTo(21, 5);
|
|
expect(contrastRatio(parseColor('#1f2937')!, parseColor('#ffffff')!)).toBeGreaterThan(4.5);
|
|
});
|
|
|
|
test('white-on-white warns; dark-on-white does not', () => {
|
|
expect(ids(run([page({ nodes: sec('#ffffff', text('#ffffff')) })]))).toEqual(['contrast:p1:t']);
|
|
expect(run([page({ nodes: sec('#ffffff', text('#1f2937')) })])).toEqual([]);
|
|
});
|
|
|
|
test('threshold depends on size: ~3.5:1 fails at 16px but passes at 24px', () => {
|
|
// #767676 on white is 4.54; #8a8a8a is ~3.4
|
|
expect(ids(run([page({ nodes: sec('#ffffff', text('#8a8a8a', '16px')) })]))).toEqual(['contrast:p1:t']);
|
|
expect(run([page({ nodes: sec('#ffffff', text('#8a8a8a', '24px')) })])).toEqual([]);
|
|
expect(ids(run([page({ nodes: sec('#ffffff', text('#cccccc', '40px')) })]))).toEqual(['contrast:p1:t']);
|
|
});
|
|
|
|
test('token colours resolve through the design (text token on bg token)', () => {
|
|
const design = { ...DEFAULT_SITE_DESIGN, textColor: '#ffffff', backgroundColor: '#ffffff' };
|
|
const cs = runPublishChecks({ pages: [page({ nodes: sec('var(--wsb-bg)', text('var(--wsb-text)')) })], design });
|
|
expect(ids(cs)).toEqual(['contrast:p1:t']);
|
|
const ok = runPublishChecks({ pages: [page({ nodes: sec('var(--wsb-bg)', text('var(--wsb-text)')) })], design: { ...design, textColor: '#111111' } });
|
|
expect(ok).toEqual([]);
|
|
});
|
|
|
|
test('transparent ancestors defer to the next one up', () => {
|
|
const nodes: Record<string, N> = {
|
|
outer: { type: 'Section', props: { style: { backgroundColor: '#000000' } }, children: ['inner'] },
|
|
inner: { type: 'Container', props: { style: { backgroundColor: 'transparent' } }, children: ['t'] },
|
|
t: text('#ffffff'),
|
|
h: h1,
|
|
};
|
|
expect(run([page({ nodes })])).toEqual([]);
|
|
nodes.outer.props.style.backgroundColor = '#ffffff';
|
|
expect(ids(run([page({ nodes })]))).toEqual(['contrast:p1:t']);
|
|
});
|
|
|
|
test('unknown / gradient / image backgrounds and unparseable colours are skipped', () => {
|
|
expect(run([page({ nodes: sec('linear-gradient(#fff,#fff)', text('#ffffff')) })])).toEqual([]);
|
|
expect(run([page({ nodes: sec('#ffffff', text('#ffffff'), { bgImage: 'a.png' }) })])).toEqual([]);
|
|
expect(run([page({ nodes: sec('#ffffff', text('inherit')) })])).toEqual([]);
|
|
expect(run([page({ nodes: { t: text('#ffffff'), h: h1 } })])).toEqual([]);
|
|
});
|
|
});
|
|
|
|
test('cap and dedupe: 80 bad images produce MAX_CHECKS items', () => {
|
|
const nodes: Record<string, N> = { h: h1 };
|
|
for (let i = 0; i < 80; i++) nodes['i' + i] = { type: 'ImageBlock', props: { src: 'assets/a.png', alt: '' } };
|
|
const cs = run([page({ nodes })]);
|
|
expect(cs).toHaveLength(MAX_CHECKS);
|
|
expect(new Set(ids(cs)).size).toBe(MAX_CHECKS);
|
|
});
|
|
});
|