import { describe, expect, it, vi } from 'vitest' import { join } from 'node:path' import { pathToFileURL } from 'node:url' import { createPluginWorkerRuntime } from './plugin-host-runtime' describe('plugin worker shutdown', () => { it('normalizes either manifest separator before importing the worker', async () => { const importModule = vi.fn(async () => ({ default: vi.fn() })) const runtime = createPluginWorkerRuntime({ send: vi.fn(), importModule }) await runtime.handleMessage({ type: 'init', pluginId: 'orca-samples.demo', pluginRoot: join('plugin-root'), mainEntry: 'nested\\worker.js', grantedCapabilities: [] }) expect(importModule).toHaveBeenCalledWith( pathToFileURL(join('plugin-root', 'nested', 'worker.js')).href ) }) it('awaits an optional deactivate export before exiting', async () => { let finishDeactivate!: () => void const deactivate = vi.fn( () => new Promise((resolve) => { finishDeactivate = resolve }) ) const send = vi.fn() const exit = vi.fn() const runtime = createPluginWorkerRuntime({ send, exit, importModule: async () => ({ default: vi.fn(), deactivate }) }) await runtime.handleMessage({ type: 'init', pluginId: 'orca-samples.demo', pluginRoot: '/plugin', mainEntry: 'worker.js', grantedCapabilities: [] }) const shutdown = runtime.handleMessage({ type: 'shutdown' }) await Promise.resolve() expect(deactivate).toHaveBeenCalledOnce() expect(exit).not.toHaveBeenCalled() finishDeactivate() await shutdown expect(exit).toHaveBeenCalledWith(0) }) it('exits immediately when the plugin has no deactivate export', async () => { const exit = vi.fn() const runtime = createPluginWorkerRuntime({ send: vi.fn(), exit, importModule: async () => ({ default: vi.fn() }) }) await runtime.handleMessage({ type: 'init', pluginId: 'orca-samples.demo', pluginRoot: '/plugin', mainEntry: 'worker.js', grantedCapabilities: [] }) await runtime.handleMessage({ type: 'shutdown' }) expect(exit).toHaveBeenCalledWith(0) }) })