Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Cause from "effect/Cause";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import * as Queue from "effect/Queue";
6import * as Stream from "effect/Stream";
7import { TestClock } from "effect/testing";
8
9import { STILL_FRAME_DELAY, repeatStillFrame } from "./mjpegStillFrame.ts";
10
11const part = (body: string) =>
12 Buffer.from(
13 `--frame\r\nContent-Type: image/jpeg\r\nContent-Length: ${body.length}\r\n\r\n${body}\r\n`,
14 );
15
16const viewer = Effect.gen(function* () {
17 const upstream = yield* Queue.unbounded<Uint8Array, Cause.Done>();
18 let received = "";
19 const fiber = yield* repeatStillFrame(Stream.fromQueue(upstream)).pipe(
20 Stream.runForEach((chunk) =>
21 Effect.sync(() => {
22 received += Buffer.from(chunk).toString();
23 }),
24 ),
25 Effect.forkChild,
26 );
27 const send = (...chunks: ReadonlyArray<Uint8Array>) =>
28 Effect.gen(function* () {
29 for (const chunk of chunks) yield* Queue.offer(upstream, chunk);
30 yield* Effect.yieldNow;
31 });
32 const finish = Effect.gen(function* () {
33 yield* Queue.end(upstream);
34 yield* Fiber.join(fiber);
35 return received;
36 });
37 return { send, finish };
38});
39
40describe("repeatStillFrame", () => {
41 it.effect("re-sends only the newest complete frame once when the screen stays still", () =>
42 Effect.gen(function* () {
43 const { send, finish } = yield* viewer;
44 yield* send(part("first"));
45 yield* TestClock.adjust(STILL_FRAME_DELAY);
46 yield* send(Buffer.concat([part("second"), part("third")]));
47 yield* TestClock.adjust(STILL_FRAME_DELAY);
48 yield* TestClock.adjust(STILL_FRAME_DELAY);
49 expect(yield* finish).toBe(
50 `${part("first")}${part("first")}${part("second")}${part("third")}${part("third")}`,
51 );
52 }),
53 );
54
55 it.effect("never repeats into a frame that is still arriving", () =>
56 Effect.gen(function* () {
57 const { send, finish } = yield* viewer;
58 const second = part("second");
59 // The pause lands inside the second part's headers.
60 yield* send(part("first"), second.subarray(0, 20));
61 yield* TestClock.adjust(STILL_FRAME_DELAY);
62 yield* send(second.subarray(20));
63 yield* TestClock.adjust(STILL_FRAME_DELAY);
64 expect(yield* finish).toBe(`${part("first")}${second}${second}`);
65 }),
66 );
67
68 it.effect("recognizes a frame whose final CRLF arrives in a later chunk", () =>
69 Effect.gen(function* () {
70 const { send, finish } = yield* viewer;
71 const first = part("first");
72 yield* send(first.subarray(0, -1), first.subarray(-1));
73 yield* TestClock.adjust(STILL_FRAME_DELAY);
74 expect(yield* finish).toBe(`${first}${first}`);
75 }),
76 );
77
78 it.effect("passes through bodies without the hub's part framing", () =>
79 Effect.gen(function* () {
80 const { send, finish } = yield* viewer;
81 const unframed = Buffer.from("--frame\r\nContent-Type: image/jpeg\r\n\r\nbody\r\n");
82 yield* send(unframed);
83 yield* TestClock.adjust(STILL_FRAME_DELAY);
84 yield* send(part("later"));
85 yield* TestClock.adjust(STILL_FRAME_DELAY);
86 expect(yield* finish).toBe(`${unframed}${part("later")}`);
87 }),
88 );
89});