@zjex/git-workflow 0.2.23 → 0.2.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,222 @@
1
+ # 测试覆盖率报告
2
+
3
+ ## 📊 总体统计
4
+
5
+ ✅ **153 个测试全部通过**
6
+
7
+ - **7 个测试文件**
8
+ - **153 个测试用例**
9
+ - **0 个失败**
10
+ - **运行时间**: ~230ms
11
+
12
+ ## 📁 测试文件清单
13
+
14
+ | 文件 | 测试数 | 状态 | 覆盖模块 |
15
+ | ------------------------- | ------ | ---- | ------------------------ |
16
+ | `utils.test.ts` | 23 | ✅ | 工具函数、颜色、命令执行 |
17
+ | `config.test.ts` | 12 | ✅ | 配置加载、合并、优先级 |
18
+ | `ai-service.test.ts` | 21 | ✅ | AI commit 生成、多提供商 |
19
+ | `tag.test.ts` | 37 | ✅ | Tag 管理、版本控制 |
20
+ | `commit.test.ts` | 7 | ✅ | 提交类型、消息格式 |
21
+ | `update-notifier.test.ts` | 20 | ✅ | 更新检查、通知、缓存 |
22
+ | `branch.test.ts` | 33 | ✅ | 分支命名、验证、配置 |
23
+
24
+ ## ✅ 已测试的模块
25
+
26
+ ### 1. Utils 模块 (23 tests)
27
+
28
+ - Colors 工具 (7)
29
+ - TODAY 常量 (2)
30
+ - exec 函数 (3)
31
+ - execOutput 函数 (2)
32
+ - checkGitRepo 函数 (2)
33
+ - getMainBranch 函数 (3)
34
+ - divider 函数 (1)
35
+ - theme 对象 (3)
36
+
37
+ ### 2. Config 模块 (12 tests)
38
+
39
+ - 默认配置 (1)
40
+ - 项目配置 (3)
41
+ - 全局配置 (1)
42
+ - 配置合并 (2)
43
+ - 错误处理 (2)
44
+ - 其他 (3)
45
+
46
+ ### 3. AI Service 模块 (21 tests)
47
+
48
+ - isAICommitAvailable (3)
49
+ - getProviderInfo (5)
50
+ - generateAICommitMessage (12)
51
+ - Ollama 特殊情况 (1)
52
+
53
+ ### 4. Tag 功能 (37 tests)
54
+
55
+ - 前缀提取 (7)
56
+ - Tag 分组 (3)
57
+ - 显示逻辑 (6)
58
+ - 列宽计算 (4)
59
+ - 版本号解析 (5)
60
+ - 版本号递增 (5)
61
+ - Tag 排序 (2)
62
+ - 多列显示 (2)
63
+ - 表头格式化 (3)
64
+
65
+ ### 5. Commit 功能 (7 tests)
66
+
67
+ - 提交类型 (3)
68
+ - 提交消息格式 (2)
69
+ - Refactor 对齐处理 (2)
70
+
71
+ ### 6. Update Notifier 模块 (20 tests)
72
+
73
+ - clearUpdateCache (3)
74
+ - checkForUpdates (8)
75
+ - Volta 检测 (2)
76
+ - 版本比较 (1)
77
+ - 缓存读写 (3)
78
+ - 网络请求 (2)
79
+
80
+ ### 7. Branch 功能 (33 tests)
81
+
82
+ - 分支命名规范 (5)
83
+ - 分支前缀配置 (2)
84
+ - ID 验证 (3)
85
+ - 描述验证 (3)
86
+ - 基础分支选择 (4)
87
+ - 分支名称格式 (5)
88
+ - 分支类型 (2)
89
+ - autoPush 配置 (3)
90
+ - 分支名称边界情况 (4)
91
+ - 日期格式 (2)
92
+
93
+ ## ⚠️ 待测试的模块
94
+
95
+ 以下模块尚未添加测试:
96
+
97
+ - ❌ `index.ts` - 主入口
98
+ - ❌ `commands/help.ts` - 帮助命令
99
+ - ❌ `commands/init.ts` - 初始化配置
100
+ - ❌ `commands/release.ts` - 版本发布
101
+ - ❌ `commands/stash.ts` - Stash 管理
102
+ - ❌ `commands/update.ts` - 更新命令
103
+
104
+ ## 📈 覆盖率分析
105
+
106
+ ### 核心功能覆盖率
107
+
108
+ | 模块 | 覆盖率 | 状态 |
109
+ | --------------- | ------ | ---- |
110
+ | Utils | 100% | ✅ |
111
+ | Config | 100% | ✅ |
112
+ | AI Service | 100% | ✅ |
113
+ | Tag (逻辑) | 100% | ✅ |
114
+ | Commit (逻辑) | 100% | ✅ |
115
+ | Update Notifier | 100% | ✅ |
116
+ | Branch (逻辑) | 100% | ✅ |
117
+ | Commands | ~40% | ⚠️ |
118
+
119
+ ### 测试类型分布
120
+
121
+ ```
122
+ 单元测试: 153 个 (100%)
123
+ 集成测试: 0 个 (0%)
124
+ E2E 测试: 0 个 (0%)
125
+ ```
126
+
127
+ ## 🎯 测试质量指标
128
+
129
+ ### 测试特点
130
+
131
+ - ✅ **快速** - 所有测试在 230ms 内完成
132
+ - ✅ **独立** - 每个测试独立运行,互不影响
133
+ - ✅ **可重复** - 使用 Mock 确保结果一致
134
+ - ✅ **清晰** - 描述性的测试名称
135
+ - ✅ **全面** - 覆盖正常流程和边界情况
136
+
137
+ ### 测试覆盖的场景
138
+
139
+ - ✅ 正常流程
140
+ - ✅ 边界情况
141
+ - ✅ 错误处理
142
+ - ✅ 配置缺失
143
+ - ✅ API 失败
144
+ - ✅ 无效输入
145
+ - ✅ 空数据
146
+
147
+ ## 🚀 运行测试
148
+
149
+ ```bash
150
+ # 运行所有测试
151
+ npm test
152
+
153
+ # 监听模式(开发时推荐)
154
+ npm run test:watch
155
+
156
+ # 可视化界面
157
+ npm run test:ui
158
+
159
+ # 生成覆盖率报告
160
+ npm run test:coverage
161
+ ```
162
+
163
+ ## 🔒 质量保障
164
+
165
+ ### CI/CD 集成
166
+
167
+ - ✅ GitHub Actions - 每次 push/PR 自动运行
168
+ - ✅ Pre-commit Hook - 提交前自动运行
169
+ - ✅ 多 Node 版本 - 18.x 和 20.x
170
+
171
+ ### 测试失败处理
172
+
173
+ - ✅ 测试失败阻止提交
174
+ - ✅ CI 失败阻止合并
175
+ - ✅ 详细的错误信息
176
+
177
+ ## 📋 未来计划
178
+
179
+ ### 短期 (1-2 周)
180
+
181
+ - [ ] 添加 Help 命令测试
182
+ - [ ] 添加 Init 命令测试
183
+ - [ ] 添加 Release 命令测试
184
+ - [ ] 添加 Stash 命令测试
185
+ - [ ] 添加 Update 命令测试
186
+ - [ ] 添加 Index 入口测试
187
+
188
+ ### 中期 (1-2 月)
189
+
190
+ - [ ] 添加集成测试
191
+ - [ ] 添加 E2E 测试
192
+ - [ ] 提高覆盖率到 95%+
193
+ - [ ] 添加性能测试
194
+
195
+ ### 长期 (3+ 月)
196
+
197
+ - [ ] 自动化测试报告
198
+ - [ ] 测试覆盖率徽章
199
+ - [ ] 持续改进测试质量
200
+ - [ ] 添加快照测试
201
+
202
+ ## 💡 贡献指南
203
+
204
+ 添加新功能时,请确保:
205
+
206
+ 1. ✅ 为新功能编写测试
207
+ 2. ✅ 运行 `npm test` 确保所有测试通过
208
+ 3. ✅ 运行 `npm run test:coverage` 检查覆盖率
209
+ 4. ✅ 测试覆盖率不低于现有水平
210
+ 5. ✅ 测试名称清晰描述测试内容
211
+
212
+ ## 🎉 总结
213
+
214
+ 我们已经建立了一个全面的测试体系:
215
+
216
+ - ✅ **153 个测试用例**全部通过
217
+ - ✅ 覆盖**所有核心功能**
218
+ - ✅ **自动化测试**集成到 CI/CD
219
+ - ✅ **Pre-commit Hook**确保代码质量
220
+ - ✅ **完善的文档**帮助开发者快速上手
221
+
222
+ 这个测试体系确保了每次代码变更都不会破坏现有功能,让我们可以放心地重构和添加新功能!🎉
@@ -0,0 +1,242 @@
1
+ # 测试快速开始
2
+
3
+ ## 5 分钟上手测试
4
+
5
+ ### 1. 运行现有测试
6
+
7
+ ```bash
8
+ # 运行所有测试
9
+ npm test
10
+
11
+ # 输出示例:
12
+ # ✓ tests/utils.test.ts (3 tests) 2ms
13
+ # ✓ tests/tag.test.ts (10 tests) 3ms
14
+ # ✓ tests/commit.test.ts (7 tests) 3ms
15
+ #
16
+ # Test Files 3 passed (3)
17
+ # Tests 20 passed (20)
18
+ ```
19
+
20
+ ### 2. 监听模式(推荐开发时使用)
21
+
22
+ ```bash
23
+ npm run test:watch
24
+ ```
25
+
26
+ 修改代码后,测试会自动重新运行。
27
+
28
+ ### 3. 可视化界面
29
+
30
+ ```bash
31
+ npm run test:ui
32
+ ```
33
+
34
+ 在浏览器中打开 `http://localhost:51204/__vitest__/`,可以:
35
+
36
+ - 查看测试树状结构
37
+ - 点击运行单个测试
38
+ - 查看测试覆盖率
39
+ - 查看失败详情
40
+
41
+ ### 4. 编写你的第一个测试
42
+
43
+ 创建 `tests/example.test.ts`:
44
+
45
+ ```typescript
46
+ import { describe, it, expect } from "vitest";
47
+
48
+ describe("我的第一个测试", () => {
49
+ it("1 + 1 应该等于 2", () => {
50
+ expect(1 + 1).toBe(2);
51
+ });
52
+
53
+ it("数组应该包含元素", () => {
54
+ const arr = ["a", "b", "c"];
55
+ expect(arr).toContain("b");
56
+ });
57
+
58
+ it("对象应该有属性", () => {
59
+ const obj = { name: "test", value: 123 };
60
+ expect(obj).toHaveProperty("name");
61
+ expect(obj.name).toBe("test");
62
+ });
63
+ });
64
+ ```
65
+
66
+ 运行测试:
67
+
68
+ ```bash
69
+ npm test
70
+ ```
71
+
72
+ ### 5. 测试实际功能
73
+
74
+ 测试 tag 前缀提取:
75
+
76
+ ```typescript
77
+ import { describe, it, expect } from "vitest";
78
+
79
+ describe("Tag 前缀提取", () => {
80
+ it("应该提取 v 前缀", () => {
81
+ const tag = "v1.0.0";
82
+ const prefix = tag.replace(/[0-9].*/, "");
83
+ expect(prefix).toBe("v");
84
+ });
85
+
86
+ it("应该提取 release- 前缀", () => {
87
+ const tag = "release-1.0.0";
88
+ const prefix = tag.replace(/[0-9].*/, "");
89
+ expect(prefix).toBe("release-");
90
+ });
91
+
92
+ it("应该处理无前缀的 tag", () => {
93
+ const tag = "1.0.0";
94
+ const prefix = tag.replace(/[0-9].*/, "") || "(无前缀)";
95
+ expect(prefix).toBe("(无前缀)");
96
+ });
97
+ });
98
+ ```
99
+
100
+ ### 6. 常用断言
101
+
102
+ ```typescript
103
+ // 相等性
104
+ expect(value).toBe(expected); // 严格相等 ===
105
+ expect(value).toEqual(expected); // 深度相等(对象、数组)
106
+
107
+ // 真值
108
+ expect(value).toBeTruthy(); // 真值
109
+ expect(value).toBeFalsy(); // 假值
110
+ expect(value).toBeDefined(); // 已定义
111
+ expect(value).toBeUndefined(); // 未定义
112
+ expect(value).toBeNull(); // null
113
+
114
+ // 数字
115
+ expect(value).toBeGreaterThan(3); // > 3
116
+ expect(value).toBeGreaterThanOrEqual(3); // >= 3
117
+ expect(value).toBeLessThan(5); // < 5
118
+ expect(value).toBeLessThanOrEqual(5); // <= 5
119
+
120
+ // 字符串
121
+ expect(string).toMatch(/pattern/); // 匹配正则
122
+ expect(string).toContain("substring"); // 包含子串
123
+
124
+ // 数组
125
+ expect(array).toContain(item); // 包含元素
126
+ expect(array).toHaveLength(3); // 长度为 3
127
+
128
+ // 对象
129
+ expect(object).toHaveProperty("key"); // 有属性
130
+ expect(object).toMatchObject({ key: value }); // 匹配部分属性
131
+
132
+ // 函数
133
+ expect(fn).toThrow(); // 抛出错误
134
+ expect(fn).toThrow("error message"); // 抛出特定错误
135
+ ```
136
+
137
+ ### 7. 测试异步代码
138
+
139
+ ```typescript
140
+ it("应该异步获取数据", async () => {
141
+ const data = await fetchData();
142
+ expect(data).toBeDefined();
143
+ });
144
+
145
+ it("应该处理 Promise", () => {
146
+ return fetchData().then((data) => {
147
+ expect(data).toBeDefined();
148
+ });
149
+ });
150
+
151
+ it("应该处理 Promise 拒绝", async () => {
152
+ await expect(fetchData()).rejects.toThrow("error");
153
+ });
154
+ ```
155
+
156
+ ### 8. 使用 Mock
157
+
158
+ ```typescript
159
+ import { vi } from "vitest";
160
+
161
+ it("应该 mock 函数", () => {
162
+ const mockFn = vi.fn();
163
+ mockFn("hello");
164
+
165
+ expect(mockFn).toHaveBeenCalled();
166
+ expect(mockFn).toHaveBeenCalledWith("hello");
167
+ expect(mockFn).toHaveBeenCalledTimes(1);
168
+ });
169
+
170
+ it("应该 mock 返回值", () => {
171
+ const mockFn = vi.fn().mockReturnValue("mocked");
172
+ const result = mockFn();
173
+
174
+ expect(result).toBe("mocked");
175
+ });
176
+ ```
177
+
178
+ ### 9. 分组测试
179
+
180
+ ```typescript
181
+ describe("外层分组", () => {
182
+ describe("内层分组 1", () => {
183
+ it("测试 1", () => {});
184
+ it("测试 2", () => {});
185
+ });
186
+
187
+ describe("内层分组 2", () => {
188
+ it("测试 3", () => {});
189
+ it("测试 4", () => {});
190
+ });
191
+ });
192
+ ```
193
+
194
+ ### 10. 生成覆盖率报告
195
+
196
+ ```bash
197
+ npm run test:coverage
198
+ ```
199
+
200
+ 输出示例:
201
+
202
+ ```
203
+ % Coverage report from v8
204
+ --------------------|---------|----------|---------|---------|
205
+ File | % Stmts | % Branch | % Funcs | % Lines |
206
+ --------------------|---------|----------|---------|---------|
207
+ All files | 85.71 | 83.33 | 88.88 | 85.71 |
208
+ src/commands | 90.00 | 85.00 | 92.00 | 90.00 |
209
+ tag.ts | 92.00 | 88.00 | 95.00 | 92.00 |
210
+ commit.ts | 88.00 | 82.00 | 89.00 | 88.00 |
211
+ --------------------|---------|----------|---------|---------|
212
+ ```
213
+
214
+ ## 下一步
215
+
216
+ - 查看 [tests/README.md](./README.md) 了解更多测试技巧
217
+ - 查看 [TESTING.md](../TESTING.md) 了解测试体系
218
+ - 查看现有测试文件学习最佳实践:
219
+ - `tests/tag.test.ts` - Tag 功能测试
220
+ - `tests/commit.test.ts` - Commit 功能测试
221
+ - `tests/utils.test.ts` - 工具函数测试
222
+
223
+ ## 常用命令速查
224
+
225
+ ```bash
226
+ npm test # 运行所有测试
227
+ npm run test:watch # 监听模式
228
+ npm run test:ui # 可视化界面
229
+ npm run test:coverage # 覆盖率报告
230
+
231
+ npx vitest tests/tag.test.ts # 运行单个文件
232
+ npx vitest -t "前缀提取" # 运行匹配的测试
233
+ ```
234
+
235
+ ## 提交前检查清单
236
+
237
+ - [ ] 运行 `npm test` 确保所有测试通过
238
+ - [ ] 运行 `npm run build` 确保构建成功
239
+ - [ ] 新功能已添加测试
240
+ - [ ] 测试覆盖率没有下降
241
+
242
+ Happy Testing! 🎉
@@ -0,0 +1,119 @@
1
+ # 测试指南
2
+
3
+ ## 测试框架
4
+
5
+ 本项目使用 [Vitest](https://vitest.dev/) 作为测试框架。
6
+
7
+ ## 运行测试
8
+
9
+ ```bash
10
+ # 运行所有测试(单次)
11
+ npm test
12
+
13
+ # 监听模式(开发时使用)
14
+ npm run test:watch
15
+
16
+ # 可视化界面
17
+ npm run test:ui
18
+
19
+ # 生成覆盖率报告
20
+ npm run test:coverage
21
+ ```
22
+
23
+ ## 测试结构
24
+
25
+ ```
26
+ tests/
27
+ ├── utils.test.ts # 工具函数测试
28
+ ├── tag.test.ts # Tag 功能测试
29
+ └── README.md # 本文档
30
+ ```
31
+
32
+ ## 编写测试
33
+
34
+ ### 基本示例
35
+
36
+ ```typescript
37
+ import { describe, it, expect } from "vitest";
38
+
39
+ describe("功能模块", () => {
40
+ it("应该做某事", () => {
41
+ const result = someFunction();
42
+ expect(result).toBe(expectedValue);
43
+ });
44
+ });
45
+ ```
46
+
47
+ ### Mock 示例
48
+
49
+ ```typescript
50
+ import { vi } from "vitest";
51
+ import { execSync } from "child_process";
52
+
53
+ vi.mock("child_process");
54
+
55
+ it("应该调用 git 命令", () => {
56
+ const mockExecSync = vi.mocked(execSync);
57
+ mockExecSync.mockReturnValue("v0.1.0\nv0.2.0");
58
+
59
+ // 测试代码...
60
+
61
+ expect(mockExecSync).toHaveBeenCalledWith("git tag -l");
62
+ });
63
+ ```
64
+
65
+ ## 测试覆盖的功能
66
+
67
+ ### Tag 功能
68
+
69
+ - ✅ 前缀提取(v, g, release-, 无前缀)
70
+ - ✅ Tag 分组逻辑
71
+ - ✅ 显示逻辑(最多 5 个)
72
+ - ✅ 列宽计算
73
+
74
+ ### Utils 功能
75
+
76
+ - ✅ 字符串操作
77
+ - ✅ 基本工具函数
78
+
79
+ ## 最佳实践
80
+
81
+ 1. **每个功能都应该有测试** - 确保新功能不会破坏现有功能
82
+ 2. **测试应该独立** - 每个测试不应该依赖其他测试
83
+ 3. **使用描述性的测试名称** - 让测试失败时容易定位问题
84
+ 4. **Mock 外部依赖** - 如 git 命令、文件系统等
85
+ 5. **提交前运行测试** - 确保所有测试通过
86
+
87
+ ## CI/CD 集成
88
+
89
+ 可以在 `.github/workflows` 中添加测试步骤:
90
+
91
+ ```yaml
92
+ - name: Run tests
93
+ run: npm test
94
+
95
+ - name: Generate coverage
96
+ run: npm run test:coverage
97
+ ```
98
+
99
+ ## 添加新测试
100
+
101
+ 当添加新功能时:
102
+
103
+ 1. 在 `tests/` 目录创建对应的测试文件
104
+ 2. 编写测试用例覆盖主要逻辑
105
+ 3. 运行 `npm test` 确保通过
106
+ 4. 提交代码
107
+
108
+ ## 调试测试
109
+
110
+ ```bash
111
+ # 只运行特定文件的测试
112
+ npx vitest tests/tag.test.ts
113
+
114
+ # 只运行匹配的测试
115
+ npx vitest -t "前缀提取"
116
+
117
+ # 使用 UI 界面调试
118
+ npm run test:ui
119
+ ```