feat: CLI 入口和 init 命令

This commit is contained in:
2026-06-08 17:28:37 +08:00
parent 7530a5a743
commit 8e126a95c6
3 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
import { existsSync } from "node:fs";
import { mkdir, rm, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { runInit } from "../../src/commands/init.ts";
const TMP_DIR = join(import.meta.dir, "__tmp_init_test__");
beforeEach(async () => {
await mkdir(TMP_DIR, { recursive: true });
});
afterEach(async () => {
await rm(TMP_DIR, { recursive: true, force: true });
});
describe("runInit", () => {
it("创建 .rune 目录和默认 config.yaml", async () => {
await runInit(TMP_DIR, ["opencode"]);
expect(existsSync(join(TMP_DIR, ".rune"))).toBe(true);
expect(existsSync(join(TMP_DIR, ".rune", "config.yaml"))).toBe(true);
const content = await readFile(
join(TMP_DIR, ".rune", "config.yaml"),
"utf-8",
);
expect(content).toContain("discuss");
expect(content).toContain("plan");
expect(content).toContain("build");
expect(content).toContain("archive");
});
it("创建 .rune/changes 和 .rune/archive 目录", async () => {
await runInit(TMP_DIR, ["opencode"]);
expect(existsSync(join(TMP_DIR, ".rune", "changes"))).toBe(true);
expect(existsSync(join(TMP_DIR, ".rune", "archive"))).toBe(true);
});
it("注入 OpenCode command 和 skill", async () => {
await runInit(TMP_DIR, ["opencode"]);
expect(existsSync(join(TMP_DIR, ".opencode", "commands", "discuss.md"))).toBe(true);
expect(existsSync(join(TMP_DIR, ".opencode", "skills", "rune-discuss.md"))).toBe(true);
});
it("重复 init 不覆盖 config.yaml", async () => {
await runInit(TMP_DIR, ["opencode"]);
await writeFile(
join(TMP_DIR, ".rune", "config.yaml"),
"自定义内容",
);
await runInit(TMP_DIR, ["opencode"]);
const content = await readFile(
join(TMP_DIR, ".rune", "config.yaml"),
"utf-8",
);
expect(content).toBe("自定义内容");
});
it("不支持的工具名抛出错误", async () => {
await expect(runInit(TMP_DIR, ["unknown-tool"])).rejects.toThrow(
"不支持的工具",
);
});
});