| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- import { existsSync } from "node:fs";
- import { mkdir, mkdtemp, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { basename, dirname, join } from "node:path";
- import type { Logger } from "@zcode/contracts";
- import { afterEach, describe, expect, it, vi } from "vitest";
- const faults = vi.hoisted(() => ({
- moveTargetSuffix: undefined as string | undefined,
- promoteCurrentTargetSuffix: undefined as string | undefined,
- promoteTargetSuffix: undefined as string | undefined,
- recoverBackupTargetSuffix: undefined as string | undefined,
- removeTargetSuffix: undefined as string | undefined,
- runtimeManifestNotFoundTargetSuffix: undefined as string | undefined,
- runtimeManifestTargetSuffix: undefined as string | undefined,
- seedLockHeldTargetSuffixes: [] as string[],
- seedLockObservedTimeoutsMs: [] as number[],
- }));
- vi.mock("../src/app/official-plugin-cache-fs.js", async (importOriginal) => {
- const actual = await importOriginal<typeof import("../src/app/official-plugin-cache-fs.js")>();
- return {
- ...actual,
- removeOfficialPluginCacheDirectory(path: string): void {
- if (faults.removeTargetSuffix && path.endsWith(faults.removeTargetSuffix)) {
- throw exhaustedErrno("EPERM");
- }
- actual.removeOfficialPluginCacheDirectory(path);
- },
- renameOfficialPluginCachePath(
- fromPath: string,
- toPath: string,
- budget: ReturnType<typeof actual.createOfficialPluginCacheRetryBudget>,
- ): void {
- if (
- faults.recoverBackupTargetSuffix &&
- fromPath === `${toPath}.backup` &&
- toPath.endsWith(faults.recoverBackupTargetSuffix)
- ) {
- throw errno("EOWNER");
- }
- if (
- faults.moveTargetSuffix &&
- fromPath.endsWith(faults.moveTargetSuffix) &&
- toPath.startsWith(`${fromPath}.backup-`)
- ) {
- actual.removeOfficialPluginCacheDirectory(fromPath, budget);
- throw errno("ENOENT");
- }
- if (
- faults.promoteCurrentTargetSuffix &&
- fromPath.includes(".tmp-") &&
- toPath.endsWith(faults.promoteCurrentTargetSuffix)
- ) {
- actual.renameOfficialPluginCachePath(fromPath, toPath, budget);
- throw errno("EEXIST");
- }
- if (
- faults.promoteTargetSuffix &&
- fromPath.includes(".tmp-") &&
- toPath.endsWith(faults.promoteTargetSuffix)
- ) {
- throw exhaustedErrno("EPERM");
- }
- actual.renameOfficialPluginCachePath(fromPath, toPath, budget);
- },
- writeTextFileAtomicallyWithRetry(
- path: string,
- contents: string,
- budget: ReturnType<typeof actual.createOfficialPluginCacheRetryBudget>,
- ): void {
- if (
- faults.runtimeManifestNotFoundTargetSuffix &&
- path.endsWith(faults.runtimeManifestNotFoundTargetSuffix)
- ) {
- throw errno("ENOENT");
- }
- if (faults.runtimeManifestTargetSuffix && path.endsWith(faults.runtimeManifestTargetSuffix)) {
- throw exhaustedErrno("EPERM");
- }
- actual.writeTextFileAtomicallyWithRetry(path, contents, budget);
- },
- };
- });
- vi.mock("../src/app/official-plugin-seed-lock.js", async (importOriginal) => {
- const actual = await importOriginal<typeof import("../src/app/official-plugin-seed-lock.js")>();
- return {
- ...actual,
- withOfficialPluginSeedLock<T>(
- targetRoot: string,
- action: () => T,
- options?: Parameters<typeof actual.withOfficialPluginSeedLock>[2],
- ): T {
- if (!faults.seedLockHeldTargetSuffixes.some((suffix) => targetRoot.endsWith(suffix))) {
- return actual.withOfficialPluginSeedLock(targetRoot, action, options);
- }
- faults.seedLockObservedTimeoutsMs.push(options?.timeoutMs ?? -1);
- // 只压缩等待参数并保留调用方预算上限(默认 15s 会拖垮测试),
- // 仍走真实锁竞争与真实超时错误。
- return actual.withOfficialPluginSeedLock(targetRoot, action, {
- ...options,
- retryDelayMs: 1,
- timeoutMs: Math.min(10, options?.timeoutMs ?? 10),
- });
- },
- };
- });
- import { resolveOfficialPluginRoots } from "../src/app/bundled-plugins.js";
- import { OFFICIAL_PLUGIN_DEFINITIONS } from "../src/app/official-plugin-definitions.js";
- import { resolveZCodePlugins } from "../src/plugins.js";
- const OFFICIAL_CACHE_ROOT = join("cache", "zcode-plugins-official");
- const BROWSER_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, "browser-use", "0.5.1");
- const ANDROID_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, "android-emulator", "0.1.0");
- const SKILL_CREATOR_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, "skill-creator", "0.1.0");
- const ANDROID_MANIFEST_SUFFIX = join(ANDROID_CACHE_SUFFIX, ".zcode-plugin", "plugin.json");
- const CUA_DEFINITION = OFFICIAL_PLUGIN_DEFINITIONS.find(({ name }) => name === "computer-use");
- if (!CUA_DEFINITION) throw new Error("computer-use official plugin definition is missing");
- const CUA_CACHE_SUFFIX = join(OFFICIAL_CACHE_ROOT, CUA_DEFINITION.name, CUA_DEFINITION.version);
- describe("bundled official plugin cache resilience", () => {
- const cleanups: string[] = [];
- const originalEntrypoint = process.argv[1];
- afterEach(async () => {
- process.argv[1] = originalEntrypoint;
- faults.moveTargetSuffix = undefined;
- faults.promoteCurrentTargetSuffix = undefined;
- faults.promoteTargetSuffix = undefined;
- faults.recoverBackupTargetSuffix = undefined;
- faults.removeTargetSuffix = undefined;
- faults.runtimeManifestNotFoundTargetSuffix = undefined;
- faults.runtimeManifestTargetSuffix = undefined;
- faults.seedLockHeldTargetSuffixes = [];
- faults.seedLockObservedTimeoutsMs = [];
- await Promise.all(cleanups.splice(0).map((root) => rm(root, { force: true, recursive: true })));
- });
- it("degrades a persistent transient seed failure when the target is absent", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const logger = createLogger();
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- expect(() => resolveOfficialPluginRoots({ logger, storageRoot })).not.toThrow();
- expect(existsSync(join(storageRoot, BROWSER_CACHE_SUFFIX))).toBe(false);
- expect(existsSync(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX))).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- attempts: 4,
- degraded: true,
- errorCode: "EPERM",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("keeps degraded cache diagnostics through the ordinary plugin resolver", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- const logger = createLogger();
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- expect(() =>
- resolveZCodePlugins({
- logger,
- pluginStorageRoot: storageRoot,
- skipUserConfig: true,
- workingDirectory: dirname(storageRoot),
- }),
- ).not.toThrow();
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "EPERM",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("keeps a stale but usable target when promotion fails", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const staleMarker = `${await readFile(markerPath, "utf8")}\n`;
- await writeFile(markerPath, staleMarker.replace('"hash": "', '"hash": "stale-'));
- const logger = createLogger();
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- expect(() => resolveOfficialPluginRoots({ logger, storageRoot })).not.toThrow();
- expect(existsSync(browserRoot)).toBe(true);
- expect(await readFile(markerPath, "utf8")).toContain('"hash": "stale-');
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- errorCode: "EPERM",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("falls back to a previous usable version when the new target cannot be promoted", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.2.1");
- await rename(currentRoot, staleRoot);
- faults.promoteTargetSuffix = BROWSER_CACHE_SUFFIX;
- const roots = resolveOfficialPluginRoots({ storageRoot });
- expect(roots).toContain(staleRoot);
- });
- it("degrades an incomplete seed source to a usable previous version instead of failing startup", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.2.1");
- await rename(currentRoot, staleRoot);
- await rm(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX), { force: true, recursive: true });
- // 入口目录旁只有一份缺 dist/docs/skills 的 browser-use 拷贝(升级中的桌面包、
- // 未构建 dist 的旧 checkout);其余插件继续从真实 monorepo 源解析。
- const entrypointRoot = join(storageRoot, "stale-entrypoint");
- const incompletePluginRoot = join(entrypointRoot, "packages", "browser-use-plugin");
- await mkdir(join(incompletePluginRoot, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(incompletePluginRoot, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "browser-use", version: "0.5.1" }),
- );
- process.argv[1] = join(entrypointRoot, "zcode.cjs");
- const logger = createLogger();
- let roots: string[] = [];
- expect(() => {
- roots = resolveOfficialPluginRoots({ logger, storageRoot });
- }).not.toThrow();
- expect(roots).toContain(staleRoot);
- // 残缺源绝不能落成当前版本缓存(会永久命中 hash),也不能拖垮其他插件的 seeding。
- expect(existsSync(currentRoot)).toBe(false);
- expect(existsSync(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX))).toBe(true);
- const marketplace = JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
- "utf8",
- ),
- ) as { plugins: Array<{ name: string }> };
- expect(marketplace.plugins.some((plugin) => plugin.name === "browser-use")).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "ZCODE_PLUGIN_SEED_INCOMPLETE",
- // browser-use 不再携带 node_repl 宿主产物(那是 node-repl-host 的),
- // 它自己必需的运行时资产是 browser-client。
- missingSeedPaths: expect.arrayContaining(["scripts/browser-client.mjs"]),
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("keeps session bootstrap alive when an incomplete seed source has no usable fallback cache", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- // 全新安装:storageRoot 从未 seed 过,也没有任何旧版本缓存可回退。
- const entrypointRoot = join(storageRoot, "fresh-install-entrypoint");
- const incompletePluginRoot = join(entrypointRoot, "packages", "browser-use-plugin");
- await mkdir(join(incompletePluginRoot, ".zcode-plugin"), { recursive: true });
- await writeFile(
- join(incompletePluginRoot, ".zcode-plugin", "plugin.json"),
- JSON.stringify({ name: "browser-use", version: "0.5.1" }),
- );
- process.argv[1] = join(entrypointRoot, "zcode.cjs");
- const logger = createLogger();
- let outcome!: ReturnType<typeof resolveZCodePlugins>;
- expect(() => {
- outcome = resolveZCodePlugins({
- logger,
- pluginStorageRoot: storageRoot,
- skipUserConfig: true,
- workingDirectory: dirname(storageRoot),
- });
- }).not.toThrow();
- // 降级终态之二:无可用旧缓存时该插件本次缺席(不写残缺缓存),只留 warning 级诊断,
- // 其余 defaultEnabled 插件与会话引导不受影响。
- expect(existsSync(join(storageRoot, BROWSER_CACHE_SUFFIX))).toBe(false);
- expect(
- outcome.plugins.find((plugin) => plugin.id === "browser-use@zcode-plugins-official"),
- ).toBeUndefined();
- const rootNotFound = outcome.diagnostics.filter(
- (diagnostic) => diagnostic.code === "plugin_root_not_found",
- );
- expect(rootNotFound).toEqual([
- expect.objectContaining({
- path: join(storageRoot, BROWSER_CACHE_SUFFIX),
- severity: "warning",
- }),
- ]);
- const documentSkills = outcome.plugins.find(
- (plugin) => plugin.id === "documents@zcode-plugins-official",
- );
- expect(documentSkills?.enabled).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- errorCode: "ZCODE_PLUGIN_SEED_INCOMPLETE",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("degrades a held seed lock to a usable previous version instead of failing startup", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.2.1");
- await rename(currentRoot, staleRoot);
- const lockRoot = `${currentRoot}.seed-lock`;
- await mkdir(lockRoot, { recursive: true });
- await writeFile(
- join(lockRoot, "owner.json"),
- JSON.stringify({ createdAt: new Date().toISOString(), pid: process.pid }),
- );
- const logger = createLogger();
- faults.seedLockHeldTargetSuffixes = [BROWSER_CACHE_SUFFIX];
- let roots: string[] = [];
- expect(() => {
- roots = resolveOfficialPluginRoots({ logger, storageRoot });
- }).not.toThrow();
- expect(roots).toContain(staleRoot);
- // 锁属于「另一个进程」(owner 存活),降级方绝不能顺手删掉它。
- expect(existsSync(lockRoot)).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "ZCODE_PLUGIN_SEED_LOCK_TIMEOUT",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- });
- it("shares one seed lock wait budget across all plugins", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- for (const cacheSuffix of [BROWSER_CACHE_SUFFIX, SKILL_CREATOR_CACHE_SUFFIX]) {
- const lockRoot = `${join(storageRoot, cacheSuffix)}.seed-lock`;
- await mkdir(lockRoot, { recursive: true });
- await writeFile(
- join(lockRoot, "owner.json"),
- JSON.stringify({ createdAt: new Date().toISOString(), pid: process.pid }),
- );
- }
- faults.seedLockHeldTargetSuffixes = [BROWSER_CACHE_SUFFIX, SKILL_CREATOR_CACHE_SUFFIX];
- expect(() => resolveOfficialPluginRoots({ storageRoot })).not.toThrow();
- // 两把被持有的锁必须共享同一截止时间:后一次调用拿到的剩余预算严格更小,
- // 而不是每个插件都重置成完整的 15s(否则成组遗留锁会让启动冻结 N×15s)。
- expect(faults.seedLockObservedTimeoutsMs).toHaveLength(2);
- const [firstBudgetMs, secondBudgetMs] = faults.seedLockObservedTimeoutsMs;
- expect(firstBudgetMs).toBeGreaterThan(0);
- expect(firstBudgetMs).toBeLessThanOrEqual(15_000);
- expect(secondBudgetMs).toBeLessThan(firstBudgetMs as number);
- });
- it("keeps an intact current cache discoverable when the seed lock times out", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- expect(resolveOfficialPluginRoots({ storageRoot })).toEqual([]);
- // 当前版本目录保持完整(不 rename 成旧版本),只把锁交给「另一个存活进程」。
- const currentRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const lockRoot = `${currentRoot}.seed-lock`;
- await mkdir(lockRoot, { recursive: true });
- await writeFile(
- join(lockRoot, "owner.json"),
- JSON.stringify({ createdAt: new Date().toISOString(), pid: process.pid }),
- );
- const logger = createLogger();
- faults.seedLockHeldTargetSuffixes = [BROWSER_CACHE_SUFFIX];
- const outcome = resolveZCodePlugins({
- logger,
- pluginStorageRoot: storageRoot,
- skipUserConfig: true,
- workingDirectory: dirname(storageRoot),
- });
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- errorCode: "ZCODE_PLUGIN_SEED_LOCK_TIMEOUT",
- operation: "seed_plugin",
- pluginId: "browser-use@zcode-plugins-official",
- }),
- );
- // 锁只保护 seed 写入,不保护读取:等锁超时不能让一份完整的当前缓存从 discovery 消失。
- // 该不变量依赖 bundled marketplace 分片在 seeding 循环之前写入且不按 seed 成败过滤;
- // 若未来把分片写入挪到循环之后,这条断言会失败。
- const browserPlugin = outcome.plugins.find(
- (plugin) => plugin.id === "browser-use@zcode-plugins-official",
- );
- expect(browserPlugin?.rootPath).toBe(currentRoot);
- expect(browserPlugin?.enabled).toBe(true);
- expect(
- outcome.diagnostics.filter((diagnostic) => diagnostic.code === "plugin_root_not_found"),
- ).toEqual([]);
- });
- it("does not mix an old zcode-cua cache with the current consumer contract", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const currentRoot = join(storageRoot, CUA_CACHE_SUFFIX);
- const staleRoot = join(dirname(currentRoot), "0.5.9");
- await rename(currentRoot, staleRoot);
- faults.promoteTargetSuffix = CUA_CACHE_SUFFIX;
- const roots = resolveOfficialPluginRoots({ storageRoot });
- expect(roots).not.toContain(staleRoot);
- });
- it("continues when another process moves the target after the existence check", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const staleMarker = `${await readFile(markerPath, "utf8")}\n`;
- await writeFile(markerPath, staleMarker.replace('"hash": "', '"hash": "stale-'));
- faults.moveTargetSuffix = BROWSER_CACHE_SUFFIX;
- expect(() => resolveOfficialPluginRoots({ storageRoot })).not.toThrow();
- expect(await readFile(markerPath, "utf8")).not.toContain('"hash": "stale-');
- faults.moveTargetSuffix = undefined;
- expect(() => resolveOfficialPluginRoots({ storageRoot })).not.toThrow();
- await expectNoSeedBackups(browserRoot);
- });
- it("converges when another process completes the current seed during promotion", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const markerPath = join(browserRoot, ".zcode-plugin-seed.json");
- const staleMarker = `${await readFile(markerPath, "utf8")}\n`;
- await writeFile(markerPath, staleMarker.replace('"hash": "', '"hash": "stale-'));
- faults.promoteCurrentTargetSuffix = BROWSER_CACHE_SUFFIX;
- expect(() => resolveOfficialPluginRoots({ storageRoot })).not.toThrow();
- expect(await readFile(markerPath, "utf8")).not.toContain('"hash": "stale-');
- await expectNoSeedBackups(browserRoot);
- });
- it("rebuilds after a legacy backup is left by an interrupted replacement", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const backupRoot = `${browserRoot}.backup`;
- await rename(browserRoot, backupRoot);
- expect(() => resolveOfficialPluginRoots({ storageRoot })).not.toThrow();
- expect(existsSync(browserRoot)).toBe(true);
- await expectNoSeedBackups(browserRoot);
- });
- it("does not restore a fixed backup that may belong to an active replacement", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const browserRoot = join(storageRoot, BROWSER_CACHE_SUFFIX);
- const backupRoot = `${browserRoot}.backup`;
- await rename(browserRoot, backupRoot);
- faults.recoverBackupTargetSuffix = BROWSER_CACHE_SUFFIX;
- expect(() => resolveOfficialPluginRoots({ storageRoot })).not.toThrow();
- expect(existsSync(browserRoot)).toBe(true);
- await expectNoSeedBackups(browserRoot);
- });
- it("keeps a current target usable when only runtime manifest refresh is locked", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const logger = createLogger();
- process.argv[1] = join(storageRoot, "different-zcode.cjs");
- faults.runtimeManifestTargetSuffix = ANDROID_MANIFEST_SUFFIX;
- expect(() => resolveOfficialPluginRoots({ logger, storageRoot })).not.toThrow();
- expect(existsSync(join(storageRoot, ANDROID_CACHE_SUFFIX))).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- operation: "seed_plugin",
- pluginId: "android-emulator@zcode-plugins-official",
- }),
- );
- });
- it("keeps a concurrent winner when runtime manifest temporary file disappears", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const logger = createLogger();
- process.argv[1] = join(storageRoot, "different-zcode.cjs");
- faults.runtimeManifestNotFoundTargetSuffix = ANDROID_MANIFEST_SUFFIX;
- expect(() => resolveOfficialPluginRoots({ logger, storageRoot })).not.toThrow();
- expect(existsSync(join(storageRoot, ANDROID_CACHE_SUFFIX))).toBe(true);
- expect(logger.warn).toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({
- degraded: true,
- errorCode: "ENOENT",
- operation: "seed_plugin",
- pluginId: "android-emulator@zcode-plugins-official",
- }),
- );
- });
- it("preserves suppressed plugin catalog/cache without attempting a Windows-locked deletion", async () => {
- const storageRoot = await createStorageRoot(cleanups);
- resolveOfficialPluginRoots({ storageRoot });
- const logger = createLogger();
- faults.removeTargetSuffix = SKILL_CREATOR_CACHE_SUFFIX;
- expect(() =>
- resolveOfficialPluginRoots({
- logger,
- storageRoot,
- suppressedBuiltins: new Set(["skill-creator@zcode-plugins-official"]),
- }),
- ).not.toThrow();
- expect(existsSync(join(storageRoot, SKILL_CREATOR_CACHE_SUFFIX))).toBe(true);
- const marketplace = JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
- "utf8",
- ),
- ) as { plugins: Array<{ name: string }> };
- // 现行契约:suppressedBuiltins 只控制 runtime discovery。catalog/cache 是恢复入口
- // 的不可变资产,不应触发删除;注入的 EPERM 因而不会被命中,也不产生降级告警。
- expect(marketplace.plugins.some((plugin) => plugin.name === "skill-creator")).toBe(true);
- expect(logger.warn).not.toHaveBeenCalledWith(
- "Official plugin cache operation degraded",
- expect.objectContaining({ operation: "remove_suppressed_plugin" }),
- );
- });
- });
- async function createStorageRoot(cleanups: string[]): Promise<string> {
- const root = await mkdtemp(join(tmpdir(), "zcode-plugin-cache-resilience-"));
- cleanups.push(root);
- return join(root, "plugins");
- }
- async function expectNoSeedBackups(targetRoot: string): Promise<void> {
- const targetName = basename(targetRoot);
- const siblings = await readdir(dirname(targetRoot));
- expect(siblings.filter((entry) => entry.startsWith(`${targetName}.backup`))).toEqual([]);
- }
- function createLogger(): Logger & { warn: ReturnType<typeof vi.fn> } {
- const logger = {
- child: () => logger,
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn: vi.fn(),
- };
- return logger;
- }
- function exhaustedErrno(code: string): NodeJS.ErrnoException {
- return Object.assign(new Error(code), {
- code,
- zcodeOfficialPluginCacheAttempts: 4,
- });
- }
- function errno(code: string): NodeJS.ErrnoException {
- return Object.assign(new Error(code), { code });
- }
|