Files
orca/src/main/plugins/plugin-list-projection.test.ts
T

124 lines
4.0 KiB
TypeScript

import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { emptyPluginLockfile } from '../../shared/plugins/plugin-install-lockfile'
import { pluginManifestSchema } from '../../shared/plugins/plugin-manifest'
import type { InvalidDiscoveredPlugin, ValidDiscoveredPlugin } from './plugin-discovery'
import { buildPluginList } from './plugin-list-projection'
import type { PluginService } from './plugin-service'
const manifest = pluginManifestSchema.parse({
manifestVersion: 1,
id: 'demo',
publisher: 'orca-samples',
name: 'Demo',
version: '1.0.0',
engines: { orca: '>=1.0.0' },
pluginApi: 1,
contributes: { panels: [], commands: [], events: [] },
capabilities: [{ kind: 'workspace:read' }]
})
function serviceWith(
discovered: ValidDiscoveredPlugin,
options: {
activation?: ReturnType<PluginService['activationState']>
worker?: ReturnType<PluginService['workerState']>
} = {}
): PluginService {
return {
options: {
getPluginConsents: () => ({}),
getDisabledPlugins: () => []
},
getDiscovered: () => [discovered],
activationState: () => options.activation ?? 'pending',
workerState: () => options.worker ?? { state: 'inactive', restarts: 0 },
activationError: () => null
} as unknown as PluginService
}
describe('buildPluginList consent identity', () => {
it('projects the exact current fingerprint for an optimistic consent write', () => {
const plugin: ValidDiscoveredPlugin = {
pluginKey: 'orca-samples.demo',
rootDir: join(tmpdir(), 'plugins', 'demo'),
manifest,
consentFingerprint: 'sha256-current',
contentHash: null,
isDev: true
}
expect(buildPluginList(serviceWith(plugin), emptyPluginLockfile())[0]).toMatchObject({
pluginKey: plugin.pluginKey,
consentFingerprint: 'sha256-current',
status: 'pending'
})
})
it('projects supervised backoff as restarting instead of running', () => {
const plugin: ValidDiscoveredPlugin = {
pluginKey: 'orca-samples.demo',
rootDir: join(tmpdir(), 'plugins', 'demo'),
manifest,
consentFingerprint: 'sha256-current',
contentHash: null,
isDev: true
}
expect(
buildPluginList(
serviceWith(plugin, {
activation: 'approved',
worker: { state: 'restarting', restarts: 2 }
}),
emptyPluginLockfile()
)[0]
).toMatchObject({ status: 'restarting', restarts: 2 })
})
it('does not attribute a shadowing dev plugin to the installed source', () => {
const plugin: ValidDiscoveredPlugin = {
pluginKey: 'orca-samples.demo',
rootDir: join(tmpdir(), 'development', 'demo'),
manifest,
consentFingerprint: 'sha256-current',
contentHash: null,
isDev: true
}
const lock = {
version: 1 as const,
plugins: {
[plugin.pluginKey]: {
pluginKey: plugin.pluginKey,
version: '1.0.0',
source: { kind: 'git' as const, url: 'https://example.com/demo.git', ref: 'v1' },
resolvedCommit: 'a'.repeat(40),
contentHash: 'b'.repeat(64),
consentFingerprint: 'sha256-installed',
installedAt: 1
}
}
}
expect(buildPluginList(serviceWith(plugin), lock)[0]).not.toHaveProperty('source')
})
it('does not expose an invalid development plugin absolute path as identity', () => {
const invalid: InvalidDiscoveredPlugin = {
rootDir: join(tmpdir(), 'private', 'secret-plugin-path'),
error: 'missing orca-plugin.json',
isDev: true
}
const service = {
options: { getPluginConsents: () => ({}), getDisabledPlugins: () => [] },
getDiscovered: () => [invalid]
} as unknown as PluginService
const projected = buildPluginList(service, emptyPluginLockfile())[0]!
expect(projected.pluginKey).toBe('invalid-development-plugin-1')
expect(projected.name).toBe('invalid-development-plugin-1')
expect(JSON.stringify(projected)).not.toContain(invalid.rootDir)
})
})