|
| 1 | +import { |
| 2 | + afterEach, |
| 3 | + beforeEach, |
| 4 | + describe, |
| 5 | + expect, |
| 6 | + spyOn, |
| 7 | + test, |
| 8 | +} from 'bun:test'; |
| 9 | +import crypto from 'crypto'; |
| 10 | + |
| 11 | +import * as api from '../src/api'; |
| 12 | +import * as utils from '../src/utils'; |
| 13 | +import { userCommands } from '../src/user'; |
| 14 | + |
| 15 | +function md5(str: string) { |
| 16 | + return crypto.createHash('md5').update(str).digest('hex'); |
| 17 | +} |
| 18 | + |
| 19 | +describe('userCommands.login', () => { |
| 20 | + let consoleSpy: ReturnType<typeof spyOn>; |
| 21 | + let postSpy: ReturnType<typeof spyOn>; |
| 22 | + let replaceSessionSpy: ReturnType<typeof spyOn>; |
| 23 | + let saveSessionSpy: ReturnType<typeof spyOn>; |
| 24 | + let questionSpy: ReturnType<typeof spyOn>; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + consoleSpy = spyOn(console, 'log').mockImplementation(() => {}); |
| 28 | + postSpy = spyOn(api, 'post').mockResolvedValue({ |
| 29 | + token: 'session-token-abc', |
| 30 | + info: { name: 'TestUser', email: 'test@example.com' }, |
| 31 | + }); |
| 32 | + replaceSessionSpy = spyOn(api, 'replaceSession').mockImplementation( |
| 33 | + () => {}, |
| 34 | + ); |
| 35 | + saveSessionSpy = spyOn(api, 'saveSession').mockResolvedValue(undefined); |
| 36 | + questionSpy = spyOn(utils, 'question').mockResolvedValue('fallback'); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + consoleSpy.mockRestore(); |
| 41 | + postSpy.mockRestore(); |
| 42 | + replaceSessionSpy.mockRestore(); |
| 43 | + saveSessionSpy.mockRestore(); |
| 44 | + questionSpy.mockRestore(); |
| 45 | + }); |
| 46 | + |
| 47 | + test('calls post with /user/login and md5-hashes the password', async () => { |
| 48 | + await userCommands.login({ |
| 49 | + args: ['user@example.com', 'mypassword'], |
| 50 | + }); |
| 51 | + |
| 52 | + expect(postSpy).toHaveBeenCalledWith('/user/login', { |
| 53 | + email: 'user@example.com', |
| 54 | + pwd: md5('mypassword'), |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + test('md5 hash is a valid 32-char hex string', async () => { |
| 59 | + await userCommands.login({ |
| 60 | + args: ['user@example.com', 'secret123'], |
| 61 | + }); |
| 62 | + |
| 63 | + const callArgs = postSpy.mock.calls[0]; |
| 64 | + const pwdHash = callArgs[1].pwd as string; |
| 65 | + expect(pwdHash).toHaveLength(32); |
| 66 | + expect(pwdHash).toMatch(/^[0-9a-f]{32}$/); |
| 67 | + expect(pwdHash).toBe(md5('secret123')); |
| 68 | + }); |
| 69 | + |
| 70 | + test('calls replaceSession with the returned token', async () => { |
| 71 | + await userCommands.login({ |
| 72 | + args: ['user@example.com', 'mypassword'], |
| 73 | + }); |
| 74 | + |
| 75 | + expect(replaceSessionSpy).toHaveBeenCalledWith({ |
| 76 | + token: 'session-token-abc', |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + test('calls saveSession after replaceSession', async () => { |
| 81 | + await userCommands.login({ |
| 82 | + args: ['user@example.com', 'mypassword'], |
| 83 | + }); |
| 84 | + |
| 85 | + expect(saveSessionSpy).toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + test('prompts for email and password when args are missing', async () => { |
| 89 | + let callCount = 0; |
| 90 | + questionSpy.mockImplementation(async (prompt: string) => { |
| 91 | + callCount++; |
| 92 | + if (prompt === 'email:') return 'asked@email.com'; |
| 93 | + return 'asked-password'; |
| 94 | + }); |
| 95 | + |
| 96 | + await userCommands.login({ args: [] }); |
| 97 | + |
| 98 | + expect(questionSpy).toHaveBeenCalledTimes(2); |
| 99 | + expect(postSpy).toHaveBeenCalledWith('/user/login', { |
| 100 | + email: 'asked@email.com', |
| 101 | + pwd: md5('asked-password'), |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + test('logs welcome message with user name', async () => { |
| 106 | + await userCommands.login({ |
| 107 | + args: ['user@example.com', 'mypassword'], |
| 108 | + }); |
| 109 | + |
| 110 | + expect(consoleSpy).toHaveBeenCalled(); |
| 111 | + // The welcome message includes the user's name |
| 112 | + const logOutput = consoleSpy.mock.calls[0][0] as string; |
| 113 | + expect(logOutput).toContain('TestUser'); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('userCommands.logout', () => { |
| 118 | + let consoleSpy: ReturnType<typeof spyOn>; |
| 119 | + let closeSessionSpy: ReturnType<typeof spyOn>; |
| 120 | + |
| 121 | + beforeEach(() => { |
| 122 | + consoleSpy = spyOn(console, 'log').mockImplementation(() => {}); |
| 123 | + closeSessionSpy = spyOn(api, 'closeSession').mockImplementation( |
| 124 | + () => {}, |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + afterEach(() => { |
| 129 | + consoleSpy.mockRestore(); |
| 130 | + closeSessionSpy.mockRestore(); |
| 131 | + }); |
| 132 | + |
| 133 | + test('calls closeSession', async () => { |
| 134 | + await userCommands.logout({} as any); |
| 135 | + |
| 136 | + expect(closeSessionSpy).toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + test('logs a message after logout', async () => { |
| 140 | + await userCommands.logout({} as any); |
| 141 | + |
| 142 | + expect(consoleSpy).toHaveBeenCalled(); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('userCommands.me', () => { |
| 147 | + let consoleSpy: ReturnType<typeof spyOn>; |
| 148 | + let getSpy: ReturnType<typeof spyOn>; |
| 149 | + |
| 150 | + beforeEach(() => { |
| 151 | + consoleSpy = spyOn(console, 'log').mockImplementation(() => {}); |
| 152 | + getSpy = spyOn(api, 'get').mockResolvedValue({ |
| 153 | + ok: true, |
| 154 | + name: 'TestUser', |
| 155 | + email: 'test@example.com', |
| 156 | + id: '12345', |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + afterEach(() => { |
| 161 | + consoleSpy.mockRestore(); |
| 162 | + getSpy.mockRestore(); |
| 163 | + }); |
| 164 | + |
| 165 | + test('calls get with /user/me', async () => { |
| 166 | + await userCommands.me(); |
| 167 | + |
| 168 | + expect(getSpy).toHaveBeenCalledWith('/user/me'); |
| 169 | + }); |
| 170 | + |
| 171 | + test('logs each field except "ok"', async () => { |
| 172 | + await userCommands.me(); |
| 173 | + |
| 174 | + // Should log name, email, id but NOT ok |
| 175 | + const logCalls = consoleSpy.mock.calls.map((c) => c[0] as string); |
| 176 | + expect(logCalls).toContain('name: TestUser'); |
| 177 | + expect(logCalls).toContain('email: test@example.com'); |
| 178 | + expect(logCalls).toContain('id: 12345'); |
| 179 | + |
| 180 | + // Should not log the "ok" field |
| 181 | + const hasOk = logCalls.some((msg) => msg.startsWith('ok:')); |
| 182 | + expect(hasOk).toBe(false); |
| 183 | + }); |
| 184 | + |
| 185 | + test('skips the "ok" field when logging', async () => { |
| 186 | + await userCommands.me(); |
| 187 | + |
| 188 | + const logCalls = consoleSpy.mock.calls.map((c) => c[0] as string); |
| 189 | + for (const msg of logCalls) { |
| 190 | + expect(msg).not.toMatch(/^ok:/); |
| 191 | + } |
| 192 | + }); |
| 193 | +}); |
0 commit comments