|
1 | 1 | /** |
2 | 2 | * @vitest-environment node |
3 | 3 | */ |
4 | | -import { describe, expect, it } from 'vitest' |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockWriteWorkspaceFileByPath } = vi.hoisted(() => ({ |
| 7 | + mockWriteWorkspaceFileByPath: vi.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +vi.mock('@/lib/copilot/vfs/resource-writer', () => ({ |
| 11 | + writeWorkspaceFileByPath: mockWriteWorkspaceFileByPath, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/copilot/request/otel', () => ({ |
| 15 | + withCopilotSpan: ( |
| 16 | + _name: string, |
| 17 | + _attrs: Record<string, unknown> | undefined, |
| 18 | + fn: (span: unknown) => Promise<unknown> |
| 19 | + ) => fn({ setAttribute: vi.fn(), setAttributes: vi.fn(), addEvent: vi.fn() }), |
| 20 | +})) |
| 21 | + |
| 22 | +import { FunctionExecute } from '@/lib/copilot/generated/tool-catalog-v1' |
5 | 23 | import { |
6 | 24 | extractTabularData, |
| 25 | + maybeWriteOutputToFile, |
7 | 26 | normalizeOutputWorkspaceFileName, |
8 | 27 | serializeOutputForFile, |
9 | 28 | unwrapFunctionExecuteOutput, |
10 | 29 | } from '@/lib/copilot/request/tools/files' |
| 30 | +import type { ExecutionContext } from '@/lib/copilot/request/types' |
11 | 31 |
|
12 | 32 | describe('unwrapFunctionExecuteOutput', () => { |
13 | 33 | it('unwraps the function_execute envelope { result, stdout }', () => { |
@@ -87,6 +107,53 @@ describe('normalizeOutputWorkspaceFileName', () => { |
87 | 107 | }) |
88 | 108 | }) |
89 | 109 |
|
| 110 | +describe('maybeWriteOutputToFile', () => { |
| 111 | + function buildContext(overrides: Partial<ExecutionContext> = {}): ExecutionContext { |
| 112 | + return { |
| 113 | + userId: 'user-1', |
| 114 | + workflowId: 'wf-1', |
| 115 | + workspaceId: 'workspace-1', |
| 116 | + userPermission: 'write', |
| 117 | + ...overrides, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + beforeEach(() => { |
| 122 | + vi.clearAllMocks() |
| 123 | + mockWriteWorkspaceFileByPath.mockResolvedValue({ |
| 124 | + id: 'file-1', |
| 125 | + name: 'report.csv', |
| 126 | + vfsPath: 'files/report.csv', |
| 127 | + mode: 'overwrite', |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + it('denies a read-only principal without writing the file', async () => { |
| 132 | + const result = await maybeWriteOutputToFile( |
| 133 | + FunctionExecute.id, |
| 134 | + { outputs: { files: [{ path: 'files/report.csv', mode: 'overwrite' }] } }, |
| 135 | + { success: true, output: { result: 'name,age\nAlice,30', stdout: '' } }, |
| 136 | + buildContext({ userPermission: 'read' }) |
| 137 | + ) |
| 138 | + |
| 139 | + expect(result.success).toBe(false) |
| 140 | + expect(result.error).toContain('requires write access') |
| 141 | + expect(mockWriteWorkspaceFileByPath).not.toHaveBeenCalled() |
| 142 | + }) |
| 143 | + |
| 144 | + it('writes the output file for a write principal', async () => { |
| 145 | + const result = await maybeWriteOutputToFile( |
| 146 | + FunctionExecute.id, |
| 147 | + { outputs: { files: [{ path: 'files/report.csv', mode: 'overwrite' }] } }, |
| 148 | + { success: true, output: { result: 'name,age\nAlice,30', stdout: '' } }, |
| 149 | + buildContext() |
| 150 | + ) |
| 151 | + |
| 152 | + expect(result.success).toBe(true) |
| 153 | + expect(mockWriteWorkspaceFileByPath).toHaveBeenCalledTimes(1) |
| 154 | + }) |
| 155 | +}) |
| 156 | + |
90 | 157 | describe('extractTabularData', () => { |
91 | 158 | it('extracts rows directly from an array input', () => { |
92 | 159 | expect(extractTabularData([{ a: 1 }, { a: 2 }])).toEqual([{ a: 1 }, { a: 2 }]) |
|
0 commit comments