codehere 2.0.4 → 2.1.0-rc.2
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/README.md +47 -14
- package/bin/codehere.js +11 -1
- package/package.json +1 -1
- package/src/audit.js +163 -0
- package/src/brain.js +298 -13
- package/src/commands.js +187 -65
- package/src/hooks.js +417 -64
- package/src/signals.js +49 -19
package/src/signals.js
CHANGED
|
@@ -75,6 +75,15 @@ const REPAIR =
|
|
|
75
75
|
const POSITIVE = /\b(thanks|thank you|thx|works now|working now|great|nice|good|perfect|awesome|mantap|makasih|terima kasih|sip|berhasil|sudah bisa|udah bisa)\b/i;
|
|
76
76
|
|
|
77
77
|
// "still broken", "fix it", "masih error": short, no specifics, asks for a repair.
|
|
78
|
+
// The person's answer to a draft: a yes word, optionally with one polite word. A fixed list, in the
|
|
79
|
+
// style of askApproval(), so no model decides what is saved for the team.
|
|
80
|
+
const YES = new Set(['yes', 'y', 'yep', 'yeah', 'ok', 'okay', 'sure', 'save', 'save it', 'go ahead', 'do it', 'ya', 'iya', 'oke', 'boleh', 'simpan']);
|
|
81
|
+
export function plainYes(p) {
|
|
82
|
+
const t = String(p || '').trim().toLowerCase().replace(/[.!]+$/, '').trim();
|
|
83
|
+
const m = /^(.+?)(?:[\s,]+(?:please|thanks|thank you|makasih|dong))?$/.exec(t);
|
|
84
|
+
return !!m && YES.has(m[1].trim());
|
|
85
|
+
}
|
|
86
|
+
|
|
78
87
|
export function lowInfoPrompt(p) {
|
|
79
88
|
const w = words(p);
|
|
80
89
|
if (!w.length || w.length > 8 || SPECIFIC.test(p) || POSITIVE.test(p)) return false;
|
|
@@ -123,27 +132,35 @@ export function similarity(a, b) {
|
|
|
123
132
|
// ---------- test tampering ----------
|
|
124
133
|
|
|
125
134
|
const TEST_FILE = /(^|\/)(test|tests|__tests__|spec)\/|\.(test|spec)\.[a-z]+$|_test\.(go|py)$|(^|\/)test_[^/]+\.py$/i;
|
|
135
|
+
export const isTestFile = (file) => TEST_FILE.test(file);
|
|
136
|
+
|
|
137
|
+
// Kinds: the first three weaken the tests themselves, which is what `codehere audit` counts;
|
|
138
|
+
// checks-off turns off a check around them (hooks, types, the test script's exit code).
|
|
139
|
+
const DELETED = 'test-deleted';
|
|
140
|
+
const SKIPPED = 'test-skipped';
|
|
141
|
+
const WEAKENED = 'assertion-weakened';
|
|
142
|
+
const CHECKS_OFF = 'checks-off';
|
|
126
143
|
|
|
127
144
|
const TAMPER_ADDED = [
|
|
128
|
-
{ re: /\b(it|test|describe)\.skip\s*\(/, why: 'a test was skipped (.skip)' },
|
|
129
|
-
{ re: /\b(xit|xdescribe|xtest)\s*\(/, why: 'a test was disabled (xit/xdescribe)' },
|
|
130
|
-
{ re: /@pytest\.mark\.skip|unittest\.skip/, why: 'a Python test was skipped' },
|
|
131
|
-
{ re: /@ts-nocheck/, why: 'type checking was turned off (@ts-nocheck)' },
|
|
132
|
-
{ re: /--no-verify/, why: 'git hooks were bypassed (--no-verify)' },
|
|
133
|
-
{ re: /expect\([^)]*\)\.(toBe|toEqual)\(\s*(true|1)\s*\)\s*;?\s*\/\/\s*(todo|temp|hack)/i, why: 'an assertion was replaced with a placeholder' },
|
|
145
|
+
{ re: /\b(it|test|describe)\.skip\s*\(/, kind: SKIPPED, why: 'a test was skipped (.skip)' },
|
|
146
|
+
{ re: /\b(xit|xdescribe|xtest)\s*\(/, kind: SKIPPED, why: 'a test was disabled (xit/xdescribe)' },
|
|
147
|
+
{ re: /@pytest\.mark\.skip|unittest\.skip/, kind: SKIPPED, why: 'a Python test was skipped' },
|
|
148
|
+
{ re: /@ts-nocheck/, kind: CHECKS_OFF, why: 'type checking was turned off (@ts-nocheck)' },
|
|
149
|
+
{ re: /--no-verify/, kind: CHECKS_OFF, why: 'git hooks were bypassed (--no-verify)' },
|
|
150
|
+
{ re: /expect\([^)]*\)\.(toBe|toEqual)\(\s*(true|1)\s*\)\s*;?\s*\/\/\s*(todo|temp|hack)/i, kind: WEAKENED, why: 'an assertion was replaced with a placeholder' },
|
|
134
151
|
];
|
|
135
152
|
|
|
136
153
|
// Only in test files: elsewhere `{ skip: true }` or `assert.equal(1, 1)` can be ordinary code.
|
|
137
154
|
const LITERAL = String.raw`(\d+(?:\.\d+)?|true|false|null|'[^']*'|"[^"]*")`;
|
|
138
155
|
const TAMPER_ADDED_IN_TESTS = [
|
|
139
156
|
// test('name', { skip: true }, ...): the name cannot run past its closing quote, so the options are this test's
|
|
140
|
-
{ re: /(?<![.\w])(it|test|describe|suite)\s*\(\s*(['"`])(?:(?!\2)[^\\]|\\.)*\2\s*,\s*\{[^}]*\b(skip|todo)\s*:\s*(true\b|['"`])/, why: 'a test was skipped ({ skip: true })' },
|
|
157
|
+
{ re: /(?<![.\w])(it|test|describe|suite)\s*\(\s*(['"`])(?:(?!\2)[^\\]|\\.)*\2\s*,\s*\{[^}]*\b(skip|todo)\s*:\s*(true\b|['"`])/, kind: SKIPPED, why: 'a test was skipped ({ skip: true })' },
|
|
141
158
|
// fit/fdescribe only in jest's form, starting a statement with a quoted name then a callback:
|
|
142
159
|
// `fit(` alone is a common function name
|
|
143
|
-
{ re: /(?<![.\w])(it|test|describe|context|suite)\.only\s*\(|^[+-]\s*(fit|fdescribe)\s*\(\s*(['"`])(?:(?!\3)[^\\]|\\.)*\3\s*,\s*(async\s+)?(\(|function\b|\w+\s*=>)/, why: 'other tests were turned off (.only)' },
|
|
144
|
-
{ re: /\bassert(\.ok)?\(\s*true\s*\)|^\+\s*assert\s+True\s*$/, why: 'an assertion that cannot fail was added' },
|
|
145
|
-
{ re: new RegExp(String.raw`\bassert\.\w+\(\s*${LITERAL}\s*,\s*\1\s*\)`), why: 'an assertion that cannot fail was added' },
|
|
146
|
-
{ re: new RegExp(String.raw`\bexpect\(\s*${LITERAL}\s*\)\.(toBe|toEqual|toStrictEqual)\(\s*\1\s*\)`), why: 'an assertion that cannot fail was added' },
|
|
160
|
+
{ re: /(?<![.\w])(it|test|describe|context|suite)\.only\s*\(|^[+-]\s*(fit|fdescribe)\s*\(\s*(['"`])(?:(?!\3)[^\\]|\\.)*\3\s*,\s*(async\s+)?(\(|function\b|\w+\s*=>)/, kind: SKIPPED, why: 'other tests were turned off (.only)' },
|
|
161
|
+
{ re: /\bassert(\.ok)?\(\s*true\s*\)|^\+\s*assert\s+True\s*$/, kind: WEAKENED, why: 'an assertion that cannot fail was added' },
|
|
162
|
+
{ re: new RegExp(String.raw`\bassert\.\w+\(\s*${LITERAL}\s*,\s*\1\s*\)`), kind: WEAKENED, why: 'an assertion that cannot fail was added' },
|
|
163
|
+
{ re: new RegExp(String.raw`\bexpect\(\s*${LITERAL}\s*\)\.(toBe|toEqual|toStrictEqual)\(\s*\1\s*\)`), kind: WEAKENED, why: 'an assertion that cannot fail was added' },
|
|
147
164
|
];
|
|
148
165
|
// assert(...), assert.equal(...), expect(...), self.assertEqual(...), and pytest's bare `assert x`;
|
|
149
166
|
// not expect.objectContaining(...) and the other matchers inside one assertion
|
|
@@ -152,11 +169,24 @@ const ASSERT = /\b(expect\s*\(|assert[\w.]*\s*\()|^[+-]\s*assert\s/;
|
|
|
152
169
|
const ALWAYS_PASSES = /"test"\s*:\s*"[^"]*(\|\|\s*(true|:|exit\s+0|echo\b[^"&|;]*)|;\s*(true|exit\s+0))\s*"/;
|
|
153
170
|
const COMMENT = /^[+-]\s*(\/\/|#|\/\*|\*)\s*/;
|
|
154
171
|
|
|
172
|
+
// What the finish check prints: the events' text, in order, each once.
|
|
155
173
|
export function tamperFindings(nameStatus, diff) {
|
|
156
|
-
|
|
174
|
+
return tamperEvents(nameStatus, diff).map((e) => e.why);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Each finding as { kind, file, why }; file is undefined for the count of removed assertions,
|
|
178
|
+
// which spans the whole diff. A why already seen is not repeated.
|
|
179
|
+
export function tamperEvents(nameStatus, diff) {
|
|
180
|
+
const events = [];
|
|
181
|
+
const seen = new Set();
|
|
182
|
+
const add = (kind, file, why) => {
|
|
183
|
+
if (seen.has(why)) return;
|
|
184
|
+
seen.add(why);
|
|
185
|
+
events.push({ kind, file, why });
|
|
186
|
+
};
|
|
157
187
|
for (const line of nameStatus.split('\n')) {
|
|
158
188
|
const [status, file] = line.split(/\s+/);
|
|
159
|
-
if (status === 'D' && file && TEST_FILE.test(file))
|
|
189
|
+
if (status === 'D' && file && TEST_FILE.test(file)) add(DELETED, file, `test file deleted: ${file}`);
|
|
160
190
|
}
|
|
161
191
|
let current = '';
|
|
162
192
|
let removedAsserts = 0;
|
|
@@ -175,14 +205,14 @@ export function tamperFindings(nameStatus, diff) {
|
|
|
175
205
|
const probe = line.slice(0, 10000); // the patterns can take quadratic time on a long line
|
|
176
206
|
if (/(^|\/)package\.json$/.test(current) && ALWAYS_PASSES.test(probe)) {
|
|
177
207
|
if (line.startsWith('-')) hadFallback = true;
|
|
178
|
-
else if (!hadFallback)
|
|
208
|
+
else if (!hadFallback) add(CHECKS_OFF, current, `the test script was changed to always exit 0 in ${current}`);
|
|
179
209
|
}
|
|
180
210
|
if (line.startsWith('+')) {
|
|
181
|
-
for (const t of TAMPER_ADDED) if (t.re.test(probe))
|
|
211
|
+
for (const t of TAMPER_ADDED) if (t.re.test(probe)) add(t.kind, current, `${t.why} in ${current}`);
|
|
182
212
|
if (!inTest) continue;
|
|
183
|
-
for (const t of TAMPER_ADDED_IN_TESTS) if (t.re.test(probe))
|
|
213
|
+
for (const t of TAMPER_ADDED_IN_TESTS) if (t.re.test(probe)) add(t.kind, current, `${t.why} in ${current}`);
|
|
184
214
|
if (COMMENT.test(line)) {
|
|
185
|
-
if (removed.has(line.replace(COMMENT, '').trim()))
|
|
215
|
+
if (removed.has(line.replace(COMMENT, '').trim())) add(WEAKENED, current, `an assertion was commented out in ${current}`);
|
|
186
216
|
} else if (ASSERT.test(line)) addedAsserts++;
|
|
187
217
|
} else if (line.startsWith('-') && inTest && !COMMENT.test(line) && ASSERT.test(line)) {
|
|
188
218
|
removedAsserts++;
|
|
@@ -190,9 +220,9 @@ export function tamperFindings(nameStatus, diff) {
|
|
|
190
220
|
}
|
|
191
221
|
}
|
|
192
222
|
if (removedAsserts - addedAsserts >= 2) {
|
|
193
|
-
|
|
223
|
+
add(WEAKENED, undefined, `${removedAsserts - addedAsserts} assertions removed from test files`);
|
|
194
224
|
}
|
|
195
|
-
return
|
|
225
|
+
return events;
|
|
196
226
|
}
|
|
197
227
|
|
|
198
228
|
// ---------- redaction for anything that leaves the machine ----------
|