Skip to content

Commit 533e161

Browse files
author
Megha Narayanan
committed
misc merging stuff
1 parent a74efcb commit 533e161

File tree

5 files changed

+143
-47
lines changed

5 files changed

+143
-47
lines changed

packages/cli/src/commands/sandbox/sandbox-devtools/react-app/src/services/sandbox_client_service.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ void describe('SandboxClientService', () => {
9292
});
9393
});
9494

95+
void describe('getSavedCloudFormationEvents', () => {
96+
void it('emits GET_SAVED_CLOUD_FORMATION_EVENTS event', () => {
97+
service.getSavedCloudFormationEvents();
98+
99+
assert.strictEqual(mockSocket.mockEmit.mock.callCount(), 1);
100+
assert.strictEqual(
101+
mockSocket.mockEmit.mock.calls[0].arguments[0],
102+
SOCKET_EVENTS.GET_SAVED_CLOUD_FORMATION_EVENTS,
103+
);
104+
});
105+
});
106+
95107
void describe('saveLogSettings', () => {
96108
void it('emits SAVE_LOG_SETTINGS event with correct parameters', () => {
97109
const settings = { maxLogSizeMB: 50 };
@@ -216,5 +228,17 @@ void describe('SandboxClientService', () => {
216228
SOCKET_EVENTS.SAVED_CONSOLE_LOGS,
217229
);
218230
});
231+
232+
void it('registers onSavedCloudFormationEvents handler correctly', () => {
233+
const mockHandler = mock.fn();
234+
235+
service.onSavedCloudFormationEvents(mockHandler);
236+
237+
assert.strictEqual(mockSocket.mockOn.mock.callCount(), 1);
238+
assert.strictEqual(
239+
mockSocket.mockOn.mock.calls[0].arguments[0],
240+
SOCKET_EVENTS.SAVED_CLOUD_FORMATION_EVENTS,
241+
);
242+
});
219243
});
220244
});

packages/cli/src/commands/sandbox/sandbox-devtools/react-app/src/services/sandbox_client_service.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import {
77
} from '../../../shared/socket_types';
88
import { DeploymentEvent } from './deployment_client_service';
99

10+
/**
11+
* Interface for log settings data
12+
*/
13+
export interface LogSettings {
14+
maxLogSizeMB: number;
15+
currentSizeMB?: number;
16+
}
1017
/**
1118
* Service for handling sandbox-related socket communication
1219
*/
@@ -58,6 +65,13 @@ export class SandboxClientService extends SocketClientService {
5865
return this.on(SOCKET_EVENTS.SANDBOX_STATUS, handler);
5966
}
6067

68+
/**
69+
* Gets saved CloudFormation events
70+
*/
71+
public getSavedCloudFormationEvents(): void {
72+
this.emit(SOCKET_EVENTS.GET_SAVED_CLOUD_FORMATION_EVENTS);
73+
}
74+
6175
/**
6276
* Saves log settings
6377
* @param settings The log settings to save
@@ -77,13 +91,6 @@ export class SandboxClientService extends SocketClientService {
7791
return this.on(SOCKET_EVENTS.LOG_SETTINGS, handler);
7892
}
7993

80-
/**
81-
* Gets saved CloudFormation events
82-
*/
83-
public getSavedCloudFormationEvents(): void {
84-
this.emit(SOCKET_EVENTS.GET_SAVED_CLOUD_FORMATION_EVENTS);
85-
}
86-
8794
/**
8895
* Gets log settings
8996
*/
@@ -102,17 +109,6 @@ export class SandboxClientService extends SocketClientService {
102109
return this.on('log', handler);
103110
}
104111

105-
/**
106-
* Registers a handler for saved CloudFormation events
107-
* @param handler The event handler
108-
* @returns An object with an unsubscribe method
109-
*/
110-
public onSavedCloudFormationEvents(
111-
handler: (events: DeploymentEvent[]) => void,
112-
): { unsubscribe: () => void } {
113-
return this.on(SOCKET_EVENTS.SAVED_CLOUD_FORMATION_EVENTS, handler);
114-
}
115-
116112
/**
117113
* Saves console logs
118114
* @param logs The console logs to save
@@ -138,4 +134,15 @@ export class SandboxClientService extends SocketClientService {
138134
} {
139135
return this.on(SOCKET_EVENTS.SAVED_CONSOLE_LOGS, handler);
140136
}
137+
138+
/**
139+
* Registers a handler for saved CloudFormation events
140+
* @param handler The event handler
141+
* @returns An object with an unsubscribe method
142+
*/
143+
public onSavedCloudFormationEvents(
144+
handler: (events: DeploymentEvent[]) => void,
145+
): { unsubscribe: () => void } {
146+
return this.on(SOCKET_EVENTS.SAVED_CLOUD_FORMATION_EVENTS, handler);
147+
}
141148
}

packages/cli/src/commands/sandbox/sandbox-devtools/sandbox_devtools_command.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ export class SandboxDevToolsCommand implements CommandModule<object> {
181181
storageManager: LocalStorageManager,
182182
): void {
183183
// Listen for deployment started
184-
sandbox.on('deploymentStarted', (data: { timestamp?: string }) => { void (async () => {
184+
sandbox.on('deploymentStarted', (data: { timestamp?: string }) => {
185+
void (async () => {
185186
this.printer.log('Deployment started', LogLevel.DEBUG);
186187

187188
const currentState = await getSandboxState();
@@ -194,20 +195,20 @@ export class SandboxDevToolsCommand implements CommandModule<object> {
194195
);
195196
}
196197

197-
// Clear CloudFormation events when a new deployment starts
198-
storageManager.clearCloudFormationEvents();
199-
// Reset CloudFormation timestamp to avoid showing old events
200-
storageManager.clearCloudFormationTimestamp();
201-
// Save current timestamp to start tracking from now
202-
storageManager.saveLastCloudFormationTimestamp(new Date());
203-
this.printer.log(
204-
'Cleared previous CloudFormation events and reset timestamp',
205-
LogLevel.DEBUG,
206-
);
207-
this.printer.log(
208-
'Cleared previous CloudFormation events',
209-
LogLevel.DEBUG,
210-
);
198+
// Clear CloudFormation events when a new deployment starts
199+
storageManager.clearCloudFormationEvents();
200+
// Reset CloudFormation timestamp to avoid showing old events
201+
storageManager.clearCloudFormationTimestamp();
202+
// Save current timestamp to start tracking from now
203+
storageManager.saveLastCloudFormationTimestamp(new Date());
204+
this.printer.log(
205+
'Cleared previous CloudFormation events and reset timestamp',
206+
LogLevel.DEBUG,
207+
);
208+
this.printer.log(
209+
'Cleared previous CloudFormation events',
210+
LogLevel.DEBUG,
211+
);
211212
const statusData: SandboxStatusData = {
212213
status: currentState, // This should be 'deploying' after deployment starts,
213214
identifier: backendId.name,
@@ -246,20 +247,20 @@ export class SandboxDevToolsCommand implements CommandModule<object> {
246247

247248
const currentState = await getSandboxState();
248249
// Clear CloudFormation events when a new deployment starts
249-
storageManager.clearCloudFormationEvents();
250-
// Reset CloudFormation timestamp to avoid showing old events
251-
storageManager.clearCloudFormationTimestamp();
252-
// Save current timestamp to start tracking from now
253-
storageManager.saveLastCloudFormationTimestamp(new Date());
254-
this.printer.log(
255-
'Cleared previous CloudFormation events and reset timestamp',
256-
LogLevel.DEBUG,
257-
);
258-
this.printer.log(
259-
'Cleared previous CloudFormation events',
260-
LogLevel.DEBUG,
261-
);
262-
const statusData: SandboxStatusData = {
250+
storageManager.clearCloudFormationEvents();
251+
// Reset CloudFormation timestamp to avoid showing old events
252+
storageManager.clearCloudFormationTimestamp();
253+
// Save current timestamp to start tracking from now
254+
storageManager.saveLastCloudFormationTimestamp(new Date());
255+
this.printer.log(
256+
'Cleared previous CloudFormation events and reset timestamp',
257+
LogLevel.DEBUG,
258+
);
259+
this.printer.log(
260+
'Cleared previous CloudFormation events',
261+
LogLevel.DEBUG,
262+
);
263+
const statusData: SandboxStatusData = {
263264
status: currentState, // This should be 'deleting' after deletion starts
264265
identifier: backendId.name,
265266
message: 'Deletion started',

packages/cli/src/commands/sandbox/sandbox-devtools/services/socket_handlers_resources.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,68 @@ export class SocketHandlerResources {
5252
);
5353
}
5454

55+
/**
56+
* Handles the testLambdaFunction event
57+
*/
58+
public async handleTestLambdaFunction(
59+
socket: Socket,
60+
data: SocketEvents['testLambdaFunction'],
61+
): Promise<void> {
62+
if (!data?.resourceId || !data?.functionName) {
63+
socket.emit(SOCKET_EVENTS.LAMBDA_TEST_RESULT, {
64+
resourceId: data?.resourceId || 'unknown',
65+
error: 'Invalid function information provided',
66+
});
67+
return;
68+
}
69+
70+
try {
71+
const { resourceId, functionName, input } = data;
72+
73+
// Parse the input as JSON
74+
let payload: Record<string, unknown>;
75+
try {
76+
payload = input ? JSON.parse(input) : {};
77+
} catch (error) {
78+
socket.emit(SOCKET_EVENTS.LAMBDA_TEST_RESULT, {
79+
resourceId,
80+
error: `Invalid JSON input: ${String(error)}`,
81+
});
82+
return;
83+
}
84+
85+
// Invoke the Lambda function
86+
const command = new InvokeCommand({
87+
FunctionName: functionName,
88+
Payload: JSON.stringify(payload),
89+
});
90+
91+
const response = await this.lambdaClient.send(command);
92+
93+
// Parse the response payload
94+
let result = '';
95+
if (response.Payload) {
96+
const responseText = Buffer.from(response.Payload).toString('utf-8');
97+
result = responseText;
98+
}
99+
100+
// Send the result to the client
101+
socket.emit(SOCKET_EVENTS.LAMBDA_TEST_RESULT, {
102+
resourceId,
103+
result,
104+
});
105+
} catch (error) {
106+
this.printer.log(
107+
`Error testing Lambda function ${data.functionName}: ${String(error)}`,
108+
LogLevel.ERROR,
109+
);
110+
socket.emit(SOCKET_EVENTS.LAMBDA_TEST_RESULT, {
111+
resourceId: data.resourceId,
112+
error: String(error),
113+
});
114+
}
115+
}
116+
55117
/**
56118
* Handles the getSavedCloudFormationEvents event
57119
*/

packages/cli/src/commands/sandbox/sandbox-devtools/shared/socket_events.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const SOCKET_EVENTS = {
9393
* Event received when log settings are sent from the server
9494
*/
9595
LOG_SETTINGS: 'logSettings',
96+
9697
/**
9798
* Event to test a Lambda function
9899
*/
@@ -152,6 +153,7 @@ export const SOCKET_EVENTS = {
152153
* Event received when saved console logs are sent from the server
153154
*/
154155
SAVED_CONSOLE_LOGS: 'savedConsoleLogs',
156+
155157
/**
156158
* Event which triggers UI to show a deployment error
157159
* Contains error details like name, message, resolution, and timestamp.

0 commit comments

Comments
 (0)