|
| 1 | +import { Command } from 'commander'; |
| 2 | +import consola from 'consola'; |
| 3 | +import { http, HttpResponse } from 'msw'; |
| 4 | +import { afterAll, afterEach, beforeAll, expect, MockInstance, test, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { link } from '../../src/commands/link'; |
| 7 | +import { mkTempDir } from '../../src/lib/mk-temp-dir'; |
| 8 | +import { ProjectConfig } from '../../src/lib/project-config'; |
| 9 | +import { program } from '../../src/program'; |
| 10 | +import { server } from '../mocks/node'; |
| 11 | + |
| 12 | +let exitMock: MockInstance; |
| 13 | + |
| 14 | +let tmpDir: string; |
| 15 | +let cleanup: () => Promise<void>; |
| 16 | +let config: ProjectConfig; |
| 17 | + |
| 18 | +const projectUuid1 = 'a23f5785-fd99-4a94-9fb3-945551623923'; |
| 19 | +const projectUuid2 = 'b23f5785-fd99-4a94-9fb3-945551623924'; |
| 20 | +const storeHash = 'test-store'; |
| 21 | +const accessToken = 'test-token'; |
| 22 | + |
| 23 | +beforeAll(async () => { |
| 24 | + consola.mockTypes(() => vi.fn()); |
| 25 | + |
| 26 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 27 | + exitMock = vi.spyOn(process, 'exit').mockImplementation(() => null as never); |
| 28 | + |
| 29 | + [tmpDir, cleanup] = await mkTempDir(); |
| 30 | + |
| 31 | + config = new ProjectConfig(tmpDir); |
| 32 | +}); |
| 33 | + |
| 34 | +afterEach(() => { |
| 35 | + vi.clearAllMocks(); |
| 36 | +}); |
| 37 | + |
| 38 | +afterAll(async () => { |
| 39 | + vi.restoreAllMocks(); |
| 40 | + exitMock.mockRestore(); |
| 41 | + |
| 42 | + await cleanup(); |
| 43 | +}); |
| 44 | + |
| 45 | +test('properly configured Command instance', () => { |
| 46 | + expect(link).toBeInstanceOf(Command); |
| 47 | + expect(link.name()).toBe('link'); |
| 48 | + expect(link.description()).toBe( |
| 49 | + 'Link your local Catalyst project to a BigCommerce headless project. You can provide a project UUID directly, or fetch and select from available projects using your store credentials.', |
| 50 | + ); |
| 51 | + expect(link.options).toEqual( |
| 52 | + expect.arrayContaining([ |
| 53 | + expect.objectContaining({ flags: '--store-hash <hash>' }), |
| 54 | + expect.objectContaining({ flags: '--access-token <token>' }), |
| 55 | + expect.objectContaining({ flags: '--api-host <host>', defaultValue: 'api.bigcommerce.com' }), |
| 56 | + expect.objectContaining({ flags: '--project-uuid <uuid>' }), |
| 57 | + expect.objectContaining({ flags: '--root-dir <path>', defaultValue: process.cwd() }), |
| 58 | + ]), |
| 59 | + ); |
| 60 | +}); |
| 61 | + |
| 62 | +test('sets projectUuid when called with --project-uuid', async () => { |
| 63 | + await program.parseAsync([ |
| 64 | + 'node', |
| 65 | + 'catalyst', |
| 66 | + 'link', |
| 67 | + '--project-uuid', |
| 68 | + projectUuid1, |
| 69 | + '--root-dir', |
| 70 | + tmpDir, |
| 71 | + ]); |
| 72 | + |
| 73 | + expect(consola.start).toHaveBeenCalledWith( |
| 74 | + 'Writing project UUID to .bigcommerce/project.json...', |
| 75 | + ); |
| 76 | + expect(consola.success).toHaveBeenCalledWith( |
| 77 | + 'Project UUID written to .bigcommerce/project.json.', |
| 78 | + ); |
| 79 | + expect(exitMock).toHaveBeenCalledWith(0); |
| 80 | + expect(config.get('projectUuid')).toBe(projectUuid1); |
| 81 | + expect(config.get('framework')).toBe('catalyst'); |
| 82 | +}); |
| 83 | + |
| 84 | +test('fetches projects and prompts user to select one', async () => { |
| 85 | + const consolaPromptMock = vi |
| 86 | + .spyOn(consola, 'prompt') |
| 87 | + .mockImplementation(async (message, opts) => { |
| 88 | + // Assert the prompt message and options |
| 89 | + expect(message).toContain('Select a project (Press <enter> to select).'); |
| 90 | + |
| 91 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 92 | + const options = (opts as { options: Array<{ label: string; value: string }> }).options; |
| 93 | + |
| 94 | + expect(options).toHaveLength(2); |
| 95 | + expect(options[0]).toMatchObject({ label: 'Project One', value: projectUuid1 }); |
| 96 | + expect(options[1]).toMatchObject({ |
| 97 | + label: 'Project Two', |
| 98 | + value: projectUuid2, |
| 99 | + }); |
| 100 | + |
| 101 | + // Simulate selecting the second option |
| 102 | + return new Promise((resolve) => resolve(projectUuid2)); |
| 103 | + }); |
| 104 | + |
| 105 | + await program.parseAsync([ |
| 106 | + 'node', |
| 107 | + 'catalyst', |
| 108 | + 'link', |
| 109 | + '--store-hash', |
| 110 | + storeHash, |
| 111 | + '--access-token', |
| 112 | + accessToken, |
| 113 | + '--root-dir', |
| 114 | + tmpDir, |
| 115 | + ]); |
| 116 | + |
| 117 | + expect(consola.start).toHaveBeenCalledWith('Fetching projects...'); |
| 118 | + expect(consola.success).toHaveBeenCalledWith('Projects fetched.'); |
| 119 | + |
| 120 | + expect(consola.start).toHaveBeenCalledWith( |
| 121 | + 'Writing project UUID to .bigcommerce/project.json...', |
| 122 | + ); |
| 123 | + expect(consola.success).toHaveBeenCalledWith( |
| 124 | + 'Project UUID written to .bigcommerce/project.json.', |
| 125 | + ); |
| 126 | + |
| 127 | + expect(exitMock).toHaveBeenCalledWith(0); |
| 128 | + |
| 129 | + expect(config.get('projectUuid')).toBe(projectUuid2); |
| 130 | + expect(config.get('framework')).toBe('catalyst'); |
| 131 | + |
| 132 | + consolaPromptMock.mockRestore(); |
| 133 | +}); |
| 134 | + |
| 135 | +test('errors when no projects are found', async () => { |
| 136 | + server.use( |
| 137 | + http.get('https://:apiHost/stores/:storeHash/v3/headless/projects', () => |
| 138 | + HttpResponse.json({ |
| 139 | + data: [], |
| 140 | + }), |
| 141 | + ), |
| 142 | + ); |
| 143 | + |
| 144 | + await program.parseAsync([ |
| 145 | + 'node', |
| 146 | + 'catalyst', |
| 147 | + 'link', |
| 148 | + '--store-hash', |
| 149 | + storeHash, |
| 150 | + '--access-token', |
| 151 | + accessToken, |
| 152 | + '--root-dir', |
| 153 | + tmpDir, |
| 154 | + ]); |
| 155 | + |
| 156 | + expect(consola.start).toHaveBeenCalledWith('Fetching projects...'); |
| 157 | + expect(consola.error).toHaveBeenCalledWith('No headless projects found for this store.'); |
| 158 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 159 | +}); |
| 160 | + |
| 161 | +test('errors when headless projects API is not found', async () => { |
| 162 | + server.use( |
| 163 | + http.get('https://:apiHost/stores/:storeHash/v3/headless/projects', () => |
| 164 | + HttpResponse.json({}, { status: 404 }), |
| 165 | + ), |
| 166 | + ); |
| 167 | + await program.parseAsync([ |
| 168 | + 'node', |
| 169 | + 'catalyst', |
| 170 | + 'link', |
| 171 | + '--store-hash', |
| 172 | + storeHash, |
| 173 | + '--access-token', |
| 174 | + accessToken, |
| 175 | + '--root-dir', |
| 176 | + tmpDir, |
| 177 | + ]); |
| 178 | + |
| 179 | + expect(consola.start).toHaveBeenCalledWith('Fetching projects...'); |
| 180 | + expect(consola.error).toHaveBeenCalledWith( |
| 181 | + 'Headless Projects API not enabled. If you are part of the alpha, contact support@bigcommerce.com to enable it.', |
| 182 | + ); |
| 183 | +}); |
| 184 | + |
| 185 | +test('errors when no projectUuid, storeHash, or accessToken are provided', async () => { |
| 186 | + await program.parseAsync(['node', 'catalyst', 'link', '--root-dir', tmpDir]); |
| 187 | + |
| 188 | + expect(consola.start).not.toHaveBeenCalled(); |
| 189 | + expect(consola.success).not.toHaveBeenCalled(); |
| 190 | + expect(consola.error).toHaveBeenCalledWith('Insufficient information to link a project.'); |
| 191 | + expect(consola.info).toHaveBeenCalledWith('Provide a project UUID with --project-uuid, or'); |
| 192 | + expect(consola.info).toHaveBeenCalledWith( |
| 193 | + 'Provide both --store-hash and --access-token to fetch and select a project.', |
| 194 | + ); |
| 195 | + |
| 196 | + expect(exitMock).toHaveBeenCalledWith(1); |
| 197 | +}); |
0 commit comments