fix(app): set lone-surrogate texts aside or refuse them with a message (L1)
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.
This commit is contained in:
1 parent
20d03cf7fd
commit
da8a0370cb
12 files changed
+151
-11
No files matched your search
@@ -7,9 +7,14 @@
|
||||
*/
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import { importReportFromPdf, ImportError, MAX_PDF_BYTES } from '@/pdf/import'
|
||||
import {
|
||||
importAttachment,
|
||||
importReportFromPdf,
|
||||
ImportError,
|
||||
MAX_PDF_BYTES,
|
||||
} from '@/pdf/import'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet } from '../helpers/sheet'
|
||||
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
||||
|
||||
let sheet: SheetIndex
|
||||
beforeAll(async () => {
|
||||
@@ -46,3 +51,24 @@ describe('importReportFromPdf error kinds', () => {
|
||||
await rejectsWith(new Uint8Array(MAX_PDF_BYTES + 1), 'invalid_pdf')
|
||||
})
|
||||
})
|
||||
|
||||
describe('importAttachment on the core (L1)', () => {
|
||||
it('refuses an attachment with a lone surrogate as lone_surrogate, never rewritten', () => {
|
||||
for (const text of ['a\ud800b', '\udfff']) {
|
||||
const attachment = minimalReport(sheet, { conclusion: text })
|
||||
expect(() => importAttachment(attachment, sheet)).toThrow(
|
||||
expect.objectContaining({ kind: 'lone_surrogate' }),
|
||||
)
|
||||
expect(attachment.conclusion).toBe(text)
|
||||
}
|
||||
})
|
||||
|
||||
it('imports normal text with pairs and literal backslashes unchanged', () => {
|
||||
const text = 'é 😀 \\ud800 \ufffd'
|
||||
const imported = importAttachment(
|
||||
minimalReport(sheet, { conclusion: text }),
|
||||
sheet,
|
||||
)
|
||||
expect(imported.rebase.report.conclusion).toBe(text)
|
||||
})
|
||||
})
|
||||
@@ -4,7 +4,7 @@
|
||||
* then `importAttachment`, the core's layered read and rebase on the loaded
|
||||
* sheet): the migration alert (degraded headline, then the count), the three
|
||||
* ImportError kinds plus the generic error mapped to the FR messages, and the
|
||||
* navigation to /editor on success.
|
||||
* navigation to /editor on success; `lone_surrogate` (L1) has its own message.
|
||||
*/
|
||||
import {
|
||||
describe,
|
||||
@@ -171,6 +171,11 @@ describe('HomeRoute import: error paths', () => {
|
||||
new ImportError('no_attachment'),
|
||||
fr['home.import.error_no_attachment']!,
|
||||
],
|
||||
[
|
||||
'lone_surrogate → malformed text message, file untouched',
|
||||
new ImportError('lone_surrogate'),
|
||||
fr['home.import.error_lone_surrogate']!,
|
||||
],
|
||||
[
|
||||
'generic Error → generic message',
|
||||
new Error('disk on fire'),
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
/**
|
||||
* 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`) quarantine, any other failure
|
||||
* 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.
|
||||
@@ -57,6 +58,18 @@ describe('an unreadable draft', () => {
|
||||
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)
|
||||
@@ -108,6 +121,40 @@ describe('an unreadable archive beside a readable draft', () => {
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user