|
| 1 | +import DocSearch from './search.js' |
| 2 | +import {docSearchJsonOutputSchema} from '../../services/commands/doc/types.js' |
| 3 | +import {shopifyFetch, Response} from '@shopify/cli-kit/node/http' |
| 4 | +import {launchCLI} from '@shopify/cli-kit/node/cli-launcher' |
| 5 | +import {ShopifyConfig} from '@shopify/cli-kit/node/custom-oclif-loader' |
| 6 | +import {mockAndCaptureOutput, withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output' |
| 7 | +import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' |
| 8 | + |
| 9 | +vi.mock('@shopify/cli-kit/node/http', async (importOriginal) => ({ |
| 10 | + ...(await importOriginal<typeof import('@shopify/cli-kit/node/http')>()), |
| 11 | + shopifyFetch: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +const entry = {score: 0.99, content: 'About webhooks', url: 'https://shopify.dev/x', title: 'Webhooks', domain: null} |
| 15 | +const body = `[\n {"title":"Webhooks","url":"https://shopify.dev/x","content":"About webhooks","score":0.99,"domain":null}\n]` |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + vi.stubEnv('CI', '1') |
| 19 | + vi.stubEnv('SHOPIFY_CLI_NO_ANALYTICS', '1') |
| 20 | + vi.mocked(shopifyFetch).mockResolvedValue(new Response(body)) |
| 21 | + vi.spyOn(process, 'exit').mockReturnValue(undefined as never) |
| 22 | +}) |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + vi.unstubAllEnvs() |
| 26 | + mockAndCaptureOutput().clear() |
| 27 | +}) |
| 28 | + |
| 29 | +describe('doc search command', () => { |
| 30 | + test('preserves the exact response bytes on stdout without JSON selection', async () => { |
| 31 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 32 | + await DocSearch.run(['--query', 'webhooks', '--no-input'], import.meta.url) |
| 33 | + expect(stdout()).toBe(`${body}\n`) |
| 34 | + expect(stderr()).toBe('') |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + test.each([ |
| 39 | + 'not JSON', |
| 40 | + JSON.stringify([{...entry, title: null}]), |
| 41 | + JSON.stringify([{score: entry.score, content: entry.content, url: entry.url, domain: entry.domain}]), |
| 42 | + ])('preserves raw successful responses outside the JSON schema: %s', async (response) => { |
| 43 | + vi.mocked(shopifyFetch).mockResolvedValue(new Response(response)) |
| 44 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 45 | + await DocSearch.run(['--query', 'webhooks', '--no-input'], import.meta.url) |
| 46 | + expect(stdout()).toBe(`${response}\n`) |
| 47 | + expect(stderr()).toBe('') |
| 48 | + expect(process.exit).not.toHaveBeenCalled() |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + test.each([ |
| 53 | + JSON.stringify([{...entry, title: null}]), |
| 54 | + JSON.stringify([{score: entry.score, content: entry.content, url: entry.url, domain: entry.domain}]), |
| 55 | + ])('reports incompatible search data as one fatal JSON document: %s', async (response) => { |
| 56 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 57 | + vi.mocked(shopifyFetch).mockResolvedValue(new Response(response)) |
| 58 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 59 | + await DocSearch.run(['--query', 'webhooks', '--json'], import.meta.url) |
| 60 | + expect(JSON.parse(stdout())).toEqual({ |
| 61 | + error: {type: 'abort', message: 'Search returned an invalid documentation response.'}, |
| 62 | + }) |
| 63 | + expect(stderr()).toBe('') |
| 64 | + expect(process.exit).toHaveBeenCalledWith(1) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + test.each([{flags: []}, {flags: ['--no-input']}])( |
| 69 | + 'writes one result object with JSON and flags $flags', |
| 70 | + async ({flags}) => { |
| 71 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 72 | + await DocSearch.run( |
| 73 | + ['--query', 'webhooks', '--api-name', 'admin', '--api-version', 'latest', '--json', ...flags], |
| 74 | + import.meta.url, |
| 75 | + ) |
| 76 | + expect(JSON.parse(stdout())).toEqual({results: [entry], pageInfo: {hasNextPage: null}}) |
| 77 | + expect(stderr()).toBe('') |
| 78 | + expect(shopifyFetch).toHaveBeenCalledWith( |
| 79 | + 'https://shopify.dev/assistant/search?query=webhooks&api_name=admin&api_version=latest', |
| 80 | + { |
| 81 | + headers: {Accept: 'application/json', 'X-Shopify-Surface': 'cli'}, |
| 82 | + }, |
| 83 | + ) |
| 84 | + }) |
| 85 | + }, |
| 86 | + ) |
| 87 | + |
| 88 | + test('supports the shared JSON environment flag', async () => { |
| 89 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 90 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 91 | + await DocSearch.run(['--query', 'webhooks'], import.meta.url) |
| 92 | + expect(JSON.parse(stdout())).toEqual({results: [entry], pageInfo: {hasNextPage: null}}) |
| 93 | + expect(stderr()).toBe('') |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + test('reports missing required input before making a request', async () => { |
| 98 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 99 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 100 | + await DocSearch.run(['--json', '--no-input'], import.meta.url) |
| 101 | + expect(JSON.parse(stdout())).toHaveProperty('error') |
| 102 | + expect(process.exit).toHaveBeenCalledWith(2) |
| 103 | + expect(stderr()).toBe('') |
| 104 | + expect(shopifyFetch).not.toHaveBeenCalled() |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + test.each([{results: []}, {results: [{...entry, score: 0, domain: 'admin'}]}])( |
| 109 | + 'encodes empty results and nullable metadata: $results', |
| 110 | + async ({results}) => { |
| 111 | + vi.mocked(shopifyFetch).mockResolvedValue(new Response(JSON.stringify(results))) |
| 112 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 113 | + await DocSearch.run(['--query', 'webhooks', '--json'], import.meta.url) |
| 114 | + expect(JSON.parse(stdout())).toEqual({results, pageInfo: {hasNextPage: null}}) |
| 115 | + expect(stderr()).toBe('') |
| 116 | + }) |
| 117 | + }, |
| 118 | + ) |
| 119 | + |
| 120 | + test.each([ |
| 121 | + { |
| 122 | + status: 400, |
| 123 | + statusText: 'Bad Request', |
| 124 | + response: '{"error":"Invalid api_version"}', |
| 125 | + message: 'Search failed: Invalid api_version', |
| 126 | + }, |
| 127 | + { |
| 128 | + status: 500, |
| 129 | + statusText: 'Internal Server Error', |
| 130 | + response: '<html>nope</html>', |
| 131 | + message: 'Search failed: 500 Internal Server Error', |
| 132 | + }, |
| 133 | + { |
| 134 | + status: 200, |
| 135 | + statusText: 'OK', |
| 136 | + response: 'not JSON', |
| 137 | + message: 'Search returned an invalid documentation response.', |
| 138 | + }, |
| 139 | + ])('reports $status failures through the shared error document', async ({status, statusText, response, message}) => { |
| 140 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 141 | + vi.mocked(shopifyFetch).mockResolvedValue(new Response(response, {status, statusText})) |
| 142 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 143 | + await expect(DocSearch.run(['--query', 'webhooks', '--json'], import.meta.url)).resolves.toBeUndefined() |
| 144 | + expect(process.exit).toHaveBeenCalledWith(1) |
| 145 | + expect(JSON.parse(stdout())).toEqual({error: {type: 'abort', message}}) |
| 146 | + expect(stderr()).toBe('') |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + test('keeps the transport failure guidance and nonzero exit', async () => { |
| 151 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 152 | + vi.mocked(shopifyFetch).mockRejectedValue(new Error('getaddrinfo ENOTFOUND shopify.dev')) |
| 153 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 154 | + await expect(DocSearch.run(['--query', 'webhooks', '--json'], import.meta.url)).resolves.toBeUndefined() |
| 155 | + expect(process.exit).toHaveBeenCalledWith(1) |
| 156 | + expect(JSON.parse(stdout())).toEqual({ |
| 157 | + error: { |
| 158 | + type: 'abort', |
| 159 | + message: 'Could not reach shopify.dev to run the search.', |
| 160 | + tryMessage: 'Check your network connection and try again.', |
| 161 | + }, |
| 162 | + }) |
| 163 | + expect(stderr()).toBe('') |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + test('exposes the schema in help and through the launcher without a search request', async () => { |
| 168 | + expect(DocSearch.jsonOutputSchema).toBe(docSearchJsonOutputSchema) |
| 169 | + expect(DocSearch.flags.json).toBeDefined() |
| 170 | + expect(DocSearch.description).toContain('Output from `--json` conforms to the `DocSearchResult` schema.') |
| 171 | + vi.spyOn(ShopifyConfig.prototype, 'runHook').mockResolvedValue({successes: [], failures: []}) |
| 172 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 173 | + await launchCLI({ |
| 174 | + moduleURL: import.meta.url, |
| 175 | + argv: ['doc', 'search', '--json-schema'], |
| 176 | + lazyCommandLoader: async () => DocSearch, |
| 177 | + }) |
| 178 | + const schema = JSON.parse(stdout()) |
| 179 | + expect(schema.definitions.Result.required).toEqual(['results', 'pageInfo']) |
| 180 | + expect(schema.definitions.Result.properties.results.items.properties.domain.type).toEqual(['string', 'null']) |
| 181 | + expect(schema.definitions.Result.additionalProperties).toBe(false) |
| 182 | + expect(stderr()).toBe('') |
| 183 | + expect(shopifyFetch).not.toHaveBeenCalled() |
| 184 | + }) |
| 185 | + }) |
| 186 | +}) |
0 commit comments