Files
seal-use/src/core/embedded-skill.test.ts
T

71 lines
3.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { mkdtempSync, rmSync, existsSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import packageJson from "../../package.json" with { type: "json" };
import {
EMBEDDED_SKILL_ALL_PATHS,
EMBEDDED_SKILL_ASSETS,
EMBEDDED_SKILL_FILES,
EMBEDDED_SKILL_VERSION,
getSkillContent,
installSkillFiles,
parseSkillVersion,
readInstalledSkillVersion
} from "./embedded-skill.js";
describe("embedded-skill", () => {
test("embeds skill text and asset files", () => {
expect(EMBEDDED_SKILL_FILES["SKILL.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["SKILL.md"]).toContain("name: seal-use");
expect(EMBEDDED_SKILL_FILES["references/cli.md"]).toBeDefined();
expect(EMBEDDED_SKILL_FILES["references/enterprise-onboarding.md"]).toContain("# Enterprise Onboarding");
expect(EMBEDDED_SKILL_FILES["references/first-time-user.md"]).toContain("复制 → 以 cURL 格式复制");
expect(EMBEDDED_SKILL_ASSETS["assets/hose-curl-copy-as-curl.png"]).toBeDefined();
expect(EMBEDDED_SKILL_ALL_PATHS).toContain("assets/hose-curl-copy-as-curl.png");
expect(getSkillContent()).toContain("name: seal-use");
});
test("installSkillFiles writes all embedded files to target directory", () => {
const tmp = mkdtempSync(join(tmpdir(), "skill-test-"));
try {
const res = installSkillFiles(tmp);
expect(res.targetDir).toBe(tmp);
expect(res.files.length).toBeGreaterThan(5);
expect(existsSync(join(tmp, "SKILL.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "cli.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "enterprise-onboarding.md"))).toBe(true);
expect(existsSync(join(tmp, "references", "first-time-user.md"))).toBe(true);
expect(existsSync(join(tmp, "assets", "hose-curl-copy-as-curl.png"))).toBe(true);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
});
describe("embedded-skill version contract", () => {
// A version-tagged document that nothing asserts on drifts silently: `seal-use update` shipped
// the previous release's skill for several versions, because "the file was written" looked like
// success from every angle. Keeping the skill and the package in step is a failing test now
// rather than a discovery.
test("the skill's version matches the package version", () => {
expect(EMBEDDED_SKILL_VERSION).toBe(packageJson.version);
});
test("parses the version out of SKILL.md front matter", () => {
expect(parseSkillVersion("---\nname: seal-use\nversion: 1.2.3\n---\n")).toBe("1.2.3");
expect(parseSkillVersion("# no front matter here")).toBeUndefined();
});
test("reads back the version of an installed skill", () => {
const tmp = mkdtempSync(join(tmpdir(), "skill-version-"));
try {
const res = installSkillFiles(tmp);
expect(readInstalledSkillVersion(join(res.targetDir, "SKILL.md"))).toBe(EMBEDDED_SKILL_VERSION);
expect(readInstalledSkillVersion(join(tmp, "missing.md"))).toBeUndefined();
} finally {
rmSync(tmp, { recursive: true, force: true });
}
});
});