|
| 1 | +import { CoreConfig, PluginConfig, Report } from '@quality-metrics/models'; |
| 2 | +import { CollectOptions } from '@quality-metrics/utils'; |
| 3 | +import { readFileSync } from 'node:fs'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { yargsCli } from '../cli'; |
| 6 | +import { getDirname } from '../implementation/utils'; |
| 7 | +import { middlewares } from '../middlewares'; |
| 8 | +import { yargsGlobalOptionsDefinition } from '../options'; |
| 9 | +import { yargsCollectCommandObject } from './command-object'; |
| 10 | + |
| 11 | +const outputPath = 'collect-command-object.json'; |
| 12 | +const dummyConfig: CoreConfig = { |
| 13 | + persist: { outputPath }, |
| 14 | + plugins: [mockPlugin()], |
| 15 | + categories: [], |
| 16 | +}; |
| 17 | + |
| 18 | +describe('collect-command-object', () => { |
| 19 | + it('should parse arguments correctly', async () => { |
| 20 | + const args = ['collect', '--verbose', '--configPath', '']; |
| 21 | + const cli = yargsCli([], { options: yargsGlobalOptionsDefinition() }) |
| 22 | + .config(dummyConfig) |
| 23 | + .command(yargsCollectCommandObject()); |
| 24 | + const parsedArgv = (await cli.parseAsync( |
| 25 | + args, |
| 26 | + )) as unknown as CollectOptions; |
| 27 | + const { persist } = parsedArgv; |
| 28 | + const { outputPath: outPath } = persist; |
| 29 | + expect(outPath).toBe(outputPath); |
| 30 | + return Promise.resolve(void 0); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should execute middleware correctly', async () => { |
| 34 | + const args = [ |
| 35 | + 'collect', |
| 36 | + '--configPath', |
| 37 | + join( |
| 38 | + getDirname(import.meta.url), |
| 39 | + '..', |
| 40 | + 'implementation', |
| 41 | + 'mock', |
| 42 | + 'config-middleware-config.mock.mjs', |
| 43 | + ), |
| 44 | + ]; |
| 45 | + await yargsCli([], { middlewares }) |
| 46 | + .config(dummyConfig) |
| 47 | + .command(yargsCollectCommandObject()) |
| 48 | + .parseAsync(args); |
| 49 | + const report = JSON.parse(readFileSync(outputPath).toString()) as Report; |
| 50 | + expect(report.plugins[0]?.meta.slug).toBe('collect-command-object'); |
| 51 | + expect(report.plugins[0]?.audits[0]?.slug).toBe( |
| 52 | + 'command-object-audit-slug', |
| 53 | + ); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +function mockPlugin(): PluginConfig { |
| 58 | + return { |
| 59 | + audits: [ |
| 60 | + { |
| 61 | + slug: 'command-object-audit-slug', |
| 62 | + title: 'audit title', |
| 63 | + description: 'audit description', |
| 64 | + label: 'mock audit label', |
| 65 | + docsUrl: 'http://www.my-docs.dev', |
| 66 | + }, |
| 67 | + ], |
| 68 | + runner: { |
| 69 | + command: 'bash', |
| 70 | + args: [ |
| 71 | + '-c', |
| 72 | + `echo '${JSON.stringify({ |
| 73 | + audits: [ |
| 74 | + { |
| 75 | + slug: 'command-object-audit-slug', |
| 76 | + value: 0, |
| 77 | + score: 0, |
| 78 | + }, |
| 79 | + ], |
| 80 | + })}' > ${outputPath}`, |
| 81 | + ], |
| 82 | + outputPath, |
| 83 | + }, |
| 84 | + groups: [], |
| 85 | + meta: { |
| 86 | + slug: 'collect-command-object', |
| 87 | + name: 'collect command object', |
| 88 | + }, |
| 89 | + } satisfies PluginConfig; |
| 90 | +} |
0 commit comments