Timeline testing
Use fromTimeline to script value and error arrivals and assert the results. Times are milliseconds from the first read.
ts
import { catchError, fromTimeline, of, timeoutEach } from "mogami-stream";
export function delayedResponse() {
return fromTimeline([
{ at: 0, kind: "value", value: "A" },
{ at: 100, kind: "value", value: "B" },
{ at: 150, kind: "complete" },
]).pipe(timeoutEach(250));
}
export function fallbackOnFailure() {
return fromTimeline<string>([
{ at: 0, kind: "value", value: "cached" },
{ at: 50, kind: "error", error: new Error("upstream offline") },
]).pipe(catchError(() => of("fallback")));
}These checks use Node.js assert:
ts
import assert from "node:assert/strict";
assert.deepEqual(await delayedResponse().toArray(), ["A", "B"]);
assert.deepEqual(await fallbackOnFailure().toArray(), ["cached", "fallback"]);
await assert.rejects(
fromTimeline([
{ at: 50, kind: "error", error: new Error("offline") },
]).toArray(),
/offline/,
);This is a pull-driven source with real waits. No timer runs while downstream is idle, and completion or errors wait for the next read too. Events requested after their scheduled time are processed immediately.
Use finite, non-negative timestamps in non-decreasing order. Equal times follow array order; completion or error may appear only last. Without a terminal event, the source completes after its final value. Invalid schedules throw during construction. See fromTimeline for the full contract.