|
| 1 | +/** |
| 2 | + * Unit tests for organization list output formatting. |
| 3 | + * |
| 4 | + * Purpose: |
| 5 | + * Tests the output formatting for organization list results. |
| 6 | + * |
| 7 | + * Test Coverage: |
| 8 | + * - outputOrganizationList function |
| 9 | + * - JSON output format |
| 10 | + * - Text output format |
| 11 | + * - Markdown output format |
| 12 | + * - Error handling |
| 13 | + * |
| 14 | + * Related Files: |
| 15 | + * - src/commands/organization/output-organization-list.mts (implementation) |
| 16 | + */ |
| 17 | + |
| 18 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 19 | + |
| 20 | +// Mock yoctocolors. |
| 21 | +vi.mock('yoctocolors-cjs', () => ({ |
| 22 | + default: { |
| 23 | + bold: (s: string) => s, |
| 24 | + italic: (s: string) => s, |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +// Mock logger. |
| 29 | +const mockLogger = vi.hoisted(() => ({ |
| 30 | + log: vi.fn(), |
| 31 | + error: vi.fn(), |
| 32 | + warn: vi.fn(), |
| 33 | + fail: vi.fn(), |
| 34 | + success: vi.fn(), |
| 35 | + info: vi.fn(), |
| 36 | +})) |
| 37 | +vi.mock('@socketsecurity/lib/logger', () => ({ |
| 38 | + getDefaultLogger: () => mockLogger, |
| 39 | +})) |
| 40 | + |
| 41 | +// Mock utilities. |
| 42 | +vi.mock('../../../../src/utils/error/fail-msg-with-badge.mts', () => ({ |
| 43 | + failMsgWithBadge: (msg: string, cause?: string) => |
| 44 | + cause ? `${msg}: ${cause}` : msg, |
| 45 | +})) |
| 46 | + |
| 47 | +vi.mock('../../../../src/utils/output/markdown.mts', () => ({ |
| 48 | + mdHeader: (text: string) => `# ${text}`, |
| 49 | +})) |
| 50 | + |
| 51 | +vi.mock('../../../../src/utils/output/result-json.mjs', () => ({ |
| 52 | + serializeResultJson: (result: unknown) => JSON.stringify(result, null, 2), |
| 53 | +})) |
| 54 | + |
| 55 | +const mockGetVisibleTokenPrefix = vi.hoisted(() => vi.fn(() => 'sk_live_')) |
| 56 | +vi.mock('../../../../src/utils/socket/sdk.mjs', () => ({ |
| 57 | + getVisibleTokenPrefix: mockGetVisibleTokenPrefix, |
| 58 | +})) |
| 59 | + |
| 60 | +import { outputOrganizationList } from '../../../../src/commands/organization/output-organization-list.mts' |
| 61 | + |
| 62 | +import type { OrganizationsCResult } from '../../../../src/commands/organization/fetch-organization-list.mts' |
| 63 | + |
| 64 | +describe('output-organization-list', () => { |
| 65 | + beforeEach(() => { |
| 66 | + vi.clearAllMocks() |
| 67 | + process.exitCode = undefined |
| 68 | + }) |
| 69 | + |
| 70 | + describe('outputOrganizationList', () => { |
| 71 | + const mockOrganizations = [ |
| 72 | + { id: 'org-1', name: 'My Org', slug: 'my-org', plan: 'pro' }, |
| 73 | + { id: 'org-2', name: 'Other Org', slug: 'other-org', plan: 'free' }, |
| 74 | + ] |
| 75 | + |
| 76 | + describe('JSON output', () => { |
| 77 | + it('outputs success result as JSON', async () => { |
| 78 | + const result: OrganizationsCResult = { |
| 79 | + ok: true, |
| 80 | + data: { organizations: mockOrganizations }, |
| 81 | + } |
| 82 | + |
| 83 | + await outputOrganizationList(result, 'json') |
| 84 | + |
| 85 | + expect(mockLogger.log).toHaveBeenCalledWith( |
| 86 | + expect.stringContaining('"ok": true'), |
| 87 | + ) |
| 88 | + }) |
| 89 | + |
| 90 | + it('outputs error result as JSON', async () => { |
| 91 | + const result: OrganizationsCResult = { |
| 92 | + ok: false, |
| 93 | + message: 'Failed to fetch', |
| 94 | + } |
| 95 | + |
| 96 | + await outputOrganizationList(result, 'json') |
| 97 | + |
| 98 | + expect(mockLogger.log).toHaveBeenCalledWith( |
| 99 | + expect.stringContaining('"ok": false'), |
| 100 | + ) |
| 101 | + expect(process.exitCode).toBe(1) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe('Text output', () => { |
| 106 | + it('outputs organization list in text format', async () => { |
| 107 | + const result: OrganizationsCResult = { |
| 108 | + ok: true, |
| 109 | + data: { organizations: mockOrganizations }, |
| 110 | + } |
| 111 | + |
| 112 | + await outputOrganizationList(result, 'text') |
| 113 | + |
| 114 | + const logs = mockLogger.log.mock.calls.map(c => c[0]).join('\n') |
| 115 | + expect(logs).toContain('sk_live_') |
| 116 | + expect(logs).toContain('My Org') |
| 117 | + expect(logs).toContain('Other Org') |
| 118 | + }) |
| 119 | + |
| 120 | + it('outputs error with fail message', async () => { |
| 121 | + const result: OrganizationsCResult = { |
| 122 | + ok: false, |
| 123 | + message: 'Authentication failed', |
| 124 | + cause: 'Invalid token', |
| 125 | + } |
| 126 | + |
| 127 | + await outputOrganizationList(result, 'text') |
| 128 | + |
| 129 | + expect(mockLogger.fail).toHaveBeenCalledWith( |
| 130 | + expect.stringContaining('Authentication failed'), |
| 131 | + ) |
| 132 | + expect(process.exitCode).toBe(1) |
| 133 | + }) |
| 134 | + |
| 135 | + it('uses custom exit code when provided', async () => { |
| 136 | + const result: OrganizationsCResult = { |
| 137 | + ok: false, |
| 138 | + message: 'Rate limited', |
| 139 | + code: 429, |
| 140 | + } |
| 141 | + |
| 142 | + await outputOrganizationList(result, 'text') |
| 143 | + |
| 144 | + expect(process.exitCode).toBe(429) |
| 145 | + }) |
| 146 | + }) |
| 147 | + |
| 148 | + describe('Markdown output', () => { |
| 149 | + it('outputs organization list as markdown table', async () => { |
| 150 | + const result: OrganizationsCResult = { |
| 151 | + ok: true, |
| 152 | + data: { organizations: mockOrganizations }, |
| 153 | + } |
| 154 | + |
| 155 | + await outputOrganizationList(result, 'markdown') |
| 156 | + |
| 157 | + const logs = mockLogger.log.mock.calls.map(c => c[0]).join('\n') |
| 158 | + expect(logs).toContain('# Organizations') |
| 159 | + expect(logs).toContain('Name') |
| 160 | + expect(logs).toContain('ID') |
| 161 | + expect(logs).toContain('Plan') |
| 162 | + expect(logs).toContain('My Org') |
| 163 | + expect(logs).toContain('org-1') |
| 164 | + }) |
| 165 | + |
| 166 | + it('handles organizations with missing name', async () => { |
| 167 | + const result: OrganizationsCResult = { |
| 168 | + ok: true, |
| 169 | + data: { |
| 170 | + organizations: [ |
| 171 | + { id: 'org-1', name: undefined as any, slug: 'my-org', plan: 'pro' }, |
| 172 | + ], |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + await outputOrganizationList(result, 'markdown') |
| 177 | + |
| 178 | + // Should not throw. |
| 179 | + expect(mockLogger.log).toHaveBeenCalled() |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + describe('Default output', () => { |
| 184 | + it('defaults to text output', async () => { |
| 185 | + const result: OrganizationsCResult = { |
| 186 | + ok: true, |
| 187 | + data: { organizations: mockOrganizations }, |
| 188 | + } |
| 189 | + |
| 190 | + await outputOrganizationList(result) |
| 191 | + |
| 192 | + const logs = mockLogger.log.mock.calls.map(c => c[0]).join('\n') |
| 193 | + expect(logs).toContain('Name:') |
| 194 | + expect(logs).toContain('ID:') |
| 195 | + expect(logs).toContain('Plan:') |
| 196 | + }) |
| 197 | + }) |
| 198 | + }) |
| 199 | +}) |
0 commit comments