Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
import fs from "node:fs";
import path from "node:path";
import vm from "node:vm";
import { afterEach, describe, expect, it, vi } from "vitest";
const source = fs.readFileSync(
path.join(process.cwd(), "src-tauri/src/inject/theme_refresh.js"),
"utf-8",
);
function makeElement({
classes = [],
dataTheme = null,
colorScheme = "",
} = {}) {
const classSet = new Set(classes);
let themeAttr = dataTheme;
return {
classList: {
contains: (name) => classSet.has(name),
add: (name) => classSet.add(name),
remove: (name) => classSet.delete(name),
},
getAttribute: (name) => (name === "data-theme" ? themeAttr : null),
setAttribute: (name, value) => {
if (name === "data-theme") themeAttr = value;
},
removeAttribute: (name) => {
if (name === "data-theme") themeAttr = null;
},
style: { colorScheme },
};
}
function loadThemeRefresh({
darkMode = false,
systemDark = false,
docOptions = {},
bodyOptions = {},
noBody = false,
rejectInvoke = false,
} = {}) {
const invokeCalls = [];
const observed = [];
let observerCallback = null;
let domReady = null;
let mediaChange = null;
const media = {
matches: systemDark,
addEventListener: (event, fn) => {
if (event === "change") mediaChange = fn;
},
};
const documentElement = makeElement(docOptions);
const body = noBody ? null : makeElement(bodyOptions);
class MutationObserver {
constructor(cb) {
observerCallback = cb;
}
observe(target, config) {
if (!target) {
throw new TypeError("Failed to execute 'observe': target is null");
}
observed.push({ target, config });
}
}
const window = {
pakeConfig: { dark_mode: darkMode },
matchMedia: () => media,
__PAKE_NATIVE__: {
core: {
invoke: (command, payload) => {
invokeCalls.push([command, payload]);
return rejectInvoke
? Promise.reject(new Error("Command update_theme_mode not allowed"))
: Promise.resolve();
},
},
},
};
window.top = window;
const document = {
documentElement,
body,
addEventListener: (event, fn) => {
if (event === "DOMContentLoaded") domReady = fn;
},
};
vm.runInNewContext(source, {
window,
document,
MutationObserver,
Promise,
setTimeout,
clearTimeout,
});
return {
window,
document,
documentElement,
body,
media,
invokeCalls,
observed,
triggerDomReady: () => domReady?.(),
triggerMutation: () => observerCallback?.(),
triggerMediaChange: (matches) => {
media.matches = matches;
mediaChange?.();
},
};
}
describe("theme_refresh", () => {
afterEach(() => {
vi.useRealTimers();
});
it("never overrides an explicit dark_mode: true config", () => {
vi.useFakeTimers();
const env = loadThemeRefresh({
darkMode: true,
docOptions: { classes: ["light"] },
});
env.triggerDomReady();
vi.advanceTimersByTime(1000);
expect(env.invokeCalls).toEqual([]);
expect(env.observed).toEqual([]);
});
it("does not pin the window when page theme matches the OS appearance", () => {
vi.useFakeTimers();
const env = loadThemeRefresh({
systemDark: false,
docOptions: { classes: ["light"] },
});
env.triggerDomReady();
vi.advanceTimersByTime(600);
expect(env.invokeCalls).toEqual([]);
// OS switches to dark and the page updates to match: still no pinning.
env.documentElement.classList.remove("light");
env.documentElement.classList.add("dark");
env.triggerMediaChange(true);
expect(env.invokeCalls).toEqual([]);
});
it("applies an explicit page override and restores system mode when cleared", () => {
vi.useFakeTimers();
const env = loadThemeRefresh({
systemDark: false,
docOptions: { classes: ["dark"] },
});
env.triggerDomReady();
vi.advanceTimersByTime(600);
expect(env.invokeCalls).toEqual([["update_theme_mode", { mode: "dark" }]]);
// Unchanged mutations do not re-invoke.
env.triggerMutation();
vi.advanceTimersByTime(300);
expect(env.invokeCalls).toHaveLength(1);
// Removing the explicit theme returns the window to OS tracking.
env.documentElement.classList.remove("dark");
env.triggerMutation();
vi.advanceTimersByTime(300);
expect(env.invokeCalls).toEqual([
["update_theme_mode", { mode: "dark" }],
["update_theme_mode", { mode: "system" }],
]);
});
it("null-guards document.body before observing and catches rejected invokes", async () => {
vi.useFakeTimers();
const env = loadThemeRefresh({
systemDark: false,
noBody: true,
docOptions: { dataTheme: "dark" },
rejectInvoke: true,
});
expect(() => env.triggerDomReady()).not.toThrow();
expect(env.observed).toHaveLength(1);
expect(env.observed[0].target).toBe(env.documentElement);
vi.advanceTimersByTime(600);
await Promise.resolve();
expect(env.invokeCalls).toEqual([["update_theme_mode", { mode: "dark" }]]);
});
});