The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import { FILE_TEXT_PREVIEW_MAX_BYTES } from "@t3tools/shared/filePreview"; |
| 3 | import { readFilePreviewResponse } from "./filePreview.ts"; |
| 4 | |
| 5 | describe("readFilePreviewResponse", () => { |
| 6 | it("cancels a pending read when its preview closes", async () => { |
| 7 | const controller = new AbortController(); |
| 8 | let cancelled = false; |
| 9 | const body = new ReadableStream<Uint8Array>({ |
| 10 | cancel() { |
| 11 | cancelled = true; |
| 12 | }, |
| 13 | }); |
| 14 | const result = readFilePreviewResponse(new Response(body), controller.signal); |
| 15 | controller.abort(); |
| 16 | await expect(result).rejects.toThrow(); |
| 17 | expect(cancelled).toBe(true); |
| 18 | }); |
| 19 | it("supports native AbortSignal implementations without throwIfAborted", async () => { |
| 20 | const signal = new AbortController().signal; |
| 21 | Object.defineProperty(signal, "throwIfAborted", { value: undefined }); |
| 22 | await expect(readFilePreviewResponse(new Response("native text"), signal)).resolves.toEqual({ |
| 23 | text: "native text", |
| 24 | truncated: false, |
| 25 | }); |
| 26 | }); |
| 27 | |
| 28 | it("stops consuming an unbounded response and cancels its stream", async () => { |
| 29 | let cancelled = false; |
| 30 | const body = new ReadableStream<Uint8Array>({ |
| 31 | pull(controller) { |
| 32 | controller.enqueue(new Uint8Array(FILE_TEXT_PREVIEW_MAX_BYTES).fill(97)); |
| 33 | }, |
| 34 | cancel() { |
| 35 | cancelled = true; |
| 36 | }, |
| 37 | }); |
| 38 | const result = await readFilePreviewResponse(new Response(body), new AbortController().signal); |
| 39 | expect(result.text.length).toBe(FILE_TEXT_PREVIEW_MAX_BYTES); |
| 40 | expect(result.truncated).toBe(true); |
| 41 | expect(cancelled).toBe(true); |
| 42 | }); |
| 43 | }); |