forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_lifecycle.test.ts
More file actions
90 lines (67 loc) · 3.63 KB
/
Copy pathsession_lifecycle.test.ts
File metadata and controls
90 lines (67 loc) · 3.63 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, it } from "vitest";
import { SessionEvent, approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext";
describe("Session Lifecycle", async () => {
const { copilotClient: client } = await createSdkTestContext();
it("should list created sessions after sending a message", async () => {
const session1 = await client.createSession({ onPermissionRequest: approveAll });
const session2 = await client.createSession({ onPermissionRequest: approveAll });
// Sessions must have activity to be persisted to disk
await session1.sendAndWait({ prompt: "Say hello" });
await session2.sendAndWait({ prompt: "Say world" });
// Wait for session data to flush to disk
await new Promise((r) => setTimeout(r, 500));
const sessions = await client.listSessions();
const sessionIds = sessions.map((s) => s.sessionId);
expect(sessionIds).toContain(session1.sessionId);
expect(sessionIds).toContain(session2.sessionId);
await session1.disconnect();
await session2.disconnect();
});
it("should delete session permanently", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const sessionId = session.sessionId;
// Send a message so the session is persisted
await session.sendAndWait({ prompt: "Say hi" });
// Wait for session data to flush to disk
await new Promise((r) => setTimeout(r, 500));
// Verify it appears in the list
const before = await client.listSessions();
expect(before.map((s) => s.sessionId)).toContain(sessionId);
await session.disconnect();
await client.deleteSession(sessionId);
// After delete, the session should not be in the list
const after = await client.listSessions();
expect(after.map((s) => s.sessionId)).not.toContain(sessionId);
});
it("should return events via getMessages after conversation", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
await session.sendAndWait({
prompt: "What is 2+2? Reply with just the number.",
});
const messages = await session.getMessages();
expect(messages.length).toBeGreaterThan(0);
// Should have at least session.start, user.message, assistant.message, session.idle
const types = messages.map((m: SessionEvent) => m.type);
expect(types).toContain("session.start");
expect(types).toContain("user.message");
expect(types).toContain("assistant.message");
await session.disconnect();
});
it("should support multiple concurrent sessions", async () => {
const session1 = await client.createSession({ onPermissionRequest: approveAll });
const session2 = await client.createSession({ onPermissionRequest: approveAll });
// Send to both sessions
const [msg1, msg2] = await Promise.all([
session1.sendAndWait({ prompt: "What is 1+1? Reply with just the number." }),
session2.sendAndWait({ prompt: "What is 3+3? Reply with just the number." }),
]);
expect(msg1?.data.content).toContain("2");
expect(msg2?.data.content).toContain("6");
await session1.disconnect();
await session2.disconnect();
});
});