diff --git a/.eslintrc.js b/.eslintrc.js index c7038c13..f2b81c02 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,17 +1,15 @@ module.exports = { parser: "@typescript-eslint/parser", - plugins: [ - "@typescript-eslint" - ], + plugins: ["@typescript-eslint"], extends: [ "eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended" + "plugin:@typescript-eslint/recommended", ], env: { - node: true + node: true, }, rules: { - "no-console": ["error", {"allow": ["warn"]}] - } -} \ No newline at end of file + "no-console": ["error", { allow: ["warn"] }], + }, +}; diff --git a/.github/workflows/code-quality.yaml b/.github/workflows/code-quality.yaml new file mode 100644 index 00000000..303fdf9b --- /dev/null +++ b/.github/workflows/code-quality.yaml @@ -0,0 +1,43 @@ +name: Code Quality +on: [push] + +concurrency: ${{ github.workflow }}-${{ github.ref }} + +env: + CI: true + +jobs: + publish-packages: + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Configure CI Git User + run: | + git config --global user.name 'Rajat Saxena' + git config --global user.email 'hi@sub.rajatsaxena.dev' + git remote set-url origin https://$GITHUB_ACTOR:$GITHUB_PAT@github.com/codelitdev/medialit + env: + GITHUB_PAT: ${{ secrets.PAT }} + + - name: Checkout and pull branch + run: git checkout "${GITHUB_REF:11}" && git pull + + - name: Install pnpm + run: | + npm i pnpm@latest -g + + - name: Install dependencies + run: pnpm install + + - name: Run lint + run: pnpm lint + + - name: Run prettier + run: pnpm prettier + + - name: Run test + run: pnpm test \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index 76add878..f222cd41 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,3 @@ node_modules -dist \ No newline at end of file +dist +**/.next \ No newline at end of file diff --git a/apps/api/__tests__/apikey/handlers.test.ts b/apps/api/__tests__/apikey/handlers.test.ts new file mode 100644 index 00000000..2b4a536f --- /dev/null +++ b/apps/api/__tests__/apikey/handlers.test.ts @@ -0,0 +1,44 @@ +import test, { afterEach, describe, mock } from "node:test"; +import { createApikey } from "../../lib/apikey/handlers"; +import assert from "node:assert"; +import queries from "../../lib/apikey/queries"; + +describe("API key test suite", () => { + afterEach(() => { + mock.restoreAll(); + }); + + test("Create API key throws an error if name is empty", async (t) => { + const req = { + body: {}, + }; + const res = { + status: () => ({ + json: (data: any) => data, + }), + }; + const response = await createApikey(req, res, () => {}); // eslint-disable-line @typescript-eslint/no-empty-function + assert.strictEqual(response.error, "Name is required"); + }); + + test("Create API succeeds if name is provided", async (t) => { + const req = { + body: { + name: "Test API", + }, + user: { + id: "123", + }, + }; + const res = { + status: () => ({ + json: (data: any) => data, + }), + }; + mock.method(queries, "createApiKey").mock.mockImplementation( + async () => ({ key: "123" }) + ); + const response = await createApikey(req, res, () => {}); // eslint-disable-line @typescript-eslint/no-empty-function + assert.strictEqual(response.key, "123"); + }); +}); diff --git a/apps/api/__tests__/core.test.js b/apps/api/__tests__/core.test.js deleted file mode 100644 index 5b723152..00000000 --- a/apps/api/__tests__/core.test.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -const core = require(".."); - -describe("core", () => { - it("needs tests"); -}); diff --git a/apps/api/lib/apikey/handlers.ts b/apps/api/lib/apikey/handlers.ts index df440bb3..3e4ccf8a 100644 --- a/apps/api/lib/apikey/handlers.ts +++ b/apps/api/lib/apikey/handlers.ts @@ -1,6 +1,6 @@ import logger from "../services/log"; import { NOT_FOUND, SUCCESS } from "../config/strings"; -import { createApiKey, deleteApiKey, getApiKeyByUserId } from "./queries"; +import queries, { deleteApiKey, getApiKeyByUserId } from "./queries"; import { Apikey } from "@medialit/models"; export async function createApikey( @@ -14,7 +14,7 @@ export async function createApikey( } try { - const apikey: Apikey = await createApiKey(req.user.id, name); + const apikey: Apikey = await queries.createApiKey(req.user.id, name); return res.status(200).json({ key: apikey.key, diff --git a/apps/api/lib/apikey/queries.ts b/apps/api/lib/apikey/queries.ts index 14a1563f..3f90ccaa 100644 --- a/apps/api/lib/apikey/queries.ts +++ b/apps/api/lib/apikey/queries.ts @@ -59,3 +59,10 @@ export async function deleteApiKey( userId, }); } + +export default { + createApiKey, + getApiKeyUsingKeyId, + getApiKeyByUserId, + deleteApiKey, +}; diff --git a/apps/api/lib/user/__test__/queries.test.js b/apps/api/lib/user/__test__/queries.test.js index e7ac6d84..e69de29b 100644 --- a/apps/api/lib/user/__test__/queries.test.js +++ b/apps/api/lib/user/__test__/queries.test.js @@ -1,98 +0,0 @@ -const test = require("node:test"); -const assert = require("node:assert/strict"); -const { getUser } = require("../queries"); // Adjust the path accordingly -const UserModel = require("../model"); // Import your actual user model - -// Mock UserModel -const mockUserModel = { - findById: async (id) => { - // Mock user object with the specified fields - return { - _id: id, - email: "mock@example.com", - active: true, - name: "Mock User", - }; - }, -}; - -test("should return a valid userId", async (t) => { - const validUserId = "12345abcd"; - - const user = await mockUserModel.findById(validUserId); - - const result = await getUser(validUserId); - assert.strictEqual(typeof result.id, "string", "UserId should be a string"); - assert.deepStrictEqual(result.id, user.id); -}); - -test("should return a invalid userId", async (t) => { - const validUserId = "12345efgh"; - - const user = await mockUserModel.findById(validUserId); - - const result = await getUser(validUserId); - // assert.strictEqual(retrievedUser.id, existingUserId); - assert.notDeepStrictEqual(result.id, user.id); -}); - -// describe('getUser function', () => { -// it('should return a user when a valid ID is provided', async () => { -// // Arrange -// const userId = 'validUserId'; - -// // Mock the findById method -// const originalFindById = UserModel.findById; -// UserModel.findById = async (id) => mockUserModel.findById(id); - -// // Act -// const result = await getUser(userId); - -// // Assert -// assert.deepStrictEqual(result, { -// _id: userId, -// email: 'mock@example.com', -// active: true, -// name: 'Mock User', -// }); - -// // Restore the original method -// UserModel.findById = originalFindById; -// }); - -// it('should return null when an invalid ID is provided', async () => { -// // Arrange -// const invalidUserId = 'invalidUserId'; - -// // Mock the findById method -// const originalFindById = UserModel.findById; -// UserModel.findById = async (id) => null; - -// // Act -// const result = await getUser(invalidUserId); - -// // Assert -// assert.strictEqual(result, null); - -// // Restore the original method -// UserModel.findById = originalFindById; -// }); - -// it('should handle errors and reject the promise', async () => { -// // Arrange -// const errorUserId = 'errorUserId'; -// const errorMessage = 'An error occurred'; - -// // Mock the findById method -// const originalFindById = UserModel.findById; -// UserModel.findById = async (id) => { -// throw new Error(errorMessage); -// }; - -// // Act & Assert -// await assert.rejects(async () => await getUser(errorUserId), { message: errorMessage }); - -// // Restore the original method -// UserModel.findById = originalFindById; -// }); -// }); diff --git a/apps/api/package.json b/apps/api/package.json index 07d75172..412ed87b 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -27,7 +27,8 @@ "scripts": { "build": "tsc", "dev": "nodemon lib/index.ts", - "start": "node dist/index.js" + "start": "node dist/index.js", + "test": "node --import tsx --test **/*.test.ts" }, "dependencies": { "@aws-sdk/client-s3": "^3.55.0", @@ -67,6 +68,7 @@ "eslint": "^8.12.0", "nodemon": "^2.0.15", "ts-node": "^10.7.0", + "tsx": "^4.7.0", "typescript": "^5.2.2" } } diff --git a/apps/web/app/dashboard/app/[name]/files/loading.tsx b/apps/web/app/dashboard/app/[name]/files/loading.tsx index 1f49d72b..88861e00 100644 --- a/apps/web/app/dashboard/app/[name]/files/loading.tsx +++ b/apps/web/app/dashboard/app/[name]/files/loading.tsx @@ -1,9 +1,7 @@ -import React from 'react' +import React from "react"; const Loading = () => { - return ( -