forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-shell.test.ts
More file actions
149 lines (115 loc) · 4.88 KB
/
Copy pathsession-shell.test.ts
File metadata and controls
149 lines (115 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { describe, it, expect } from "vitest";
import { CopilotSession } from "../src/session.js";
import type { ShellOutputNotification, ShellExitNotification } from "../src/types.js";
// Create a minimal mock session for testing
function createMockSession(): CopilotSession {
const mockConnection = {
sendRequest: async () => ({}),
} as any;
return new CopilotSession("test-session", mockConnection);
}
describe("CopilotSession shell notifications", () => {
describe("onShellOutput", () => {
it("should register and dispatch shell output notifications", () => {
const session = createMockSession();
const received: ShellOutputNotification[] = [];
session.onShellOutput((notification) => {
received.push(notification);
});
const notification: ShellOutputNotification = {
processId: "proc-1",
stream: "stdout",
data: "hello world\n",
};
session._dispatchShellOutput(notification);
expect(received).toHaveLength(1);
expect(received[0]).toEqual(notification);
});
it("should support multiple handlers", () => {
const session = createMockSession();
const received1: ShellOutputNotification[] = [];
const received2: ShellOutputNotification[] = [];
session.onShellOutput((n) => received1.push(n));
session.onShellOutput((n) => received2.push(n));
const notification: ShellOutputNotification = {
processId: "proc-1",
stream: "stderr",
data: "error output",
};
session._dispatchShellOutput(notification);
expect(received1).toHaveLength(1);
expect(received2).toHaveLength(1);
});
it("should unsubscribe when the returned function is called", () => {
const session = createMockSession();
const received: ShellOutputNotification[] = [];
const unsubscribe = session.onShellOutput((n) => received.push(n));
session._dispatchShellOutput({
processId: "proc-1",
stream: "stdout",
data: "first",
});
unsubscribe();
session._dispatchShellOutput({
processId: "proc-1",
stream: "stdout",
data: "second",
});
expect(received).toHaveLength(1);
expect(received[0].data).toBe("first");
});
it("should not crash when a handler throws", () => {
const session = createMockSession();
const received: ShellOutputNotification[] = [];
session.onShellOutput(() => {
throw new Error("handler error");
});
session.onShellOutput((n) => received.push(n));
session._dispatchShellOutput({
processId: "proc-1",
stream: "stdout",
data: "test",
});
expect(received).toHaveLength(1);
});
});
describe("onShellExit", () => {
it("should register and dispatch shell exit notifications", () => {
const session = createMockSession();
const received: ShellExitNotification[] = [];
session.onShellExit((notification) => {
received.push(notification);
});
const notification: ShellExitNotification = {
processId: "proc-1",
exitCode: 0,
};
session._dispatchShellExit(notification);
expect(received).toHaveLength(1);
expect(received[0]).toEqual(notification);
});
it("should unsubscribe when the returned function is called", () => {
const session = createMockSession();
const received: ShellExitNotification[] = [];
const unsubscribe = session.onShellExit((n) => received.push(n));
session._dispatchShellExit({ processId: "proc-1", exitCode: 0 });
unsubscribe();
session._dispatchShellExit({ processId: "proc-2", exitCode: 1 });
expect(received).toHaveLength(1);
});
});
describe("shell process tracking", () => {
it("should track and untrack process IDs via callbacks", () => {
const session = createMockSession();
const registered = new Map<string, any>();
session._setShellProcessCallbacks(
(processId, s) => registered.set(processId, s),
(processId) => registered.delete(processId)
);
session._trackShellProcess("proc-1");
expect(registered.has("proc-1")).toBe(true);
session._untrackShellProcess("proc-1");
expect(registered.has("proc-1")).toBe(false);
});
});
});