Skip to content
← Public packages

@kentcdodds/photo-portfolio

A fast, photo-first portfolio site you update by chatting with your agent: galleries, about, contact, and one-step rollback.

FollowFork

Files

content.test.ts

219 lines · 6.1 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import {
	addGallery,
	addPhotos,
	diffContent,
	featuredGalleries,
	removeGallery,
	removePhoto,
	reorderGalleries,
	slugify,
	storedPhotoIds,
	updateGallery,
} from '../src/domain/content.ts'
import { defaultContent } from '../src/domain/defaults.ts'
import { emptyContent, storedPhoto } from './fixtures.ts'

const now = '2026-10-10T18:00:00.000Z'

function withGallery(
	title: string,
	ids: Array<string>,
	content = emptyContent,
	extra: { slug?: string; position?: 'start' | 'end' } = {},
) {
	return addGallery(
		content,
		{
			id: `g-${title}`,
			title,
			featured: true,
			photos: ids.map((id) => storedPhoto(id)),
			cover: 0,
			position: extra.position ?? 'end',
			slug: extra.slug,
		},
		now,
	).content
}

test('slugify makes readable URL slugs', () => {
	assert.equal(slugify('Smith Family, Fall 2026'), 'smith-family-fall-2026')
	assert.equal(slugify('Mountains & Mist'), 'mountains-and-mist')
	assert.equal(slugify('Café Portraits'), 'cafe-portraits')
	assert.equal(slugify('!!!'), 'gallery')
})

test('addGallery picks a unique slug, keeps the cover, and respects position', () => {
	let content = withGallery('Weddings', ['a', 'b'])
	content = withGallery('Weddings', ['c'], content, { position: 'start' })
	assert.deepEqual(
		content.galleries.map((g) => g.slug),
		['weddings-2', 'weddings'],
	)
	assert.equal(content.galleries[1]!.coverPhotoId, 'a')
	assert.throws(
		() => withGallery('Other', ['d'], content, { slug: 'weddings' }),
		/already uses the slug "weddings"/,
	)
	assert.throws(
		() => withGallery('Other', ['d'], content, { slug: 'Bad Slug' }),
		/not a valid slug/,
	)
	assert.throws(() => withGallery('Empty', []), /at least one photo/)
})

test('updateGallery edits captions, order, cover, and slug without touching other galleries', () => {
	let content = withGallery('Weddings', ['a', 'b', 'c'])
	content = withGallery('Portraits', ['p'], content)
	const result = updateGallery(
		content,
		'weddings',
		{
			slug: 'wedding-stories',
			description: 'Days to remember',
			coverPhotoId: 'c',
			photoOrder: ['c', 'a'],
			photos: [{ id: 'a', caption: 'First look', alt: 'Bride and groom' }],
		},
		now,
	)
	const gallery = result.gallery
	assert.equal(gallery.slug, 'wedding-stories')
	assert.deepEqual(
		gallery.photos.map((p) => p.id),
		['c', 'a', 'b'],
	)
	assert.equal(gallery.coverPhotoId, 'c')
	assert.equal(gallery.photos[1]!.caption, 'First look')
	assert.equal(gallery.photos[1]!.alt, 'Bride and groom')
	assert.equal(
		result.content.galleries[1],
		content.galleries[1],
		'other galleries are untouched',
	)
	const cleared = updateGallery(
		result.content,
		'wedding-stories',
		{ description: null, photos: [{ id: 'a', caption: null }] },
		now,
	)
	assert.equal(cleared.gallery.description, undefined)
	assert.equal(cleared.gallery.photos[1]!.caption, undefined)
	assert.throws(
		() => updateGallery(content, 'weddings', { coverPhotoId: 'zzz' }, now),
		/no photo "zzz"/,
	)
	assert.throws(
		() => updateGallery(content, 'weddings', { photoOrder: ['a', 'a'] }, now),
		/listed twice/,
	)
	assert.throws(
		() => updateGallery(content, 'nope', {}, now),
		/No gallery "nope". Known galleries: weddings, portraits/,
	)
})

test('reorder, add/remove photos, and remove galleries', () => {
	let content = withGallery('A', ['a1'])
	content = withGallery('B', ['b1'], content)
	content = withGallery('C', ['c1'], content)
	content = reorderGalleries(content, ['c', 'a'], now)
	assert.deepEqual(
		content.galleries.map((g) => g.slug),
		['c', 'a', 'b'],
	)
	assert.throws(
		() => reorderGalleries(content, ['zzz'], now),
		/Unknown gallery "zzz"/,
	)

	content = addPhotos(
		content,
		'a',
		[storedPhoto('a2'), storedPhoto('a3')],
		'end',
		now,
	).content
	const removed = removePhoto(content, 'a', 'a1', now)
	assert.equal(
		removed.gallery.coverPhotoId,
		'a2',
		'cover moves to the next photo',
	)
	assert.throws(() => removePhoto(content, 'b', 'b1', now), /would be empty/)

	content = removeGallery(removed.content, 'b', now).content
	assert.deepEqual(
		content.galleries.map((g) => g.slug),
		['c', 'a'],
	)
})

test('storedPhotoIds covers galleries and the headshot but not bundled samples', () => {
	assert.equal(storedPhotoIds(defaultContent).size, 0)
	const content = withGallery('A', ['a1', 'a2'], defaultContent)
	const withHeadshot = {
		...content,
		config: { ...content.config, headshot: storedPhoto('me') },
	}
	assert.deepEqual([...storedPhotoIds(withHeadshot)].sort(), ['a1', 'a2', 'me'])
})

test('featuredGalleries falls back to the first six when none are featured', () => {
	const none = {
		...defaultContent,
		galleries: defaultContent.galleries.map((g) => ({ ...g, featured: false })),
	}
	assert.equal(featuredGalleries(none).length, 3)
	const one = {
		...none,
		galleries: none.galleries.map((g, i) => ({ ...g, featured: i === 1 })),
	}
	assert.deepEqual(
		featuredGalleries(one).map((g) => g.slug),
		['mountains-and-mist'],
	)
})

test('diffContent summarizes what a publish would change', () => {
	const next = updateGallery(
		withGallery('New', ['n1'], defaultContent),
		'golden-hour',
		{ title: 'Golden' },
		now,
	).content
	const reordered = reorderGalleries(
		{ ...next, config: { ...next.config, tagline: 'Hi' } },
		['people-and-places'],
		now,
	)
	assert.deepEqual(diffContent(defaultContent, reordered), {
		hasChanges: true,
		configFields: ['tagline'],
		galleriesAdded: ['new'],
		galleriesRemoved: [],
		galleriesChanged: ['golden-hour'],
		orderChanged: true,
	})
	assert.equal(diffContent(defaultContent, defaultContent).hasChanges, false)
})

test('the bundled sample galleries are complete and self-consistent', () => {
	for (const gallery of defaultContent.galleries) {
		assert.ok(
			gallery.photos.some((p) => p.id === gallery.coverPhotoId),
			`${gallery.slug} cover exists`,
		)
		for (const photo of gallery.photos) {
			assert.equal(photo.source, 'bundled')
			assert.ok(photo.alt.length > 10, `${photo.id} has real alt text`)
			assert.ok(photo.credits?.artist, `${photo.id} is credited`)
			assert.ok(
				photo.variants.some((v) => v.format === 'jpeg'),
				`${photo.id} has a JPEG fallback`,
			)
		}
	}
})