fix: setup Jest with TS

This commit is contained in:
Lin, Hung-Shin authored and wancat committed 2024-01-29 12:42:50 +08:00
1 parent 557af91d4b
commit a18e0d9b68
12 files changed
+45 -9

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import { isValidEmail, isValidUsername } from '../src/auth'
test('check valid username', () => {
const valid = ['alex', 'alex123', 'john_doe', 'john-doe', 'rudeus']
const invalid = ['root', 'admin', '12michael', 'Justin', 'testUser']
valid.forEach((username) => {
expect(isValidUsername(username)).toBe(true)
})
invalid.forEach((username) => {
expect(isValidUsername(username)).toBe(false)
})
})
test('check valid email', () => {
const validEmails = [
'user@example.com',
'user.name@example.co.uk',
'user1234@email.domain',
'user+tag@example.net',
'user@subdomain.example.com',
'user@123.45.67.89',
]
const invalidEmails = [
'user@.com',
'@example.com',
'user@-invalid-domain.com',
'user@.123',
'user@.123.45.67',
]
validEmails.forEach((email) => {
expect(isValidEmail(email)).toBe(true)
})
invalidEmails.forEach((email) => {
expect(isValidEmail(email)).toBe(false)
})
})
+41
View File
@@ -0,0 +1,41 @@
import { buildFileCheckCmd } from '../src/commands'
describe('buildFileCheckCmd function', () => {
it('builds the file check command correctly', () => {
const testcases = [
{
input: { path: '/etc/passwd', type: 'file' },
output: ['test', '-f', '/etc/passwd'],
},
{
input: { path: '/etc', type: 'folder' },
output: ['test', '-d', '/etc'],
},
{
input: { path: '/tmp/socket', type: 'socket' },
output: ['test', '-S', '/tmp/socket'],
},
{
input: { path: '/tmp/link', type: 'link' },
output: ['test', '-L', '/tmp/link'],
},
{
input: { path: '/unknown', type: 'unknown' },
output: ['test', '-e', '/unknown'],
},
// Add some injection tests
{
input: { path: '/etc/passwd;ls', type: 'file' },
output: ['test', '-f', '/etc/passwdls'],
},
{
input: { path: '/etc/passwd && ls', type: 'file' },
output: ['test', '-f', '/etc/passwdls'],
},
]
testcases.forEach(({ input, output }) => {
expect(buildFileCheckCmd(input)).toStrictEqual(output)
})
})
})
+165
View File
@@ -0,0 +1,165 @@
import DAG from '../src/dag'
describe('dag layers', () => {
it('two layer', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getNode('helloworld').layer).toBe(1)
expect(dag.getNode('spell').layer).toBe(2)
})
it('branch', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
{
_id: 'discover',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getNode('helloworld').layer).toBe(1)
expect(dag.getNode('spell').layer).toBe(2)
expect(dag.getNode('discover').layer).toBe(2)
})
it('merge', () => {
const data = [
{
_id: 'A',
requirements: [],
},
{
_id: 'B',
requirements: [],
},
{
_id: 'C',
requirements: ['A', 'B'],
},
]
const dag = new DAG(data)
expect(dag.getNode('A').layer).toBe(1)
expect(dag.getNode('B').layer).toBe(1)
expect(dag.getNode('C').layer).toBe(2)
})
it('skip', () => {
const data = [
{
_id: 'A',
requirements: [],
},
{
_id: 'B',
requirements: [],
},
{
_id: 'C',
requirements: ['A'],
},
{
_id: 'D',
requirements: ['B', 'C'],
},
]
const dag = new DAG(data)
expect(dag.getNode('A').layer).toBe(1)
expect(dag.getNode('B').layer).toBe(1)
expect(dag.getNode('C').layer).toBe(2)
expect(dag.getNode('D').layer).toBe(3)
})
})
describe('dag edges', () => {
it('one edge', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getEdges()).toEqual(new Set([['helloworld', 'spell']]))
})
it('branch edges', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
{
_id: 'discover',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getEdges()).toEqual(
new Set([
['helloworld', 'discover'],
['helloworld', 'spell'],
])
)
})
})
describe('dag layers', () => {
it('two layer', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getLayers()).toEqual([1, 1])
})
it('branch layers', () => {
const data = [
{
_id: 'helloworld',
requirements: [],
},
{
_id: 'spell',
requirements: ['helloworld'],
},
{
_id: 'discover',
requirements: ['helloworld'],
},
]
const dag = new DAG(data)
expect(dag.getLayers()).toEqual([1, 2])
})
})