const fs=require('fs'),vm=require('vm'),assert=require('assert');
const src=fs.readFileSync('static/app.js','utf8');
for(const marker of ["2. Who’s here?","creatorAddSomeone","melumiyachat_scenario_participants_v1","stable_key:'scenario:participants'","'main character':'secondary character'","User Persona is controlled only by the user"]){assert(src.includes(marker),`missing ${marker}`)}
const start=src.indexOf('function scenarioParticipantWinId('),end=src.indexOf('function scenarioParticipantWinRender(');assert(start>=0&&end>start,'participant helper block missing');
const ctx={console,state:{creatorCharacters:[],scenarioParticipantsDraft:[]},profileCreationImage:()=>'',scenarioDetailText:x=>typeof x==='string'?x:String(x?.details||x?.name||''),scenarioSlug:v=>String(v||'').toLowerCase().replace(/[^a-z0-9]+/g,'-').replace(/^-|-$/g,'').slice(0,100)||'item'};vm.createContext(ctx);vm.runInContext(src.slice(start,end),ctx);
let p=ctx.scenarioParticipantWinNormalize({type:'character',name:'Keegan',story_role:'main',present_at_start:false},0);assert.equal(p.story_role,'main');assert.equal(p.present_at_start,false);
let bot=ctx.scenarioParticipantWinNormalize({type:'bot',name:'Narrator',story_role:'narrator',present_at_start:true,diegetic:false},1);assert.equal(bot.story_role,'narrator');assert.equal(bot.present_at_start,false);assert.equal(bot.diegetic,false);
let persona=ctx.scenarioParticipantWinNormalize({type:'persona',name:'User Persona'},2);assert.equal(persona.speaking_enabled,false);assert.equal(persona.story_role,'user');
ctx.state.scenarioParticipantsDraft=[p,ctx.scenarioParticipantWinNormalize({type:'character',name:'Logan',story_role:'main'},1),ctx.scenarioParticipantWinNormalize({type:'character',name:'Hesh',story_role:'secondary'},2),bot,persona];
const c=ctx.scenarioParticipantWinCompat();assert.equal(c.participants.filter(x=>x.story_role==='main').length,2,'multiple mains must be allowed');assert.deepEqual(Array.from(c.present),['Logan','Hesh']);assert(c.linked.length===0);const sys=ctx.scenarioParticipantWinSystemEntry(c.participants);assert.equal(sys.kind,'scenario_participants');assert.equal(sys.participants.find(x=>x.type==='persona').speaking_enabled,false);
console.log('scenario participants contract: PASS — multi-main, secondary, presence, bot and persona rules');
