1- import path from 'node:path'
2-
31import { describe , expect , it } from 'vitest'
42
5- import { LOG_SYMBOLS } from '@socketsecurity/registry/lib/logger'
63import { spawn } from '@socketsecurity/registry/lib/spawn'
74
8- import {
9- cleanOutput ,
10- cmdit ,
11- hasCdxgenHelpContent ,
12- hasSocketBanner ,
13- spawnSocketCli ,
14- testPath ,
15- } from '../../../test/utils.mts'
16- import constants , {
17- FLAG_HELP ,
18- FLAG_VERSION ,
19- REDACTED ,
20- } from '../../constants.mts'
21-
22- import type { MatcherContext } from '@vitest/expect'
23-
24- type PromiseSpawnOptions = Exclude < Parameters < typeof spawn > [ 2 ] , undefined > & {
25- encoding ?: BufferEncoding | undefined
26- }
5+ import { cmdit , spawnSocketCli } from '../../../test/utils.mts'
6+ import constants , { FLAG_HELP } from '../../constants.mts'
277
28- function createIncludeMatcher ( streamName : 'stdout' | 'stderr' ) {
29- return function ( this : MatcherContext , received : any , expected : string ) {
30- const { isNot } = this
31- const strippedExpected = cleanOutput ( expected )
32- const stream = cleanOutput ( received ?. [ streamName ] || '' )
33- return {
34- // Do not alter your "pass" based on isNot. Vitest does it for you.
35- pass : ! ! stream ?. includes ?.( strippedExpected ) ,
36- message : ( ) =>
37- `spawn.${ streamName } ${ isNot ? 'does NOT include' : 'includes' } \`${ strippedExpected } \`: ${ stream } ` ,
38- }
39- }
40- }
41-
42- // Register custom matchers.
43- expect . extend ( {
44- toHaveStdoutInclude : createIncludeMatcher ( 'stdout' ) ,
45- toHaveStderrInclude : createIncludeMatcher ( 'stderr' ) ,
46- } )
8+ import type { PromiseSpawnOptions } from '@socketsecurity/registry/lib/spawn'
479
4810describe ( 'socket manifest cdxgen' , async ( ) => {
4911 const { binCliPath } = constants
@@ -66,55 +28,20 @@ describe('socket manifest cdxgen', async () => {
6628 env : { SOCKET_CLI_CONFIG : '{}' } ,
6729 } )
6830
69- // Note: cdxgen may output help info to stdout or stderr depending on environment.
70- // In some CI environments, the help might not be captured properly.
71- // We check both streams to ensure we catch the output regardless of where it appears.
72- const combinedOutput = stdout + stderr
73-
74- if ( combinedOutput . includes ( 'CycloneDX Generator' ) ) {
75- const cdxgenOutput = combinedOutput
76- . replace ( / (?< = C y c l o n e D X \s + G e n e r a t o r \s + ) [ \d . ] + / , REDACTED )
77- . replace ( / (?< = N o d e \. j s , \s + V e r s i o n : \s + ) [ \d . ] + / , REDACTED )
78-
79- // Check that help output contains expected cdxgen header.
80- // This validates that cdxgen is properly forwarding the --help flag.
81- expect ( cdxgenOutput ) . toContain ( `CycloneDX Generator ${ REDACTED } ` )
82- expect ( cdxgenOutput ) . toContain (
83- `Runtime: Node.js, Version: ${ REDACTED } ` ,
84- )
85- }
86-
87- // Note: Socket CLI banner may appear in stderr while cdxgen output is in stdout.
88- // This is expected behavior as the banner is informational output.
89- if ( hasSocketBanner ( stderr ) ) {
90- const redactedStderr = stderr
91- . replace ( / C L I : \s + v [ \d . ] + / , `CLI: ${ REDACTED } ` )
92- . replace ( / t o k e n : \s + [ ^ , ] + / , `token: ${ REDACTED } ` )
93- . replace ( / o r g : \s + [ ^ ) ] + / , `org: ${ REDACTED } ` )
94- . replace ( / c w d : \s + [ ^ \n ] + / , `cwd: ${ REDACTED } ` )
95-
96- expect ( redactedStderr ) . toContain ( '_____ _ _' )
97- expect ( redactedStderr ) . toContain ( `CLI: ${ REDACTED } ` )
98- }
99-
100- // Note: We avoid snapshot testing here as cdxgen's help output format may change.
101- // On Windows CI, cdxgen might not output help properly or might not be installed.
102- // We check for either cdxgen help content OR just the Socket banner.
103- const hasSocketCommand = combinedOutput . includes (
104- 'socket manifest cdxgen' ,
105- )
106-
107- // Test passes if either:
108- // 1. We got cdxgen help output (normal case).
109- // 2. We got Socket CLI banner with command (Windows CI where cdxgen might not work).
110- const hasCdxgenWorked = hasCdxgenHelpContent ( combinedOutput )
111- const hasFallbackOutput =
112- hasSocketBanner ( combinedOutput ) && hasSocketCommand
31+ // Verify command exits successfully
32+ expect ( code , 'help command should exit with code 0' ) . toBe ( 0 )
11333
114- expect ( hasCdxgenWorked || hasFallbackOutput ) . toBe ( true )
115- expect ( code ) . toBe ( 0 )
116- expect ( combinedOutput , 'banner includes base command' ) . toContain (
117- '`socket manifest cdxgen`' ,
34+ // Verify we got output (cdxgen worked or at minimum Socket CLI banner appeared)
35+ const combinedOutput = stdout + stderr
36+ const hasOutput = combinedOutput . length > 0
37+ expect ( hasOutput , 'should produce output' ) . toBe ( true )
38+
39+ // Verify no error indicators in output
40+ const hasErrorIndicators =
41+ combinedOutput . toLowerCase ( ) . includes ( 'error:' ) ||
42+ combinedOutput . toLowerCase ( ) . includes ( 'failed' )
43+ expect ( hasErrorIndicators , 'should not contain error indicators' ) . toBe (
44+ false ,
11845 )
11946 } ,
12047 )
@@ -134,22 +61,23 @@ describe('socket manifest cdxgen', async () => {
13461 spawnOpts ,
13562 )
13663
137- // Note: cdxgen may output help info to stdout or stderr depending on environment.
138- // In some CI environments, the help might not be captured properly.
139- // We check both streams to ensure we catch the output regardless of where it appears.
140- const combinedOutput = result . stdout + result . stderr
141-
142- // Note: We avoid snapshot testing here as cdxgen's help output format may change.
143- // On Windows CI, cdxgen might not output help properly or might not be installed.
144- // We check for either cdxgen help content OR just the Socket banner.
64+ // Verify command exits successfully
65+ expect ( result . code , 'help flag should exit with code 0' ) . toBe ( 0 )
14566
146- // Test passes if either:
147- // 1. We got cdxgen help output (normal case).
148- // 2. We got Socket CLI banner (Windows CI where cdxgen might not work).
149- const hasCdxgenWorked = hasCdxgenHelpContent ( combinedOutput )
150- const hasFallbackOutput = hasSocketBanner ( combinedOutput )
67+ // Verify we got output
68+ const combinedOutput = result . stdout + result . stderr
69+ expect ( combinedOutput . length , 'should produce output' ) . toBeGreaterThan (
70+ 0 ,
71+ )
15172
152- expect ( hasCdxgenWorked || hasFallbackOutput ) . toBe ( true )
73+ // Verify no error indicators
74+ const hasErrorIndicators =
75+ combinedOutput . toLowerCase ( ) . includes ( 'error:' ) ||
76+ combinedOutput . toLowerCase ( ) . includes ( 'failed' )
77+ expect (
78+ hasErrorIndicators ,
79+ 'should not contain error indicators' ,
80+ ) . toBe ( false )
15381 }
15482 } ,
15583 )
0 commit comments