-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpand.test.ts
More file actions
135 lines (115 loc) · 4.98 KB
/
Copy pathexpand.test.ts
File metadata and controls
135 lines (115 loc) · 4.98 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { describe, it, expect } from 'vitest';
import { expand } from '../../src/expander';
describe('expand', () => {
const queryExpansion = {
synonyms: {
'折线图': ['line chart', '折线'],
'tooltip': ['提示框', '提示', 'hover'],
'animation': ['动效', 'animate', 'transition'],
'config': ['配置', 'configuration', '设置'],
},
};
it('should expand CN chart type to EN equivalent', () => {
const result = expand('折线图', queryExpansion);
expect(result).toContain('line chart');
});
it('should expand EN term to CN equivalent', () => {
const result = expand('tooltip', queryExpansion);
expect(result).toContain('提示框');
});
it('should not duplicate terms already in query', () => {
const result = expand('tooltip 提示框', queryExpansion);
const promptCount = result.split('提示框').length - 1;
expect(promptCount).toBe(1);
});
it('should expand multiple terms in one query', () => {
const result = expand('tooltip config', queryExpansion);
expect(result).toContain('提示框');
expect(result).toContain('配置');
});
it('should preserve original query text', () => {
const result = expand('animation settings', queryExpansion);
expect(result.startsWith('animation settings')).toBe(true);
});
it('should return original query when no synonyms match', () => {
const result = expand('random unrelated terms', queryExpansion);
expect(result).toBe('random unrelated terms');
});
it('should handle empty query', () => {
const result = expand('', queryExpansion);
expect(result).toBe('');
});
it('should return original query with no synonyms', () => {
const result = expand('tooltip configuration');
expect(result).toBe('tooltip configuration');
});
it('should return original query with empty synonyms', () => {
const result = expand('tooltip configuration', {});
expect(result).toBe('tooltip configuration');
});
it('should return query unchanged when queryExpansion is false', () => {
const result = expand('折线图配置', false);
expect(result).toBe('折线图配置');
});
it('should handle CJK terms with substring match', () => {
// Line 14: containsCJK(term) returns true, so substring match is used
const result = expand('折线图', queryExpansion);
expect(result).toContain('折线');
});
it('should handle term at start of query', () => {
// Line 18-28: while loop with word boundary at start
const result = expand('tooltip chart', queryExpansion);
expect(result).toContain('提示框');
});
it('should handle term at end of query', () => {
// Test term matching at end of query
const result = expand('chart tooltip', queryExpansion);
expect(result).toContain('提示框');
});
it('should handle term in middle of query', () => {
const result = expand('show tooltip here', queryExpansion);
expect(result).toContain('提示框');
});
it('should not add synonym already in query', () => {
// Line 45: containsTerm check prevents duplication
const result = expand('tooltip 提示框', queryExpansion);
const parts = result.split(' ');
const promptCount = parts.filter(p => p === '提示框').length;
expect(promptCount).toBe(1);
});
it('should handle synonyms with already added terms', () => {
// Multiple terms matching same synonym
const result = expand('折线图 折线', queryExpansion);
expect(result).toContain('line chart');
});
it('should handle term at word boundary returning true immediately', () => {
// This tests the path where containsTerm finds a match and returns true (line 26)
// Direct term at word boundary
const result = expand('test tooltip config', queryExpansion);
expect(result).toContain('提示');
});
it('should not match partial Latin substring in word', () => {
// This tests the path where term is found but not at word boundaries (lines 27-29)
// Searching for "config" in "configured" - gets past include check but not boundary check
const testExpansion = {
synonyms: { 'config': ['setting'] }
};
const result = expand('configured', testExpansion);
// "config" is part of "configured", not a standalone word - should not match
expect(result).toBe('configured');
});
it('should handle term found but never at word boundary', () => {
// This tests line 29-30: term found but all occurrences fail boundary check
// Using a term that appears only as part of a longer word
const testExpansion = {
synonyms: { 'abc': ['xyz'] }
};
// "abc" does NOT appear in this text at all - so line 11 returns false early
// Need text that contains "abc" but never as a standalone word
const result = expand('xabcy', testExpansion);
// "abc" is in "xabcy" but not at word boundary (surrounded by letters)
// This will go through the loop, never find a boundary match, then return false
// This triggers lines 29-30
expect(result).toBe('xabcy');
});
});