File size: 5,654 Bytes
39e315a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { describe, expect, test } from 'bun:test'
import { normalizeToolArguments } from './toolArgumentNormalization'

describe('normalizeToolArguments', () => {
  describe('Bash tool', () => {
    test('wraps plain string into { command }', () => {
      expect(normalizeToolArguments('Bash', 'pwd')).toEqual({ command: 'pwd' })
    })

    test('wraps multi-word command', () => {
      expect(normalizeToolArguments('Bash', 'ls -la /tmp')).toEqual({
        command: 'ls -la /tmp',
      })
    })

    test('passes through structured JSON object', () => {
      expect(
        normalizeToolArguments('Bash', '{"command":"echo hi"}'),
      ).toEqual({ command: 'echo hi' })
    })

    test('returns empty object for blank string', () => {
      expect(normalizeToolArguments('Bash', '')).toEqual({})
      expect(normalizeToolArguments('Bash', '   ')).toEqual({})
    })

    test('returns parsed blank for JSON-encoded blank string', () => {
      expect(normalizeToolArguments('Bash', '""')).toEqual('')
      expect(normalizeToolArguments('Bash', '"  "')).toEqual('  ')
    })

    test('returns empty object for malformed structured object literal', () => {
      expect(normalizeToolArguments('Bash', '{ "command": "pwd"')).toEqual({})
    })

    test.each([
      ['{command:"pwd"}'],
      ["{'command':'pwd'}"],
      ['{command: pwd}'],
    ])(
      'returns empty object for malformed object-shaped string %s (does not wrap into command)',
      (input) => {
        expect(normalizeToolArguments('Bash', input)).toEqual({})
      },
    )

    test.each([
      ['false', false],
      ['null', null],
      ['[]', [] as unknown[]],
      ['0', 0],
      ['true', true],
      ['123', 123],
    ])(
      'preserves JSON literal %s as-is (does not wrap into command)',
      (input, expected) => {
        expect(normalizeToolArguments('Bash', input)).toEqual(expected)
      },
    )

    test('wraps JSON-encoded string into { command }', () => {
      expect(normalizeToolArguments('Bash', '"pwd"')).toEqual({
        command: 'pwd',
      })
    })
  })

  describe('undefined arguments', () => {
    test('returns empty object for undefined', () => {
      expect(normalizeToolArguments('Bash', undefined)).toEqual({})
      expect(normalizeToolArguments('UnknownTool', undefined)).toEqual({})
    })
  })

  describe('Read tool', () => {
    test('wraps plain string into { file_path }', () => {
      expect(normalizeToolArguments('Read', '/home/user/file.txt')).toEqual({
        file_path: '/home/user/file.txt',
      })
    })

    test('wraps JSON-encoded string into { file_path }', () => {
      expect(normalizeToolArguments('Read', '"/home/user/file.txt"')).toEqual({
        file_path: '/home/user/file.txt',
      })
    })

    test('passes through structured JSON object', () => {
      expect(
        normalizeToolArguments('Read', '{"file_path":"/tmp/f.txt","limit":10}'),
      ).toEqual({ file_path: '/tmp/f.txt', limit: 10 })
    })
  })

  describe('Write tool', () => {
    test('wraps plain string into { file_path }', () => {
      expect(normalizeToolArguments('Write', '/tmp/out.txt')).toEqual({
        file_path: '/tmp/out.txt',
      })
    })

    test('passes through structured JSON object', () => {
      expect(
        normalizeToolArguments(
          'Write',
          '{"file_path":"/tmp/out.txt","content":"hello"}',
        ),
      ).toEqual({ file_path: '/tmp/out.txt', content: 'hello' })
    })
  })

  describe('Edit tool', () => {
    test('wraps plain string into { file_path }', () => {
      expect(normalizeToolArguments('Edit', '/tmp/edit.ts')).toEqual({
        file_path: '/tmp/edit.ts',
      })
    })

    test('passes through structured JSON object', () => {
      expect(
        normalizeToolArguments(
          'Edit',
          '{"file_path":"/tmp/f.ts","old_string":"a","new_string":"b"}',
        ),
      ).toEqual({ file_path: '/tmp/f.ts', old_string: 'a', new_string: 'b' })
    })
  })

  describe('Glob tool', () => {
    test('wraps plain string into { pattern }', () => {
      expect(normalizeToolArguments('Glob', '**/*.ts')).toEqual({
        pattern: '**/*.ts',
      })
    })

    test('passes through structured JSON object', () => {
      expect(
        normalizeToolArguments('Glob', '{"pattern":"*.js","path":"/src"}'),
      ).toEqual({ pattern: '*.js', path: '/src' })
    })
  })

  describe('Grep tool', () => {
    test('wraps plain string into { pattern }', () => {
      expect(normalizeToolArguments('Grep', 'TODO')).toEqual({
        pattern: 'TODO',
      })
    })

    test('passes through structured JSON object', () => {
      expect(
        normalizeToolArguments('Grep', '{"pattern":"fixme","path":"/src"}'),
      ).toEqual({ pattern: 'fixme', path: '/src' })
    })
  })

  describe('unknown tools', () => {
    test('returns empty object for plain string (no known field mapping)', () => {
      expect(normalizeToolArguments('UnknownTool', 'some value')).toEqual({})
    })

    test('passes through structured JSON object', () => {
      expect(
        normalizeToolArguments('UnknownTool', '{"key":"val"}'),
      ).toEqual({ key: 'val' })
    })

    test('preserves JSON literals as-is', () => {
      expect(normalizeToolArguments('UnknownTool', 'false')).toEqual(false)
      expect(normalizeToolArguments('UnknownTool', 'null')).toEqual(null)
      expect(normalizeToolArguments('UnknownTool', '[]')).toEqual([])
    })

    test('returns parsed string for JSON-encoded string on unknown tools', () => {
      expect(normalizeToolArguments('UnknownTool', '"hello"')).toEqual(
        'hello',
      )
    })
  })
})