diff --git a/client/settings.ts b/client/settings.ts index c00ce2a7..fb7ab15e 100644 --- a/client/settings.ts +++ b/client/settings.ts @@ -1 +1,159 @@ -// TODO: launch test one by one, using authent Bearer. +interface Result { + label?: string + messages: string[] + next?: string | ((r: Result) => void) + ok: boolean + test: string +} + +function launchTests (): void { + let tokenType: string + let accessToken: string + let refreshToken: string + + const container = document.querySelector('div') + if (!container) { + throw new Error('Cant find main container') + } + const ul = document.createElement('ul') + container.innerHTML = '' + container.append(ul) + + function appendMessages (result: Result): void { + const li = document.createElement('li') + + const label = document.createElement('span') + label.textContent = (result.label ?? result.test) + ': ' + li.append(label) + + const state = document.createElement('b') + if (result.ok) { + state.textContent = 'OK' + state.style.color = 'green' + } else { + state.textContent = 'KO' + state.style.color = 'red' + } + li.append(state) + + if (result.messages.length) { + const messageUl = document.createElement('ul') + for (let i = 0; i < result.messages.length; i++) { + const messageLi = document.createElement('li') + messageLi.textContent = result.messages[i] + messageUl.append(messageLi) + } + li.append(messageUl) + } + + ul.append(li) + } + + function testBrowser (): Result { + const r: Result = { + ok: false, + next: 'backend', + messages: [], + test: 'browser', + label: 'Browser' + } + + if (!window.fetch) { + r.messages.push('Missing fetch function') + } + + if (!window.localStorage) { + r.messages.push('Browser has no local storage') + } else { + tokenType = window.localStorage.getItem('token_type') ?? '' + accessToken = window.localStorage.getItem('access_token') ?? '' + refreshToken = window.localStorage.getItem('refresh_token') ?? '' + if (tokenType === '') { + r.messages.push('Your browser is not authenticated on your Peertube instance (no token_type)') + } else if (tokenType.toLowerCase() !== 'bearer') { + r.messages.push('Your brower is authenticated with an unknown token type: ' + tokenType) + } else if (accessToken === '') { + r.messages.push('Your browser is not authenticated on your Peertube instance (no access_token)') + } else if (refreshToken === '') { + r.messages.push('Your browser is not authenticated on your Peertube instance (no refresh_token)') + } + } + + r.ok = r.messages.length === 0 + return r + } + + let testUrl: string = window.location.pathname + if (testUrl[testUrl.length - 1] !== '/') testUrl += '/' + testUrl += 'test' + async function callTest (test: string): Promise { + const response = await window.fetch(testUrl, { + method: 'POST', + headers: new Headers({ + Authorization: 'Bearer ' + accessToken, + 'content-type': 'application/json;charset=UTF-8' + }), + body: JSON.stringify({ + test: test + }) + }) + const data = await response.json() + if (!response.ok) { + return { + test: test, + messages: ['Unknown error'], + ok: false + } + } + if ((typeof data) !== 'object') { + return { + test: test, + messages: ['Incorrect reponse type: ' + (typeof data)], + ok: false + } + } + return data as Result + } + + async function machine (result: Result): Promise { + appendMessages(result) + if (!result.ok) { + return + } + if (!result.next) { + return + } + const waiting = document.createElement('li') + waiting.innerHTML = 'Testing...' + ul.append(waiting) + if ((typeof result.next) === 'function') { + const r: Result = (result.next as Function)() + waiting.remove() + await machine(r) + } else { + console.log('Calling test ' + (result.next as string)) + const r: Result = await callTest(result.next as string) + waiting.remove() + await machine(r) + } + } + + machine({ + label: 'Starting tests', + messages: [], + next: testBrowser, + ok: true, + test: 'start' + }).then( + () => {}, + (error) => { + console.error(error) + const d = document.createElement('div') + d.style.color = 'red' + d.textContent = error + document.body.append(d) + } + ) +} + +launchTests() diff --git a/server/lib/routers/settings.ts b/server/lib/routers/settings.ts index 20189b88..61b1d120 100644 --- a/server/lib/routers/settings.ts +++ b/server/lib/routers/settings.ts @@ -1,6 +1,14 @@ import type { Router, Request, Response, NextFunction } from 'express' import { getBaseStaticRoute } from '../helpers' +interface Result { + label?: string + messages: string[] + next?: string + ok: boolean + test: string +} + async function initSettingsRouter ({ peertubeHelpers, getRouter @@ -30,11 +38,22 @@ async function initSettingsRouter ({ const test: string = req.body.test || '' logger.info('Accessing peertube-plugin-livechat diagnostic tool, test "' + test + '".') - const result: any = { - test: test, - message: null, - next: null, - ok: false + + let result: Result + if (test === 'backend') { + result = { + ok: true, + label: 'Backend connection', + test: test, + messages: [] + } + } else { + result = { + label: test, + test: test, + messages: ['Unknown test'], + ok: false + } } res.status(200)