Skip to content

Commit f0a7da6

Browse files
committed
test
1 parent 4d9e03b commit f0a7da6

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

packages/cache/test/cache.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { CACHE_PATH, ROOT_PATH } from '../src/paths.js'
3+
4+
describe('paths', () => {
5+
describe('ROOT_PATH', () => {
6+
it('should be a valid path', () => {
7+
expect(ROOT_PATH).toBeDefined()
8+
expect(typeof ROOT_PATH).toBe('string')
9+
})
10+
11+
it('should point to a packages directory', () => {
12+
expect(ROOT_PATH).toContain('packages')
13+
})
14+
})
15+
16+
describe('CACHE_PATH', () => {
17+
it('should be a valid path', () => {
18+
expect(CACHE_PATH).toBeDefined()
19+
expect(typeof CACHE_PATH).toBe('string')
20+
})
21+
22+
it('should be a subdirectory of ROOT_PATH', () => {
23+
expect(CACHE_PATH).toContain(ROOT_PATH)
24+
})
25+
})
26+
})

packages/git/test/git.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from 'vitest'
2+
import getCurrentBranch from '../src/getCurrentBranch.js'
3+
import getGitVersion from '../src/getGitVersion.js'
4+
import getIsGitProject from '../src/getIsGitProject.js'
5+
6+
describe('git functions', () => {
7+
describe('getGitVersion', () => {
8+
it('should return a version string', () => {
9+
const version = getGitVersion()
10+
11+
expect(version).toBeDefined()
12+
expect(typeof version).toBe('string')
13+
})
14+
15+
it('should return a valid semver format', () => {
16+
const version = getGitVersion()
17+
18+
expect(version).toMatch(/^\d+\.\d+\.\d+/)
19+
})
20+
})
21+
22+
describe('getIsGitProject', () => {
23+
it('should return true when in a git repository', () => {
24+
// This test runs in the gitmars repo which is a git project
25+
const result = getIsGitProject()
26+
27+
expect(result).toBeTruthy()
28+
})
29+
})
30+
31+
describe('getCurrentBranch', () => {
32+
it('should return the current branch name', () => {
33+
const branch = getCurrentBranch()
34+
35+
expect(branch).toBeDefined()
36+
expect(typeof branch).toBe('string')
37+
expect(branch.length).toBeGreaterThan(0)
38+
})
39+
40+
it('should return a valid branch name', () => {
41+
const branch = getCurrentBranch()
42+
43+
// Branch names should not contain spaces
44+
expect(branch).not.toMatch(/\s/)
45+
})
46+
})
47+
})

packages/hook/test/hook.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { isGhooks, isGitmars, isHusky, isPreCommit, isYorkie } from '../src/getHookType.js'
3+
4+
describe('getHookType', () => {
5+
describe('isHusky', () => {
6+
it('should return true for husky hooks', () => {
7+
expect(isHusky('# husky\n#!/bin/sh')).toBeTruthy()
8+
expect(isHusky('#husky\n#!/bin/sh')).toBeTruthy()
9+
})
10+
11+
it('should return false for non-husky hooks', () => {
12+
expect(isHusky('#!/bin/sh')).toBeFalsy()
13+
expect(isHusky('# gitmars hook')).toBeFalsy()
14+
})
15+
16+
it('should handle empty string', () => {
17+
expect(isHusky('')).toBeFalsy()
18+
})
19+
})
20+
21+
describe('isGitmars', () => {
22+
it('should return true for gitmars hooks', () => {
23+
expect(isGitmars('# gitmars\n#!/bin/sh')).toBeTruthy()
24+
})
25+
26+
it('should return false for non-gitmars hooks', () => {
27+
expect(isGitmars('#!/bin/sh')).toBeFalsy()
28+
expect(isGitmars('# husky\n#!/bin/sh')).toBeFalsy()
29+
})
30+
31+
it('should handle empty string', () => {
32+
expect(isGitmars('')).toBeFalsy()
33+
})
34+
})
35+
36+
describe('isYorkie', () => {
37+
it('should return true for yorkie hooks', () => {
38+
expect(isYorkie('#yorkie\n#!/bin/sh')).toBeTruthy()
39+
})
40+
41+
it('should return false for non-yorkie hooks', () => {
42+
expect(isYorkie('#!/bin/sh')).toBeFalsy()
43+
expect(isYorkie('# husky\n#!/bin/sh')).toBeFalsy()
44+
})
45+
46+
it('should handle empty string', () => {
47+
expect(isYorkie('')).toBeFalsy()
48+
})
49+
})
50+
51+
describe('isGhooks', () => {
52+
it('should return true for ghooks hooks', () => {
53+
expect(isGhooks('// Generated by ghooks. Do not edit this file.\n#!/bin/sh')).toBeTruthy()
54+
})
55+
56+
it('should return false for non-ghooks hooks', () => {
57+
expect(isGhooks('#!/bin/sh')).toBeFalsy()
58+
expect(isGhooks('# husky\n#!/bin/sh')).toBeFalsy()
59+
})
60+
61+
it('should handle empty string', () => {
62+
expect(isGhooks('')).toBeFalsy()
63+
})
64+
})
65+
66+
describe('isPreCommit', () => {
67+
it('should return true for pre-commit hooks', () => {
68+
expect(isPreCommit('./node_modules/pre-commit/hook')).toBeTruthy()
69+
})
70+
71+
it('should return false for non-pre-commit hooks', () => {
72+
expect(isPreCommit('#!/bin/sh')).toBeFalsy()
73+
expect(isPreCommit('# husky\n#!/bin/sh')).toBeFalsy()
74+
})
75+
76+
it('should handle empty string', () => {
77+
expect(isPreCommit('')).toBeFalsy()
78+
})
79+
})
80+
})

packages/utils/test/utils.test.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { describe, expect, it } from 'vitest'
2+
import getSeconds from '../src/getSeconds.js'
3+
import isWin32 from '../src/isWin32.js'
4+
import stringify from '../src/stringify.js'
5+
import { decodeUnicode, encodeUnicode } from '../src/unicode.js'
6+
7+
describe('getSeconds', () => {
8+
it('should return null for invalid input', () => {
9+
expect(getSeconds('invalid')).toBeNull()
10+
expect(getSeconds('123')).toBeNull()
11+
expect(getSeconds('abc')).toBeNull()
12+
})
13+
14+
it('should convert minutes correctly', () => {
15+
const result = getSeconds('5m')
16+
17+
expect(result).toBeLessThan(Date.now() / 1000)
18+
})
19+
20+
it('should convert hours correctly', () => {
21+
const result = getSeconds('2h')
22+
23+
expect(result).toBeLessThan(Date.now() / 1000)
24+
})
25+
26+
it('should convert days correctly', () => {
27+
const result = getSeconds('1d')
28+
29+
expect(result).toBeLessThan(Date.now() / 1000)
30+
})
31+
32+
it('should convert weeks correctly', () => {
33+
const result = getSeconds('1w')
34+
35+
expect(result).toBeLessThan(Date.now() / 1000)
36+
})
37+
38+
it('should convert months correctly', () => {
39+
const result = getSeconds('1M')
40+
41+
expect(result).toBeLessThan(Date.now() / 1000)
42+
})
43+
44+
it('should convert years correctly', () => {
45+
const result = getSeconds('1y')
46+
47+
expect(result).toBeLessThan(Date.now() / 1000)
48+
})
49+
})
50+
51+
describe('isWin32', () => {
52+
it('should return a boolean', () => {
53+
const result = isWin32()
54+
55+
expect(typeof result).toBe('boolean')
56+
})
57+
58+
it('should return true on Windows platform', () => {
59+
// This test just verifies the function works
60+
// Actual value depends on the platform running the test
61+
expect([true, false]).toContain(isWin32())
62+
})
63+
})
64+
65+
describe('stringify', () => {
66+
it('should return the original string on non-Windows', () => {
67+
const input = 'echo hello'
68+
const result = stringify(input)
69+
70+
// On non-Windows, should return original string
71+
// On Windows with special chars, would be JSON.stringify'd
72+
if (!isWin32()) {
73+
expect(result).toBe(input)
74+
}
75+
})
76+
77+
it('should handle strings with special characters on Windows', () => {
78+
const input = 'echo hello ^ world'
79+
const result = stringify(input)
80+
81+
if (isWin32()) {
82+
expect(result).toBe(JSON.stringify(input))
83+
}
84+
})
85+
86+
it('should handle strings with & character on Windows', () => {
87+
const input = 'cmd1 & cmd2'
88+
const result = stringify(input)
89+
90+
if (isWin32()) {
91+
expect(result).toBe(JSON.stringify(input))
92+
}
93+
})
94+
})
95+
96+
describe('unicode', () => {
97+
describe('encodeUnicode', () => {
98+
it('should encode Chinese characters to unicode', () => {
99+
const result = encodeUnicode('你好')
100+
101+
expect(result).toContain('\\u')
102+
})
103+
104+
it('should handle empty string', () => {
105+
const result = encodeUnicode('')
106+
107+
expect(result).toBe('\\u')
108+
})
109+
110+
it('should handle ASCII characters', () => {
111+
const result = encodeUnicode('abc')
112+
113+
expect(result).toContain('\\u')
114+
})
115+
})
116+
117+
describe('decodeUnicode', () => {
118+
it('should decode unicode to Chinese characters', () => {
119+
const encoded = encodeUnicode('你好')
120+
const decoded = decodeUnicode(encoded)
121+
122+
expect(decoded).toBe('你好')
123+
})
124+
125+
it('should handle empty string', () => {
126+
const result = decodeUnicode('')
127+
128+
expect(result).toBe('')
129+
})
130+
})
131+
132+
describe('roundtrip', () => {
133+
it('should correctly roundtrip Chinese text', () => {
134+
const original = '测试文本'
135+
const encoded = encodeUnicode(original)
136+
const decoded = decodeUnicode(encoded)
137+
138+
expect(decoded).toBe(original)
139+
})
140+
})
141+
})

0 commit comments

Comments
 (0)