Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

5.8 KB
Raw Blame
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { createTextCycle, startTextRotation } from "../src/client/ui/text-rotation";

const page = Object.assign(new EventTarget(), { visibilityState: "visible" });
const motion = Object.assign(new EventTarget(), { matches: false });
let stop: (() => void) | undefined;
let intersect: IntersectionObserverCallback;
const disconnect = vi.fn();

beforeEach(() => {
  vi.useFakeTimers();
  page.visibilityState = "visible";
  motion.matches = false;
  vi.stubGlobal("document", page);
  vi.stubGlobal("window", { setInterval, clearInterval, matchMedia: () => motion });
  vi.stubGlobal("IntersectionObserver", class {
    constructor(callback: IntersectionObserverCallback) { intersect = callback; }
    observe() {}
    disconnect = disconnect;
  });
});

afterEach(() => {
  stop?.();
  stop = undefined;
  vi.restoreAllMocks();
  vi.unstubAllGlobals();
  vi.useRealTimers();
});

it("exhausts each pool before repeating and avoids repeating across a round boundary", () => {
  vi.spyOn(Math, "random").mockReturnValue(0.99);
  const next = createTextCycle(["A", "B", "C"]);
  const first = [next(), next(), next()];
  vi.mocked(Math.random).mockReturnValue(0);
  const second = [next(), next(), next()];
  expect(new Set(first)).toEqual(new Set(["A", "B", "C"]));
  expect(new Set(second)).toEqual(new Set(first));
  expect(second[0]).not.toBe(first[2]);
  expect(createTextCycle([])()).toBeUndefined();
  const single = createTextCycle(["Only"]);
  expect([single(), single()]).toEqual(["Only", "Only"]);
});

it("advances on the shared fifteen-second waiting cadence", () => {
  const tick = vi.fn();
  stop = startTextRotation(tick);
  vi.advanceTimersByTime(14_999);
  expect(tick).not.toHaveBeenCalled();
  vi.advanceTimersByTime(1);
  expect(tick).toHaveBeenCalledTimes(1);
  vi.advanceTimersByTime(30_000);
  expect(tick).toHaveBeenCalledTimes(3);
});

it("does not consume hidden entries and resumes with a full reading interval", () => {
  const tick = vi.fn();
  stop = startTextRotation(tick);
  vi.advanceTimersByTime(6_000);
  page.visibilityState = "hidden";
  page.dispatchEvent(new Event("visibilitychange"));
  expect(vi.getTimerCount()).toBe(0);
  vi.advanceTimersByTime(60_000);
  page.visibilityState = "visible";
  page.dispatchEvent(new Event("visibilitychange"));
  vi.advanceTimersByTime(14_999);
  expect(tick).not.toHaveBeenCalled();
  vi.advanceTimersByTime(1);
  expect(tick).toHaveBeenCalledTimes(1);
});

it("honors reduced motion at entry and when the preference changes", () => {
  motion.matches = true;
  const tick = vi.fn();
  stop = startTextRotation(tick);
  expect(vi.getTimerCount()).toBe(0);
  motion.matches = false;
  motion.dispatchEvent(new Event("change"));
  vi.advanceTimersByTime(15_000);
  expect(tick).toHaveBeenCalledTimes(1);
  motion.matches = true;
  motion.dispatchEvent(new Event("change"));
  vi.advanceTimersByTime(60_000);
  expect(tick).toHaveBeenCalledTimes(1);
  motion.matches = false;
  motion.dispatchEvent(new Event("change"));
  vi.advanceTimersByTime(14_999);
  expect(tick).toHaveBeenCalledTimes(1);
  vi.advanceTimersByTime(1);
  expect(tick).toHaveBeenCalledTimes(2);
});

it("pauses offscreen surfaces and releases all listeners at retirement", () => {
  const tick = vi.fn();
  const activity = vi.fn();
  const show = (visible: boolean) => intersect([
    { isIntersecting: visible, intersectionRatio: visible ? 1 : 0 } as IntersectionObserverEntry,
  ], {} as IntersectionObserver);
  stop = startTextRotation(tick, {} as Element, activity);
  expect(vi.getTimerCount()).toBe(0);
  expect(activity).toHaveBeenLastCalledWith(false);
  intersect([
    { isIntersecting: false, intersectionRatio: 0 } as IntersectionObserverEntry,
    { isIntersecting: true, intersectionRatio: 1 } as IntersectionObserverEntry,
  ], {} as IntersectionObserver); // One notification can contain several queued states.
  expect(activity).toHaveBeenLastCalledWith(true);
  page.visibilityState = "hidden";
  page.dispatchEvent(new Event("visibilitychange"));
  expect(activity).toHaveBeenLastCalledWith(false);
  page.visibilityState = "visible";
  page.dispatchEvent(new Event("visibilitychange"));
  expect(activity).toHaveBeenLastCalledWith(true);
  motion.matches = true;
  motion.dispatchEvent(new Event("change"));
  expect(activity).toHaveBeenLastCalledWith(false);
  motion.matches = false;
  motion.dispatchEvent(new Event("change"));
  expect(activity).toHaveBeenLastCalledWith(true);
  vi.advanceTimersByTime(7_000);
  show(true); // A repeated observation must not reset the running clock.
  vi.advanceTimersByTime(8_000);
  expect(tick).toHaveBeenCalledTimes(1);
  show(false);
  expect(activity).toHaveBeenLastCalledWith(false);
  vi.advanceTimersByTime(60_000);
  expect(tick).toHaveBeenCalledTimes(1);
  show(true);
  vi.advanceTimersByTime(14_999);
  expect(tick).toHaveBeenCalledTimes(1);
  vi.advanceTimersByTime(1);
  expect(tick).toHaveBeenCalledTimes(2);
  show(false);
  stop();
  stop = undefined;
  show(true); // A notification queued before disconnect must not revive the timer.
  page.dispatchEvent(new Event("visibilitychange"));
  motion.dispatchEvent(new Event("change"));
  expect(vi.getTimerCount()).toBe(0);
  expect(activity).toHaveBeenLastCalledWith(false);
  expect(disconnect).toHaveBeenCalled();
});

it("observes a static welcome's visibility without running a rotation timer", () => {
  const activity = vi.fn();
  stop = startTextRotation(undefined, {} as Element, activity);
  intersect([{ isIntersecting: true, intersectionRatio: 1 } as IntersectionObserverEntry], {} as IntersectionObserver);
  expect(activity).toHaveBeenLastCalledWith(true);
  expect(vi.getTimerCount()).toBe(0);
  page.visibilityState = "hidden";
  page.dispatchEvent(new Event("visibilitychange"));
  expect(activity).toHaveBeenLastCalledWith(false);
});