|
| 1 | +import DocFetch from './fetch.js' |
| 2 | +import {docFetchJsonOutputSchema} from '../../services/commands/doc/types.js' |
| 3 | +import {fetch, Response} from '@shopify/cli-kit/node/http' |
| 4 | +import {inTemporaryDirectory, readFile, writeFile, fileExists} from '@shopify/cli-kit/node/fs' |
| 5 | +import {joinPath, relativePath, cwd} from '@shopify/cli-kit/node/path' |
| 6 | +import {launchCLI} from '@shopify/cli-kit/node/cli-launcher' |
| 7 | +import {ShopifyConfig} from '@shopify/cli-kit/node/custom-oclif-loader' |
| 8 | +import {mockAndCaptureOutput, withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output' |
| 9 | +import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' |
| 10 | + |
| 11 | +vi.mock('@shopify/cli-kit/node/http', async (importOriginal) => ({ |
| 12 | + ...(await importOriginal<typeof import('@shopify/cli-kit/node/http')>()), |
| 13 | + fetch: vi.fn(), |
| 14 | +})) |
| 15 | + |
| 16 | +const url = 'https://shopify.dev/docs/api/shopify-cli' |
| 17 | +const content = '# Shopify CLI\n\nA document.\n' |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + vi.stubEnv('CI', '1') |
| 21 | + vi.stubEnv('SHOPIFY_CLI_NO_ANALYTICS', '1') |
| 22 | + vi.mocked(fetch).mockResolvedValue(new Response(content)) |
| 23 | + vi.spyOn(process, 'exit').mockReturnValue(undefined as never) |
| 24 | +}) |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + vi.unstubAllEnvs() |
| 28 | + mockAndCaptureOutput().clear() |
| 29 | +}) |
| 30 | + |
| 31 | +describe('doc fetch command', () => { |
| 32 | + test('preserves the Markdown output and stdout channel', async () => { |
| 33 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 34 | + await DocFetch.run(['--url', url, '--no-input'], import.meta.url) |
| 35 | + expect(stdout()).toBe(`${content}\n`) |
| 36 | + expect(stderr()).toBe('') |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + test.each([{flags: []}, {flags: ['--no-input']}])( |
| 41 | + 'writes one document object with JSON and flags $flags', |
| 42 | + async ({flags}) => { |
| 43 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 44 | + await DocFetch.run(['--url', url, '--language', 'ruby', '--json', ...flags], import.meta.url) |
| 45 | + expect(JSON.parse(stdout())).toEqual({document: {url, content}}) |
| 46 | + expect(stderr()).toBe('') |
| 47 | + expect(fetch).toHaveBeenCalledWith(url, { |
| 48 | + headers: {Accept: 'text/markdown', 'X-Shopify-Surface': 'cli', 'Accept-Language': 'ruby'}, |
| 49 | + }) |
| 50 | + }) |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + test('supports the shared JSON environment flag', async () => { |
| 55 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 56 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 57 | + await DocFetch.run(['--url', url], import.meta.url) |
| 58 | + expect(JSON.parse(stdout())).toEqual({document: {url, content}}) |
| 59 | + expect(stderr()).toBe('') |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + test('reports missing required input before fetching', async () => { |
| 64 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 65 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 66 | + await DocFetch.run(['--json', '--no-input'], import.meta.url) |
| 67 | + expect(JSON.parse(stdout())).toHaveProperty('error') |
| 68 | + expect(process.exit).toHaveBeenCalledWith(2) |
| 69 | + expect(stderr()).toBe('') |
| 70 | + expect(fetch).not.toHaveBeenCalled() |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + test.each(['text', 'json'] as const)('saves exact Markdown bytes with a relative path in %s mode', async (format) => { |
| 75 | + await inTemporaryDirectory(async (directory) => { |
| 76 | + const path = joinPath(directory, 'docs/shopify-cli.md') |
| 77 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 78 | + await DocFetch.run( |
| 79 | + ['--url', url, '--output', relativePath(cwd(), path), ...(format === 'json' ? ['--json'] : [])], |
| 80 | + import.meta.url, |
| 81 | + ) |
| 82 | + |
| 83 | + await expect(readFile(path)).resolves.toBe(content) |
| 84 | + if (format === 'json') { |
| 85 | + expect(JSON.parse(stdout())).toEqual({path, format: 'markdown'}) |
| 86 | + expect(JSON.parse(stderr())).toMatchObject({ |
| 87 | + type: 'diagnostic', |
| 88 | + level: 'info', |
| 89 | + message: `Saved ${url} to ${path}`, |
| 90 | + }) |
| 91 | + } else { |
| 92 | + expect(stdout()).toBe('') |
| 93 | + expect(stderr()).toBe(`Saved ${url} to ${path}\n`) |
| 94 | + } |
| 95 | + }) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + test('does not emit a receipt when the file write fails', async () => { |
| 100 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 101 | + await inTemporaryDirectory(async (directory) => { |
| 102 | + const parent = joinPath(directory, 'file') |
| 103 | + await writeFile(parent, 'existing content') |
| 104 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 105 | + await expect( |
| 106 | + DocFetch.run(['--url', url, '--output', joinPath(parent, 'doc.md'), '--json'], import.meta.url), |
| 107 | + ).resolves.toBeUndefined() |
| 108 | + expect(process.exit).toHaveBeenCalledWith(1) |
| 109 | + expect(JSON.parse(stdout())).toHaveProperty('error') |
| 110 | + expect(stdout()).not.toContain('"format": "markdown"') |
| 111 | + expect(stderr()).toBe('') |
| 112 | + await expect(readFile(parent)).resolves.toBe('existing content') |
| 113 | + }) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + test('does not create a file when fetching fails', async () => { |
| 118 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 119 | + vi.mocked(fetch).mockResolvedValue(new Response('', {status: 404, statusText: 'Not Found'})) |
| 120 | + await inTemporaryDirectory(async (directory) => { |
| 121 | + const path = joinPath(directory, 'doc.md') |
| 122 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 123 | + await expect(DocFetch.run(['--url', url, '--output', path, '--json'], import.meta.url)).resolves.toBeUndefined() |
| 124 | + expect(process.exit).toHaveBeenCalledWith(1) |
| 125 | + expect(JSON.parse(stdout())).toEqual({error: {type: 'abort', message: `Failed to fetch ${url}: 404 Not Found`}}) |
| 126 | + expect(stderr()).toBe('') |
| 127 | + await expect(fileExists(path)).resolves.toBe(false) |
| 128 | + }) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + test.each(['not a url', 'https://example.com/docs'])( |
| 133 | + 'reports invalid input as one fatal document: %s', |
| 134 | + async (input) => { |
| 135 | + vi.stubEnv('SHOPIFY_FLAG_JSON', '1') |
| 136 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 137 | + await expect(DocFetch.run(['--url', input, '--json'], import.meta.url)).resolves.toBeUndefined() |
| 138 | + expect(process.exit).toHaveBeenCalledWith(1) |
| 139 | + expect(JSON.parse(stdout()).error.type).toBe('abort') |
| 140 | + expect(stderr()).toBe('') |
| 141 | + expect(fetch).not.toHaveBeenCalled() |
| 142 | + }) |
| 143 | + }, |
| 144 | + ) |
| 145 | + |
| 146 | + test('exposes the schema in help and through the launcher without fetching', async () => { |
| 147 | + expect(DocFetch.jsonOutputSchema).toBe(docFetchJsonOutputSchema) |
| 148 | + expect(DocFetch.flags.json).toBeDefined() |
| 149 | + expect(DocFetch.description).toContain('Output from `--json` conforms to the `DocFetchResult` schema.') |
| 150 | + vi.spyOn(ShopifyConfig.prototype, 'runHook').mockResolvedValue({successes: [], failures: []}) |
| 151 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 152 | + await launchCLI({ |
| 153 | + moduleURL: import.meta.url, |
| 154 | + argv: ['doc', 'fetch', '--json-schema'], |
| 155 | + lazyCommandLoader: async () => DocFetch, |
| 156 | + }) |
| 157 | + const schema = JSON.parse(stdout()) |
| 158 | + expect(schema.definitions.Result.anyOf).toHaveLength(2) |
| 159 | + expect(schema.definitions.Result.anyOf[0].properties.document.properties.content.type).toBe('string') |
| 160 | + expect(schema.definitions.Result.anyOf[1].properties.format.const).toBe('markdown') |
| 161 | + expect(stderr()).toBe('') |
| 162 | + expect(fetch).not.toHaveBeenCalled() |
| 163 | + }) |
| 164 | + }) |
| 165 | +}) |
0 commit comments