Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/happy-app/sources/sync/ops.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Tests for session operations (ops.ts)
*/

import { describe, it, expect, vi, beforeEach } from 'vitest';

const { mockSessionRPC } = vi.hoisted(() => ({
mockSessionRPC: vi.fn(),
}));

vi.mock('./apiSocket', () => ({
apiSocket: {
sessionRPC: mockSessionRPC,
}
}));

vi.mock('./sync', () => ({
sync: {}
}));

import { sessionKill } from './ops';

describe('sessionKill', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('should return success when RPC succeeds', async () => {
mockSessionRPC.mockResolvedValue({
success: true,
message: 'Killing happy-cli process'
});

const result = await sessionKill('test-session-123');

expect(result.success).toBe(true);
expect(mockSessionRPC).toHaveBeenCalledWith(
'test-session-123',
'killSession',
{}
);
});

it('should return success when RPC fails because process already exited (regression #687)', async () => {
mockSessionRPC.mockRejectedValue(new Error('RPC call failed'));

const result = await sessionKill('dead-session-456');

expect(result.success).toBe(true);
expect(result.message).toBe('Session already stopped');
});

it('should return success even on non-Error RPC failures', async () => {
mockSessionRPC.mockRejectedValue('timeout');

const result = await sessionKill('dead-session-789');

expect(result.success).toBe(true);
expect(result.message).toBe('Session already stopped');
});
});
11 changes: 8 additions & 3 deletions packages/happy-app/sources/sync/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ export async function sessionRipgrep(
}

/**
* Kill the session process immediately
* Kill the session process immediately.
* Idempotent: if the process already exited, treat it as success
* since the desired outcome (session stopped) is already achieved.
*/
export async function sessionKill(sessionId: string): Promise<SessionKillResponse> {
try {
Expand All @@ -484,9 +486,12 @@ export async function sessionKill(sessionId: string): Promise<SessionKillRespons
);
return response;
} catch (error) {
// RPC failure likely means the session process already exited.
// The user's intent is "stop this session" — if it's already
// stopped, that's a success, not an error.
return {
success: false,
message: error instanceof Error ? error.message : 'Unknown error'
success: true,
message: 'Session already stopped'
};
}
}
Expand Down