|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ operation: vi.fn(), job: vi.fn(), file: vi.fn() })) |
| 5 | +vi.mock('@/lib/internal/oracle-epm-profitability/operations', () => ({ |
| 6 | + executeOraclePcmOperation: mocks.operation, |
| 7 | +})) |
| 8 | +vi.mock('@/lib/internal/oracle-epm-profitability/jobs', () => ({ |
| 9 | + executeOraclePcmJobOperation: mocks.job, |
| 10 | +})) |
| 11 | +vi.mock('@/lib/internal/oracle-epm-profitability/files.server', () => ({ |
| 12 | + executeOraclePcmFileOperation: mocks.file, |
| 13 | +})) |
| 14 | +vi.mock('@/lib/internal/oracle-epm/files.server', () => ({ |
| 15 | + openOracleEpmSourceFile: vi.fn(), |
| 16 | + storeOracleEpmDownload: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +import { oracleEpmLocalError } from '@/lib/internal/oracle-epm/errors' |
| 20 | +import { executeOraclePcmTool } from '@/lib/internal/oracle-epm-profitability/execute-tool' |
| 21 | +import { OraclePcmOperationError } from '@/lib/internal/oracle-epm-profitability/normalizers' |
| 22 | +import type { InternalToolOperationCall } from '@/lib/internal/tool-operations/types' |
| 23 | + |
| 24 | +function call(operation: string): InternalToolOperationCall { |
| 25 | + return { |
| 26 | + toolId: `oracle_epm_profitability_${operation}`, |
| 27 | + input: { oauthCredential: 'credential-1', accessToken: 'injected-token', instanceUrl: 'https://epm.example.com', userId: 'untrusted' }, |
| 28 | + context: { workflowId: 'workflow-1', userId: 'trusted-user' }, |
| 29 | + headers: new Headers(), |
| 30 | + requestId: 'request-1', |
| 31 | + signal: new AbortController().signal, |
| 32 | + } |
| 33 | +} |
| 34 | +describe('Oracle PCM direct tool handler', () => { |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks() |
| 37 | + for (const mock of Object.values(mocks)) mock.mockResolvedValue({ success: true, output: {} }) |
| 38 | + }) |
| 39 | + it.each([ |
| 40 | + 'create_application', |
| 41 | + 'enable_application', |
| 42 | + 'deploy_cube', |
| 43 | + 'update_dimensions', |
| 44 | + 'load_data', |
| 45 | + 'run_calculation', |
| 46 | + 'copy_pov', |
| 47 | + 'clear_pov', |
| 48 | + 'get_rule_balancing', |
| 49 | + 'generate_program_documentation', |
| 50 | + 'export_query_results', |
| 51 | + 'import_template', |
| 52 | + 'apply_data_grants', |
| 53 | + 'merge_slices', |
| 54 | + 'optimize_cube', |
| 55 | + ])('dispatches %s without an internal HTTP hop', async (operation) => { |
| 56 | + const request = call(operation) |
| 57 | + expect((await executeOraclePcmTool(request)).status).toBe(200) |
| 58 | + expect(mocks.operation).toHaveBeenCalledWith(operation, request.input, request.signal) |
| 59 | + }) |
| 60 | + it.each(['get_task_status', 'wait_for_task'])( |
| 61 | + 'forwards cancellation for %s', |
| 62 | + async (operation) => { |
| 63 | + const request = call(operation) |
| 64 | + await executeOraclePcmTool(request) |
| 65 | + expect(mocks.job).toHaveBeenCalledWith(operation, request.input, request.signal) |
| 66 | + } |
| 67 | + ) |
| 68 | + it.each(['list_files', 'upload_file', 'download_file'])( |
| 69 | + 'forwards trusted context for %s', |
| 70 | + async (operation) => { |
| 71 | + const request = call(operation) |
| 72 | + await executeOraclePcmTool(request) |
| 73 | + expect(mocks.file).toHaveBeenCalledWith( |
| 74 | + operation, |
| 75 | + request.input, |
| 76 | + request.signal, |
| 77 | + request.context |
| 78 | + ) |
| 79 | + } |
| 80 | + ) |
| 81 | + it.each([ |
| 82 | + [new OraclePcmOperationError('Invalid application', 400), 400, 'Invalid application'], |
| 83 | + [oracleEpmLocalError('invalid_response'), 502, 'Oracle EPM returned an invalid response'], |
| 84 | + [oracleEpmLocalError('timeout'), 408, 'The Oracle EPM request timed out'], |
| 85 | + [ |
| 86 | + oracleEpmLocalError('payload_too_large'), |
| 87 | + 413, |
| 88 | + 'The Oracle EPM payload exceeded the allowed size', |
| 89 | + ], |
| 90 | + [new Error('credential-secret-canary'), 500, 'Oracle PCM operation failed'], |
| 91 | + ])( |
| 92 | + 'returns safe failures and disallows automatic resubmission', |
| 93 | + async (error, status, message) => { |
| 94 | + mocks.operation.mockRejectedValue(error) |
| 95 | + const response = await executeOraclePcmTool(call('run_calculation')) |
| 96 | + expect(response.status).toBe(status) |
| 97 | + expect(await response.json()).toEqual({ |
| 98 | + success: false, |
| 99 | + output: {}, |
| 100 | + error: message, |
| 101 | + retryable: false, |
| 102 | + }) |
| 103 | + } |
| 104 | + ) |
| 105 | + it('distinguishes local timeout from remote cancellation', async () => { |
| 106 | + mocks.job.mockRejectedValue(new DOMException('deadline', 'TimeoutError')) |
| 107 | + const response = await executeOraclePcmTool(call('wait_for_task')) |
| 108 | + expect(response.status).toBe(408) |
| 109 | + expect((await response.json()).error).toContain('remote task was not cancelled') |
| 110 | + }) |
| 111 | + it('propagates aborts and rejects other tool namespaces', async () => { |
| 112 | + const request = call('run_calculation') |
| 113 | + request.signal = AbortSignal.abort() |
| 114 | + await expect(executeOraclePcmTool(request)).rejects.toThrow() |
| 115 | + expect(mocks.operation).not.toHaveBeenCalled() |
| 116 | + expect( |
| 117 | + (await executeOraclePcmTool({ ...call('list_files'), toolId: 'unrelated_tool' })).status |
| 118 | + ).toBe(400) |
| 119 | + }) |
| 120 | +}) |
0 commit comments