|
| 1 | +import { TwistRequestError } from '@doist/twist-sdk' |
| 2 | +import { Command } from 'commander' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +const apiMocks = vi.hoisted(() => ({ |
| 6 | + getSessionUser: vi.fn(), |
| 7 | + getTwistClient: vi.fn(), |
| 8 | + updateUser: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('../lib/api.js', () => ({ |
| 12 | + getSessionUser: apiMocks.getSessionUser, |
| 13 | + getTwistClient: apiMocks.getTwistClient, |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('chalk') |
| 17 | + |
| 18 | +import { registerAwayCommand } from '../commands/away.js' |
| 19 | + |
| 20 | +function createProgram() { |
| 21 | + const program = new Command() |
| 22 | + program.exitOverride() |
| 23 | + registerAwayCommand(program) |
| 24 | + return program |
| 25 | +} |
| 26 | + |
| 27 | +describe('away', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks() |
| 30 | + apiMocks.updateUser.mockResolvedValue({ |
| 31 | + id: 1, |
| 32 | + name: 'Test User', |
| 33 | + email: 'test@example.com', |
| 34 | + awayMode: null, |
| 35 | + }) |
| 36 | + apiMocks.getTwistClient.mockResolvedValue({ |
| 37 | + users: { update: apiMocks.updateUser }, |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('show', () => { |
| 42 | + it('shows not away when awayMode is null', async () => { |
| 43 | + apiMocks.getSessionUser.mockResolvedValue({ |
| 44 | + id: 1, |
| 45 | + name: 'Test User', |
| 46 | + awayMode: null, |
| 47 | + }) |
| 48 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 49 | + const program = createProgram() |
| 50 | + |
| 51 | + await program.parseAsync(['node', 'tw', 'away']) |
| 52 | + |
| 53 | + expect(logSpy).toHaveBeenCalledWith('Not away.') |
| 54 | + logSpy.mockRestore() |
| 55 | + }) |
| 56 | + |
| 57 | + it('shows away status when set', async () => { |
| 58 | + apiMocks.getSessionUser.mockResolvedValue({ |
| 59 | + id: 1, |
| 60 | + name: 'Test User', |
| 61 | + awayMode: { type: 'vacation', dateFrom: '2026-03-10', dateTo: '2026-03-20' }, |
| 62 | + }) |
| 63 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 64 | + const program = createProgram() |
| 65 | + |
| 66 | + await program.parseAsync(['node', 'tw', 'away']) |
| 67 | + |
| 68 | + expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('Vacation')) |
| 69 | + logSpy.mockRestore() |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe('set', () => { |
| 74 | + it('calls users.update with awayMode', async () => { |
| 75 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 76 | + const program = createProgram() |
| 77 | + |
| 78 | + await program.parseAsync(['node', 'tw', 'away', 'set', 'vacation', '2026-03-20']) |
| 79 | + |
| 80 | + expect(apiMocks.updateUser).toHaveBeenCalledWith( |
| 81 | + expect.objectContaining({ |
| 82 | + awayMode: expect.objectContaining({ |
| 83 | + type: 'vacation', |
| 84 | + dateTo: '2026-03-20', |
| 85 | + }), |
| 86 | + }), |
| 87 | + ) |
| 88 | + logSpy.mockRestore() |
| 89 | + }) |
| 90 | + |
| 91 | + it('supports --from flag', async () => { |
| 92 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 93 | + const program = createProgram() |
| 94 | + |
| 95 | + await program.parseAsync([ |
| 96 | + 'node', |
| 97 | + 'tw', |
| 98 | + 'away', |
| 99 | + 'set', |
| 100 | + 'vacation', |
| 101 | + '2026-03-20', |
| 102 | + '--from', |
| 103 | + '2026-03-15', |
| 104 | + ]) |
| 105 | + |
| 106 | + expect(apiMocks.updateUser).toHaveBeenCalledWith({ |
| 107 | + awayMode: { type: 'vacation', dateFrom: '2026-03-15', dateTo: '2026-03-20' }, |
| 108 | + }) |
| 109 | + logSpy.mockRestore() |
| 110 | + }) |
| 111 | + |
| 112 | + it('shows dry-run message', async () => { |
| 113 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 114 | + const program = createProgram() |
| 115 | + |
| 116 | + await program.parseAsync([ |
| 117 | + 'node', |
| 118 | + 'tw', |
| 119 | + 'away', |
| 120 | + 'set', |
| 121 | + 'vacation', |
| 122 | + '2026-03-20', |
| 123 | + '--dry-run', |
| 124 | + ]) |
| 125 | + |
| 126 | + expect(apiMocks.updateUser).not.toHaveBeenCalled() |
| 127 | + expect(logSpy).toHaveBeenCalledWith(expect.stringContaining('Dry run')) |
| 128 | + logSpy.mockRestore() |
| 129 | + }) |
| 130 | + |
| 131 | + it('shows friendly error on insufficient scope (403)', async () => { |
| 132 | + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 133 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 134 | + throw new Error('process.exit') |
| 135 | + }) |
| 136 | + apiMocks.updateUser.mockRejectedValue( |
| 137 | + new TwistRequestError('Request failed with status 403', 403, { |
| 138 | + error_code: 109, |
| 139 | + error_string: 'Insufficient scope provided: user:write', |
| 140 | + }), |
| 141 | + ) |
| 142 | + const program = createProgram() |
| 143 | + |
| 144 | + await expect( |
| 145 | + program.parseAsync(['node', 'tw', 'away', 'set', 'vacation', '2026-03-20']), |
| 146 | + ).rejects.toThrow() |
| 147 | + |
| 148 | + expect(errorSpy).toHaveBeenCalledWith( |
| 149 | + expect.anything(), |
| 150 | + 'The away status feature requires additional permissions.', |
| 151 | + ) |
| 152 | + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('tw auth login')) |
| 153 | + errorSpy.mockRestore() |
| 154 | + exitSpy.mockRestore() |
| 155 | + }) |
| 156 | + |
| 157 | + it('rejects invalid away type', async () => { |
| 158 | + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 159 | + const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 160 | + throw new Error('process.exit') |
| 161 | + }) |
| 162 | + const program = createProgram() |
| 163 | + |
| 164 | + await expect( |
| 165 | + program.parseAsync(['node', 'tw', 'away', 'set', 'invalid', '2026-03-20']), |
| 166 | + ).rejects.toThrow() |
| 167 | + |
| 168 | + expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid away type')) |
| 169 | + errorSpy.mockRestore() |
| 170 | + exitSpy.mockRestore() |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + describe('clear', () => { |
| 175 | + it('calls users.update with empty string awayMode to clear', async () => { |
| 176 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 177 | + const program = createProgram() |
| 178 | + |
| 179 | + await program.parseAsync(['node', 'tw', 'away', 'clear']) |
| 180 | + |
| 181 | + expect(apiMocks.updateUser).toHaveBeenCalledWith({ awayMode: '' }) |
| 182 | + expect(logSpy).toHaveBeenCalledWith('Away status cleared.') |
| 183 | + logSpy.mockRestore() |
| 184 | + }) |
| 185 | + |
| 186 | + it('shows dry-run message', async () => { |
| 187 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 188 | + const program = createProgram() |
| 189 | + |
| 190 | + await program.parseAsync(['node', 'tw', 'away', 'clear', '--dry-run']) |
| 191 | + |
| 192 | + expect(apiMocks.updateUser).not.toHaveBeenCalled() |
| 193 | + expect(logSpy).toHaveBeenCalledWith('Dry run: would clear away status') |
| 194 | + logSpy.mockRestore() |
| 195 | + }) |
| 196 | + }) |
| 197 | +}) |
0 commit comments