Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fullFileText.test.ts

74 lines2.5 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Stream from "effect/Stream";
5import { FetchHttpClient } from "effect/http";
6​
7import { downloadText } from "./fullFileText";
8import { RENDERED_MAX_LINES } from "./largeTextProtocol";
9​
10const encoder = new TextEncoder();
11​
12const downloadChunks = (chunks: ReadonlyArray<Uint8Array>) =>
13 downloadText("http://localhost/assets/file.ts").pipe(
14 Stream.runLast,
15 Effect.map(Option.getOrThrow),
16 Effect.provide(FetchHttpClient.layer),
17 Effect.provideService(FetchHttpClient.Fetch, () =>
18 Promise.resolve(
19 new Response(
20 new ReadableStream<Uint8Array>({
21 start(controller) {
22 for (const chunk of chunks) {
23 controller.enqueue(chunk.slice());
24 }
25 controller.close();
26 },
27 }),
28 ),
29 ),
30 ),
31 );
32​
33const download = (file: Uint8Array) => downloadChunks([file]);
34​
35describe("downloadText", () => {
36 it.effect(
37 "drops a leading byte order mark as the preview read does, and keeps a later U+FEFF",
38 () =>
39 Effect.gen(function* () {
40 const whole = new Uint8Array([0xef, 0xbb, 0xbf, ...encoder.encode("one\ntwo\n")]);
41 expect((yield* download(whole)).text).toBe("one\ntwo\n");
42 }),
43 );
44​
45 it.effect("stops at the first text past the lines the main thread can lay out", () =>
46 Effect.gen(function* () {
47 const file = yield* download(encoder.encode("\n".repeat(RENDERED_MAX_LINES + 10)));
48 expect(file.lines).toBeGreaterThan(RENDERED_MAX_LINES);
49 }),
50 );
51​
52 it.effect("enforces the line limit for carriage-return line endings", () =>
53 Effect.gen(function* () {
54 const cr = yield* download(encoder.encode("x\r".repeat(RENDERED_MAX_LINES + 1)));
55 const lf = yield* download(encoder.encode("x\n".repeat(RENDERED_MAX_LINES + 1)));
56 expect(cr.lines).toBeGreaterThan(RENDERED_MAX_LINES);
57 expect(cr.lines).toBe(lf.lines);
58 }),
59 );
60​
61 it.effect("counts a CRLF split across streamed chunks as a single line break", () =>
62 Effect.gen(function* () {
63 const file = yield* downloadChunks([
64 encoder.encode("one\r"),
65 encoder.encode("\ntwo\r"),
66 encoder.encode(""),
67 encoder.encode("\nthree\r"),
68 encoder.encode("\rfour"),
69 ]);
70 expect(file.text).toBe("one\r\ntwo\r\nthree\r\rfour");
71 expect(file.lines).toBe(5);
72 }),
73 );
74});