diff --git a/src/settings-onboarding-page.js b/src/settings-onboarding-page.js index 3d4136b7..16949dde 100644 --- a/src/settings-onboarding-page.js +++ b/src/settings-onboarding-page.js @@ -66,7 +66,7 @@ $('hosted-connect').addEventListener('click',async()=>{ if(generation!==hostedGeneration)return; hostedFlow=false; if(e.message==='invalid_url') $('hosted-address').setAttribute('aria-invalid','true'); - say('hosted-service-result','GitHub sign-in could not start: '+(e.message==='invalid_url'?'Enter an HTTPS card service address. Use HTTP only for localhost, 127.0.0.1 or [::1].':e.message==='no_credential_store'?'This installation cannot store sign-in credentials. GitHub sign-in is unavailable.':e.message==='not_configured'?'GitHub sign-in is not configured in this installation.':e.message==='unreachable'?'Could not reach GitHub. Check your connection and try again.':e.message)); + say('hosted-service-result','GitHub sign-in could not start: '+(e.message==='invalid_url'?'Enter an HTTPS card service address. Use HTTP only for localhost, 127.0.0.1 or [::1].':e.message==='no_credential_store'?'This installation cannot store sign-in credentials. GitHub sign-in is unavailable.':e.message==='not_configured'?'GitHub sign-in is not configured in this installation.':e.message==='unreachable'?'Could not reach GitHub. Check your connection and try again.':e.message==='github_error'?'GitHub could not complete sign-in. Try signing in again.':e.message)); } finally { if(generation===hostedGeneration)hostedStarting=false; } }); async function pollHosted(generation){ @@ -82,7 +82,7 @@ async function pollHosted(generation){ } catch(e){ if(generation!==hostedGeneration)return; hostedFlow=false; - say('hosted-service-result','GitHub sign-in failed: '+(e.message==='denied'?'Sign-in was not approved. Click Sign in with GitHub to try again.':e.message==='expired'?'The sign-in code expired. Click Sign in with GitHub to get a new code.':e.message==='rate_limited'?'The card service received too many sign-in requests. Wait a while before signing in again.':e.message)); + say('hosted-service-result','GitHub sign-in failed: '+(e.message==='denied'?'Sign-in was not approved. Click Sign in with GitHub to try again.':e.message==='expired'?'The sign-in code expired. Click Sign in with GitHub to get a new code.':e.message==='rate_limited'?'The card service received too many sign-in requests. Wait a while before signing in again.':e.message==='github_error'?'GitHub could not complete sign-in. Try signing in again.':e.message)); } } load(); diff --git a/test/settings-github-error-feedback.test.js b/test/settings-github-error-feedback.test.js new file mode 100644 index 00000000..d21a2a63 --- /dev/null +++ b/test/settings-github-error-feedback.test.js @@ -0,0 +1,20 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {runInNewContext} from 'node:vm'; +import {SERVICE_SCRIPT} from '../src/settings-onboarding-page.js'; +test('GitHub start explains an unsuccessful GitHub response',async()=>{ + const nodes=new Map();const get=id=>{if(!nodes.has(id))nodes.set(id,{value:'https://cards.example',hidden:true,setAttribute(k,v){this[k]=v},addEventListener(k,fn){this[k]=fn},replaceChildren(){}});return nodes.get(id)}; + runInNewContext(SERVICE_SCRIPT,{document:{getElementById:get},fetch:async path=>path==='/api/settings/services'?{ok:true,json:async()=>({spotify:null,hosted:{url:'https://cards.example'}})}:{ok:false,json:async()=>({status:'github_error'})},performance:{now:()=>0},setTimeout(){}}); + await new Promise(r=>setImmediate(r));await get('hosted-connect').click(); + assert.equal(get('hosted-service-result').textContent,'GitHub sign-in could not start: GitHub could not complete sign-in. Try signing in again.'); +}); + +test('GitHub poll explains an unsuccessful GitHub response',async()=>{ + const nodes=new Map();const get=id=>{if(!nodes.has(id))nodes.set(id,{value:'https://cards.example',hidden:true,addEventListener(k,fn){this[k]=fn},replaceChildren(){}});return nodes.get(id)}; + const say=(id,text)=>get(id).textContent=text; + const start=SERVICE_SCRIPT.indexOf('async function pollHosted(');const end=SERVICE_SCRIPT.lastIndexOf('load();'); + const context={$:get,say,hostedFlow:true,hostedGeneration:1,api:async()=>({status:'github_error'}),load(){throw Error('should not load after rate limit')}}; + runInNewContext(SERVICE_SCRIPT.slice(start,end),context);await context.pollHosted(1); + assert.equal(get('hosted-service-result').textContent,'GitHub sign-in failed: GitHub could not complete sign-in. Try signing in again.'); + assert.equal(context.hostedFlow,false); +});