89 lines
3.2 KiB
TypeScript
89 lines
3.2 KiB
TypeScript
import { beforeEach, describe, expect, test } from "bun:test"
|
|||
|
|
import { useInputStore } from "./input-store"
|
||
|
|
|
||
|
|
class MockFileReader {
|
||
|
|
result: string | ArrayBuffer | null = null
|
||
|
|
onload: ((this: FileReader, event: ProgressEvent<FileReader>) => unknown) | null = null
|
||
|
|
|
||
|
|
readAsDataURL() {
|
||
|
|
pendingReaders.push(this)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const pendingReaders: MockFileReader[] = []
|
||
|
|
|
||
|
|
const resolveReader = (reader: MockFileReader, result: string) => {
|
||
|
|
reader.result = result
|
||
|
|
reader.onload?.call(reader as unknown as FileReader, {} as ProgressEvent<FileReader>)
|
||
|
|
}
|
||
|
|
|
||
|
|
describe("input-store attachments", () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
pendingReaders.length = 0
|
||
|
|
globalThis.FileReader = MockFileReader as unknown as typeof FileReader
|
||
|
|
useInputStore.setState({
|
||
|
|
pendingInputText: null,
|
||
|
|
pendingInputMode: "replace",
|
||
|
|
pendingSyntheticParts: null,
|
||
|
|
activeEditorFile: null,
|
||
|
|
})
|
||
|
|
useInputStore.getState().setAttachedFiles([])
|
||
|
|
})
|
||
|
|
|
||
|
|
test("does not attach a local file that finishes reading after attachments are cleared", async () => {
|
||
|
|
const addPromise = useInputStore.getState().addAttachedFile(new File(["hello"], "hello.txt", { type: "text/plain" }))
|
||
|
|
expect(pendingReaders).toHaveLength(1)
|
||
|
|
|
||
|
|
useInputStore.getState().clearAttachedFiles()
|
||
|
|
resolveReader(pendingReaders[0], "data:text/plain;base64,aGVsbG8=")
|
||
|
|
await addPromise
|
||
|
|
|
||
|
|
expect(useInputStore.getState().attachedFiles).toEqual([])
|
||
|
|
})
|
||
|
|
|
||
|
|
test("does not attach a local file after attached files are replaced", async () => {
|
||
|
|
const addPromise = useInputStore.getState().addAttachedFile(new File(["hello"], "hello.txt", { type: "text/plain" }))
|
||
|
|
expect(pendingReaders).toHaveLength(1)
|
||
|
|
|
||
|
|
useInputStore.getState().setAttachedFiles([])
|
||
|
|
resolveReader(pendingReaders[0], "data:text/plain;base64,aGVsbG8=")
|
||
|
|
await addPromise
|
||
|
|
|
||
|
|
expect(useInputStore.getState().attachedFiles).toEqual([])
|
||
|
|
})
|
||
|
|
|
||
|
|
test("does not attach a local file after attached files are restored", async () => {
|
||
|
|
const addPromise = useInputStore.getState().addAttachedFile(new File(["hello"], "hello.txt", { type: "text/plain" }))
|
||
|
|
expect(pendingReaders).toHaveLength(1)
|
||
|
|
|
||
|
|
const restored = new File(["restored"], "restored.txt", { type: "text/plain" })
|
||
|
|
useInputStore.getState().setAttachedFiles([{
|
||
|
|
id: "restored",
|
||
|
|
file: restored,
|
||
|
|
dataUrl: "data:text/plain;base64,cmVzdG9yZWQ=",
|
||
|
|
mimeType: "text/plain",
|
||
|
|
filename: "restored.txt",
|
||
|
|
size: restored.size,
|
||
|
|
source: "local",
|
||
|
|
}])
|
||
|
|
resolveReader(pendingReaders[0], "data:text/plain;base64,aGVsbG8=")
|
||
|
|
await addPromise
|
||
|
|
|
||
|
|
expect(useInputStore.getState().attachedFiles.map((file) => file.filename)).toEqual(["restored.txt"])
|
||
|
|
})
|
||
|
|
|
||
|
|
test("does not attach a VS Code selection that finishes reading after attachments are cleared", async () => {
|
||
|
|
const addPromise = useInputStore.getState().addVSCodeSelectionAttachment(
|
||
|
|
"/workspace/hello.txt",
|
||
|
|
new File(["hello"], "hello.txt", { type: "text/plain" })
|
||
|
|
)
|
||
|
|
expect(pendingReaders).toHaveLength(1)
|
||
|
|
|
||
|
|
useInputStore.getState().clearAttachedFiles()
|
||
|
|
resolveReader(pendingReaders[0], "data:text/plain;base64,aGVsbG8=")
|
||
|
|
await addPromise
|
||
|
|
|
||
|
|
expect(useInputStore.getState().attachedFiles).toEqual([])
|
||
|
|
})
|
||
|
|
})
|