Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/cli/commands/preview/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export async function runCommand(

try {
if ( outputFormat === 'json' ) {
const config = await readCliConfig();
const json = JSON.stringify( config.snapshots );
// Snapshots are per-user; when logged out emit an empty list instead of
// erroring so consumers (e.g. the desktop app) degrade gracefully.
const token = await readAuthToken();
const json = JSON.stringify( token ? await getSnapshotsFromConfig( token.id ) : [] );
console.log( json );
logger.reportKeyValuePair( 'snapshots', json );
return;
Expand Down
28 changes: 28 additions & 0 deletions apps/cli/commands/preview/tests/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,34 @@ describe( 'Preview List Command', () => {
} );
} );

describe( 'with --format json', () => {
let consoleLogSpy: ReturnType< typeof vi.spyOn >;

beforeEach( () => {
consoleLogSpy = vi.spyOn( console, 'log' ).mockImplementation( () => undefined );
} );

it( 'should output only the authenticated user’s snapshots', async () => {
await runCommand( mockFolder, 'json' );

expect( getSnapshotsFromConfig ).toHaveBeenCalledWith( mockAuthToken.id );
const json = JSON.stringify( mockSnapshots );
expect( consoleLogSpy ).toHaveBeenCalledWith( json );
expect( mockReportKeyValuePair ).toHaveBeenCalledWith( 'snapshots', json );
} );

it( 'should output an empty array without erroring when logged out', async () => {
vi.mocked( readAuthToken ).mockResolvedValue( null );

await runCommand( mockFolder, 'json' );

expect( getSnapshotsFromConfig ).not.toHaveBeenCalled();
expect( consoleLogSpy ).toHaveBeenCalledWith( '[]' );
expect( mockReportKeyValuePair ).toHaveBeenCalledWith( 'snapshots', '[]' );
expect( mockReportError ).not.toHaveBeenCalled();
} );
} );

it( 'should prune expired orphaned snapshots even when running without --all', async () => {
await runCommand( mockFolder, 'table' );

Expand Down
Loading