Build shared connections from authorized intent before preparing payloads. Select source paths independently for media, files and webpage bytes, retaining healthy leases through handoff and signaling recovery. Channel protocol 39 adds the required screen-helper intent projection. App control and persisted formats keep their existing identities.
157 lines
8.4 KiB
TypeScript
157 lines
8.4 KiB
TypeScript
import Hls, { LoaderContextType } from "hls.js";
|
|
import { afterEach, expect, test, vi } from "vitest";
|
|
import { WatchBytes } from "../src/client/channel/watch-bytes";
|
|
import { createResourceWatchMedia } from "../src/client/channel/watch-media";
|
|
import * as hosting from "../src/client/channel/hosting";
|
|
import type { WatchBytePlan } from "../src/client/channel/protocol";
|
|
|
|
vi.mock("../src/shared/media-resource", () => ({ mediaManifestBytes: () => 1_048_576 }));
|
|
|
|
const roomId = "room", activityId = crypto.randomUUID(), sessionId = crypto.randomUUID(), peer = crypto.randomUUID();
|
|
const source = { sessionId: peer, resourceId: crypto.randomUUID(), format: "file" as const };
|
|
const plan: WatchBytePlan = { roomId, activityId, source, upstream: peer, downstream: [], receiving: true };
|
|
class Delivery {
|
|
reads: string[] = []; cancels: string[] = [];
|
|
available = true;
|
|
update() { return "[]"; }
|
|
read(key: string, requestId: string) {
|
|
this.reads.push(requestId);
|
|
return JSON.stringify(this.available
|
|
? [{ type: "send_control", peer, control: { type: "pull", requestId, key: JSON.parse(key) } }]
|
|
: [{ type: "failed", requestId, code: "watch_path_unavailable" }]);
|
|
}
|
|
cancel(id: string) { this.cancels.push(id); return "[]"; }
|
|
control(_peer: string, control: string) { return control; }
|
|
chunk() { return "[]"; }
|
|
peer_closed() { return "[]"; }
|
|
read_failed() { return "[]"; }
|
|
request_failed() { return "[]"; }
|
|
free = vi.fn();
|
|
}
|
|
afterEach(() => { vi.restoreAllMocks(); vi.useRealTimers(); vi.unstubAllGlobals(); });
|
|
function setup() {
|
|
const delivery = new Delivery();
|
|
vi.spyOn(hosting, "loadRoomHosting").mockResolvedValue({ initializeRuntime: async () => {},
|
|
encode_control: (value: string) => new TextEncoder().encode(value),
|
|
WatchDelivery: class { constructor() { return delivery; } } } as unknown as Awaited<ReturnType<typeof hosting.loadRoomHosting>>);
|
|
const send = vi.fn(async () => {});
|
|
const bytes = new WatchBytes(send); bytes.update(sessionId, [plan], [{ peerId: peer, rooms: [roomId] }]);
|
|
return { bytes, delivery, send };
|
|
}
|
|
test("range waits for Core effects without polling and clears its deadline after receipt", async () => {
|
|
vi.useFakeTimers();
|
|
const { bytes, delivery } = setup();
|
|
try {
|
|
const reading = bytes.read(roomId, activityId, source, source.resourceId, 0, 1024, new AbortController().signal);
|
|
await vi.waitFor(() => expect(delivery.reads.length).toBeGreaterThan(0));
|
|
const count = delivery.reads.length, id = delivery.reads.at(-1)!;
|
|
await vi.advanceTimersByTimeAsync(2000);
|
|
expect(delivery.reads).toHaveLength(count);
|
|
|
|
bytes.data(peer, "control", JSON.stringify([{ type: "received", requestId: id, result: { mimeType: "video/mp4", totalSize: 3, bytes: [1, 2, 3], eof: true } }]));
|
|
await expect(reading).resolves.toMatchObject({ bytes: Uint8Array.of(1, 2, 3), eof: true });
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
} finally { bytes.reset(); }
|
|
});
|
|
test("waiting range cancels on deadline and cannot revive after reset", async () => {
|
|
vi.useFakeTimers();
|
|
const { bytes, delivery } = setup();
|
|
const reading = bytes.read(roomId, activityId, source, source.resourceId, 0, 1024, new AbortController().signal);
|
|
const rejected = expect(reading).rejects.toThrow("watch_read_timeout");
|
|
await vi.waitFor(() => expect(delivery.reads.length).toBeGreaterThan(0));
|
|
await vi.advanceTimersByTimeAsync(10000); await rejected;
|
|
expect(delivery.cancels).toContain(delivery.reads.at(-1));
|
|
bytes.reset(); await Promise.resolve();
|
|
expect(delivery.free).toHaveBeenCalledTimes(1);
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
});
|
|
test("queued range rejects a replaced byte source and abort releases a waiting read", async () => {
|
|
const { bytes, delivery } = setup();
|
|
try {
|
|
const cancel = new AbortController();
|
|
const reading = bytes.read(roomId, activityId, source, source.resourceId, 0, 1024, cancel.signal);
|
|
const rejected = expect(reading).rejects.toMatchObject({ name: "AbortError" });
|
|
await vi.waitFor(() => expect(delivery.reads.length).toBeGreaterThan(0));
|
|
cancel.abort(); await rejected;
|
|
const stale = bytes.read(roomId, activityId, source, source.resourceId, 0, 1024, new AbortController().signal);
|
|
const replaced = expect(stale).rejects.toThrow("watch_source_changed");
|
|
bytes.update(sessionId, [{ ...plan, receiving: false, source: { ...source, resourceId: crypto.randomUUID() } }], [{ peerId: peer, rooms: [roomId] }]);
|
|
await replaced;
|
|
} finally { bytes.reset(); }
|
|
});
|
|
|
|
test("a local media fetch waits for its join's receiving plan before sending its first Pull", async () => {
|
|
vi.useFakeTimers();
|
|
const { bytes, delivery } = setup();
|
|
bytes.update(sessionId, [], [{ peerId: peer, rooms: [roomId] }]);
|
|
const cancel = new AbortController();
|
|
const reading = bytes.read(roomId, activityId, source, source.resourceId, 0, 1024, cancel.signal);
|
|
const rejected = expect(reading).rejects.toMatchObject({ name: "AbortError" });
|
|
await vi.advanceTimersByTimeAsync(100);
|
|
expect(delivery.reads).toHaveLength(0);
|
|
bytes.update(sessionId, [plan], [{ peerId: peer, rooms: [roomId] }]);
|
|
await vi.waitFor(() => expect(delivery.reads).toHaveLength(1));
|
|
cancel.abort(); await rejected;
|
|
bytes.reset();
|
|
});
|
|
|
|
test.each(["response", "timeout", "cancel"] as const)("HLS waits beyond the old manifest deadline before %s", async outcome => {
|
|
vi.useFakeTimers();
|
|
const { bytes, delivery, send } = setup();
|
|
const hlsSource = { ...source, format: "hls" as const };
|
|
const routed = { ...plan, source: hlsSource };
|
|
const pairs = [{ peerId: peer, rooms: [roomId] }];
|
|
const callbacks = { onSuccess: vi.fn(), onError: vi.fn(), onTimeout: vi.fn(), onAbort: vi.fn() };
|
|
const video = Object.assign(new EventTarget(), { pause: vi.fn(), removeAttribute: vi.fn(), load: vi.fn(), remove: vi.fn() });
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
vi.spyOn(Hls, "isSupported").mockReturnValue(true);
|
|
const loadSource = vi.spyOn(Hls.prototype, "loadSource").mockImplementation(() => {});
|
|
const attached = new Promise<Hls>(resolve => {
|
|
vi.spyOn(Hls.prototype, "attachMedia").mockImplementation(function (this: Hls) { resolve(this); });
|
|
});
|
|
const media = createResourceWatchMedia(hlsSource, (id, offset, length, signal) =>
|
|
bytes.read(roomId, activityId, hlsSource, id, offset, length, signal), { hlsDeadlineOwner: "reader" });
|
|
try {
|
|
bytes.update(sessionId, [{ ...routed, upstream: null }], []);
|
|
const { config } = await attached;
|
|
const loadPolicy = config.manifestLoadPolicy.default;
|
|
const manifestURL = loadSource.mock.calls[0][0];
|
|
new config.loader(config).load({ type: LoaderContextType.MANIFEST, url: manifestURL, responseType: "text" },
|
|
{ loadPolicy, timeout: loadPolicy.maxLoadTimeMs, maxRetry: 0, retryDelay: 0, maxRetryDelay: 0 }, callbacks);
|
|
await vi.advanceTimersByTimeAsync(loadPolicy.maxLoadTimeMs + 5000);
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect(Object.values(callbacks).every(callback => callback.mock.calls.length === 0)).toBe(true);
|
|
|
|
if (outcome === "cancel") media.close();
|
|
bytes.update(sessionId, [routed], pairs);
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
if (outcome === "cancel") {
|
|
expect(callbacks.onAbort).toHaveBeenCalledOnce();
|
|
expect(send).not.toHaveBeenCalled();
|
|
} else {
|
|
expect(send).toHaveBeenCalledOnce();
|
|
const requestId = delivery.reads[0];
|
|
if (outcome === "response") {
|
|
const manifest = "#EXTM3U\n#EXT-X-ENDLIST\n";
|
|
const payload = Array.from(new TextEncoder().encode(manifest));
|
|
bytes.data(peer, "control", JSON.stringify([{ type: "received", requestId, result: {
|
|
mimeType: "application/vnd.apple.mpegurl", totalSize: payload.length, bytes: payload, eof: true,
|
|
} }]));
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(callbacks.onError).not.toHaveBeenCalled();
|
|
expect(callbacks.onSuccess).toHaveBeenCalledOnce();
|
|
expect(callbacks.onSuccess.mock.calls[0][0].data).toBe(manifest);
|
|
} else {
|
|
await vi.advanceTimersByTimeAsync(9999);
|
|
expect(callbacks.onError).not.toHaveBeenCalled();
|
|
await vi.advanceTimersByTimeAsync(1);
|
|
expect(callbacks.onError).toHaveBeenCalledOnce();
|
|
expect(callbacks.onError.mock.calls[0][0].text).toBe("watch_read_timeout");
|
|
expect(delivery.cancels).toContain(requestId);
|
|
}
|
|
}
|
|
expect(callbacks.onTimeout).not.toHaveBeenCalled();
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
} finally { media.close(); bytes.reset(); }
|
|
});
|