You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.7 KiB
47 lines
1.7 KiB
import { describe, expect, it, vi } from 'vitest'
|
|
import { Scope } from '@template/core'
|
|
import {
|
|
optionalService,
|
|
requireCapability,
|
|
resolveCapabilities
|
|
} from '../../src/main/capabilities'
|
|
|
|
describe('optional service construction', () => {
|
|
it('validates deployment config and only permits reducing launch capabilities', () => {
|
|
const config = { files: false, storage: true, credentials: true, updates: true }
|
|
expect(resolveCapabilities(config, ' storage,updates ')).toEqual({
|
|
files: false,
|
|
storage: false,
|
|
credentials: true,
|
|
updates: false
|
|
})
|
|
expect(config.storage).toBe(true)
|
|
expect(() => resolveCapabilities(config, '__proto__')).toThrow()
|
|
expect(() => resolveCapabilities({ ...config, storage: 'yes' })).toThrow()
|
|
})
|
|
it('does not construct disabled services or register their resources', async () => {
|
|
const scope = new Scope()
|
|
const create = vi.fn(() => ({ stop: vi.fn() }))
|
|
const dispose = vi.fn()
|
|
expect(optionalService(false, create, scope, dispose)).toBeUndefined()
|
|
await scope.dispose()
|
|
expect(create).not.toHaveBeenCalled()
|
|
expect(dispose).not.toHaveBeenCalled()
|
|
expect(() => requireCapability(undefined, 'files')).toThrow('files capability is disabled')
|
|
})
|
|
it('constructs once and cleans enabled resources during shutdown', async () => {
|
|
const scope = new Scope()
|
|
const service = { stop: vi.fn() }
|
|
expect(
|
|
optionalService(
|
|
true,
|
|
() => service,
|
|
scope,
|
|
(value) => value.stop()
|
|
)
|
|
).toBe(service)
|
|
expect(requireCapability(service, 'files')).toBe(service)
|
|
await scope.dispose()
|
|
expect(service.stop).toHaveBeenCalledOnce()
|
|
})
|
|
})
|
|
|