import { describe, it, expect } from 'vitest' import { findFirstTopLevelComma, extractMethodName, findClosingParenthesis, needsParentheses, } from '../../src/autofix/parameter-parser' describe('parameter-parser branch coverage improvements', () => { describe('findFirstTopLevelComma edge cases', () => { it('should handle null characters in text', () => { // Test line 85: if (!char) continue // Create a string with null characters that could be falsy const textWithNullChar = 'test\u0000,second' const result = findFirstTopLevelComma(textWithNullChar) expect(result).toBe(5) // Should find comma after null char }) it('should handle text with consecutive null characters', () => { // More edge cases for line 85 const textWithMultipleNulls = 'func(\u0000\u0000arg, second)' const result = findFirstTopLevelComma(textWithMultipleNulls) expect(result).toBe(-1) // No top-level comma (inside parentheses) }) it('should handle empty string characters', () => { // Test various falsy character scenarios const textWithEmptyChars = 'test\u0000\u0001\u0002, second' const result = findFirstTopLevelComma(textWithEmptyChars) expect(result).toBe(7) // Should find comma }) }) describe('extractMethodName edge cases', () => { it('should handle empty method name after split', () => { // Test line 116: methodPart.split('[')[0] || null // When split returns empty string const result = extractMethodName('Array.prototype.[0]') // Method name is empty expect(result).toBe(null) }) it('should handle malformed prototype patterns', () => { // Test various edge cases that could return empty strings expect(extractMethodName('Array.prototype.[')).toBe(null) expect(extractMethodName('Array.prototype.]')).toBe(']') expect(extractMethodName('.prototype.')).toBe(null) }) it('should handle method names that become empty after bracket split', () => { // Test the || null fallback in line 116 expect(extractMethodName('Array.prototype.[someParam]')).toBe(null) expect(extractMethodName('String.prototype.[]')).toBe(null) }) }) describe('findClosingParenthesis edge cases', () => { it('should handle specific edge cases', () => { // Test various scenarios that might hit uncovered branches expect(findClosingParenthesis('func(', 4)).toBe(4) // No closing paren expect(findClosingParenthesis('func)', 0)).toBe(0) // Starts before opening paren expect(findClosingParenthesis('()', 0)).toBe(2) // Simple case }) it('should handle malformed parentheses patterns', () => { // Test edge cases in parentheses handling expect(findClosingParenthesis('func((arg)', 4)).toBe(4) // Unmatched parens expect(findClosingParenthesis('func(arg))', 4)).toBe(9) // Extra closing paren }) }) describe('needsParentheses comprehensive edge cases', () => { it('should test all operator combinations', () => { // Test various operators that should/shouldn\'t need parentheses expect(needsParentheses('a = b')).toBe(true) expect(needsParentheses('a += b')).toBe(true) expect(needsParentheses('a -= b')).toBe(true) expect(needsParentheses('a *= b')).toBe(true) expect(needsParentheses('a /= b')).toBe(true) expect(needsParentheses('a %= b')).toBe(true) // Advanced assignment operators - test actual behavior expect(needsParentheses('a **= b')).toBe(true) // Shift operators are not detected as assignments (by design) expect(needsParentheses('a <<= b')).toBe(false) expect(needsParentheses('a >>= b')).toBe(false) expect(needsParentheses('a >>>= b')).toBe(false) expect(needsParentheses('a &= b')).toBe(true) expect(needsParentheses('a ^= b')).toBe(true) expect(needsParentheses('a |= b')).toBe(true) expect(needsParentheses('a ||= b')).toBe(true) expect(needsParentheses('a &&= b')).toBe(true) expect(needsParentheses('a ??= b')).toBe(true) // Core logical operators expect(needsParentheses('a || b')).toBe(true) expect(needsParentheses('a && b')).toBe(true) expect(needsParentheses('a ? b : c')).toBe(true) }) it('should test string edge cases', () => { // Test string handling edge cases expect(needsParentheses('\'string with = inside\'')).toBe(false) expect(needsParentheses('"string with && inside"')).toBe(false) expect(needsParentheses('`template with ${expr} inside`')).toBe(false) }) it('should test complex nested expressions', () => { // Test complex expressions that might hit different branches expect(needsParentheses('func(a, b) || func2(c, d)')).toBe(true) expect(needsParentheses('obj.prop = value')).toBe(true) expect(needsParentheses('arr[index] = value')).toBe(true) }) it('should test template literal edge cases', () => { // Test template literals with expressions expect(needsParentheses('`start ${a = b} end`')).toBe(false) // Assignment inside template expect(needsParentheses('`start ${a || b} end`')).toBe(false) // Logical op inside template }) it('should test escape sequence handling', () => { // Test escaped quotes and complex strings expect(needsParentheses('"escaped\\"quote\\" = value"')).toBe(false) expect(needsParentheses('\'escaped\\\'quote\\\' = value\'')).toBe(false) }) }) describe('complex parsing scenarios', () => { it('should handle deeply nested structures', () => { // Test complex nested scenarios that might hit various branches const complex1 = 'func({a: {b: {c: d}}}, {e: {f: g}}), final' expect(findFirstTopLevelComma(complex1)).toBe(35) const complex2 = 'func([1, [2, [3, 4]]], [5, [6, 7]]), final' expect(findFirstTopLevelComma(complex2)).toBe(35) }) it('should handle mixed quotes and templates', () => { // Test complex string scenarios const mixed1 = 'func("test", \'other\', `template`), final' expect(findFirstTopLevelComma(mixed1)).toBe(33) const mixed2 = 'func("test \\"inner\\" test", \'other\'), final' expect(findFirstTopLevelComma(mixed2)).toBe(36) }) it('should handle null and undefined edge cases', () => { // Test edge cases with null/undefined handling expect(findFirstTopLevelComma('\u0000,\u0000')).toBe(1) expect(needsParentheses('\u0000 = \u0000')).toBe(true) }) }) describe('regex and pattern edge cases', () => { it('should handle various method pattern edge cases', () => { // Test extractMethodName with unusual but valid patterns expect(extractMethodName('')).toBe(null) expect(extractMethodName('prototype')).toBe(null) expect(extractMethodName('Array.')).toBe(null) expect(extractMethodName('.method')).toBe('method') expect(extractMethodName('Array.prototype')).toBe('prototype') }) it('should handle bracket patterns comprehensively', () => { // Test bracket handling edge cases expect(extractMethodName('Array.prototype.method[')).toBe('method') expect(extractMethodName('Array.prototype.method]')).toBe('method]') expect(extractMethodName('Array.prototype.[method]')).toBe(null) }) }) })