import { describe, it, expect } from 'vitest' import { existsSync, mkdtempSync, rmSync } from 'fs' import { tmpdir } from 'os' import { join, dirname } from 'path' import { preparePromptForCli } from '../../src/utils/prompt-file.js' describe('preparePromptForCli', () => { it('should return original prompt when under threshold', () => { const result = preparePromptForCli('small prompt') expect(result.prompt).toBe('small prompt') result.cleanup() }) it('should write large prompts to temp file and cleanup', () => { const largePrompt = 'x'.repeat(200 * 1024) const result = preparePromptForCli(largePrompt) expect(result.prompt).toContain('file') expect(result.prompt).not.toBe(largePrompt) const pathMatch = result.prompt.match(/\/.*magpie_prompt_\S+/) expect(pathMatch).toBeTruthy() const tmpPath = pathMatch![0] expect(existsSync(tmpPath)).toBe(true) result.cleanup() expect(existsSync(tmpPath)).toBe(false) }) it('writes the spilled prompt into the supplied tmpDir', () => { const customDir = mkdtempSync(join(tmpdir(), 'magpie-tmpdir-test-')) try { const largePrompt = 'y'.repeat(200 * 1024) const result = preparePromptForCli(largePrompt, { tmpDir: customDir }) const pathMatch = result.prompt.match(/\/.*magpie_prompt_\S+/) expect(pathMatch).toBeTruthy() const tmpPath = pathMatch![0] expect(dirname(tmpPath)).toBe(customDir) expect(existsSync(tmpPath)).toBe(true) result.cleanup() expect(existsSync(tmpPath)).toBe(false) } finally { rmSync(customDir, { recursive: true, force: true }) } }) })