Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
import { describe, expect, it, vi } from 'vitest'
import type { ExtensionContext, ExtensionModule } from '@janhq/extension-api'
import {
EngineManager,
ServiceKey,
type AIEngine,
type InternalExtensionContext,
} from '@janhq/core'
import { useGeneralSetting } from '@/hooks/useGeneralSetting'
import { ExtensionHost, toastTitle } from '../host'
import { listExtensionTools } from '../tools'
import { loopbackFetch } from '@/lib/loopback-fetch'
import { builtin, manifest } from './fixtures'
import { memoryAppSettings, memorySecrets } from './memory'
const newHost = () =>
new ExtensionHost({
appSettings: memoryAppSettings(),
secrets: memorySecrets(),
notify: vi.fn(),
})
const recorder = (order: string[], id: string): ExtensionModule => ({
activate: () => {
order.push(id)
},
})
const states = (host: ExtensionHost) =>
Object.fromEntries(host.list().map((e) => [e.id, e.state]))
describe('ExtensionHost', () => {
it('accepts an empty built-in list', async () => {
const host = newHost()
await host.activateBuiltins([])
expect(host.list()).toEqual([])
await host.deactivateAll()
})
it('activates dependencies before their dependents', async () => {
const order: string[] = []
const host = newHost()
await host.activateBuiltins([
builtin(manifest('jan.rag'), recorder(order, 'jan.rag'), {
dependsOn: ['jan.vector-db'],
}),
builtin(manifest('jan.vector-db'), recorder(order, 'jan.vector-db')),
builtin(manifest('jan.other'), recorder(order, 'jan.other')),
])
expect(order).toEqual(['jan.vector-db', 'jan.rag', 'jan.other'])
})
it('isolates a failing activate and fails its dependents', async () => {
const error = vi.spyOn(console, 'error').mockImplementation(() => {})
const order: string[] = []
const host = newHost()
await expect(
host.activateBuiltins([
builtin(manifest('jan.broken'), {
activate: () => {
throw new Error('boom')
},
}),
builtin(
manifest('jan.needs-broken'),
recorder(order, 'jan.needs-broken'),
{ dependsOn: ['jan.broken'] }
),
builtin(
manifest('jan.missing-dep'),
recorder(order, 'jan.missing-dep'),
{ dependsOn: ['jan.nope'] }
),
builtin(manifest('jan.fine'), recorder(order, 'jan.fine')),
])
).resolves.toBeUndefined()
expect(order).toEqual(['jan.fine'])
expect(states(host)).toEqual({
'jan.broken': 'failed',
'jan.needs-broken': 'failed',
'jan.missing-dep': 'failed',
'jan.fine': 'active',
})
expect(host.list().find((e) => e.id === 'jan.broken')?.error).toBe('boom')
expect(error).toHaveBeenCalled()
error.mockRestore()
})
it('fails a dependency cycle instead of hanging', async () => {
const error = vi.spyOn(console, 'error').mockImplementation(() => {})
const host = newHost()
await host.activateBuiltins([
builtin(
manifest('jan.a'),
{ activate: () => {} },
{ dependsOn: ['jan.b'] }
),
builtin(
manifest('jan.b'),
{ activate: () => {} },
{ dependsOn: ['jan.a'] }
),
])
expect(states(host)).toEqual({ 'jan.a': 'failed', 'jan.b': 'failed' })
error.mockRestore()
})
it('fails a built-in whose apiVersion this host does not satisfy, or whose module fails to load', async () => {
const error = vi.spyOn(console, 'error').mockImplementation(() => {})
const activate = vi.fn()
const host = newHost()
await host.activateBuiltin(
builtin(manifest('jan.future', { apiVersion: '^9.0.0' }), { activate })
)
await host.activateBuiltin({
manifest: manifest('jan.unloadable'),
load: () => Promise.reject(new Error('chunk failed')),
})
expect(activate).not.toHaveBeenCalled()
expect(states(host)).toEqual({
'jan.future': 'failed',
'jan.unloadable': 'failed',
})
error.mockRestore()
})
it('gives built-ins an internal context that reaches the service registry', async () => {
const key = new ServiceKey<{ ping(): string }>('ping')
const host = newHost()
await host.activateBuiltin(
builtin(manifest('jan.pinger'), {
activate: (ctx: ExtensionContext) => {
const internal = ctx as InternalExtensionContext
ctx.subscriptions.add(
internal.internal.services.provide(key, { ping: () => 'pong' })
)
},
})
)
expect(host.services.get(key)?.ping()).toBe('pong')
await host.deactivateAll()
expect(host.services.get(key)).toBeUndefined()
})
it('registers built-in engines with the app engine manager until deactivation', async () => {
const manager = new EngineManager()
window.core = { ...window.core, engineManager: manager } as typeof window.core
const engine = { provider: 'local' } as AIEngine
const host = newHost()
await host.activateBuiltin(
builtin(manifest('jan.engine'), {
activate: (ctx: ExtensionContext) => {
const { internal } = ctx as InternalExtensionContext
ctx.subscriptions.add(internal.engines.register(engine))
},
})
)
expect(manager.get('local')).toBe(engine)
await host.deactivateAll()
expect(manager.get('local')).toBeUndefined()
})
it('lets built-ins read host keyring secrets through the internal context', async () => {
const secrets = memorySecrets()
secrets.data.set('huggingface', 'hf_abc')
const host = new ExtensionHost({
appSettings: memoryAppSettings(),
secrets,
notify: vi.fn(),
})
const seen: (string | null)[] = []
await host.activateBuiltin(
builtin(manifest('jan.reader'), {
activate: async (ctx: ExtensionContext) => {
const { internal } = ctx as InternalExtensionContext
seen.push(await internal.secrets.get('huggingface'))
seen.push(await internal.secrets.get('absent'))
},
})
)
expect(seen).toEqual(['hf_abc', null])
})
it('gives built-ins the host loopback fetch, which bypasses every proxy', async () => {
let received: InternalExtensionContext['internal']['loopbackFetch'] | undefined
const host = newHost()
await host.activateBuiltin(
builtin(manifest('jan.engine'), {
activate: (ctx: ExtensionContext) => {
received = (ctx as InternalExtensionContext).internal.loopbackFetch
},
})
)
expect(received).toBe(loopbackFetch)
})
it('grants a built-in exactly the permissions its manifest requests', async () => {
const host = newHost()
await host.activateBuiltin(
builtin(manifest('jan.notifier', { permissions: ['ui:notify'] }), {
activate: () => {},
})
)
expect(host.dispatcher.has('jan.notifier', 'ui:notify')).toBe(true)
expect(host.dispatcher.has('jan.notifier', 'tools')).toBe(false)
})
it('passes model capabilities through listModels and rejects unknown or duplicate ones', async () => {
const host = newHost()
const models = vi.fn()
await host.activateBuiltin(
builtin(
manifest('acme.llm', {
permissions: ['providers', 'network:127.0.0.1'],
contributes: {
providers: [
{ id: 'local', displayName: 'Local', baseUrl: 'http://127.0.0.1:1/v1' },
],
},
}),
{
activate: (ctx) => {
ctx.providers.registerProvider('local', { listModels: models })
},
}
)
)
models.mockResolvedValueOnce([{ id: 'm1', capabilities: ['tools'] }])
await expect(host.listProviderModels('acme.llm', 'local')).resolves.toEqual([
{ id: 'm1', capabilities: ['tools'] },
])
models.mockResolvedValueOnce([{ id: 'm1', capabilities: ['teleport'] }])
await expect(
host.listProviderModels('acme.llm', 'local')
).rejects.toMatchObject({ code: 'E_INVALID_ARGS' })
models.mockResolvedValueOnce([{ id: 'm1', capabilities: ['tools', 'tools'] }])
await expect(
host.listProviderModels('acme.llm', 'local')
).rejects.toMatchObject({ code: 'E_INVALID_ARGS' })
await host.deactivateAll()
})
it('deactivates in reverse order, disposing subscriptions and contributions', async () => {
const order: string[] = []
const disposed = vi.fn()
const host = newHost()
const withCommand = manifest('jan.second', {
contributes: { commands: [{ id: 'go', title: 'Go' }] },
})
await host.activateBuiltins([
builtin(manifest('jan.first'), {
activate: () => {},
deactivate: () => void order.push('jan.first'),
}),
builtin(withCommand, {
activate: (ctx) => {
ctx.subscriptions.add({ dispose: disposed })
},
deactivate: () => void order.push('jan.second'),
}),
])
expect(host.contributions.getState().commands['jan.second']).toBeDefined()
await host.deactivateAll()
expect(order).toEqual(['jan.second', 'jan.first'])
expect(disposed).toHaveBeenCalledTimes(1)
expect(host.contributions.getState().commands['jan.second']).toBeUndefined()
expect(states(host)).toEqual({
'jan.first': 'inactive',
'jan.second': 'inactive',
})
})
it('keeps tearing down when a deactivate throws', async () => {
const error = vi.spyOn(console, 'error').mockImplementation(() => {})
const disposed = vi.fn()
const host = newHost()
await host.activateBuiltin(
builtin(manifest('jan.bad-exit'), {
activate: (ctx) => void ctx.subscriptions.add({ dispose: disposed }),
deactivate: () => {
throw new Error('nope')
},
})
)
await host.deactivateAll()
expect(disposed).toHaveBeenCalledTimes(1)
expect(states(host)).toEqual({ 'jan.bad-exit': 'inactive' })
error.mockRestore()
})
it('gives a rebound same-schema tool a new generation and refuses a call approved for the old one', async () => {
const replies = ['old', 'new']
const host = newHost()
const def = builtin(
manifest('jan.regen', {
permissions: ['tools'],
contributes: {
tools: [
{ name: 'echo', description: 'Echo', inputSchema: { type: 'object' } },
],
},
}),
{
activate: (ctx) => {
const reply = replies.shift()!
ctx.subscriptions.add(
ctx.tools.registerTool('echo', async () => ({
content: [{ type: 'text', text: reply }],
}))
)
},
}
)
const listed = () => listExtensionTools(host.contributions.getState())
await host.activateBuiltins([def])
const [before] = listed()
expect(await host.callTool('jan.regen', 'echo', {}, { generation: before.generation })).toEqual({
content: [{ type: 'text', text: 'old' }],
})
await host.deactivateAll()
expect(listed()).toEqual([])
await host.activateBuiltins([def])
const [after] = listed()
// Identical declaration, different binding: only `generation` tells them apart.
expect({ ...after, generation: 0 }).toEqual({ ...before, generation: 0 })
expect(after.generation).not.toBe(before.generation)
expect(JSON.stringify(after)).not.toBe(JSON.stringify(before))
const stale = await host.callTool('jan.regen', 'echo', {}, { generation: before.generation })
expect(stale.error).toMatch(/changed since it was offered/)
expect(
await host.callTool('jan.regen', 'echo', {}, { generation: after.generation })
).toEqual({ content: [{ type: 'text', text: 'new' }] })
expect(replies).toEqual([])
})
it('ignores a second activation of an active built-in and reactivates after deactivateAll', async () => {
const activate = vi.fn()
const host = newHost()
const def = builtin(manifest('jan.once'), { activate })
// Not awaited in between: React StrictMode mounts, unmounts and mounts again.
const runs = [
host.activateBuiltins([def]),
host.activateBuiltins([def]),
host.deactivateAll(),
host.activateBuiltins([def]),
]
await Promise.all(runs)
expect(activate).toHaveBeenCalledTimes(2)
expect(states(host)).toEqual({ 'jan.once': 'active' })
})
it('notifies listeners when the list changes', async () => {
const host = newHost()
const listener = vi.fn()
host.onDidChange(listener)
await host.activateBuiltin(
builtin(manifest('jan.x'), { activate: () => {} })
)
expect(listener).toHaveBeenCalled()
})
it("labels an extension's toast with a fixed 'Extension:' prefix", async () => {
const notify = vi.fn()
const host = new ExtensionHost({
appSettings: memoryAppSettings(),
secrets: memorySecrets(),
notify,
})
await host.activateBuiltin(
builtin(
manifest('jan.notifier', { displayName: 'Jan', permissions: ['ui:notify'] }),
{
activate: (ctx) =>
ctx.ui.notify({ level: 'warning', message: 'Update now' }),
}
)
)
expect(notify).toHaveBeenCalledWith({
extensionName: 'Jan',
level: 'warning',
message: 'Update now',
})
expect(toastTitle('Jan')).toBe('Extension: Jan')
})
it('names the extension of a notification in the app language', async () => {
const notify = vi.fn()
const host = new ExtensionHost({
appSettings: memoryAppSettings(),
secrets: memorySecrets(),
notify,
})
useGeneralSetting.setState({ currentLanguage: 'ja' })
await host.activateBuiltin(
builtin(
manifest('jan.notifier', {
displayName: '%name%',
permissions: ['ui:notify'],
l10n: { en: { name: 'Notifier' }, ja: { name: 'Tsuchi' } },
}),
{
activate: (ctx) =>
ctx.ui.notify({ level: 'info', message: 'Hi' }),
}
)
)
useGeneralSetting.setState({ currentLanguage: 'en' })
expect(notify).toHaveBeenCalledWith({
extensionName: 'Tsuchi',
level: 'info',
message: 'Hi',
})
})
})