import test from 'node:test'; import assert from 'node:assert/strict'; import {openPriceAsset} from '../src/prices.js'; test('workspace writers can backfill the same observation concurrently without replacing edits or creating duplicates',async () => { let revision = 0, conflict = true; const records = new Map(), writes = []; const identity = {storeId:'s',incarnation:'i',supportsExpectedRevision:true}; const scope = {workspaceId:'w',groupId:'g'}; const bridge = {async invoke(command,args) { if(command === 'filesystem_open_workspace_asset') return {...scope,asset:'prices',folderId:'f',path:'/shared/prices',canWrite:true}; if(command === 'kv_store_open') return {...identity,revision}; if(command === 'kv_store_query_list') return {...identity,revision,page:{items:args.ids.filter(id=>records.has(id)).map(id=>({id,value:records.get(id)}))}}; if(command === 'kv_store_apply') { if(conflict) { records.set('a',{description:'Manual correction',supplier:'A',unitPrice:0}); revision++; conflict = false; throw Error('revision_conflict'); } assert.equal(args.expectedRevision,revision); for(const row of args.operations) records.set(row.id,row.value); writes.push(args.operations.length); return {...identity,pending:false,acceptedRevision:++revision}; } assert.fail(command); }}; const asset = await openPriceAsset({bridge,scope}); const rows = [{id:'a',value:{description:'old',supplierId:'supplier-a',unitPrice:0}}]; await asset.importRows(rows); await asset.importRows(rows); assert.deepEqual(records.get('a'), {description:'Manual correction',supplier:'A',unitPrice:0,supplierId:'supplier-a'}); assert.deepEqual(writes,[1]); }); test('a read-only open distinguishes a genuinely empty asset from an inaccessible inventory and creates no KV file', async () => { const scope = {workspaceId:'w',groupId:'g'}; let denied = false; const bridge = {async invoke(command) { if(command === 'filesystem_open_workspace_asset') return {...scope,asset:'prices',folderId:'f',path:'/shared/prices',canWrite:false}; if(command === 'filesystem_list_dir') { if(denied) throw Error('access revoked'); return {authoritative:true,files:[]}; } assert.fail(command); }}; const asset = await openPriceAsset({bridge,scope,access:'read'}); assert.deepEqual((await asset.query()).items,[]); denied = true; await assert.rejects(asset.query(), /access revoked/); });