Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

test(e2e): pin TMPDIR, and assert isolation from disk

James Dam3def9733d151
diff --git a/e2e/README.md b/e2e/README.md
index 94077349b..c2b646ea4 100644
--- a/e2e/README.md
+++ b/e2e/README.md
@@ -6,17 +6,22 @@ WebDriver-driven end-to-end tests against a real Jan desktop build, using
**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:
+On Linux you need the Tauri build dependencies plus a display. `libssl-dev` is
+easy to miss: `openssl-sys` is in the app's default dependency graph, but GitHub
+runners ship OpenSSL preinstalled and the CI workflows never install it, so a
+clean machine is the only place the gap shows. (`.devcontainer/postCreateCommand.sh`
+does list it.)
```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
+ libwebkit2gtk-4.1-dev librsvg2-dev xvfb
```
+No `webkit2gtk-driver`: the embedded provider drives the webview in-process and
+never launches `WebKitWebDriver`. You would only need it to switch to the
+`official` provider.
+
Then run headless under Xvfb. Only the suite needs the display, so build first
and wrap just the run:
@@ -50,6 +55,11 @@ not just installed -- `web-app` imports `@janhq/core`, which resolves to
`core/dist`. Without it `build:web` fails with
`TS2307: Cannot find module '@janhq/core'`.
+`build:e2e:deps` is deliberately the same four steps as `Makefile:44-47`,
+including its redundant second build of `core` (`build:extensions` starts by
+building `core` again). Staying in step with the Makefile is worth more than the
+minute that would save.
+
To iterate on specs without rebuilding the app:
```bash
@@ -94,9 +104,11 @@ cargo tree --manifest-path src-tauri/Cargo.toml -i tauri-plugin-wdio-webdriver
```
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
+is a hardcoded `/tmp/{identifier}_si.sock` on macOS (`platform_impl/macos.rs` —
+literally `/tmp`, not `$TMPDIR`, because the path must stay under 100 chars), a
+session-bus **D-Bus name** on Linux, and a named mutex on Windows. None live
+under `HOME` or any XDG directory, and the macOS socket ignores the pinned
+`TMPDIR` too, 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.
diff --git a/e2e/isolation.ts b/e2e/isolation.ts
index 055b40b11..a651f5121 100644
--- a/e2e/isolation.ts
+++ b/e2e/isolation.ts
@@ -18,6 +18,14 @@ import { join } from 'node:path'
* inherited, a run DELETES the developer's real ~/.config/Jan/settings.json.
* Confirmed against a decoy: the file did not survive one run.
*
+ * TMPDIR is pinned on both platforms, not just Linux. `std::env::temp_dir()`
+ * reads it on Unix, and tauri-plugin-agent-tools puts agent scratch at
+ * `temp_dir()/jan-agent-<session>` -- then sweep_stale_scratch_dirs()
+ * (workspace.rs) DELETES every `jan-agent-*` older than 24h it finds there. The
+ * current smoke suite never reaches that sweep (it is gated on an existing
+ * thread, and a fresh profile has none), so this is pre-emptive rather than a
+ * live bug. It stops being pre-emptive the moment a spec creates a thread.
+ *
* Deliberately absent:
* - XDG_DATA_DIRS / XDG_CONFIG_DIRS: read-only system paths; overriding them
* breaks GTK schema and theme lookup.
@@ -25,9 +33,12 @@ import { join } from 'node:path'
* 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 }
+ // Callers must ensure TMPDIR exists: temp_dir() does not create it.
+ const tmp = join(home, 'tmp')
+ if (process.platform !== 'linux') return { HOME: home, TMPDIR: tmp }
return {
HOME: home,
+ TMPDIR: tmp,
XDG_DATA_HOME: join(home, '.local', 'share'),
XDG_CONFIG_HOME: join(home, '.config'),
XDG_STATE_HOME: join(home, '.local', 'state'),
diff --git a/e2e/specs/smoke.e2e.ts b/e2e/specs/smoke.e2e.ts
index 54d97760e..fdd0af149 100644
--- a/e2e/specs/smoke.e2e.ts
+++ b/e2e/specs/smoke.e2e.ts
@@ -1,8 +1,7 @@
-import { browser, expect, $ } from '@wdio/globals'
+import { existsSync } from 'node:fs'
+import { join } from 'node:path'
-// 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'
+import { browser, expect, $ } from '@wdio/globals'
// 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
@@ -78,22 +77,29 @@ 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))
- }
+// The regression guard for the destructive bug. Asserting on isolationEnv()
+// directly would be tautological -- it returns join(home, ...), so checking the
+// result contains `home` proves nothing, and would still pass if wdio.conf.ts
+// stopped passing that env to the app at all.
+//
+// This asserts the opposite end: a file the Rust side wrote, at a path the Rust
+// side resolved through dirs::data_dir(). It only exists here if the overridden
+// environment actually reached the app process.
+describe('on-disk isolation', () => {
+ it('wrote its config inside the throwaway profile', async () => {
+ // app_data_dir_with_fallback() -> data_dir()/Jan, + CONFIGURATION_FILE_NAME
+ // (core/app/commands.rs, core/app/constants.rs).
+ const settings =
+ process.platform === 'darwin'
+ ? join(testHome, 'Library/Application Support/Jan/settings.json')
+ : join(testHome, '.local/share/Jan/settings.json')
+
+ await browser.waitUntil(() => existsSync(settings), {
+ timeout: 30_000,
+ timeoutMsg:
+ `the app never wrote ${settings}. Either it resolved data_dir() ` +
+ 'outside the throwaway profile -- which is the isolation failing -- or ' +
+ 'the config path changed in core/app/commands.rs.',
+ })
})
})
diff --git a/e2e/wdio.conf.ts b/e2e/wdio.conf.ts
index 49f9a7517..a7ad499b9 100644
--- a/e2e/wdio.conf.ts
+++ b/e2e/wdio.conf.ts
@@ -1,4 +1,4 @@
-import { existsSync, mkdtempSync, realpathSync, rmSync } from 'node:fs'
+import { existsSync, mkdirSync, 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'
@@ -58,8 +58,11 @@ if (!existsSync(appBinary)) {
// is used but never removed.
const inheritedHome = process.env.JAN_E2E_HOME
const ownsTestHome = !inheritedHome
-const testHome =
- inheritedHome ?? mkdtempSync(join(realpathSync(tmpdir()), 'jan-e2e-'))
+// resolve() because `dirs` silently ignores a relative XDG value and falls back
+// to its $HOME-relative default -- which, with a relative HOME, is also relative.
+const testHome = inheritedHome
+ ? resolve(inheritedHome)
+ : mkdtempSync(join(realpathSync(tmpdir()), 'jan-e2e-'))
process.env.JAN_E2E_HOME = testHome
export const testHomeForSpecs = testHome
@@ -86,6 +89,10 @@ if (ownsTestHome) {
// one directory under the OS temp root, which is the cheaper failure.
}
+// TMPDIR is pinned to a path inside the profile, and temp_dir() will not create
+// it. An app that cannot write scratch is a confusing failure; make it exist.
+mkdirSync(join(testHome, 'tmp'), { recursive: true })
+
const tauriServiceOptions: TauriServiceOptions = {
// Embedded WebDriver server (tauri-plugin-wdio-webdriver). Required on macOS,
// where there is no WKWebView driver to attach from outside; used on Linux too
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index fe189bff3..d0c034650 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -262,9 +262,9 @@ pub fn run() {
// thing that touched `builder`, and a `mut` binding would then be unused --
// which CI's `clippy -D warnings` treats as an error.
//
- // Not in e2e builds: single-instance keys off a TMPDIR socket on macOS (a
- // D-Bus name on Linux, a named mutex on Windows), none of which the test
- // harness's HOME override isolates. With a real Jan already running, the
+ // Not in e2e builds: single-instance keys off a hardcoded /tmp socket on
+ // macOS (a D-Bus name on Linux, a named mutex on Windows), none of which the
+ // test harness's HOME/XDG/TMPDIR overrides isolate. With a real Jan already running, the
// test binary would hand over its argv and exit before the embedded
// WebDriver server ever bound.
#[cfg(all(desktop, not(feature = "e2e")))]