Skip to content

Commit 9a8e121

Browse files
committed
feat: Add integration test --story=1
1 parent ccff149 commit 9a8e121

29 files changed

+1393
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ release/
6666
# C/C++ artifacts (for CGO)
6767
*.o
6868
*.a
69+
coverage-data

adapters/rust/extractor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ func isEmptyComment(content string) bool {
9090
default:
9191
return strings.TrimSpace(trimmed) == ""
9292
}
93-
}
93+
}

adapters/translator/llm_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"os"
77
"testing"
88

9+
openai "github.com/sashabaranov/go-openai"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
11-
openai "github.com/sashabaranov/go-openai"
1212
)
1313

1414
// TestBatchTranslationMock tests batch translation using a mock server
@@ -20,17 +20,17 @@ func TestBatchTranslationMock(t *testing.T) {
2020
if !contains(content, `["Hello","World"]`) {
2121
return nil, fmt.Errorf("unexpected prompt: %s", content)
2222
}
23-
23+
2424
// Return valid JSON array
2525
return createMockResponse(`["你好", "世界"]`), nil
2626
}
27-
27+
2828
server1 := NewMockLLMServer(successHandler)
2929
defer server1.Close()
3030

3131
trans1 := NewLLMTranslator("key", server1.URL, "model")
3232
results, err := trans1.TranslateBatch(context.Background(), []string{"Hello", "World"}, "en", "zh-CN")
33-
33+
3434
require.NoError(t, err)
3535
assert.Equal(t, []string{"你好", "世界"}, results)
3636

@@ -72,7 +72,7 @@ func TestDeepSeekIntegration(t *testing.T) {
7272
if testing.Short() {
7373
t.Skip("Skipping integration test in short mode")
7474
}
75-
75+
7676
apiKey := os.Getenv("OPENAI_API_KEY")
7777
if apiKey == "" {
7878
t.Skip("Skipping integration test: OPENAI_API_KEY not set")
@@ -119,7 +119,7 @@ func TestEdgeCases(t *testing.T) {
119119
return createMockResponse(`[]`), nil
120120
})
121121
defer server.Close()
122-
122+
123123
trans := NewLLMTranslator("key", server.URL, "model")
124124
res, err := trans.TranslateBatch(context.Background(), []string{}, "en", "zh")
125125
assert.NoError(t, err)
@@ -132,7 +132,7 @@ func TestEdgeCases(t *testing.T) {
132132
return createMockResponse(`["Quote \"", "Newline \\n"]`), nil
133133
})
134134
defer server2.Close()
135-
135+
136136
trans2 := NewLLMTranslator("key", server2.URL, "model")
137137
res2, err := trans2.TranslateBatch(context.Background(), specials, "en", "zh")
138138
assert.NoError(t, err)

adapters/translator/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func parseBatchResponse(resp string) ([]string, error) {
2525
cleanResp = cleanResp[:idx]
2626
}
2727
}
28-
28+
2929
cleanResp = strings.TrimSpace(cleanResp)
3030

3131
var results []string

adapters/translator/parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ func TestParseBatchResponse(t *testing.T) {
1919
want: []string{"Hello", "World"},
2020
},
2121
{
22-
name: "Markdown JSON",
22+
name: "Markdown JSON",
2323
input: "```json\n[\"Hello\", \"World\"]\n```",
2424
want: []string{"Hello", "World"},
2525
},
2626
{
27-
name: "Markdown without language",
27+
name: "Markdown without language",
2828
input: "```\n[\"Hello\", \"World\"]\n```",
2929
want: []string{"Hello", "World"},
3030
},
@@ -39,7 +39,7 @@ func TestParseBatchResponse(t *testing.T) {
3939
// If LLM says "Here is the JSON: [...]", it might fail.
4040
// We can improve this in T006 Edge Cases if needed.
4141
input: "Here is the JSON: [\"Hello\"]",
42-
wantErr: true,
42+
wantErr: true,
4343
},
4444
}
4545

cmd/codei18n/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func runMapUpdate() {
9696
if _, exists := m.Comments[c.ID]; !exists {
9797
// [MOCK zh-CN->en] // Intelligently detect comment language
9898
detectedLang := utils.DetectLanguage(c.SourceText)
99-
99+
100100
if detectedLang == cfg.LocalLanguage {
101101
// [MOCK zh-CN->en] // The comment is in the local language (Chinese), stored as LocalLanguage
102102
store.Set(c.ID, cfg.LocalLanguage, c.SourceText)

cmd/codei18n/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,4 @@ func loadConfigFile(path string, merge bool) (bool, error) {
112112

113113
func main() {
114114
Execute()
115-
}
115+
}

cmd/codei18n/scan.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
scanDir string
2222
scanFormat string
2323
scanOutput string
24+
scanLang string
2425
scanStdin bool
2526
scanWithTranslations bool
2627
)
@@ -43,6 +44,7 @@ func init() {
4344
scanCmd.Flags().StringVarP(&scanDir, "dir", "d", ".", "指定扫描的目录路径")
4445
scanCmd.Flags().StringVar(&scanFormat, "format", "table", "输出格式 (json, table)")
4546
scanCmd.Flags().StringVarP(&scanOutput, "output", "o", "", "将输出写入指定文件 (默认 stdout)")
47+
scanCmd.Flags().StringVar(&scanLang, "lang", "", "指定目标语言 (覆盖配置)")
4648
scanCmd.Flags().BoolVar(&scanStdin, "stdin", false, "从 stdin 读取文件内容 (必须同时指定 --file)")
4749
scanCmd.Flags().BoolVar(&scanWithTranslations, "with-translations", false, "在 JSON 输出中包含翻译文本")
4850
}
@@ -95,6 +97,9 @@ func runScan() {
9597
} else {
9698
// Populate LocalizedText
9799
targetLang := cfg.LocalLanguage // Default target
100+
if scanLang != "" {
101+
targetLang = scanLang
102+
}
98103
for _, c := range comments {
99104
if text, ok := store.Get(c.ID, targetLang); ok {
100105
c.LocalizedText = text
@@ -128,8 +133,15 @@ func scanSingleFile(filename string) ([]*domain.Comment, error) {
128133
if err != nil {
129134
return nil, err
130135
}
131-
// Pass nil src to read from file
132-
return adapter.Parse(filename, nil)
136+
137+
// Read file content manually to ensure consistency with directory scan
138+
// and to debug potential read issues
139+
content, err := os.ReadFile(filename)
140+
if err != nil {
141+
return nil, fmt.Errorf("read file failed: %w", err)
142+
}
143+
144+
return adapter.Parse(filename, content)
133145
}
134146

135147
func scanDirectory(dir string) ([]*domain.Comment, error) {

cmd/codei18n/translate.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,32 +202,32 @@ func runTranslate() {
202202
// Current simple implementation:
203203
// If direction changes in batch, we can't batch efficiently.
204204
// Let's refine batching strategy.
205-
205+
206206
// Group by direction
207207
// Actually, just calling TranslateBatch with mixed tasks is wrong because TranslateBatch takes `from, to` args.
208208
// So we MUST ensure batch has same direction.
209209
// Given loop order is random, we should probably separate tasks by direction first.
210210
// But for now let's assume we can just check.
211-
212-
// Refined logic: TranslateBatch takes single from/to.
211+
212+
// Refined logic: TranslateBatch takes single from/to.
213213
// So we must verify batch consistency.
214-
214+
215215
// Since we can't easily change the batching structure inside this goroutine loop without complicating it,
216216
// let's do grouping before creating batches.
217-
217+
218218
// Just use the first task's direction. If mixed, we might fail or need to split.
219219
// Actually, let's fix the task collection to be grouped.
220220
// But wait, I already split into batches.
221221
// The correct fix is to sort tasks or group tasks by direction before batching.
222222
// Let's just group by direction in the loop below.
223223
// BUT: simpler is just to ensure TranslateBatch is called with correct params.
224224
// If a batch has mixed directions, we can't use TranslateBatch easily.
225-
225+
226226
// Let's assume for typical usage (filling missing), it's usually En->Zh.
227227
// But mixed is possible.
228228
// I will add a check: if batch has mixed directions, fallback to sequential logic or split it?
229229
// Or better: Sort tasks by direction before batching.
230-
230+
231231
// Let's perform translation
232232
from := currentBatch[0].fromLang
233233
to := currentBatch[0].toLang

core/utils/lang_detect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
func DetectLanguage(text string) string {
1212
// [MOCK zh-CN->en] // Remove comment markers
1313
normalized := NormalizeCommentText(text)
14-
14+
1515
// [MOCK zh-CN->en] // Check if it contains Chinese characters (CJK Unified Ideographs)
1616
for _, r := range normalized {
1717
if unicode.Is(unicode.Han, r) {
1818
return "zh-CN"
1919
}
2020
}
21-
21+
2222
return "en"
2323
}
2424

0 commit comments

Comments
 (0)