Skip to content

Commit

Permalink
Update command & tests for API error
Browse files Browse the repository at this point in the history
  • Loading branch information
zwhitfield3 committed Sep 18, 2024
1 parent 17d4de4 commit 9ef0330
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/commands/ai/models/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ux} from '@oclif/core'
import {ModelList} from '../../../lib/ai/types'
import {CLIError} from '@oclif/core/lib/errors'
import Command from '../../../lib/base'

const displayModels = (models: any) => {
Expand Down Expand Up @@ -28,13 +29,14 @@ export default class List extends Command {

const herokuAIClient = this.herokuAI
const urlPath = '/available-models'
const {body: availableModels} = await herokuAIClient.get<ModelList>(urlPath)

if (availableModels.length > 0) {
try {
const {body: availableModels} = await herokuAIClient.get<ModelList>(urlPath)
displayModels(availableModels)
ux.log('\nSee https://devcenter.heroku.com/articles/rainbow-unicorn-princess-models for more info.')
} else {
ux.warn('Failed to retrieve the list of available models. Check the Heroku Status page https://status.heroku.com/ for system outages. After all incidents have resolved, try again. You can also see a list of models at https://devcenter.heroku.com/articles/rainbow-unicorn-princess-models.')
} catch (error) {
const {message} = error as CLIError
ux.error(message, {exit: 1})
}
}
}
21 changes: 13 additions & 8 deletions test/commands/ai/models/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {stderr, stdout} from 'stdout-stderr'
import Cmd from '../../../../src/commands/ai/models/list'
import stripAnsi from '../../../helpers/strip-ansi'
import {runCommand} from '../../../run-command'
import {availableModels} from '../../../helpers/fixtures'
import {availableModels, mockAPIErrors} from '../../../helpers/fixtures'
import {CLIError} from '@oclif/core/lib/errors'
import nock from 'nock'

describe('ai:models:list', function () {
Expand Down Expand Up @@ -41,12 +42,16 @@ describe('ai:models:list', function () {

herokuAI
.get('/available-models')
.reply(200, [])

await runCommand(Cmd)
.then(() => expect(stdout.output).to.eq(''))
.then(() => expect(stripAnsi(stderr.output)).to.contain('Failed to retrieve the list of available models.'))
.then(() => expect(stripAnsi(stderr.output)).to.contain(statusURL))
.then(() => expect(stripAnsi(stderr.output)).to.contain(modelsDevCenterURL))
.reply(500, mockAPIErrors.modelsListErrors)

try {
await runCommand(Cmd)
} catch (error) {
const {message, oclif} = error as CLIError
expect(stripAnsi(message)).to.contains('Failed to retrieve the list of available models.')
expect(stripAnsi(message)).to.contains(statusURL)
expect(stripAnsi(message)).to.contains(modelsDevCenterURL)
expect(oclif.exit).to.equal(1)
}
})
})
7 changes: 7 additions & 0 deletions test/helpers/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export const availableModels = [
},
]

export const mockAPIErrors = {
modelsListErrors: {
id: 'error',
message: 'Failed to retrieve the list of available models. Check the Heroku Status page https://status.heroku.com/ for system outages. After all incidents have resolved, try again. You can also see a list of models at https://devcenter.heroku.com/articles/rainbow-unicorn-princess-models.',
},
}

export const addon1: Heroku.AddOn = {
addon_service: {
id: '4b46be3f-d0e6-4b3f-b616-0a857115d71d',
Expand Down

0 comments on commit 9ef0330

Please sign in to comment.