-
Notifications
You must be signed in to change notification settings - Fork 170
/
helper_test.ts
55 lines (46 loc) · 2.16 KB
/
helper_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { assert, assertEquals } from 'std/testing/asserts.ts'
import util from '../shared/util.ts'
import { VERSION } from '../version.ts'
import {
computeHash,
formatBytesWithColor,
getAlephPkgUri,
toRelativePath,
toLocalPath
} from './helper.ts'
Deno.test('server/helper: getAlephPkgUri dev', () => {
const port = 3000
Deno.env.set('ALEPH_DEV_PORT', port.toString())
assert(getAlephPkgUri().endsWith(port.toString()))
})
Deno.test('server/helper: getAlephPkgUri non-dev', () => {
Deno.env.delete('ALEPH_DEV_PORT')
assert(getAlephPkgUri().endsWith(VERSION))
})
Deno.test('server/helper: toRelativePath', () => {
assertEquals(toRelativePath('/baz/foobar', '/baz/aleph'), '../aleph')
assertEquals(toRelativePath('baz/foobar', 'baz/aleph'), '../aleph')
assertEquals(toRelativePath('baz/foobar', 'baz/foobar/aleph'), './aleph')
})
Deno.test('server/helper: toLocalPath', () => {
assertEquals(toLocalPath('https://foo.com/lib@0.1.0?action'), `/-/foo.com/lib@0.1.0.${util.btoaUrl('action')}.js`)
assertEquals(toLocalPath('https://deno.land/x/aleph@v0.3.0-alpha.29/'), '/-/deno.land/x/aleph@v0.3.0-alpha.29/')
assertEquals(toLocalPath('http://foo.com/bar?lang=us-en'), `/-/http_foo.com/bar.${util.btoaUrl('lang=us-en')}.js`)
assertEquals(toLocalPath('http://foo.com:8080/bar'), '/-/http_foo.com_8080/bar')
assertEquals(toLocalPath('file://foo/bar/'), 'foo/bar/')
assertEquals(toLocalPath('/foo/bar/'), '/foo/bar/')
})
Deno.test('server/helper: computeHash', () => {
assertEquals(computeHash('hello world!'), '430ce34d020724ed75a196dfc2ad67c77772d169')
assertEquals(computeHash(new Uint8Array([21, 31])), 'b0d04c3ac51b86296251d73c20f348e9ae0042a4')
})
Deno.test('server/helper: formatBytesWithColor', () => {
const OneLeftShift20 = 1048576 // 1 << 20 = 1048576 (1MB)
const TenLeftShift20 = 10485760 // 10 << 20 = 10485760 (10MB)
const OneMb = OneLeftShift20
const OneHalfMb = OneLeftShift20 + 512 * 1024
const ElevenMb = TenLeftShift20 + 1024
assertEquals(formatBytesWithColor(OneMb), "\x1b[2m1MB\x1b[22m")
assertEquals(formatBytesWithColor(OneHalfMb), "\x1b[33m1.5MB\x1b[39m")
assertEquals(formatBytesWithColor(ElevenMb), "\x1b[31m11MB\x1b[39m")
})