feat(app): editor store, undo, recovery copy and geometry accept rules
This commit is contained in:
1 parent
f7e7bf4ee1
commit
7f1d44065b
20 files changed
+2538
-5
No files matched your search
@@ -0,0 +1,66 @@
|
||||
/** Lot D: `readVersion` draft mode (ruling R1) beside the unchanged catalogue path. */
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { SheetFailure, type FaunosteoClient } from '@faunosteo/supabase'
|
||||
import * as supabase from '@faunosteo/supabase'
|
||||
import { readVersion } from '@/catalogue/catalogue'
|
||||
|
||||
vi.mock('@faunosteo/supabase', async (orig) => ({
|
||||
...(await orig<typeof import('@faunosteo/supabase')>()),
|
||||
getSheetVersion: vi.fn(),
|
||||
downloadSheetDrawing: vi.fn(),
|
||||
}))
|
||||
|
||||
const sb = vi.mocked(supabase)
|
||||
const client = {} as FaunosteoClient
|
||||
const HASH_A = 'a'.repeat(64)
|
||||
const HASH_B = 'b'.repeat(64)
|
||||
|
||||
function answer(over: Record<string, unknown> = {}) {
|
||||
return {
|
||||
sheet_id: 's', version_id: 'v', version_number: 1, status: 'draft', species_id: 'equine',
|
||||
document: { any: 'thing' }, fingerprint: null, revision: 4,
|
||||
drawings: [{ asset_id: 'one', sha256: HASH_A }, { asset_id: 'two', sha256: HASH_B }],
|
||||
...over,
|
||||
}
|
||||
}
|
||||
|
||||
describe('readVersion draft mode', () => {
|
||||
it('accepts a draft, returns its revision and settles each drawing alone', async () => {
|
||||
sb.getSheetVersion.mockResolvedValue(answer() as never)
|
||||
sb.downloadSheetDrawing.mockImplementation(async (_c, sha) => {
|
||||
if (sha === HASH_B) throw new SheetFailure('not_found')
|
||||
return new Uint8Array([1])
|
||||
})
|
||||
const read = await readVersion(client, 'v', { draft: true })
|
||||
expect(read).toMatchObject({ ok: true, revision: 4 })
|
||||
if (!read.ok) return
|
||||
expect(read.drawings[0]).toMatchObject({ ok: true, drawing: { assetId: 'one' } })
|
||||
expect(read.drawings[1]).toEqual({ ok: false, assetId: 'two', sha256: HASH_B, failure: 'drawing_missing' })
|
||||
})
|
||||
|
||||
it('skips the drawings the caller holds', async () => {
|
||||
sb.getSheetVersion.mockResolvedValue(answer() as never)
|
||||
sb.downloadSheetDrawing.mockClear()
|
||||
sb.downloadSheetDrawing.mockResolvedValue(new Uint8Array([1]))
|
||||
const read = await readVersion(client, 'v', { draft: true, have: new Set(['one']) })
|
||||
expect(read.ok && read.drawings.map((d) => d.ok && d.drawing.assetId)).toEqual(['two'])
|
||||
})
|
||||
|
||||
it('needs the revision: an answer without it is unavailable', async () => {
|
||||
sb.getSheetVersion.mockResolvedValue(answer({ revision: undefined }) as never)
|
||||
expect(await readVersion(client, 'v', { draft: true })).toEqual({ ok: false, failure: 'unavailable' })
|
||||
})
|
||||
|
||||
it('maps a server refusal like the catalogue does', async () => {
|
||||
sb.getSheetVersion.mockRejectedValue(new SheetFailure('not_found'))
|
||||
expect(await readVersion(client, 'v', { draft: true })).toEqual({ ok: false, failure: 'denied' })
|
||||
})
|
||||
|
||||
it('leaves the catalogue path all-or-nothing and draft-refusing', async () => {
|
||||
sb.getSheetVersion.mockResolvedValue(answer() as never)
|
||||
expect(await readVersion(client, 'v')).toEqual({ ok: false, failure: 'denied' })
|
||||
sb.getSheetVersion.mockResolvedValue(answer({ status: 'published' }) as never)
|
||||
sb.downloadSheetDrawing.mockRejectedValue(new SheetFailure('not_found'))
|
||||
expect(await readVersion(client, 'v')).toEqual({ ok: false, failure: 'drawing_missing' })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Lot D: geometry errors and the geometry `accept`, read from ONE `PreviewState` and proved
|
||||
* through the real `replaceDocument` rollback on the Node core.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { PreviewState } from '@faunosteo/core-wasm/types'
|
||||
import { CataloguePreview } from '@/catalogue/preview'
|
||||
import { coreRuntime } from '@/core/runtime'
|
||||
import {
|
||||
geometryAccept,
|
||||
geometryErrorsById,
|
||||
unmeasuredViews,
|
||||
type GeometryRefusal,
|
||||
} from '@/editor/issues'
|
||||
import { ASSET, drawnText, edit, insert, point, svgBytes } from '../helpers/editorFixtures'
|
||||
|
||||
function draft(text: string, viewBox = '0 0 100 100') {
|
||||
const drawing = svgBytes(viewBox)
|
||||
const preview = new CataloguePreview(coreRuntime(), { status: 'draft', document: text }, [
|
||||
{ assetId: ASSET, sha256: drawing.sha256, bytes: drawing.bytes },
|
||||
])
|
||||
const state = preview.state()
|
||||
if (!state.ok) throw new Error(state.kind)
|
||||
return { preview, state: state.value }
|
||||
}
|
||||
|
||||
function moved(text: string, id: string, cx: number, cy: number, r: number) {
|
||||
return edit(text, { op: 'update', key: { kind: 'annotation', id }, element: point(id, cx, cy, r) })
|
||||
}
|
||||
|
||||
function refusalOf(outcome: ReturnType<CataloguePreview['replaceDocument']>) {
|
||||
if (outcome.ok || outcome.reason !== 'refused') throw new Error('not refused')
|
||||
return outcome.refusal as GeometryRefusal
|
||||
}
|
||||
|
||||
const valid = () => insert(drawnText(), point('anno-a', 50, 50, 5))
|
||||
|
||||
describe('geometryErrorsById', () => {
|
||||
it('maps the issue index to the id after an insert before the annotation', () => {
|
||||
const text = insert(valid(), point('anno-b', 1, 1, 5))
|
||||
expect([...geometryErrorsById(draft(text).state).keys()]).toEqual(['anno-b'])
|
||||
const shifted = edit(text, {
|
||||
op: 'insert',
|
||||
element: point('anno-c', 60, 60, 5),
|
||||
position: 0,
|
||||
})
|
||||
const { state } = draft(shifted)
|
||||
expect(state.report.issues.some((i) => i.path === 'annotations[2].geometry')).toBe(true)
|
||||
expect([...geometryErrorsById(state).keys()]).toEqual(['anno-b'])
|
||||
})
|
||||
|
||||
it('ignores an Error that is not at annotations[i].geometry', () => {
|
||||
const { state } = draft(valid())
|
||||
const faked: PreviewState = {
|
||||
...state,
|
||||
report: {
|
||||
...state.report,
|
||||
issues: [
|
||||
{ code: 'x.y', severity: 'error', path: 'annotations[0].name_fr', message: 'm' },
|
||||
{ code: 'annotation.geometry.out_of_frame', severity: 'warning', path: 'annotations[0].geometry', message: 'm' },
|
||||
],
|
||||
},
|
||||
}
|
||||
expect(geometryErrorsById(faked).size).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('unmeasuredViews', () => {
|
||||
it('measures a view through its own drawing or the one it crops', () => {
|
||||
const detail = insert(drawnText(), {
|
||||
kind: 'view',
|
||||
value: {
|
||||
kind: 'detail', id: 'view-det', name: { fr: 'D', en: 'D' },
|
||||
svg_asset_id: null, view_box: { min_x: 0, min_y: 0, width: 50, height: 50 },
|
||||
parent_view_id: 'view-main',
|
||||
},
|
||||
})
|
||||
expect(unmeasuredViews(draft(detail).state).size).toBe(0)
|
||||
})
|
||||
|
||||
it('counts a missing drawing and a frame-less view as unmeasured', () => {
|
||||
const { state } = draft(drawnText())
|
||||
const without = { ...state, drawings: [] }
|
||||
expect([...unmeasuredViews(without)]).toEqual(['view-main'])
|
||||
const frameless: PreviewState = {
|
||||
...state,
|
||||
report: {
|
||||
...state.report,
|
||||
issues: [{ code: 'view.frame.unknown', severity: 'error', path: 'views[0]', message: 'm' }],
|
||||
},
|
||||
}
|
||||
expect([...unmeasuredViews(frameless)]).toEqual(['view-main'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('geometryAccept through replaceDocument', () => {
|
||||
it('refuses a valid point moved out of the frame and keeps the previous text', () => {
|
||||
const text = valid()
|
||||
const { preview, state } = draft(text)
|
||||
const accept = geometryAccept({ before: state, changedIds: ['anno-a'] })
|
||||
const outcome = preview.replaceDocument(moved(text, 'anno-a', 1, 1, 5), accept)
|
||||
expect(refusalOf(outcome)).toMatchObject({
|
||||
reason: 'changed_invalid',
|
||||
annotationIds: ['anno-a'],
|
||||
issues: [{ code: 'annotation.geometry.out_of_frame' }],
|
||||
})
|
||||
expect(preview.document).toBe(text)
|
||||
})
|
||||
|
||||
it('refuses a typed radius the core reports too_small', () => {
|
||||
const big = '0 0 5000 5000'
|
||||
const text = insert(drawnText(), point('anno-a', 2500, 2500, 400))
|
||||
const { preview, state } = draft(text, big)
|
||||
expect(geometryErrorsById(state).size).toBe(0)
|
||||
const accept = geometryAccept({ before: state, changedIds: ['anno-a'] })
|
||||
const outcome = preview.replaceDocument(moved(text, 'anno-a', 2500, 2500, 1), accept)
|
||||
expect(refusalOf(outcome).issues[0].code).toBe('annotation.geometry.too_small')
|
||||
})
|
||||
|
||||
it('repairs an inherited out-of-frame point only with a coherent set', () => {
|
||||
const text = insert(drawnText(), point('anno-a', 150, 150, 5))
|
||||
const { preview, state } = draft(text)
|
||||
expect(geometryErrorsById(state).has('anno-a')).toBe(true)
|
||||
const accept = geometryAccept({ before: state, changedIds: ['anno-a'] })
|
||||
const half = preview.replaceDocument(moved(text, 'anno-a', 50, 150, 5), accept)
|
||||
expect(refusalOf(half).reason).toBe('changed_invalid')
|
||||
const whole = preview.replaceDocument(moved(text, 'anno-a', 50, 50, 5), accept)
|
||||
expect(whole.ok).toBe(true)
|
||||
})
|
||||
|
||||
it('never blocks a commit on an unrelated annotation already invalid', () => {
|
||||
const text = insert(valid(), point('anno-b', 1, 1, 5))
|
||||
const { preview, state } = draft(text)
|
||||
const accept = geometryAccept({ before: state, changedIds: ['anno-a'] })
|
||||
expect(preview.replaceDocument(moved(text, 'anno-a', 40, 40, 5), accept).ok).toBe(true)
|
||||
})
|
||||
|
||||
it('refuses a view_box that invalidates a valid point', () => {
|
||||
const text = valid()
|
||||
const { preview, state } = draft(text)
|
||||
const main = state.document.views[0]
|
||||
const next = edit(text, {
|
||||
op: 'update',
|
||||
key: { kind: 'view', id: 'view-main' },
|
||||
element: { kind: 'view', value: { ...main, view_box: { min_x: 0, min_y: 0, width: 40, height: 40 } } },
|
||||
})
|
||||
const outcome = preview.replaceDocument(next, geometryAccept({ before: state, changedIds: [] }))
|
||||
expect(refusalOf(outcome)).toMatchObject({ reason: 'newly_invalid', annotationIds: ['anno-a'] })
|
||||
})
|
||||
|
||||
it('accepts a view_box that only touches an already invalid point', () => {
|
||||
const text = insert(drawnText(), point('anno-a', 150, 50, 5))
|
||||
const { preview, state } = draft(text)
|
||||
const main = state.document.views[0]
|
||||
const next = edit(text, {
|
||||
op: 'update',
|
||||
key: { kind: 'view', id: 'view-main' },
|
||||
element: { kind: 'view', value: { ...main, view_box: { min_x: 0, min_y: 0, width: 90, height: 90 } } },
|
||||
})
|
||||
const outcome = preview.replaceDocument(next, geometryAccept({ before: state, changedIds: [] }))
|
||||
expect(outcome.ok).toBe(true)
|
||||
})
|
||||
|
||||
it('refuses a frame change that leaves a view with annotations unmeasured', () => {
|
||||
const text = valid()
|
||||
const { preview, state } = draft(text)
|
||||
const main = state.document.views[0]
|
||||
const next = edit(text, {
|
||||
op: 'update',
|
||||
key: { kind: 'view', id: 'view-main' },
|
||||
element: { kind: 'view', value: { ...main, svg_asset_id: '22222222-2222-4222-8222-222222222222' } },
|
||||
})
|
||||
const outcome = preview.replaceDocument(next, geometryAccept({ before: state, changedIds: [] }))
|
||||
expect(refusalOf(outcome)).toMatchObject({ reason: 'view_unmeasured', viewIds: ['view-main'] })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,265 @@
|
||||
/** Lot D: the local recovery copy (offer, restore, ignore, cap, quota) on the Node core. */
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { SheetFailure } from '@faunosteo/supabase'
|
||||
import { leaveGuardArmed } from '@/editor/state/editorStore'
|
||||
import {
|
||||
SHEET, USER, VERSION, baseText, commitMove, copyKeyOf, openReady, resetStore, sb, serverHas,
|
||||
storedCopy, store,
|
||||
} from '../helpers/editorHarness'
|
||||
import { ASSET, svgBytes } from '../helpers/editorFixtures'
|
||||
|
||||
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
|
||||
vi.mock('@faunosteo/supabase', async (orig) => ({
|
||||
...(await orig<typeof import('@faunosteo/supabase')>()),
|
||||
getSheetVersion: vi.fn(),
|
||||
downloadSheetDrawing: vi.fn(),
|
||||
saveSheetDraft: vi.fn(),
|
||||
getOfficialSheetAdmin: vi.fn(),
|
||||
submitSheetReview: vi.fn(),
|
||||
}))
|
||||
|
||||
afterEach(resetStore)
|
||||
|
||||
/** The text of the server document with anno-a moved: what a copy would hold. */
|
||||
function movedText(x: number): string {
|
||||
return JSON.stringify(
|
||||
JSON.parse(baseText(), (key, value) => (key === 'cx' ? x : value)),
|
||||
)
|
||||
}
|
||||
|
||||
function seed(text: string, revision: number, over: Record<string, unknown> = {}, version = VERSION, user = USER) {
|
||||
sessionStorage.setItem(
|
||||
copyKeyOf(version, user),
|
||||
JSON.stringify({
|
||||
v: 1, user_id: user, version_id: version, sheet_id: SHEET, sheet_name: 'Fiche',
|
||||
revision, written_at: '2026-11-03T10:00:00Z', document: JSON.parse(text), drawings: [], ...over,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
describe('writing the copy', () => {
|
||||
it('writes synchronously on a committed change while unsaved, with the base revision', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
expect(storedCopy()).toBeNull()
|
||||
commitMove(40)
|
||||
expect(storedCopy()).toMatchObject({
|
||||
v: 1, user_id: USER, version_id: VERSION, sheet_id: SHEET, sheet_name: 'Fiche', revision: 3,
|
||||
document: JSON.parse(store.getState().documentText),
|
||||
drawings: [{ asset_id: ASSET }],
|
||||
})
|
||||
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
||||
expect(leaveGuardArmed(store.getState())).toBe(true)
|
||||
})
|
||||
|
||||
it('leaves the copy untouched in a mode that cannot save', async () => {
|
||||
seed(movedText(10), 3)
|
||||
const raw = sessionStorage.getItem(copyKeyOf())
|
||||
await openReady({ canEdit: false })
|
||||
expect(store.getState()).toMatchObject({ recovery: 'none', offer: null, unusableCopy: true })
|
||||
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
||||
})
|
||||
})
|
||||
|
||||
describe('the blocking offer', () => {
|
||||
it('refuses every change and never writes or removes the copy while pending', async () => {
|
||||
seed(movedText(10), 3)
|
||||
const raw = sessionStorage.getItem(copyKeyOf())
|
||||
await openReady({ revision: 3 })
|
||||
const state = store.getState()
|
||||
expect(state).toMatchObject({ recovery: 'pending', offer: { revision: 3, serverRevision: 3 } })
|
||||
expect(leaveGuardArmed(state)).toBe(true)
|
||||
const blocked = { ok: false, reason: 'blocked', kind: 'recovery.pending' }
|
||||
expect(commitMove(40)).toMatchObject(blocked)
|
||||
expect(state.undoStep()).toMatchObject(blocked)
|
||||
expect(state.redoStep()).toMatchObject(blocked)
|
||||
expect(await state.save()).toMatchObject(blocked)
|
||||
expect(await state.submit()).toMatchObject(blocked)
|
||||
expect(await state.discardChanges({ confirmed: true })).toMatchObject(blocked)
|
||||
expect(await state.reloadServerDraft({ confirmed: true })).toMatchObject(blocked)
|
||||
expect(sb.saveSheetDraft).not.toHaveBeenCalled()
|
||||
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
||||
expect(state.documentText).toBe(state.savedText)
|
||||
})
|
||||
|
||||
it('offers nothing for a copy equal to the server document, whatever the key order', async () => {
|
||||
seed(baseText(), 3)
|
||||
await openReady()
|
||||
expect(store.getState().recovery).toBe('none')
|
||||
})
|
||||
|
||||
it('needs a confirmation to ignore, then removes the copy and resumes from the server', async () => {
|
||||
seed(movedText(10), 3)
|
||||
await openReady()
|
||||
expect(store.getState().ignoreCopy({ confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
expect(store.getState().ignoreCopy({ confirmed: true })).toEqual({ ok: true })
|
||||
expect(storedCopy()).toBeNull()
|
||||
expect(store.getState()).toMatchObject({ recovery: 'none', offer: null })
|
||||
expect(commitMove(40)).toEqual({ ok: true })
|
||||
})
|
||||
})
|
||||
|
||||
describe('restore', () => {
|
||||
it('applies the copy in one store update and rewrites it on its base revision', async () => {
|
||||
seed(movedText(10), 3)
|
||||
await openReady({ revision: 3 })
|
||||
const seen: Array<{ recovery: string; text: string }> = []
|
||||
const stop = store.subscribe((s) => seen.push({ recovery: s.recovery, text: s.documentText }))
|
||||
expect(await store.getState().restoreCopy()).toEqual({ ok: true })
|
||||
stop()
|
||||
const state = store.getState()
|
||||
expect(state.documentText).toBe(movedText(10))
|
||||
expect(state).toMatchObject({ savedRevision: 3, staleBase: false, unsaved: true, recovery: 'none' })
|
||||
expect(state.savedText).toBe(JSON.stringify(JSON.parse(baseText())))
|
||||
expect(seen.filter((s) => s.recovery === 'none').every((s) => s.text === movedText(10))).toBe(true)
|
||||
expect(storedCopy()).toMatchObject({ revision: 3, document: JSON.parse(movedText(10)) })
|
||||
expect(state.previewState?.document.annotations[0]).toMatchObject({ geometry: { cx: 10 } })
|
||||
})
|
||||
|
||||
it('downloads and adds the copy drawings the page lacks', async () => {
|
||||
serverHas(baseText(), 3, false)
|
||||
sb.downloadSheetDrawing.mockResolvedValue(svgBytes().bytes)
|
||||
seed(movedText(10), 3, { drawings: [{ asset_id: ASSET, sha256: svgBytes().sha256 }] })
|
||||
store.getState().open(VERSION, USER)
|
||||
await vi.waitFor(() => expect(store.getState().status).toBe('ready'))
|
||||
expect(store.getState().previewState?.drawings[0]).toMatchObject({ state: 'missing' })
|
||||
await store.getState().restoreCopy()
|
||||
expect(store.getState().previewState?.drawings[0]).toMatchObject({ state: 'ok' })
|
||||
expect(store.getState().drawings).toEqual([expect.objectContaining({ asset_id: ASSET })])
|
||||
})
|
||||
|
||||
it('keeps the base revision R of an older copy, sets staleBase and meets the conflict', async () => {
|
||||
seed(movedText(10), 2)
|
||||
await openReady({ revision: 5 })
|
||||
expect(store.getState().offer).toMatchObject({ revision: 2, serverRevision: 5 })
|
||||
await store.getState().restoreCopy()
|
||||
expect(store.getState()).toMatchObject({ savedRevision: 2, staleBase: true, unsaved: true })
|
||||
commitMove(30)
|
||||
expect(storedCopy()).toMatchObject({ revision: 2 })
|
||||
expect(await store.getState().submit()).toMatchObject({ ok: false, kind: 'stale_base' })
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft_conflict'))
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue({
|
||||
draft_version: { version_id: VERSION, version_number: 1, revision: 5 },
|
||||
draft_last_save: null,
|
||||
} as never)
|
||||
await store.getState().save()
|
||||
expect(sb.saveSheetDraft).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ expected_revision: 2 }))
|
||||
expect(store.getState().conflict).toMatchObject({ status: 'known', revision: 5 })
|
||||
})
|
||||
|
||||
it('takes the reload path for « Annuler les modifications » under staleBase', async () => {
|
||||
seed(movedText(10), 2)
|
||||
await openReady({ revision: 5 })
|
||||
await store.getState().restoreCopy()
|
||||
serverHas(baseText(), 6)
|
||||
expect(await store.getState().discardChanges({ confirmed: true })).toEqual({ ok: true })
|
||||
expect(store.getState()).toMatchObject({ savedRevision: 6, staleBase: false, unsaved: false })
|
||||
expect(store.getState().documentText).toBe(store.getState().savedText)
|
||||
expect(storedCopy()).toBeNull()
|
||||
})
|
||||
|
||||
it('reports a copy the core refuses, keeps it, blocks editing until ignored', async () => {
|
||||
sessionStorage.setItem(
|
||||
copyKeyOf(),
|
||||
JSON.stringify({
|
||||
v: 1, user_id: USER, version_id: VERSION, sheet_id: SHEET, sheet_name: 'X',
|
||||
revision: 3, written_at: 'T', document: { not: 'a document' }, drawings: [],
|
||||
}),
|
||||
)
|
||||
const raw = sessionStorage.getItem(copyKeyOf())
|
||||
await openReady()
|
||||
const outcome = await store.getState().restoreCopy()
|
||||
expect(outcome).toMatchObject({ ok: false, reason: 'core' })
|
||||
const state = store.getState()
|
||||
expect(state.recovery).toBe('refused')
|
||||
expect(state.refusedKind).toEqual(expect.any(String))
|
||||
expect(state.documentText).toBe(state.savedText)
|
||||
expect(commitMove(40)).toMatchObject({ kind: 'recovery.pending' })
|
||||
expect(await state.restoreCopy()).toMatchObject({ ok: false })
|
||||
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw)
|
||||
expect(store.getState().ignoreCopy({ confirmed: true })).toEqual({ ok: true })
|
||||
expect(commitMove(40)).toEqual({ ok: true })
|
||||
})
|
||||
})
|
||||
|
||||
describe('an unreadable copy', () => {
|
||||
it.each([
|
||||
['malformed JSON', () => 'not json{'],
|
||||
['an unknown version', () => JSON.stringify({ v: 2 })],
|
||||
['another user in the key', () =>
|
||||
JSON.stringify({ v: 1, user_id: 'x', version_id: VERSION, sheet_id: SHEET, sheet_name: '', revision: 1, written_at: 'T', document: {}, drawings: [] })],
|
||||
])('is refused and kept byte for byte (%s)', async (_label, raw) => {
|
||||
sessionStorage.setItem(copyKeyOf(), raw())
|
||||
await openReady()
|
||||
expect(store.getState()).toMatchObject({ recovery: 'refused', refusedKind: 'unreadable' })
|
||||
expect(commitMove(40)).toMatchObject({ kind: 'recovery.pending' })
|
||||
expect(sessionStorage.getItem(copyKeyOf())).toBe(raw())
|
||||
expect(leaveGuardArmed(store.getState())).toBe(true)
|
||||
expect(store.getState().ignoreCopy({ confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
||||
expect(store.getState().ignoreCopy({ confirmed: true })).toEqual({ ok: true })
|
||||
expect(storedCopy()).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('cap, quota and other accounts', () => {
|
||||
const other = (n: number) => `ver-other-${n}`
|
||||
|
||||
it('refuses a third version, keeps both others, retries after an explicit abandonment', async () => {
|
||||
seed(baseText(), 1, {}, other(1))
|
||||
seed(baseText(), 2, { sheet_name: 'Autre' }, other(2))
|
||||
await openReady()
|
||||
commitMove(40)
|
||||
expect(store.getState()).toMatchObject({ copyWritten: false, copyIssue: 'cap' })
|
||||
expect(storedCopy()).toBeNull()
|
||||
expect(store.getState().otherCopies.map((c) => [c.versionId, c.revision])).toEqual([
|
||||
[other(1), 1], [other(2), 2],
|
||||
])
|
||||
expect(store.getState().abandonOtherCopy(other(1), { confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
||||
expect(storedCopy(other(1))).not.toBeNull()
|
||||
expect(store.getState().abandonOtherCopy(other(1), { confirmed: true })).toEqual({ ok: true })
|
||||
expect(storedCopy(other(1))).toBeNull()
|
||||
expect(storedCopy(other(2))).not.toBeNull()
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
||||
})
|
||||
|
||||
it('reports a full storage and keeps the work in the document', async () => {
|
||||
await openReady()
|
||||
const spy = vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
||||
throw new DOMException('full', 'QuotaExceededError')
|
||||
})
|
||||
commitMove(40)
|
||||
expect(store.getState()).toMatchObject({ copyWritten: false, copyIssue: 'quota', unsaved: true })
|
||||
spy.mockRestore()
|
||||
store.getState().retryCopyWrite()
|
||||
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
||||
})
|
||||
|
||||
it('never lists, counts, offers or removes the copies of another account', async () => {
|
||||
seed(movedText(10), 3, {}, VERSION, 'user-2')
|
||||
seed(baseText(), 1, {}, other(1), 'user-2')
|
||||
seed(baseText(), 1, {}, other(2), 'user-2')
|
||||
seed(baseText(), 1, {}, other(3), 'user-2')
|
||||
await openReady()
|
||||
expect(store.getState()).toMatchObject({ recovery: 'none', otherCopies: [] })
|
||||
commitMove(40)
|
||||
expect(store.getState()).toMatchObject({ copyWritten: true, copyIssue: null })
|
||||
expect(store.getState().abandonOtherCopy(other(1), { confirmed: true })).toEqual({ ok: true })
|
||||
for (const version of [VERSION, other(1), other(2), other(3)]) {
|
||||
expect(storedCopy(version, 'user-2')).not.toBeNull()
|
||||
}
|
||||
})
|
||||
|
||||
it('removes the copy only on the explicit outcomes', async () => {
|
||||
await openReady()
|
||||
commitMove(40)
|
||||
store.getState().undoStep()
|
||||
store.getState().redoStep()
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft.frozen'))
|
||||
await store.getState().save()
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
await store.getState().save()
|
||||
expect(storedCopy()).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,334 @@
|
||||
/** Lot D: the editor store on the Node core, Supabase accessors mocked at the module boundary. */
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { SheetFailure } from '@faunosteo/supabase'
|
||||
import { leaveGuardArmed, canRedo, canUndo } from '@/editor/state/editorStore'
|
||||
import {
|
||||
USER, VERSION, baseText, commitMove, moveCommand, openReady, resetStore, sb, serverHas,
|
||||
storedCopy, store,
|
||||
} from '../helpers/editorHarness'
|
||||
import { ASSET } from '../helpers/editorFixtures'
|
||||
|
||||
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
|
||||
vi.mock('@faunosteo/supabase', async (orig) => ({
|
||||
...(await orig<typeof import('@faunosteo/supabase')>()),
|
||||
getSheetVersion: vi.fn(),
|
||||
downloadSheetDrawing: vi.fn(),
|
||||
saveSheetDraft: vi.fn(),
|
||||
getOfficialSheetAdmin: vi.fn(),
|
||||
submitSheetReview: vi.fn(),
|
||||
}))
|
||||
|
||||
afterEach(resetStore)
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void
|
||||
let reject!: (error: unknown) => void
|
||||
const promise = new Promise<T>((res, rej) => {
|
||||
resolve = res
|
||||
reject = rej
|
||||
})
|
||||
return { promise, resolve, reject }
|
||||
}
|
||||
|
||||
describe('open and sessions', () => {
|
||||
it('loads the draft with its revision and drawings from one read', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
const state = store.getState()
|
||||
expect(state).toMatchObject({ status: 'ready', savedRevision: 3, unsaved: false, recovery: 'none' })
|
||||
expect(state.documentText).toBe(state.savedText)
|
||||
expect(state.drawings).toEqual([expect.objectContaining({ asset_id: ASSET })])
|
||||
expect(state.previewState?.drawings[0]).toMatchObject({ state: 'ok' })
|
||||
expect(state.handleVersion).toBe(1)
|
||||
})
|
||||
|
||||
it('does not fail the load on a drawing that cannot be downloaded', async () => {
|
||||
sb.downloadSheetDrawing.mockRejectedValue(new SheetFailure('not_found'))
|
||||
serverHas(baseText(), 3)
|
||||
sb.downloadSheetDrawing.mockRejectedValue(new SheetFailure('not_found'))
|
||||
store.getState().open(VERSION, USER)
|
||||
await vi.waitFor(() => expect(store.getState().status).toBe('ready'))
|
||||
expect(store.getState().drawingFailures[ASSET]).toBe('drawing_missing')
|
||||
expect(store.getState().previewState?.drawings[0]).toMatchObject({ state: 'missing' })
|
||||
})
|
||||
|
||||
it('drops a late load after close and frees the preview it built', async () => {
|
||||
const late = deferred<unknown>()
|
||||
sb.getSheetVersion.mockReturnValue(late.promise as never)
|
||||
store.getState().open(VERSION, USER)
|
||||
store.getState().close()
|
||||
late.resolve({})
|
||||
await Promise.resolve()
|
||||
expect(store.getState()).toMatchObject({ status: 'idle', preview: null })
|
||||
})
|
||||
|
||||
it('lets the second open of a StrictMode double mount win over the first', async () => {
|
||||
const first = deferred<unknown>()
|
||||
sb.getSheetVersion.mockReturnValueOnce(first.promise as never)
|
||||
const one = store.getState().open(VERSION, USER)
|
||||
store.getState().close()
|
||||
serverHas(baseText(), 7)
|
||||
const two = store.getState().open(VERSION, USER)
|
||||
expect(two).toBeGreaterThan(one)
|
||||
await vi.waitFor(() => expect(store.getState().status).toBe('ready'))
|
||||
first.resolve({ sheet_id: 'x' })
|
||||
await Promise.resolve()
|
||||
expect(store.getState().savedRevision).toBe(7)
|
||||
})
|
||||
|
||||
it('drops a save answer that lands after close', async () => {
|
||||
await openReady()
|
||||
commitMove(40)
|
||||
const answer = deferred<unknown>()
|
||||
sb.saveSheetDraft.mockReturnValue(answer.promise as never)
|
||||
const saving = store.getState().save()
|
||||
store.getState().close()
|
||||
answer.resolve({ revision: 4 })
|
||||
expect(await saving).toMatchObject({ ok: false, reason: 'blocked', kind: 'session_changed' })
|
||||
expect(store.getState()).toMatchObject({ status: 'idle', savedRevision: 0 })
|
||||
})
|
||||
})
|
||||
|
||||
describe('commit, undo, redo', () => {
|
||||
it('commits in one store update', async () => {
|
||||
await openReady()
|
||||
const before = store.getState()
|
||||
expect(commitMove(40)).toEqual({ ok: true })
|
||||
const after = store.getState()
|
||||
expect(after.documentText).not.toBe(before.documentText)
|
||||
expect(after).toMatchObject({ unsaved: true, handleVersion: before.handleVersion + 1 })
|
||||
expect(after.undo).toHaveLength(1)
|
||||
expect(after.previewState).not.toBe(before.previewState)
|
||||
})
|
||||
|
||||
it('keeps nothing on a core refusal or an accept refusal', async () => {
|
||||
await openReady()
|
||||
const before = store.getState()
|
||||
const missing = store.getState().commit({ op: 'remove', key: { kind: 'annotation', id: 'nope' } })
|
||||
expect(missing).toMatchObject({ ok: false, reason: 'core' })
|
||||
const refused = store.getState().commit(moveCommand(40), {
|
||||
accept: () => ({ accepted: false, refusal: 'no' as const }),
|
||||
})
|
||||
expect(refused).toEqual({ ok: false, reason: 'refused', refusal: 'no' })
|
||||
const after = store.getState()
|
||||
expect(after.documentText).toBe(before.documentText)
|
||||
expect(after.handleVersion).toBe(before.handleVersion)
|
||||
expect(after.undo).toHaveLength(0)
|
||||
expect(storedCopy()).toBeNull()
|
||||
expect(after.preview?.document).toBe(before.documentText)
|
||||
})
|
||||
|
||||
it('undoes, redoes, and clears redo on a new commit', async () => {
|
||||
await openReady()
|
||||
const saved = store.getState().documentText
|
||||
commitMove(40)
|
||||
commitMove(30)
|
||||
expect(store.getState().undoStep()).toEqual({ ok: true })
|
||||
expect(canRedo(store.getState())).toBe(true)
|
||||
expect(store.getState().redoStep()).toEqual({ ok: true })
|
||||
store.getState().undoStep()
|
||||
commitMove(20)
|
||||
expect(canRedo(store.getState())).toBe(false)
|
||||
expect(store.getState().undo).toHaveLength(2)
|
||||
store.getState().undoStep()
|
||||
store.getState().undoStep()
|
||||
expect(store.getState().documentText).toBe(saved)
|
||||
expect(store.getState().unsaved).toBe(false)
|
||||
expect(canUndo(store.getState())).toBe(false)
|
||||
expect(store.getState().undoStep()).toMatchObject({ ok: false, kind: 'empty' })
|
||||
})
|
||||
|
||||
it('keeps the copy when undo returns to the saved content', async () => {
|
||||
await openReady()
|
||||
commitMove(40)
|
||||
const written = storedCopy()
|
||||
store.getState().undoStep()
|
||||
expect(store.getState().unsaved).toBe(false)
|
||||
expect(storedCopy()).toEqual(written)
|
||||
})
|
||||
|
||||
it('refuses every change in a read-only mode', async () => {
|
||||
await openReady({ canEdit: false })
|
||||
expect(commitMove(40)).toMatchObject({ ok: false, kind: 'read_only' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('save', () => {
|
||||
it('saves against the saved revision and removes the copy', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
const sent = store.getState().documentText
|
||||
expect(await store.getState().save()).toEqual({ ok: true, revision: 4 })
|
||||
expect(sb.saveSheetDraft).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({ version_id: VERSION, expected_revision: 3 }),
|
||||
)
|
||||
expect(store.getState()).toMatchObject({ savedRevision: 4, savedText: sent, unsaved: false })
|
||||
expect(storedCopy()).toBeNull()
|
||||
expect(leaveGuardArmed(store.getState())).toBe(false)
|
||||
})
|
||||
|
||||
it('keeps the sent text and rewrites the copy on the new revision for commits in flight', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
const sent = store.getState().documentText
|
||||
const answer = deferred<unknown>()
|
||||
sb.saveSheetDraft.mockReturnValue(answer.promise as never)
|
||||
const saving = store.getState().save()
|
||||
commitMove(30)
|
||||
const latest = store.getState().documentText
|
||||
answer.resolve({ revision: 4 })
|
||||
await saving
|
||||
expect(store.getState()).toMatchObject({ savedText: sent, savedRevision: 4, unsaved: true })
|
||||
expect(storedCopy()).toMatchObject({ revision: 4, document: JSON.parse(latest) })
|
||||
})
|
||||
|
||||
it('treats draft.unchanged as saved at the revision sent', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft.unchanged'))
|
||||
const sent = store.getState().documentText
|
||||
expect(await store.getState().save()).toEqual({ ok: true, revision: 3 })
|
||||
expect(store.getState()).toMatchObject({ savedText: sent, savedRevision: 3, unsaved: false })
|
||||
expect(storedCopy()).toBeNull()
|
||||
})
|
||||
|
||||
it('keeps the document and the copy on any other server kind', async () => {
|
||||
await openReady()
|
||||
commitMove(40)
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft.frozen'))
|
||||
expect(await store.getState().save()).toEqual({ ok: false, reason: 'server', kind: 'draft.frozen' })
|
||||
expect(store.getState()).toMatchObject({ unsaved: true, saveFailure: 'draft.frozen', saving: false })
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
})
|
||||
|
||||
it('refuses a second save while one is in flight', async () => {
|
||||
await openReady()
|
||||
commitMove(40)
|
||||
sb.saveSheetDraft.mockReturnValue(deferred<unknown>().promise as never)
|
||||
void store.getState().save()
|
||||
expect(await store.getState().save()).toMatchObject({ ok: false, kind: 'busy' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('conflict', () => {
|
||||
async function conflicted(admin: unknown | 'fail', revision = 5) {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft_conflict'))
|
||||
if (admin === 'fail') sb.getOfficialSheetAdmin.mockRejectedValue(new SheetFailure('network'))
|
||||
else sb.getOfficialSheetAdmin.mockResolvedValue(admin as never)
|
||||
const outcome = await store.getState().save()
|
||||
expect(outcome).toMatchObject({ ok: false, kind: 'draft_conflict' })
|
||||
return revision
|
||||
}
|
||||
|
||||
const adminWith = (over: Record<string, unknown>) => ({
|
||||
draft_version: { version_id: VERSION, version_number: 1, revision: 5 },
|
||||
draft_last_save: null,
|
||||
...over,
|
||||
})
|
||||
|
||||
it('takes N from draft_version and the author only when draft_last_save matches N', async () => {
|
||||
const person = { user_id: 'other', display_name: 'Marie' }
|
||||
await conflicted(adminWith({ draft_last_save: { revision: 5, saved_at: 'T', saved_by: person } }))
|
||||
expect(store.getState().conflict).toEqual({
|
||||
status: 'known',
|
||||
revision: 5,
|
||||
lastSave: { savedAt: 'T', savedBy: person, byCaller: false },
|
||||
})
|
||||
})
|
||||
|
||||
it('omits the author when draft_last_save is for another revision, flags the caller', async () => {
|
||||
await conflicted(adminWith({ draft_last_save: { revision: 4, saved_at: 'T', saved_by: null } }))
|
||||
expect(store.getState().conflict).toEqual({ status: 'known', revision: 5, lastSave: null })
|
||||
const me = { user_id: USER, display_name: 'Moi' }
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(
|
||||
adminWith({ draft_last_save: { revision: 5, saved_at: 'T', saved_by: me } }) as never,
|
||||
)
|
||||
await store.getState().retryConflictRead()
|
||||
expect(store.getState().conflict).toMatchObject({ lastSave: { byCaller: true } })
|
||||
})
|
||||
|
||||
it('is unknown on a failed read, another version or no draft_version, and refuses keepMine', async () => {
|
||||
await conflicted('fail')
|
||||
expect(store.getState().conflict).toEqual({ status: 'unknown' })
|
||||
expect(await store.getState().keepMine({ confirmed: true })).toMatchObject({ kind: 'revision_unknown' })
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(
|
||||
adminWith({ draft_version: { version_id: 'other', version_number: 1, revision: 9 } }) as never,
|
||||
)
|
||||
await store.getState().retryConflictRead()
|
||||
expect(store.getState().conflict).toEqual({ status: 'unknown' })
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(adminWith({ draft_version: null }) as never)
|
||||
await store.getState().retryConflictRead()
|
||||
expect(store.getState().conflict).toEqual({ status: 'unknown' })
|
||||
})
|
||||
|
||||
it('keeps my version only after confirmation, against N', async () => {
|
||||
await conflicted(adminWith({}))
|
||||
sb.saveSheetDraft.mockClear()
|
||||
expect(await store.getState().keepMine({ confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
||||
expect(sb.saveSheetDraft).not.toHaveBeenCalled()
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 6 } as never)
|
||||
expect(await store.getState().keepMine({ confirmed: true })).toEqual({ ok: true, revision: 6 })
|
||||
expect(sb.saveSheetDraft).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({ expected_revision: 5 }),
|
||||
)
|
||||
expect(store.getState()).toMatchObject({ savedRevision: 6, unsaved: false, conflict: null })
|
||||
})
|
||||
|
||||
it('re-reads on a new conflict and never writes by itself', async () => {
|
||||
await conflicted(adminWith({}))
|
||||
sb.saveSheetDraft.mockClear()
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft_conflict'))
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(
|
||||
adminWith({ draft_version: { version_id: VERSION, version_number: 1, revision: 8 } }) as never,
|
||||
)
|
||||
await store.getState().keepMine({ confirmed: true })
|
||||
expect(sb.saveSheetDraft).toHaveBeenCalledTimes(1)
|
||||
expect(store.getState().conflict).toMatchObject({ status: 'known', revision: 8 })
|
||||
store.getState().dismissConflict()
|
||||
expect(store.getState()).toMatchObject({ conflict: null, unsaved: true, savedRevision: 3 })
|
||||
})
|
||||
|
||||
it('reloads the server draft after confirmation: stacks cleared, copy removed', async () => {
|
||||
await conflicted(adminWith({}))
|
||||
const server = baseText().replace('"Fiche"', '"Serveur"')
|
||||
serverHas(server, 5)
|
||||
expect(await store.getState().reloadServerDraft({ confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
expect(await store.getState().reloadServerDraft({ confirmed: true })).toEqual({ ok: true })
|
||||
expect(store.getState()).toMatchObject({
|
||||
savedRevision: 5, unsaved: false, conflict: null, undo: [], redo: [], staleBase: false,
|
||||
})
|
||||
expect(store.getState().documentText).toBe(JSON.stringify(JSON.parse(server)))
|
||||
expect(storedCopy()).toBeNull()
|
||||
expect(sb.downloadSheetDrawing).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('discard and submit', () => {
|
||||
it('discards back to the saved text after confirmation, clears stacks, removes the copy', async () => {
|
||||
await openReady()
|
||||
const saved = store.getState().savedText
|
||||
commitMove(40)
|
||||
expect(await store.getState().discardChanges({ confirmed: false })).toMatchObject({ kind: 'confirmation_required' })
|
||||
expect(await store.getState().discardChanges({ confirmed: true })).toEqual({ ok: true })
|
||||
expect(store.getState()).toMatchObject({ documentText: saved, unsaved: false, undo: [], redo: [] })
|
||||
expect(storedCopy()).toBeNull()
|
||||
})
|
||||
|
||||
it('submits the saved revision and never re-submits on a conflict', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
sb.submitSheetReview.mockResolvedValue('review-1' as never)
|
||||
expect(await store.getState().submit()).toEqual({ ok: true, reviewId: 'review-1' })
|
||||
expect(sb.submitSheetReview).toHaveBeenCalledWith(expect.anything(), { version_id: VERSION, expected_revision: 3 })
|
||||
sb.submitSheetReview.mockRejectedValue(new SheetFailure('draft_conflict'))
|
||||
sb.getOfficialSheetAdmin.mockRejectedValue(new SheetFailure('network'))
|
||||
expect(await store.getState().submit()).toMatchObject({ kind: 'draft_conflict' })
|
||||
expect(sb.submitSheetReview).toHaveBeenCalledTimes(2)
|
||||
expect(store.getState().conflict).toEqual({ status: 'unknown' })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
/** Lot D: the history's byte bound and its steps. */
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
HISTORY_LIMIT_BYTES, emptyHistory, entryOf, recordCommit, stepBack, stepForward,
|
||||
} from '@/editor/state/undo'
|
||||
|
||||
const chunk = (bytes: number, tag: string) => tag + 'x'.repeat(bytes - tag.length)
|
||||
|
||||
describe('history', () => {
|
||||
it('measures the UTF-8 size of each entry once', () => {
|
||||
expect(entryOf('é').bytes).toBe(2)
|
||||
})
|
||||
|
||||
it('drops the oldest undo entries while both stacks exceed the bound', () => {
|
||||
const third = Math.floor(HISTORY_LIMIT_BYTES / 3) + 1
|
||||
let history = emptyHistory()
|
||||
for (const tag of ['a', 'b', 'c', 'd']) history = recordCommit(history, chunk(third, tag))
|
||||
expect(history.undo.map((e) => e.text[0])).toEqual(['c', 'd'])
|
||||
const total = history.undo.reduce((sum, e) => sum + e.bytes, 0)
|
||||
expect(total).toBeLessThanOrEqual(HISTORY_LIMIT_BYTES)
|
||||
})
|
||||
|
||||
it('counts the redo stack in the bound and clears it on a commit', () => {
|
||||
const half = Math.floor(HISTORY_LIMIT_BYTES / 2)
|
||||
let history = recordCommit(recordCommit(emptyHistory(), chunk(half, 'a')), chunk(half, 'b'))
|
||||
const back = stepBack(history, chunk(half, 'c'))
|
||||
expect(back?.text[0]).toBe('b')
|
||||
history = back!.history
|
||||
expect(history.redo).toHaveLength(1)
|
||||
expect(stepForward(history, 'now')?.text[0]).toBe('c')
|
||||
expect(recordCommit(history, 'z').redo).toEqual([])
|
||||
})
|
||||
|
||||
it('answers null with nothing to step to', () => {
|
||||
expect(stepBack(emptyHistory(), 'x')).toBeNull()
|
||||
expect(stepForward(emptyHistory(), 'x')).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user