Skip to content

Commit 2bef1dd

Browse files
committed
refactor: clean up shared types and update CI tests
- Remove unused WSClient and ServerConfig interfaces - Update test imports to use specific WSMessage types - Add args and workdir fields to session type definitions - Include types.test.ts in CI workflow for automated testing Improve type safety, eliminate dead code, and ensure type definitions are validated in CI.
1 parent 9330675 commit 2bef1dd

3 files changed

Lines changed: 34 additions & 63 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444

4545
- name: Run test
4646
if: matrix.quality == 'test'
47-
run: bun test websocket.test.ts web-server.test.ts
47+
run: bun test websocket.test.ts web-server.test.ts types.test.ts
4848

4949
- name: Type check
5050
if: matrix.quality == 'typecheck'

src/web/shared/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ServerWebSocket } from 'bun'
21
import type { PTYSessionInfo, SpawnOptions } from '../../plugin/pty/types'
32

43
export class CustomError extends Error {
@@ -110,11 +109,6 @@ export interface WSMessageServerError extends WSMessageServer {
110109
error: CustomError
111110
}
112111

113-
export interface WSClient {
114-
socket: ServerWebSocket<WSClient>
115-
subscribedSessions: Set<string>
116-
}
117-
118112
// React component types
119113
export interface Session {
120114
id: string

test/types.test.ts

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, it, expect } from 'bun:test'
2-
import type { WSMessage, SessionData, ServerConfig, WSClient } from '../src/web/shared/types.ts'
2+
import { CustomError, type WSMessageClientSubscribeSession, type WSMessageServerData, type WSMessageServerError, type WSMessageServerSessionList } from '../src/web/shared/types.ts'
3+
import type { PTYSessionInfo } from '../src/plugin/pty/types.ts'
34

45
describe('Web Types', () => {
56
describe('WSMessage', () => {
67
it('should validate subscribe message structure', () => {
7-
const message: WSMessage = {
8+
const message: WSMessageClientSubscribeSession = {
89
type: 'subscribe',
910
sessionId: 'pty_12345',
1011
}
@@ -14,31 +15,43 @@ describe('Web Types', () => {
1415
})
1516

1617
it('should validate data message structure', () => {
17-
const message: WSMessage = {
18+
const message: WSMessageServerData = {
1819
type: 'data',
19-
sessionId: 'pty_12345',
20+
session: {
21+
id: 'pty_12345',
22+
title: 'Test Session',
23+
command: 'echo',
24+
status: 'running',
25+
pid: 1234,
26+
lineCount: 10,
27+
createdAt: new Date(),
28+
args: ['test'],
29+
workdir: '/home/user',
30+
},
2031
data: ['test output', ''],
2132
}
2233

2334
expect(message.type).toBe('data')
24-
expect(message.sessionId).toBe('pty_12345')
35+
expect(message.session.id).toBe('pty_12345')
2536
expect(message.data).toEqual(['test output', ''])
2637
})
2738

2839
it('should validate session_list message structure', () => {
29-
const sessions: SessionData[] = [
40+
const sessions: PTYSessionInfo[] = [
3041
{
3142
id: 'pty_12345',
3243
title: 'Test Session',
3344
command: 'echo',
3445
status: 'running',
3546
pid: 1234,
3647
lineCount: 5,
37-
createdAt: '2026-01-21T10:00:00.000Z',
48+
createdAt: new Date(),
49+
args: ['hello'],
50+
workdir: '/home/user',
3851
},
3952
]
4053

41-
const message: WSMessage = {
54+
const message: WSMessageServerSessionList = {
4255
type: 'session_list',
4356
sessions,
4457
}
@@ -48,27 +61,29 @@ describe('Web Types', () => {
4861
})
4962

5063
it('should validate error message structure', () => {
51-
const message: WSMessage = {
64+
const message: WSMessageServerError = {
5265
type: 'error',
53-
error: 'Session not found',
66+
error: new CustomError('Session not found'),
5467
}
5568

5669
expect(message.type).toBe('error')
57-
expect(message.error).toBe('Session not found')
70+
expect(message.error.message).toBe('Session not found')
5871
})
5972
})
6073

6174
describe('SessionData', () => {
6275
it('should validate complete session data structure', () => {
63-
const session: SessionData = {
76+
const session: PTYSessionInfo = {
6477
id: 'pty_12345',
6578
title: 'Test Echo Session',
6679
command: 'echo',
6780
status: 'exited',
6881
exitCode: 0,
6982
pid: 1234,
7083
lineCount: 2,
71-
createdAt: '2026-01-21T10:00:00.000Z',
84+
createdAt: new Date(),
85+
args: ['Hello, World!'],
86+
workdir: '/home/user',
7287
}
7388

7489
expect(session.id).toBe('pty_12345')
@@ -78,62 +93,24 @@ describe('Web Types', () => {
7893
expect(session.exitCode).toBe(0)
7994
expect(session.pid).toBe(1234)
8095
expect(session.lineCount).toBe(2)
81-
expect(session.createdAt).toBe('2026-01-21T10:00:00.000Z')
96+
expect(session.createdAt).toBeInstanceOf(Date)
8297
})
8398

8499
it('should allow optional exitCode', () => {
85-
const session: SessionData = {
100+
const session: PTYSessionInfo = {
86101
id: 'pty_67890',
87102
title: 'Running Session',
88103
command: 'sleep',
89104
status: 'running',
90105
pid: 5678,
91106
lineCount: 0,
92-
createdAt: '2026-01-21T10:00:00.000Z',
107+
createdAt: new Date('2026-01-21T10:00:00.000Z'),
108+
args: ['Hello, World!'],
109+
workdir: '/home/user',
93110
}
94111

95112
expect(session.exitCode).toBeUndefined()
96113
expect(session.status).toBe('running')
97114
})
98115
})
99-
100-
describe('ServerConfig', () => {
101-
it('should validate server configuration', () => {
102-
const config: ServerConfig = {
103-
port: 8765,
104-
hostname: 'localhost',
105-
}
106-
107-
expect(config.port).toBe(8765)
108-
expect(config.hostname).toBe('localhost')
109-
})
110-
})
111-
112-
describe('WSClient', () => {
113-
it('should validate WebSocket client structure', () => {
114-
const mockWebSocket = {} as any // Mock WebSocket for testing
115-
116-
const client: WSClient = {
117-
socket: mockWebSocket,
118-
subscribedSessions: new Set(['pty_12345', 'pty_67890']),
119-
}
120-
121-
expect(client.socket).toBe(mockWebSocket)
122-
expect(client.subscribedSessions).toBeInstanceOf(Set)
123-
expect(client.subscribedSessions.has('pty_12345')).toBe(true)
124-
expect(client.subscribedSessions.has('pty_67890')).toBe(true)
125-
expect(client.subscribedSessions.has('pty_99999')).toBe(false)
126-
})
127-
128-
it('should handle empty subscriptions', () => {
129-
const mockWebSocket = {} as any
130-
131-
const client: WSClient = {
132-
socket: mockWebSocket,
133-
subscribedSessions: new Set(),
134-
}
135-
136-
expect(client.subscribedSessions.size).toBe(0)
137-
})
138-
})
139116
})

0 commit comments

Comments
 (0)