All files / qr-scanner/src/__testing__ camera.ts

100% Statements 43/43
95% Branches 19/20
100% Functions 19/19
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143                                                                                36x 36x   36x 25x       25x   28x   53x     25x 25x     36x 31x 6x 6x 6x   25x   25x       31x 31x 25x 82x       36x     28x                 36x                     36x 36x 36x   36x     13x           36x   35x 35x     36x   36x         14x 14x     11x 11x 11x 11x     36x 36x        
import { vi } from "vitest";
 
/**
 * A fake camera stack for the React-layer tests.
 *
 * jsdom has no `getUserMedia`, no `<video>` playback and no frame callbacks, so
 * the scanner's loop would never run at all. These doubles supply exactly the
 * pieces the loop touches — and, importantly, let a test *drive* frames rather
 * than wait for them, so nothing here depends on timing.
 */
 
export interface FakeCamera {
  /** Every track handed out, so a test can assert none survived. */
  readonly tracks: FakeTrack[];
  getUserMedia: ReturnType<typeof vi.fn>;
  enumerateDevices: ReturnType<typeof vi.fn>;
  /** Advance the video element by one presented frame. */
  emitFrame(): void;
  /** Fire `loadedmetadata`, as a stream announcing its dimensions does. */
  emitMetadata(video: HTMLVideoElement, width: number, height: number): void;
  restore(): void;
}
 
export interface FakeTrack {
  kind: string;
  readyState: string;
  stop: ReturnType<typeof vi.fn>;
  getSettings: () => MediaTrackSettings;
  getCapabilities: () => MediaTrackCapabilities;
  applyConstraints: ReturnType<typeof vi.fn>;
}
 
export interface FakeCameraOptions {
  devices?: Array<{ deviceId: string; label?: string }>;
  torch?: boolean;
  /** Reject with this `DOMException` name instead of granting. */
  failWith?: string;
}
 
export function installFakeCamera(options: FakeCameraOptions = {}): FakeCamera {
  const tracks: FakeTrack[] = [];
  const frameCallbacks: Array<() => void> = [];
 
  const makeTrack = (deviceId: string): FakeTrack => {
    const track: FakeTrack = {
      kind: "video",
      readyState: "live",
      stop: vi.fn(() => {
        track.readyState = "ended";
      }),
      getSettings: () => ({ deviceId }) as MediaTrackSettings,
      getCapabilities: () =>
        (options.torch ? { torch: true } : {}) as MediaTrackCapabilities,
      applyConstraints: vi.fn(async () => {}),
    };
    tracks.push(track);
    return track;
  };
 
  const getUserMedia = vi.fn(async (constraints: MediaStreamConstraints) => {
    if (options.failWith) {
      const error = new Error(options.failWith);
      error.name = options.failWith;
      throw error;
    }
    const video = constraints.video;
    const requested =
      typeof video === "object" && video && "deviceId" in video
        ? ((video.deviceId as { exact?: string })?.exact ?? "cam-1")
        : (options.devices?.[0]?.deviceId ?? "cam-1");
 
    const track = makeTrack(requested);
    return {
      getTracks: () => [track],
      getVideoTracks: () => [track],
    } as unknown as MediaStream;
  });
 
  const enumerateDevices = vi.fn(async () =>
    (options.devices ?? [{ deviceId: "cam-1" }]).map(
      (device) =>
        ({
          deviceId: device.deviceId,
          label: device.label ?? "",
          kind: "videoinput",
          groupId: "g",
        }) as MediaDeviceInfo,
    ),
  );
 
  vi.stubGlobal("navigator", {
    ...globalThis.navigator,
    mediaDevices: {
      getUserMedia,
      enumerateDevices,
      addEventListener: () => {},
      removeEventListener: () => {},
    },
  });
 
  // `<video>` in jsdom never plays and never reports a size.
  const videoProto = globalThis.HTMLVideoElement.prototype;
  const originalPlay = videoProto.play;
  videoProto.play = vi.fn(async () => {}) as typeof videoProto.play;
 
  Object.defineProperty(videoProto, "readyState", {
    configurable: true,
    get(this: HTMLVideoElement & { _readyState?: number }) {
      return this._readyState ?? 0;
    },
  });
 
  // The scanner prefers `requestVideoFrameCallback`; wiring it to a queue the
  // test drains is what makes these tests deterministic instead of timed.
  (videoProto as unknown as { requestVideoFrameCallback: unknown }).requestVideoFrameCallback = vi.fn(
    (callback: () => void) => {
      frameCallbacks.push(callback);
      return frameCallbacks.length;
    },
  );
  (videoProto as unknown as { cancelVideoFrameCallback: unknown }).cancelVideoFrameCallback = vi.fn();
 
  return {
    tracks,
    getUserMedia,
    enumerateDevices,
    emitFrame() {
      const pending = frameCallbacks.splice(0, frameCallbacks.length);
      for (const callback of pending) callback();
    },
    emitMetadata(video, width, height) {
      Object.defineProperty(video, "videoWidth", { configurable: true, value: width });
      Object.defineProperty(video, "videoHeight", { configurable: true, value: height });
      (video as HTMLVideoElement & { _readyState?: number })._readyState = 4;
      video.dispatchEvent(new Event("loadedmetadata"));
    },
    restore() {
      videoProto.play = originalPlay;
      vi.unstubAllGlobals();
    },
  };
}