// Explicit Development-only authentication test. Uses a freshly provisioned private fixture identity; no manuscripts/AI. const {chromium,request}=require('playwright'),fs=require('fs'),assert=require('assert/strict'),crypto=require('crypto');let step='initialise'; (async()=>{const path=process.argv[2];assert.ok(path?.startsWith('/tmp/pd-dev-browser-'));assert.equal(fs.statSync(path).mode&0o077,0);const identity=JSON.parse(fs.readFileSync(path,'utf8'));assert.equal(identity.Database,'PlotDirector_Development');assert.match(identity.Email,/^pwa-browser-[a-f0-9]+@example\.invalid$/);const base='https://dev.plotdirector.com',browser=await chromium.launch();const contexts=[];try{ const context=await browser.newContext();const page=await context.newPage();step='normal login';await page.goto(base+'/Account/Login');await page.locator('input[name=Email]').fill(identity.Email);await page.locator('input[name=Password]').fill(identity.Password);await Promise.all([page.waitForURL(u=>u.pathname!='/Account/Login'),page.locator('form:has(input[name=Password]) button[type=submit]').click()]); const bare=await request.newContext({baseURL:base});contexts.push(bare); async function pair(){const connectionId=crypto.randomUUID(),verifier=crypto.randomBytes(32).toString('hex'),challenge=crypto.createHash('sha256').update(verifier).digest('hex').toUpperCase();const response=await bare.post('/api/native-companion/pair',{data:{challenge,persistent:true,connectionId,deviceName:'Isolated persistent-authorisation test'}});assert.ok(response.ok());const {code}=await response.json();await page.goto(base+'/native-companion/authorize?code='+encodeURIComponent(code));assert.match(await page.locator('body').innerText(),/90 days/);const token=await page.locator('input[name=__RequestVerificationToken]').inputValue();assert.ok((await context.request.post(base+'/native-companion/authorize',{form:{code,__RequestVerificationToken:token}})).ok());const tokens=await(await bare.post('/api/native-companion/token',{data:{code,verifier}})).json();assert.equal(tokens.accountId,identity.UserID);assert.equal(tokens.connectionId,connectionId);assert.equal(tokens.deviceSecret.length,64);assert.ok(Date.parse(tokens.expiresUtc)-Date.now()<11*60*1000);return tokens;} async function agent(token){const client=await request.newContext({baseURL:base,extraHTTPHeaders:{Authorization:'Bearer '+token}});contexts.push(client);return client;} step='persistent first approval';const initial=await pair();let client=await agent(initial.accessToken);assert.ok((await client.post('/api/native-companion/heartbeat',{data:{}})).ok()); step='permission-free agent state without Word';let state=await(await context.request.get(base+'/native-companion/connection-state')).json();assert.equal(state.state,'Connected');assert.equal(state.wordConnected,false);assert.deepEqual(await(await context.request.get(base+'/native-companion/status')).json(),[]); step='rotation and lost-response retry';const next=crypto.randomBytes(32).toString('hex').toUpperCase(),rotation={installationId:initial.installationId,credential:initial.deviceSecret,nextCredential:next,rotationId:crypto.randomUUID(),connectionId:initial.connectionId};let response=await bare.post('/api/native-companion/renew',{data:rotation});assert.ok(response.ok());let renewed=await response.json();assert.equal(renewed.installationId,initial.installationId);assert.equal(renewed.accountId,identity.UserID);assert.ok((await bare.post('/api/native-companion/renew',{data:rotation})).ok());assert.equal((await bare.post('/api/native-companion/renew',{data:{...rotation,rotationId:crypto.randomUUID()}})).status(),401); step='process restart replaces access without approval';const newest=crypto.randomBytes(32).toString('hex').toUpperCase(),restart={...rotation,credential:next,nextCredential:newest,rotationId:crypto.randomUUID(),connectionId:crypto.randomUUID()};response=await bare.post('/api/native-companion/renew',{data:restart});assert.ok(response.ok());const restarted=await response.json();assert.equal((await client.get('/api/native-companion/books')).status(),401);client=await agent(restarted.accessToken);assert.ok((await client.get('/api/native-companion/books')).ok()); step='authorised device management and antiforgery';await page.goto(base+'/native-companion/installations');assert.match(await page.locator('main').innerText(),/Isolated persistent-authorisation test/);assert.ok(!(await page.content()).includes(initial.deviceSecret));assert.equal((await context.request.post(base+'/native-companion/installations/'+initial.installationId+'/revoke',{form:{}})).status(),400);let csrf=await page.locator('input[name=__RequestVerificationToken]').first().inputValue();assert.ok((await context.request.post(base+'/native-companion/installations/'+initial.installationId+'/revoke',{form:{__RequestVerificationToken:csrf}})).ok());assert.equal((await client.get('/api/native-companion/books')).status(),401);assert.equal((await bare.post('/api/native-companion/renew',{data:{...restart,credential:newest,nextCredential:crypto.randomBytes(32).toString('hex').toUpperCase(),rotationId:crypto.randomUUID()}})).status(),401);state=await(await context.request.get(base+'/native-companion/connection-state')).json();assert.equal(state.state,'Action required'); step='website logout revokes approved installations';const second=await pair();client=await agent(second.accessToken);assert.ok((await client.get('/api/native-companion/books')).ok());await page.goto(base+'/native-companion/installations');csrf=await page.locator('input[name=__RequestVerificationToken]').first().inputValue();assert.ok((await context.request.post(base+'/Account/Logout',{form:{__RequestVerificationToken:csrf}})).ok());assert.equal((await client.get('/api/native-companion/books')).status(),401);assert.equal((await bare.post('/api/native-companion/renew',{data:{installationId:second.installationId,credential:second.deviceSecret,nextCredential:crypto.randomBytes(32).toString('hex').toUpperCase(),rotationId:crypto.randomUUID(),connectionId:second.connectionId}})).status(),401); console.log('PASS live Development persistent pairing, SQL-backed renewal/retry, process replacement, agent-only liveness, antiforgery, unexpired-access revocation and website logout. No manuscript or AI requests.'); }finally{for(const context of contexts)await context.dispose();await browser.close();}})().catch(error=>{fs.writeFileSync('/tmp/pd-native-authorisation-error.txt',String(error.stack),{mode:0o600});console.error('FAIL authentication smoke at '+step+'; private diagnostic recorded.');process.exit(1);});