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。

30.4 KB
Raw Blame
// @vitest-environment jsdom
import { afterEach, expect, it, vi } from 'vitest';
import { DESKTOP_AUDIO_RETRY_MS } from '../../../../shared/remoteDesktop';
import { startDesktopCaptureHost } from '../captureHost';
import { nativeCaptureStream } from '../nativeCaptureStream';

vi.mock('../nativeCaptureStream', () => ({ nativeCaptureStream: vi.fn() }));
const disposers: Array<() => void> = [];
afterEach(() => {
  disposers.splice(0).forEach((dispose) => dispose());
  vi.unstubAllGlobals();
  vi.useRealTimers();
});
const flush = async () => {
  for (let i = 0; i < 30; i++) await Promise.resolve();
};
function media(audio = true) {
  const video = { kind: 'video', stop: vi.fn() };
  const sound = { kind: 'audio', stop: vi.fn() };
  const tracks = audio ? [video, sound] : [video];
  return {
    video,
    sound,
    getTracks: () => tracks,
    getVideoTracks: () => tracks.filter((track) => track.kind === 'video'),
    getAudioTracks: () => tracks.filter((track) => track.kind === 'audio'),
    addTrack: (track: typeof sound) => tracks.push(track),
    removeTrack: (track: typeof sound) => tracks.splice(tracks.indexOf(track), 1),
  };
}
function setup() {
  vi.useFakeTimers();
  const video = media(false);
  const nativeStop = vi.fn();
  const hold = vi.fn();
  const resume = vi.fn(() => true);
  vi.mocked(nativeCaptureStream).mockResolvedValue({
    stream: video,
    stop: nativeStop,
    clear: vi.fn(),
    hold,
    resume,
  } as any);
  const capture = vi.fn().mockRejectedValue(new Error('NotAllowedError'));
  vi.stubGlobal('navigator', { mediaDevices: { getDisplayMedia: capture } });
  const peers: Peer[] = [];
  class Peer {
    remoteReady = false;
    localDescription = { sdp: 'answer' };
    iceGatheringState = 'complete';
    close = vi.fn();
    addTrack = vi.fn((track: any) => {
      if (track.kind === 'audio') this.audio.sender.track = track;
    });
    audio = {
      direction: 'recvonly',
      receiver: { track: { kind: 'audio' } },
      sender: {
        track: undefined as any,
        setStreams: vi.fn(),
        replaceTrack: vi.fn(async (_track: unknown) => {}),
      },
    };
    video = {
      track: { kind: 'video' },
      getParameters: () => ({ encodings: [{}] }),
      setParameters: vi.fn(async (_parameters: unknown) => {}),
    };
    constructor() {
      peers.push(this);
    }
    getSenders() {
      return this.audio.sender.track ? [this.video, this.audio.sender] : [this.video];
    }
    getTransceivers() {
      expect(this.remoteReady).toBe(true);
      return [this.audio];
    }
    remote: { sdp?: string } | undefined;
    async setRemoteDescription(description?: { sdp?: string }) {
      this.remoteReady = true;
      this.remote = description;
    }
    async setLocalDescription() {}
    async createAnswer() {
      return {};
    }
  }
  vi.stubGlobal('RTCPeerConnection', Peer);
  let command!: (value: any) => void;
  const reply = vi.fn(async () => {});
  const api = {
    request: vi.fn(async (_lease: string, _request: unknown): Promise<any> => ({
      ok: true,
      result: { ok: true },
    })),
    stop: vi.fn(async () => {}),
    viewHeartbeat: vi.fn(async (_lease: string) => {}),
    input: vi.fn(async (_lease: string, _sequence: number, _events: unknown[]) => {}),
    nativeAudio: vi.fn(async () => new Uint8Array(0)),
    onCommand: (callback: typeof command) => {
      command = callback;
      return () => {};
    },
    registerHost: vi.fn(async () => {}),
    reply,
  };
  disposers.push(startDesktopCaptureHost(api as any));
  const offer = (
    audio = true,
    overlay = true,
    nativeAudio = false,
    quality?: 'auto' | 'saver' | 'hd',
    fps: 30 | 60 = 30,
    sdp = 'offer',
    background = false,
  ) =>
    command({
      id: 'offer',
      op: 'offer',
      lease: 'lease',
      sdp,
      attemptId: 'attempt',
      sourceId: 'screen:1',
      nativeCapture: true,
      nativeAudio,
      cursorOverlay: overlay,
      settings: { audio, fps, ...(quality ? { quality } : {}) },
      background,
    });
  const offerWithoutSettings = (sdp: string) =>
    command({
      id: 'offer',
      op: 'offer',
      lease: 'lease',
      sdp,
      attemptId: 'attempt',
      sourceId: 'screen:1',
      nativeCapture: true,
      cursorOverlay: true,
    });
  return {
    api,
    video,
    nativeStop,
    hold,
    resume,
    pause: (lease = 'lease') => command({ id: 'hold', op: 'display-hold', lease }),
    swap: (lease = 'lease') => command({ id: 'swap', op: 'display-swap', lease }),
    hide: (hidden: boolean, lease = 'lease') =>
      command({ id: 'hide', op: 'viewer-hidden', lease, hidden }),
    background: (background: boolean, lease = 'lease') =>
      command({ id: 'background', op: 'background-viewing', lease, background }),
    peers,
    capture,
    reply,
    offer,
    offerWithoutSettings,
    reset: (resume = false) =>
      command({ op: 'capture-reset', lease: 'lease', nativeAudio: resume }),
    stop: () => command({ op: 'stop' }),
  };
}

function nativeSound() {
  const sound = media().sound;
  const close = vi.fn(async () => {});
  vi.stubGlobal(
    'AudioContext',
    class {
      close = close;
      async resume() {}
      createMediaStreamDestination() {
        return { stream: { getTracks: () => [sound], getAudioTracks: () => [sound] } };
      }
    },
  );
  return { sound, close };
}

it.each([
  [undefined, 60, 'maintain-framerate', 60, 20_000_000, ''],
  ['auto', 60, 'maintain-framerate', 60, 20_000_000, ''],
  ['saver', 60, 'maintain-framerate', 60, 2_000_000, ''],
  ['saver', 30, 'maintain-framerate', 30, 2_000_000, ''],
  ['hd', 60, 'maintain-resolution', 60, 20_000_000, 'text'],
  ['hd', 30, 'maintain-resolution', 30, 20_000_000, 'text'],
] as const)(
  'applies the %s tier at %i fps as sender ceilings without restarting capture',
  async (quality, fps, degradationPreference, maxFramerate, maxBitrate, contentHint) => {
    const h = setup();
    h.offer(false, true, false, quality, fps);
    await flush();
    expect(h.peers[0].video.setParameters).toHaveBeenCalledExactlyOnceWith({
      degradationPreference,
      encodings: [{ maxFramerate, maxBitrate }],
    });
    expect(vi.mocked(nativeCaptureStream).mock.calls.at(-1)?.[4]).toBe(maxFramerate);
    expect((h.video.video as { contentHint?: string }).contentHint).toBe(contentHint);
    expect(h.peers).toHaveLength(1);
    expect(h.reply).toHaveBeenCalledWith('offer', 'answer');
    expect(h.nativeStop).not.toHaveBeenCalled();
  },
);

it('keeps full resolution while the screen is still only for tiers that ask for it', async () => {
  const h = setup();
  h.offer(false, true, false, 'auto');
  await flush();
  const onMotion = vi.mocked(nativeCaptureStream).mock.calls.at(-1)?.[5];
  expect(onMotion).toBeTypeOf('function');
  const video = h.peers[0].video.setParameters;
  onMotion!(false);
  await flush();
  expect(video).toHaveBeenLastCalledWith(
    expect.objectContaining({ degradationPreference: 'maintain-resolution' }),
  );
  onMotion!(true);
  await flush();
  expect(video).toHaveBeenLastCalledWith(
    expect.objectContaining({ degradationPreference: 'maintain-framerate' }),
  );

  // HD tracks motion too, since it may switch to the saver tier in the background,
  // but in the foreground it keeps full resolution either way.
  const hd = setup();
  hd.offer(false, true, false, 'hd');
  await flush();
  const hdMotion = vi.mocked(nativeCaptureStream).mock.calls.at(-1)?.[5];
  expect(hdMotion).toBeTypeOf('function');
  hdMotion!(false);
  await flush();
  hdMotion!(true);
  await flush();
  for (const [parameters] of hd.peers[0].video.setParameters.mock.calls)
    expect(parameters).toMatchObject({ degradationPreference: 'maintain-resolution' });
});

it('passes the tier bandwidth floor to the viewer offer, leaving legacy offers untouched', async () => {
  const sdp = 'v=0\r\nm=video 9 UDP/TLS/RTP/SAVPF 100\r\na=rtpmap:100 VP8/90000\r\n';
  const h = setup();
  h.offer(false, true, false, 'saver', 30, sdp);
  await flush();
  expect(h.peers[0].remote?.sdp).toContain('a=fmtp:100 x-google-start-bitrate=1500;');
  const legacy = setup();
  legacy.offerWithoutSettings(sdp);
  await flush();
  expect(legacy.peers.at(-1)?.remote?.sdp).toBe(sdp);
});

it('clears locked audio and restores its existing sender after unlock without Chromium capture', async () => {
  const h = setup();
  const old = nativeSound();
  h.offer(true, true, true);
  await flush();
  h.capture.mockClear();
  h.reset();
  expect(old.sound.stop).toHaveBeenCalledOnce();
  const next = nativeSound();
  h.reset(true);
  await flush();
  expect(h.peers[0].audio.sender.replaceTrack).toHaveBeenCalledWith(next.sound);
  expect(h.video.getAudioTracks()).toEqual([next.sound]);
  expect(h.capture).not.toHaveBeenCalled();
  expect(h.peers[0].close).not.toHaveBeenCalled();
  h.stop();
  expect(next.sound.stop).toHaveBeenCalled();
});

it.each(['lock', 'stop'])('discards late native audio recovery after %s', async (end) => {
  const h = setup();
  nativeSound();
  h.offer(true, true, true);
  await flush();
  const next = nativeSound();
  let resolve!: (bytes: Uint8Array<ArrayBuffer>) => void;
  h.api.nativeAudio.mockImplementationOnce(
    () =>
      new Promise((done) => {
        resolve = done;
      }),
  );
  h.reset(true);
  await flush();
  if (end === 'lock') h.reset();
  else h.stop();
  resolve(new Uint8Array(0));
  await flush();
  expect(next.sound.stop).toHaveBeenCalledOnce();
  expect(h.peers[0].audio.sender.replaceTrack).not.toHaveBeenCalled();
});

it.each(['startup', 'connected'] as const)(
  'isolates %s native audio failure from video and the lease',
  async (phase) => {
    const h = setup();
    const audio = nativeSound();
    if (phase === 'startup') h.api.nativeAudio.mockRejectedValueOnce(new Error('audio failed'));
    h.offer(true, true, true);
    await flush();
    expect(h.reply).toHaveBeenCalledWith('offer', 'answer');
    if (phase === 'connected') {
      h.api.nativeAudio.mockRejectedValueOnce(new Error('audio failed'));
      await vi.advanceTimersByTimeAsync(20);
    }
    expect(audio.sound.stop).toHaveBeenCalledOnce();
    expect(audio.close).toHaveBeenCalledOnce();
    expect(h.peers[0].close).not.toHaveBeenCalled();
    expect(h.nativeStop).not.toHaveBeenCalled();
    expect(h.video.video.stop).not.toHaveBeenCalled();
    expect(h.api.stop).not.toHaveBeenCalled();
  },
);

it.each(['resolve', 'reject'] as const)(
  'fences late native audio startup %s after a replacement offer',
  async (outcome) => {
    const h = setup();
    const old = nativeSound();
    let resolve!: (bytes: Uint8Array<ArrayBuffer>) => void;
    let reject!: (error: Error) => void;
    h.api.nativeAudio.mockImplementationOnce(
      () =>
        new Promise((yes, no) => {
          resolve = yes;
          reject = no;
        }),
    );
    h.offer(true, true, true);
    await flush();
    const current = nativeSound();
    h.offer(true, true, true);
    await flush();
    if (outcome === 'resolve') resolve(new Uint8Array(0));
    else reject(new Error('old audio failed'));
    await flush();
    expect(old.sound.stop).toHaveBeenCalledOnce();
    expect(current.sound.stop).not.toHaveBeenCalled();
    expect(h.peers).toHaveLength(1);
    expect(h.peers[0].close).not.toHaveBeenCalled();
    expect(h.api.stop).not.toHaveBeenCalled();
    expect(h.reply).toHaveBeenCalledTimes(1);
  },
);

it.each(['rejected', 'missing', 'without-overlay'])(
  'keeps video after %s audio and restores only its audio sender when permission becomes ready',
  async (mode) => {
    const h = setup();
    const empty = media(false);
    if (mode === 'missing') h.capture.mockResolvedValueOnce(empty);
    h.offer(true, mode !== 'without-overlay');
    await flush();
    const peer = h.peers[0];
    expect(h.reply).toHaveBeenCalledWith('offer', 'answer');
    expect(peer.audio.direction).toBe('sendonly');
    expect(peer.audio.sender.setStreams).toHaveBeenCalledWith(h.video);
    expect(h.video.video.stop).not.toHaveBeenCalled();
    const recovered = media();
    h.capture.mockResolvedValueOnce(recovered);
    await vi.advanceTimersByTimeAsync(DESKTOP_AUDIO_RETRY_MS[0]);
    expect(peer.audio.sender.replaceTrack).toHaveBeenCalledWith(recovered.sound);
    expect(h.peers).toHaveLength(1);
    expect(peer.close).not.toHaveBeenCalled();
    expect(recovered.video.stop).toHaveBeenCalledOnce();
    expect(recovered.sound.stop).not.toHaveBeenCalled();
    expect(h.video.getAudioTracks()).toEqual([recovered.sound]);
    await vi.advanceTimersByTimeAsync(120_000);
    expect(h.capture).toHaveBeenCalledTimes(2);
    h.stop();
    expect(recovered.sound.stop).toHaveBeenCalledOnce();
  },
);

it('exhausts audio retries without closing video or restarting capture', async () => {
  const h = setup();
  h.offer();
  await flush();
  await vi.advanceTimersByTimeAsync(120_000);
  expect(h.capture).toHaveBeenCalledTimes(1 + DESKTOP_AUDIO_RETRY_MS.length);
  expect(h.peers).toHaveLength(1);
  expect(h.peers[0].close).not.toHaveBeenCalled();
  expect(h.video.video.stop).not.toHaveBeenCalled();
  expect(h.nativeStop).not.toHaveBeenCalled();
});

it('waits for a timed-out permission request to settle before retrying and stops its late tracks', async () => {
  const h = setup();
  let finish!: (value: unknown) => void;
  h.capture.mockImplementationOnce(
    () =>
      new Promise((resolve) => {
        finish = resolve;
      }),
  );
  h.offer();
  await vi.advanceTimersByTimeAsync(60_000);
  expect(h.reply).toHaveBeenCalledWith('offer', 'answer');
  expect(h.capture).toHaveBeenCalledTimes(1);
  const late = media();
  finish(late);
  await flush();
  late.getTracks().forEach((track) => expect(track.stop).toHaveBeenCalledOnce());
  const recovered = media();
  h.capture.mockResolvedValueOnce(recovered);
  await vi.advanceTimersByTimeAsync(DESKTOP_AUDIO_RETRY_MS[0]);
  expect(h.peers[0].audio.sender.replaceTrack).toHaveBeenCalledWith(recovered.sound);
});

it.each(['capture', 'replaceTrack'] as const)(
  'fences late %s success after audio is disabled by a new offer',
  async (phase) => {
    const h = setup();
    h.offer();
    await flush();
    let finish!: (value?: any) => void;
    const recovered = media();
    if (phase === 'capture')
      h.capture.mockImplementationOnce(
        () =>
          new Promise((resolve) => {
            finish = resolve;
          }),
      );
    else {
      h.capture.mockResolvedValueOnce(recovered);
      h.peers[0].audio.sender.replaceTrack.mockImplementationOnce(
        () =>
          new Promise((resolve) => {
            finish = resolve;
          }),
      );
    }
    await vi.advanceTimersByTimeAsync(DESKTOP_AUDIO_RETRY_MS[0]);
    h.offer(false);
    await flush();
    finish(phase === 'capture' ? recovered : undefined);
    await flush();
    recovered.getTracks().forEach((track) => expect(track.stop).toHaveBeenCalledOnce());
    await vi.advanceTimersByTimeAsync(120_000);
    expect(h.capture).toHaveBeenCalledTimes(2);
    expect(h.peers[1].audio.sender.replaceTrack).not.toHaveBeenCalled();
    expect(h.peers[1].close).not.toHaveBeenCalled();
  },
);

it('cancels scheduled recovery on stop and never attempts audio for an audio-off offer', async () => {
  const h = setup();
  h.offer();
  await flush();
  h.stop();
  await vi.advanceTimersByTimeAsync(120_000);
  expect(h.capture).toHaveBeenCalledTimes(1);
  h.offer(false);
  await flush();
  await vi.advanceTimersByTimeAsync(120_000);
  expect(h.capture).toHaveBeenCalledTimes(1);
  expect(h.peers[1].audio.sender.setStreams).not.toHaveBeenCalled();
});

it('keeps retrying locally after replaceTrack rejects without dropping the video peer', async () => {
  const h = setup();
  h.offer();
  await flush();
  const failed = media();
  h.capture.mockResolvedValueOnce(failed);
  h.peers[0].audio.sender.replaceTrack.mockRejectedValueOnce(new Error('InvalidStateError'));
  await vi.advanceTimersByTimeAsync(DESKTOP_AUDIO_RETRY_MS[0]);
  failed.getTracks().forEach((track) => expect(track.stop).toHaveBeenCalledOnce());
  const recovered = media();
  h.capture.mockResolvedValueOnce(recovered);
  await vi.advanceTimersByTimeAsync(DESKTOP_AUDIO_RETRY_MS[1]);
  expect(h.peers[0].audio.sender.replaceTrack).toHaveBeenLastCalledWith(recovered.sound);
  expect(h.peers[0].close).not.toHaveBeenCalled();
});

it('answers control requests on the input channel without risking the session', async () => {
  const h = setup();
  h.offer(false);
  await flush();
  const [peer] = h.peers as any[];
  const channel: any = {
    label: 'input-v1',
    readyState: 'open',
    bufferedAmount: 0,
    send: vi.fn(),
    close: vi.fn(),
  };
  peer.ondatachannel({ channel });
  const replies = () =>
    channel.send.mock.calls
      .map(([data]: [string]) => JSON.parse(data))
      .filter((message: any) => message.type === 'reply');
  const ask = async (id: string, request: unknown) => {
    channel.onmessage({ data: JSON.stringify({ type: 'request', id, request }) });
    await flush();
  };
  await ask('mute', { op: 'hostMute', lease: 'lease', enabled: true });
  expect(h.api.request).toHaveBeenCalledWith('lease', {
    op: 'hostMute',
    lease: 'lease',
    enabled: true,
  });
  expect(replies().at(-1)).toEqual({ type: 'reply', id: 'mute', ok: true, result: { ok: true } });
  h.api.request.mockResolvedValueOnce({ ok: false, error: 'DESKTOP_VIEW_ONLY' });
  await ask('control', { op: 'control', lease: 'lease', enabled: true });
  expect(replies().at(-1)).toEqual({
    type: 'reply',
    id: 'control',
    ok: false,
    error: 'DESKTOP_VIEW_ONLY',
  });
  // Operations outside the channel set are refused, not fatal: the viewer
  // falls back to the relay and the session continues.
  h.api.request.mockClear();
  await ask('start', { op: 'start', displayId: '1' });
  await ask('list', { op: 'windowAction', lease: 'lease', action: 'list' });
  expect(h.api.request).not.toHaveBeenCalled();
  expect(
    replies()
      .slice(-2)
      .map((reply: any) => reply.error),
  ).toEqual(['DESKTOP_CHANNEL_UNSUPPORTED', 'DESKTOP_CHANNEL_UNSUPPORTED']);
  // A result too large for one channel message is reported, never truncated.
  h.api.request.mockResolvedValueOnce({ ok: true, result: 'x'.repeat(40_000) });
  await ask('modes', { op: 'displayModes', lease: 'lease' });
  expect(replies().at(-1)).toMatchObject({ id: 'modes', error: 'DESKTOP_REPLY_TOO_LARGE' });
  // Requests have their own bound; overflow is refused, not a session stop.
  h.api.request.mockImplementation(() => new Promise(() => {}));
  for (let i = 0; i < 9; i++) await ask(`slow-${i}`, { op: 'clipboardVersion', lease: 'lease' });
  expect(replies().at(-1)).toMatchObject({ id: 'slow-8', error: 'DESKTOP_CHANNEL_BUSY' });
  expect(h.api.stop).not.toHaveBeenCalled();
  expect(peer.close).not.toHaveBeenCalled();
  expect(channel.close).not.toHaveBeenCalled();
});

// Regression: nine valid batches during a slow IPC round trip used to stop the
// entire desktop. Unit coverage enters the real channel handler and checks that
// overload releases input before resuming without stopping video/lease or PiP.
it.each([false, true])(
  'preserves capture on input overload (release rejects: %s)',
  async (reject) => {
    const h = setup();
    h.offer(false);
    await flush();
    const [peer] = h.peers as any[];
    const channel: any = {
      label: 'input-v1',
      readyState: 'open',
      bufferedAmount: 0,
      send: vi.fn(),
      close: vi.fn(),
    };
    peer.ondatachannel({ channel });
    const inputs: Array<{ resolve: () => void; reject: () => void }> = [];
    h.api.input.mockImplementation(
      () =>
        new Promise<void>((resolve, reject) => {
          inputs.push({ resolve, reject: () => reject(new Error('Input unavailable')) });
        }),
    );
    const batch = (sequence: number) =>
      channel.onmessage?.({
        data: JSON.stringify({ sequence, events: [{ kind: 'scroll', dx: 0, dy: 1 }] }),
      });
    for (let sequence = 1; sequence <= 8; sequence++) batch(sequence);
    channel.onmessage({
      data: JSON.stringify({
        type: 'request',
        id: 'mute',
        request: { op: 'hostMute', lease: 'lease', enabled: true },
      }),
    });
    await flush();
    expect(JSON.parse(channel.send.mock.calls.at(-1)[0])).toEqual({
      type: 'reply',
      id: 'mute',
      ok: true,
      result: { ok: true },
    });
    expect(h.api.stop).not.toHaveBeenCalled();
    batch(9);
    expect(h.api.input).toHaveBeenLastCalledWith('lease', 9, [{ kind: 'release' }]);
    channel.onmessage({
      data: JSON.stringify({
        type: 'request',
        id: 'during-release',
        request: { op: 'clipboardVersion', lease: 'lease' },
      }),
    });
    batch(10); // Already queued channel messages must not replay after release.
    expect(h.api.input).toHaveBeenCalledTimes(9);
    inputs.slice(0, 8).forEach((input) => input.resolve());
    await flush();
    expect(JSON.parse(channel.send.mock.calls.at(-1)[0])).toMatchObject({
      id: 'during-release',
      ok: true,
    });
    batch(11); // Completion of earlier inputs cannot bypass the release fence.
    expect(h.api.input).toHaveBeenCalledTimes(9);
    await vi.advanceTimersByTimeAsync(2000);
    const ping = channel.send.mock.calls
      .map(([data]: [string]) => JSON.parse(data))
      .find((message: any) => message.type === 'viewPing');
    channel.onmessage({ data: ping.challenge });
    expect(h.api.viewHeartbeat).toHaveBeenCalledWith('lease');
    expect(channel.close).not.toHaveBeenCalled();
    if (reject) inputs[8].reject();
    else inputs[8].resolve();
    await flush();
    if (reject) expect(channel.close).toHaveBeenCalledOnce();
    else {
      expect(channel.close).not.toHaveBeenCalled();
      batch(12);
      expect(h.api.input).toHaveBeenLastCalledWith('lease', 12, [{ kind: 'scroll', dx: 0, dy: 1 }]);
    }
    expect(h.api.stop).not.toHaveBeenCalled();
    expect(peer.close).not.toHaveBeenCalled();
    expect(h.nativeStop).not.toHaveBeenCalled();
    expect(h.video.video.stop).not.toHaveBeenCalled();
  },
);

it.each(['{', JSON.stringify({ sequence: 9, events: [{ kind: 'scroll', dx: 0, dy: 2001 }] })])(
  'still rejects malformed input when the input channel is full: %s',
  async (data) => {
    const h = setup();
    h.offer(false);
    await flush();
    const channel: any = { label: 'input-v1', readyState: 'open', close: vi.fn() };
    (h.peers[0] as any).ondatachannel({ channel });
    h.api.input.mockImplementation(() => new Promise(() => {}));
    for (let sequence = 1; sequence <= 8; sequence++)
      channel.onmessage({ data: JSON.stringify({ sequence, events: [] }) });
    channel.onmessage({ data });
    expect(h.api.stop).toHaveBeenCalledOnce();
    expect(h.api.input).toHaveBeenCalledTimes(8);
  },
);

it('follows a display swap on the same peer without restarting capture', async () => {
  const h = setup();
  h.offer(false);
  await flush();
  const [peer] = h.peers;
  h.pause('other-lease');
  h.swap('other-lease');
  expect(h.hold).not.toHaveBeenCalled();
  expect(h.resume).not.toHaveBeenCalled();
  expect(h.reply).toHaveBeenLastCalledWith('swap', false);
  h.pause();
  expect(h.hold).toHaveBeenCalledOnce();
  h.swap();
  await flush();
  expect(h.resume).toHaveBeenCalledOnce();
  expect(h.reply).toHaveBeenLastCalledWith('swap', true);
  expect(h.peers).toHaveLength(1);
  expect(peer.close).not.toHaveBeenCalled();
  expect(h.nativeStop).not.toHaveBeenCalled();
  expect(h.api.stop).not.toHaveBeenCalled();
});

it('reports a browser-captured stream as not kept, so the video is rebuilt', async () => {
  const h = setup();
  // No cursor overlay: the browser capture succeeds and native is never used.
  h.capture.mockResolvedValueOnce(media(false));
  const nativeStarts = vi.mocked(nativeCaptureStream).mock.calls.length;
  h.offer(false, false);
  await flush();
  expect(h.reply).toHaveBeenCalledWith('offer', 'answer');
  expect(vi.mocked(nativeCaptureStream).mock.calls).toHaveLength(nativeStarts);
  h.pause();
  h.swap();
  await flush();
  expect(h.hold).not.toHaveBeenCalled();
  expect(h.reply).toHaveBeenLastCalledWith('swap', false);
});

it('reports a native stream that already ended as not kept', async () => {
  const h = setup();
  h.offer(false);
  await flush();
  h.resume.mockReturnValueOnce(false);
  h.pause();
  h.swap();
  await flush();
  expect(h.reply).toHaveBeenLastCalledWith('swap', false);
});

it('pauses and resumes the video encoder in place while the viewer is hidden', async () => {
  const h = setup();
  h.offer(false, true, false, 'auto');
  await flush();
  const [peer] = h.peers;
  const tuned = { maxFramerate: 30, maxBitrate: 20_000_000 };
  let current = { encodings: [{ ...tuned }] } as RTCRtpSendParameters;
  peer.video.getParameters = () => structuredClone(current) as any;
  const setParameters = vi.fn(async (next: any) => {
    current = structuredClone(next);
  });
  peer.video.setParameters = setParameters;

  h.hide(true, 'other-lease');
  await flush();
  expect(setParameters).not.toHaveBeenCalled();
  expect(h.reply).toHaveBeenLastCalledWith('hide', false);
  h.hide(true);
  await flush();
  expect(current.encodings).toEqual([{ ...tuned, active: false }]);
  expect(h.reply).toHaveBeenLastCalledWith('hide', true);
  // Motion updates keep the pause instead of overwriting it.
  vi.mocked(nativeCaptureStream).mock.calls.at(-1)?.[5]?.(false);
  await flush();
  expect(current).toMatchObject({
    encodings: [{ active: false }],
    degradationPreference: 'maintain-resolution',
  });
  h.hide(true);
  await flush();
  expect(setParameters).toHaveBeenCalledTimes(2);
  h.hide(false);
  await flush();
  expect(current.encodings).toEqual([{ ...tuned, active: true }]);
  expect(h.peers).toHaveLength(1);
  expect(peer.close).not.toHaveBeenCalled();
  expect(h.nativeStop).not.toHaveBeenCalled();
  expect(h.api.stop).not.toHaveBeenCalled();
});

it('applies a pause that arrives while the new peer is still being set up', async () => {
  const h = setup();
  h.offer(false, true, false, 'auto');
  h.hide(true);
  await flush();
  const [peer] = h.peers;
  expect(peer.video.setParameters).toHaveBeenLastCalledWith(
    expect.objectContaining({ encodings: [expect.objectContaining({ active: false })] }),
  );
});

it('reports a rejected encoder resume so the viewer rebuilds the video', async () => {
  const h = setup();
  h.offer(false, true, false, 'auto');
  await flush();
  const [peer] = h.peers;
  peer.video.setParameters = vi.fn(async () => {
    throw new Error('InvalidModificationError');
  });
  h.hide(false);
  await flush();
  expect(h.reply).toHaveBeenLastCalledWith('hide', false);
  // A rejected update does not block later ones.
  peer.video.setParameters = vi.fn(async (_parameters: unknown) => {});
  h.hide(true);
  await flush();
  expect(h.reply).toHaveBeenLastCalledWith('hide', true);
  expect(peer.close).not.toHaveBeenCalled();
});

it('caps a background viewer at the saver tier in place and restores its own tier', async () => {
  const h = setup();
  h.offer(false, true, false, 'hd', 60);
  await flush();
  const [peer] = h.peers;
  let current = structuredClone(peer.video.setParameters.mock.calls[0][0]) as RTCRtpSendParameters;
  peer.video.getParameters = () => structuredClone(current) as any;
  const setParameters = vi.fn(async (next: any) => {
    current = structuredClone(next);
  });
  peer.video.setParameters = setParameters;

  h.background(true, 'other-lease');
  await flush();
  expect(setParameters).not.toHaveBeenCalled();
  h.background(true);
  await flush();
  expect(current).toEqual({
    degradationPreference: 'maintain-framerate',
    encodings: [{ maxFramerate: 30, maxBitrate: 2_000_000 }],
  });
  h.background(true);
  await flush();
  expect(setParameters).toHaveBeenCalledOnce();
  h.background(false);
  await flush();
  expect(current).toEqual({
    degradationPreference: 'maintain-resolution',
    encodings: [{ maxFramerate: 60, maxBitrate: 20_000_000 }],
  });
  expect(h.peers).toHaveLength(1);
  expect(peer.close).not.toHaveBeenCalled();
  expect(h.nativeStop).not.toHaveBeenCalled();
});

it('starts a peer negotiated in the background at the saver ceilings, keeping its capture rate', async () => {
  const h = setup();
  h.offer(false, true, false, 'auto', 60, 'offer', true);
  await flush();
  expect(h.peers[0].video.setParameters).toHaveBeenCalledExactlyOnceWith({
    degradationPreference: 'maintain-framerate',
    encodings: [{ maxFramerate: 30, maxBitrate: 2_000_000 }],
  });
  // Returning to fullscreen only lifts the encoder ceilings; capture is not rebuilt.
  expect(vi.mocked(nativeCaptureStream).mock.calls.at(-1)?.[4]).toBe(60);
});

it('applies a background change that arrives while the new peer is still being set up', async () => {
  const h = setup();
  h.offer(false, true, false, 'auto', 60);
  h.background(true);
  await flush();
  expect(h.peers[0].video.setParameters).toHaveBeenLastCalledWith(
    expect.objectContaining({ encodings: [{ maxFramerate: 30, maxBitrate: 2_000_000 }] }),
  );
});

it('keeps a still HD screen sharp under the background saver ceilings', async () => {
  const h = setup();
  h.offer(false, true, false, 'hd', 60);
  await flush();
  const onMotion = vi.mocked(nativeCaptureStream).mock.calls.at(-1)?.[5];
  const video = h.peers[0].video.setParameters;
  h.background(true);
  await flush();
  expect(video).toHaveBeenLastCalledWith(
    expect.objectContaining({ degradationPreference: 'maintain-framerate' }),
  );
  onMotion!(false);
  await flush();
  expect(video).toHaveBeenLastCalledWith({
    degradationPreference: 'maintain-resolution',
    encodings: [{ maxFramerate: 30, maxBitrate: 2_000_000 }],
  });
});

it('converges a rejected background retune on the next heartbeat tick', async () => {
  const h = setup();
  h.offer(false, true, false, 'hd', 60);
  await flush();
  const [peer] = h.peers as any[];
  let current = structuredClone(peer.video.setParameters.mock.calls[0][0]);
  peer.video.getParameters = () => structuredClone(current);
  let reject = true;
  const setParameters = vi.fn(async (next: any) => {
    if (reject) throw new Error('InvalidModificationError');
    current = structuredClone(next);
  });
  peer.video.setParameters = setParameters;
  vi.stubGlobal('crypto', { randomUUID: () => 'challenge' });
  peer.ondatachannel({
    channel: { label: 'input-v1', readyState: 'open', bufferedAmount: 0, send: vi.fn() },
  });
  h.background(true);
  await flush();
  expect(current.encodings).toEqual([{ maxFramerate: 60, maxBitrate: 20_000_000 }]);
  reject = false;
  await vi.advanceTimersByTimeAsync(2000);
  expect(current.encodings).toEqual([{ maxFramerate: 30, maxBitrate: 2_000_000 }]);
  // A matching encoder is left untouched on later ticks.
  const calls = setParameters.mock.calls.length;
  await vi.advanceTimersByTimeAsync(4000);
  expect(setParameters).toHaveBeenCalledTimes(calls);
  expect(peer.close).not.toHaveBeenCalled();
});