Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
test(e2e): run on Linux, and pin every XDG base directory
James Dam92b8a140f64f
diff --git a/.github/workflows/e2e-check.yml b/.github/workflows/e2e-check.ymlindex cc60c410d..f3dffdb4e 100644--- a/.github/workflows/e2e-check.yml+++ b/.github/workflows/e2e-check.yml@@ -1,7 +1,9 @@# e2e/ is a standalone yarn project, so no other workflow's path filter or# install covers it -- an e2e-only PR would otherwise trigger nothing at all.# This validates the harness type-checks and its lockfile is current. It does-# NOT run the suite: that needs a macOS runner and a full app build.+# NOT run the suite: that needs a full Tauri app build (~20 min cold). The suite+# does run headless on Linux under xvfb-run, so promoting this to a real run on+# ubuntu-latest is a build-cost decision, not a technical blocker.name: e2e harness checkon:diff --git a/e2e/README.md b/e2e/README.mdindex 67f55ffcf..6903fec3b 100644--- a/e2e/README.md+++ b/e2e/README.md@@ -3,7 +3,30 @@WebDriver-driven end-to-end tests against a real Jan desktop build, using[WebdriverIO](https://webdriver.io) and `@wdio/tauri-service`.-**macOS only.** The config refuses to run elsewhere — see [Isolation](#isolation).+**macOS and Linux.** Windows cannot be isolated and the config refuses to run+there — see [Isolation](#isolation).++On Linux you need the Tauri build dependencies plus a WebDriver and a display.+`libssl-dev` is required but absent from the repo's other dependency lists —+`openssl-sys` is in the app's default dependency graph, and GitHub runners ship+it preinstalled, so a clean machine is the only place you notice:++```bash+sudo apt-get install -y build-essential cmake pkg-config libssl-dev \+ libglib2.0-dev libatk1.0-dev libpango1.0-dev libgtk-3-dev libsoup-3.0-dev \+ libwebkit2gtk-4.1-dev librsvg2-dev webkit2gtk-driver xvfb+```++Then run headless under Xvfb. Only the suite needs the display, so build first+and wrap just the run:++```bash+yarn build:e2e:app # from the repo root+cd e2e && xvfb-run -a yarn test+```++`xvfb-run -a yarn e2e` also works; it just puts the ~20-minute build inside the+virtual display for no reason.## Running@@ -38,10 +61,13 @@ browsers it ships. WebDriver is the protocol the webviews themselves implement,which is why this is the approach Tauri documents.On macOS there is no WKWebView driver to attach from outside, so the app hosts-an **embedded** WebDriver server itself via `tauri-plugin-wdio-webdriver`. That-plugin is an optional dependency behind the `e2e` cargo feature, and the `e2e`-feature is not enabled by any supported build path — not `default`, not-`desktop`, and not by any command in the `Makefile` or `.github/workflows/`.+an **embedded** WebDriver server itself via `tauri-plugin-wdio-webdriver`. Linux+uses the same embedded provider — it could drive `WebKitWebDriver` through the+`official` provider instead, but sharing one path means neither platform needs+`tauri-driver` installed. That plugin is an optional dependency behind the `e2e`+cargo feature, and the `e2e` feature is not enabled by any supported build path+— not `default`, not `desktop`, and not by any command in the `Makefile` or+`.github/workflows/`.Release binaries therefore do not contain a WebDriver listener. (This is a gateagainst accidental inclusion, not a hard impossibility: anyone can pass`--features e2e` to Cargo explicitly, which is exactly what `build:e2e:app`@@ -54,25 +80,43 @@ cargo tree --manifest-path src-tauri/Cargo.toml -i tauri-plugin-wdio-webdriver# "did not match any packages" = correctly absent from a default build```-The `e2e` feature also disables `tauri-plugin-single-instance`. It keys off a-`TMPDIR` socket on macOS, which the `HOME` override does not isolate, so a-developer with the real Jan open would otherwise see the test binary hand over-its argv and exit before the WebDriver server ever bound.+The `e2e` feature also disables `tauri-plugin-single-instance`. Its rendezvous+is a `TMPDIR` socket on macOS, a session-bus **D-Bus name** on Linux, and a named+mutex on Windows — none of which live under `HOME` or any XDG directory, so+nothing in `isolationEnv` isolates them. A developer with the real Jan open would+otherwise see the test binary hand over its argv and exit before the WebDriver+server ever bound.## Isolation-Each run gets a throwaway `HOME` (a temp dir), because the desktop app resolves-its data folder from `app_handle.path().data_dir()`, which on macOS is-`$HOME/Library/Application Support`. Tests never touch a real Jan profile.--This is why the suite is macOS-only. `dirs::data_dir()` resolves differently-elsewhere, and neither variant is redirected by `HOME`:--- **Windows** reads `FOLDERID_RoamingAppData` through the Win32 known-folder- API. It ignores `USERPROFILE`, and there is no environment override — so a- run would write to the developer's real profile.-- **Linux** prefers `$XDG_DATA_HOME` over `$HOME/.local/share`. Supporting it- means setting that variable too, not just `HOME`.+Each run gets a throwaway profile in a temp dir, so tests never touch real Jan+data. The desktop app resolves its data folder from+`app_handle.path().data_dir()`; which variables that leaves you needing to+override depends on the platform.++`dirs::data_dir()` resolves differently per platform, which is what decides+where the suite can run at all:++| Platform | `data_dir()` | Redirected by |+| --- | --- | --- |+| macOS | `$HOME/Library/Application Support` | `HOME` |+| Linux | `$XDG_DATA_HOME`, else `$HOME/.local/share` | `HOME` **and** `XDG_DATA_HOME` |+| Windows | `FOLDERID_RoamingAppData` (Win32 known-folder API) | nothing — no env override exists |++On Linux the harness pins **every** XDG base directory+(`XDG_DATA_HOME`, `XDG_CONFIG_HOME`, `XDG_STATE_HOME`, `XDG_CACHE_HOME`) rather+than relying on them being unset. Each outranks its `$HOME`-relative default, so+a single inherited value defeats the `HOME` override.++`XDG_CONFIG_HOME` is the dangerous one and the reason this is belt-and-braces+rather than minimal. `dirs::config_dir()` feeds the legacy-config migration in+`core/app/commands.rs`, which does `fs::copy` followed by `fs::remove_file`. With+that variable inherited, a run **deletes** the developer's real+`~/.config/Jan/settings.json` — confirmed against a decoy, which did not survive+a single run.++Windows has no equivalent lever, so the config refuses to run there rather than+write into a real Jan profile.Two things that look like they'd work but don't:diff --git a/e2e/isolation.ts b/e2e/isolation.tsnew file mode 100644index 000000000..055b40b11--- /dev/null+++ b/e2e/isolation.ts@@ -0,0 +1,36 @@+import { join } from 'node:path'++/**+ * Environment that confines the app's on-disk state to `home`.+ *+ * Its own module, with no side effects, so specs can assert on it without+ * re-evaluating wdio.conf.ts (which creates the temp profile at import time).+ *+ * On Linux every XDG base directory is pinned, not just the ones Jan is known to+ * read today. Each outranks its `$HOME`-relative default, so any single one+ * inherited from the outer environment silently defeats the HOME override -- and+ * "the ones that matter" is a list that rots as the app changes.+ *+ * `XDG_CONFIG_HOME` is why this is belt-and-braces rather than minimal, and the+ * stakes are destructive rather than untidy. `dirs::config_dir()` feeds the+ * legacy-config migration in core/app/commands.rs, which runs on every fresh+ * profile and does `fs::copy` followed by `fs::remove_file`. With that variable+ * inherited, a run DELETES the developer's real ~/.config/Jan/settings.json.+ * Confirmed against a decoy: the file did not survive one run.+ *+ * Deliberately absent:+ * - XDG_DATA_DIRS / XDG_CONFIG_DIRS: read-only system paths; overriding them+ * breaks GTK schema and theme lookup.+ * - XDG_RUNTIME_DIR: the app needs the real one for D-Bus. It cannot be+ * isolated, which is an accepted gap rather than an oversight.+ */+export function isolationEnv(home: string): Record<string, string> {+ if (process.platform !== 'linux') return { HOME: home }+ return {+ HOME: home,+ XDG_DATA_HOME: join(home, '.local', 'share'),+ XDG_CONFIG_HOME: join(home, '.config'),+ XDG_STATE_HOME: join(home, '.local', 'state'),+ XDG_CACHE_HOME: join(home, '.cache'),+ }+}diff --git a/e2e/specs/smoke.e2e.ts b/e2e/specs/smoke.e2e.tsindex 442c67c28..54d97760e 100644--- a/e2e/specs/smoke.e2e.ts+++ b/e2e/specs/smoke.e2e.ts@@ -1,5 +1,9 @@import { browser, expect, $ } from '@wdio/globals'+// Side-effect-free by design, so importing it here does not re-run the profile+// creation that wdio.conf.ts does at module scope.+import { isolationEnv } from '../isolation.js'+// The worker inherits this from the launcher; see wdio.conf.ts. Read from the// environment rather than importing the config, so the spec does not trigger a// second evaluation of a module with top-level side effects.@@ -73,3 +77,23 @@ describe('Jan desktop app', () => {)})})++// Not a UI test: a regression guard for a destructive bug. Before isolation.ts+// pinned XDG_CONFIG_HOME, a run deleted the developer's real+// ~/.config/Jan/settings.json through the fs::copy + fs::remove_file+// legacy-config migration in core/app/commands.rs -- and nothing about the run+// looked wrong while it happened. Dropping any one of these puts that back.+describe('isolation environment', () => {+ it('confines every XDG base directory to the throwaway profile', function () {+ if (process.platform !== 'linux') this.skip()+ const env = isolationEnv(testHome)+ for (const key of [+ 'XDG_DATA_HOME',+ 'XDG_CONFIG_HOME',+ 'XDG_STATE_HOME',+ 'XDG_CACHE_HOME',+ ]) {+ expect(env[key]).toEqual(expect.stringContaining(testHome))+ }+ })+})diff --git a/e2e/wdio.conf.ts b/e2e/wdio.conf.tsindex ac6fd9ec2..49f9a7517 100644--- a/e2e/wdio.conf.ts+++ b/e2e/wdio.conf.ts@@ -1,18 +1,24 @@-import { mkdtempSync, realpathSync, rmSync } from 'node:fs'+import { existsSync, mkdtempSync, realpathSync, rmSync } from 'node:fs'import { tmpdir } from 'node:os'import { join, resolve } from 'node:path'import type { TauriCapabilities, TauriServiceOptions } from '@wdio/tauri-service'+import { isolationEnv } from './isolation.js'-// Isolation is only sound on macOS, and silently pretending otherwise is worse-// than not running: the app resolves its data folder through `dirs::data_dir()`,-// which on Windows reads FOLDERID_RoamingAppData via the Win32 known-folder API-// (ignoring USERPROFILE entirely), and on Linux prefers $XDG_DATA_HOME over-// $HOME. Either would let the suite write to a real Jan profile.-if (process.platform !== 'darwin') {+// The app resolves its data folder through `dirs::data_dir()`, so isolation is+// only sound where that call can be redirected by environment:+//+// macOS $HOME/Library/Application Support -> HOME+// Linux $XDG_DATA_HOME, else $HOME/.local/share -> both (see isolationEnv)+// Windows FOLDERID_RoamingAppData via the Win32+// known-folder API -> no env override exists+//+// Windows therefore cannot be isolated at all, and a run there would use a real+// Jan profile. Failing loudly beats pretending.+if (process.platform !== 'darwin' && process.platform !== 'linux') {throw new Error(- `e2e isolation is only verified on macOS; refusing to run on ${process.platform}. ` +- 'Supporting another platform means redirecting that platform\'s data dir ' +- '(XDG_DATA_HOME on Linux; the known-folder API on Windows has no env override).'+ `e2e isolation is not possible on ${process.platform}; refusing to run. ` ++ 'On Windows `dirs::data_dir()` reads FOLDERID_RoamingAppData through the ' ++ 'known-folder API, which has no environment override.')}@@ -22,10 +28,18 @@ const repoRoot = resolve(import.meta.dirname, '..')// feature is a test-only opt-in and never goes near a release artifact.const appBinary = join(repoRoot, 'src-tauri/target/debug/Jan-Desktop')-// A throwaway HOME per run. The desktop app resolves its data folder from-// `app_handle.path().data_dir()` (core/app/commands.rs), which on macOS is-// $HOME/Library/Application Support -- so redirecting HOME is what keeps the-// suite away from the developer's real Jan profile.+// Fail here rather than 2 minutes later inside a driver-connection retry loop,+// which is what a missing binary otherwise looks like.+if (!existsSync(appBinary)) {+ throw new Error(+ `no app binary at ${appBinary}. Run \`yarn build:e2e:app\` from the repo root ` ++ '(or `yarn e2e`, which builds and then runs this suite).'+ )+}++// A throwaway profile per run, which is what keeps the suite away from the+// developer's real Jan data. See isolationEnv() for the variables that confine+// the app to it -- HOME alone is not sufficient on Linux.//// Deliberately NOT using JAN_DATA_FOLDER: only resolve_jan_data_folder() reads// it, and that is the CLI path. The desktop build ignores it.@@ -66,13 +80,19 @@ if (ownsTestHome) {if (!testHome.startsWith(realpathSync(tmpdir()))) returnrmSync(testHome, { recursive: true, force: true })})+ // No SIGINT/SIGTERM handler on purpose. wdio installs its own, and a second+ // listener would delete the tree while that one is still tearing the app+ // down -- the race the comment above exists to avoid. Ctrl-C therefore leaks+ // one directory under the OS temp root, which is the cheaper failure.}const tauriServiceOptions: TauriServiceOptions = {- // Embedded WebDriver server (tauri-plugin-wdio-webdriver), the only provider- // that works on macOS -- there is no WKWebView driver to drive from outside.+ // Embedded WebDriver server (tauri-plugin-wdio-webdriver). Required on macOS,+ // where there is no WKWebView driver to attach from outside; used on Linux too+ // so both platforms exercise one path and neither needs `tauri-driver`+ // installed (Linux could otherwise drive WebKitWebDriver via 'official').driverProvider: 'embedded',- env: { HOME: testHome },+ env: isolationEnv(testHome),}export const config: WebdriverIO.Config = {diff --git a/package.json b/package.jsonindex 14fd22b38..084d371b9 100644--- a/package.json+++ b/package.json@@ -50,7 +50,8 @@"build:extensions": "yarn workspace @janhq/core build && yarn workspaces foreach -Apt --include '@janhq/*-extension' run build","prepare": "husky","e2e": "yarn build:e2e:app && cd e2e && yarn install --immutable && yarn test",- "build:e2e:app": "yarn build:icon && ./scripts/stub-tauri-resources.sh && cross-env IS_TAURI=true yarn build:web && cargo build --locked --manifest-path src-tauri/Cargo.toml --features e2e --bin Jan-Desktop"+ "build:e2e:app": "yarn build:icon && ./scripts/stub-tauri-resources.sh && yarn build:e2e:deps && cross-env IS_TAURI=true yarn build:web && cargo build --locked --manifest-path src-tauri/Cargo.toml --features e2e --bin Jan-Desktop",+ "build:e2e:deps": "yarn install && yarn build:tauri:plugin:api && yarn build:core && yarn build:extensions"},"devDependencies": {"@tauri-apps/cli": "^2.7.0",diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex 28c5b5053..ceed84d7e 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -43,8 +43,9 @@ default = [hardware = ["dep:tauri-plugin-hardware"]deep-link = ["dep:tauri-plugin-deep-link"]# WebDriver e2e only. Runs an embedded WebDriver server inside the app so-# @wdio/tauri-service can drive it on macOS, where no WKWebView driver exists.-# Opt-in so release builds never ship a remote-control listener.+# @wdio/tauri-service can drive it. Required on macOS, where no WKWebView driver+# exists to attach from outside; used on Linux too so both platforms exercise+# one path. Opt-in so release builds never ship a remote-control listener.e2e = ["dep:tauri-plugin-wdio-webdriver"]# MLX (tauri-plugin-mlx) is Apple-Silicon-only; it's a macOS-target dependency# (see [target.'cfg(target_os = "macos")'.dependencies]) rather than a feature.