import { beforeEach, describe, expect, it } from 'vitest'; import { eq, sql } from 'drizzle-orm'; import { db, tables } from './db/index.ts'; import { DEFAULT_PROJECT_ACCENT, ProjectDetailsError, createProject, listProjects, updateProjectDetails } from './projects.ts'; beforeEach(() => { db.delete(tables.project).run(); db.run(sql`delete from search_index`); }); function rowOf(projectId: string) { return db.select().from(tables.project).where(eq(tables.project.id, projectId)).get()!; } describe('project details', () => { it('writes the title-page facts a writer edits', () => { const projectId = createProject('owner', 'Working title'); updateProjectDetails(projectId, { title: ' Low Tide Country ', subtitle: ' Saltmarsh, book one ', author: ' Maren Holt ', description: ' A keeper counts what the sea returns. ', accent: 'blue-screen' }); expect(rowOf(projectId)).toMatchObject({ title: 'Low Tide Country', subtitle: 'Saltmarsh, book one', author: 'Maren Holt', description: 'A keeper counts what the sea returns.', accent: 'blue-screen' }); }); it('stores a cleared field as null rather than an empty string', () => { const projectId = createProject('owner', 'Low Tide Country'); updateProjectDetails(projectId, { title: 'Low Tide Country', subtitle: 'a novel', author: 'Maren Holt', description: 'A keeper counts what the sea returns.' }); updateProjectDetails(projectId, { title: 'Low Tide Country', subtitle: ' ', author: '' }); expect(rowOf(projectId)).toMatchObject({ subtitle: null, author: null, description: null }); }); it('falls back to the rust mark for an accent this build does not know', () => { const projectId = createProject('owner', 'Low Tide Country'); updateProjectDetails(projectId, { title: 'Low Tide Country', accent: 'not-a-colour' }); expect(rowOf(projectId).accent).toBe(DEFAULT_PROJECT_ACCENT); }); it('refuses a blank title and leaves the row alone', () => { const projectId = createProject('owner', 'Low Tide Country'); expect(() => updateProjectDetails(projectId, { title: ' ' })).toThrow(ProjectDetailsError); expect(rowOf(projectId).title).toBe('Low Tide Country'); }); it('clamps a field longer than the card can hold', () => { const projectId = createProject('owner', 'Low Tide Country'); updateProjectDetails(projectId, { title: 'T'.repeat(200), subtitle: 'S'.repeat(400), author: 'A'.repeat(400), description: 'D'.repeat(2000) }); const row = rowOf(projectId); expect(row.title).toHaveLength(120); expect(row.subtitle).toHaveLength(160); expect(row.author).toHaveLength(120); expect(row.description).toHaveLength(600); }); it('carries the new values to the library card', () => { const projectId = createProject('owner', 'Working title'); updateProjectDetails(projectId, { title: 'Low Tide Country', subtitle: 'Saltmarsh, book one', author: 'Maren Holt', description: 'A keeper counts what the sea returns.', accent: 'thermal-throttle' }); expect(listProjects('owner').find((card) => card.id === projectId)).toMatchObject({ title: 'Low Tide Country', subtitle: 'Saltmarsh, book one', author: 'Maren Holt', description: 'A keeper counts what the sea returns.', accent: 'thermal-throttle' }); }); });