diff --git a/src/client/EditorHost.tsx b/src/client/EditorHost.tsx index 17b1f940..4a808192 100644 --- a/src/client/EditorHost.tsx +++ b/src/client/EditorHost.tsx @@ -478,7 +478,7 @@ export function EditorHost(props: { )} {toolbar?.dirty === true && } - {toolbar?.editable === true && ( + {toolbar?.editable === true && toolbar?.truncated !== true && ( { if (update.docChanged) { @@ -215,7 +220,7 @@ export function TextEditor(props: FileViewerProps) { // tab's lifetime, and the dark flip is handled by the reconfigure // effect below (recreating the view here would drop the draft). // eslint-disable-next-line react-hooks/exhaustive-deps - }, [content, path]) + }, [content, path, truncated]) // Scheme flip: re-theme in place (the compartment holds only the // scheme-dependent extensions; everything else is untouched). @@ -290,6 +295,11 @@ export function TextEditor(props: FileViewerProps) { const save = (): void => { const view = viewRef.current if (view === null || savingRef.current) return + // Defense in depth for issue #732: a truncated read only holds the first + // readLimit bytes, and fs.write replaces the whole file — saving partial + // content would destroy the tail. The readOnly extension already blocks + // edits; this guards the keymap path against future trigger points. + if (truncated === true) return savingRef.current = true setSaveState('saving') api.fsWrite(scope, path, view.state.doc.toString()).then(() => { @@ -422,7 +432,7 @@ export function TextEditor(props: FileViewerProps) { const lastToolbarRef = useRef('') useEffect(() => { if (!hostToolbar) return - const state: EditorToolbarState = { modes: markdown || html, mode, dirty, editable, saveState } + const state: EditorToolbarState = { modes: markdown || html, mode, dirty, editable, truncated: truncated === true, saveState } const key = JSON.stringify(state) if (lastToolbarRef.current === key) return lastToolbarRef.current = key @@ -460,7 +470,7 @@ export function TextEditor(props: FileViewerProps) { )} {dirty && } - {editable && ( + {editable && truncated !== true && ( - {truncated === true && mode === 'edit' && {t('truncation')}} + {truncated === true && {t('truncation')}} { } }) + it('the header hides the save button for a truncated load (#732)', () => { + const { store, ctx } = setup() + const service = ctx.betterSidebar + // The TextEditor contract reports `truncated` alongside `editable`; the + // host's merged header must not offer save while only partial content + // is loaded (fs.write would replace the whole file with the prefix). + let report: ((truncated: boolean) => void) | undefined + const FakeViewer = (viewerProps: FileViewerProps): ReactNode => { + useEffect(() => { + viewerProps.onToolbarControls?.({ setMode: () => {}, save: () => {} }) + viewerProps.onToolbarState?.({ modes: true, mode: 'preview', dirty: false, editable: true, truncated: true, saveState: 'idle' }) + report = (truncated) => { + viewerProps.onToolbarState?.({ modes: true, mode: 'preview', dirty: false, editable: true, truncated, saveState: 'idle' }) + } + return () => { viewerProps.onToolbarControls?.(null) } + // Mount-only: re-running would re-fire the toolbar registration. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []) + return null + } + service.registerFileViewer({ + id: 'test:fake', + exts: ['fake'], + fetchStrategy: 'none', + component: FakeViewer, + }) + service.openTab({ type: 'editor', title: 'big.fake', path: '/tmp/big.fake', id: 'editor:/tmp/big.fake' }) + const fileTab = (): SidebarTab => + allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs) + .find(tab => tab.path === '/tmp/big.fake')! + const { container, unmount } = mountHost(ctx, store, fileTab) + try { + const header = container.querySelector('input')!.parentElement! + expect(header.querySelector('button[aria-label="Save"]')).toBeNull() + // A full read replacing the content re-enables saving. + act(() => { report!(false) }) + expect(header.querySelector('button[aria-label="Save"]')).not.toBeNull() + } finally { + unmount() + } + }) + it('a folder tab (meta.dir) renders the tree rooted at the folder, no editor chrome', () => { const { store, ctx } = setup() ctx.betterSidebar!.openTab({ diff --git a/tests/editor-truncated-save.spec.tsx b/tests/editor-truncated-save.spec.tsx new file mode 100644 index 00000000..9cac31d1 --- /dev/null +++ b/tests/editor-truncated-save.spec.tsx @@ -0,0 +1,63 @@ +/** + * 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): () => void { + return () => {} + } +} + +const CTX = {} as Parameters[0]['ctx'] + +function viewerProps(overrides: Partial = {}): 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"') + }) +})