Skip to content

Commit

Permalink
Make more tests | Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
inventionpro committed Dec 16, 2024
1 parent ff9ebb9 commit 93ec767
Show file tree
Hide file tree
Showing 15 changed files with 323 additions and 37 deletions.
10 changes: 8 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Contributing
## Endpoints
To contribute an endpoint you create a file in the `apis` folder.\
There is a base in `base.ts` or if you alredy know how it works you may use `base_min.ts`.
To contribute an endpoint create a file in the `apis` folder.\
There is a base in `base.ts` or `base_min.ts` for a clean base.

> [!NOTE]
> The bases have a .ts extension so the automatic assigner skips it, your api should be a .js file.\
Expand All @@ -25,6 +25,12 @@ This function will do the formating and turning it into a json compatible string
> - No spaces or - (use _ )
> - Short
### Testing
We now require tests to be written for new endpoints under the `/tests` directory.\
The files should be named `API_NAME.test.js`.\
The tests should at minimum include a test for each possible user error (like forgetting query params) and a successful case.\
For contributions fixing bugs its recomended (and we might require it) to add a test for it to prevent regressions.

### Errors
If theres an error like the user forgot to add a parameter you can use the res.error function:
```js
Expand Down
4 changes: 2 additions & 2 deletions apis/8ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module.exports = {
type: 'get',
params: [],
category: "text",

async execute(req, res) {
const random = Math.floor(Math.random() * responses.length);

res.json({
response: responses[random]
})
Expand Down
2 changes: 1 addition & 1 deletion apis/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
}
],
category: "hidden", // category (only some appear in menu)

async execute(req, res) {

}
Expand Down
10 changes: 5 additions & 5 deletions apis/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ module.exports = {

async execute(req, res) {
if (!req.query["type"]) {
res.error('No conversion type recived')
res.error('No conversion type recived');
return;
}
if (!req.query["text"]) {
res.error('No text recived')
res.error('No text recived');
return;
}
if (req.query["type"] === "encode") {
res.json({
text: Buffer.from(req.query["text"], 'UTF8').toString('Base64')
})
});
return;
}
if (req.query["type"] === "decode") {
res.json({
text: Buffer.from(req.query["text"], 'Base64').toString('UTF8')
})
});
return;
}
res.error('Type not valid')
res.error('Type not valid');
}
}
2 changes: 1 addition & 1 deletion apis/base_min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
type: 'get',
params: [],
category: "hidden",

async execute(req, res) {

}
Expand Down
10 changes: 5 additions & 5 deletions apis/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ module.exports = {

async execute(req, res){
if (!req.query["type"]) {
res.error('No conversion type recived')
res.error('No conversion type recived');
return;
}
if (!req.query["text"]) {
res.error('No text recived')
res.error('No text recived');
return;
}
if (req.query["type"] === "encode") {
res.json({
text: toBin(req.query["text"])
})
});
return;
}
if (req.query["type"] === "decode") {
res.json({
text: toStr(req.query["text"])
})
});
return;
}
res.error('Type not valid')
res.error('Type not valid');
}
}
10 changes: 5 additions & 5 deletions apis/hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ module.exports = {

async execute(req, res) {
if (!req.query["type"]) {
res.error('No conversion type recived')
res.error('No conversion type recived');
return;
}
if (!req.query["text"]) {
res.error('No text recived')
res.error('No text recived');
return;
}
if (req.query["type"] === "encode") {
res.json({
text: Buffer.from(req.query["text"], 'UTF8').toString('Hex')
})
});
return;
}
if (req.query["type"] === "decode") {
res.json({
text: Buffer.from(req.query["text"], 'Hex').toString('UTF8')
})
});
return;
}
res.error('Type not valid')
res.error('Type not valid');
}
}
4 changes: 4 additions & 0 deletions apis/md5.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module.exports = {
category: "text",

async execute(req, res) {
if (!req.query['text']) {
res.error('Include text');
return;
}
res.json({
text: Buffer.from(crypto.createHash('md5').update(req.query["text"]).digest()).toString('hex')
})
Expand Down
66 changes: 66 additions & 0 deletions tests/base64.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { describe, it } = require('node:test');
const assert = require('node:assert');
const api = require('../apis/base64.js');

describe('Base64 api', () => {
it('should error on empty', async () => {
let req = {query:{}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on missing text', async () => {
let req = {query:{type:'encode'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on missing type', async () => {
let req = {query:{text:'hello'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on invalid type', async () => {
let req = {query:{type:'fjda',text:'hello'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should encode hello to aGVsbG8=', async () => {
let req = {query:{type:'encode',text:'hello'}};
let errored = false;
let json = null;
let res = {
error: function(){errored=true},
json: function(j){json=j}
};
api.execute(req,res);
assert.strictEqual(errored, false);
assert.strictEqual(json.text, 'aGVsbG8=');
});
it('should decode aGVsbG8= to hello', async () => {
let req = {query:{type:'decode',text:'aGVsbG8='}};
let errored = false;
let json = null;
let res = {
error: function(){errored=true},
json: function(j){json=j}
};
api.execute(req,res);
assert.strictEqual(errored, false);
assert.strictEqual(json.text, 'hello');
});
});
78 changes: 78 additions & 0 deletions tests/binary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { describe, it } = require('node:test');
const assert = require('node:assert');
const api = require('../apis/binary.js');

describe('Binary api', () => {
it('should error on empty', async () => {
let req = {query:{}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on missing text', async () => {
let req = {query:{type:'encode'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on missing type', async () => {
let req = {query:{text:'hello'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on invalid type', async () => {
let req = {query:{type:'fjda',text:'hello'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should encode hello to 0110100001100101011011000110110001101111', async () => {
let req = {query:{type:'encode',text:'hello'}};
let errored = false;
let json = null;
let res = {
error: function(){errored=true},
json: function(j){json=j}
};
api.execute(req,res);
assert.strictEqual(errored, false);
assert.strictEqual(json.text, '0110100001100101011011000110110001101111');
});
it('should decode 0110100001100101011011000110110001101111 to hello', async () => {
let req = {query:{type:'decode',text:'0110100001100101011011000110110001101111'}};
let errored = false;
let json = null;
let res = {
error: function(){errored=true},
json: function(j){json=j}
};
api.execute(req,res);
assert.strictEqual(errored, false);
assert.strictEqual(json.text, 'hello');
});
it('should decode 7_11010001100101110110011011001101111 to hello', async () => {
let req = {query:{type:'decode',text:'7_11010001100101110110011011001101111'}};
let errored = false;
let json = null;
let res = {
error: function(){errored=true},
json: function(j){json=j}
};
api.execute(req,res);
assert.strictEqual(errored, false);
assert.strictEqual(json.text, 'hello');
});
});
66 changes: 66 additions & 0 deletions tests/hex.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { describe, it } = require('node:test');
const assert = require('node:assert');
const api = require('../apis/hex.js');

describe('Hex api', () => {
it('should error on empty', async () => {
let req = {query:{}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on missing text', async () => {
let req = {query:{type:'encode'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on missing type', async () => {
let req = {query:{text:'hello'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should error on invalid type', async () => {
let req = {query:{type:'fjda',text:'hello'}};
let errored = false;
let res = {
error: function(){errored=true}
};
api.execute(req,res);
assert.strictEqual(errored, true);
});
it('should encode hello to 68656c6c6f', async () => {
let req = {query:{type:'encode',text:'hello'}};
let errored = false;
let json = null;
let res = {
error: function(){errored=true},
json: function(j){json=j}
};
api.execute(req,res);
assert.strictEqual(errored, false);
assert.strictEqual(json.text, '68656c6c6f');
});
it('should decode 68656c6c6f to hello', async () => {
let req = {query:{type:'decode',text:'68656c6c6f'}};
let errored = false;
let json = null;
let res = {
error: function(){errored=true},
json: function(j){json=j}
};
api.execute(req,res);
assert.strictEqual(errored, false);
assert.strictEqual(json.text, 'hello');
});
});
Loading

0 comments on commit 93ec767

Please sign in to comment.