|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockConsumeLatestFileIntent, mockUpdateWorkspaceFileContent } = vi.hoisted(() => ({ |
| 7 | + mockConsumeLatestFileIntent: vi.fn(), |
| 8 | + mockUpdateWorkspaceFileContent: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/core/config/env-flags', () => ({ isDocSandboxEnabled: false })) |
| 12 | +vi.mock('@/lib/copilot/generated/tool-catalog-v1', () => ({ |
| 13 | + WorkspaceFile: { id: 'workspace_file' }, |
| 14 | +})) |
| 15 | +vi.mock('@/lib/copilot/tools/handlers/access', () => ({ ensureWorkspaceAccess: vi.fn() })) |
| 16 | +vi.mock('@/lib/execution/sandbox/run-task', () => ({ runSandboxTask: vi.fn() })) |
| 17 | +vi.mock('@/lib/uploads/contexts/workspace/workspace-file-manager', () => ({ |
| 18 | + fetchWorkspaceFileBuffer: vi.fn(), |
| 19 | + getWorkspaceFile: vi.fn(), |
| 20 | + resolveWorkspaceFileReference: vi.fn(), |
| 21 | + updateWorkspaceFileContent: mockUpdateWorkspaceFileContent, |
| 22 | +})) |
| 23 | +vi.mock('@/lib/workspace-files/orchestration', () => ({ |
| 24 | + performDeleteWorkspaceFileItems: vi.fn(), |
| 25 | + performRenameWorkspaceFile: vi.fn(), |
| 26 | +})) |
| 27 | +vi.mock('@/lib/copilot/tools/server/files/doc-compile', () => ({ |
| 28 | + compileDoc: vi.fn(), |
| 29 | + getE2BDocFormat: vi.fn(async () => null), |
| 30 | + DocCompileUserError: class DocCompileUserError extends Error {}, |
| 31 | + DOCXJS_SOURCE_MIME: 'text/x-docxjs', |
| 32 | + PPTXGENJS_SOURCE_MIME: 'text/x-pptxgenjs', |
| 33 | +})) |
| 34 | +vi.mock('@/lib/copilot/tools/server/files/embedded-image-refs', () => ({ |
| 35 | + buildEmbeddedImageRefWarning: vi.fn(async () => ''), |
| 36 | +})) |
| 37 | +vi.mock('@/lib/copilot/tools/server/files/file-intent-store', () => ({ |
| 38 | + consumeLatestFileIntent: mockConsumeLatestFileIntent, |
| 39 | + storeFileIntent: vi.fn(), |
| 40 | +})) |
| 41 | + |
| 42 | +import { editContentServerTool } from '@/lib/copilot/tools/server/files/edit-content' |
| 43 | + |
| 44 | +/** Extension-less markdown file — the exact shape of the md->txt reversion regression. */ |
| 45 | +const markdownRecord = { |
| 46 | + id: 'file-1', |
| 47 | + workspaceId: 'ws-1', |
| 48 | + name: 'new-boi', |
| 49 | + key: 'workspace/ws-1/1-abc-new-boi', |
| 50 | + path: '/api/files/serve/workspace/ws-1/1-abc-new-boi', |
| 51 | + size: 10, |
| 52 | + type: 'text/markdown', |
| 53 | + uploadedBy: 'user-1', |
| 54 | + uploadedAt: new Date('2026-01-01'), |
| 55 | + updatedAt: new Date('2026-01-01'), |
| 56 | +} |
| 57 | + |
| 58 | +const context = { userId: 'user-1', workspaceId: 'ws-1' } |
| 59 | + |
| 60 | +function intentWith(overrides: Record<string, unknown>) { |
| 61 | + return { |
| 62 | + operation: 'update', |
| 63 | + fileId: markdownRecord.id, |
| 64 | + workspaceId: 'ws-1', |
| 65 | + userId: 'user-1', |
| 66 | + fileRecord: markdownRecord, |
| 67 | + createdAt: Date.now(), |
| 68 | + ...overrides, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +describe('editContentServerTool stored-type preservation', () => { |
| 73 | + beforeEach(() => { |
| 74 | + vi.clearAllMocks() |
| 75 | + mockUpdateWorkspaceFileContent.mockResolvedValue(undefined) |
| 76 | + }) |
| 77 | + |
| 78 | + it('preserves the stored type when the intent has no contentType (md->txt regression)', async () => { |
| 79 | + mockConsumeLatestFileIntent.mockResolvedValue(intentWith({})) |
| 80 | + |
| 81 | + const result = await editContentServerTool.execute({ content: '# hello' }, context) |
| 82 | + |
| 83 | + expect(result.success).toBe(true) |
| 84 | + expect(mockUpdateWorkspaceFileContent).toHaveBeenCalledTimes(1) |
| 85 | + const [, , , , storedMime] = mockUpdateWorkspaceFileContent.mock.calls[0] |
| 86 | + expect(storedMime).toBe('text/markdown') |
| 87 | + expect(result.data?.contentType).toBe('text/markdown') |
| 88 | + }) |
| 89 | + |
| 90 | + it('applies an explicit intent contentType as a deliberate conversion', async () => { |
| 91 | + mockConsumeLatestFileIntent.mockResolvedValue(intentWith({ contentType: 'text/html' })) |
| 92 | + |
| 93 | + const result = await editContentServerTool.execute({ content: '<p>hi</p>' }, context) |
| 94 | + |
| 95 | + expect(result.success).toBe(true) |
| 96 | + const [, , , , storedMime] = mockUpdateWorkspaceFileContent.mock.calls[0] |
| 97 | + expect(storedMime).toBe('text/html') |
| 98 | + }) |
| 99 | + |
| 100 | + it('preserves the stored type through a patch write', async () => { |
| 101 | + mockConsumeLatestFileIntent.mockResolvedValue( |
| 102 | + intentWith({ |
| 103 | + operation: 'patch', |
| 104 | + existingContent: 'Hello World', |
| 105 | + edit: { strategy: 'search_replace', search: 'World' }, |
| 106 | + }) |
| 107 | + ) |
| 108 | + |
| 109 | + const result = await editContentServerTool.execute({ content: 'Sim' }, context) |
| 110 | + |
| 111 | + expect(result.success).toBe(true) |
| 112 | + const [, , , buffer, storedMime] = mockUpdateWorkspaceFileContent.mock.calls[0] |
| 113 | + expect(buffer.toString('utf-8')).toBe('Hello Sim') |
| 114 | + expect(storedMime).toBe('text/markdown') |
| 115 | + }) |
| 116 | + |
| 117 | + it('fails with guidance when no workspace_file intent exists', async () => { |
| 118 | + mockConsumeLatestFileIntent.mockResolvedValue(null) |
| 119 | + |
| 120 | + const result = await editContentServerTool.execute({ content: 'x' }, context) |
| 121 | + |
| 122 | + expect(result.success).toBe(false) |
| 123 | + expect(result.message).toContain('workspace_file') |
| 124 | + expect(mockUpdateWorkspaceFileContent).not.toHaveBeenCalled() |
| 125 | + }) |
| 126 | +}) |
0 commit comments