Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amgelo563 committed Oct 22, 2024
1 parent 5cf65cc commit 16886db
Show file tree
Hide file tree
Showing 13 changed files with 843 additions and 11 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start": "node --trace-deprecation -r source-map-support/register dist/index.js",
"prisma:gen": "prisma generate",
"prisma:push": "prisma db push",
"prepare": "husky || true"
"prepare": "husky || true",
"test": "vitest"
},
"license": "MIT",
"dependencies": {
Expand Down Expand Up @@ -49,7 +50,8 @@
"lint-staged": "15.2.10",
"prettier": "3.3.2",
"typescript": "5.5.4",
"typescript-eslint": "8.6.0"
"typescript-eslint": "8.6.0",
"vitest": "2.1.3"
},
"engines": {
"node": ">=20.0.0"
Expand Down
746 changes: 738 additions & 8 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions test/defaults/helpers/getLanguages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { readdirSync } from 'node:fs';
import { join as joinPath } from 'path';

const getDirectories = (source: string) =>
readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

export function getLanguages(parent: string) {
const path = joinPath(parent, 'lang');
return getDirectories(path);
}
35 changes: 35 additions & 0 deletions test/defaults/helpers/testFileRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { statSync } from 'fs';
import { existsSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import type { z } from 'zod';

import { FileReader } from '../../../src/file/FileReader';

export function testFileRead(
file: string,
path: string,
schema: z.ZodTypeAny,
): boolean {
describe(`${file} - "${path}"`, () => {
it('should exist', () => {
const exists = existsSync(path);
expect(exists).toBeTruthy();
});

it('should be a file', () => {
const stat = statSync(path);
expect(stat.isFile()).toBeTruthy();
});

it('should match the schema and conf format', async () => {
const reader = new FileReader(path, schema);
try {
await reader.read();
} catch (error) {
expect(error).toBeUndefined();
}
});
});

return true;
}
4 changes: 4 additions & 0 deletions test/defaults/messages/BlacklistMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { BlacklistMessagesSchema } from '../../../src/blacklist/message/read/BlacklistMessagesSchema';
import { testMessageFile } from './helpers/testMessageFile';

testMessageFile('blacklist', BlacklistMessagesSchema);
4 changes: 4 additions & 0 deletions test/defaults/messages/GeneralMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { GeneralMessagesSchema } from '../../../src/hermes/message/messages/general/GeneralMessagesSchema';
import { testMessageFile } from './helpers/testMessageFile';

testMessageFile('general', GeneralMessagesSchema);
4 changes: 4 additions & 0 deletions test/defaults/messages/OfferMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { OfferMessagesSchema } from '../../../src/offer/message/read/OfferMessagesSchema';
import { testMessageFile } from './helpers/testMessageFile';

testMessageFile('offer', OfferMessagesSchema);
4 changes: 4 additions & 0 deletions test/defaults/messages/RequestMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { RequestMessagesSchema } from '../../../src/request/message/read/RequestMessagesSchema';
import { testMessageFile } from './helpers/testMessageFile';

testMessageFile('request', RequestMessagesSchema);
4 changes: 4 additions & 0 deletions test/defaults/messages/TagsMessages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { TagsMessagesSchema } from '../../../src/tag/message/TagsMessagesSchema';
import { testMessageFile } from './helpers/testMessageFile';

testMessageFile('tags', TagsMessagesSchema);
10 changes: 10 additions & 0 deletions test/defaults/messages/helpers/getMessagesLanguages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getLanguages } from '../../helpers/getLanguages';

let cache: string[] | undefined;

export function getMessagesLanguages() {
if (!cache) {
cache = getLanguages('messages');
}
return cache;
}
14 changes: 14 additions & 0 deletions test/defaults/messages/helpers/testMessageFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { join as joinPath } from 'path';
import type { z } from 'zod';

import { testFileRead } from '../../helpers/testFileRead';
import { getMessagesLanguages } from './getMessagesLanguages';

const languages = getMessagesLanguages();

export function testMessageFile(file: string, schema: z.ZodTypeAny) {
for (const language of languages) {
const path = joinPath('lang', language, `${file}.conf`);
testFileRead(`${file} messages (${language})`, path, schema);
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"strict": true
},
"include": [
"src"
"src",
"test"
],
"exclude": [
"dist",
Expand Down
8 changes: 8 additions & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
name: 'Hermes',
passWithNoTests: true,
},
});

0 comments on commit 16886db

Please sign in to comment.