fix(editor): read-only undo, upload session check, restore and submit guards

This commit is contained in:
bchanot committed 2026-11-04 18:06:40 +01:00
1 parent 5979cd59ec
commit 05c9b3e10b
20 files changed
+102 -33

No files matched your search

@@ -127,6 +127,28 @@ describe('drawing upload flow', () => {
).toBeTruthy()
})
it('a download that ends after another draft opened holds nothing in the new session', async () => {
const stored = serverGets(svg('0 0 200 100'))
let release: (bytes: Uint8Array) => void = () => {}
const held = new Promise<Uint8Array>((resolve) => (release = resolve))
sb.downloadSheetDrawing.mockImplementation(async (_c, hash) =>
hash === stored.sha256 ? held : await baseBytes(),
)
await pick(svg('0 0 200 100'))
await waitFor(() =>
expect(sb.downloadSheetDrawing).toHaveBeenCalledWith({}, stored.sha256),
)
store.getState().close()
await openReady()
const session = store.getState().session
release(stored.bytes)
await new Promise((done) => setTimeout(done, 50))
expect(store.getState().session).toBe(session)
expect(store.getState().drawings.map((d) => d.asset_id)).not.toContain(
NEW_ASSET,
)
})
it('a cleanable file shows the dialog with the core-composed drawing, then uploads on accept', async () => {
const dirty = svg(
'0 0 200 100',
@@ -147,6 +147,30 @@ describe('restore', () => {
expect(store.getState().conflict).toMatchObject({ status: 'known', revision: 5 })
})
it('stays unsaved under staleBase when edited back to the server text (no dead end)', async () => {
seed(movedText(10), 2)
await openReady({ revision: 5 })
await store.getState().restoreCopy()
expect(commitMove(50)).toMatchObject({ ok: true })
expect(store.getState().documentText).toBe(store.getState().savedText)
expect(store.getState()).toMatchObject({ staleBase: true, unsaved: true })
})
it('a restored copy holding bare nodes takes the core defaults-filled document', async () => {
const bare = JSON.parse(baseText())
bare.finding_zones = [
{
id: 'z', name: { fr: 'Z', en: 'Z' },
categories: [{ id: 'c', kind: 'dysfunction', name: { fr: 'C', en: 'C' }, children: [{ id: 'n', name: { fr: 'N', en: 'N' } }] }],
},
]
seed(JSON.stringify(bare), 3)
await openReady({ revision: 3 })
await store.getState().restoreCopy()
const node = JSON.parse(store.getState().documentText).finding_zones[0].categories[0].children[0]
expect(node).toMatchObject({ reference_ids: [], children: [], review_status: 'inherited' })
})
it('takes the reload path for « Annuler les modifications » under staleBase', async () => {
seed(movedText(10), 2)
await openReady({ revision: 5 })
+12
View File
@@ -334,6 +334,8 @@ describe('discard and submit', () => {
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 })
// The accepted submission froze the store; a fresh session meets the conflict.
await openReady({ revision: 3 })
sb.submitSheetReview.mockRejectedValue(new SheetFailure('draft_conflict'))
sb.getOfficialSheetAdmin.mockRejectedValue(new SheetFailure('network'))
expect(await store.getState().submit()).toMatchObject({ kind: 'draft_conflict' })
@@ -342,6 +344,16 @@ describe('discard and submit', () => {
})
})
describe('after the server accepted the submission', () => {
it('takes no further change before the sheet reload turns the draft frozen', async () => {
await openReady({ revision: 3 })
sb.submitSheetReview.mockResolvedValue('review-1' as never)
expect(await store.getState().submit()).toMatchObject({ ok: true })
expect(commitMove(40)).toMatchObject({ ok: false, reason: 'blocked', kind: 'read_only' })
expect(store.getState().unsaved).toBe(false)
})
})
describe('guards', () => {
it('refuses a submit while the document differs from the saved text', async () => {
await openReady()
@@ -67,13 +67,19 @@ describe('editor-wide undo', () => {
dialog.remove()
})
it('is off in a read-only mode', async () => {
mountDraft({ width: 800 })
it("is off in the narrow read-only mode, with the route's own store state", async () => {
mountDraft({ width: 1280 })
await ready()
store.getState().setCanEdit(true)
act(() => void commitMove(40))
store.getState().setCanEdit(false)
act(() => {
setWidth(800)
window.dispatchEvent(new Event('resize'))
})
await screen.findByText(/L'édition nécessite une largeur/)
// The route keeps the store able to hold a recovery copy in `narrow`: no forced flag.
expect(store.getState().canEdit).toBe(true)
ctrlZ(document.body)
expect(cx()).toMatchObject({ cx: 40 })
expect(store.getState().unsaved).toBe(true)
})
})