You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.2 KiB
79 lines
2.2 KiB
// Unit tests for the AO |
|
// The tests actually happen so your database will be modified (future: allow switching databases or automatically switch) |
|
// The tests use an AO API file saved in the same directory; this file must be kept up-to-date |
|
// Maybe in the future a precompiled api.js created from api.ts can be hosted so that ao-cli does not have to compile any TypeScript |
|
import inquirer from 'inquirer' |
|
import { createSession, logout } from './api.js' |
|
|
|
async function testLoginAndOut() { |
|
const username = 'ao' |
|
const password = 'ao' |
|
try { |
|
console.log('Attempting login as', username, 'with password', '*'.repeat(password.length)) |
|
const response = await createSession(username, password) |
|
if(response === true) { |
|
console.log('Logged in as', username) |
|
} else { |
|
console.log('Login failed. Response:', response) |
|
return false |
|
} |
|
} catch(err) { |
|
if(err.status === 401) { |
|
console.log("No account on the AO server matched the username and password you entered. (401 Unauthorized)") |
|
} else { |
|
console.log(err) |
|
} |
|
} |
|
|
|
try { |
|
console.log('Logging out...') |
|
const response = await logout() |
|
if(response.statusCode === 200) { |
|
console.log('Logged out') |
|
} else { |
|
console.log('Logout failed. Response:', response) |
|
return false |
|
} |
|
} catch(err) { |
|
console.log(err) |
|
} |
|
return true |
|
} |
|
|
|
async function runAllTests() { |
|
await testLoginAndOut() |
|
} |
|
|
|
const tests = { |
|
"Run All Tests": runAllTests, |
|
"Test Login/Logout": testLoginAndOut |
|
} |
|
|
|
// Prints the AO Unit Tests Menu and executes the user's choice |
|
export default async function testsMenu() { |
|
console.log(`\n${headerStyle('AO Unit Tests')}`) |
|
let testChoices = Object.entries(tests).map(([menuTitle, testFunction]) => { |
|
return menuTitle |
|
}) |
|
testChoices.push('Back to Main Menu') |
|
let answer |
|
try { |
|
answer = await inquirer.prompt({ |
|
name: 'tests_menu', |
|
type: 'list', |
|
message: 'Please choose:', |
|
choices: testChoices |
|
}) |
|
} catch(error) { |
|
if (error === 'EVENT_INTERRUPTED') { |
|
console.log('\nESC') |
|
return false |
|
} |
|
} |
|
if(answer.tests_menu === 'Back to Main Menu') { |
|
return false |
|
} |
|
const testFunction = tests[answer.tests_menu] |
|
if(testFunction) await testFunction() |
|
return true |
|
} |