|
| 1 | +import { jest } from '@jest/globals' |
| 2 | + |
| 3 | +import type { BorderConfig, getBorderCharacters, table } from 'table' |
| 4 | +import type { findTopicsAndSubcommands } from '../../lib/command-util.js' |
| 5 | + |
| 6 | + |
| 7 | +const borderConfig = { topLeft: 'top-left' } as BorderConfig |
| 8 | +const getBorderCharactersMock = jest.fn<typeof getBorderCharacters>() |
| 9 | + .mockReturnValue(borderConfig) |
| 10 | +const tableMock = jest.fn<typeof table>() |
| 11 | +jest.unstable_mockModule('table', () => ({ |
| 12 | + getBorderCharacters: getBorderCharactersMock, |
| 13 | + table: tableMock, |
| 14 | +})) |
| 15 | + |
| 16 | +const findTopicsAndSubcommandsMock = jest.fn<typeof findTopicsAndSubcommands>() |
| 17 | + .mockReturnValue({ topics: [], subCommands: [] }) |
| 18 | +jest.unstable_mockModule('../../lib/command-util.js', () => ({ |
| 19 | + findTopicsAndSubcommands: findTopicsAndSubcommandsMock, |
| 20 | +})) |
| 21 | + |
| 22 | + |
| 23 | +const { buildEpilog, apiDocsURL, itemInputHelpText } = await import('../../lib/help.js') |
| 24 | + |
| 25 | + |
| 26 | +describe('apiDocsURL', () => { |
| 27 | + it('builds URL stanza for single api name', () => { |
| 28 | + const result = apiDocsURL('getDevice') |
| 29 | + expect(result).toBe('For API information, see:\n' + |
| 30 | + ' https://developer.smartthings.com/docs/api/public/#operation/getDevice') |
| 31 | + }) |
| 32 | + |
| 33 | + it('builds URL stanza for multiple api names', () => { |
| 34 | + const result = apiDocsURL(['getDevice', 'listDevices']) |
| 35 | + expect(result).toBe( |
| 36 | + 'For API information, see:\n' + |
| 37 | + ' https://developer.smartthings.com/docs/api/public/#operation/getDevice\n' + |
| 38 | + ' https://developer.smartthings.com/docs/api/public/#operation/listDevices', |
| 39 | + ) |
| 40 | + }) |
| 41 | + |
| 42 | + it('passes through existing URLs', () => { |
| 43 | + const result = apiDocsURL(['http://example.com/doc', 'https://example.com/ssl-doc', 'getDevice']) |
| 44 | + expect(result).toBe( |
| 45 | + 'For API information, see:\n' + |
| 46 | + ' http://example.com/doc\n' + |
| 47 | + ' https://example.com/ssl-doc\n' + |
| 48 | + ' https://developer.smartthings.com/docs/api/public/#operation/getDevice', |
| 49 | + ) |
| 50 | + }) |
| 51 | +}) |
| 52 | + |
| 53 | +describe('itemInputHelpText', () => { |
| 54 | + it('builds help text for a single name', () => { |
| 55 | + const result = itemInputHelpText('getDevice') |
| 56 | + expect(result).toBe('More information can be found at:\n' + |
| 57 | + ' https://developer.smartthings.com/docs/api/public/#operation/getDevice') |
| 58 | + }) |
| 59 | + |
| 60 | + it('builds help text for multiple names and URLs', () => { |
| 61 | + const result = itemInputHelpText('getDevice', 'http://example.com/doc') |
| 62 | + expect(result).toBe( |
| 63 | + 'More information can be found at:\n' + |
| 64 | + ' https://developer.smartthings.com/docs/api/public/#operation/getDevice\n' + |
| 65 | + ' http://example.com/doc', |
| 66 | + ) |
| 67 | + }) |
| 68 | +}) |
| 69 | + |
| 70 | +describe('buildEpilog', () => { |
| 71 | + it('returns empty string when no options provided', () => { |
| 72 | + expect(buildEpilog({ command: 'test' })).toBe('') |
| 73 | + }) |
| 74 | + |
| 75 | + it('includes note from note provided via `notes`', () => { |
| 76 | + const epilog = buildEpilog({ command: 'test', notes: 'Single note' }) |
| 77 | + expect(epilog).toBe('Notes:\n Single note') |
| 78 | + }) |
| 79 | + |
| 80 | + it('includes note from formattedNotes', () => { |
| 81 | + expect(buildEpilog({ command: 'test', formattedNotes: 'formatted note' })).toBe('Notes:\nformatted note') |
| 82 | + }) |
| 83 | + |
| 84 | + it('includes all notes from multiple notes provided via `notes`', () => { |
| 85 | + expect(buildEpilog({ command: 'test', notes: ['First note', 'Second note', 'Third note'] })) |
| 86 | + .toBe('Notes:\n First note\n Second note\n Third note') |
| 87 | + }) |
| 88 | + |
| 89 | + it('includes notes from both notes and formattedNotes', () => { |
| 90 | + expect(buildEpilog({ command: 'test', notes: ['note 1', 'note 2'], formattedNotes: 'formatted notes' })) |
| 91 | + .toBe('Notes:\n note 1\n note 2\nformatted notes') |
| 92 | + }) |
| 93 | + |
| 94 | + it('includes topics section when topics found', () => { |
| 95 | + findTopicsAndSubcommandsMock.mockReturnValueOnce({ topics: ['test::topic'], subCommands: [] }) |
| 96 | + expect(buildEpilog({ command: 'test' })).toBe('Topics:\n test::topic') |
| 97 | + |
| 98 | + findTopicsAndSubcommandsMock.mockReturnValueOnce({ topics: ['topic1', 'topic2'], subCommands: [] }) |
| 99 | + expect(buildEpilog({ command: 'test' })).toBe('Topics:\n topic1\n topic2') |
| 100 | + }) |
| 101 | + |
| 102 | + it('includes apiDocs section when apiDocs provided', () => { |
| 103 | + expect(buildEpilog({ command: 'devices', apiDocs: ['getDevice', 'listDevices'] })) |
| 104 | + .toBe( |
| 105 | + 'For API information, see:\n' + |
| 106 | + ' https://developer.smartthings.com/docs/api/public/#operation/getDevice\n' + |
| 107 | + ' https://developer.smartthings.com/docs/api/public/#operation/listDevices', |
| 108 | + ) |
| 109 | + }) |
| 110 | + |
| 111 | + it('includes sub-commands section when sub-commands found', () => { |
| 112 | + tableMock.mockReturnValueOnce('sub-command table output') |
| 113 | + const subCommands = [ |
| 114 | + { |
| 115 | + relatedName: 'test:sub1', |
| 116 | + command: { |
| 117 | + describe: 'sub 1 description', |
| 118 | + handler: () => { /* noop */ }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + relatedName: 'test:sub2', |
| 123 | + command: { |
| 124 | + describe: 'sub 2 description', |
| 125 | + handler: () => { /* noop */ }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + ] |
| 129 | + findTopicsAndSubcommandsMock.mockReturnValueOnce({ topics: [], subCommands }) |
| 130 | + |
| 131 | + expect(buildEpilog({ command: 'test' })).toBe(('Sub-Commands:\nsub-command table output')) |
| 132 | + |
| 133 | + expect(getBorderCharactersMock).toHaveBeenCalledExactlyOnceWith('void') |
| 134 | + expect(tableMock).toHaveBeenCalledExactlyOnceWith([ |
| 135 | + [' test:sub1', 'sub 1 description' ], |
| 136 | + [' test:sub2', 'sub 2 description' ], |
| 137 | + ], expect.objectContaining({ border: borderConfig })) |
| 138 | + |
| 139 | + // Call this trivial function to fulfill test coverage. :-) |
| 140 | + expect(tableMock.mock.calls[0][1]?.drawHorizontalLine?.(0, 0)).toBe(false) |
| 141 | + }) |
| 142 | + |
| 143 | + it('joins sections correctly when multiple present', () => { |
| 144 | + findTopicsAndSubcommandsMock.mockReturnValueOnce({ topics: ['test::topic'], subCommands: [] }) |
| 145 | + |
| 146 | + expect(buildEpilog({ command: 'test', formattedNotes: 'formatted note' })) |
| 147 | + .toBe('Notes:\nformatted note\n\nTopics:\n test::topic') |
| 148 | + }) |
| 149 | +}) |
0 commit comments