Files
qiming/qimingclaw/crates/agent-electron-client/src/main/services/digitalEmployee/planTemplateService.test.ts
2026-06-07 19:53:24 +08:00

106 lines
3.1 KiB
TypeScript

import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const mockElectron = vi.hoisted(() => ({
homeDir: "",
}));
vi.mock("electron", () => ({
app: {
getPath: (key: string) => {
if (key === "home") return mockElectron.homeDir;
return mockElectron.homeDir;
},
},
}));
vi.mock("electron-log", () => ({
default: {
warn: vi.fn(),
},
}));
describe("digital employee plan template service", () => {
beforeEach(() => {
mockElectron.homeDir = fs.mkdtempSync(path.join(os.tmpdir(), "qiming-plan-templates-"));
});
afterEach(() => {
fs.rmSync(mockElectron.homeDir, { recursive: true, force: true });
});
it("returns an empty list when the template directory is missing", async () => {
const { listDigitalEmployeePlanTemplates } = await import("./planTemplateService");
const result = listDigitalEmployeePlanTemplates();
expect(result.templates).toEqual([]);
expect(result.templateDir).toBe(path.join(mockElectron.homeDir, ".qimingclaw", "plan_templates"));
});
it("reads JSON and TOML plan templates from qimingclaw home", async () => {
const templateDir = path.join(mockElectron.homeDir, ".qimingclaw", "plan_templates");
fs.mkdirSync(templateDir, { recursive: true });
fs.writeFileSync(
path.join(templateDir, "customer-followup.json"),
JSON.stringify({
plan_id: "customer-followup",
title: "客户跟进",
objective: "汇总客户待办并生成跟进计划",
steps: [
{
step_id: "collect",
title: "收集待办",
preferred_skills: ["qimingclaw-mcp-tools"],
tasks: [{ task_id: "collect-crm", title: "读取 CRM 待办" }],
},
],
}),
);
fs.writeFileSync(
path.join(templateDir, "daily-report.toml"),
`id = "daily-report"\n` +
`title = "日报模板"\n` +
`objective = "生成今日业务日报"\n` +
`[[steps]]\n` +
`id = "write"\n` +
`title = "撰写日报"\n` +
`skills = ["qimingclaw-artifact-report"]\n` +
`[[steps.tasks]]\n` +
`id = "write-report"\n` +
`title = "整理日报正文"\n`,
);
const { listDigitalEmployeePlanTemplates } = await import("./planTemplateService");
const result = listDigitalEmployeePlanTemplates();
expect(result.templates).toEqual([
expect.objectContaining({
plan_id: "customer-followup",
title: "客户跟进",
steps: [
expect.objectContaining({
step_id: "collect",
preferred_skills: ["qimingclaw-mcp-tools"],
tasks: [expect.objectContaining({ task_id: "collect-crm" })],
}),
],
}),
expect.objectContaining({
plan_id: "daily-report",
title: "日报模板",
steps: [
expect.objectContaining({
step_id: "write",
preferred_skills: ["qimingclaw-artifact-report"],
tasks: [expect.objectContaining({ task_id: "write-report" })],
}),
],
}),
]);
});
});