/** Lot N3: one atomic batch of edit commands (R23), on the Node core. */ import { afterEach, describe, expect, it, vi } from 'vitest' import type { EditCommand } from '@faunosteo/core-wasm/types' import { ASSET, emptyText, insert, point, svgBytes, } from '../helpers/editorFixtures' import { openReady, resetStore, serverHas, storedCopy, store, } from '../helpers/editorHarness' vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) })) vi.mock('@faunosteo/supabase', async (orig) => ({ ...(await orig()), getSheetVersion: vi.fn(), downloadSheetDrawing: vi.fn(), saveSheetDraft: vi.fn(), getOfficialSheetAdmin: vi.fn(), submitSheetReview: vi.fn(), })) afterEach(resetStore) /** An unmeasured main view (no drawing) holding one point at (500, 400). */ function unmeasuredText(): string { const withCategory = insert(emptyText(), { kind: 'category', value: { id: 'cat-a', name: { fr: 'N', en: 'N' }, color: null }, }) return insert(withCategory, point('anno-a', 500, 400, 5)) } async function openUnmeasured() { await openReady({ text: unmeasuredText() }) serverHas(unmeasuredText(), 3, false) const drawing = svgBytes('0 0 100 100') const held = store .getState() .addDrawing({ assetId: ASSET, sha256: drawing.sha256, bytes: drawing.bytes, }) expect(held).toEqual({ ok: true }) } const doc = () => JSON.parse(store.getState().documentText) const attachCommand = (): EditCommand => ({ op: 'update', key: { kind: 'view', id: 'view-main' }, element: { kind: 'view', value: { ...doc().views[0], svg_asset_id: ASSET }, }, }) const moved = (cx: number): EditCommand => ({ op: 'update', key: { kind: 'annotation', id: 'anno-a' }, element: point('anno-a', cx, 50, 5), }) describe('commitBatch', () => { it('applies the commands in order and commits once: one undo entry, one copy write', async () => { await openUnmeasured() const before = store.getState() const outcome = store.getState().commitBatch([attachCommand(), moved(50)]) expect(outcome).toEqual({ ok: true }) const after = store.getState() expect(doc().views[0].svg_asset_id).toBe(ASSET) expect(doc().annotations[0].geometry.cx).toBe(50) expect(after.undo).toHaveLength(1) expect(after.handleVersion).toBe(before.handleVersion + 1) expect(storedCopy()).not.toBeNull() }) it('restores both the view and the geometry with one undo', async () => { await openUnmeasured() const text = store.getState().documentText store.getState().commitBatch([attachCommand(), moved(50)]) expect(store.getState().undoStep()).toEqual({ ok: true }) expect(store.getState().documentText).toBe(text) }) it('keeps nothing when one command is refused by sheet.edit', async () => { await openUnmeasured() const before = store.getState() const missing: EditCommand = { op: 'remove', key: { kind: 'annotation', id: 'nope' }, } const outcome = store.getState().commitBatch([attachCommand(), missing]) expect(outcome).toMatchObject({ ok: false, reason: 'core' }) const after = store.getState() expect(after.documentText).toBe(before.documentText) expect(after.handleVersion).toBe(before.handleVersion) expect(after.undo).toHaveLength(0) expect(storedCopy()).toBeNull() }) it('keeps nothing when accept refuses the end state', async () => { await openUnmeasured() const before = store.getState() const outcome = store.getState().commitBatch([attachCommand(), moved(50)], { accept: () => ({ accepted: false, refusal: 'no' as const }), }) expect(outcome).toEqual({ ok: false, reason: 'refused', refusal: 'no' }) const after = store.getState() expect(after.documentText).toBe(before.documentText) expect(after.undo).toHaveLength(0) expect(after.preview?.document).toBe(before.documentText) }) it('hands accept the END state only, never an intermediate one', async () => { await openUnmeasured() const accept = vi.fn(() => ({ accepted: true as const })) store.getState().commitBatch([attachCommand(), moved(50)], { accept }) expect(accept).toHaveBeenCalledTimes(1) const [state] = accept.mock.calls[0] as unknown as [ { document: { annotations: { geometry: { cx: number } }[] } }, ] expect(state.document.annotations[0].geometry.cx).toBe(50) }) it('is blocked while a recovery offer is pending and when read-only', async () => { await openUnmeasured() store.setState({ recovery: 'pending' }) expect(store.getState().commitBatch([attachCommand()])).toMatchObject({ ok: false, reason: 'blocked', kind: 'recovery.pending', }) store.setState({ recovery: 'none', canEdit: false }) expect(store.getState().commitBatch([attachCommand()])).toMatchObject({ ok: false, reason: 'blocked', kind: 'read_only', }) expect(store.getState().undo).toHaveLength(0) }) it('refuses an empty batch without touching the document', async () => { await openUnmeasured() expect(store.getState().commitBatch([])).toMatchObject({ ok: false, reason: 'blocked', kind: 'empty', }) }) })