|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('@/lib/workflows/subblocks/options', () => ({ |
| 7 | + fetchTriggerTypeOptions: vi.fn(), |
| 8 | + fetchWorkspaceWorkflowOptions: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +import { LogsV2Block } from '@/blocks/blocks/logs' |
| 12 | + |
| 13 | +function buildQueryParams(params: Record<string, unknown>) { |
| 14 | + return LogsV2Block.tools.config!.params!({ operation: 'query', ...params }) |
| 15 | +} |
| 16 | + |
| 17 | +describe('LogsV2Block trigger filter', () => { |
| 18 | + it('omits triggers when the filter is untouched, leaving pre-existing queries unfiltered', () => { |
| 19 | + expect(buildQueryParams({}).triggers).toBeUndefined() |
| 20 | + expect(buildQueryParams({ triggers: [] }).triggers).toBeUndefined() |
| 21 | + expect(buildQueryParams({ triggers: '' }).triggers).toBeUndefined() |
| 22 | + }) |
| 23 | + |
| 24 | + it('joins a multi-select selection into the comma-separated list the API expects', () => { |
| 25 | + expect(buildQueryParams({ triggers: ['api', 'schedule'] }).triggers).toBe('api,schedule') |
| 26 | + }) |
| 27 | + |
| 28 | + it('flattens a merged option id so one label can select several trigger values', () => { |
| 29 | + expect(buildQueryParams({ triggers: ['api', 'copilot,mothership'] }).triggers).toBe( |
| 30 | + 'api,copilot,mothership' |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + it('accepts an advanced-mode string of provider ids', () => { |
| 35 | + expect(buildQueryParams({ triggers: ' slack,gmail ' }).triggers).toBe('slack,gmail') |
| 36 | + }) |
| 37 | + |
| 38 | + it('trims each hand-typed entry, not just the ends of the string', () => { |
| 39 | + // The filters split on commas without trimming, so a surviving space would |
| 40 | + // match no stored trigger and silently narrow the result set to nothing. |
| 41 | + expect(buildQueryParams({ triggers: 'api, schedule, slack' }).triggers).toBe( |
| 42 | + 'api,schedule,slack' |
| 43 | + ) |
| 44 | + expect(buildQueryParams({ triggers: 'api,,schedule,' }).triggers).toBe('api,schedule') |
| 45 | + expect(buildQueryParams({ triggers: ' , ' }).triggers).toBeUndefined() |
| 46 | + }) |
| 47 | + |
| 48 | + it('trims entries inside a multi-select selection too', () => { |
| 49 | + expect(buildQueryParams({ triggers: ['api ', ' copilot, mothership'] }).triggers).toBe( |
| 50 | + 'api,copilot,mothership' |
| 51 | + ) |
| 52 | + }) |
| 53 | + |
| 54 | + it('never sends triggers on the run-details operation', () => { |
| 55 | + expect( |
| 56 | + LogsV2Block.tools.config!.params!({ |
| 57 | + operation: 'get_run_details', |
| 58 | + runId: 'run-1', |
| 59 | + triggers: ['api'], |
| 60 | + }) |
| 61 | + ).toEqual({ runId: 'run-1' }) |
| 62 | + }) |
| 63 | +}) |
| 64 | + |
| 65 | +describe('LogsV2Block backwards compatibility', () => { |
| 66 | + // `joinIds` is shared with the pre-existing workflow and status filters, so the |
| 67 | + // per-entry trimming added for hand-typed triggers must not move their output. |
| 68 | + // Every value a stored multi-select or advanced field can hold is listed here: |
| 69 | + // option ids and workflow ids contain neither spaces nor commas. |
| 70 | + const UUID = '3f2504e0-4f89-11d3-9a0c-0305e82c3301' |
| 71 | + |
| 72 | + it.each([ |
| 73 | + ['unset', undefined, undefined], |
| 74 | + ['empty selection', [], undefined], |
| 75 | + ['one workflow', [UUID], UUID], |
| 76 | + [ |
| 77 | + 'two workflows', |
| 78 | + [UUID, 'b7a1c2d3-0000-4000-8000-000000000001'], |
| 79 | + `${UUID},b7a1c2d3-0000-4000-8000-000000000001`, |
| 80 | + ], |
| 81 | + ['advanced string', 'id-one,id-two', 'id-one,id-two'], |
| 82 | + ['empty string', '', undefined], |
| 83 | + ])('leaves workflowIds untouched for %s', (_name, value, expected) => { |
| 84 | + expect(buildQueryParams({ workflowIds: value }).workflowIds).toBe(expected) |
| 85 | + }) |
| 86 | + |
| 87 | + it.each([ |
| 88 | + ['unset', undefined, undefined], |
| 89 | + ['empty selection', [], undefined], |
| 90 | + ['one status', ['info'], 'info'], |
| 91 | + ['several statuses', ['info', 'error', 'cancelled'], 'info,error,cancelled'], |
| 92 | + ])('leaves level untouched for %s', (_name, value, expected) => { |
| 93 | + expect(buildQueryParams({ level: value }).level).toBe(expected) |
| 94 | + }) |
| 95 | + |
| 96 | + it('omits triggers entirely for a block saved before the filter existed', () => { |
| 97 | + const params = buildQueryParams({ workflowIds: [UUID], level: ['info'] }) |
| 98 | + expect(params.triggers).toBeUndefined() |
| 99 | + // Undefined values are dropped on serialization, so nothing reaches the query. |
| 100 | + expect(JSON.stringify(params)).not.toContain('triggers') |
| 101 | + }) |
| 102 | +}) |
| 103 | + |
| 104 | +describe('LogsV2Block trigger subblocks', () => { |
| 105 | + const subBlockIds = LogsV2Block.subBlocks.map((subBlock) => subBlock.id) |
| 106 | + |
| 107 | + it('declares triggers as the string it is transformed into', () => { |
| 108 | + // The generic handler JSON.parses any post-transform input declared 'array' or |
| 109 | + // 'json', so declaring the joined string as an array would warn on every run |
| 110 | + // and would turn JSON-looking advanced input into an array the tool rejects. |
| 111 | + expect(LogsV2Block.inputs.triggers.type).toBe('string') |
| 112 | + expect(typeof buildQueryParams({ triggers: ['api', 'schedule'] }).triggers).toBe('string') |
| 113 | + }) |
| 114 | + |
| 115 | + it('exposes basic and advanced modes behind one canonical param', () => { |
| 116 | + expect(subBlockIds).toContain('triggerSelector') |
| 117 | + expect(subBlockIds).toContain('manualTriggers') |
| 118 | + |
| 119 | + for (const id of ['triggerSelector', 'manualTriggers']) { |
| 120 | + const subBlock = LogsV2Block.subBlocks.find((candidate) => candidate.id === id) |
| 121 | + expect(subBlock?.canonicalParamId).toBe('triggers') |
| 122 | + expect(subBlock?.condition).toEqual({ field: 'operation', value: 'query' }) |
| 123 | + } |
| 124 | + }) |
| 125 | +}) |
0 commit comments