File size: 1,160 Bytes
e079a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// <reference types="bun-types" />

import {describe, expect, test} from "bun:test"

import {readBoundedByteStream} from "../boundedByteStream"

describe("readBoundedByteStream", () => {
  test("joins chunks up to the byte ceiling", async () => {
    const stream = new ReadableStream<Uint8Array>({
      start(controller) {
        controller.enqueue(new Uint8Array([1, 2]))
        controller.enqueue(new Uint8Array([3]))
        controller.close()
      },
    })

    expect([...(await readBoundedByteStream(stream, 3))]).toEqual([1, 2, 3])
    expect(stream.locked).toBe(false)
  })

  test("cancels a chunked response as soon as it exceeds the ceiling", async () => {
    let cancelled = false
    const stream = new ReadableStream<Uint8Array>({
      start(controller) {
        controller.enqueue(new Uint8Array([1, 2]))
        controller.enqueue(new Uint8Array([3, 4]))
      },
      cancel() {
        cancelled = true
      },
    })

    await expect(readBoundedByteStream(stream, 3)).rejects.toThrow("response exceeds 3 bytes")
    expect(cancelled).toBe(true)
    expect(stream.locked).toBe(false)
  })
})