Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): adding integration test and normal unit test #93

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion backend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
plugins: ['@typescript-eslint/eslint-plugin', 'unused-imports'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
Expand All @@ -22,12 +22,23 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'prefer-const': [
'error',
{
destructuring: 'all',
ignoreReadBeforeAssign: true,
},
],
'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': [
'warn',
{
vars: 'all',
varsIgnorePattern: '^_',
args: 'after-used',
argsIgnorePattern: '^_',
},
],
},
};
5 changes: 3 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:integration": "RUN_INTEGRATION_TESTS=true jest",
"test:integration": "INTEGRATION_TEST=true jest",
"check-types": "tsc --noEmit"
},
"dependencies": {
Expand All @@ -46,6 +46,7 @@
"bcrypt": "^5.1.1",
"class-validator": "^0.14.1",
"dotenv": "^16.4.7",
"eslint-plugin-unused-imports": "^4.1.4",
"fastembed": "^1.14.1",
"fs-extra": "^11.2.0",
"gpt-3-encoder": "^1.1.4",
Expand Down Expand Up @@ -92,4 +93,4 @@
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
}
}
}
1 change: 0 additions & 1 deletion backend/src/app.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Injectable } from '@nestjs/common';
import { Query, Resolver } from '@nestjs/graphql';
import { RequireRoles } from './decorator/auth.decorator';

Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
await this.jwtService.verifyAsync(params.token);
return this.jwtCacheService.isTokenStored(params.token);
} catch (error) {
console.log(error);
Logger.log(error);
return false;
}
}
Expand All @@ -92,7 +92,7 @@
Logger.log('logout token', token);
try {
await this.jwtService.verifyAsync(token);
} catch (error) {

Check warning on line 95 in backend/src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / autofix

'error' is defined but never used
return false;
}

Expand Down
3 changes: 1 addition & 2 deletions backend/src/auth/dto/check-token.input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Field, InputType } from '@nestjs/graphql';
import { OutputTypeFactory } from '@nestjs/graphql/dist/schema-builder/factories/output-type.factory';
import { IsString, MinLength } from 'class-validator';
import { IsString } from 'class-validator';

@InputType()
export class CheckTokenInput {
Expand Down
3 changes: 1 addition & 2 deletions backend/src/auth/role/role.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ObjectType, Field, ID } from '@nestjs/graphql';
import { SystemBaseModel } from 'src/system-base-model/system-base.model';
import { ObjectType, Field } from '@nestjs/graphql';
import { User } from 'src/user/user.model';
import {
Entity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { v4 as uuidv4 } from 'uuid';
import { Logger } from '@nestjs/common';
import { getTemplatePath } from 'codefox-common';

/**
* This test ensure project template exist
*/
describe('Copy Project Template', () => {
it('should copy the template to the specified UUID folder', async () => {
const templatePath = getTemplatePath('template-backend');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { BuilderContext } from '../context';
import { BuildMonitor } from '../monitor';
import { isIntegrationTest } from 'src/common/utils';
import { BuildSequence } from '../types';
import { executeBuildSequence, objectToMarkdown, writeToFile } from './utils';

describe('Build Sequence Test', () => {
import { executeBuildSequence } from './utils';
import { Logger } from '@nestjs/common';
(isIntegrationTest ? describe : describe.skip)('Build Sequence Test', () => {
it('should execute build sequence successfully', async () => {
const sequence: BuildSequence = {
id: 'test-backend-sequence',
Expand Down Expand Up @@ -145,6 +144,6 @@ describe('Build Sequence Test', () => {
const result = await executeBuildSequence('fullstack-code-gen', sequence);
expect(result.success).toBe(true);
expect(result.metrics).toBeDefined();
console.log(`Logs saved to: ${result.logFolderPath}`);
}, 60000000);
Logger.log(`Logs saved to: ${result.logFolderPath}`);
}, 300000);
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import * as path from 'path';
import * as os from 'os';
import { existsSync, rmdirSync } from 'fs-extra';
import { saveGeneratedCode } from 'src/build-system/utils/files';
import { Logger } from '@nestjs/common';
import { getProjectPath, getProjectsDir, getRootDir } from 'codefox-common';
import { saveGeneratedCode } from 'src/build-system/utils/files';

describe('Path Utilities', () => {
const APP_NAME = 'codefox';
const ROOT_DIR = path.join(os.homedir(), `.${APP_NAME}`);

const cleanUp = () => {
// if (existsSync(ROOT_DIR)) {
// rmdirSync(ROOT_DIR, { recursive: true });
Expand Down Expand Up @@ -49,11 +44,9 @@
});

it('should create and return the root directory', async () => {
const rootDir = getRootDir();

Check warning on line 47 in backend/src/build-system/__tests__/test-file-create-and-path.spec.ts

View workflow job for this annotation

GitHub Actions / autofix

'rootDir' is assigned a value but never used. Allowed unused vars must match /^_/u

await generateAndSaveCode();
expect(rootDir).toBe(ROOT_DIR);
expect(existsSync(ROOT_DIR)).toBe(true);
});
});

Expand All @@ -74,8 +67,9 @@

try {
const filePath = await saveGeneratedCode(fileName, generatedCode);
console.log(`Generated code saved at: ${filePath}`);
// TODO: need to remove
Logger.log(`Generated code saved at: ${filePath}`);
} catch (error) {
console.error('Failed to save generated code:', error);
Logger.error('Failed to save generated code:', error);
}
}
21 changes: 11 additions & 10 deletions backend/src/build-system/__tests__/test-file-create.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import normalizePath from 'normalize-path';
import { FileGeneratorHandler } from '../handlers/file-manager/file-generate';
import { Logger } from '@nestjs/common';
import { isIntegrationTest } from 'src/common/utils';

describe('FileGeneratorHandler', () => {
const projectSrcPath = normalizePath(
(isIntegrationTest ? describe : describe.skip)('FileGeneratorHandler', () => {
const projectSrcPath = path.normalize(
path.join('src', 'build-system', '__tests__', 'test-project'),
);

const mdFilePath = normalizePath(
const mdFilePath = path.normalize(
path.join('src', 'build-system', '__tests__', 'file-arch.md'),
);

const structMdFilePath = normalizePath(
const structMdFilePath = path.normalize(
path.join('src', 'build-system', '__tests__', 'file-structure-document.md'),
);

beforeEach(async () => {
// Ensure the project directory is clean
await fs.remove(
normalizePath(
path.normalize(
path.join('src', 'build-system', '__tests__', 'test-project', 'src'),
),
);
Expand All @@ -28,19 +29,19 @@
afterEach(async () => {
// Clean up the generated test files
await fs.remove(
normalizePath(
path.normalize(
path.join('src', 'build-system', '__tests__', 'test-project', 'src'),
),
);
});

it('should generate files based on file-arch.md', async () => {
const archMarkdownContent = fs.readFileSync(
normalizePath(path.resolve(mdFilePath)),
path.normalize(path.resolve(mdFilePath)),
'utf8',
);
const structMarkdownContent = fs.readFileSync(

Check warning on line 43 in backend/src/build-system/__tests__/test-file-create.spec.ts

View workflow job for this annotation

GitHub Actions / autofix

'structMarkdownContent' is assigned a value but never used. Allowed unused vars must match /^_/u
normalizePath(path.resolve(structMdFilePath)),
path.normalize(path.resolve(structMdFilePath)),
'utf8',
);

Expand All @@ -52,7 +53,7 @@
projectSrcPath,
);

console.log('File generation result:', result);
Logger.log('File generation result:', result);

// Verify that all files exist
const jsonData = JSON.parse(
Expand Down
161 changes: 83 additions & 78 deletions backend/src/build-system/__tests__/test-generate-doc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,91 @@
import { isIntegrationTest } from 'src/common/utils';
import { BuildSequence } from '../types';
import { executeBuildSequence } from './utils';
import { Logger } from '@nestjs/common';

// TODO: adding integration flag
describe('Sequence: PRD -> UXSD -> UXDD -> UXSS', () => {
it('should execute the full sequence and log results to individual files', async () => {
const sequence: BuildSequence = {
id: 'test-backend-sequence',
version: '1.0.0',
name: 'Spotify-like Music Web',
description: 'Users can play music',
databaseType: 'SQLite',
steps: [
{
id: 'step-1',
name: 'Generate PRD',
nodes: [
{
id: 'op:PRD',
name: 'PRD Generation Node',
},
],
},
{
id: 'step-2',
name: 'Generate UX Sitemap Document',
nodes: [
{
id: 'op:UX:SMD',
name: 'UX Sitemap Document Node',
},
],
},
{
id: 'step-3',
name: 'Generate UX Sitemap Structure',
nodes: [
{
id: 'op:UX:SMS',
name: 'UX Sitemap Structure Node',
},
],
},
{
id: 'step-4',
name: 'UX Data Map Document',
nodes: [
{
id: 'op:UX:DATAMAP:DOC',
name: 'UX Data Map Document node',
},
],
},
{
id: 'step-5',
name: 'UX SMD LEVEL 2 Page Details',
nodes: [
{
id: 'op:UX:SMS:LEVEL2',
name: 'UX SMD LEVEL 2 Page Details Node',
},
],
},
],
};
(isIntegrationTest ? describe : describe.skip)(
'Sequence: PRD -> UXSD -> UXDD -> UXSS',
() => {
it('should execute the full sequence and log results to individual files', async () => {
const sequence: BuildSequence = {
id: 'test-backend-sequence',
version: '1.0.0',
name: 'Spotify-like Music Web',
description: 'Users can play music',
databaseType: 'SQLite',
steps: [
{
id: 'step-1',
name: 'Generate PRD',
nodes: [
{
id: 'op:PRD',
name: 'PRD Generation Node',
},
],
},
{
id: 'step-2',
name: 'Generate UX Sitemap Document',
nodes: [
{
id: 'op:UX:SMD',
name: 'UX Sitemap Document Node',
},
],
},
{
id: 'step-3',
name: 'Generate UX Sitemap Structure',
nodes: [
{
id: 'op:UX:SMS',
name: 'UX Sitemap Structure Node',
},
],
},
{
id: 'step-4',
name: 'UX Data Map Document',
nodes: [
{
id: 'op:UX:DATAMAP:DOC',
name: 'UX Data Map Document node',
},
],
},
{
id: 'step-5',
name: 'UX SMD LEVEL 2 Page Details',
nodes: [
{
id: 'op:UX:SMS:LEVEL2',
name: 'UX SMD LEVEL 2 Page Details Node',
},
],
},
],
};

try {
const result = await executeBuildSequence(
'test-generate-all-ux-part',
sequence,
);
try {
const result = await executeBuildSequence(
'test-generate-all-ux-part',
sequence,
);

console.log(
'Sequence completed successfully. Logs stored in:',
result.logFolderPath,
);
Logger.log(
'Sequence completed successfully. Logs stored in:',
result.logFolderPath,
);

if (!result.success) {
throw result.error;
if (!result.success) {
throw result.error;
}
} catch (error) {
Logger.error('Error during sequence execution:', error);
throw error;
}
} catch (error) {
console.error('Error during sequence execution:', error);
throw error;
}
}, 600000);
});
}, 600000);
},
);
Loading
Loading