import assert from 'node:assert/strict' import { test } from 'node:test' import { addGallery } from '../src/domain/content.ts' import { applyConfigPatch } from '../src/domain/config.ts' import { defaultContent } from '../src/domain/defaults.ts' import { maxRetainedVersions, recordVersion, emptyVersionIndex, } from '../src/domain/versions.ts' import type { ProcessedFile } from '../src/images/process.ts' import { collectGarbage, discardDraft, getPreviewToken, pendingChanges, publishDraft, readDraft, readHostedUrl, readLive, readMediaFile, readVersionIndex, recordHostedUrl, rollbackTo, saveMedia, storageKeys, writeDraft, } from '../src/store/site-store.ts' import { storedPhoto } from './fixtures.ts' import { memoryStorage } from './memory-storage.ts' const now = '2026-10-10T18:00:00.000Z' function jpegFile(width: number, bytes: Uint8Array): ProcessedFile { return { variant: { width, height: width, format: 'jpeg', bytes: bytes.length }, bytes, } } async function draftWithGallery( storage: ReturnType, title: string, photoId: string, ) { await saveMedia(storage, photoId, [jpegFile(480, new Uint8Array([1, 2, 3]))]) const { content } = addGallery( await readDraft(storage), { id: `g-${photoId}`, title, featured: true, photos: [storedPhoto(photoId)], cover: 0, position: 'start', }, now, ) await writeDraft(storage, content) } test('a fresh fork serves the bundled defaults with no versions', async () => { const storage = memoryStorage() assert.deepEqual(await readLive(storage), defaultContent) assert.deepEqual(await readDraft(storage), defaultContent) assert.equal((await pendingChanges(storage)).hasChanges, false) assert.deepEqual(await readVersionIndex(storage), emptyVersionIndex) }) test('draft → publish → rollback round trip', async () => { const storage = memoryStorage() await draftWithGallery(storage, 'Smith Family', 'ph_one') assert.equal( (await readLive(storage)).galleries.length, 3, 'drafts do not touch the live site', ) assert.deepEqual((await pendingChanges(storage)).galleriesAdded, [ 'smith-family', ]) const first = await publishDraft(storage, { note: 'Smith family', now }) assert.equal(first.entry.version, 1) assert.equal((await readLive(storage)).galleries[0]!.slug, 'smith-family') assert.equal((await pendingChanges(storage)).hasChanges, false) const draft = await readDraft(storage) await writeDraft(storage, { ...draft, config: applyConfigPatch(draft.config, { tagline: 'New tagline' }), }) const second = await publishDraft(storage, { now }) assert.equal(second.entry.version, 2) assert.deepEqual(second.diff.configFields, ['tagline']) await draftWithGallery(storage, 'Unpublished', 'ph_two') const back = await rollbackTo(storage, { version: 1, now }) assert.equal(back.entry.version, 3) assert.equal(back.entry.kind, 'rollback') assert.equal(back.entry.restoredFrom, 1) const live = await readLive(storage) assert.equal(live.config.tagline, defaultContent.config.tagline) assert.deepEqual( await readDraft(storage), live, 'the draft resets to the restored version', ) assert.deepEqual( (await readVersionIndex(storage)).entries.map((e) => e.version), [3, 2, 1], ) await assert.rejects( rollbackTo(storage, { version: 9, now }), /Version 9 is not available. Available versions: 3, 2, 1/, ) }) test('media is chunked, readable, and garbage-collected once nothing references it', async () => { const storage = memoryStorage() const big = new Uint8Array(900 * 1024).map((_, i) => i % 251) await saveMedia(storage, 'ph_big', [jpegFile(2400, big)]) assert.ok( storage.keys().includes(storageKeys.mediaChunk('ph_big', '2400.jpg', 1)), 'split across chunks', ) const read = await readMediaFile(storage, 'ph_big', '2400.jpg') assert.deepEqual(read?.bytes, big) assert.equal(read?.contentType, 'image/jpeg') assert.equal(await readMediaFile(storage, 'ph_big', '960.jpg'), null) assert.deepEqual( await collectGarbage(storage), { deletedPhotos: 1 }, 'never referenced, so collected', ) assert.ok(!storage.keys().some((key) => key.startsWith('media/ph_big'))) }) test('discarding a draft drops its unpublished photos but keeps published ones', async () => { const storage = memoryStorage() await draftWithGallery(storage, 'Kept', 'ph_kept') await publishDraft(storage, { now }) await draftWithGallery(storage, 'Dropped', 'ph_dropped') assert.deepEqual(await discardDraft(storage), { deletedPhotos: 1 }) assert.ok(await readMediaFile(storage, 'ph_kept', '480.jpg')) assert.equal(await readMediaFile(storage, 'ph_dropped', '480.jpg'), null) assert.equal((await pendingChanges(storage)).hasChanges, false) }) test('only the newest versions are retained', async () => { let index = emptyVersionIndex const pruned: Array = [] for (let i = 0; i < maxRetainedVersions + 2; i += 1) { const result = recordVersion(index, defaultContent, { kind: 'publish', now, }) index = result.index pruned.push(...result.pruned) } assert.equal(index.latest, maxRetainedVersions + 2) assert.equal(index.entries.length, maxRetainedVersions) assert.deepEqual(pruned, [1, 2]) }) test('preview tokens are stable until rotated', async () => { const storage = memoryStorage() const token = await getPreviewToken(storage) assert.match(token, /^[A-Za-z0-9_-]{24}$/) assert.equal(await getPreviewToken(storage), token) assert.notEqual(await getPreviewToken(storage, true), token) }) test('the hosted URL the app records is what exports link to', async () => { const storage = memoryStorage() assert.equal(await readHostedUrl(storage), undefined) await recordHostedUrl( storage, 'https://jane.kody.run/packages/photo-portfolio', ) await recordHostedUrl( storage, 'https://jane.kody.run/packages/photo-portfolio', ) assert.equal( await readHostedUrl(storage), 'https://jane.kody.run/packages/photo-portfolio', ) assert.deepEqual(storage.keys(), [storageKeys.hostedUrl]) })