import { beforeEach, expect, it } from 'vitest'; import { eq } from 'drizzle-orm'; import { db, tables } from './db/index.ts'; import { createProject } from './projects.ts'; import { createScene } from './structure.ts'; import { addFeedback } from './sharing.ts'; import { queueData } from './review.ts'; /** * The queue shows a quotation and where its scene sits, never the prose, so the * loader sends one short summary per scene and answers `quoteGone` up front. */ beforeEach(() => { db.delete(tables.project).run(); }); /** A project whose only scene holds `text`, plus its stored revision. */ function projectWith(text: string) { const project = createProject('owner', 'Queue'); const scene = db.select().from(tables.scene).get()!; db.update(tables.scene).set({ bodyText: text }).where(eq(tables.scene.id, scene.id)).run(); return { project, scene: { ...scene, bodyText: text } }; } function response(project: string, sceneId: string, revision: number, quote: string) { addFeedback(project, sceneId, 'Editor', { message: 'Say more about this.', quote, suggestion: '', revision }); } it('sends a summary of a scene that holds a response, and nothing else', () => { const { project, scene } = projectWith('The bell rang under the water.'); response(project, scene.id, scene.revision, 'bell rang'); const queue = queueData(project); expect(queue.scenes).toEqual([ { deleted: false, id: scene.id, title: 'Scene 1', context: ['Queue', 'Chapter 1'], revision: scene.revision } ]); }); it('carries no editor body, rendered prose, or scene text', () => { const { project, scene } = projectWith('The bell rang under the water.'); response(project, scene.id, scene.revision, 'bell rang'); const queue = queueData(project); // The words are reachable from the scene, so shipping them here sends a whole // manuscript to show a dozen sentences. expect(JSON.stringify(queue)).not.toContain('under the water'); expect(Object.keys(queue.scenes[0]!).sort()).toEqual([ 'context', 'deleted', 'id', 'revision', 'title' ]); }); it('leaves out scenes that hold no response', () => { const { project, scene } = projectWith('The bell rang.'); createScene(project, scene.chapterId, 'An undisturbed scene'); response(project, scene.id, scene.revision, 'bell'); expect(queueData(project).scenes.map((row) => row.title)).toEqual(['Scene 1']); }); it('keeps the manuscript order rather than the order of the responses', () => { const { project, scene } = projectWith('The opening.'); const second = createScene(project, scene.chapterId, 'Later scene'); response(project, second, 0, ''); response(project, scene.id, scene.revision, ''); expect(queueData(project).scenes.map((row) => row.title)).toEqual(['Scene 1', 'Later scene']); }); it('says whether the quotation is still in the scene', () => { const { project, scene } = projectWith('The bell rang under the water.'); response(project, scene.id, scene.revision, 'bell rang'); expect(queueData(project).feedback[0]!.quoteGone).toBe(false); db.update(tables.scene) .set({ bodyText: 'Nothing rang at all.' }) .where(eq(tables.scene.id, scene.id)) .run(); expect(queueData(project).feedback[0]!.quoteGone).toBe(true); }); it('keeps a response whose scene was deleted, with its context and no prose', () => { const { project, scene } = projectWith('Words that a deleted scene keeps.'); response(project, scene.id, scene.revision, 'Words'); db.update(tables.scene).set({ deletedAt: new Date() }).where(eq(tables.scene.id, scene.id)).run(); const queue = queueData(project); expect(queue.scenes).toEqual([ { deleted: true, id: scene.id, title: 'Scene 1', context: ['Queue', 'Chapter 1'], revision: scene.revision } ]); expect(queue.feedback).toHaveLength(1); expect(JSON.stringify(queue)).not.toContain('Words that a deleted scene keeps'); });