Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move test inside mod_test #103

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Run Lint
run: deno lint src/
- name: Run Tests
run: deno test -A tests/
run: deno test -A mod_test.ts
129 changes: 129 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { assert } from './deps.ts'
import {
generateFileName,
getCurrentMonth,
getTimestamp,
humanizeDuration,
isValidHttpUrl,
} from './src/utils.ts'

Deno.test('isValidHttpUrl should return true for valid HTTP URLs', () => {
assert.assertEquals(isValidHttpUrl({ url: 'http://example.com' }), true)
assert.assertEquals(isValidHttpUrl({ url: 'https://www.google.com' }), true)
assert.assertEquals(isValidHttpUrl({ url: 'http://localhost:8080' }), true)
})

Deno.test('isValidHttpUrl should return false for invalid URLs', () => {
assert.assertEquals(isValidHttpUrl({ url: 'example.com' }), false)
assert.assertEquals(isValidHttpUrl({ url: 'ftp://example.com' }), false)
assert.assertEquals(isValidHttpUrl({ url: 'mailto:[email protected]' }), false)
})

Deno.test('isValidHttpUrl should handle empty', () => {
assert.assertEquals(isValidHttpUrl({ url: '' }), false)
})

Deno.test(
'getTimestamp should return "log" timestamp format for default type',
() => {
const timestamp = getTimestamp({ type: 'log' })
assert.assert(timestamp.match(/^\d{4}-\d{2}-\d{2}$/))
},
)

Deno.test(
'getTimestamp should return "pdf" timestamp format for type "pdf"',
() => {
const timestamp = getTimestamp({ type: 'pdf' })
assert.assert(timestamp.match(/^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$/))
},
)

Deno.test(
'generateFileName should return correct filename for pdf type',
() => {
const timestamp = /^prefix_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.pdf$/
assert.assert(generateFileName('prefix', 'pdf').match(timestamp))
},
)

Deno.test('generateFileName should handle empty prefix', () => {
const tm = getTimestamp({ type: 'log' })
assert.assertEquals(generateFileName('', 'log'), `_${tm}.log`)
const timestamp = /^_\d{4}-\d{2}-\d{2}\.log$/
assert.assert(generateFileName('', 'log').match(timestamp))
})

Deno.test(
'humanizeDuration should return correct duration string for 0 milliseconds',
() => {
assert.assertEquals(humanizeDuration(0), '0s')
},
)

Deno.test(
'humanizeDuration should return correct duration string for less than a minute',
() => {
assert.assertEquals(humanizeDuration(5000), '5s')
assert.assertEquals(humanizeDuration(59000), '59s')
},
)

Deno.test(
'humanizeDuration should return correct duration string for minutes',
() => {
assert.assertEquals(humanizeDuration(60000), '1m')
assert.assertEquals(humanizeDuration(120000), '2m')
},
)

Deno.test(
'humanizeDuration should return correct duration string for hours',
() => {
assert.assertEquals(humanizeDuration(3600000), '1h')
assert.assertEquals(humanizeDuration(7200000), '2h')
},
)

Deno.test(
'humanizeDuration should return correct duration string for days',
() => {
assert.assertEquals(humanizeDuration(86400000), '1d')
assert.assertEquals(humanizeDuration(172800000), '2d')
},
)

Deno.test(
'humanizeDuration should return correct duration string for combined units',
() => {
assert.assertEquals(humanizeDuration(90000), '1m 30s')
assert.assertEquals(
humanizeDuration(7200000 + 120000),
'2h 2m',
)
assert.assertEquals(
humanizeDuration(172800000 + 7200000 + 120000),
'2d 2h 2m',
)
},
)

Deno.test(
'getCurrentMonth should return correct month name for long type',
() => {
assert.assertEquals(
getCurrentMonth({ type: 'long' }),
new Date().toLocaleString('en-US', { month: 'long' }),
)
},
)

Deno.test(
'getCurrentMonth should return correct month abbreviation for short type',
() => {
assert.assertEquals(
getCurrentMonth({ type: 'short' }),
new Date().toLocaleString('en-US', { month: 'short' }),
)
},
)
123 changes: 0 additions & 123 deletions tests/utils_test.ts

This file was deleted.

Loading