@aiready/pattern-detect 0.16.22 → 0.17.0
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.
- package/dist/analyzer-entry/index.mjs +3 -3
- package/dist/chunk-J2G742QF.mjs +162 -0
- package/dist/chunk-J5CW6NYY.mjs +64 -0
- package/dist/chunk-NQBYYWHJ.mjs +143 -0
- package/dist/chunk-SUUZMLPS.mjs +391 -0
- package/dist/cli.js +336 -303
- package/dist/cli.mjs +347 -303
- package/dist/context-rules-entry/index.d.mts +2 -2
- package/dist/context-rules-entry/index.d.ts +2 -2
- package/dist/context-rules-entry/index.js +2 -25
- package/dist/context-rules-entry/index.mjs +1 -1
- package/dist/detector-entry/index.mjs +2 -2
- package/dist/index-szjQDBsm.d.mts +49 -0
- package/dist/index-szjQDBsm.d.ts +49 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -25
- package/dist/index.mjs +6 -4
- package/package.json +2 -2
- package/dist/__tests__/context-rules.test.d.ts +0 -2
- package/dist/__tests__/context-rules.test.d.ts.map +0 -1
- package/dist/__tests__/context-rules.test.js +0 -189
- package/dist/__tests__/context-rules.test.js.map +0 -1
- package/dist/__tests__/detector.test.d.ts +0 -2
- package/dist/__tests__/detector.test.d.ts.map +0 -1
- package/dist/__tests__/detector.test.js +0 -259
- package/dist/__tests__/detector.test.js.map +0 -1
- package/dist/__tests__/grouping.test.d.ts +0 -2
- package/dist/__tests__/grouping.test.d.ts.map +0 -1
- package/dist/__tests__/grouping.test.js +0 -443
- package/dist/__tests__/grouping.test.js.map +0 -1
- package/dist/__tests__/scoring.test.d.ts +0 -2
- package/dist/__tests__/scoring.test.d.ts.map +0 -1
- package/dist/__tests__/scoring.test.js +0 -102
- package/dist/__tests__/scoring.test.js.map +0 -1
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { detectDuplicatePatterns } from '../detector';
|
|
3
|
-
describe('detectDuplicatePatterns', () => {
|
|
4
|
-
it('should detect exact duplicate functions', async () => {
|
|
5
|
-
const files = [
|
|
6
|
-
{
|
|
7
|
-
file: 'file1.ts',
|
|
8
|
-
content: `
|
|
9
|
-
async function getUserData(id: string) {
|
|
10
|
-
const user = await db.users.findOne({ id });
|
|
11
|
-
if (!user) {
|
|
12
|
-
throw new Error('Not found');
|
|
13
|
-
}
|
|
14
|
-
return user;
|
|
15
|
-
}
|
|
16
|
-
`,
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
file: 'file2.ts',
|
|
20
|
-
content: `
|
|
21
|
-
async function getUserInfo(userId: string) {
|
|
22
|
-
const user = await db.users.findOne({ id: userId });
|
|
23
|
-
if (!user) {
|
|
24
|
-
throw new Error('Not found');
|
|
25
|
-
}
|
|
26
|
-
return user;
|
|
27
|
-
}
|
|
28
|
-
`,
|
|
29
|
-
},
|
|
30
|
-
];
|
|
31
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
32
|
-
minSimilarity: 0.6,
|
|
33
|
-
minLines: 5,
|
|
34
|
-
approx: false, // Disable approximation for test reliability
|
|
35
|
-
});
|
|
36
|
-
expect(duplicates.length).toBeGreaterThan(0);
|
|
37
|
-
expect(duplicates[0].similarity).toBeGreaterThan(0.6);
|
|
38
|
-
});
|
|
39
|
-
it('should detect similar but not identical functions', async () => {
|
|
40
|
-
const files = [
|
|
41
|
-
{
|
|
42
|
-
file: 'file1.ts',
|
|
43
|
-
content: `
|
|
44
|
-
async function getUserData(id: string) {
|
|
45
|
-
const user = await database.users.findOne({ id: id });
|
|
46
|
-
if (!user) {
|
|
47
|
-
throw new Error('User not found');
|
|
48
|
-
}
|
|
49
|
-
return user;
|
|
50
|
-
}
|
|
51
|
-
`,
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
file: 'file2.ts',
|
|
55
|
-
content: `
|
|
56
|
-
async function getUserData(userId: string) {
|
|
57
|
-
const user = await database.users.findOne({ id: userId });
|
|
58
|
-
if (!user) {
|
|
59
|
-
throw new Error('User not found');
|
|
60
|
-
}
|
|
61
|
-
return user;
|
|
62
|
-
}
|
|
63
|
-
`,
|
|
64
|
-
},
|
|
65
|
-
];
|
|
66
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
67
|
-
minSimilarity: 0.6,
|
|
68
|
-
minLines: 5,
|
|
69
|
-
approx: false, // Disable approximation for test reliability
|
|
70
|
-
});
|
|
71
|
-
expect(duplicates.length).toBeGreaterThan(0);
|
|
72
|
-
});
|
|
73
|
-
it('should categorize API handler patterns', async () => {
|
|
74
|
-
const files = [
|
|
75
|
-
{
|
|
76
|
-
file: 'file1.ts',
|
|
77
|
-
content: `
|
|
78
|
-
app.get('/api/users/:id', async (request, response) => {
|
|
79
|
-
const user = await db.users.findOne({ id: request.params.id });
|
|
80
|
-
response.json(user);
|
|
81
|
-
});
|
|
82
|
-
`,
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
file: 'file2.ts',
|
|
86
|
-
content: `
|
|
87
|
-
app.get('/api/posts/:id', async (req, res) => {
|
|
88
|
-
const post = await db.posts.findOne({ id: req.params.id });
|
|
89
|
-
res.json(post);
|
|
90
|
-
});
|
|
91
|
-
`,
|
|
92
|
-
},
|
|
93
|
-
];
|
|
94
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
95
|
-
minSimilarity: 0.4,
|
|
96
|
-
minLines: 3,
|
|
97
|
-
approx: false,
|
|
98
|
-
});
|
|
99
|
-
expect(duplicates.length).toBeGreaterThan(0);
|
|
100
|
-
expect(duplicates[0].patternType).toBe('api-handler');
|
|
101
|
-
});
|
|
102
|
-
it('should categorize validator patterns', async () => {
|
|
103
|
-
const files = [
|
|
104
|
-
{
|
|
105
|
-
file: 'file1.ts',
|
|
106
|
-
content: `function validateEmail(email: string) {
|
|
107
|
-
if (!email) {
|
|
108
|
-
throw new Error('Email is required');
|
|
109
|
-
}
|
|
110
|
-
if (!email.includes('@')) {
|
|
111
|
-
throw new Error('Invalid email format');
|
|
112
|
-
}
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
`,
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
file: 'file2.ts',
|
|
119
|
-
content: `function validateUsername(username: string) {
|
|
120
|
-
if (!username) {
|
|
121
|
-
throw new Error('Username is required');
|
|
122
|
-
}
|
|
123
|
-
if (username.length < 3) {
|
|
124
|
-
throw new Error('Username too short');
|
|
125
|
-
}
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
`,
|
|
129
|
-
},
|
|
130
|
-
];
|
|
131
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
132
|
-
minSimilarity: 0.2,
|
|
133
|
-
minLines: 3,
|
|
134
|
-
approx: false,
|
|
135
|
-
});
|
|
136
|
-
expect(duplicates.length).toBeGreaterThan(0);
|
|
137
|
-
expect(duplicates[0].patternType).toBe('validator');
|
|
138
|
-
});
|
|
139
|
-
it('should calculate token cost', async () => {
|
|
140
|
-
const files = [
|
|
141
|
-
{
|
|
142
|
-
file: 'file1.ts',
|
|
143
|
-
content: `
|
|
144
|
-
function processData(data: any) {
|
|
145
|
-
const validated = validateInput(data);
|
|
146
|
-
const result = validated.map((item: any) => item.value);
|
|
147
|
-
const filtered = result.filter((x: any) => x !== null);
|
|
148
|
-
return filtered;
|
|
149
|
-
}
|
|
150
|
-
`,
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
file: 'file2.ts',
|
|
154
|
-
content: `
|
|
155
|
-
function processData(items: any) {
|
|
156
|
-
const validated = validateInput(items);
|
|
157
|
-
const result = validated.map((element: any) => element.value);
|
|
158
|
-
const filtered = result.filter((x: any) => x !== null);
|
|
159
|
-
return filtered;
|
|
160
|
-
}
|
|
161
|
-
`,
|
|
162
|
-
},
|
|
163
|
-
];
|
|
164
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
165
|
-
minSimilarity: 0.5,
|
|
166
|
-
minLines: 3,
|
|
167
|
-
approx: false,
|
|
168
|
-
});
|
|
169
|
-
expect(duplicates.length).toBeGreaterThan(0);
|
|
170
|
-
expect(duplicates[0].tokenCost).toBeGreaterThan(0);
|
|
171
|
-
});
|
|
172
|
-
it('should not detect patterns below similarity threshold', async () => {
|
|
173
|
-
const files = [
|
|
174
|
-
{
|
|
175
|
-
file: 'file1.ts',
|
|
176
|
-
content: `
|
|
177
|
-
function complexFunction(param: string) {
|
|
178
|
-
const result = someComplexLogic(param);
|
|
179
|
-
const transformed = anotherTransform(result);
|
|
180
|
-
return transformed;
|
|
181
|
-
}
|
|
182
|
-
`,
|
|
183
|
-
},
|
|
184
|
-
{
|
|
185
|
-
file: 'file2.ts',
|
|
186
|
-
content: `
|
|
187
|
-
function totallyDifferent() {
|
|
188
|
-
return 42;
|
|
189
|
-
}
|
|
190
|
-
`,
|
|
191
|
-
},
|
|
192
|
-
];
|
|
193
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
194
|
-
minSimilarity: 0.9,
|
|
195
|
-
minLines: 3,
|
|
196
|
-
});
|
|
197
|
-
expect(duplicates.length).toBe(0);
|
|
198
|
-
});
|
|
199
|
-
it('should not compare blocks from the same file', async () => {
|
|
200
|
-
const files = [
|
|
201
|
-
{
|
|
202
|
-
file: 'file1.ts',
|
|
203
|
-
content: `
|
|
204
|
-
function func1() {
|
|
205
|
-
return 1;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function func2() {
|
|
209
|
-
return 2;
|
|
210
|
-
}
|
|
211
|
-
`,
|
|
212
|
-
},
|
|
213
|
-
];
|
|
214
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
215
|
-
minSimilarity: 0.5,
|
|
216
|
-
minLines: 2,
|
|
217
|
-
});
|
|
218
|
-
expect(duplicates.length).toBe(0);
|
|
219
|
-
});
|
|
220
|
-
it('should sort duplicates by similarity and token cost', async () => {
|
|
221
|
-
const files = [
|
|
222
|
-
{
|
|
223
|
-
file: 'file1.ts',
|
|
224
|
-
content: `
|
|
225
|
-
function a() {
|
|
226
|
-
const x = 1;
|
|
227
|
-
return x;
|
|
228
|
-
}
|
|
229
|
-
`,
|
|
230
|
-
},
|
|
231
|
-
{
|
|
232
|
-
file: 'file2.ts',
|
|
233
|
-
content: `
|
|
234
|
-
function b() {
|
|
235
|
-
const x = 1;
|
|
236
|
-
return x;
|
|
237
|
-
}
|
|
238
|
-
`,
|
|
239
|
-
},
|
|
240
|
-
{
|
|
241
|
-
file: 'file3.ts',
|
|
242
|
-
content: `
|
|
243
|
-
function c() {
|
|
244
|
-
const y = 2;
|
|
245
|
-
return y;
|
|
246
|
-
}
|
|
247
|
-
`,
|
|
248
|
-
},
|
|
249
|
-
];
|
|
250
|
-
const duplicates = await detectDuplicatePatterns(files, {
|
|
251
|
-
minSimilarity: 0.7,
|
|
252
|
-
minLines: 2,
|
|
253
|
-
});
|
|
254
|
-
if (duplicates.length > 1) {
|
|
255
|
-
expect(duplicates[0].similarity).toBeGreaterThanOrEqual(duplicates[1].similarity);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
//# sourceMappingURL=detector.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"detector.test.js","sourceRoot":"","sources":["../../src/__tests__/detector.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAEtD,QAAQ,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACvC,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;QACvD,MAAM,KAAK,GAAG;YACZ;gBACE,IAAI,EAAE,UAAU;gBAChB,OAAO,EAAE;;;;;;;;SAQR;aACF;YACD;gBACE,IAAI,EAAE,UAAU;gBAChB,OAAO,EAAE;;;;;;;;SAQR;aACF;SACF,CAAC;QAEF,MAAM,UAAU,GAAG,MAAM,uBAA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