From 092242c191297b75a277373a059c4f744e1c4610 Mon Sep 17 00:00:00 2001 From: Zane Whitfield Date: Wed, 18 Sep 2024 16:56:59 -0700 Subject: [PATCH] Update command & tests for API error --- src/commands/ai/models/list.ts | 10 ++++++---- test/commands/ai/models/list.test.ts | 21 +++++++++++++-------- test/helpers/fixtures.ts | 7 +++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/commands/ai/models/list.ts b/src/commands/ai/models/list.ts index 94494ef..39e4b54 100644 --- a/src/commands/ai/models/list.ts +++ b/src/commands/ai/models/list.ts @@ -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) => { @@ -28,13 +29,14 @@ export default class List extends Command { const herokuAIClient = this.herokuAI const urlPath = '/available-models' - const {body: availableModels} = await herokuAIClient.get(urlPath) - if (availableModels.length > 0) { + try { + const {body: availableModels} = await herokuAIClient.get(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}) } } } diff --git a/test/commands/ai/models/list.test.ts b/test/commands/ai/models/list.test.ts index 1d5db27..98f35f6 100644 --- a/test/commands/ai/models/list.test.ts +++ b/test/commands/ai/models/list.test.ts @@ -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 () { @@ -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.modelsListErrorResponse) + + 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) + } }) }) diff --git a/test/helpers/fixtures.ts b/test/helpers/fixtures.ts index e201e10..d0d55b0 100644 --- a/test/helpers/fixtures.ts +++ b/test/helpers/fixtures.ts @@ -31,6 +31,13 @@ export const availableModels = [ }, ] +export const mockAPIErrors = { + modelsListErrorResponse: { + 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',