|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'bun:test'; |
| 2 | +import type { ITraceService } from '@sentris/component-sdk'; |
| 3 | +import type { NodePgDatabase } from 'drizzle-orm/node-postgres'; |
| 4 | +import * as schema from '../../../adapters/schema'; |
| 5 | +import { |
| 6 | + cancelHumanInputRequestActivity, |
| 7 | + createHumanInputRequestActivity, |
| 8 | + expireHumanInputRequestActivity, |
| 9 | + initializeHumanInputActivity, |
| 10 | +} from '../human-input.activity'; |
| 11 | + |
| 12 | +const originalDebugWorkflow = process.env.SENTRIS_DEBUG_WORKFLOW; |
| 13 | + |
| 14 | +function createMockDatabase() { |
| 15 | + const values = vi.fn(async () => {}); |
| 16 | + const where = vi.fn(async () => {}); |
| 17 | + const set = vi.fn(() => ({ where })); |
| 18 | + |
| 19 | + return { |
| 20 | + values, |
| 21 | + where, |
| 22 | + set, |
| 23 | + db: { |
| 24 | + insert: vi.fn(() => ({ values })), |
| 25 | + update: vi.fn(() => ({ set })), |
| 26 | + }, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +describe('human input activity diagnostics', () => { |
| 31 | + beforeEach(() => { |
| 32 | + delete process.env.SENTRIS_DEBUG_WORKFLOW; |
| 33 | + vi.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + if (originalDebugWorkflow === undefined) { |
| 38 | + delete process.env.SENTRIS_DEBUG_WORKFLOW; |
| 39 | + } else { |
| 40 | + process.env.SENTRIS_DEBUG_WORKFLOW = originalDebugWorkflow; |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + test('createHumanInputRequestActivity does not mirror successful creation diagnostics to console.log by default', async () => { |
| 45 | + const database = createMockDatabase(); |
| 46 | + const trace = { record: vi.fn() }; |
| 47 | + initializeHumanInputActivity({ |
| 48 | + database: database.db as unknown as NodePgDatabase<typeof schema>, |
| 49 | + trace: trace as unknown as ITraceService, |
| 50 | + baseUrl: 'https://sentris.test', |
| 51 | + }); |
| 52 | + const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 53 | + |
| 54 | + try { |
| 55 | + const result = await createHumanInputRequestActivity({ |
| 56 | + runId: 'run-1', |
| 57 | + workflowId: 'workflow-1', |
| 58 | + nodeRef: 'approval-node', |
| 59 | + inputType: 'approval', |
| 60 | + title: 'Approve deployment', |
| 61 | + description: 'Check the deployment', |
| 62 | + organizationId: 'org-1', |
| 63 | + }); |
| 64 | + |
| 65 | + expect(result.resolveUrl).toStartWith('https://sentris.test/api/v1/human-inputs/resolve/'); |
| 66 | + expect(database.db.insert).toHaveBeenCalledWith(schema.humanInputRequestsTable); |
| 67 | + expect(database.values).toHaveBeenCalledTimes(1); |
| 68 | + expect(trace.record).toHaveBeenCalledTimes(1); |
| 69 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 70 | + } finally { |
| 71 | + consoleLogSpy.mockRestore(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + test('cancelHumanInputRequestActivity does not mirror successful cancellation diagnostics to console.log by default', async () => { |
| 76 | + const database = createMockDatabase(); |
| 77 | + initializeHumanInputActivity({ |
| 78 | + database: database.db as unknown as NodePgDatabase<typeof schema>, |
| 79 | + }); |
| 80 | + const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 81 | + |
| 82 | + try { |
| 83 | + await cancelHumanInputRequestActivity('request-1'); |
| 84 | + |
| 85 | + expect(database.db.update).toHaveBeenCalledWith(schema.humanInputRequestsTable); |
| 86 | + expect(database.set).toHaveBeenCalledWith(expect.objectContaining({ status: 'cancelled' })); |
| 87 | + expect(database.where).toHaveBeenCalledTimes(1); |
| 88 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 89 | + } finally { |
| 90 | + consoleLogSpy.mockRestore(); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + test('expireHumanInputRequestActivity does not mirror successful expiration diagnostics to console.log by default', async () => { |
| 95 | + const database = createMockDatabase(); |
| 96 | + initializeHumanInputActivity({ |
| 97 | + database: database.db as unknown as NodePgDatabase<typeof schema>, |
| 98 | + }); |
| 99 | + const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 100 | + |
| 101 | + try { |
| 102 | + await expireHumanInputRequestActivity('request-1'); |
| 103 | + |
| 104 | + expect(database.db.update).toHaveBeenCalledWith(schema.humanInputRequestsTable); |
| 105 | + expect(database.set).toHaveBeenCalledWith(expect.objectContaining({ status: 'expired' })); |
| 106 | + expect(database.where).toHaveBeenCalledTimes(1); |
| 107 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 108 | + } finally { |
| 109 | + consoleLogSpy.mockRestore(); |
| 110 | + } |
| 111 | + }); |
| 112 | +}); |
0 commit comments