A draft the core refuses as lone_surrogate is unreadable content: it
goes to osteo:quarantine:draft verbatim with the existing notice. An
import is refused as ImportError('lone_surrogate') with the owner's
wording (home.import.error_lone_surrogate, FR/EN), the file untouched.
The publish gate reads the kind as invalid_shape. Tests on the real
core: quarantine keeps the exact text, an editor action leaves the
report as it was, normal text with pairs and literal backslashes reads
unchanged.
199 lines
7.5 KiB
TypeScript
199 lines
7.5 KiB
TypeScript
/**
|
|
* R2 plan P12 (rev 2): a draft or an archive the running build cannot read is
|
|
* set aside under a quarantine key, never dropped; only the content failures
|
|
* of the core (`invalid`, `unsupported_version`, `lone_surrogate`: L1, a text
|
|
* the core would alter) quarantine, any other failure
|
|
* leaves both keys where they were; the source key is removed only once the
|
|
* quarantine write succeeded; a draft rebased with unresolved items keeps its
|
|
* raw pre-rebase text.
|
|
*/
|
|
import {
|
|
afterEach,
|
|
beforeAll,
|
|
beforeEach,
|
|
describe,
|
|
expect,
|
|
it,
|
|
vi,
|
|
} from 'vitest'
|
|
import { useReportStore } from '@/state/reportStore'
|
|
import { ARCHIVE_KEY, DRAFT_KEY, QUARANTINE_KEYS } from '@/state/persistence'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
|
|
|
let sheet: SheetIndex
|
|
beforeAll(async () => {
|
|
sheet = await loadTestSheet()
|
|
})
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
useReportStore.setState({
|
|
report: null,
|
|
archive: null,
|
|
activeObservationIndex: 0,
|
|
})
|
|
})
|
|
afterEach(() => vi.restoreAllMocks())
|
|
|
|
const restore = () => useReportStore.getState().restoreDraft(sheet)
|
|
|
|
/** A sheet whose core answers `kind` to every draft restore. */
|
|
function sheetFailingWith(kind: string): SheetIndex {
|
|
return {
|
|
...sheet,
|
|
ops: { restoreDraft: () => ({ ok: false, kind }) },
|
|
} as unknown as SheetIndex
|
|
}
|
|
|
|
describe('an unreadable draft', () => {
|
|
it.each([
|
|
['a known version that does not parse', JSON.stringify({ version: 3 })],
|
|
['an unknown version', JSON.stringify({ version: 7 })],
|
|
['text that is not JSON', '{not json'],
|
|
])('%s is moved to its quarantine key', (_label, raw) => {
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
expect(restore()).toEqual({ kind: 'quarantined' })
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBe(raw)
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
|
|
expect(useReportStore.getState().report).toBeNull()
|
|
})
|
|
|
|
it('with a lone surrogate (L1) is moved verbatim, the surrogate kept', () => {
|
|
const raw = JSON.stringify(minimalReport(sheet, { conclusion: 'a\ud800b' }))
|
|
expect(raw).toContain('\\ud800')
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
expect(restore()).toEqual({ kind: 'quarantined' })
|
|
const kept = sessionStorage.getItem(QUARANTINE_KEYS.draft)
|
|
expect(kept).toBe(raw)
|
|
expect(JSON.parse(kept!).conclusion).toBe('a\ud800b')
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull()
|
|
expect(useReportStore.getState().report).toBeNull()
|
|
})
|
|
|
|
it('stays where it is when the quarantine write fails (storage full)', () => {
|
|
const raw = JSON.stringify({ version: 7 })
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
const setItem = sessionStorage.setItem.bind(sessionStorage)
|
|
vi.spyOn(sessionStorage, 'setItem').mockImplementation((key, value) => {
|
|
if (key.startsWith('osteo:quarantine:')) {
|
|
throw new DOMException('full', 'QuotaExceededError')
|
|
}
|
|
setItem(key, value)
|
|
})
|
|
// Not announced as set aside: the editor stays put instead of leaving for Home.
|
|
expect(restore()).toEqual({ kind: 'unreadable_kept' })
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(raw)
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('a core failure while reading the draft', () => {
|
|
it.each(['core_unavailable', 'internal', 'rebase_failed'])(
|
|
'%s leaves the draft and the archive untouched',
|
|
(kind) => {
|
|
const draft = JSON.stringify(minimalReport(sheet))
|
|
sessionStorage.setItem(DRAFT_KEY, draft)
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{"kept":true}')
|
|
const result = useReportStore
|
|
.getState()
|
|
.restoreDraft(sheetFailingWith(kind))
|
|
expect(result).toEqual({ kind: 'core_unavailable' })
|
|
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(draft)
|
|
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBe('{"kept":true}')
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBeNull()
|
|
expect(useReportStore.getState().report).toBeNull()
|
|
},
|
|
)
|
|
})
|
|
|
|
describe('an unreadable archive beside a readable draft', () => {
|
|
it('goes to its quarantine key while the draft restores', () => {
|
|
sessionStorage.setItem(
|
|
DRAFT_KEY,
|
|
JSON.stringify(minimalReport(sheet, { conclusion: 'kept' })),
|
|
)
|
|
sessionStorage.setItem(ARCHIVE_KEY, '{not json')
|
|
expect(restore()).toMatchObject({ kind: 'restored' })
|
|
expect(useReportStore.getState().report?.conclusion).toBe('kept')
|
|
expect(useReportStore.getState().archive).toBeNull()
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe('{not json')
|
|
expect(sessionStorage.getItem(ARCHIVE_KEY)).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('a lone surrogate (L1) beside a readable draft or in the open report', () => {
|
|
it('sets the archive aside verbatim while the draft restores', () => {
|
|
sessionStorage.setItem(
|
|
DRAFT_KEY,
|
|
JSON.stringify(minimalReport(sheet, { conclusion: 'kept' })),
|
|
)
|
|
const archive = JSON.stringify({ format: 'x', note: '\udc00' })
|
|
sessionStorage.setItem(ARCHIVE_KEY, archive)
|
|
expect(restore()).toMatchObject({ kind: 'restored' })
|
|
expect(useReportStore.getState().report?.conclusion).toBe('kept')
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe(archive)
|
|
})
|
|
|
|
it('refuses a core action and leaves the report as it was', () => {
|
|
const report = minimalReport(sheet, { conclusion: 'a\ud800b' })
|
|
useReportStore.setState({ report })
|
|
expect(useReportStore.getState().addObservation()).toEqual({ ok: false })
|
|
expect(useReportStore.getState().report).toBe(report)
|
|
expect(useReportStore.getState().report?.conclusion).toBe('a\ud800b')
|
|
})
|
|
|
|
it('leaves normal text with pairs and literal backslashes unchanged', () => {
|
|
const text = 'é 😀 \\ud800 \ufffd'
|
|
sessionStorage.setItem(
|
|
DRAFT_KEY,
|
|
JSON.stringify(minimalReport(sheet, { conclusion: text })),
|
|
)
|
|
expect(restore()).toMatchObject({ kind: 'restored' })
|
|
expect(useReportStore.getState().report?.conclusion).toBe(text)
|
|
expect(useReportStore.getState().addObservation()).toEqual({ ok: true })
|
|
expect(useReportStore.getState().report?.conclusion).toBe(text)
|
|
})
|
|
})
|
|
|
|
describe('a draft rebased onto the loaded sheet', () => {
|
|
const OTHER_SHA = 'c'.repeat(64)
|
|
|
|
it('keeps its raw text when the rebase flagged items', () => {
|
|
const stale = minimalReport(sheet, {
|
|
sheet: { ...sheet.sheet.ref, documentSha256: OTHER_SHA },
|
|
observations: [
|
|
{
|
|
id: 'o',
|
|
zones: [{ annotationId: 'point-gone', viewId: 'main' }],
|
|
findings: [],
|
|
freeTextFindings: [],
|
|
comment: '',
|
|
},
|
|
],
|
|
})
|
|
const raw = JSON.stringify(stale)
|
|
sessionStorage.setItem(DRAFT_KEY, raw)
|
|
expect(restore()).toMatchObject({
|
|
kind: 'restored',
|
|
rebase: { rebased: true, unresolvedZones: 1 },
|
|
})
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draftPreRebase)).toBe(raw)
|
|
// The mirror now holds the rebased draft.
|
|
expect(
|
|
JSON.parse(sessionStorage.getItem(DRAFT_KEY)!).sheet.documentSha256,
|
|
).toBe(sheet.sheet.ref.documentSha256)
|
|
})
|
|
|
|
it('keeps nothing aside when the rebase resolved everything', () => {
|
|
const moved = minimalReport(sheet, {
|
|
sheet: { ...sheet.sheet.ref, documentSha256: OTHER_SHA },
|
|
})
|
|
sessionStorage.setItem(DRAFT_KEY, JSON.stringify(moved))
|
|
expect(restore()).toMatchObject({
|
|
kind: 'restored',
|
|
rebase: { rebased: true, unresolvedZones: 0, unresolvedFindings: 0 },
|
|
})
|
|
expect(sessionStorage.getItem(QUARANTINE_KEYS.draftPreRebase)).toBeNull()
|
|
})
|
|
})
|