Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

3.9 KB
Raw Blame
import fs from "fs";
import path from "path";
import { runInNewContext } from "node:vm";
import { describe, expect, it } from "vitest";

// style.js is injected into every app, including sites users package
// themselves, so site tweaks must only load on their own hosts.
const source = fs.readFileSync(
  path.join(process.cwd(), "src-tauri/src/inject/style.js"),
  "utf-8",
);

function injectedStyles(
  hostname,
  { mac = true, immersive = true, pakeConfig = {} } = {},
) {
  const styles = {};
  let onReady;
  runInNewContext(source, {
    navigator: { userAgent: mac ? "Macintosh" : "Windows NT 10.0" },
    hasImmersiveHeader: () => immersive,
    window: {
      location: { hostname },
      pakeConfig,
      __PAKE_INJECT_STYLE__: (css, id) => {
        styles[id] = css;
      },
      addEventListener: (type, handler) => {
        if (type === "DOMContentLoaded") onReady = handler;
      },
    },
  });
  onReady();
  return {
    content: styles["pake-content-style"] || "",
    immersive: styles["pake-top-padding-style"] || "",
  };
}

describe("site styles", () => {
  it("gives an unrelated site only the global rules", () => {
    const { content, immersive } = injectedStyles("example.com");

    expect(content).toContain("html::-webkit-scrollbar");
    expect(content).not.toMatch(/#react-root|#__next|\.copyright/);
    expect(immersive).toContain("#pake-top-dom");
    expect(immersive).not.toMatch(/padding-top|margin-left/);
  });

  it("loads a site's rules on its host and subdomains", () => {
    expect(injectedStyles("music.youtube.com").immersive).toContain(
      "ytmusic-nav-bar",
    );
    expect(injectedStyles("www.notion.so").immersive).toContain(
      ".notion-sidebar",
    );
    expect(injectedStyles("grok.com").immersive).toContain(
      '#grok-app-root [data-sidebar="sidebar"]',
    );
    expect(injectedStyles("grok.com").immersive).not.toContain(
      ".flex.w-full.h-full.overflow-hidden",
    );
  });

  it("omits removed brittle site content hacks and hashed selectors", () => {
    expect(injectedStyles("youtube.com").content).not.toContain("#masthead-ad");
    expect(injectedStyles("x.com").content).not.toContain("#react-root");
    expect(injectedStyles("xiaohongshu.com").content).not.toContain(
      ".avatar.logged-in",
    );
    expect(injectedStyles("xiaohongshu.com").immersive).toContain(
      "#header-area .logoIcon .user-info",
    );
    expect(injectedStyles("chat.deepseek.com").immersive).not.toContain(
      "._5a20a69",
    );
    expect(injectedStyles("v.flomoapp.com").content).not.toContain(
      "#footer-wrapper",
    );
    expect(injectedStyles("qwerty.kaiyi.cool").content).not.toContain(
      "footer.justify-center",
    );
    expect(injectedStyles("lizhi.dengdengju.com").content).not.toContain(
      "a:nth-child(3)",
    );
  });

  it("does not match lookalike hosts", () => {
    expect(injectedStyles("notyoutube.com").immersive).not.toContain(
      "ytd-masthead",
    );
    expect(injectedStyles("youtube.com.example.org").immersive).not.toContain(
      "ytd-masthead",
    );
    expect(injectedStyles("notnotion.so").immersive).not.toContain(
      ".notion-sidebar",
    );
  });

  it("keeps site immersive spacing to the macOS hidden title bar", () => {
    expect(injectedStyles("youtube.com").immersive).toContain("ytd-masthead");
    expect(
      injectedStyles("youtube.com", { mac: false }).immersive,
    ).not.toContain("ytd-masthead");
    expect(injectedStyles("youtube.com", { immersive: false }).immersive).toBe(
      "",
    );
  });

  it("keeps global and window immersive rules when site_styles is false", () => {
    const off = injectedStyles("youtube.com", {
      pakeConfig: { site_styles: false },
    });
    expect(off.content).toContain("html::-webkit-scrollbar");
    // The window's own rules stay: the drag strip and the traffic-light room.
    expect(off.immersive).toContain("#pake-top-dom");
    expect(off.immersive).toContain("ytd-masthead");
  });
});