Skip to content

Commit 2cc99c1

Browse files
authored
Merge pull request #166 from cainthebest/impl/unit-tests
2 parents 6039cc2 + bde38f4 commit 2cc99c1

File tree

12 files changed

+506
-38
lines changed

12 files changed

+506
-38
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/tests.yml

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,55 @@ name: Tests
33
on: [push, pull_request]
44

55
jobs:
6-
test:
7-
strategy:
8-
matrix:
9-
node-version: [16.x, 18.x]
10-
# platform: [ubuntu-latest, windows-latest]
11-
platform: [ubuntu-latest]
12-
13-
name: Test on ${{ matrix.platform }} with NodeJS v${{ matrix.node-version }}
14-
15-
runs-on: ${{ matrix.platform }}
16-
17-
steps:
18-
- name: Checkout
19-
uses: actions/checkout@v2
20-
21-
- name: Use Node.js ${{ matrix.node-version }}
22-
uses: actions/setup-node@v3
23-
with:
24-
node-version: ${{ matrix.node-version }}
25-
26-
- name: Setup PNPM
27-
uses: pnpm/[email protected]
28-
with:
29-
version: 7.0.0
30-
31-
- name: Install Dependancies
32-
run: pnpm i
33-
34-
- name: Build Packages
35-
run: pnpm build
36-
37-
- name: Run Tests
38-
run: pnpm test
39-
40-
- name: Lint
41-
run: pnpm lint
6+
test:
7+
name: Test on ${{ matrix.platform }} with NodeJS v${{ matrix.node-version }}
8+
9+
runs-on: ${{ matrix.platform }}
10+
11+
strategy:
12+
matrix:
13+
node-version: [16.x, 18.x]
14+
platform: [ubuntu-latest, windows-latest]
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v3
19+
20+
- name: Setup Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
25+
- name: Install pnpm
26+
uses: pnpm/[email protected]
27+
id: pnpm-install
28+
with:
29+
version: 7
30+
run_install: false
31+
32+
- name: Get pnpm store directory
33+
id: pnpm-cache
34+
shell: bash
35+
run: |
36+
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
37+
38+
- name: Setup pnpm cache
39+
uses: actions/cache@v3
40+
with:
41+
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
42+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
43+
restore-keys: |
44+
${{ runner.os }}-pnpm-store-
45+
46+
- name: Install dependencies
47+
# No frozen lockfile because windows doesn't like it (Theres a issue for it somewhere in PNPM's repo)
48+
run: pnpm install --no-frozen-lockfile
49+
50+
- name: Build
51+
run: pnpm build
52+
53+
- name: Run Tests
54+
run: pnpm test
55+
56+
- name: Lint
57+
run: pnpm lint

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"release": "changeset publish",
1717
"lint": "pnpm -r lint",
1818
"fix": "pnpm -r fix",
19-
"test": "echo \"no tests yet\"",
19+
"test": "pnpm -r test",
2020
"docs:dev": "vitepress dev docs",
2121
"docs:build": "vitepress build docs",
2222
"test-create": "node packages/create-jellycommands/src/bin.js"

packages/jellycommands/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"scripts": {
2525
"dev": "tsup-node --watch src",
2626
"build": "tsup-node",
27+
"test": "tsm node_modules/uvu/bin.js tests \".*\\.test\\.ts\"",
2728
"lint": "run-s lint:*",
2829
"lint:prettier": "prettier --check --plugin-search-dir=. src",
2930
"lint:eslint": "eslint src",
@@ -39,8 +40,10 @@
3940
"nodemon": "^2.0.20",
4041
"npm-run-all": "^4.1.5",
4142
"prettier": "^2.8.4",
43+
"tsm": "^2.3.0",
4244
"tsup": "^6.6.3",
43-
"typescript": "^4.9.5"
45+
"typescript": "^4.9.5",
46+
"uvu": "^0.5.6"
4447
},
4548
"peerDependencies": {
4649
"discord.js": "^14.0.0"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Client } from 'discord.js';
2+
3+
export const rawToken = 'ImAToken';
4+
export const rawClientId = 'ImAClientId';
5+
6+
export const encodedToken = Buffer.from(rawToken).toString('base64');
7+
export const encodedClientId = Buffer.from(rawClientId).toString('base64');
8+
9+
export const mockToken = `${encodedClientId}.${encodedToken}`;
10+
export function mockClient() {
11+
return {
12+
token: mockToken,
13+
user: {
14+
id: rawClientId,
15+
},
16+
} as Client;
17+
}
18+
19+
export { test } from 'uvu';
20+
export { strictEqual as equal, deepStrictEqual as deepEqual, throws } from 'assert';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { cleanToken } from '../../src/utils/token';
2+
import { test, equal, mockToken } from '../common';
3+
4+
test('Strip Bot prefix', () => {
5+
const result = cleanToken(`Bot ${mockToken}`);
6+
equal(result, mockToken);
7+
});
8+
9+
test('Strip Bearer prefix', () => {
10+
const result = cleanToken(`Bearer ${mockToken}`);
11+
equal(result, mockToken);
12+
});
13+
14+
test('No prefix', () => {
15+
const result = cleanToken(mockToken);
16+
equal(result, mockToken);
17+
});
18+
19+
test('No token', () => {
20+
const result = cleanToken();
21+
equal(result, null);
22+
});
23+
24+
test.run();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test, equal, mockToken, rawClientId } from '../common';
2+
import { clientIdFromToken } from '../../src/utils/token';
3+
4+
test('With a valid token', () => {
5+
const result = clientIdFromToken(mockToken);
6+
equal(result, rawClientId);
7+
});
8+
9+
test.run();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { test, deepEqual, throws, mockClient, mockToken, rawClientId } from '../common';
2+
import { getAuthData, AuthData } from '../../src/utils/token';
3+
4+
test('Get the token from the client', () => {
5+
const result = getAuthData(mockClient());
6+
const expected: AuthData = {
7+
clientId: rawClientId,
8+
token: mockToken,
9+
};
10+
11+
deepEqual(result, expected);
12+
});
13+
14+
test('Get the token from the env', () => {
15+
const modifiedMockClient = mockClient();
16+
17+
modifiedMockClient.token = null;
18+
19+
process.env.DISCORD_TOKEN = mockToken;
20+
21+
const result = getAuthData(modifiedMockClient);
22+
const expected: AuthData = {
23+
clientId: rawClientId,
24+
token: mockToken,
25+
};
26+
27+
deepEqual(result, expected);
28+
29+
// Clean up
30+
delete process.env.DISCORD_TOKEN;
31+
});
32+
33+
test('No env token or no client.token to get the token from', () => {
34+
const modifiedMockClient = mockClient();
35+
36+
modifiedMockClient.token = null;
37+
38+
throws(
39+
() => {
40+
getAuthData(modifiedMockClient);
41+
},
42+
{ name: 'Error', message: 'No token found' },
43+
);
44+
});
45+
46+
//! client ID error is out of reach and cant be tested
47+
// test('No client.user or token get the client ID from', () => {
48+
// const modifiedMockClient = mockClient();
49+
//
50+
// modifiedMockClient.user = null;
51+
// modifiedMockClient.token = null;
52+
//
53+
// throws(
54+
// () => {
55+
// getAuthData(modifiedMockClient);
56+
// },
57+
// { name: 'Error', message: 'Invalid token provided' },
58+
// );
59+
// });
60+
61+
test.run();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { test, equal, mockClient, rawClientId } from '../common';
2+
import { resolveClientId } from '../../src/utils/token';
3+
4+
test('Get the ID from the client', () => {
5+
const result = resolveClientId(mockClient());
6+
equal(result, rawClientId);
7+
});
8+
9+
test('Get the ID from the token', () => {
10+
const modifiedMockClient = mockClient();
11+
12+
modifiedMockClient.user = null;
13+
14+
const result = resolveClientId(modifiedMockClient);
15+
equal(result, rawClientId);
16+
});
17+
18+
test('No token or no client.user to get the ID from', () => {
19+
const modifiedMockClient = mockClient();
20+
21+
modifiedMockClient.user = null;
22+
modifiedMockClient.token = null;
23+
24+
const result = resolveClientId(modifiedMockClient);
25+
equal(result, null);
26+
});
27+
28+
test.run();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { test, equal, mockClient, mockToken } from '../common';
2+
import { resolveToken } from '../../src/utils/token';
3+
4+
test('Get token from client', () => {
5+
const result = resolveToken(mockClient());
6+
equal(result, mockToken);
7+
});
8+
9+
test('Get token from env', () => {
10+
const modifiedMockClient = mockClient();
11+
12+
modifiedMockClient.token = null;
13+
14+
process.env.DISCORD_TOKEN = mockToken;
15+
16+
const result = resolveToken(modifiedMockClient);
17+
equal(result, mockToken);
18+
19+
// Clean up
20+
delete process.env.DISCORD_TOKEN;
21+
});
22+
23+
test('No env token or no client.token to get the token from', () => {
24+
const modifiedMockClient = mockClient();
25+
26+
modifiedMockClient.token = null;
27+
28+
const result = resolveToken(modifiedMockClient);
29+
equal(result, null);
30+
});
31+
32+
test.run();

0 commit comments

Comments
 (0)