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. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

1.3 KB
Raw Blame
import { afterEach, expect, it, vi } from "vitest";

afterEach(() => {
  vi.unstubAllGlobals();
  vi.useRealTimers();
  vi.resetModules();
});

it.each(["available", "http-error", "unreachable", "timeout"])(
  "keeps the optional website probe shared and bounded: %s", async outcome => {
    vi.useFakeTimers();
    const fetchImpl = vi.fn(async (_url: string, options: RequestInit) => {
      if (outcome === "unreachable") throw new TypeError("Failed to fetch");
      if (outcome === "timeout") return new Promise<Response>((_resolve, reject) => {
        options.signal!.addEventListener("abort", () => reject(options.signal!.reason), { once: true });
      });
      return new Response(null, { status: outcome === "available" ? 200 : 503 });
    });
    vi.stubGlobal("fetch", fetchImpl);
    const { projectWebsiteAvailable } = await import("../src/client/lib/use-project-link");
    const first = projectWebsiteAvailable();
    expect(projectWebsiteAvailable()).toBe(first);
    if (outcome === "timeout") await vi.advanceTimersByTimeAsync(3_000);
    expect(await first).toBe(outcome === "available");
    expect(fetchImpl).toHaveBeenCalledOnce();
    expect(fetchImpl.mock.calls[0]![1]).toMatchObject({
      method: "HEAD", credentials: "omit", referrerPolicy: "no-referrer",
    });
    expect(vi.getTimerCount()).toBe(0);
  },
);