Skip to content

Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.

10.8 KB
Raw Blame
import { describe, it, expect } from "vitest";
import {
  getArchiveExt,
  getDefaultStealthArgs,
  getCacheDir,
  getBinaryDir,
  normalizeReleaseChannel,
  normalizeRequestedVersion,
} from "../src/config.js";
import { _buildArgsForTest, resolveTimezone } from "../src/playwright.js";

describe("config", () => {
  it("getDefaultStealthArgs returns expected flags", () => {
    const args = getDefaultStealthArgs();
    const isMac = process.platform === "darwin";

    expect(args).toContain("--no-sandbox");

    if (isMac) {
      expect(args).toContain("--fingerprint-platform=macos");
    } else {
      expect(args).toContain("--fingerprint-platform=windows");
    }

    // GPU flags removed — binary auto-generates from seed + platform
    expect(args.some((a) => a.includes("fingerprint-gpu-vendor"))).toBe(false);
    expect(args.some((a) => a.includes("fingerprint-gpu-renderer"))).toBe(false);

    // Should have a random fingerprint seed
    const fingerprintArg = args.find((a) => a.startsWith("--fingerprint="));
    expect(fingerprintArg).toBeDefined();
    const seed = Number(fingerprintArg!.split("=")[1]);
    expect(seed).toBeGreaterThanOrEqual(10000);
    expect(seed).toBeLessThanOrEqual(99999);
  });

  it("getDefaultStealthArgs generates different seeds", () => {
    const seeds = new Set<string>();
    for (let i = 0; i < 10; i++) {
      const args = getDefaultStealthArgs();
      const fp = args.find((a) => a.startsWith("--fingerprint="))!;
      seeds.add(fp);
    }
    // With 90k possible seeds, 10 calls should produce at least 2 unique
    expect(seeds.size).toBeGreaterThan(1);
  });

  it("getCacheDir returns ~/.cloakbrowser by default", () => {
    // tests/setup.ts points the cache dir at a temp dir for isolation; drop it
    // here to exercise the real default.
    const prev = process.env.CLOAKBROWSER_CACHE_DIR;
    delete process.env.CLOAKBROWSER_CACHE_DIR;
    try {
      expect(getCacheDir()).toContain(".cloakbrowser");
    } finally {
      if (prev === undefined) delete process.env.CLOAKBROWSER_CACHE_DIR;
      else process.env.CLOAKBROWSER_CACHE_DIR = prev;
    }
  });

  it("getBinaryDir keeps the -pro suffix so existing caches stay valid", () => {
    expect(getBinaryDir("155.0.8059.39.2")).toMatch(/chromium-155\.0\.8059\.39\.2-pro$/);
  });
});

describe("release channel", () => {
  it("defaults to stable", () => {
    delete process.env.CLOAKBROWSER_RELEASE_CHANNEL;
    expect(normalizeReleaseChannel()).toBe("stable");
  });

  it("normalizes preview case and whitespace", () => {
    expect(normalizeReleaseChannel(" PREVIEW ")).toBe("preview");
  });

  it("reads the environment at call time", () => {
    process.env.CLOAKBROWSER_RELEASE_CHANNEL = "preview";
    try {
      expect(normalizeReleaseChannel()).toBe("preview");
    } finally {
      delete process.env.CLOAKBROWSER_RELEASE_CHANNEL;
    }
  });

  it("explicit stable overrides a preview environment", () => {
    process.env.CLOAKBROWSER_RELEASE_CHANNEL = "preview";
    try {
      expect(normalizeReleaseChannel("stable")).toBe("stable");
      expect(normalizeReleaseChannel("unknown")).toBe("stable");
    } finally {
      delete process.env.CLOAKBROWSER_RELEASE_CHANNEL;
    }
  });
});

describe("version pin", () => {
  it("explicit value wins over env", () => {
    process.env.CLOAKBROWSER_VERSION = "146.0.0.0";
    try {
      expect(normalizeRequestedVersion("148.0.7778.215.2")).toBe("148.0.7778.215.2");
    } finally {
      delete process.env.CLOAKBROWSER_VERSION;
    }
  });

  it("reads CLOAKBROWSER_VERSION", () => {
    process.env.CLOAKBROWSER_VERSION = "148.0.7778.215.2";
    try {
      expect(normalizeRequestedVersion()).toBe("148.0.7778.215.2");
    } finally {
      delete process.env.CLOAKBROWSER_VERSION;
    }
  });

  it("rejects unsafe values", () => {
    expect(() => normalizeRequestedVersion("../../148.0.7778.215.2")).toThrow(
      "Invalid browser version pin"
    );
  });

  it("rejects non-ASCII digits (parity with Python/.NET)", () => {
    expect(() => normalizeRequestedVersion("١٤٦.0.7680.177")).toThrow(
      "Invalid browser version pin"
    );
  });
});

describe("archive helpers", () => {
  it("getArchiveExt returns correct extension for platform", () => {
    const ext = getArchiveExt();
    if (process.platform === "win32") {
      expect(ext).toBe(".zip");
    } else {
      expect(ext).toBe(".tar.gz");
    }
  });

});

describe("buildArgs timezone/locale", () => {
  it("injects --fingerprint-timezone when timezone is set", () => {
    const args = _buildArgsForTest({ timezone: "America/New_York" });
    expect(args).toContain("--fingerprint-timezone=America/New_York");
  });

  it("injects --lang and --fingerprint-locale when locale is set", () => {
    const args = _buildArgsForTest({ locale: "en-US" });
    expect(args).toContain("--lang=en-US");
    expect(args).toContain("--fingerprint-locale=en-US");
  });

  it("injects both when both are set", () => {
    const args = _buildArgsForTest({ timezone: "Europe/Berlin", locale: "de-DE" });
    expect(args).toContain("--fingerprint-timezone=Europe/Berlin");
    expect(args).toContain("--lang=de-DE");
    expect(args).toContain("--fingerprint-locale=de-DE");
  });

  it("injects timezone/locale even when stealthArgs=false", () => {
    const args = _buildArgsForTest({ stealthArgs: false, timezone: "America/New_York", locale: "en-US" });
    expect(args).toContain("--fingerprint-timezone=America/New_York");
    expect(args).toContain("--lang=en-US");
    expect(args).toContain("--fingerprint-locale=en-US");
    expect(args.some(a => a.startsWith("--fingerprint="))).toBe(false);
  });

  it("does not inject flags when not set", () => {
    const args = _buildArgsForTest({});
    expect(args.some(a => a.startsWith("--fingerprint-timezone="))).toBe(false);
    expect(args.some(a => a.startsWith("--lang="))).toBe(false);
    expect(args.some(a => a.startsWith("--fingerprint-locale="))).toBe(false);
  });
});

describe("buildArgs deduplication", () => {
  it("user --fingerprint overrides default seed", () => {
    const args = _buildArgsForTest({ args: ["--fingerprint=99887"] });
    const fpArgs = args.filter(a => a.startsWith("--fingerprint="));
    expect(fpArgs).toHaveLength(1);
    expect(fpArgs[0]).toBe("--fingerprint=99887");
  });

  it("user --fingerprint-platform overrides default", () => {
    const args = _buildArgsForTest({ args: ["--fingerprint-platform=linux"] });
    const platArgs = args.filter(a => a.startsWith("--fingerprint-platform="));
    expect(platArgs).toHaveLength(1);
    expect(platArgs[0]).toBe("--fingerprint-platform=linux");
  });

  it("timezone param overrides user --fingerprint-timezone arg", () => {
    const args = _buildArgsForTest({
      args: ["--fingerprint-timezone=Europe/London"],
      timezone: "America/New_York",
    });
    const tzArgs = args.filter(a => a.startsWith("--fingerprint-timezone="));
    expect(tzArgs).toHaveLength(1);
    expect(tzArgs[0]).toBe("--fingerprint-timezone=America/New_York");
  });

  it("locale param overrides user --lang and --fingerprint-locale args", () => {
    const args = _buildArgsForTest({
      args: ["--lang=de-DE", "--fingerprint-locale=de-DE"],
      locale: "en-US",
    });
    const langArgs = args.filter(a => a.startsWith("--lang="));
    expect(langArgs).toHaveLength(1);
    expect(langArgs[0]).toBe("--lang=en-US");
    const localeArgs = args.filter(a => a.startsWith("--fingerprint-locale="));
    expect(localeArgs).toHaveLength(1);
    expect(localeArgs[0]).toBe("--fingerprint-locale=en-US");
  });

  it("no duplicate flag keys in output", () => {
    const args = _buildArgsForTest({
      args: ["--fingerprint=99887", "--fingerprint-timezone=UTC", "--lang=fr-FR"],
      timezone: "Europe/Berlin",
      locale: "de-DE",
    });
    const keys = args.map(a => a.split("=")[0]);
    expect(new Set(keys).size).toBe(keys.length);
  });

  it("non-value flags preserved without dedup issues", () => {
    const args = _buildArgsForTest({ args: ["--disable-gpu", "--no-zygote"] });
    expect(args).toContain("--disable-gpu");
    expect(args).toContain("--no-zygote");
    expect(args).toContain("--no-sandbox");
  });
});

describe("buildArgs webrtc IP", () => {
  it("passes --fingerprint-webrtc-ip from args", () => {
    const args = _buildArgsForTest({ args: ["--fingerprint-webrtc-ip=1.2.3.4"] });
    expect(args).toContain("--fingerprint-webrtc-ip=1.2.3.4");
  });

  it("does not inject when not in args", () => {
    const args = _buildArgsForTest({});
    expect(args.some(a => a.startsWith("--fingerprint-webrtc-ip"))).toBe(false);
  });
});

describe("resolveTimezone alias", () => {
  it("resolves timezoneId to timezone", () => {
    const result = resolveTimezone({ timezoneId: "Europe/Paris" }) as unknown as { timezone: string };
    expect(result.timezone).toBe("Europe/Paris");
    expect(result).not.toHaveProperty("timezoneId");
  });

  it("preserves explicit timezone over timezoneId", () => {
    const result = resolveTimezone({ timezone: "UTC", timezoneId: "Europe/Paris" });
    expect(result.timezone).toBe("UTC");
    expect(result).not.toHaveProperty("timezoneId");
  });

  it("returns options unchanged when no timezoneId", () => {
    const opts = { timezone: "UTC" };
    const result = resolveTimezone(opts);
    expect(result).toBe(opts); // same reference, no copy
    expect(result.timezone).toBe("UTC");
  });

  it("returns options unchanged when neither is set", () => {
    const opts = {};
    const result = resolveTimezone(opts);
    expect(result).toBe(opts);
  });
});

describe("buildArgs --start-maximized", () => {
  it("adds --start-maximized by default (an unversioned override too)", () => {
    expect(_buildArgsForTest({})).toContain("--start-maximized");
    const prev = process.env.CLOAKBROWSER_BINARY_PATH;
    process.env.CLOAKBROWSER_BINARY_PATH = "/fake/chrome";
    try {
      expect(_buildArgsForTest({})).toContain("--start-maximized");
    } finally {
      if (prev === undefined) delete process.env.CLOAKBROWSER_BINARY_PATH;
      else process.env.CLOAKBROWSER_BINARY_PATH = prev;
    }
  });
  it("is suppressed by a user --window-size", () => {
    const args = _buildArgsForTest({
      args: ["--window-size=1000,800"],
    });
    expect(args).not.toContain("--start-maximized");
    expect(args).toContain("--window-size=1000,800");
  });
  it("is suppressed by a raw Puppeteer defaultViewport size, not by null", () => {
    const sized = _buildArgsForTest({ launchOptions: { defaultViewport: { width: 800, height: 600 } } });
    expect(sized).not.toContain("--start-maximized");
    const none = _buildArgsForTest({ launchOptions: { defaultViewport: null } });
    expect(none).toContain("--start-maximized");
  });
  it("does not double a user-supplied --start-maximized", () => {
    const args = _buildArgsForTest({
      args: ["--start-maximized"],
    });
    expect(args.filter(a => a === "--start-maximized")).toHaveLength(1);
  });
});