import test from 'node:test'; import assert from 'node:assert/strict'; import {createSharedFolderReader, openWorkspaceAsset} from '../src/shared-folder.js'; test('opening a workspace asset rejects cross-group, cross-asset and overprivileged replies', async () => { const scope = {workspaceId: 'company', groupId: 'estimators'}; const grant = {...scope, asset: 'suppliers', folderId: 'folder-1', path: '/shared/suppliers', canWrite: false}; const open = reply => openWorkspaceAsset({scope, asset: 'suppliers', access: 'read', bridge: { async invoke(command, args) { assert.equal(command, 'filesystem_open_workspace_asset'); assert.deepEqual(args, {asset: 'suppliers', access: 'read'}); return reply; } }}); assert.deepEqual(await open(grant), grant); for (const patch of [{groupId:'other'}, {asset:'prices'}, {path:'/shared'}, {canWrite:true}]) { await assert.rejects(open({...grant, ...patch}), /Invalid workspace asset grant/); } }); test('specialist and other viewers read the same existing supplier register and price book', async () => { const root='/Shared/Registers'; const scope={workspaceId:'w',groupId:'g'}; const folder={path:root,...scope}; const register={format:'isa-match-workspace-suppliers-v1',workspaceId:'w',id:'abc',suppliers:[{id:'supplier-1',name:'Voorbeeld',administration:'',number:''}]}; const row={supplier:'Voorbeeld',unit:'m²',unitPrice:42,currency:'EUR'}; const commands=[]; function viewer(extension) { return createSharedFolderReader({folder,scope,bridge:{ async invoke(command,args) { commands.push([extension,command]); if (command === 'filesystem_list_dir') { assert.equal(args.path,root); return {authoritative:true,files:[{kind:'file',path:root+'/isa-match-suppliers.json'}]}; } assert.equal(command,'filesystem_get_files'); assert.equal(args.groupId,'g'); const [{path,cachePolicy}]=args.files; assert.equal(path,root+'/isa-match-suppliers.json'); assert.equal(cachePolicy,'dashboard-required'); return {items:[{path,source:'dashboard',authoritative:true,status:'ok',fileId:'index',syncRevision:1,...(extension==='other-viewer'?{bytesBase64:Buffer.from(JSON.stringify(register)).toString('base64')}:{text:JSON.stringify(register)})}]}; }, async snapshotKv(args) { commands.push([extension,'snapshot']); assert.equal(args.path,root+'/isa-match-prices-abc.kv'); return {revision:2,snapshotId:'snapshot',storeId:'store',incarnation:'1',entries:[{status:'found',kind:'value',key:'price/g/1',value:row}]}; } }}); } for(const reader of [viewer('supplier-viewer'),viewer('other-viewer')]) { assert.equal((await reader.listFiles()).length,1); const index=await reader.readJson('isa-match-suppliers.json'); assert.deepEqual(index.value,register); const result=await reader.readKv('isa-match-prices-'+index.value.id+'.kv'); assert.deepEqual(result.entries[0].value,row); await assert.rejects(reader.readKv('../other.kv'),/Unsafe/); } assert.equal(commands.length,6); }); test('revocation and mismatched launch scopes never resolve to empty or initialized data',async () => { const scope={workspaceId:'w',groupId:'g'}; const bridge={invoke:async()=>{throw new Error('Access revoked');}}; assert.throws(()=>createSharedFolderReader({scope,bridge,folder:{path:'/Shared',groupId:'other'}}),/Invalid shared folder/); const reader=createSharedFolderReader({scope,bridge,folder:{path:'/Shared'}}); await assert.rejects(reader.readJson('isa-match-suppliers.json'),/Access revoked/); });