diff --git a/apps/desktop/src/pages/harness/__tests__/SyncPage.test.tsx b/apps/desktop/src/pages/harness/__tests__/SyncPage.test.tsx index 58674820..b9e07ac9 100644 --- a/apps/desktop/src/pages/harness/__tests__/SyncPage.test.tsx +++ b/apps/desktop/src/pages/harness/__tests__/SyncPage.test.tsx @@ -93,6 +93,11 @@ function renderPage() { describe("SyncPage", () => { beforeEach(() => { vi.clearAllMocks(); + // clearAllMocks keeps queued mock*Once values; a test that fails before + // using its own would hand them to the next test. mockReset drops the + // queue and restores the factory implementation. + vi.mocked(compile).mockReset(); + vi.mocked(detectPlatforms).mockReset(); projectHarness = 'version: "1"'; mockReadHarnessFile.mockResolvedValue({ found: false, content: null, path: null }); mockSyncFileExists.mockResolvedValue(false); @@ -202,7 +207,9 @@ describe("SyncPage", () => { ); const palette = () => within(screen.getByRole("dialog", { name: "Command palette" })); await screen.findByText(/Directory found/i); - const preview = palette().getByRole("option", { name: "Preview compile changes" }); + // SyncPage registers its commands in an effect, which can land after the + // directory check renders. + const preview = await palette().findByRole("option", { name: "Preview compile changes" }); await waitFor(() => expect(preview).not.toHaveAttribute("aria-disabled")); expect(palette().queryByRole("option", { name: /Apply/ })).not.toBeInTheDocument(); diff --git a/packages/core/__tests__/import-unreadable-skill.test.ts b/packages/core/__tests__/import-unreadable-skill.test.ts new file mode 100644 index 00000000..00e6e7ec --- /dev/null +++ b/packages/core/__tests__/import-unreadable-skill.test.ts @@ -0,0 +1,57 @@ +import { describe, it, expect } from "vitest"; +import { importMachine } from "../src/import/import-project.js"; +import { readAdapterSkills } from "../src/import/read-skills.js"; +import { MockFsProvider } from "./helpers/mock-fs.js"; + +const HOME = "/home/user"; +const BLOCKED = `${HOME}/.claude/skills/linked/SKILL.md`; + +/** + * A reader that refuses one file the way Tauri's fs scope does for a symlinked + * SKILL.md whose target lies outside the capability scope. + */ +class OneForbiddenFile extends MockFsProvider { + async readFile(path: string): Promise { + if (path === BLOCKED) { + throw new Error(`forbidden path: ${path}, maybe it is not allowed on the scope`); + } + return super.readFile(path); + } +} + +function machine(): OneForbiddenFile { + return new OneForbiddenFile( + { + [`${HOME}/CLAUDE.md`]: "# Global instructions\n", + [`${HOME}/.claude/skills/alpha/SKILL.md`]: "---\nname: alpha\n---\nAlpha body\n", + [BLOCKED]: "---\nname: linked\n---\nUnreachable\n", + [`${HOME}/.claude/skills/zeta/SKILL.md`]: "---\nname: zeta\n---\nZeta body\n", + }, + HOME, + HOME, + ); +} + +describe("machine scan with an unreadable skill file", () => { + it("keeps the readable skills and reports the unreadable one as skipped", async () => { + const fragment = await readAdapterSkills(machine(), HOME, "claude-code"); + + expect(fragment?.skills?.skills.map((skill) => skill.name)).toEqual(["alpha", "zeta"]); + expect(fragment?.skipped).toEqual([ + { file: ".claude/skills/linked/SKILL.md", reason: expect.stringContaining("forbidden path") }, + ]); + }); + + it("completes importMachine instead of rejecting, and surfaces the skip in findings", async () => { + const result = await importMachine({ fs: machine() }); + + const claudeCode = result.findings.adapters.find((adapter) => adapter.adapter === "claude-code")!; + expect(claudeCode.skipped).toContainEqual({ + file: ".claude/skills/linked/SKILL.md", + reason: expect.stringContaining("forbidden path"), + }); + expect(result.harnessYaml).toContain("alpha"); + expect(result.harnessYaml).toContain("zeta"); + expect(result.harnessYaml).not.toContain("linked"); + }); +}); diff --git a/packages/core/src/import/read-skills.ts b/packages/core/src/import/read-skills.ts index 86d8fb27..965894d5 100644 --- a/packages/core/src/import/read-skills.ts +++ b/packages/core/src/import/read-skills.ts @@ -68,6 +68,7 @@ export async function readAdapterSkills( adapter: AdapterId, ): Promise { const skills: ImportedSkillRef[] = []; + const skipped: Array<{ file: string; reason: string }> = []; const seen = new Set(); const personal = projectRoot === (await fs.homedir()); const directories = personal ? GLOBAL_SKILL_DIRS[adapter] : PROJECT_SKILL_DIRS[adapter]; @@ -76,11 +77,21 @@ export async function readAdapterSkills( const root = fs.joinPath(projectRoot, directory); if (!(await fs.exists(root))) continue; for (const file of (await findSkillFiles(root, fs, 8)).sort()) { - const content = await fs.readFile(file); const relative = relativeTo(projectRoot, file); + // One unreadable skill must not abort the scan. The desktop fs scope + // refuses a SKILL.md symlinked outside it ("forbidden path"), and a + // capsule over the size limits throws too; report the skill as skipped. + let content: string; + let directoryFiles: Awaited>; + try { + content = await fs.readFile(file); + directoryFiles = await collectCapsuleFiles(fs, dirname(file)); + } catch (error) { + const reason = error instanceof Error ? error.message : String(error); + skipped.push({ file: relative, reason: `could not read skill: ${reason}` }); + continue; + } const name = frontmatterName(content, dirname(relative).split("/").pop() ?? "skill"); - const sourceRoot = dirname(file); - const directoryFiles = await collectCapsuleFiles(fs, sourceRoot); const digest = skillDirectoryDigest(directoryFiles.map((entry) => ({ path: entry.path, digest: computeFileHash(entry.content), @@ -100,7 +111,12 @@ export async function readAdapterSkills( } } - return skills.length - ? { domain: "skills", config: {}, warnings: [], skills: { skills } } - : null; + if (!skills.length && !skipped.length) return null; + return { + domain: "skills", + config: {}, + warnings: [], + ...(skills.length ? { skills: { skills } } : {}), + ...(skipped.length ? { skipped } : {}), + }; }