返回 CodeWhale
composition-membership-host.test.mjs
根目录 / crates / engine / extension-host / test / composition-membership-host.test.mjs
1 // Acceptance candidates only. Real host/fake core, not Rust admission or grants proof.
2 import { test } from 'node:test'
3 import assert from 'node:assert/strict'
4 import { createHash } from 'node:crypto'
5 import { mkdtempSync, writeFileSync, rmSync } from 'node:fs'
6 import { tmpdir } from 'node:os'
7 import { join } from 'node:path'
8 import { activate, startHost } from './harness.mjs'
9
10 const STATIC_SCOPED_SOURCE =
11 "export const inject=['tools','prompt'];export function apply(ctx,config){const value=String(config.echo);ctx.tools.register({name:'scoped_echo',description:'Entry echo',parameters:{type:'object',properties:{}},execute(){return value}});ctx.prompt.registerSection({id:'note',text:value})}"
12 const STATIC_KEPT_SOURCE =
13 "export const inject=['tools','prompt'];export function apply(ctx){ctx.tools.register({name:'kept_echo',description:'kept',parameters:{type:'object',properties:{}},execute(){return 'kept'}})}"
14 const STATIC_FAIL_SOURCE =
15 "export const inject=['tools','prompt'];export function apply(ctx){ctx.prompt.registerSection({id:'partial',text:'must retire'});throw new Error('intentional scoped failure')}"
16
17 function entries(t) {
18 const root=mkdtempSync(join(tmpdir(),'cw-entry-scopes-'))
19 t.after(()=>rmSync(root,{recursive:true,force:true}))
20 const make=(name,source=STATIC_SCOPED_SOURCE)=>{
21 const path=join(root,`${name}.mjs`)
22 writeFileSync(path,source)
23 return {path,sha256:createHash('sha256').update(source).digest('hex')}
24 }
25 return {root,make}
26 }
27
28 test('one owner can mount two scoped entries with equal names and dispose only one',async t=>{
29 const host=await startHost();t.after(()=>host.stop())
30 const {root,make}=entries(t)
31 const a=make('a'),b=make('b')
32 const first=await activate(host,'scoped-entries',a.path,{scope:a,data_dir:root,config:{echo:'A'}})
33 assert.equal(first.result.status,'ok',first.result.diagnostic)
34 const second=await host.call('ext/activate',{owner:first.ref,plugin_name:'scoped-entries',entry:b,scope:b,config:{echo:'B'},data_dir:root})
35 assert.equal(second.status,'ok',second.diagnostic)
36 const tools=host.registry.filter(row=>row.op==='register' && row.kind==='tool')
37 assert.equal(tools.length,2)
38 assert.deepEqual(tools.map(row=>row.scope),[a,b])
39 assert.notEqual(tools[0].handle,tools[1].handle)
40 const call=handle=>host.call('tool/call',{handle,input:{},call_id:`scope-${handle}`,deadline_ms:5000})
41 assert.match(JSON.stringify(await call(tools[0].handle)),/A/)
42 assert.match(JSON.stringify(await call(tools[1].handle)),/B/)
43 assert.deepEqual(await host.call('ext/deactivate',{owner:first.ref,entry:a}),{disposed:true,leaked:[]})
44 await assert.rejects(call(tools[0].handle),/not live/)
45 assert.match(JSON.stringify(await call(tools[1].handle)),/B/)
46 assert.ok(host.registry.some(row=>row.op==='unregister' && row.handle===tools[0].handle))
47 assert.ok(!host.registry.some(row=>row.op==='unregister' && row.handle===tools[1].handle))
48 assert.deepEqual(await host.call('ext/deactivate',{owner:first.ref}),{disposed:true,leaked:[]})
49 await assert.rejects(call(tools[1].handle),/not live/)
50 })
51
52 test('failed scoped activation keeps the same owner sibling live and mismatched scopes refuse',async t=>{
53 const host=await startHost();t.after(()=>host.stop())
54 const {root,make}=entries(t)
55 const good=make('good',STATIC_KEPT_SOURCE)
56 const bad=make('bad',STATIC_FAIL_SOURCE)
57 const first=await activate(host,'scoped-failure',good.path,{scope:good,data_dir:root})
58 assert.equal(first.result.status,'ok',first.result.diagnostic)
59 const refused=await host.call('ext/activate',{owner:first.ref,plugin_name:'scoped-failure',entry:bad,scope:good,config:{},data_dir:root})
60 assert.equal(refused.status,'failed');assert.match(refused.diagnostic,/scope does not match/)
61 const failed=await host.call('ext/activate',{owner:first.ref,plugin_name:'scoped-failure',entry:bad,scope:bad,config:{},data_dir:root})
62 assert.equal(failed.status,'failed');assert.match(failed.diagnostic,/intentional scoped failure/)
63 assert.deepEqual(await host.call('ext/deactivate',{owner:first.ref,entry:bad}),{disposed:true,leaked:[]})
64 const partial=host.registry.find(row=>row.op==='register' && row.kind==='prompt_section')
65 assert.ok(host.registry.some(row=>row.op==='unregister' && row.handle===partial.handle))
66 const tool=host.registry.find(row=>row.op==='register' && row.kind==='tool')
67 assert.match(JSON.stringify(await host.call('tool/call',{handle:tool.handle,input:{},call_id:'kept',deadline_ms:5000})),/kept/)
68 assert.deepEqual(await host.call('ext/deactivate',{owner:first.ref}),{disposed:true,leaked:[]})
69 })
70
70 lines Plain Text