Skip to content

Commit

Permalink
testcases added for preview command
Browse files Browse the repository at this point in the history
Signed-off-by: Gourav <[email protected]>
  • Loading branch information
GauravSaini01 committed Feb 24, 2025
1 parent 9e26f26 commit 76ecc39
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/commands/start/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class StartPreview extends Command {
for (const [key, value] of Object.entries(obj)) {
if (
key === '$ref' &&
typeof value === 'string' &&
(value.startsWith('.') || value.startsWith('./') || value.startsWith('../'))
typeof value === 'string' &&
(value.startsWith('.') || value.startsWith('./') || value.startsWith('../'))
) {
const { filePath } = this.parseRef(value);
const resolvedPath = path.resolve(basePath, filePath);
Expand Down Expand Up @@ -81,12 +81,13 @@ class StartPreview extends Command {
try {
const document = await bundle(AsyncAPIFile);
const fileContent =
outputFormat === '.yaml' || outputFormat === '.yml'
? document.yml()
: JSON.stringify(document.json());
outputFormat === '.yaml' || outputFormat === '.yml'
? document.yml()
: JSON.stringify(document.json(), null, 2);
fs.writeFileSync(bundledFilePath, fileContent, { encoding: 'utf-8' });
this.log('Bundled file updated successfully.');
} catch (error: any) {
throw new Error(`Error bundling files: ${error.message}`);
this.error(`Error bundling files: ${error.message}`);
}
}

Expand All @@ -95,6 +96,8 @@ class StartPreview extends Command {
const port = flags.port;
const filePath = args['spec-file'];

this.log('Starting Studio in preview mode...');

this.specFile = await load(filePath);
this.metricsMetadata.port = port;

Expand All @@ -106,9 +109,7 @@ class StartPreview extends Command {

const outputFormat = path.extname(AsyncAPIFile);
if (!outputFormat) {
this.error(
'Unable to determine file format from the provided AsyncAPI file.'
);
this.error('Unable to determine file format from the provided AsyncAPI file.');
}

const bundledFilePath = `./asyncapi-bundled${outputFormat}`;
Expand All @@ -121,29 +122,24 @@ class StartPreview extends Command {
);
this.findLocalRefFiles(asyncapiDocument, basePath, filesToWatch);

const watcher = chokidar.watch(Array.from(filesToWatch), {
persistent: true,
});
const watcher = chokidar.watch(Array.from(filesToWatch), { persistent: true });

await this.updateBundledFile(
AsyncAPIFile,
outputFormat,
bundledFilePath
);
await this.updateBundledFile(AsyncAPIFile, outputFormat, bundledFilePath);

watcher.on('change', async (changedPath) => {
this.log(`File changed: ${changedPath}`);
try {
await this.updateBundledFile(
AsyncAPIFile,
outputFormat,
bundledFilePath
);
await this.updateBundledFile(AsyncAPIFile, outputFormat, bundledFilePath);
} catch (error: any) {
this.error(`Error updating bundled file: ${error.message}`);
}
});

watcher.on('error', (error) => {
this.error(`Watcher error: ${error.message}`);
});

this.log(`Starting Studio on port ${port}`);
startStudio(bundledFilePath, port);
}
}
Expand Down
40 changes: 40 additions & 0 deletions test/integration/preview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { test } from '@oclif/test';
import fs from 'fs';
import TestHelper from '../helpers';
import { expect } from 'chai';

const testHelper = new TestHelper();
const bundledFilePath = './asyncapi-bundled.yml';

describe('start preview', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
if (fs.existsSync(bundledFilePath)) {
fs.unlinkSync(bundledFilePath);
}
});

test
.stderr()
.stdout()
.command(['start:preview', './test/fixtures/specification.yml'])
.it('bundles the AsyncAPI file and starts the studio', ({ stdout, stderr }) => {
const output = stdout + stderr;
expect(output).to.include('Starting Studio in preview mode...');
expect(output).to.include('Bundled file updated successfully.');
expect(fs.existsSync(bundledFilePath)).to.be.true;
});

test
.stderr()
.stdout()
.command(['start:preview', 'non-existing-context'])
.it('throws error if context does not exist', ({ stdout, stderr }) => {
const output = stdout + stderr;
expect(output).to.include('ContextError: Context "non-existing-context" does not exist');
});
});

0 comments on commit 76ecc39

Please sign in to comment.