/** * @module tests/unit/plugin-activation-loudness * @description The plugin-activation swallow closes. Two laws: * (1) THE NOT-INSTALLED FREE PASS IS EXACT — a resolution failure earns the * silent skip ONLY when it names the probed package itself, terminated * where the name ends. A missing platform-binary SIBLING package * ("-linux-x64-gnu" — what a deploy replacing node_modules * mid-restart leaves), an inner file path, or a dependency failure is a * BROKEN install and must fail loud. A production storm ran 90s of * throttled WASM behind this exact prefix-match hole. * (2) A GRACEFUL DECLINE IS NARRATED ON THE ALWAYS-ON CHANNEL — activate() * returning false warns via prodLog, which `silent: true` cannot patch * away; a declined accelerator is never an invisible degrade. */ import { describe, it, expect, vi, afterEach } from 'vitest' import { Brainy } from '../../src/brainy.js' import { prodLog } from '../../src/utils/logger.js' const isNotInstalled = (error: unknown, pkg: string): boolean => (Brainy as unknown as { isPackageNotInstalledError(e: unknown, p: string): boolean }).isPackageNotInstalledError(error, pkg) const resolutionError = (message: string): Error => { const e = new Error(message) as Error & { code?: string } e.code = 'ERR_MODULE_NOT_FOUND' return e } describe('the not-installed free pass is exact', () => { const PKG = '@soulcraft/cor' it('the package itself, quoted or bare → not-installed (the one free path)', () => { expect(isNotInstalled(resolutionError(`Cannot find package '${PKG}' imported from /app/x.js`), PKG)).toBe(true) expect(isNotInstalled(resolutionError(`Cannot find module ${PKG}`), PKG)).toBe(true) }) it('a missing platform-binary SIBLING package is a broken install, never not-installed', () => { expect(isNotInstalled(resolutionError(`Cannot find package '${PKG}-linux-x64-gnu' imported from /app`), PKG)).toBe(false) expect(isNotInstalled(resolutionError(`Failed to resolve ${PKG}-darwin-arm64`), PKG)).toBe(false) }) it('an inner file path or a non-resolution error is never not-installed', () => { expect(isNotInstalled(resolutionError(`Cannot find module '/app/node_modules/${PKG}/native/b.node'`), PKG)).toBe(false) expect(isNotInstalled(new Error(`dlopen failed: wrong ELF class in ${PKG}`), PKG)).toBe(false) }) }) describe('a graceful decline is narrated on the always-on channel', () => { afterEach(() => vi.restoreAllMocks()) it('activate() → false warns via prodLog even under silent: true', async () => { process.env.BRAINY_DETERMINISTIC_EMBEDDINGS = 'true' const warn = vi.spyOn(prodLog, 'warn') const brain: any = new Brainy({ requireSubtype: false, storage: { type: 'memory' }, silent: true, dimensions: 384 }) brain.use({ name: 'declining-accelerator', activate: async () => false }) await brain.init() try { expect( warn.mock.calls.some((c) => String(c[0]).includes('"declining-accelerator" declined activation')) ).toBe(true) } finally { await brain.close().catch(() => {}) } }) })