Skip to content

Commit 11afad3

Browse files
committed
fix(worker): gate human input activity diagnostics
Signed-off-by: zebbern <185730623+zebbern@users.noreply.github.com>
1 parent 07ff730 commit 11afad3

2 files changed

Lines changed: 116 additions & 3 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
});

worker/src/temporal/activities/human-input.activity.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ITraceService } from '@sentris/component-sdk';
55
import { ConfigurationError } from '@sentris/component-sdk';
66
import * as schema from '../../adapters/schema';
77
import type { HumanInputType } from '../../adapters/schema';
8+
import { workflowDiagnosticLog } from '../workflow-diagnostics';
89

910
/**
1011
* Human input request creation input
@@ -97,7 +98,7 @@ export async function createHumanInputRequestActivity(
9798
organizationId: input.organizationId ?? null,
9899
});
99100

100-
console.log(
101+
workflowDiagnosticLog(
101102
`[HumanInputActivity] Created ${input.inputType} request ${requestId} for run ${input.runId}, node ${input.nodeRef}`,
102103
);
103104

@@ -148,7 +149,7 @@ export async function cancelHumanInputRequestActivity(requestId: string): Promis
148149
})
149150
.where(eq(schema.humanInputRequestsTable.id, requestId));
150151

151-
console.log(`[HumanInputActivity] Cancelled human input request ${requestId}`);
152+
workflowDiagnosticLog(`[HumanInputActivity] Cancelled human input request ${requestId}`);
152153
}
153154

154155
/**
@@ -168,5 +169,5 @@ export async function expireHumanInputRequestActivity(requestId: string): Promis
168169
})
169170
.where(eq(schema.humanInputRequestsTable.id, requestId));
170171

171-
console.log(`[HumanInputActivity] Expired human input request ${requestId}`);
172+
workflowDiagnosticLog(`[HumanInputActivity] Expired human input request ${requestId}`);
172173
}

0 commit comments

Comments
 (0)