Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

3.9 KB
Raw Blame
import { expect, it, vi } from "vitest";
import { RemoteDesktopViewerMedia } from "../remoteDesktopViewerMedia";
import type { DesktopViewerRequest } from "../remoteDesktopViewerSession";

function viewer(lease: string, request: DesktopViewerRequest) {
  const send = vi.fn();
  const onOfferFailure = vi.fn();
  const state = {
    lease: {
      lease,
      controlling: false,
      display: { id: "screen", name: "Screen", width: 800, height: 600 },
    },
    caps: {
      version: 1 as const,
      enabled: true,
      platform: "linux" as const,
      canControl: false,
      displays: [],
    },
    settings: { fps: 30 as const, quality: "auto" as const, audio: false },
  };
  const media = new RemoteDesktopViewerMedia({
    request,
    send,
    onOfferFailure,
    current: () => state,
    loadIce: async () => [],
  });
  return {
    media,
    send,
    onOfferFailure,
    offer: () =>
      media.handle({
        type: "offer",
        epoch: lease,
        attemptId: "attempt",
        sdp: "offer",
      }),
  };
}

it("keeps consent pending local to one viewer and leaves other signaling untouched", async () => {
  const transport = vi.fn(async (request: { lease?: string }) => {
    if (request.lease === "waiting") throw new Error("DESKTOP_CAPTURE_PENDING");
    return { sdp: "answer" };
  });
  const waiting = viewer("waiting", transport as DesktopViewerRequest);
  const other = viewer("other", transport as DesktopViewerRequest);
  await Promise.all([waiting.offer(), other.offer()]);
  expect(waiting.send).toHaveBeenCalledWith({
    type: "fallback",
    epoch: "waiting",
    attemptId: "attempt",
    retry: true,
    capturePending: true,
  });
  expect(waiting.onOfferFailure).not.toHaveBeenCalled();
  expect(other.send).toHaveBeenCalledWith({
    type: "answer",
    epoch: "other",
    attemptId: "attempt",
    sdp: "answer",
  });
  expect(transport).toHaveBeenCalledTimes(2);
});

it("does not classify an ordinary video failure as consent and discards a retired result", async () => {
  const request = vi.fn(async () => {
    throw new Error("DESKTOP_VIDEO_UNAVAILABLE");
  });
  const h = viewer("lease", request);
  await h.offer();
  expect(h.send).toHaveBeenLastCalledWith(
    expect.objectContaining({ capturePending: false }),
  );
  expect(h.onOfferFailure).toHaveBeenCalledOnce();
  h.send.mockClear();
  let reject!: (error: Error) => void;
  request.mockImplementationOnce(
    () =>
      new Promise<never>((_, no) => {
        reject = no;
      }),
  );
  const pending = h.offer();
  h.media.reset();
  reject(new Error("DESKTOP_CAPTURE_PENDING"));
  await pending;
  expect(h.send).not.toHaveBeenCalled();
  expect(h.onOfferFailure).toHaveBeenCalledOnce();
});

it("sends the tier with a legacy bitrate only to hosts that accept video settings", async () => {
  const request = vi.fn(async () => ({ sdp: "answer" }));
  const h = viewer("lease", request as unknown as DesktopViewerRequest);
  await h.offer();
  expect(request).toHaveBeenLastCalledWith(
    expect.not.objectContaining({ settings: expect.anything() }),
    expect.any(Function),
  );
  const caps = { videoSettings: true };
  const settings = {
    fps: 60 as const,
    quality: "saver" as const,
    audio: false,
  };
  const media = new RemoteDesktopViewerMedia({
    request: request as unknown as DesktopViewerRequest,
    send: vi.fn(),
    current: () => ({
      lease: {
        lease: "lease",
        controlling: false,
        display: { id: "s", name: "S", width: 1, height: 1 },
      },
      caps: {
        version: 1,
        enabled: true,
        platform: "darwin",
        canControl: true,
        displays: [],
        ...caps,
      },
      settings,
    }),
    loadIce: async () => [],
  });
  await media.handle({
    type: "offer",
    epoch: "lease",
    attemptId: "attempt",
    sdp: "offer",
  });
  expect(request).toHaveBeenLastCalledWith(
    expect.objectContaining({
      settings: { ...settings, bitrate: 2_000_000 },
    }),
    expect.any(Function),
  );
});