|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { main, runSmartSig } from '../../src/cli.ts' |
| 3 | +import { Transloadit } from '../../src/Transloadit.ts' |
| 4 | + |
| 5 | +const mockedExpiresDate = '2025-01-01T00:00:00.000Z' |
| 6 | +const mockExpires = () => |
| 7 | + vi |
| 8 | + .spyOn(Transloadit.prototype as unknown as { _getExpiresDate: () => string }, '_getExpiresDate') |
| 9 | + .mockReturnValue(mockedExpiresDate) |
| 10 | + |
| 11 | +const resetExitCode = () => { |
| 12 | + process.exitCode = undefined |
| 13 | +} |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + vi.restoreAllMocks() |
| 17 | + vi.unstubAllEnvs() |
| 18 | + resetExitCode() |
| 19 | +}) |
| 20 | + |
| 21 | +describe('cli smart_sig', () => { |
| 22 | + it('prints signature JSON built from stdin params', async () => { |
| 23 | + mockExpires() |
| 24 | + vi.stubEnv('TRANSLOADIT_KEY', 'key') |
| 25 | + vi.stubEnv('TRANSLOADIT_SECRET', 'secret') |
| 26 | + |
| 27 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 28 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 29 | + |
| 30 | + const params = { template_id: '123' } |
| 31 | + await runSmartSig(JSON.stringify(params)) |
| 32 | + |
| 33 | + expect(stderrSpy).not.toHaveBeenCalled() |
| 34 | + expect(stdoutSpy).toHaveBeenCalledTimes(1) |
| 35 | + const output = stdoutSpy.mock.calls[0]?.[0] |
| 36 | + const parsed = JSON.parse(`${output}`.trim()) |
| 37 | + |
| 38 | + const client = new Transloadit({ authKey: 'key', authSecret: 'secret' }) |
| 39 | + const expected = client.calcSignature({ template_id: '123' }) |
| 40 | + expect(parsed).toEqual(expected) |
| 41 | + expect(process.exitCode).toBeUndefined() |
| 42 | + |
| 43 | + }) |
| 44 | + |
| 45 | + it('fails when credentials are missing', async () => { |
| 46 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 47 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 48 | + |
| 49 | + await runSmartSig('{}') |
| 50 | + |
| 51 | + expect(stdoutSpy).not.toHaveBeenCalled() |
| 52 | + expect(stderrSpy).toHaveBeenCalledWith( |
| 53 | + 'Missing credentials. Please set TRANSLOADIT_KEY and TRANSLOADIT_SECRET environment variables.', |
| 54 | + ) |
| 55 | + expect(process.exitCode).toBe(1) |
| 56 | + |
| 57 | + }) |
| 58 | + |
| 59 | + it('fails when stdin is not valid JSON', async () => { |
| 60 | + mockExpires() |
| 61 | + vi.stubEnv('TRANSLOADIT_KEY', 'key') |
| 62 | + vi.stubEnv('TRANSLOADIT_SECRET', 'secret') |
| 63 | + |
| 64 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 65 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 66 | + |
| 67 | + await runSmartSig('this is not json') |
| 68 | + |
| 69 | + expect(stdoutSpy).not.toHaveBeenCalled() |
| 70 | + expect(stderrSpy).toHaveBeenCalled() |
| 71 | + expect(stderrSpy.mock.calls[0]?.[0]).toContain('Failed to parse JSON from stdin') |
| 72 | + expect(process.exitCode).toBe(1) |
| 73 | + |
| 74 | + }) |
| 75 | + |
| 76 | + it('fails when params are not an object', async () => { |
| 77 | + mockExpires() |
| 78 | + vi.stubEnv('TRANSLOADIT_KEY', 'key') |
| 79 | + vi.stubEnv('TRANSLOADIT_SECRET', 'secret') |
| 80 | + |
| 81 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 82 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 83 | + |
| 84 | + await runSmartSig('[]') |
| 85 | + |
| 86 | + expect(stdoutSpy).not.toHaveBeenCalled() |
| 87 | + expect(stderrSpy).toHaveBeenCalledWith('Invalid params provided via stdin. Expected a JSON object.') |
| 88 | + expect(process.exitCode).toBe(1) |
| 89 | + |
| 90 | + }) |
| 91 | + |
| 92 | + it('prints usage when no command is provided', async () => { |
| 93 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 94 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 95 | + |
| 96 | + await main([]) |
| 97 | + |
| 98 | + expect(stderrSpy).not.toHaveBeenCalled() |
| 99 | + expect(stdoutSpy).toHaveBeenCalled() |
| 100 | + const message = `${stdoutSpy.mock.calls[0]?.[0]}` |
| 101 | + expect(message).toContain('Usage:') |
| 102 | + expect(message).toContain('npx transloadit smart_sig') |
| 103 | + expect(process.exitCode).toBe(1) |
| 104 | + |
| 105 | + }) |
| 106 | +}) |
0 commit comments