1+ import fs from 'fs' ;
2+ import path from 'path' ;
3+ import { describe , test , expect , beforeAll , afterAll , beforeEach } from 'vitest' ;
4+
5+ // 模拟 downloadTemplate 工具的核心逻辑
6+ function simulateDownloadTemplate ( template , overwrite = false ) {
7+ const workspaceFolder = process . env . WORKSPACE_FOLDER_PATHS || '/tmp/test-workspace' ;
8+ const testReadmePath = path . join ( workspaceFolder , 'README.md' ) ;
9+
10+ // 模拟文件复制逻辑
11+ const shouldSkipReadme = ( template , destPath , overwrite ) => {
12+ const isReadme = path . basename ( destPath ) . toLowerCase ( ) === 'readme.md' ;
13+ const isRulesTemplate = template === 'rules' ;
14+ const exists = fs . existsSync ( destPath ) ;
15+
16+ return isReadme && isRulesTemplate && exists && ! overwrite ;
17+ } ;
18+
19+ const copyFile = async ( src , dest , overwrite , template ) => {
20+ const destExists = fs . existsSync ( dest ) ;
21+
22+ // 检查是否需要跳过 README.md 文件(仅对 rules 模板)
23+ if ( template && shouldSkipReadme ( template , dest , overwrite ) ) {
24+ return { copied : false , reason : 'README.md 文件已存在,已保护' , action : 'protected' } ;
25+ }
26+
27+ // 如果目标文件存在且不允许覆盖
28+ if ( destExists && ! overwrite ) {
29+ return { copied : false , reason : '文件已存在' , action : 'skipped' } ;
30+ }
31+
32+ // 模拟复制成功
33+ return {
34+ copied : true ,
35+ action : destExists ? 'overwritten' : 'created'
36+ } ;
37+ } ;
38+
39+ return copyFile ( 'mock-src' , testReadmePath , overwrite , template ) ;
40+ }
41+
42+ describe ( 'downloadTemplate 集成测试' , ( ) => {
43+ const testWorkspace = '/tmp/test-workspace' ;
44+ const testReadmePath = path . join ( testWorkspace , 'README.md' ) ;
45+
46+ beforeAll ( ( ) => {
47+ // 创建测试工作空间
48+ if ( ! fs . existsSync ( testWorkspace ) ) {
49+ fs . mkdirSync ( testWorkspace , { recursive : true } ) ;
50+ }
51+ } ) ;
52+
53+ afterAll ( ( ) => {
54+ // 清理测试工作空间
55+ if ( fs . existsSync ( testWorkspace ) ) {
56+ fs . rmSync ( testWorkspace , { recursive : true , force : true } ) ;
57+ }
58+ } ) ;
59+
60+ beforeEach ( ( ) => {
61+ // 清理测试 README.md
62+ if ( fs . existsSync ( testReadmePath ) ) {
63+ fs . unlinkSync ( testReadmePath ) ;
64+ }
65+ } ) ;
66+
67+ test ( 'rules 模板 + 存在 README.md + overwrite=false 应该保护文件' , async ( ) => {
68+ // 创建测试 README.md
69+ fs . writeFileSync ( testReadmePath , '# 原有项目文档' ) ;
70+
71+ const result = await simulateDownloadTemplate ( 'rules' , false ) ;
72+
73+ expect ( result . copied ) . toBe ( false ) ;
74+ expect ( result . action ) . toBe ( 'protected' ) ;
75+ expect ( result . reason ) . toContain ( 'README.md 文件已存在,已保护' ) ;
76+ } ) ;
77+
78+ test ( 'rules 模板 + 不存在 README.md + overwrite=false 应该正常复制' , async ( ) => {
79+ const result = await simulateDownloadTemplate ( 'rules' , false ) ;
80+
81+ expect ( result . copied ) . toBe ( true ) ;
82+ expect ( result . action ) . toBe ( 'created' ) ;
83+ } ) ;
84+
85+ test ( 'rules 模板 + 存在 README.md + overwrite=true 应该覆盖文件' , async ( ) => {
86+ // 创建测试 README.md
87+ fs . writeFileSync ( testReadmePath , '# 原有项目文档' ) ;
88+
89+ const result = await simulateDownloadTemplate ( 'rules' , true ) ;
90+
91+ expect ( result . copied ) . toBe ( true ) ;
92+ expect ( result . action ) . toBe ( 'overwritten' ) ;
93+ } ) ;
94+
95+ test ( 'react 模板 + 存在 README.md + overwrite=false 应该跳过(原有行为)' , async ( ) => {
96+ // 创建测试 README.md
97+ fs . writeFileSync ( testReadmePath , '# 原有项目文档' ) ;
98+
99+ const result = await simulateDownloadTemplate ( 'react' , false ) ;
100+
101+ expect ( result . copied ) . toBe ( false ) ;
102+ expect ( result . action ) . toBe ( 'skipped' ) ;
103+ expect ( result . reason ) . toBe ( '文件已存在' ) ;
104+ } ) ;
105+ } ) ;
0 commit comments