|
|
|
// 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 { createSession, logout } from './api.js'
|
|
|
|
import { promptMenu } from './welcome.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')
|
|
|
|
const answer = await promptMenu(testChoices, 'AO Unit Tests')
|
|
|
|
if(answer === 'Back to Main Menu') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const testFunction = tests[answer.tests_menu]
|
|
|
|
if(testFunction) await testFunction()
|
|
|
|
return true
|
|
|
|
}
|