|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import { clearLargeValueCacheForTests } from '@/lib/execution/payloads/cache' |
| 6 | +import { |
| 7 | + MAX_DURABLE_LARGE_VALUE_BYTES, |
| 8 | + MAX_TRACE_ARCHIVE_BYTES, |
| 9 | +} from '@/lib/execution/payloads/limits' |
| 10 | +import { storeLargeValue } from '@/lib/execution/payloads/store' |
| 11 | +import { EXECUTION_RESOURCE_LIMIT_CODE } from '@/lib/execution/resource-errors' |
| 12 | +import { |
| 13 | + externalizeExecutionData, |
| 14 | + materializeExecutionData, |
| 15 | + TRACE_STORE_REF_KEY, |
| 16 | +} from '@/lib/logs/execution/trace-store' |
| 17 | + |
| 18 | +const { mockUploadFile, mockDownloadFile, mockRegisterOwner, mockAddReference } = vi.hoisted( |
| 19 | + () => ({ |
| 20 | + mockUploadFile: vi.fn(), |
| 21 | + mockDownloadFile: vi.fn(), |
| 22 | + mockRegisterOwner: vi.fn(), |
| 23 | + mockAddReference: vi.fn(), |
| 24 | + }) |
| 25 | +) |
| 26 | + |
| 27 | +/** Scale the two caps down to exercise real serialization and storage reads with small fixtures. */ |
| 28 | +vi.mock('@/lib/execution/payloads/limits', async (importOriginal) => ({ |
| 29 | + ...(await importOriginal<typeof import('@/lib/execution/payloads/limits')>()), |
| 30 | + MAX_DURABLE_LARGE_VALUE_BYTES: 1024, |
| 31 | + MAX_TRACE_ARCHIVE_BYTES: 4096, |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('@/lib/uploads', () => ({ |
| 35 | + StorageService: { uploadFile: mockUploadFile, downloadFile: mockDownloadFile }, |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('@/lib/execution/payloads/large-value-metadata', () => ({ |
| 39 | + registerLargeValueOwner: mockRegisterOwner, |
| 40 | + addLargeValueReference: mockAddReference, |
| 41 | +})) |
| 42 | + |
| 43 | +const CONTEXT = { |
| 44 | + workspaceId: 'workspace-1', |
| 45 | + workflowId: 'workflow-1', |
| 46 | + executionId: 'execution-1', |
| 47 | + userId: 'user-1', |
| 48 | +} |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + vi.clearAllMocks() |
| 52 | + clearLargeValueCacheForTests() |
| 53 | + mockRegisterOwner.mockResolvedValue(true) |
| 54 | + mockUploadFile.mockImplementation(async ({ customKey, file }) => { |
| 55 | + mockDownloadFile.mockResolvedValue(file) |
| 56 | + return { key: customKey } |
| 57 | + }) |
| 58 | +}) |
| 59 | + |
| 60 | +describe('trace archive storage round trip', () => { |
| 61 | + it('uploads an archive above the ordinary value cap and reads it back after a cache miss', async () => { |
| 62 | + const data = { |
| 63 | + traceSpans: [{ id: 'span-1', output: 'é'.repeat(MAX_DURABLE_LARGE_VALUE_BYTES) }], |
| 64 | + traceSpanCount: 1, |
| 65 | + hasTraceSpans: true, |
| 66 | + } |
| 67 | + const json = JSON.stringify(data) |
| 68 | + const size = Buffer.byteLength(json, 'utf8') |
| 69 | + expect(size).toBeGreaterThan(MAX_DURABLE_LARGE_VALUE_BYTES) |
| 70 | + expect(size).toBeLessThan(MAX_TRACE_ARCHIVE_BYTES) |
| 71 | + |
| 72 | + await expect(storeLargeValue(data, json, size, CONTEXT)).rejects.toMatchObject({ |
| 73 | + code: EXECUTION_RESOURCE_LIMIT_CODE, |
| 74 | + }) |
| 75 | + expect(mockUploadFile).not.toHaveBeenCalled() |
| 76 | + |
| 77 | + const slim = await externalizeExecutionData(data, CONTEXT, { throwOnError: true }) |
| 78 | + expect(slim).toEqual({ |
| 79 | + [TRACE_STORE_REF_KEY]: expect.objectContaining({ size, key: expect.any(String) }), |
| 80 | + traceSpanCount: 1, |
| 81 | + hasTraceSpans: true, |
| 82 | + }) |
| 83 | + expect(mockRegisterOwner).toHaveBeenCalledWith( |
| 84 | + expect.objectContaining({ |
| 85 | + workspaceId: CONTEXT.workspaceId, |
| 86 | + workflowId: CONTEXT.workflowId, |
| 87 | + executionId: CONTEXT.executionId, |
| 88 | + size, |
| 89 | + }), |
| 90 | + [] |
| 91 | + ) |
| 92 | + clearLargeValueCacheForTests() |
| 93 | + |
| 94 | + await expect(materializeExecutionData(slim, CONTEXT)).resolves.toEqual(data) |
| 95 | + expect(mockDownloadFile).toHaveBeenCalledExactlyOnceWith({ |
| 96 | + key: expect.any(String), |
| 97 | + context: 'execution', |
| 98 | + maxBytes: size, |
| 99 | + }) |
| 100 | + expect(mockAddReference).not.toHaveBeenCalled() |
| 101 | + }) |
| 102 | + |
| 103 | + it('rejects an over-limit archive before uploading it', async () => { |
| 104 | + await expect( |
| 105 | + externalizeExecutionData( |
| 106 | + { traceSpans: [{ output: 'x'.repeat(MAX_TRACE_ARCHIVE_BYTES) }] }, |
| 107 | + CONTEXT, |
| 108 | + { throwOnError: true } |
| 109 | + ) |
| 110 | + ).rejects.toMatchObject({ code: EXECUTION_RESOURCE_LIMIT_CODE }) |
| 111 | + expect(mockUploadFile).not.toHaveBeenCalled() |
| 112 | + expect(mockRegisterOwner).not.toHaveBeenCalled() |
| 113 | + }) |
| 114 | + |
| 115 | + it('bounds stored archive reads even if a reference declares a larger size', async () => { |
| 116 | + const data = { traceSpans: [], hasTraceSpans: false } |
| 117 | + const slim = await externalizeExecutionData(data, CONTEXT, { throwOnError: true }) |
| 118 | + clearLargeValueCacheForTests() |
| 119 | + |
| 120 | + await expect( |
| 121 | + materializeExecutionData( |
| 122 | + { |
| 123 | + ...slim, |
| 124 | + [TRACE_STORE_REF_KEY]: { |
| 125 | + ...(slim[TRACE_STORE_REF_KEY] as Record<string, unknown>), |
| 126 | + size: MAX_TRACE_ARCHIVE_BYTES + 1, |
| 127 | + }, |
| 128 | + }, |
| 129 | + CONTEXT |
| 130 | + ) |
| 131 | + ).resolves.toEqual({ hasTraceSpans: false }) |
| 132 | + expect(mockDownloadFile).not.toHaveBeenCalled() |
| 133 | + }) |
| 134 | +}) |
0 commit comments