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
|
# File 'lib/rspec_let_analyzer/formatters/llm_formatter.rb', line 8
def format(analyzer:, limit:, sort_by:)
sorted = analyzer.top(limit, sort_by)
sort_context = case sort_by
when 'total'
'Files sorted by total score (highest combined complexity)'
when 'root'
'Files sorted by root lets (most shared fixture setup)'
when 'it'
'Files sorted by it blocks (most test repetition)'
when 'redef'
'Files sorted by redefinitions (most complex refactoring)'
when 'before'
'Files sorted by before creates (most factory usage in before blocks)'
else
"Files sorted by #{sort_by}"
end
result = {
mission: 'Refactor let/let! to let_it_be for performance improvements',
sort_context: sort_context,
instructions: {
workflow: [
'1. Run the spec file FIRST to establish baseline: rspec <file>',
'2. Record the baseline runtime (excluding load time - shown in rspec output)',
'3. Review file and identify let_it_be conversion candidates',
'4. Make changes to ONLY let/let! declarations',
'5. Run specs again and compare runtime',
"6. If tests fail, try adding 'refind: true' option",
'7. Calculate performance improvement percentage',
'8. If improvement is <20%, ask user if change is worth keeping',
'9. Report: original time vs new time and % improvement'
],
decision_criteria: {
auto_keep: 'Performance improvement >= 20%',
ask_user: 'Performance improvement < 20% (marginal gains may not justify refactor)',
revert: 'Tests fail even with refind: true, or performance degrades'
},
requirements: [
'MUST preserve all existing test examples',
'MUST NOT reduce test coverage',
'MUST NOT change test behavior',
'Only refactor fixture setup (let/let! declarations)'
]
},
top_candidates: build_candidates(sorted, analyzer),
refactoring_guidelines: {
let_refactoring: 'let(:user) { create(:user) } → let_it_be(:user) { create(:user) }',
before_block_refactoring: 'before blocks with create() calls can be converted to before_all or extracted to let_it_be',
if_tests_fail: "Add refind option: let_it_be(:user, refind: true) { create(:user) }",
docs: 'https://test-prof.evilmartians.io/#/let_it_be'
}
}
JSON.pretty_generate(result)
end
|