|
| 1 | +// Plugin runtime symlink tests cover doctor detection of stale global symlinks. |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 6 | +import { |
| 7 | + collectStalePluginRuntimeSymlinkHealthFindings, |
| 8 | + collectStalePluginRuntimeSymlinks, |
| 9 | + stalePluginRuntimeSymlinkToHealthFinding, |
| 10 | +} from "./plugin-runtime-symlinks.js"; |
| 11 | + |
| 12 | +async function expectSymlinkPresent(targetPath: string): Promise<void> { |
| 13 | + expect((await fs.lstat(targetPath)).isSymbolicLink()).toBe(true); |
| 14 | +} |
| 15 | + |
| 16 | +async function canCreateDirectorySymlink(root: string): Promise<boolean> { |
| 17 | + const target = path.join(root, "symlink-capability-target"); |
| 18 | + const link = path.join(root, "symlink-capability-link"); |
| 19 | + await fs.mkdir(target, { recursive: true }); |
| 20 | + try { |
| 21 | + await fs.symlink(target, link, "dir"); |
| 22 | + return (await fs.lstat(link)).isSymbolicLink(); |
| 23 | + } catch (error) { |
| 24 | + const code = (error as NodeJS.ErrnoException | undefined)?.code; |
| 25 | + if (code === "EPERM" || code === "EACCES" || code === "ENOTSUP") { |
| 26 | + return false; |
| 27 | + } |
| 28 | + throw error; |
| 29 | + } finally { |
| 30 | + await fs.rm(link, { recursive: true, force: true }); |
| 31 | + await fs.rm(target, { recursive: true, force: true }); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +describe("plugin runtime symlink health findings", () => { |
| 36 | + let tempDir: string; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-plugin-runtime-symlinks-")); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(async () => { |
| 43 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("maps dangling plugin-runtime symlinks to read-only lint findings", async () => { |
| 47 | + if (!(await canCreateDirectorySymlink(tempDir))) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const packageRoot = path.join(tempDir, "prefix", "lib", "node_modules", "openclaw"); |
| 52 | + const nodeModulesRoot = path.dirname(packageRoot); |
| 53 | + const legacyRoot = path.join(tempDir, "state", "plugin-runtime-deps"); |
| 54 | + const missingTarget = path.join( |
| 55 | + legacyRoot, |
| 56 | + "openclaw-slack", |
| 57 | + "node_modules", |
| 58 | + "@slack", |
| 59 | + "web-api", |
| 60 | + ); |
| 61 | + const scopeRoot = path.join(nodeModulesRoot, "@slack"); |
| 62 | + const staleLink = path.join(scopeRoot, "web-api"); |
| 63 | + const liveTarget = path.join(tempDir, "live", "@slack", "bolt"); |
| 64 | + const liveLink = path.join(scopeRoot, "bolt"); |
| 65 | + |
| 66 | + await fs.mkdir(packageRoot, { recursive: true }); |
| 67 | + await fs.mkdir(scopeRoot, { recursive: true }); |
| 68 | + await fs.mkdir(liveTarget, { recursive: true }); |
| 69 | + await fs.symlink(missingTarget, staleLink, "dir"); |
| 70 | + await fs.symlink(liveTarget, liveLink, "dir"); |
| 71 | + |
| 72 | + const [stale] = await collectStalePluginRuntimeSymlinks(packageRoot); |
| 73 | + if (!stale) { |
| 74 | + throw new Error("expected stale plugin-runtime symlink finding"); |
| 75 | + } |
| 76 | + |
| 77 | + expect(stale).toEqual({ |
| 78 | + name: "@slack/web-api", |
| 79 | + path: staleLink, |
| 80 | + target: missingTarget, |
| 81 | + }); |
| 82 | + expect(stalePluginRuntimeSymlinkToHealthFinding(stale)).toEqual({ |
| 83 | + checkId: "core/doctor/stale-plugin-runtime-symlinks", |
| 84 | + severity: "warning", |
| 85 | + message: `Stale plugin-runtime symlink @slack/web-api points at ${missingTarget}.`, |
| 86 | + path: staleLink, |
| 87 | + target: staleLink, |
| 88 | + requirement: "stale-plugin-runtime-symlink-removed", |
| 89 | + fixHint: "Run `openclaw doctor --fix` to remove stale plugin-runtime symlinks.", |
| 90 | + }); |
| 91 | + expect(await collectStalePluginRuntimeSymlinkHealthFindings({ packageRoot })).toEqual([ |
| 92 | + expect.objectContaining({ |
| 93 | + checkId: "core/doctor/stale-plugin-runtime-symlinks", |
| 94 | + path: staleLink, |
| 95 | + }), |
| 96 | + ]); |
| 97 | + await expectSymlinkPresent(staleLink); |
| 98 | + await expectSymlinkPresent(liveLink); |
| 99 | + }); |
| 100 | + |
| 101 | + it("reports symlinks that point inside classified stale roots", async () => { |
| 102 | + if (!(await canCreateDirectorySymlink(tempDir))) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const packageRoot = path.join(tempDir, "prefix", "lib", "node_modules", "openclaw"); |
| 107 | + const nodeModulesRoot = path.dirname(packageRoot); |
| 108 | + const legacyRoot = path.join(tempDir, "state", "plugin-runtime-deps"); |
| 109 | + const existingTarget = path.join(legacyRoot, "openclaw-demo", "node_modules", "left-pad"); |
| 110 | + const staleLink = path.join(nodeModulesRoot, "left-pad"); |
| 111 | + |
| 112 | + await fs.mkdir(packageRoot, { recursive: true }); |
| 113 | + await fs.mkdir(existingTarget, { recursive: true }); |
| 114 | + await fs.symlink(existingTarget, staleLink, "dir"); |
| 115 | + |
| 116 | + await expect(collectStalePluginRuntimeSymlinks(packageRoot)).resolves.toEqual([]); |
| 117 | + await expect( |
| 118 | + collectStalePluginRuntimeSymlinkHealthFindings({ |
| 119 | + packageRoot, |
| 120 | + staleRoots: [legacyRoot], |
| 121 | + }), |
| 122 | + ).resolves.toEqual([ |
| 123 | + expect.objectContaining({ |
| 124 | + checkId: "core/doctor/stale-plugin-runtime-symlinks", |
| 125 | + path: staleLink, |
| 126 | + target: staleLink, |
| 127 | + }), |
| 128 | + ]); |
| 129 | + await expectSymlinkPresent(staleLink); |
| 130 | + }); |
| 131 | +}); |
0 commit comments