-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathclient_lifecycle.test.ts
More file actions
57 lines (43 loc) · 2.22 KB
/
client_lifecycle.test.ts
File metadata and controls
57 lines (43 loc) · 2.22 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, it } from "vitest";
import { SessionLifecycleEvent, approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext";
describe("Client Lifecycle", async () => {
const { copilotClient: client } = await createSdkTestContext();
it("should return last session id after sending a message", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
await session.sendAndWait({ prompt: "Say hello" });
// Wait for session data to flush to disk
await new Promise((r) => setTimeout(r, 500));
const lastSessionId = await client.getLastSessionId();
expect(lastSessionId).toBe(session.sessionId);
await session.destroy();
});
it("should return undefined for getLastSessionId with no sessions", async () => {
// On a fresh client this may return undefined or an older session ID
const lastSessionId = await client.getLastSessionId();
expect(lastSessionId === undefined || typeof lastSessionId === "string").toBe(true);
});
it("should emit session lifecycle events", async () => {
const events: SessionLifecycleEvent[] = [];
const unsubscribe = client.on((event: SessionLifecycleEvent) => {
events.push(event);
});
try {
const session = await client.createSession({ onPermissionRequest: approveAll });
await session.sendAndWait({ prompt: "Say hello" });
// Wait for session data to flush to disk
await new Promise((r) => setTimeout(r, 500));
// Lifecycle events may not fire in all runtimes
if (events.length > 0) {
const sessionEvents = events.filter((e) => e.sessionId === session.sessionId);
expect(sessionEvents.length).toBeGreaterThan(0);
}
await session.destroy();
} finally {
unsubscribe();
}
});
});