Skip to content

Commit

Permalink
Move to modules and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Oct 17, 2024
1 parent da9e2d0 commit 6f76765
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { FileModule } from 'src/engine/core-modules/file/file.module';
import { ServerlessModule } from 'src/engine/core-modules/serverless/serverless.module';
import { ThrottlerModule } from 'src/engine/core-modules/throttler/throttler.module';
import { WorkspaceAuthGuard } from 'src/engine/guards/workspace-auth.guard';
import { CodeIntrospectionModule } from 'src/engine/metadata-modules/serverless-function/code-introspection/code-introspection.module';
import { ServerlessFunctionDTO } from 'src/engine/metadata-modules/serverless-function/dtos/serverless-function.dto';
import { ServerlessFunctionEntity } from 'src/engine/metadata-modules/serverless-function/serverless-function.entity';
import { ServerlessFunctionResolver } from 'src/engine/metadata-modules/serverless-function/serverless-function.resolver';
Expand All @@ -32,7 +31,6 @@ import { ServerlessFunctionService } from 'src/engine/metadata-modules/serverles
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
FileModule,
ThrottlerModule,
CodeIntrospectionModule,
],
services: [ServerlessFunctionService],
resolvers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ export class ServerlessFunctionService extends TypeOrmQueryService<ServerlessFun
filename: ENV_FILE_NAME,
});

const fileContent = await readFileContent(indexFileStream);

return {
'.env': await readFileContent(envFileStream),
'src/index.ts': fileContent,
'src/index.ts': await readFileContent(indexFileStream),
};
} catch (error) {
if (error.code === FileStorageExceptionCode.FILE_NOT_FOUND) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Test, TestingModule } from '@nestjs/testing';

import { CodeIntrospectionException } from 'src/modules/code-introspection/code-introspection.exception';
import { CodeIntrospectionService } from 'src/modules/code-introspection/code-introspection.service';

describe('CodeIntrospectionService', () => {
let service: CodeIntrospectionService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CodeIntrospectionService],
}).compile();

service = module.get<CodeIntrospectionService>(CodeIntrospectionService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('analyze', () => {
it('should analyze a function declaration correctly', () => {
const fileContent = `
function testFunction(param1: string, param2: number): void {
console.log(param1, param2);
}
`;

const result = service.analyze(fileContent);

expect(result).toEqual([
{ name: 'param1', type: 'string' },
{ name: 'param2', type: 'number' },
]);
});

it('should analyze an arrow function correctly', () => {
const fileContent = `
const testArrowFunction = (param1: string, param2: number): void => {
console.log(param1, param2);
};
`;

const result = service.analyze(fileContent);

expect(result).toEqual([
{ name: 'param1', type: 'string' },
{ name: 'param2', type: 'number' },
]);
});

it('should return an empty array for files without functions', () => {
const fileContent = `
const x = 5;
console.log(x);
`;

const result = service.analyze(fileContent);

expect(result).toEqual([]);
});

it('should throw an exception for multiple function declarations', () => {
const fileContent = `
function func1(param1: string) {}
function func2(param2: number) {}
`;

expect(() => service.analyze(fileContent)).toThrow(
CodeIntrospectionException,
);
expect(() => service.analyze(fileContent)).toThrow(
'Only one function is allowed',
);
});

it('should throw an exception for multiple arrow functions', () => {
const fileContent = `
const func1 = (param1: string) => {};
const func2 = (param2: number) => {};
`;

expect(() => service.analyze(fileContent)).toThrow(
CodeIntrospectionException,
);
expect(() => service.analyze(fileContent)).toThrow(
'Only one arrow function is allowed',
);
});

it('should correctly analyze complex types', () => {
const fileContent = `
function complexFunction(param1: string[], param2: { key: number }): Promise<boolean> {
return Promise.resolve(true);
}
`;

const result = service.analyze(fileContent);

expect(result).toEqual([
{ name: 'param1', type: 'string[]' },
{ name: 'param2', type: '{ key: number; }' },
]);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';

import { CodeIntrospectionService } from 'src/engine/metadata-modules/serverless-function/code-introspection/code-introspection.service';
import { CodeIntrospectionService } from 'src/modules/code-introspection/code-introspection.service';

@Module({
providers: [CodeIntrospectionService],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import {
CodeIntrospectionException,
CodeIntrospectionExceptionCode,
} from 'src/engine/metadata-modules/serverless-function/code-introspection/code-introspection.exception';
} from 'src/modules/code-introspection/code-introspection.exception';

type FunctionParameter = {
name: string;
Expand Down

0 comments on commit 6f76765

Please sign in to comment.