fix(sync): settle failed attachment reads (#1193)

Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
Isaac Sanchez-Hawkins
2026-05-12 11:09:31 +03:00
committed by GitHub
co-authored by Isaac Sanchez
parent f4ec7d6c1b
commit f7c5144c62
2 changed files with 51 additions and 10 deletions
+34
View File
@@ -4,6 +4,9 @@ import { useInputStore } from "./input-store"
class MockFileReader {
result: string | ArrayBuffer | null = null
onload: ((this: FileReader, event: ProgressEvent<FileReader>) => unknown) | null = null
onerror: ((this: FileReader, event: ProgressEvent<FileReader>) => unknown) | null = null
onabort: ((this: FileReader, event: ProgressEvent<FileReader>) => unknown) | null = null
error: DOMException | null = null
readAsDataURL() {
pendingReaders.push(this)
@@ -17,6 +20,11 @@ const resolveReader = (reader: MockFileReader, result: string) => {
reader.onload?.call(reader as unknown as FileReader, {} as ProgressEvent<FileReader>)
}
const rejectReader = (reader: MockFileReader) => {
reader.error = new DOMException("read failed", "NotReadableError")
reader.onerror?.call(reader as unknown as FileReader, {} as ProgressEvent<FileReader>)
}
describe("input-store attachments", () => {
beforeEach(() => {
pendingReaders.length = 0
@@ -85,4 +93,30 @@ describe("input-store attachments", () => {
expect(useInputStore.getState().attachedFiles).toEqual([])
})
test("does not leave local file reads pending after a reader error", async () => {
const addPromise = useInputStore.getState().addAttachedFile(new File(["hello"], "hello.txt", { type: "text/plain" }))
expect(pendingReaders).toHaveLength(1)
rejectReader(pendingReaders[0])
await addPromise
expect(useInputStore.getState().attachedFiles).toEqual([])
})
test("cleans up pending VS Code selection keys after a reader error", async () => {
const file = new File(["hello"], "hello.txt", { type: "text/plain" })
const firstAdd = useInputStore.getState().addVSCodeSelectionAttachment("/workspace/hello.txt", file)
expect(pendingReaders).toHaveLength(1)
rejectReader(pendingReaders[0])
await firstAdd
const secondAdd = useInputStore.getState().addVSCodeSelectionAttachment("/workspace/hello.txt", file)
expect(pendingReaders).toHaveLength(2)
resolveReader(pendingReaders[1], "data:text/plain;base64,aGVsbG8=")
await secondAdd
expect(useInputStore.getState().attachedFiles.map((attached) => attached.filename)).toEqual(["hello.txt"])
})
})