Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/astro-auto-adapter. Choose Astro Adapters based off of the
ASTRO_ADAPTER_MODE environment Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484// tests/cli.test.ts// Unit tests for the astro-auto-adapter CLI helper functions.// These tests exercise pure-logic functions that do not require a TTY or// interactive prompts, so they run safely in CI environments.
import { describe, test, expect, beforeEach, afterEach } from "vitest";import { writeFile, mkdir } from "node:fs/promises";import { join } from "node:path";import { tmpdir } from "node:os";import process from "node:process";import { pathToFileURL } from "node:url";
import { ADAPTERS, detectPackageManager, buildInstallCommand, buildRemoveCommand, getInstalledAdapters, getCliVersion, runCommand,} from "../src/cli.ts";
const ENV_KEYS = ["PACKAGE_MANAGER", "npm_config_user_agent"] as const;type RestoredEnv = Partial<Record<(typeof ENV_KEYS)[number], string | undefined>>;
// ============================================================// Shared test helpers// ============================================================
/** * Creates a unique temporary directory path for test isolation. * Each call generates a directory with a unique name based on * the current timestamp and a random suffix. */async function makeTmpDir(): Promise<string> { const dir = join( tmpdir(), `aaa-test-${Date.now()}-${Math.random().toString(36).slice(2)}` ); await mkdir(dir, { recursive: true }); return dir;}
// ============================================================// ADAPTERS constant// ============================================================
describe("ADAPTERS constant", () => { test("contains all built-in adapters", () => { const values = ADAPTERS.map((a) => a.value); expect(values).toContain("node"); expect(values).toContain("vercel"); expect(values).toContain("netlify"); expect(values).toContain("cloudflare"); expect(values).toContain("deno"); expect(values).toContain("sst"); });
test("every adapter has required fields", () => { for (const adapter of ADAPTERS) { expect(adapter.label).toBeTruthy(); expect(adapter.value).toBeTruthy(); expect(adapter.pkg).toBeTruthy(); expect(adapter.hint).toBeTruthy(); expect(Array.isArray(adapter.envVars)).toBe(true); } });
test("marks SST as static-only on Astro 6 for now", () => { const sst = ADAPTERS.find((adapter) => adapter.value === "sst"); expect(sst?.compatibilityNote).toContain("Astro 6"); expect(sst?.compatibilityNote).toContain("static"); });
test("adapter packages match known npm packages", () => { const pkgMap: Record<string, string> = { node: "@astrojs/node", vercel: "@astrojs/vercel", netlify: "@astrojs/netlify", cloudflare: "@astrojs/cloudflare", deno: "@deno/astro-adapter", sst: "astro-sst", }; for (const adapter of ADAPTERS) { expect(adapter.pkg).toBe(pkgMap[adapter.value]); } });});
// ============================================================// detectPackageManager// ============================================================
describe("detectPackageManager", () => { let originalEnv: RestoredEnv;
beforeEach(() => { originalEnv = { PACKAGE_MANAGER: process.env.PACKAGE_MANAGER, npm_config_user_agent: process.env.npm_config_user_agent, }; });
afterEach(() => { for (const key of ENV_KEYS) { const originalValue = originalEnv[key]; if (originalValue === undefined) { delete process.env[key]; } else { process.env[key] = originalValue; } } });
test("respects PACKAGE_MANAGER env override", () => { process.env.PACKAGE_MANAGER = "yarn"; delete process.env.npm_config_user_agent; expect(detectPackageManager()).toBe("yarn"); });
test("detects pnpm from user agent", () => { delete process.env.PACKAGE_MANAGER; process.env.npm_config_user_agent = "pnpm/8.0.0 npm/? node/v20.0.0 linux x64"; expect(detectPackageManager()).toBe("pnpm"); });
test("detects yarn from user agent", () => { delete process.env.PACKAGE_MANAGER; process.env.npm_config_user_agent = "yarn/1.22.0 npm/? node/v20.0.0"; expect(detectPackageManager()).toBe("yarn"); });
test("detects npm from user agent", () => { delete process.env.PACKAGE_MANAGER; process.env.npm_config_user_agent = "npm/10.0.0 node/v20.0.0 linux x64"; expect(detectPackageManager()).toBe("npm"); });
test("detects bun from user agent", () => { delete process.env.PACKAGE_MANAGER; process.env.npm_config_user_agent = "bun/1.0.0 npm/? node/v20.0.0"; expect(detectPackageManager()).toBe("bun"); });
test("detects vlt from user agent", () => { delete process.env.PACKAGE_MANAGER; process.env.npm_config_user_agent = "vlt/1.0.0 node/v20.0.0"; expect(detectPackageManager()).toBe("vlt"); });
test("detects deno from user agent", () => { delete process.env.PACKAGE_MANAGER; process.env.npm_config_user_agent = "deno/2.0.0 node/v20.0.0"; expect(detectPackageManager()).toBe("deno"); });
test("respects PACKAGE_MANAGER=vlt override", () => { process.env.PACKAGE_MANAGER = "vlt"; delete process.env.npm_config_user_agent; expect(detectPackageManager()).toBe("vlt"); });
test("respects PACKAGE_MANAGER=deno override", () => { process.env.PACKAGE_MANAGER = "deno"; delete process.env.npm_config_user_agent; expect(detectPackageManager()).toBe("deno"); });
test("ignores invalid PACKAGE_MANAGER values", () => { process.env.PACKAGE_MANAGER = "pip"; delete process.env.npm_config_user_agent; // Should not return "pip"; falls through to lockfile or default const result = detectPackageManager(); expect(["npm", "pnpm", "yarn", "bun", "vlt", "deno"]).toContain(result); });});
// ============================================================// detectPackageManager – lockfile detection// ============================================================
describe("detectPackageManager (lockfile detection)", () => { let originalEnv: RestoredEnv; let originalCwd: string; let tmpDir: string;
beforeEach(async () => { originalEnv = { PACKAGE_MANAGER: process.env.PACKAGE_MANAGER, npm_config_user_agent: process.env.npm_config_user_agent, }; delete process.env.PACKAGE_MANAGER; delete process.env.npm_config_user_agent; originalCwd = process.cwd(); tmpDir = await makeTmpDir(); process.chdir(tmpDir); });
afterEach(() => { process.chdir(originalCwd); for (const key of ENV_KEYS) { const originalValue = originalEnv[key]; if (originalValue === undefined) { delete process.env[key]; } else { process.env[key] = originalValue; } } });
test("detects vlt from vlt-lock.json", async () => { await writeFile(join(tmpDir, "vlt-lock.json"), "{}"); expect(detectPackageManager()).toBe("vlt"); });
test("detects deno from deno.lock", async () => { await writeFile(join(tmpDir, "deno.lock"), "{}"); expect(detectPackageManager()).toBe("deno"); });
test("deno.lock takes precedence over package-lock.json", async () => { await writeFile(join(tmpDir, "deno.lock"), "{}"); await writeFile(join(tmpDir, "package-lock.json"), "{}"); expect(detectPackageManager()).toBe("deno"); });
test("vlt-lock.json takes precedence over package-lock.json", async () => { await writeFile(join(tmpDir, "vlt-lock.json"), "{}"); await writeFile(join(tmpDir, "package-lock.json"), "{}"); expect(detectPackageManager()).toBe("vlt"); });
test("defaults to npm when no lockfile is present", () => { expect(detectPackageManager()).toBe("npm"); });});
// ============================================================// buildInstallCommand// ============================================================
describe("buildInstallCommand", () => { test("builds correct pnpm install command", () => { expect(buildInstallCommand(["@astrojs/vercel"], "pnpm")).toBe( "pnpm add -D @astrojs/vercel" ); });
test("builds correct npm install command", () => { expect(buildInstallCommand(["@astrojs/vercel"], "npm")).toBe( "npm install --save-dev @astrojs/vercel" ); });
test("builds correct yarn install command", () => { expect(buildInstallCommand(["@astrojs/vercel"], "yarn")).toBe( "yarn add --dev @astrojs/vercel" ); });
test("builds correct bun install command", () => { expect(buildInstallCommand(["@astrojs/vercel"], "bun")).toBe( "bun add -D @astrojs/vercel" ); });
test("builds correct vlt install command", () => { expect(buildInstallCommand(["@astrojs/vercel"], "vlt")).toBe( "vlt install -D @astrojs/vercel" ); });
test("builds correct deno install command", () => { expect(buildInstallCommand(["@astrojs/vercel"], "deno")).toBe( "deno add --dev @astrojs/vercel" ); });
test("installs multiple packages in one command", () => { expect( buildInstallCommand(["@astrojs/vercel", "@astrojs/netlify"], "pnpm") ).toBe("pnpm add -D @astrojs/vercel @astrojs/netlify"); });
test("respects asDev=false for production dependencies", () => { expect(buildInstallCommand(["@astrojs/node"], "npm", false)).toBe( "npm install @astrojs/node" ); });
test("respects asDev=false for vlt production dependencies", () => { expect(buildInstallCommand(["@astrojs/node"], "vlt", false)).toBe( "vlt install @astrojs/node" ); });
test("respects asDev=false for deno production dependencies", () => { expect(buildInstallCommand(["@astrojs/node"], "deno", false)).toBe( "deno add @astrojs/node" ); });});
// ============================================================// buildRemoveCommand// ============================================================
describe("buildRemoveCommand", () => { test("builds correct pnpm remove command", () => { expect(buildRemoveCommand(["@astrojs/vercel"], "pnpm")).toBe( "pnpm remove @astrojs/vercel" ); });
test("builds correct npm uninstall command", () => { expect(buildRemoveCommand(["@astrojs/vercel"], "npm")).toBe( "npm uninstall @astrojs/vercel" ); });
test("builds correct yarn remove command", () => { expect(buildRemoveCommand(["@astrojs/vercel"], "yarn")).toBe( "yarn remove @astrojs/vercel" ); });
test("builds correct bun remove command", () => { expect(buildRemoveCommand(["@astrojs/vercel"], "bun")).toBe( "bun remove @astrojs/vercel" ); });
test("builds correct vlt uninstall command", () => { expect(buildRemoveCommand(["@astrojs/vercel"], "vlt")).toBe( "vlt uninstall @astrojs/vercel" ); });
test("builds correct deno remove command", () => { expect(buildRemoveCommand(["@astrojs/vercel"], "deno")).toBe( "deno remove @astrojs/vercel" ); });
test("removes multiple packages in one command", () => { expect( buildRemoveCommand(["@astrojs/vercel", "@astrojs/netlify"], "pnpm") ).toBe("pnpm remove @astrojs/vercel @astrojs/netlify"); });});
// ============================================================// getInstalledAdapters// ============================================================
describe("getInstalledAdapters", () => { let tmpDir: string;
beforeEach(async () => { tmpDir = await makeTmpDir(); });
test("returns empty set when no package.json exists", async () => { const result = await getInstalledAdapters(tmpDir); expect(result.size).toBe(0); });
test("detects adapters in dependencies", async () => { await writeFile( join(tmpDir, "package.json"), JSON.stringify({ dependencies: { "@astrojs/vercel": "^8.0.0", "@astrojs/node": "^9.0.0", }, }) ); const result = await getInstalledAdapters(tmpDir); expect(result.has("vercel")).toBe(true); expect(result.has("node")).toBe(true); expect(result.has("netlify")).toBe(false); });
test("detects adapters in devDependencies", async () => { await writeFile( join(tmpDir, "package.json"), JSON.stringify({ devDependencies: { "@astrojs/netlify": "^6.0.0", "astro-sst": "^3.0.0", }, }) ); const result = await getInstalledAdapters(tmpDir); expect(result.has("netlify")).toBe(true); expect(result.has("sst")).toBe(true); expect(result.has("cloudflare")).toBe(false); });
test("merges dependencies and devDependencies", async () => { await writeFile( join(tmpDir, "package.json"), JSON.stringify({ dependencies: { "@astrojs/vercel": "^8.0.0", }, devDependencies: { "@astrojs/node": "^9.0.0", }, }) ); const result = await getInstalledAdapters(tmpDir); expect(result.has("vercel")).toBe(true); expect(result.has("node")).toBe(true); });
test("returns empty set for malformed package.json", async () => { await writeFile(join(tmpDir, "package.json"), "{ invalid json }"); const result = await getInstalledAdapters(tmpDir); expect(result.size).toBe(0); });
test("detects all built-in adapters when all installed", async () => { await writeFile( join(tmpDir, "package.json"), JSON.stringify({ dependencies: { "@astrojs/node": "^9.0.0", "@astrojs/vercel": "^8.0.0", "@astrojs/netlify": "^6.0.0", "@astrojs/cloudflare": "^12.0.0", "@deno/astro-adapter": "^0.3.0", "astro-sst": "^3.0.0", }, }) ); const result = await getInstalledAdapters(tmpDir); expect(result.size).toBe(6); });});
// ============================================================// runCommand// ============================================================
describe("runCommand", () => { test("returns true for a successful command", () => { expect(runCommand("node --version", true)).toBe(true); });
test("returns false for a failing command", () => { expect(runCommand("node -e 'process.exit(1)'", true)).toBe(false); });
test("returns false for a nonexistent command", () => { expect(runCommand("this-command-does-not-exist-xyz", true)).toBe(false); });});
// ============================================================// getCliVersion// ============================================================
describe("getCliVersion", () => { test("reads the version from a package.json file", async () => { const tmpDir = await makeTmpDir(); const pkgPath = join(tmpDir, "package.json");
await writeFile(pkgPath, JSON.stringify({ version: "1.2.3" }));
await expect(getCliVersion(pathToFileURL(pkgPath))).resolves.toBe( "1.2.3" ); });
test("falls back when package.json is missing or invalid", async () => { const missing = join(await makeTmpDir(), "missing-package.json");
await expect(getCliVersion(pathToFileURL(missing))).resolves.toBe( "0.0.0" ); });});