forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginModulePack.test.ts
More file actions
214 lines (194 loc) · 7.26 KB
/
Copy pathpluginModulePack.test.ts
File metadata and controls
214 lines (194 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
import {
activatePluginModulePack,
activateSandboxedPluginModulePack,
deactivatePluginModulePack,
listPluginRegisteredModuleIds,
resetPluginModulePacks,
type SandboxedModulePack,
} from '@core/plugins/modulePackLoader'
import {
pluginModuleToHostModule,
validatePluginModuleId,
PluginModuleValidationError,
} from '@core/plugins/moduleAdapter'
import { registry } from '@core/module-engine'
import type { PluginManifest } from '@core/plugin-sdk'
const sampleManifest: PluginManifest = {
id: 'acme.canvas',
name: 'Canvas Pack',
version: '1.0.0',
apiVersion: 1,
permissions: ['modules.register'],
grantedPermissions: ['modules.register'],
resources: [],
adminPages: [],
}
const counterDefinition = {
id: 'acme.canvas.counter',
name: 'Counter',
category: 'Acme Pack',
version: '1.0.0',
defaults: { count: 0 },
schema: {
count: { type: 'number' as const, label: 'Count', min: 0 },
},
render: (props: Record<string, unknown>) => ({
html: `<div class="counter">${String(props.count ?? 0)}</div>`,
}),
}
beforeEach(() => {
resetPluginModulePacks()
})
afterEach(() => {
resetPluginModulePacks()
})
function makeStubPack(pluginId: string, onDispose: () => void): SandboxedModulePack {
return {
pluginId,
modules: [
{
id: 'acme.canvas.counter',
name: 'Counter',
category: 'Acme Pack',
version: '1.0.0',
defaults: {},
schema: {},
hasPreview: false,
},
],
render: () => ({ html: '' }),
preview: () => ({ html: '' }),
dispose: onDispose,
}
}
// ISS-033: server-side QuickJS module-pack contexts must be disposed on every
// lifecycle teardown, otherwise each activate/upgrade/restart cycle leaks a
// native context for the host-process lifetime.
describe('sandboxed module-pack VM disposal', () => {
it('disposes the VM on deactivate', () => {
let disposed = 0
activateSandboxedPluginModulePack(sampleManifest, makeStubPack('acme.canvas', () => { disposed++ }))
deactivatePluginModulePack('acme.canvas')
expect(disposed).toBe(1)
})
it('disposes the prior VM when the pack is re-activated', () => {
let disposedFirst = 0
activateSandboxedPluginModulePack(sampleManifest, makeStubPack('acme.canvas', () => { disposedFirst++ }))
activateSandboxedPluginModulePack(sampleManifest, makeStubPack('acme.canvas', () => {}))
expect(disposedFirst).toBe(1)
})
it('disposes every VM on reset', () => {
let disposed = 0
activateSandboxedPluginModulePack(sampleManifest, makeStubPack('acme.canvas', () => { disposed++ }))
resetPluginModulePacks()
expect(disposed).toBe(1)
})
})
describe('pluginModuleToHostModule', () => {
it('produces a host module definition that delegates render to the plugin', () => {
const hostModule = pluginModuleToHostModule('acme.canvas', counterDefinition, () => () => null, [])
expect(hostModule.id).toBe('acme.canvas.counter')
expect(hostModule.trusted).toBe(false)
expect(hostModule.render({ count: 5 }, [])).toEqual({
html: '<div class="counter">5</div>',
})
})
it('rejects module ids that do not start with the plugin id', () => {
expect(() =>
pluginModuleToHostModule('acme.canvas', { ...counterDefinition, id: 'evil.canvas.counter' }, () => () => null, []),
).toThrow(PluginModuleValidationError)
expect(() =>
pluginModuleToHostModule('acme.canvas', { ...counterDefinition, id: 'base.text' }, () => () => null, []),
).toThrow(PluginModuleValidationError)
})
it('accepts the bare plugin-id as namespace and a kebab-case name segment', () => {
expect(() =>
validatePluginModuleId('acme.canvas', 'acme.canvas.fancy-card'),
).not.toThrow()
})
it('rejects modules without render', () => {
expect(() =>
pluginModuleToHostModule('acme.canvas', { ...counterDefinition, render: undefined as unknown as typeof counterDefinition.render }, () => () => null, []),
).toThrow(/must export a render/)
})
it('drops render() js without the frontend.assets grant and warns once per module', () => {
const jsDefinition = {
...counterDefinition,
id: 'acme.canvas.jsy',
render: () => ({ html: '<div></div>', js: '(function(){})();' }),
}
const warnings: string[] = []
const originalWarn = console.warn
console.warn = (...args: unknown[]) => { warnings.push(args.map(String).join(' ')) }
try {
const hostModule = pluginModuleToHostModule('acme.canvas', jsDefinition, () => () => null, [])
expect(hostModule.render({}, []).js).toBeUndefined()
expect(hostModule.render({}, []).js).toBeUndefined()
expect(warnings.filter((w) => w.includes('frontend.assets')).length).toBe(1)
expect(warnings[0]).toContain('[plugin-module:acme.canvas.jsy]')
} finally {
console.warn = originalWarn
}
})
it('passes render() js through with the frontend.assets grant', () => {
const jsDefinition = {
...counterDefinition,
id: 'acme.canvas.jsy',
render: () => ({ html: '<div></div>', js: '(function(){})();' }),
}
const hostModule = pluginModuleToHostModule('acme.canvas', jsDefinition, () => () => null, ['frontend.assets'])
expect(hostModule.render({}, []).js).toBe('(function(){})();')
})
})
describe('activatePluginModulePack', () => {
it('registers each module from the pack and tracks them by plugin id', () => {
activatePluginModulePack(
sampleManifest,
{ default: [counterDefinition] },
)
expect(listPluginRegisteredModuleIds('acme.canvas')).toEqual(['acme.canvas.counter'])
const registered = registry.get('acme.canvas.counter')
expect(registered).toBeDefined()
expect(registered?.render({}, []).html).toBe('<div class="counter">0</div>')
})
it('replaces previous registrations on re-activation', () => {
activatePluginModulePack(sampleManifest, { default: [counterDefinition] })
activatePluginModulePack(sampleManifest, {
default: [
{
...counterDefinition,
id: 'acme.canvas.replaced',
},
],
})
expect(listPluginRegisteredModuleIds('acme.canvas')).toEqual(['acme.canvas.replaced'])
expect(registry.get('acme.canvas.counter')).toBeUndefined()
expect(registry.get('acme.canvas.replaced')).toBeDefined()
})
it('deactivates a pack and unregisters every module from the canvas registry', () => {
activatePluginModulePack(sampleManifest, { default: [counterDefinition] })
deactivatePluginModulePack('acme.canvas')
expect(registry.get('acme.canvas.counter')).toBeUndefined()
expect(listPluginRegisteredModuleIds('acme.canvas')).toEqual([])
})
it('refuses to activate when modules.register is not granted', () => {
expect(() =>
activatePluginModulePack(
{ ...sampleManifest, grantedPermissions: [] },
{ default: [counterDefinition] },
),
).toThrow(/requires permission "modules.register"/)
})
it('accepts a function entrypoint that returns module definitions', () => {
activatePluginModulePack(sampleManifest, {
default: ({ pluginId }) => [
{
...counterDefinition,
id: `${pluginId}.counter`,
},
],
})
expect(listPluginRegisteredModuleIds('acme.canvas')).toEqual(['acme.canvas.counter'])
})
})