import fs from 'node:fs';import assert from 'node:assert/strict';import {createSupplierPicker} from '../public/supplier-picker-model.mjs'; const out='docs/proofs/supplier-picker-model-20261103.json';assert(!fs.existsSync(out));const checks=[],pending=[],states=[];let active=true;const picker=createSupplierPicker((q,p)=>new Promise((resolve,reject)=>pending.push({q,p,resolve,reject})),s=>states.push(s),()=>active),page=(id,p=1)=>({data:[{id,name:'QA',selection_revision:'v'+id}],current_page:p,last_page:5,total:125}); const a=picker.search('old'),b=picker.search('new');pending[1].resolve(page(2));await b;pending[0].resolve(page(1));await a;assert.equal(states.at(-1).items[0].id,2);checks.push('late older query cannot overwrite newer results');assert.equal(picker.selected,null);checks.push('results never automatically select first supplier');picker.choose(2);assert.equal(picker.selected.selection_revision,'v2');checks.push('individual choice retains exact supplier revision'); const c=picker.search('new',2);assert.equal(picker.selected,null);checks.push('changing page immediately invalidates previous choice');pending[2].reject(Error('QA unavailable'));await c;assert.equal(states.at(-1).items.length,0);assert.equal(states.at(-1).error,'QA unavailable');checks.push('failed request keeps no stale selectable options'); const d=picker.search('recover');pending[3].resolve(page(3));await d;picker.choose(3);picker.invalidate();assert.equal(picker.selected,null);assert.equal(states.at(-1).items.length,0);checks.push('query editing invalidates choice and options'); const e=picker.search('departed');active=false;const count=states.length;pending[4].resolve(page(4));await e;assert.equal(states.length,count);checks.push('departed workspace ignores outstanding response');active=true;picker.choose(9999);assert.equal(picker.selected,null);checks.push('unknown id cannot become a selection'); fs.writeFileSync(out,JSON.stringify({passed:true,checks,visual_review:false},null,2)+'\n');console.log(JSON.stringify({passed:true,checks:checks.length}));