Files
apes-sidebar/tests/editor-truncated-save.spec.tsx
T
nanami 2bd72809d4 fix(editor): never save a truncated read over the full file (#732)
A text load truncated at readLimit only carries the file's first bytes,
but the editor still offered editing and saving on it: fs.write replaces
the whole file, so one Ctrl+S on a >512KB file silently destroyed
everything past the limit.

- render truncated documents read-only (EditorState.readOnly)
- guard save() against the truncated keymap path
- report `truncated` in EditorToolbarState and hide the save buttons
  (host header + the editor's own toolbar) while it is set
- show the truncation banner in preview mode too, so the read-only state
  is explained before any edit attempt
2026-09-21 02:14:46 +08:00

64 lines
2.1 KiB
TypeScript

/**
* Truncated-read save protection (#732): a text load truncated at readLimit
* only carries the file's first bytes, so the editor must not offer a save
* surface for it — the truncation banner shows in every mode and the save
* button stays out of the editor's own toolbar until a full read replaces
* the content.
*/
import { afterEach, describe, expect, it } from 'vitest'
import { createElement } from 'react'
import { renderToString } from 'react-dom/server'
import './browser-globals.ts'
import { TextEditor } from '../src/client/TextEditor.tsx'
import { createSidebarStore } from '../src/client/state.ts'
import { attachLocale } from '../src/client/locales.ts'
import type { FileViewerProps } from '../src/client/service.ts'
/** Minimal structural fake of the DSH LocaleService face the sidebar uses. */
class FakeLocale {
active: string = 'en'
getSnapshot(): { active: string } {
return { active: this.active }
}
subscribe(_fn: () => void): () => void {
return () => {}
}
register(_ns: string, _locale: string, _dict: Record<string, string>): () => void {
return () => {}
}
}
const CTX = {} as Parameters<typeof TextEditor>[0]['ctx']
function viewerProps(overrides: Partial<FileViewerProps> = {}): FileViewerProps {
return {
ctx: CTX,
store: createSidebarStore(),
scope: { sessionId: 's1', cwd: '/p' },
path: '/p/big.json',
title: 'big.json',
viewerId: 'code',
content: '{"head": "…"}',
...overrides,
}
}
afterEach(() => {
attachLocale(undefined)
})
describe('TextEditor truncated-read save protection (#732)', () => {
it('shows the truncation banner and no save button for a truncated load', () => {
attachLocale(new FakeLocale())
const html = renderToString(createElement(TextEditor, viewerProps({ truncated: true })))
expect(html).toContain('File too large')
expect(html).not.toContain('aria-label="Save"')
})
it('keeps the save button for a full (non-truncated) load', () => {
attachLocale(new FakeLocale())
const html = renderToString(createElement(TextEditor, viewerProps()))
expect(html).toContain('aria-label="Save"')
})
})