Files
s1mba 731cc340d8 [packages/cli] Add comprehensive CLI test suite (#128)
* [packages/cli]test: add comprehensive CLI test suite with E2E browser tests

- Add unit tests for wrapper (actionbook.js), postinstall, config, profile,
  search, get, and sources commands
- Add Tier 1 browser tests (arg validation, help output, defaults)
- Add Tier 2 browser E2E tests (navigation, interaction, screenshots,
  cookies, tabs, error handling, JSON output)
- Fix E2E test reliability: add --wait for element readiness after navigation,
  use eval-based form submit for login flow, match actual CLI exit codes
- Suppress macOS Keychain dialogs in tests via --use-mock-keychain config
- Set testTimeout to 60s for browser E2E operations
- Export modules from actionbook.js and postinstall.js for testability

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* [packages/cli]fix: resolve Windows .exe suffix when locating test CLI binary

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* [packages/cli]fix: skip shell-script mock tests on Windows

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 14:36:15 +08:00

52 lines
1.6 KiB
TypeScript

import { mkdtempSync, mkdirSync, writeFileSync } from "fs";
import path from "path";
import os from "os";
/**
* Create an isolated environment with temp directories for HOME, XDG_CONFIG_HOME, XDG_DATA_HOME.
* This prevents tests from interfering with the user's real config.
* Matches the Rust cli_test.rs setup_config() pattern.
*
* On macOS, pre-seeds the config with --use-mock-keychain in the default
* profile's extra_args to suppress Keychain permission dialogs during tests.
*/
export function createIsolatedEnv(): {
tmpDir: string;
env: Record<string, string>;
} {
const tmpDir = mkdtempSync(path.join(os.tmpdir(), "actionbook-test-"));
const home = path.join(tmpDir, "home");
const configHome = path.join(tmpDir, "config");
const dataHome = path.join(tmpDir, "data");
mkdirSync(home, { recursive: true });
mkdirSync(configHome, { recursive: true });
mkdirSync(dataHome, { recursive: true });
// On macOS, Chrome accesses the Keychain for cookie encryption, which
// triggers permission dialogs in test environments. Pre-seed the config
// with --use-mock-keychain to suppress these prompts.
if (process.platform === "darwin") {
const configDir = path.join(home, "Library", "Application Support", "actionbook");
mkdirSync(configDir, { recursive: true });
writeFileSync(
path.join(configDir, "config.toml"),
[
"[profiles.actionbook]",
"cdp_port = 9222",
'extra_args = ["--use-mock-keychain"]',
"",
].join("\n")
);
}
return {
tmpDir,
env: {
HOME: home,
XDG_CONFIG_HOME: configHome,
XDG_DATA_HOME: dataHome,
},
};
}