Files
seal-use/src/cli/run/runner.test.ts
T
gitea ca9d87e13f Group CLI modules into parse, run, output, and help
Move parser, tokenize, and area-aliases under cli/parse; runner and read-only under cli/run; printer under cli/output; help plus the docs/naming contract tests under cli/help. types.ts and helpers.ts stay at the cli root. Update imports, the app-root depth calculation, test fixtures, and the CLI doc generator path. No behavior change.
2026-11-19 16:38:14 +08:00

193 lines
7.7 KiB
TypeScript

import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { runCli } from "./runner.js";
import { saveUpdateCache, UPDATE_CACHE_FILE_ENV } from "../../core/update/update-cache.js";
import { GLOBAL_SKILL_DIR_ENV } from "../../core/skill/skill-version.js";
describe("CLI runner", () => {
let tempSkillDir: string;
let prevSkillEnv: string | undefined;
beforeEach(() => {
tempSkillDir = mkdtempSync(join(tmpdir(), "seal-use-runner-skill-"));
prevSkillEnv = process.env[GLOBAL_SKILL_DIR_ENV];
process.env[GLOBAL_SKILL_DIR_ENV] = tempSkillDir;
});
afterEach(() => {
if (prevSkillEnv !== undefined) {
process.env[GLOBAL_SKILL_DIR_ENV] = prevSkillEnv;
} else {
delete process.env[GLOBAL_SKILL_DIR_ENV];
}
rmSync(tempSkillDir, { recursive: true, force: true });
});
test("returns version output without touching the process lifecycle", async () => {
const tempDir = mkdtempSync(join(tmpdir(), "seal-use-runner-version-test-"));
const tempCacheFile = join(tempDir, "update-cache.json");
const prevEnv = process.env[UPDATE_CACHE_FILE_ENV];
process.env[UPDATE_CACHE_FILE_ENV] = tempCacheFile;
try {
const result = await runCli(["version"]);
expect(result).toMatchObject({
stderr: "",
isError: false,
exitCode: 0
});
expect(JSON.parse(result.stdout)).toMatchObject({ name: "seal-use" });
} finally {
if (prevEnv !== undefined) {
process.env[UPDATE_CACHE_FILE_ENV] = prevEnv;
} else {
delete process.env[UPDATE_CACHE_FILE_ENV];
}
rmSync(tempDir, { recursive: true, force: true });
}
});
test("returns diagnostics and a non-zero exit code for invalid commands", async () => {
const result = await runCli(["not-a-command"]);
expect(result.isError).toBe(true);
expect(result.exitCode).toBe(2);
expect(result.stderr).toContain("Unknown command: not-a-command");
});
test("returns a semantic usage exit code for invalid JSON", async () => {
const result = await runCli(["version", "--json", "not-json"]);
expect(result.isError).toBe(true);
expect(result.exitCode).toBe(2);
expect(result.stderr).toContain("Invalid JSON:");
});
test("returns usage exit code 2 for unknown subcommands across standalone domains", async () => {
const result = await runCli(["skill", "not-a-subcommand"]);
expect(result.isError).toBe(true);
expect(result.exitCode).toBe(2);
expect(result.stderr).toContain("Unknown skill command: not-a-subcommand");
});
test("returns usage exit code 2 for unknown subcommands with an active enterprise", async () => {
const tempDir = mkdtempSync(join(tmpdir(), "seal-use-runner-test-"));
const originalEnv = process.env.SEAL_USE_ENTERPRISES_DIR;
try {
process.env.SEAL_USE_ENTERPRISES_DIR = tempDir;
writeFileSync(
join(tempDir, "test.json"),
JSON.stringify({
id: "test",
name: "Test Corp",
source: { type: "seal", token: "dummy-token", sealUrl: "https://test.sealai.cc" },
seal: { url: "https://test.sealai.cc" }
})
);
const result = await runCli(["contacts", "not-a-subcommand", "--corp", "test"]);
expect(result.isError).toBe(true);
expect(result.exitCode).toBe(2);
expect(result.stderr).toContain("Unknown contacts command: not-a-subcommand");
} finally {
if (originalEnv === undefined) delete process.env.SEAL_USE_ENTERPRISES_DIR;
else process.env.SEAL_USE_ENTERPRISES_DIR = originalEnv;
rmSync(tempDir, { recursive: true, force: true });
}
});
test("supports update aliases (upgrade, up) with notice in stderr and exit code 0", async () => {
const upgradeResult = await runCli(["upgrade", "0.3.3"]);
expect(upgradeResult.exitCode).toBe(0);
expect(upgradeResult.isError).toBe(false);
expect(upgradeResult.stderr).toContain("Notice: 'upgrade' is an alias for 'seal-use update'");
expect(JSON.parse(upgradeResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" });
const upResult = await runCli(["up", "0.3.3"]);
expect(upResult.exitCode).toBe(0);
expect(upResult.isError).toBe(false);
expect(upResult.stderr).toContain("Notice: 'up' is an alias for 'seal-use update'");
expect(JSON.parse(upResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" });
});
test("supports case-insensitive update invocations with original command in notice", async () => {
const mixedResult = await runCli(["Update", "0.3.3"]);
expect(mixedResult.exitCode).toBe(0);
expect(mixedResult.stderr).toContain("Notice: 'Update' is an alias for 'seal-use update'");
expect(JSON.parse(mixedResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" });
const upperResult = await runCli(["UPGRADE", "0.3.3"]);
expect(upperResult.exitCode).toBe(0);
expect(upperResult.stderr).toContain("Notice: 'UPGRADE' is an alias for 'seal-use update'");
expect(JSON.parse(upperResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" });
});
test("canonical update command does not emit alias notice", async () => {
const result = await runCli(["update", "0.3.3"]);
expect(result.exitCode).toBe(0);
expect(result.stderr).not.toContain("is an alias for 'seal-use update'");
expect(JSON.parse(result.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" });
});
test("version output enriches with cache and alerts on newer version via stderr", async () => {
const tempDir = mkdtempSync(join(tmpdir(), "seal-use-version-test-"));
const tempCacheFile = join(tempDir, "update-cache.json");
const prevEnv = process.env[UPDATE_CACHE_FILE_ENV];
process.env[UPDATE_CACHE_FILE_ENV] = tempCacheFile;
try {
// 1. Without cache: plain version output
const coldResult = await runCli(["version"]);
expect(coldResult.exitCode).toBe(0);
expect(coldResult.stderr).toBe("");
const coldJson = JSON.parse(coldResult.stdout);
expect(coldJson.name).toBe("seal-use");
expect(coldJson.latestVersion).toBeUndefined();
// 2. With cache (newer version available)
saveUpdateCache(
{
latestVersion: "99.0.0",
publishedAt: "2026-09-20T12:00:00Z",
releaseUrl: "https://github.com/yunyingk/seal-use/releases/tag/v99.0.0"
},
tempCacheFile
);
const warmResult = await runCli(["version"]);
expect(warmResult.exitCode).toBe(0);
expect(warmResult.stderr).toContain("💡 发现新版本可用:");
expect(warmResult.stderr).toContain("99.0.0");
expect(warmResult.stderr).toContain("👉 Next action: seal-use update");
const warmJson = JSON.parse(warmResult.stdout);
expect(warmJson.latestVersion).toBe("99.0.0");
expect(warmJson.updateAvailable).toBe(true);
expect(typeof warmJson.lastCheckedAt).toBe("string");
} finally {
if (prevEnv !== undefined) {
process.env[UPDATE_CACHE_FILE_ENV] = prevEnv;
} else {
delete process.env[UPDATE_CACHE_FILE_ENV];
}
rmSync(tempDir, { recursive: true, force: true });
}
});
test("version output alerts on newer installed skill via stderr", async () => {
writeFileSync(
join(tempSkillDir, "SKILL.md"),
"---\nname: seal-use\nmetadata:\n version: \"99.99.99\"\n---\n"
);
const result = await runCli(["version"]);
expect(result.exitCode).toBe(0);
expect(result.stderr).toContain("技能版本与 CLI 需要核对: CLI");
expect(result.stderr).toContain("Skill 99.99.99");
expect(result.stderr).toContain("👉 Next action: seal-use update");
});
});