feat(editor): session tag in history, confirm before leaving
This commit is contained in:
1 parent
4018344ffc
commit
07bebbc855
19 files changed
+844
-76
No files matched your search
@@ -45,6 +45,8 @@ export const testLayer: FindingLayer = {
|
||||
register: () => undefined,
|
||||
ask: () => ({ allowed: true }),
|
||||
askAll: () => ({ allowed: true }),
|
||||
holdLeave: () => undefined,
|
||||
takeLeave: () => null,
|
||||
},
|
||||
open: () => undefined,
|
||||
start: () => undefined,
|
||||
|
||||
@@ -11,6 +11,10 @@ import {
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type { Location, NavigateFunction } from 'react-router-dom'
|
||||
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
|
||||
import {
|
||||
forgetSessionTag,
|
||||
resolveSessionTag,
|
||||
} from '@/editor/history/sessionTag'
|
||||
import { useCitationDraft } from '@/editor/findings/citationDraft'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { edit, insert } from '../helpers/editorFixtures'
|
||||
@@ -51,7 +55,12 @@ const written = () =>
|
||||
(location.state as Record<string, HistoryEntry> | null)?.[ENTRY_KEY]
|
||||
const back = () => act(async () => navigate(-1))
|
||||
|
||||
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
|
||||
/** The session tag of USER in this tab: entries and flags carry it (criterion 22). */
|
||||
let TAG: string
|
||||
beforeEach(async () => {
|
||||
useLocaleStore.setState({ locale: 'fr' })
|
||||
TAG = await resolveSessionTag(USER)
|
||||
})
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
resetStore()
|
||||
@@ -485,7 +494,7 @@ describe('read-only flip and the gone rule', () => {
|
||||
view,
|
||||
below: null,
|
||||
sheetId: SHEET,
|
||||
userId: USER,
|
||||
tag: TAG,
|
||||
},
|
||||
}
|
||||
mountDraft({ probe, entries: [{ pathname: path, state }] })
|
||||
@@ -515,7 +524,7 @@ describe('read-only flip and the gone rule', () => {
|
||||
view,
|
||||
below: null,
|
||||
sheetId: SHEET,
|
||||
userId: USER,
|
||||
tag: TAG,
|
||||
},
|
||||
}
|
||||
mountDraft({ probe, entries: [{ pathname: path, state }] })
|
||||
@@ -586,7 +595,7 @@ describe('final review: the zone of a new category is part of the draft (criteri
|
||||
})
|
||||
|
||||
describe('final review: leaving the editor with a dirty dialog (criterion 17)', () => {
|
||||
const flagKey = `faunosteo:editor:left-draft:${SHEET}:${USER}`
|
||||
const flagKey = () => `faunosteo:editor:left-draft:${SHEET}:${TAG}`
|
||||
const notice =
|
||||
"Une saisie non enregistrée a été abandonnée en quittant l'éditeur"
|
||||
|
||||
@@ -596,7 +605,7 @@ describe('final review: leaving the editor with a dirty dialog (criterion 17)',
|
||||
await modify(user)
|
||||
await user.type(nameField(), ' bis')
|
||||
cleanup()
|
||||
const flag = sessionStorage.getItem(flagKey)
|
||||
const flag = sessionStorage.getItem(flagKey())
|
||||
expect(flag).not.toBeNull()
|
||||
expect(flag).not.toContain('bis')
|
||||
expect(JSON.parse(flag!)).toEqual({
|
||||
@@ -605,17 +614,133 @@ describe('final review: leaving the editor with a dirty dialog (criterion 17)',
|
||||
})
|
||||
await mount()
|
||||
expect(screen.getByText(notice)).toBeVisible()
|
||||
expect(sessionStorage.getItem(flagKey)).toBeNull()
|
||||
expect(sessionStorage.getItem(flagKey())).toBeNull()
|
||||
cleanup()
|
||||
await mount()
|
||||
expect(screen.queryByText(notice)).toBeNull()
|
||||
})
|
||||
|
||||
it('a sign-out leaves no flag under the tag it rotated', async () => {
|
||||
const user = await mount()
|
||||
await openFinding(user, /Node A/)
|
||||
await modify(user)
|
||||
await user.type(nameField(), ' bis')
|
||||
act(() => forgetSessionTag())
|
||||
await waitFor(() => expect(findingDialog()).toBeNull())
|
||||
const flags = Object.keys(sessionStorage).filter((k) =>
|
||||
k.startsWith('faunosteo:editor:left-draft:'),
|
||||
)
|
||||
expect(flags).toEqual([])
|
||||
})
|
||||
|
||||
it('a clean dialog leaves no flag', async () => {
|
||||
const user = await mount()
|
||||
await openFinding(user, /Node A/)
|
||||
cleanup()
|
||||
expect(sessionStorage.getItem(flagKey)).toBeNull()
|
||||
expect(sessionStorage.getItem(flagKey())).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
/** The Navigation API as the guard reads it: `navigate` events and `traverseTo`. */
|
||||
class NavigationStub extends EventTarget {
|
||||
traverseTo = vi.fn((key: string) => {
|
||||
void key
|
||||
return { committed: Promise.resolve(), finished: Promise.resolve() }
|
||||
})
|
||||
}
|
||||
|
||||
describe('a jump of several entries out of the editor (criterion 23)', () => {
|
||||
const flagKey = () => `faunosteo:editor:left-draft:${SHEET}:${TAG}`
|
||||
const notice =
|
||||
"Une saisie non enregistrée a été abandonnée en quittant l'éditeur"
|
||||
let api: NavigationStub
|
||||
beforeEach(() => {
|
||||
api = new NavigationStub()
|
||||
Object.defineProperty(window, 'navigation', {
|
||||
value: api,
|
||||
configurable: true,
|
||||
})
|
||||
})
|
||||
afterEach(() => {
|
||||
Reflect.deleteProperty(window, 'navigation')
|
||||
})
|
||||
|
||||
/** A history jump to `pathname`, as Chromium dispatches it before it commits. */
|
||||
function traverse(pathname: string, key: string, cancelable = true) {
|
||||
const url = new URL(pathname, window.location.origin).href
|
||||
const event = Object.assign(new Event('navigate', { cancelable }), {
|
||||
navigationType: 'traverse',
|
||||
destination: { url, key, sameDocument: true },
|
||||
})
|
||||
act(() => {
|
||||
api.dispatchEvent(event)
|
||||
})
|
||||
return event
|
||||
}
|
||||
|
||||
async function dirtyDialog() {
|
||||
const user = await mount()
|
||||
await openFinding(user, /Node A/)
|
||||
await modify(user)
|
||||
await user.type(nameField(), ' bis')
|
||||
return user
|
||||
}
|
||||
|
||||
it('is cancelled and asks first; « Continuer la saisie » keeps the draft', async () => {
|
||||
const user = await dirtyDialog()
|
||||
const jump = traverse('/app/admin/sheets', 'list')
|
||||
expect(jump.defaultPrevented).toBe(true)
|
||||
expect(inDialog().getByText('Abandonner les modifications ?')).toBeVisible()
|
||||
await user.click(
|
||||
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
|
||||
)
|
||||
expect(nameField()).toHaveValue('Node A bis')
|
||||
expect(api.traverseTo).not.toHaveBeenCalled()
|
||||
expect(sessionStorage.getItem(flagKey())).toBeNull()
|
||||
})
|
||||
|
||||
it('« Abandonner » goes to the destination, with no notice afterwards', async () => {
|
||||
const user = await dirtyDialog()
|
||||
traverse('/app/admin/sheets', 'list')
|
||||
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
|
||||
expect(api.traverseTo).toHaveBeenCalledWith('list')
|
||||
expect(traverse('/app/admin/sheets', 'list').defaultPrevented).toBe(false)
|
||||
cleanup()
|
||||
expect(sessionStorage.getItem(flagKey())).toBeNull()
|
||||
await mount()
|
||||
expect(screen.queryByText(notice)).toBeNull()
|
||||
})
|
||||
|
||||
it('« Abandonner » after « Continuer la saisie » closes the dialog only', async () => {
|
||||
const user = await dirtyDialog()
|
||||
traverse('/app/admin/sheets', 'list')
|
||||
await user.click(
|
||||
inDialog().getByRole('button', { name: 'Continuer la saisie' }),
|
||||
)
|
||||
await user.click(inDialog().getByRole('button', { name: 'Annuler' }))
|
||||
await user.click(inDialog().getByRole('button', { name: 'Abandonner' }))
|
||||
expect(api.traverseTo).not.toHaveBeenCalled()
|
||||
expect(findingDialog()).not.toBeNull()
|
||||
})
|
||||
|
||||
it('a jump that cannot be cancelled keeps the notice after the fact', async () => {
|
||||
await dirtyDialog()
|
||||
expect(traverse('/app/admin/sheets', 'list', false).defaultPrevented).toBe(
|
||||
false,
|
||||
)
|
||||
expect(screen.queryByText('Abandonner les modifications ?')).toBeNull()
|
||||
cleanup()
|
||||
expect(sessionStorage.getItem(flagKey())).not.toBeNull()
|
||||
})
|
||||
|
||||
it('a traversal inside the editor, or a clean dialog, is left alone', async () => {
|
||||
const user = await mount()
|
||||
await openFinding(user, /Node A/)
|
||||
expect(traverse('/app/admin/sheets', 'list').defaultPrevented).toBe(false)
|
||||
await modify(user)
|
||||
await user.type(nameField(), ' bis')
|
||||
expect(traverse(path, 'inside').defaultPrevented).toBe(false)
|
||||
expect(screen.queryByText('Abandonner les modifications ?')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -844,6 +844,8 @@ describe('the category form zone line (N1 review, C46)', () => {
|
||||
register: vi.fn(),
|
||||
ask: () => ({ allowed: true }),
|
||||
askAll: () => ({ allowed: true }),
|
||||
holdLeave: () => undefined,
|
||||
takeLeave: () => null,
|
||||
}}
|
||||
onCreated={vi.fn()}
|
||||
onClose={vi.fn()}
|
||||
|
||||
@@ -29,7 +29,7 @@ const entry = (patch: Partial<HistoryEntry> = {}): HistoryEntry => ({
|
||||
view: view('general'),
|
||||
below: null,
|
||||
sheetId: 'S',
|
||||
userId: 'U',
|
||||
tag: 'T',
|
||||
...patch,
|
||||
})
|
||||
const stateOf = (value: unknown) => ({ [ENTRY_KEY]: value })
|
||||
@@ -145,19 +145,34 @@ describe('readEntry and ownership', () => {
|
||||
expect(readEntry(stateOf({ ...entry(), view: 'x' }))).toBeNull()
|
||||
})
|
||||
|
||||
it('restores only for the sheet and the account that wrote it', () => {
|
||||
const mine = { sheetId: 'S', userId: 'U' }
|
||||
it('restores only for the sheet and the session tag that wrote it', () => {
|
||||
const mine = { sheetId: 'S', tag: 'T' }
|
||||
expect(ownedEntry(stateOf(entry()), mine)).not.toBeNull()
|
||||
expect(ownedEntry(stateOf(entry()), { ...mine, sheetId: 'T' })).toBeNull()
|
||||
expect(ownedEntry(stateOf(entry()), { ...mine, userId: 'V' })).toBeNull()
|
||||
expect(ownedEntry(stateOf(entry()), { ...mine, sheetId: 'X' })).toBeNull()
|
||||
expect(ownedEntry(stateOf(entry()), { ...mine, tag: 'V' })).toBeNull()
|
||||
})
|
||||
|
||||
it('refuses an owner without a sheet or an account', () => {
|
||||
const blank = entry({ sheetId: '', userId: '' })
|
||||
expect(ownedEntry(stateOf(blank), { sheetId: '', userId: '' })).toBeNull()
|
||||
expect(
|
||||
ownedEntry(stateOf(entry()), { sheetId: 'S', userId: '' }),
|
||||
).toBeNull()
|
||||
it('refuses an owner without a sheet or a tag: no tag yet restores nothing', () => {
|
||||
const blank = entry({ sheetId: '', tag: '' })
|
||||
expect(ownedEntry(stateOf(blank), { sheetId: '', tag: '' })).toBeNull()
|
||||
expect(ownedEntry(stateOf(entry()), { sheetId: 'S', tag: '' })).toBeNull()
|
||||
})
|
||||
|
||||
it('reads an entry of the older shape (an account id, no tag) as foreign', () => {
|
||||
const { id, view, below, sheetId } = entry()
|
||||
const older = { id, view, below, sheetId, userId: 'T' }
|
||||
expect(readEntry(stateOf(older))).toBeNull()
|
||||
expect(ownedEntry(stateOf(older), { sheetId: 'S', tag: 'T' })).toBeNull()
|
||||
})
|
||||
|
||||
it('never writes an account id in the entry', () => {
|
||||
expect(Object.keys(readEntry(stateOf(entry()))!).sort()).toEqual([
|
||||
'below',
|
||||
'id',
|
||||
'sheetId',
|
||||
'tag',
|
||||
'view',
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps only navigation: nothing typed survives the read', () => {
|
||||
@@ -362,11 +377,11 @@ describe('final review: read-back values are capped', () => {
|
||||
const onFindings = (key: ElementKey) =>
|
||||
view('findings', { finding: { key, citations: false } })
|
||||
|
||||
it('drops an entry whose id, sheet or account is over 128 characters', () => {
|
||||
it('drops an entry whose id, sheet or tag is over 128 characters', () => {
|
||||
expect(readEntry(stateOf(entry({ id: atCap })))).not.toBeNull()
|
||||
expect(readEntry(stateOf(entry({ id: long })))).toBeNull()
|
||||
expect(readEntry(stateOf(entry({ sheetId: long })))).toBeNull()
|
||||
expect(readEntry(stateOf(entry({ userId: long })))).toBeNull()
|
||||
expect(readEntry(stateOf(entry({ tag: long })))).toBeNull()
|
||||
})
|
||||
|
||||
it('drops a key with an over-long id or a path deeper than the tree allows', () => {
|
||||
|
||||
@@ -29,7 +29,7 @@ import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
afterEach(cleanup)
|
||||
|
||||
const owner = { sheetId: 'S', userId: 'U' }
|
||||
const owner = { sheetId: 'S', tag: 'T' }
|
||||
const finding: ElementKey = {
|
||||
kind: 'finding',
|
||||
zone_id: 'zone',
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type { Location, NavigateFunction } from 'react-router-dom'
|
||||
import { ENTRY_KEY, type HistoryEntry } from '@/editor/history/entry'
|
||||
import { resolveSessionTag } from '@/editor/history/sessionTag'
|
||||
import { defaultView } from '@/editor/navigation'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { emptyText } from '../helpers/editorFixtures'
|
||||
@@ -48,12 +49,15 @@ const foreign = (patch: Partial<HistoryEntry>) => ({
|
||||
view: defaultView('findings'),
|
||||
below: null,
|
||||
sheetId: SHEET,
|
||||
userId: USER,
|
||||
tag: TAG,
|
||||
...patch,
|
||||
},
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
/** The session tag of USER in this tab (criterion 22). */
|
||||
let TAG: string
|
||||
beforeEach(async () => {
|
||||
TAG = await resolveSessionTag(USER)
|
||||
useLocaleStore.setState({ locale: 'fr' })
|
||||
serverHas(emptyText(), 3, false)
|
||||
})
|
||||
@@ -75,7 +79,7 @@ describe('mount', () => {
|
||||
expect(written()?.view.tab).toBe(tabs[selected()])
|
||||
expect(written()).toMatchObject({
|
||||
sheetId: SHEET,
|
||||
userId: USER,
|
||||
tag: TAG,
|
||||
below: null,
|
||||
})
|
||||
})
|
||||
@@ -110,6 +114,20 @@ describe('mount', () => {
|
||||
expect(selected()).not.toBe('Constats')
|
||||
expect(written()?.id).not.toBe('old')
|
||||
})
|
||||
|
||||
it('an entry of another session tag, or of the older shape, is foreign', async () => {
|
||||
const older = foreign({}) as Record<string, Record<string, unknown>>
|
||||
older[ENTRY_KEY] = { ...older[ENTRY_KEY], tag: undefined, userId: USER }
|
||||
for (const state of [foreign({ tag: '0'.repeat(32) }), older]) {
|
||||
mountDraft({ probe, entries: [{ pathname: path, state }] })
|
||||
await ready()
|
||||
await waitFor(() => expect(written()?.tag).toBe(TAG))
|
||||
expect(selected()).not.toBe('Constats')
|
||||
expect(written()?.id).not.toBe('old')
|
||||
expect(JSON.stringify(written())).not.toContain(USER)
|
||||
cleanup()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('tab and section moves', () => {
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/** Criterion 22: the session tag the history entries and the left-draft flag carry. */
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
forgetSessionTag,
|
||||
resolveSessionTag,
|
||||
startSessionTagSync,
|
||||
useSessionTagStore,
|
||||
} from '@/editor/history/sessionTag'
|
||||
import { leftDraftKey } from '@/editor/leftDraft'
|
||||
import { useAuthStore } from '@/state/authStore'
|
||||
|
||||
const STORAGE_KEY = 'faunosteo:editor:session-tag'
|
||||
const stored = () =>
|
||||
JSON.parse(sessionStorage.getItem(STORAGE_KEY) ?? 'null') as {
|
||||
tag: string
|
||||
account: string
|
||||
} | null
|
||||
|
||||
/** A new page load of this tab: the memory goes, the tab's storage stays. */
|
||||
const reload = () => useSessionTagStore.setState({ held: null })
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
reload()
|
||||
sessionStorage.clear()
|
||||
useAuthStore.setState({ user: null })
|
||||
})
|
||||
|
||||
describe('resolveSessionTag', () => {
|
||||
it('draws a 128-bit hex tag, stored with the account digest, never the id', async () => {
|
||||
const tag = await resolveSessionTag('account-a')
|
||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||
const record = stored()
|
||||
expect(record?.tag).toBe(tag)
|
||||
expect(record?.account).toMatch(/^[0-9a-f]{64}$/)
|
||||
expect(sessionStorage.getItem(STORAGE_KEY)).not.toContain('account-a')
|
||||
})
|
||||
|
||||
it('reuses the tag for the same account, after a reload too', async () => {
|
||||
const tag = await resolveSessionTag('account-a')
|
||||
expect(await resolveSessionTag('account-a')).toBe(tag)
|
||||
reload()
|
||||
expect(await resolveSessionTag('account-a')).toBe(tag)
|
||||
})
|
||||
|
||||
it('rotates on another account and clears the flags of the old tag', async () => {
|
||||
const old = await resolveSessionTag('account-a')
|
||||
const flag = leftDraftKey({ sheetId: 'S', tag: old })
|
||||
sessionStorage.setItem(flag, '{}')
|
||||
reload()
|
||||
const tag = await resolveSessionTag('account-b')
|
||||
expect(tag).not.toBe(old)
|
||||
expect(sessionStorage.getItem(flag)).toBeNull()
|
||||
reload()
|
||||
expect(await resolveSessionTag('account-a')).not.toBe(old)
|
||||
})
|
||||
|
||||
it('keeps the tag in memory when the storage refuses: a reload restores nothing', async () => {
|
||||
vi.spyOn(sessionStorage, 'getItem').mockImplementation(() => {
|
||||
throw new Error('blocked')
|
||||
})
|
||||
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
||||
throw new Error('blocked')
|
||||
})
|
||||
const tag = await resolveSessionTag('account-a')
|
||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||
expect(await resolveSessionTag('account-a')).toBe(tag)
|
||||
reload()
|
||||
expect(await resolveSessionTag('account-a')).not.toBe(tag)
|
||||
})
|
||||
|
||||
it('keeps the tag in memory when no digest is available', async () => {
|
||||
vi.spyOn(crypto.subtle, 'digest').mockRejectedValue(new Error('insecure'))
|
||||
const tag = await resolveSessionTag('account-a')
|
||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||
expect(stored()).toBeNull()
|
||||
expect(await resolveSessionTag('account-a')).toBe(tag)
|
||||
})
|
||||
|
||||
it('reads a tampered record as none', async () => {
|
||||
sessionStorage.setItem(STORAGE_KEY, '{"tag":"x","account":"y"}')
|
||||
const tag = await resolveSessionTag('account-a')
|
||||
expect(tag).toMatch(/^[0-9a-f]{32}$/)
|
||||
expect(stored()?.tag).toBe(tag)
|
||||
})
|
||||
})
|
||||
|
||||
describe('forgetSessionTag and the sign-out', () => {
|
||||
it('rotates the tag of the same account and clears its flags', async () => {
|
||||
const old = await resolveSessionTag('account-a')
|
||||
const flag = leftDraftKey({ sheetId: 'S', tag: old })
|
||||
sessionStorage.setItem(flag, '{}')
|
||||
forgetSessionTag()
|
||||
expect(stored()).toBeNull()
|
||||
expect(sessionStorage.getItem(flag)).toBeNull()
|
||||
expect(await resolveSessionTag('account-a')).not.toBe(old)
|
||||
})
|
||||
|
||||
it('follows the account: a sign-out forgets, a sign-in resolves', async () => {
|
||||
const stop = startSessionTagSync()
|
||||
useAuthStore.setState({ user: { id: 'account-a' } as never })
|
||||
await vi.waitFor(() =>
|
||||
expect(useSessionTagStore.getState().held?.tag).toMatch(/^[0-9a-f]{32}$/),
|
||||
)
|
||||
const old = useSessionTagStore.getState().held!.tag
|
||||
useAuthStore.setState({ user: null })
|
||||
expect(useSessionTagStore.getState().held).toBeNull()
|
||||
expect(stored()).toBeNull()
|
||||
stop()
|
||||
expect(await resolveSessionTag('account-a')).not.toBe(old)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user