tools.spec.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { afterEach, describe, expect, it } from 'vitest'
  5. import { Context } from 'cordis'
  6. import { FsVersion } from '@deepseek-ai/dsh-fs'
  7. import { CallId } from '@deepseek-ai/dsh-llm'
  8. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  9. import AgentRegistry from '@deepseek-ai/dsh-agent'
  10. import type { Agent } from '@deepseek-ai/dsh-agent'
  11. import LocalFileSystem from '@deepseek-ai/dsh-fs-local'
  12. import * as FsPolicy from '@deepseek-ai/dsh-fs-policy'
  13. import SandboxedFileSystem from '@deepseek-ai/dsh-fs-sandbox'
  14. import SandboxPolicy from '@deepseek-ai/dsh-sandbox-policy'
  15. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  16. import ToolRegistry from '@deepseek-ai/dsh-tools'
  17. import * as ToolStrReplaceEditor from '@deepseek-ai/dsh-tool-str-replace-editor'
  18. const contexts: Context[] = []
  19. const roots: string[] = []
  20. let callNumber = 0
  21. afterEach(async () => {
  22. for (const ctx of contexts.splice(0)) await ctx.fiber.dispose()
  23. for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true })
  24. })
  25. function agent(ctx: Context, cwd: string): Agent {
  26. const id = SessionId(`str-replace-editor-owner-${callNumber}`)
  27. const scope = ctx.plugin(() => {})
  28. const value: Agent = {
  29. id,
  30. options: {},
  31. session: new Session(id, [], { version: 0, id, createdAt: 0, cwd }),
  32. status: 'idle',
  33. acceptsNextStep: false,
  34. ctx: scope.ctx,
  35. followup: () => {},
  36. steer: () => {},
  37. inject: () => {},
  38. send: () => {},
  39. updateInbox: () => 'not-found',
  40. cancel() {},
  41. whenIdle: () => Promise.resolve(),
  42. }
  43. ctx.agents.register(value)
  44. return value
  45. }
  46. function text(result: { content: { type: string; text?: string }[] }): string {
  47. return result.content.filter(block => block.type === 'text').map(block => block.text).join('')
  48. }
  49. function call(ctx: Context, owner: Agent | undefined, args: unknown) {
  50. return ctx.tools.execute({
  51. signal: new AbortController().signal,
  52. callId: CallId(`str-replace-editor-${++callNumber}`),
  53. name: 'str_replace_editor',
  54. arguments: args,
  55. ...owner === undefined ? {} : { agent: owner },
  56. })
  57. }
  58. async function setup(
  59. config: ToolStrReplaceEditor.Config = {},
  60. options: { fsPolicy?: boolean; sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access' } = {},
  61. ) {
  62. const root = await mkdtemp(join(tmpdir(), 'dsh-tool-str-replace-editor-'))
  63. roots.push(root)
  64. const ctx = new Context()
  65. contexts.push(ctx)
  66. await ctx.plugin(SystemPrompt)
  67. await ctx.plugin(ToolRegistry)
  68. await ctx.plugin(AgentRegistry)
  69. if (options.sandboxMode === undefined) {
  70. await ctx.plugin(LocalFileSystem, { cwd: root })
  71. } else {
  72. await ctx.plugin(SandboxPolicy, { mode: options.sandboxMode, workspaceRoot: root })
  73. await ctx.plugin(SandboxedFileSystem, { cwd: root })
  74. }
  75. if (options.fsPolicy === true) await ctx.plugin(FsPolicy)
  76. const fiber = await ctx.plugin(ToolStrReplaceEditor, config)
  77. return { ctx, root, fiber, owner: agent(ctx, root) }
  78. }
  79. describe('tool-str-replace-editor', () => {
  80. it('registers the standalone schema and configurable description', async () => {
  81. const { ctx, fiber } = await setup({ description: 'custom editor description' })
  82. const schema = ctx.tools.schemas()[0]
  83. expect(ctx.tools.schemas().map(item => item.name)).toEqual(['str_replace_editor'])
  84. expect(schema?.description).toBe('custom editor description')
  85. const properties = (schema?.parameters as {
  86. properties: Record<string, { type?: string; items?: { type?: string } }>
  87. }).properties
  88. expect(properties).not.toHaveProperty('replace_all')
  89. expect(properties.insert_line?.type).toBe('integer')
  90. expect(properties.view_range?.items?.type).toBe('integer')
  91. expect(ctx.tools.get('str_replace_editor')?.presentCall?.({
  92. command: 'view',
  93. path: '/workspace/a.txt',
  94. })).toMatchObject({
  95. card: 'generic',
  96. kind: 'read',
  97. locations: [{ path: '/workspace/a.txt' }],
  98. })
  99. expect(ctx.tools.get('str_replace_editor')?.presentCall?.({
  100. command: 'create',
  101. path: '/workspace/a.txt',
  102. file_text: 'hello',
  103. })).toMatchObject({
  104. card: 'diff',
  105. diffs: [{ path: '/workspace/a.txt', oldText: null, newText: 'hello' }],
  106. })
  107. expect(ctx.tools.get('str_replace_editor')?.presentCall?.({
  108. command: 'str_replace',
  109. path: '/workspace/a.txt',
  110. old_str: 'old',
  111. new_str: 'new',
  112. })).toMatchObject({
  113. card: 'diff',
  114. diffs: [{ path: '/workspace/a.txt', oldText: 'old', newText: 'new' }],
  115. })
  116. expect(ctx.tools.get('str_replace_editor')?.presentCall?.({
  117. command: 'insert',
  118. path: '/workspace/a.txt',
  119. insert_line: 0,
  120. new_str: 'x',
  121. })).toMatchObject({
  122. card: 'generic',
  123. kind: 'edit',
  124. locations: [{ path: '/workspace/a.txt', line: 1 }],
  125. })
  126. expect(ctx.tools.get('str_replace_editor')?.presentCall?.({
  127. command: 'create',
  128. path: '/workspace/empty.txt',
  129. })).toMatchObject({
  130. diffs: [{ path: '/workspace/empty.txt', oldText: null, newText: '' }],
  131. })
  132. expect(ctx.tools.get('str_replace_editor')?.presentCall?.({
  133. command: 'str_replace',
  134. path: '/workspace/a.txt',
  135. })).toMatchObject({
  136. diffs: [{ path: '/workspace/a.txt', oldText: null, newText: '' }],
  137. })
  138. expect(ctx.tools.get('str_replace_editor')?.presentCall?.({
  139. command: 'insert',
  140. path: '/workspace/a.txt',
  141. })).toMatchObject({
  142. locations: [{ path: '/workspace/a.txt' }],
  143. })
  144. await fiber.dispose()
  145. expect(ctx.tools.schemas()).toEqual([])
  146. expect(ctx.tools.get('str_replace_editor')).toBeUndefined()
  147. })
  148. it('creates, views, replaces, and inserts with the canonical model-facing output', async () => {
  149. const { ctx, root, owner } = await setup()
  150. const sample = join(root, 'sample.txt')
  151. expect(text(await call(ctx, owner, {
  152. command: 'create',
  153. path: sample,
  154. file_text: 'one\ntwo\nthree\n',
  155. }))).toBe(`New file created successfully at: ${sample}`)
  156. expect(text(await call(ctx, owner, {
  157. command: 'view',
  158. path: sample,
  159. view_range: [2, -1],
  160. }))).toBe([
  161. `Here's the content of ${sample} with line numbers (which has a total of 4 lines) with view_range=[2, -1]:`,
  162. ' 2 two',
  163. ' 3 three',
  164. ' 4 ',
  165. '',
  166. ].join('\n'))
  167. expect(text(await call(ctx, owner, {
  168. command: 'str_replace',
  169. path: sample,
  170. old_str: 'two',
  171. new_str: 'TWO',
  172. }))).toBe(`The file ${sample} has been edited successfully.`)
  173. expect(text(await call(ctx, owner, {
  174. command: 'str_replace',
  175. path: sample,
  176. old_str: 'TWO',
  177. }))).toBe(`The file ${sample} has been edited successfully.`)
  178. expect(text(await call(ctx, owner, {
  179. command: 'insert',
  180. path: sample,
  181. insert_line: 1,
  182. new_str: 'between',
  183. }))).toBe(`The file ${sample} has been edited successfully.`)
  184. expect(await readFile(sample, 'utf8')).toBe('one\nbetween\n\nthree\n')
  185. })
  186. it('writes replacement text literally', async () => {
  187. const { ctx, root, owner } = await setup()
  188. const sample = join(root, 'literal.txt')
  189. const replacement = "$&|$`|$'|$$"
  190. await writeFile(sample, 'before OLD after')
  191. expect((await call(ctx, owner, {
  192. command: 'str_replace',
  193. path: sample,
  194. old_str: 'OLD',
  195. new_str: replacement,
  196. })).isError).toBe(false)
  197. expect(await readFile(sample, 'utf8')).toBe(`before ${replacement} after`)
  198. })
  199. it('lists visible entries to depth two and clips at the configured view limit', async () => {
  200. const { ctx, root, owner } = await setup({ maxOutputChars: 10_000 })
  201. await mkdir(join(root, 'dir', 'nested', 'third'), { recursive: true })
  202. await mkdir(join(root, 'dir', 'node_modules', 'pkg'), { recursive: true })
  203. await mkdir(join(root, 'dir', 'node_modules_old'), { recursive: true })
  204. await mkdir(join(root, 'dir', '__pycache__'), { recursive: true })
  205. await mkdir(join(root, 'dir', '__pycache__backup'), { recursive: true })
  206. await writeFile(join(root, 'dir', 'visible.txt'), 'ok')
  207. await writeFile(join(root, 'dir', '.hidden'), 'hidden')
  208. await writeFile(join(root, 'dir', 'nested', 'child.txt'), 'child')
  209. await writeFile(join(root, 'dir', 'nested', 'third', 'too-deep.txt'), 'deep')
  210. await writeFile(join(root, 'dir', 'node_modules', 'pkg', 'index.js'), 'hidden dependency')
  211. await writeFile(join(root, 'dir', 'node_modules_old', 'kept.js'), 'visible source')
  212. await writeFile(join(root, 'dir', '__pycache__', 'module.pyc'), 'cache')
  213. await writeFile(join(root, 'dir', '__pycache__backup', 'kept.py'), 'visible source')
  214. const listDir = ctx.fs.listDir.bind(ctx.fs)
  215. const otherTarget = await ctx.fs.resolve(join(root, 'dir', 'other'))
  216. ctx.fs.listDir = async (target, signal) => {
  217. const entries = await listDir(target, signal)
  218. return target.displayPath === join(root, 'dir')
  219. ? [
  220. { name: 'same-target', type: 'other', target: otherTarget },
  221. { name: 'other', type: 'other', target: otherTarget },
  222. ...entries.toReversed(),
  223. ]
  224. : entries
  225. }
  226. const listing = text(await call(ctx, owner, { command: 'view', path: join(root, 'dir') }))
  227. expect(listing).not.toContain('.hidden')
  228. expect(listing).not.toContain('too-deep.txt')
  229. expect(listing).not.toContain('index.js')
  230. expect(listing).not.toContain('module.pyc')
  231. expect(listing).toContain('node_modules_old/kept.js')
  232. expect(listing).toContain('__pycache__backup/kept.py')
  233. const clipped = await setup({ maxOutputChars: 10 })
  234. await writeFile(join(clipped.root, 'large.txt'), 'x'.repeat(100))
  235. expect(text(await call(clipped.ctx, clipped.owner, {
  236. command: 'view',
  237. path: join(clipped.root, 'large.txt'),
  238. })))
  239. .toContain('<response clipped>')
  240. })
  241. it('matches canonical empty-line, range, and end-insert behavior', async () => {
  242. const { ctx, root, owner } = await setup()
  243. const empty = join(root, 'empty.txt')
  244. const newline = join(root, 'newline.txt')
  245. const plain = join(root, 'plain.txt')
  246. await writeFile(empty, '')
  247. await writeFile(newline, '\n')
  248. await writeFile(plain, 'one\ntwo')
  249. expect(text(await call(ctx, owner, { command: 'view', path: empty })))
  250. .toContain('(which has a total of 1 lines):\n 1 \n')
  251. expect(text(await call(ctx, owner, { command: 'view', path: newline })))
  252. .toContain('(which has a total of 2 lines):\n 1 \n 2 \n')
  253. expect(text(await call(ctx, owner, {
  254. command: 'view',
  255. path: plain,
  256. view_range: [1, 2],
  257. }))).toContain(' 2 two')
  258. expect(text(await call(ctx, undefined, {
  259. command: 'view',
  260. path: plain,
  261. }))).toContain(' 1 one')
  262. expect((await call(ctx, undefined, {
  263. command: 'create',
  264. path: join(root, 'ownerless.txt'),
  265. file_text: 'ownerless',
  266. })).isError).toBe(false)
  267. await call(ctx, owner, {
  268. command: 'insert',
  269. path: plain,
  270. insert_line: 2,
  271. new_str: 'three',
  272. })
  273. expect(await readFile(plain, 'utf8')).toBe('one\ntwo\nthree')
  274. await writeFile(newline, 'one\n')
  275. await call(ctx, owner, {
  276. command: 'insert',
  277. path: newline,
  278. insert_line: 2,
  279. new_str: 'three',
  280. })
  281. expect(await readFile(newline, 'utf8')).toBe('one\n\nthree')
  282. })
  283. it('uses old_str-only replacement failures and rejects relative paths', async () => {
  284. const { ctx, root, owner } = await setup()
  285. const ambiguous = join(root, 'ambiguous.txt')
  286. await writeFile(ambiguous, 'same\nother\nsame')
  287. const missing = await call(ctx, owner, {
  288. command: 'str_replace',
  289. path: ambiguous,
  290. old_str: 'absent',
  291. new_str: 'x',
  292. })
  293. expect(missing.isError).toBe(true)
  294. expect(text(missing)).toContain(`old_str \`absent\` did not appear verbatim in ${ambiguous}`)
  295. expect(text(missing)).not.toContain('old_string')
  296. const repeated = await call(ctx, owner, {
  297. command: 'str_replace',
  298. path: ambiguous,
  299. old_str: 'same',
  300. new_str: 'x',
  301. })
  302. expect(repeated.isError).toBe(true)
  303. expect(text(repeated)).toContain('Multiple occurrences of old_str `same` in lines [1, 3]')
  304. expect(text(repeated)).not.toContain('replace_all')
  305. await writeFile(ambiguous, 'alpha\nbeta\nmiddle\nalpha\nbeta')
  306. const repeatedMultiline = await call(ctx, owner, {
  307. command: 'str_replace',
  308. path: ambiguous,
  309. old_str: 'alpha\nbeta',
  310. new_str: 'x',
  311. })
  312. expect(text(repeatedMultiline))
  313. .toContain('Multiple occurrences of old_str `alpha\nbeta` in lines [1, 4]')
  314. const mixedEol = join(root, 'mixed-eol.txt')
  315. await writeFile(mixedEol, 'alpha\r\nbeta\nmiddle\nalpha\nbeta')
  316. expect((await call(ctx, owner, {
  317. command: 'str_replace',
  318. path: mixedEol,
  319. old_str: 'alpha\r\nbeta',
  320. new_str: 'replaced',
  321. })).isError).toBe(false)
  322. expect(await readFile(mixedEol, 'utf8')).toBe('replaced\nmiddle\nalpha\nbeta')
  323. const relative = await call(ctx, owner, { command: 'view', path: 'ambiguous.txt' })
  324. expect(relative.isError).toBe(true)
  325. expect(text(relative)).toContain('is not an absolute path')
  326. expect(await readFile(ambiguous, 'utf8')).toBe('alpha\nbeta\nmiddle\nalpha\nbeta')
  327. })
  328. it('reports invalid commands or arguments without mutating files', async () => {
  329. const { ctx, root, owner } = await setup()
  330. const ambiguous = join(root, 'ambiguous.txt')
  331. const empty = join(root, 'empty.txt')
  332. const trailingNewline = join(root, 'trailing-newline.txt')
  333. const threeLines = join(root, 'three-lines.txt')
  334. const directory = join(root, 'directory')
  335. await writeFile(ambiguous, 'same same')
  336. await writeFile(empty, '')
  337. await writeFile(trailingNewline, 'one\n')
  338. await writeFile(threeLines, 'one\ntwo\nthree')
  339. await mkdir(directory)
  340. const cases = [
  341. { command: 'view', path: '' },
  342. { command: 'view', path: join(root, 'missing.txt') },
  343. { command: 'view', path: ambiguous, view_range: [1] },
  344. { command: 'view', path: ambiguous, view_range: [0, 1] },
  345. { command: 'view', path: ambiguous, view_range: [1.5, 2] },
  346. { command: 'view', path: threeLines, view_range: [1, 99] },
  347. { command: 'view', path: threeLines, view_range: [2, 1] },
  348. { command: 'view', path: directory, view_range: [1, 1] },
  349. { command: 'create', path: join(root, 'new.txt') },
  350. { command: 'create', path: ambiguous, file_text: 'overwrite' },
  351. { command: 'str_replace', path: ambiguous, new_str: 'x' },
  352. { command: 'str_replace', path: ambiguous, old_str: '', new_str: 'x' },
  353. { command: 'insert', path: ambiguous, new_str: 'x' },
  354. { command: 'insert', path: ambiguous, insert_line: -1, new_str: 'x' },
  355. { command: 'insert', path: ambiguous, insert_line: 1.5, new_str: 'x' },
  356. { command: 'insert', path: ambiguous, insert_line: 99, new_str: 'x' },
  357. { command: 'insert', path: empty, insert_line: 2, new_str: 'x' },
  358. { command: 'insert', path: directory, insert_line: 0, new_str: 'x' },
  359. ]
  360. for (const args of cases) {
  361. expect((await call(ctx, owner, args)).isError).toBe(true)
  362. }
  363. expect(await readFile(ambiguous, 'utf8')).toBe('same same')
  364. ctx.fs.stat = async () => ({ version: FsVersion('special'), type: 'other' })
  365. const special = await call(ctx, owner, { command: 'view', path: join(root, 'special') })
  366. expect(special.isError).toBe(true)
  367. expect(special.error).toMatchObject({ info: { code: 'FS_NOT_REGULAR_FILE' } })
  368. expect((await call(ctx, owner, {
  369. command: 'str_replace',
  370. path: join(root, 'special'),
  371. old_str: 'x',
  372. new_str: 'y',
  373. })).error).toMatchObject({ info: { code: 'FS_NOT_REGULAR_FILE' } })
  374. expect((await call(ctx, owner, {
  375. command: 'insert',
  376. path: join(root, 'special'),
  377. insert_line: 0,
  378. new_str: 'x',
  379. })).error).toMatchObject({ info: { code: 'FS_NOT_REGULAR_FILE' } })
  380. })
  381. it('delegates read-before-edit decisions to fs-policy', async () => {
  382. const { ctx, root, owner } = await setup({}, { fsPolicy: true })
  383. const existing = join(root, 'existing.txt')
  384. const created = join(root, 'created.txt')
  385. await writeFile(existing, 'before')
  386. const blindEdit = await call(ctx, owner, {
  387. command: 'str_replace',
  388. path: existing,
  389. old_str: 'before',
  390. new_str: 'after',
  391. })
  392. expect(blindEdit.error).toMatchObject({ info: { code: 'FS_NOT_OBSERVED' } })
  393. expect(await readFile(existing, 'utf8')).toBe('before')
  394. await call(ctx, owner, { command: 'view', path: existing })
  395. expect((await call(ctx, owner, {
  396. command: 'str_replace',
  397. path: existing,
  398. old_str: 'before',
  399. new_str: 'after',
  400. })).isError).toBe(false)
  401. expect(await readFile(existing, 'utf8')).toBe('after')
  402. expect((await call(ctx, owner, {
  403. command: 'insert',
  404. path: existing,
  405. insert_line: 1,
  406. new_str: 'tail',
  407. })).isError).toBe(false)
  408. expect(await readFile(existing, 'utf8')).toBe('after\ntail')
  409. expect((await call(ctx, owner, {
  410. command: 'create',
  411. path: created,
  412. file_text: 'new',
  413. })).isError).toBe(false)
  414. expect(await readFile(created, 'utf8')).toBe('new')
  415. })
  416. it('passes the session sandbox policy to every mutation', async () => {
  417. const { ctx, root, owner } = await setup({}, { sandboxMode: 'read-only' })
  418. const path = join(root, 'blocked.txt')
  419. const result = await call(ctx, owner, {
  420. command: 'create',
  421. path,
  422. file_text: 'blocked',
  423. })
  424. expect(result.error).toMatchObject({ info: { code: 'FS_SANDBOX_DENIED' } })
  425. expect(text(result)).toContain('[sandbox: file access denied under read-only mode]')
  426. const ownerless = await call(ctx, undefined, {
  427. command: 'create',
  428. path: join(root, 'ownerless-blocked.txt'),
  429. file_text: 'blocked',
  430. })
  431. expect(ownerless.error).toMatchObject({ info: { code: 'FS_SANDBOX_DENIED' } })
  432. })
  433. it('preserves tabs outside the edited region', async () => {
  434. const { ctx, root, owner } = await setup()
  435. const path = join(root, 'Makefile')
  436. await writeFile(path, 'target:\n\told\nremove\n')
  437. expect(text(await call(ctx, owner, { command: 'view', path })))
  438. .toContain(' 2 \told')
  439. await call(ctx, owner, {
  440. command: 'str_replace',
  441. path,
  442. old_str: '\told',
  443. new_str: '\tnew',
  444. })
  445. await call(ctx, owner, {
  446. command: 'str_replace',
  447. path,
  448. old_str: 'remove\n',
  449. })
  450. await call(ctx, owner, {
  451. command: 'insert',
  452. path,
  453. insert_line: 1,
  454. new_str: '\tkept',
  455. })
  456. expect(await readFile(path, 'utf8')).toBe('target:\n\tkept\n\tnew\n')
  457. })
  458. it('reports missing sandbox-policy composition during plugin startup', async () => {
  459. const root = await mkdtemp(join(tmpdir(), 'dsh-tool-str-replace-editor-missing-policy-'))
  460. roots.push(root)
  461. const ctx = new Context()
  462. contexts.push(ctx)
  463. await ctx.plugin(SystemPrompt)
  464. await ctx.plugin(ToolRegistry)
  465. await ctx.plugin(AgentRegistry)
  466. await ctx.plugin(LocalFileSystem, { cwd: root })
  467. Object.defineProperty(ctx.fs, 'sandboxMode', { value: 'read-only' })
  468. await expect(ctx.plugin(ToolStrReplaceEditor))
  469. .rejects.toThrow('the mounted filesystem confines but ctx.sandboxPolicy is missing')
  470. })
  471. it('maps unexpected backend write failures for replace and insert', async () => {
  472. const { ctx, root, owner } = await setup()
  473. const path = join(root, 'backend-error.txt')
  474. await writeFile(path, 'old\n')
  475. const failWrite = async (): Promise<never> => {
  476. throw new Error('backend write failed')
  477. }
  478. ctx.fs.writeText = failWrite
  479. const replace = await call(ctx, owner, {
  480. command: 'str_replace',
  481. path,
  482. old_str: 'old',
  483. new_str: 'new',
  484. })
  485. expect(replace.isError).toBe(true)
  486. expect(text(replace)).toContain('backend write failed')
  487. const insert = await call(ctx, owner, {
  488. command: 'insert',
  489. path,
  490. insert_line: 1,
  491. new_str: 'new',
  492. })
  493. expect(insert.isError).toBe(true)
  494. expect(text(insert)).toContain('backend write failed')
  495. })
  496. it('rejects invalid plugin config', () => {
  497. expect(() => {
  498. ToolStrReplaceEditor.apply(new Context(), { maxOutputChars: 0 })
  499. }).toThrow('maxOutputChars must be a positive safe integer')
  500. expect(() => {
  501. ToolStrReplaceEditor.apply(new Context(), { description: ' ' })
  502. }).toThrow('description must be non-empty')
  503. })
  504. })